GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid usage of the non monotonic clock System.currentTimeMillis() in favor of System.nanoTime() by Nateckert · Pull Request #6392 · testcontainers/testcontainers-java

Conversation

@Nateckert

@Nateckert Nateckert commented Jan 4, 2023

Copy link
Copy Markdown

In Java, System.currentTimeMillis() is a non-monotonic clock, which means that

long start = System.currentTimeMillis();
...
long end = System.currentTimeMillis();

System.println.out(end - start);

might print a number smaller than zero !

This is due to the fact that the internal clock of computer if not accurate and it is put back on the "right" time thanks to the UDP protocol.

Additional read: https://superuser.com/questions/759730/how-much-clock-drift-is-considered-normal-for-a-non-networked-windows-7-pc

Instead, System.nanoTime() should be prefer for timing purpose, as it will guarantee that two successive calls to it are greater than zero

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Nateckert
Avoid usage of System.currentTimeMillis()
c65fb6b
@Nateckert
Nateckert requested a review from a team January 4, 2023 17:07
@github-actions github-actions Bot added the modules/toxiproxy label Jan 4, 2023
@Nateckert

Nateckert commented Jan 4, 2023

Copy link
Copy Markdown
Author

The comment https://github.com/testcontainers/testcontainers-java/pull/6392/files#diff-1cb71d0ebd85bd607c3da0c4e693f353e5d0b9c947a2396ffc32042f3ccbb63eL45 is now incorrect, but a good estimate is hard to guess

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Nateckert Nateckert changed the title Avoid usage of the non monotonic clock System.currentTimeMillis() in favor for System.nanoTime() Avoid usage of the non monotonic clock System.currentTimeMillis() in favor of System.nanoTime() Jan 4, 2023
@kiview

kiview commented Jan 5, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Thanks for providing a PR @Nateckert. Is this an issue you have observed in your system while using Testcontainers?

Could you please run ./gradlew :database-commons:spotlessApply to fix the failing CI job?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kiview kiview added the type/enhancement label Jan 5, 2023
Nateckert
Fix formatting
c5b9cac
@Nateckert

Nateckert commented Jan 6, 2023

Copy link
Copy Markdown
Author

Thanks for providing a PR @Nateckert. Is this an issue you have observed in your system while using Testcontainers?

Could you please run ./gradlew :database-commons:spotlessApply to fix the failing CI job?

I ran the formatter.

I actually don't know if we encounter that issue, but we are currently trying to sanitize our CI and this was a quick fix to a unlikely problem that can be almost impossible to debug (looks random, there is no associated logs, ...), so that's why I wanted to contribute

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jtnord
jtnord reviewed Jan 30, 2023
View reviewed changes
Comment thread core/src/main/java/org/testcontainers/containers/output/WaitingConsumer.java Outdated
private void waitUntil(Predicate<OutputFrame> predicate, long expiry, int times) throws TimeoutException {
int numberOfMatches = 0;
while (System.currentTimeMillis() < expiry) {
while (System.nanoTime() < expiry) {

@jtnord jtnord Jan 30, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

nanoTime is subject to overflow/wrapping and the addition of an offset may have wrapped.

Whilst this could have happened with mills you wouldn't care for a few more years. nanoTime is relative to a random point in time so it could be a very large number or a very small number, positive or megative

As per javadoc

To compare two nanoTime values

long t0 = System.nanoTime();
...
long t1 = System.nanoTime();
one should use t1 - t0 < 0, not t1 < t0, because of the possibility of numerical overflow.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Nateckert Nateckert Feb 3, 2023

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Fixed

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Nateckert
Fix time comparison to deal with overflow
a81a8af
@Nateckert

Nateckert commented Feb 3, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

The comment https://github.com/testcontainers/testcontainers-java/pull/6392/files#diff-1cb71d0ebd85bd607c3da0c4e693f353e5d0b9c947a2396ffc32042f3ccbb63eL45 is now incorrect, but a good estimate is hard to guess

With the changes after @jtnord's feedback, I managed to update the estimate

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Nateckert
Merge branch 'main' into fix/clock_to_timer
1fe1c50
kiview
kiview previously approved these changes Mar 6, 2023
View reviewed changes
@kiview
kiview requested a review from eddumelendez March 6, 2023 14:16
@eddumelendez
Merge branch 'main' into fix/clock_to_timer
d6dd0be
@github-actions github-actions Bot added the modules/jdbc label Mar 14, 2024
@eddumelendez
Fix format
5d04f0c
@eddumelendez
eddumelendez dismissed kiview’s stale review via 5d04f0c March 14, 2024 22:58
@eddumelendez eddumelendez added this to the next milestone Mar 14, 2024
eddumelendez
eddumelendez approved these changes Mar 14, 2024
View reviewed changes
@eddumelendez
eddumelendez merged commit 6e5fbe6 into testcontainers:main Mar 14, 2024
@eddumelendez

eddumelendez commented Mar 14, 2024

Copy link
Copy Markdown
Member

Thanks for your contribution, @Nateckert !

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

fokion pushed a commit to fokion/testcontainers-java that referenced this pull request Jan 18, 2025
Nateckert @eddumelendez @fokion
Avoid usage of the non monotonic clock System.currentTimeMillis() in …
9715b32
…favor of System.nanoTime() (testcontainers#6392)

Co-authored-by: Eddú Meléndez Gonzales <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@eddumelendez eddumelendez eddumelendez approved these changes
@kiview kiview kiview left review comments
+1 more reviewer
@jtnord jtnord jtnord left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

modules/jdbc modules/toxiproxy type/enhancement

Projects

None yet

Milestone

1.19.8

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@Nateckert @kiview @eddumelendez @jtnord
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.