Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time unit of Expiry#expireAfterCreate currentTime #441

Closed
Azbesciak opened this issue Aug 3, 2020 · 4 comments
Closed

Time unit of Expiry#expireAfterCreate currentTime #441

Azbesciak opened this issue Aug 3, 2020 · 4 comments

Comments

@Azbesciak
Copy link

Azbesciak commented Aug 3, 2020

Hi,
I am trying to set expiry time of an entry depending of the current time and entry time.
I want the solution to be testable, so I wanted to use provided in arguments current time.
It is said in doc that currentTime is in nano

but, I am checking that with the following code:

override fun expireAfterCreate(key: TrafficFeedRequest, value: TrafficFeed, currentTime: Long): Long {
   println("expireAfterCreate\n currentTime: $currentTime\n currentTime (system): ${System.currentTimeMillis()}\n current time in nanos (system) ${TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis())}")
   return 1000
}

and the result is
image
as you see currentTime in args is 17701871302300, taking system current time in nanos is 1596437734903000000, which is about 90k times more.
So, what is the real unit? To be honest, I did not debug it internaly. Hovewer, I am using the standard ticker you provided, which seems to use nanoTime, so in general the question is - how to externalize it, and is it possible without any workarounds on the Ticker?.

Thanks

@ben-manes
Copy link
Owner

System.nanoTime() and System.currentTimeMillis() can return very different results, as they represent different clock sources. The milliTime is wall-clock based, whereas nanoTime is machine specific. You cannot compare the two time sources in any meaningful way. If you use jshell and play with them, you'll see they do not offer equivalent values.

We only use relative time and assume time is monatomic, so nanoTime is the right fit. If you need to calculate the duration based on wall clock time, e.g. offset from an external resource's calendar time, then you'll need to obtain that reading yourself. For example if you cache geocode results for 30 days in a db, then you'll need to take use the wall clock time with the creation_at timestamp to determine the row's lifetime. Since the system clock may be adjusted at any time, it can go backwards unlike nanoTime.

For testing you can use Guava's FakeTicker. If you need wall clock time, then use java.time.Clock for mockability.

@Azbesciak
Copy link
Author

Yes, nanoTime is useful only to measure delta.
So in general, what is the point of using this currentTime?

@ben-manes
Copy link
Owner

currentTime is the ticker's reading of nanoTime. It is provided for convenience if a user did want nanoTime, so as to avoid re-reading it, but is most often an ignorable parameter.

@Azbesciak
Copy link
Author

Ok, thanks!

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

No branches or pull requests

2 participants