Skip to content

Truncate DB date bindings to whole seconds in GenericDaoBase - #14929

Open
sudo87 wants to merge 2 commits into
apache:4.20from
shapeblue:fix-datetime-truncate-millis
Open

sudo87 wants to merge 2 commits into
apache:4.20from
shapeblue:fix-datetime-truncate-millis

Conversation

@sudo87

@sudo87 sudo87 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

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

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • Build/CI
  • Test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Feature/Enhancement Scale

  • Major
  • Minor

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

Screenshots (if appropriate):

How Has This Been Tested?

How did you try to break this feature and the system with this change?

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.
@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 42.85714% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 16.46%. Comparing base (2ebcaf6) to head (04ace8d).
⚠️ Report is 5 commits behind head on 4.20.

Files with missing lines Patch % Lines
...c/main/java/com/cloud/utils/db/GenericDaoBase.java 0.00% 5 Missing ⚠️
...a/com/cloud/utils/db/datetime/SqlDateTimeUtil.java 66.66% 2 Missing and 1 partial ⚠️
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     
Flag Coverage Δ
uitests 4.16% <ø> (-0.01%) ⬇️
unittests 17.33% <42.85%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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) {
@sudo87

sudo87 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

@blueorangutan package

@blueorangutan

Copy link
Copy Markdown

@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.

@Alpha162

Alpha162 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Tested this on a two-host lab running a build of main at 58fda71 (Ubuntu 24.04, MySQL 8.0.46, Connector/J 8.4.0), with the usage server on a 10-minute aggregation range. I swapped only cloud-framework-db in the usage server between runs.

Jar usage_job.end_millis Stored end_date Records ending on :59 listUsageRecords for the job's own period
As packaged (no fix) 00:40:59.999 00:41:00 0 of 151,099 Only the 24 records written by raw SQL (types 21 to 24)
This PR 00:44:59.999 00:44:59 39 of 39 All 39
Same source without the PR, built the same way (control) 00:54:59.999 00:55:00 0 of 39 0 of 39. All 39 appear only with enddate=00:55:00

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 :59, as the issue says, since they're written with raw SQL.

One thing: the three new tests in GenericDaoBaseTest won't run in CI. framework/db/pom.xml excludes com/cloud/utils/db/*Test* from surefire, so the default build skips the whole class. They pass when run by hand (-Dtest=GenericDaoBaseTest#truncateToSeconds*+wholeSecondTimestamp*). Moving them to a class outside that package would get them run.

@blueorangutan

Copy link
Copy Markdown

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 20260

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.
@sudo87

sudo87 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants