Skip to content

Set clock precision to milliseconds for Datetime->Instant migration#2999

Merged
CydeWeys merged 1 commit intogoogle:masterfrom
CydeWeys:datetime2-clock-precision
Apr 3, 2026
Merged

Set clock precision to milliseconds for Datetime->Instant migration#2999
CydeWeys merged 1 commit intogoogle:masterfrom
CydeWeys:datetime2-clock-precision

Conversation

@CydeWeys
Copy link
Copy Markdown
Member

@CydeWeys CydeWeys commented Mar 31, 2026

Our existing precision is milliseconds so we want to stick with that for Instants. If we want to increase the precision globally after that we can do so all in one go post-migration, but for now, it would be a bad thing to have mixed precision going on just depending on whether a class happens to be migrated yet or not.

This PR also migrates all existing DateTime.nowUtc() calls to use the Clock interface, so that when they are migrated they will get the benefit of this precision-setting as well.

BUG= http://b/496985355


This change is Reviewable

@CydeWeys CydeWeys requested a review from gbrodman March 31, 2026 21:49
AtomicInteger hardDeletedDomains = new AtomicInteger();
AtomicReference<ImmutableList<Domain>> domainsBatch = new AtomicReference<>();
DateTime startTime = DateTime.now(UTC);
DateTime startTime = clock.nowUtc();

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
Clock.nowUtc
should be avoided because it has been deprecated.

