From 9d31061d313ebad19da486dee7519ae1780d9d29 Mon Sep 17 00:00:00 2001 From: Manoj Kumar Date: Tue, 1 Dec 2026 15:18:55 +0530 Subject: [PATCH 1/2] Truncate DB date bindings to whole seconds in GenericDaoBase 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. --- .../com/cloud/utils/db/GenericDaoBase.java | 23 +++++++++++++++---- .../cloud/utils/db/GenericDaoBaseTest.java | 21 +++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java b/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java index 3290d7125f2f..2dc91c77f6d4 100644 --- a/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java +++ b/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java @@ -140,6 +140,19 @@ protected static Calendar gmtCalendar() { return Calendar.getInstance(s_gmtTimeZone); } + /** + * Drops the sub-second part of an epoch-millis value. DATETIME/TIME columns have no fractional + * 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) { + return Math.floorDiv(millis, 1000L) * 1000L; + } + + protected static Timestamp wholeSecondTimestamp(long millis) { + return new Timestamp(truncateToSeconds(millis)); + } + /** * Returns the SQL type ({@link Types}) matching the temporal flag of the given attribute, so a * null date/time/timestamp column is bound with the correct type instead of always TIMESTAMP. @@ -1720,7 +1733,7 @@ protected void insertElementCollection(T entity, Attribute idAttribute, ID id, M if (d == null) { pstmt.setNull(1, Types.TIMESTAMP); } else { - pstmt.setTimestamp(1, new Timestamp(d.getTime()), gmtCalendar()); + pstmt.setTimestamp(1, wholeSecondTimestamp(d.getTime()), gmtCalendar()); } } else { pstmt.setObject(1, en.nextElement()); @@ -1831,9 +1844,9 @@ protected void prepareAttribute(final int j, final PreparedStatement pstmt, fina if (attr.is(Attribute.Flag.Date)) { pstmt.setDate(j, new java.sql.Date(date.getTime()), gmtCalendar()); } else if (attr.is(Attribute.Flag.TimeStamp)) { - pstmt.setTimestamp(j, new Timestamp(date.getTime()), gmtCalendar()); + pstmt.setTimestamp(j, wholeSecondTimestamp(date.getTime()), gmtCalendar()); } else if (attr.is(Attribute.Flag.Time)) { - pstmt.setTime(j, new java.sql.Time(date.getTime()), gmtCalendar()); + pstmt.setTime(j, new java.sql.Time(truncateToSeconds(date.getTime())), gmtCalendar()); } } else if (attr.field.getType() == Calendar.class) { final Calendar cal = (Calendar)value; @@ -1844,9 +1857,9 @@ protected void prepareAttribute(final int j, final PreparedStatement pstmt, fina if (attr.is(Attribute.Flag.Date)) { pstmt.setDate(j, new java.sql.Date(cal.getTimeInMillis()), gmtCalendar()); } else if (attr.is(Attribute.Flag.TimeStamp)) { - pstmt.setTimestamp(j, new Timestamp(cal.getTimeInMillis()), gmtCalendar()); + pstmt.setTimestamp(j, wholeSecondTimestamp(cal.getTimeInMillis()), gmtCalendar()); } else if (attr.is(Attribute.Flag.Time)) { - pstmt.setTime(j, new Time(cal.getTimeInMillis()), gmtCalendar()); + pstmt.setTime(j, new Time(truncateToSeconds(cal.getTimeInMillis())), gmtCalendar()); } } else if (attr.field.getType().isEnum()) { final Enumerated enumerated = attr.field.getAnnotation(Enumerated.class); diff --git a/framework/db/src/test/java/com/cloud/utils/db/GenericDaoBaseTest.java b/framework/db/src/test/java/com/cloud/utils/db/GenericDaoBaseTest.java index 41a7be4ab867..dad70222be5d 100644 --- a/framework/db/src/test/java/com/cloud/utils/db/GenericDaoBaseTest.java +++ b/framework/db/src/test/java/com/cloud/utils/db/GenericDaoBaseTest.java @@ -337,6 +337,27 @@ public void testLockOneRandomRowReturnsFirstElement() { Assert.assertEquals(expectedResult, result); } + @Test + public void truncateToSecondsDropsMillisInsteadOfRounding() { + long endOfSecond = 1_795_000_859_999L; + + Assert.assertEquals(1_795_000_859_000L, GenericDaoBase.truncateToSeconds(endOfSecond)); + Assert.assertEquals(1_795_000_859_000L, GenericDaoBase.truncateToSeconds(1_795_000_859_000L)); + } + + @Test + public void truncateToSecondsFloorsNegativeEpochValues() { + Assert.assertEquals(-1000L, GenericDaoBase.truncateToSeconds(-1L)); + } + + @Test + public void wholeSecondTimestampHasNoFractionalPart() { + Timestamp ts = GenericDaoBase.wholeSecondTimestamp(1_795_000_859_999L); + + Assert.assertEquals(1_795_000_859_000L, ts.getTime()); + Assert.assertEquals(0, ts.getNanos()); + } + @Test public void gmtCalendarUsesGmtTimeZone() { Calendar calendar = GenericDaoBase.gmtCalendar(); From 04ace8d69b0a987cb9ad763a650f5020d10c4a8b Mon Sep 17 00:00:00 2001 From: Manoj Kumar Date: Wed, 2 Dec 2026 11:20:25 +0530 Subject: [PATCH 2/2] Move date truncation helpers to SqlDateTimeUtil so their tests run in CI 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. --- .../com/cloud/utils/db/GenericDaoBase.java | 24 +++------- .../utils/db/datetime/SqlDateTimeUtil.java | 41 +++++++++++++++++ .../cloud/utils/db/GenericDaoBaseTest.java | 21 --------- .../db/datetime/SqlDateTimeUtilTest.java | 45 +++++++++++++++++++ 4 files changed, 92 insertions(+), 39 deletions(-) create mode 100644 framework/db/src/main/java/com/cloud/utils/db/datetime/SqlDateTimeUtil.java create mode 100644 framework/db/src/test/java/com/cloud/utils/db/datetime/SqlDateTimeUtilTest.java diff --git a/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java b/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java index 2dc91c77f6d4..500124e8ff12 100644 --- a/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java +++ b/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java @@ -78,6 +78,7 @@ import com.cloud.utils.component.ComponentMethodInterceptable; import com.cloud.utils.crypt.DBEncryptionUtil; import com.cloud.utils.db.SearchCriteria.SelectType; +import com.cloud.utils.db.datetime.SqlDateTimeUtil; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.Ip; import com.cloud.utils.net.NetUtils; @@ -140,19 +141,6 @@ protected static Calendar gmtCalendar() { return Calendar.getInstance(s_gmtTimeZone); } - /** - * Drops the sub-second part of an epoch-millis value. DATETIME/TIME columns have no fractional - * 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) { - return Math.floorDiv(millis, 1000L) * 1000L; - } - - protected static Timestamp wholeSecondTimestamp(long millis) { - return new Timestamp(truncateToSeconds(millis)); - } - /** * Returns the SQL type ({@link Types}) matching the temporal flag of the given attribute, so a * null date/time/timestamp column is bound with the correct type instead of always TIMESTAMP. @@ -1733,7 +1721,7 @@ protected void insertElementCollection(T entity, Attribute idAttribute, ID id, M if (d == null) { pstmt.setNull(1, Types.TIMESTAMP); } else { - pstmt.setTimestamp(1, wholeSecondTimestamp(d.getTime()), gmtCalendar()); + pstmt.setTimestamp(1, SqlDateTimeUtil.toWholeSecondTimestamp(d.getTime()), gmtCalendar()); } } else { pstmt.setObject(1, en.nextElement()); @@ -1844,9 +1832,9 @@ protected void prepareAttribute(final int j, final PreparedStatement pstmt, fina if (attr.is(Attribute.Flag.Date)) { pstmt.setDate(j, new java.sql.Date(date.getTime()), gmtCalendar()); } else if (attr.is(Attribute.Flag.TimeStamp)) { - pstmt.setTimestamp(j, wholeSecondTimestamp(date.getTime()), gmtCalendar()); + pstmt.setTimestamp(j, SqlDateTimeUtil.toWholeSecondTimestamp(date.getTime()), gmtCalendar()); } else if (attr.is(Attribute.Flag.Time)) { - pstmt.setTime(j, new java.sql.Time(truncateToSeconds(date.getTime())), gmtCalendar()); + pstmt.setTime(j, new java.sql.Time(SqlDateTimeUtil.truncateToSeconds(date.getTime())), gmtCalendar()); } } else if (attr.field.getType() == Calendar.class) { final Calendar cal = (Calendar)value; @@ -1857,9 +1845,9 @@ protected void prepareAttribute(final int j, final PreparedStatement pstmt, fina if (attr.is(Attribute.Flag.Date)) { pstmt.setDate(j, new java.sql.Date(cal.getTimeInMillis()), gmtCalendar()); } else if (attr.is(Attribute.Flag.TimeStamp)) { - pstmt.setTimestamp(j, wholeSecondTimestamp(cal.getTimeInMillis()), gmtCalendar()); + pstmt.setTimestamp(j, SqlDateTimeUtil.toWholeSecondTimestamp(cal.getTimeInMillis()), gmtCalendar()); } else if (attr.is(Attribute.Flag.Time)) { - pstmt.setTime(j, new Time(truncateToSeconds(cal.getTimeInMillis())), gmtCalendar()); + pstmt.setTime(j, new Time(SqlDateTimeUtil.truncateToSeconds(cal.getTimeInMillis())), gmtCalendar()); } } else if (attr.field.getType().isEnum()) { final Enumerated enumerated = attr.field.getAnnotation(Enumerated.class); diff --git a/framework/db/src/main/java/com/cloud/utils/db/datetime/SqlDateTimeUtil.java b/framework/db/src/main/java/com/cloud/utils/db/datetime/SqlDateTimeUtil.java new file mode 100644 index 000000000000..a930836844a8 --- /dev/null +++ b/framework/db/src/main/java/com/cloud/utils/db/datetime/SqlDateTimeUtil.java @@ -0,0 +1,41 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.utils.db.datetime; + +import java.sql.Timestamp; + +/** + * Helpers for binding dates to DATETIME/TIME columns, which have no fractional seconds. + * The MySQL driver sends milliseconds to the server, which rounds rather than truncates them + * (e.g. 10:59:59.999 is stored as 11:00:00), so values are cut to whole seconds before binding. + */ +public final class SqlDateTimeUtil { + + private SqlDateTimeUtil() { + } + + /** + * Drops the sub-second part of an epoch-millis value, rounding towards the past. + */ + public static long truncateToSeconds(long millis) { + return Math.floorDiv(millis, 1000L) * 1000L; + } + + public static Timestamp toWholeSecondTimestamp(long millis) { + return new Timestamp(truncateToSeconds(millis)); + } +} diff --git a/framework/db/src/test/java/com/cloud/utils/db/GenericDaoBaseTest.java b/framework/db/src/test/java/com/cloud/utils/db/GenericDaoBaseTest.java index dad70222be5d..41a7be4ab867 100644 --- a/framework/db/src/test/java/com/cloud/utils/db/GenericDaoBaseTest.java +++ b/framework/db/src/test/java/com/cloud/utils/db/GenericDaoBaseTest.java @@ -337,27 +337,6 @@ public void testLockOneRandomRowReturnsFirstElement() { Assert.assertEquals(expectedResult, result); } - @Test - public void truncateToSecondsDropsMillisInsteadOfRounding() { - long endOfSecond = 1_795_000_859_999L; - - Assert.assertEquals(1_795_000_859_000L, GenericDaoBase.truncateToSeconds(endOfSecond)); - Assert.assertEquals(1_795_000_859_000L, GenericDaoBase.truncateToSeconds(1_795_000_859_000L)); - } - - @Test - public void truncateToSecondsFloorsNegativeEpochValues() { - Assert.assertEquals(-1000L, GenericDaoBase.truncateToSeconds(-1L)); - } - - @Test - public void wholeSecondTimestampHasNoFractionalPart() { - Timestamp ts = GenericDaoBase.wholeSecondTimestamp(1_795_000_859_999L); - - Assert.assertEquals(1_795_000_859_000L, ts.getTime()); - Assert.assertEquals(0, ts.getNanos()); - } - @Test public void gmtCalendarUsesGmtTimeZone() { Calendar calendar = GenericDaoBase.gmtCalendar(); diff --git a/framework/db/src/test/java/com/cloud/utils/db/datetime/SqlDateTimeUtilTest.java b/framework/db/src/test/java/com/cloud/utils/db/datetime/SqlDateTimeUtilTest.java new file mode 100644 index 000000000000..b6f65617de99 --- /dev/null +++ b/framework/db/src/test/java/com/cloud/utils/db/datetime/SqlDateTimeUtilTest.java @@ -0,0 +1,45 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.utils.db.datetime; + +import java.sql.Timestamp; + +import org.junit.Assert; +import org.junit.Test; + +public class SqlDateTimeUtilTest { + + @Test + public void truncateToSecondsDropsMillisInsteadOfRounding() { + Assert.assertEquals(1_795_000_859_000L, SqlDateTimeUtil.truncateToSeconds(1_795_000_859_999L)); + Assert.assertEquals(1_795_000_859_000L, SqlDateTimeUtil.truncateToSeconds(1_795_000_859_000L)); + Assert.assertEquals(1_795_000_859_000L, SqlDateTimeUtil.truncateToSeconds(1_795_000_859_001L)); + } + + @Test + public void truncateToSecondsFloorsNegativeEpochValues() { + Assert.assertEquals(-1000L, SqlDateTimeUtil.truncateToSeconds(-1L)); + } + + @Test + public void toWholeSecondTimestampHasNoFractionalPart() { + Timestamp ts = SqlDateTimeUtil.toWholeSecondTimestamp(1_795_000_859_999L); + + Assert.assertEquals(1_795_000_859_000L, ts.getTime()); + Assert.assertEquals(0, ts.getNanos()); + } +}