Repository navigation
Truncate DB date bindings to whole seconds in GenericDaoBase - #14929
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 4.20 #14929 +/- ##
============================================
+ Coverage 16.44% 16.46% +0.01%
- Complexity 13687 13715 +28
============================================
Files 5669 5671 +2
Lines 501651 501780 +129
Branches 60944 60965 +21
============================================
+ Hits 82491 82606 +115
- Misses 409920 409924 +4
- Partials 9240 9250 +10
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟡 Changes recommended
An active documentation gap remains because the smoke integration-suite is still in progress and its completed results, including relevant job outcomes, have not been added to the PR description.
1 open issue
What changed in this PR
This pull request fixes date-boundary regressions caused by MySQL rounding millisecond values when GenericDaoBase binds temporal values to whole-second database columns. Temporal bindings are truncated to whole seconds before persistence.
Changes:
- Added floor-based epoch-millisecond truncation and whole-second timestamp conversion.
- Applied truncation to timestamp and time bindings for dates, calendars, and date element collections.
- Added tests covering millisecond removal, negative epoch values, and timestamp precision.
| File | Description |
|---|---|
| framework/db/src/test/java/com/cloud/utils/db/GenericDaoBaseTest.java | Adds tests for whole-second truncation, negative epoch values, and timestamp precision behavior. |
| framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java | Truncates DAO-bound timestamp and time values to whole seconds before JDBC binding. |
Review effort: Lite
| * seconds, and the MySQL driver sends them to the server, which rounds rather than truncates | ||
| * (e.g. 10:59:59.999 is stored as 11:00:00), so values are cut to whole seconds before binding. | ||
| */ | ||
| protected static long truncateToSeconds(long millis) { |
|
@blueorangutan package |
|
@sudo87 a [SL] Jenkins job has been kicked to build packages. It will be bundled with KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress. |
|
Tested this on a two-host lab running a build of
So the fix restores the stored values, and the control rules out the build itself. In the unpatched run, types 21 to 24 still ended on One thing: the three new tests in |
|
Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 20260 |
|
Thank you @Alpha162 for testing out the patch. Good catch on the tests, I moved the helpers and tests into new class so that those are run in CI. |
|
@blueorangutan package |
|
@vladimirpetrov a [SL] Jenkins job has been kicked to build packages. It will be bundled with KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress. |
|
Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 20265 |
|
Thanks @sudo87 - let's rebase this fix to 4.22 branch and we can then backport the fix to branch 4.20 cc @sureshanaparti @vladimirpetrov |
Since the move to setTimestamp()/setTime() in GenericDaoBase, Connector/J sends the milliseconds and MySQL rounds them into DATETIME(0) columns instead of dropping them. A value such as 10:59:59.999 is now stored as 11:00:00. The usage job computes period end as periodEnd - 1ms, so every usage record and usage_job end_date lands on the next period boundary and the last day of a listUsageRecords query is missed. Any other DATETIME written through the DAO can also be up to 0.5s in the future. Cut the sub-second part before binding Date and Calendar values as timestamps or times, which restores the previous stored values.
framework-db excludes com/cloud/utils/db/*Test* from surefire, so the tests added for the truncation helpers in GenericDaoBaseTest never ran. Put the helpers in com.cloud.utils.db.datetime, which is not excluded, and test them there.
04ace8d to
ada605e
Compare
|
@blueorangutan package |
|
@nvazquez a [SL] Jenkins job has been kicked to build packages. It will be bundled with KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress. |
|
Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✖️ suse15. SL-JID 20269 |
vladimirpetrov
left a comment
There was a problem hiding this comment.
LGTM
Tested this on a 4.22.2.0-SNAPSHOT build with the patch: KVM, Advanced zone, 1 MS on Oracle Linux 9, MySQL 8.0.46, serverTimezone=UTC, no TIME_TRUNCATE_FRACTIONAL in sql_mode. The usage server ran with usage.stats.job.aggregation.range=10.
The management and usage server jars both contain SqlDateTimeUtil, and GenericDaoBase is identical in both (5 calls, no un-truncated new Timestamp(...) left). As a baseline, the same MySQL rounds '…14:29:59.999' and '….500' to 14:30:00 on insert, so it is affected without the fix.
| Job | end_millis |
Stored end_date |
Records | listUsageRecords for that period |
|---|---|---|---|---|
| 1 | 14:29:59.999 |
14:29:59 |
18 | 18 |
| 2 | 14:39:59.999 |
14:39:59 |
17 | 17 |
| 3 | 14:49:59.999 |
14:49:59 |
12 | 12 |
| 4 | 14:59:59.999 |
14:59:59 |
12 | 12 |
- All 59 records (types 1, 2, 3, 6, 9, 12, 13, 30) end on
:x9:59. None end on the next period boundary. - A date-only
listUsageRecords startdate=<today> enddate=<today>returns all 59. - Consecutive jobs no longer overlap:
14:29:59→14:30:00. Before the fix, the stored end equalled the next job's start. - Other DAO date paths look fine:
- VM deploy/stop/start/reboot/expunge store sane
created/update_time/removedvalues. listEventsstartdate/enddatefilters are exact to the second and inclusive at both ends.
- VM deploy/stop/start/reboot/expunge store sane
- No errors in the management or usage logs during the run.
For comparison, on 4.22.2.0-RC2 without this fix, end_millis …:59.999 was stored as …:00, and the period query returned 0 records (22 only when enddate was the next boundary).
Not covered: daily/hourly aggregation over a real boundary (same periodEnd - 1 ms logic), the raw-SQL network/disk usage types (unchanged by this PR; the VR reported 0 bytes in my run), and upgrades with records already stored at :00.
|
@blueorangutan test |
1 similar comment
|
@blueorangutan test |

Description
Fixes #14928
GenericDaoBase now binds dates with setTimestamp()/setTime(), so the MySQL driver sends milliseconds and the server rounds them into DATETIME columns 10:59:59.999 is stored as 11:00:00.
The usage job uses periodEnd - 1ms as the end date, so usage records now get the next period's boundary as end_date and the last day of a listUsageRecords query is missed. Other DAO-written dates can also end up up to 0.5s in the future.
Truncate to whole seconds before binding, which restores the old stored values.
Types of changes
Feature/Enhancement Scale or Bug Severity
Feature/Enhancement Scale
Bug Severity
Screenshots (if appropriate):
How Has This Been Tested?
How did you try to break this feature and the system with this change?