// Automatically kill the job if it is running for over 20 hours
} while (DateTime.now(UTC).isBefore(startTime.plusHours(20))
} while (clock.nowUtc().isBefore(startTime.plusHours(20))

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
Clock.nowUtc
should be avoided because it has been deprecated.
updateLastNotificationSentDate(
registrar,
DateTime.now(UTC).minusMinutes((int) UPDATE_TIME_OFFSET.getStandardMinutes()),
clock.nowUtc().minusMinutes((int) UPDATE_TIME_OFFSET.getStandardMinutes()),

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
Clock.nowUtc
should be avoided because it has been deprecated.

public Builder timeToLive(Duration duration) {
this.table.setExpirationTime(DateTime.now(UTC).plus(duration).getMillis());
this.table.setExpirationTime(clock.nowUtc().plus(duration).getMillis());

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
Clock.nowUtc
should be avoided because it has been deprecated.
consoleApiParams
.response()
.setDateHeader("Expires", DateTime.now(UTC).withTimeAtStartOfDay().plusDays(1));
.setDateHeader("Expires", clock.nowUtc().withTimeAtStartOfDay().plusDays(1));

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
Clock.nowUtc
should be avoided because it has been deprecated.
keyGen.generateKeyPair(), DEFAULT_ISSUER_FQDN, DEFAULT_NOT_BEFORE, DEFAULT_NOT_AFTER);
keyGen.generateKeyPair(),
DEFAULT_ISSUER_FQDN,
clock.nowUtc().minusHours(1),

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
Clock.nowUtc
should be avoided because it has been deprecated.
keyGen.generateKeyPair(),
DEFAULT_ISSUER_FQDN,
clock.nowUtc().minusHours(1),
clock.nowUtc().plusDays(1));

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
Clock.nowUtc
should be avoided because it has been deprecated.
public static SelfSignedCaCertificate create(String fqdn) throws Exception {
return create(fqdn, DEFAULT_NOT_BEFORE, DEFAULT_NOT_AFTER);
public static SelfSignedCaCertificate create(String fqdn, Clock clock) throws Exception {
return create(fqdn, clock.nowUtc().minusHours(1), clock.nowUtc().plusDays(1));

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
Clock.nowUtc
should be avoided because it has been deprecated.
public static SelfSignedCaCertificate create(String fqdn) throws Exception {
return create(fqdn, DEFAULT_NOT_BEFORE, DEFAULT_NOT_AFTER);
public static SelfSignedCaCertificate create(String fqdn, Clock clock) throws Exception {
return create(fqdn, clock.nowUtc().minusHours(1), clock.nowUtc().plusDays(1));

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
Clock.nowUtc
should be avoided because it has been deprecated.
@CydeWeys CydeWeys force-pushed the datetime2-clock-precision branch 3 times, most recently from 6334a05 to 07fae7b Compare April 3, 2026 15:32
@CydeWeys CydeWeys requested a review from ptkach April 3, 2026 15:32
@CydeWeys CydeWeys force-pushed the datetime2-clock-precision branch from 07fae7b to fc829f4 Compare April 3, 2026 15:59
Copy link
Copy Markdown
Collaborator

@gbrodman gbrodman left a comment

Choose a reason for hiding this comment

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

if we're trying to get rid of all the calls to DateTime.now() except in the special allowed Clock classes, we should add a presubmit check forbidding that except in those special cases

@gbrodman reviewed 56 files and all commit messages, and made 5 comments.
Reviewable status: all files reviewed, 21 unresolved discussions (waiting on CydeWeys and ptkach).


core/src/main/java/google/registry/model/tld/label/PremiumListDao.java line 132 at r4 (raw file):

  public static PremiumList save(String name, CurrencyUnit currencyUnit, List<String> inputData) {
    checkArgument(!inputData.isEmpty(), "New premium list data cannot be empty");
    return tm().transact(

need to use reTransact or refactor things to avoid nested transactions


core/src/test/java/google/registry/model/tld/label/PremiumListUtilsTest.java line 31 at r4 (raw file):

class PremiumListUtilsTest {

  private static final DateTime SAMPLE_TIME = DateTime.now(UTC);

use a constant instead of "now" to avoid potential random test failure as time goes on


core/src/test/java/google/registry/reporting/icann/IcannReportingStagerTest.java line 46 at r4 (raw file):

class IcannReportingStagerTest {

  private final FakeClock clock = new FakeClock(DateTime.now(UTC));

ditto comment about using a static time rather than a variable one


util/src/test/java/google/registry/util/PosixTarHeaderTest.java line 206 at r4 (raw file):

            .setName("(◕‿◕).txt")
            .setSize(31337)
            .setMtime(DateTime.now(DateTimeZone.UTC))

we should probably use a clock in this test class too

@CydeWeys CydeWeys force-pushed the datetime2-clock-precision branch from fc829f4 to 4f440ec Compare April 3, 2026 17:22
Copy link
Copy Markdown
Collaborator

@gbrodman gbrodman left a comment

Choose a reason for hiding this comment

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

@gbrodman reviewed 9 files and all commit messages.
Reviewable status: all files reviewed, 21 unresolved discussions (waiting on CydeWeys and ptkach).

return String.format(
"prober-%s-%d-%d",
"localhost", System.currentTimeMillis(), clientIdSuffix.incrementAndGet());
"localhost", clock.nowUtc().getMillis(), clientIdSuffix.incrementAndGet());

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
Clock.nowUtc
should be avoided because it has been deprecated.
Copy link
Copy Markdown
Member Author

@CydeWeys CydeWeys left a comment

Choose a reason for hiding this comment

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

@CydeWeys made 1 comment.
Reviewable status: all files reviewed, 22 unresolved discussions (waiting on gbrodman and ptkach).


util/src/test/java/google/registry/util/PosixTarHeaderTest.java line 206 at r4 (raw file):

Previously, gbrodman wrote…

we should probably use a clock in this test class too

It doesn't really make sense to use a FakeClock instance solely for the purpose of getting the time in tests. A static method that handles the millisecond-chopping should be sufficient. Can do that in a follow-up. (There are a lot of calls of DateTime.now() in tests).

@CydeWeys CydeWeys force-pushed the datetime2-clock-precision branch from 4f440ec to 968c9ac Compare April 3, 2026 17:44
Copy link
Copy Markdown
Member Author

@CydeWeys CydeWeys left a comment

Choose a reason for hiding this comment

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

Seems right for a follow-up. Also it's more than just DateTime.now() -- it's in particular things like Instant.now() and the equivalent ZoneDateTime method, as those are the ones that include finer precision by default.

@CydeWeys made 1 comment.
Reviewable status: 62 of 64 files reviewed, 22 unresolved discussions (waiting on gbrodman and ptkach).

Copy link
Copy Markdown
Member Author

@CydeWeys CydeWeys left a comment

Choose a reason for hiding this comment

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

Seems right for a follow-up. Also it's more than just DateTime.now() -- it's in particular things like Instant.now() and the equivalent ZoneDateTime method, as those are the ones that include finer precision by default.

@CydeWeys made 1 comment.
Reviewable status: 62 of 64 files reviewed, 22 unresolved discussions (waiting on gbrodman and ptkach).

Copy link
Copy Markdown
Member Author

@CydeWeys CydeWeys left a comment

Choose a reason for hiding this comment

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

Seems right for a follow-up. Also it's more than just DateTime.now() -- it's in particular things like Instant.now() and the equivalent ZoneDateTime method, as those are the ones that include finer precision by default.

@CydeWeys made 1 comment.
Reviewable status: 62 of 64 files reviewed, 22 unresolved discussions (waiting on gbrodman and ptkach).

@CydeWeys CydeWeys force-pushed the datetime2-clock-precision branch from 968c9ac to 565a39e Compare April 3, 2026 17:59
Copy link
Copy Markdown
Member Author

@CydeWeys CydeWeys left a comment

Choose a reason for hiding this comment

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

@CydeWeys made 1 comment.
Reviewable status: 62 of 64 files reviewed, 22 unresolved discussions (waiting on gbrodman and ptkach).


core/src/main/java/google/registry/model/tld/label/PremiumListDao.java line 132 at r4 (raw file):

Previously, gbrodman wrote…

need to use reTransact or refactor things to avoid nested transactions

Fixed.

Copy link
Copy Markdown
Member Author

@CydeWeys CydeWeys left a comment

Choose a reason for hiding this comment

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

@CydeWeys made 2 comments.
Reviewable status: 60 of 64 files reviewed, 22 unresolved discussions (waiting on gbrodman and ptkach).


core/src/test/java/google/registry/model/tld/label/PremiumListUtilsTest.java line 31 at r4 (raw file):

Previously, gbrodman wrote…

use a constant instead of "now" to avoid potential random test failure as time goes on

Done.


core/src/test/java/google/registry/reporting/icann/IcannReportingStagerTest.java line 46 at r4 (raw file):

Previously, gbrodman wrote…

ditto comment about using a static time rather than a variable one

Done.

Copy link
Copy Markdown
Member Author

@CydeWeys CydeWeys left a comment

Choose a reason for hiding this comment

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

PTAL

@CydeWeys made 1 comment.
Reviewable status: 60 of 64 files reviewed, 22 unresolved discussions (waiting on gbrodman and ptkach).

@CydeWeys CydeWeys force-pushed the datetime2-clock-precision branch from 565a39e to 7869a7d Compare April 3, 2026 18:09
tm().assertInTransaction();
return save(
PremiumListUtils.parseToPremiumList(
name, currencyUnit, inputData, tm().getTransactionTime()));

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
TransactionManager.getTransactionTime
should be avoided because it has been deprecated.
Copy link
Copy Markdown
Collaborator

@gbrodman gbrodman left a comment

Choose a reason for hiding this comment

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

@gbrodman reviewed 4 files and all commit messages, and resolved 4 discussions.
Reviewable status: all files reviewed, 19 unresolved discussions (waiting on CydeWeys and ptkach).

@CydeWeys CydeWeys enabled auto-merge April 3, 2026 18:45
@CydeWeys CydeWeys removed the request for review from ptkach April 3, 2026 19:01
@CydeWeys CydeWeys force-pushed the datetime2-clock-precision branch 2 times, most recently from afcc312 to ded9fa3 Compare April 3, 2026 19:33
Our existing precision is milliseconds so we want to stick with that for
Instants. If we want to increase the precision globally after that we can do so
all in one go post-migration, but for now, it would be a bad thing to have mixed
precision going on just depending on whether a class happens to be migrated yet
or not.

This PR also migrates all existing DateTime.nowUtc() calls to use the Clock
interface, so that when they are migrated they will get the benefit of this
precision-setting as well.

BUG= http://b/496985355
@CydeWeys CydeWeys force-pushed the datetime2-clock-precision branch from ded9fa3 to b377f41 Compare April 3, 2026 19:40
@CydeWeys
Copy link
Copy Markdown
Member Author

CydeWeys commented Apr 3, 2026

PTAL, had to make some more changes related to transaction-handling because of tm() removals in PremiumListDao.

Copy link
Copy Markdown
Collaborator

@gbrodman gbrodman left a comment

Choose a reason for hiding this comment

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

@gbrodman reviewed 8 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 20 unresolved discussions (waiting on CydeWeys).


core/src/test/java/google/registry/testing/DatabaseHelper.java line 349 at r9 (raw file):

    // unit tests.
    tm().reTransact(tm()::allocateId);
    tm().transact(() -> PremiumListDao.save(premiumList));

we probably want reTransact here and, if they exist, any other test-library places

Copy link
Copy Markdown
Member Author

@CydeWeys CydeWeys left a comment

Choose a reason for hiding this comment

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

@CydeWeys made 1 comment.
Reviewable status: all files reviewed, 20 unresolved discussions (waiting on gbrodman).


core/src/test/java/google/registry/testing/DatabaseHelper.java line 349 at r9 (raw file):

Previously, gbrodman wrote…

we probably want reTransact here and, if they exist, any other test-library places

I'm actually not sure we do. (And the existing reTransact() call around allocateId should perhaps just be normal transact). We don't typically have a pattern in test classes of large wrapping transactions around multiple things happening, and none of the callers to persistPremiumList() are wrapping it in a transaction.

Copy link
Copy Markdown
Collaborator

@gbrodman gbrodman left a comment

Choose a reason for hiding this comment

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

:lgtm:

@gbrodman made 1 comment and resolved 1 discussion.
Reviewable status: all files reviewed, 19 unresolved discussions (waiting on CydeWeys).

@CydeWeys CydeWeys added this pull request to the merge queue Apr 3, 2026
Merged via the queue into google:master with commit 49f14b5 Apr 3, 2026
9 of 10 checks passed
@CydeWeys CydeWeys deleted the datetime2-clock-precision branch April 3, 2026 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants