-
Notifications
You must be signed in to change notification settings - Fork 1k
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
DateTime Concerns #846
Comments
I think we should just switch to UTC on those calls - since we're really only using the current time to measure elapsed time, right? |
@Aaronontheweb yepp |
That would probably be the simplest thing to do to avoid DST issues. |
Though - you may also want to consider what to do with existing data. If the value is persisted anywhere, then switching to UTC would mean possibly comparing the previous local data with the new UTC value. If that's something you're concerned with, the |
@mj1856 I'll take a deeper look at the code base, but most of those calls appear to be for things that are part of the ephemeral state of an Akka.NET application as it runs in-memory. Scheduled recurring messages and so forth. I'll look closely to see if the local clock gets accidentally exposed to the end-user anywhere they might need to store something. But the TL;DR; of it is that I think we'll probably be ok just switching it. |
If current time is used for internal scheduling purposes, anything that uses current UTC/Local time is unreliable. System time can be arbitrary changed by anything. So, it's better to use GetTickCount64 (CLOCK_MONOTONIC for *nix) which never goes backward and increments monotonically. |
I've checked referencesource and Mono sources. |
Mono's Stopwatch and Environment.TickCount use CLOCK_MONOTONIC. So it's safe to just use |
All time related calls should go through So for stopwatch we could add a |
I seem to have mixed up two concepts here. :) |
@kekekeks Looking at your PR https://github.com/akkadotnet/akka.net/pull/876/files#diff-acc01fe0c12b1eaa67908bf90766ac05R7. |
We should definitely switch to use a monotonic clock instead of subtracting
We have around 30 places where we use No uses of I'd like to think about this for a while and try different ideas I have. |
@kekekeks - That article is 12 years old. Are you sure it's still accurate? I'm not sure about the perf cost on modern hardware. Some of it is likely true still, but not sure about the cost overhead. According to this post, Still, any of those are better than using I've actually never used |
It's monotonic clock. It ticks from some unspecified point in the past monotonically. You can't compare its value from different machines. The only guarantee is that it never goes backward and increments monotonically.
People usually use |
For our purposes, I don't think this is necessary - all of the scheduling in Akka.NET is done locally. Just needs to happen on regular intervals, not at specific dates and times. As for time-ordered network events, our built-in ones use vector clocks that don't take any dependencies on the CLR time system: https://github.com/akkadotnet/akka.net/blob/dev/src/core/Akka.Cluster/VectorClock.cs |
A little late for the discussion... I completely agree on moving from DateTime to a monotonic clock. But does akka.net spend so much time requesting the current time that it requires worrying about the performance of QueryPerformanceCounter? I can call this method 8 million times/second on my laptop on debug mode attached to visual studio with energy savings on. This goes to 20 million/second on release mode... From what I measured, it's ~4 times slower than DateTime.UtcNow, and ~8 times slower than Environment.TickCount. But should it matter? All I'm saying is that maybe we should first see what is really required. Does Akka.net need nanosecond precision? Then Stopwatch is the best answer. If it's used for scheduling, maybe For reference, TPL uses Timers, which use Environment.TickCount until Windows 7, that means you can't schedule tasks for more than 29 days anyway. If it works for TPL, should work for Akka. Apparently the Mono people agree. |
@nvivo the performance is less of a concern than DST / users tampering with the clock IMHO.
The design concerns that the TPL and Akka.NET have overlap by only a very small amount. Discuss designs on their own merits; not by appealing to the authority of another library. Stick to what's important for Akka. In this case - raises a question I hadn't thought about: what mechanism is most effective for ensuring consistent scheduling over very long periods of time? Wrap TickCount in an Users should be able to have long-running actors that run for months without restart - and they should be able to use a scheduler that entire time. |
Looks like the answer to my question lies in the comments on https://msdn.microsoft.com/en-us/library/windows/desktop/ms724411%28v=vs.85%29.aspx
Sounds like @kekekeks's PR should work for that scenario then. |
Just a side note, the int32 part shouldn't affect anything in a system running for more than 29 or 47 days. But that's a good solution anyway. |
Can this be closed? we did merge the Monotonic clock PR |
Motonic clock is yet to be integrated into the code base. |
I think we can close this. |
In many places throughout the code the current time is retrieved by calling
DateTime.Now
.For example, here are just a few of the lines I found:
This is not a good practice, as it means that code could fail during daylight saving time transitions.
DateTime.Now
returns the local date and time based on the time zone setting of the computer that the code is running on, however it does not take that time zone into account when doing math.There are few valid choices for remedies:
DateTime.UtcNow
.DateTimeOffset
values instead ofDateTime
values. The fields and properties would have to change, and the current time would come fromDateTimeOffset.Now
.Instant
,OffsetDateTime
, orZonedDateTime
, depending on exactly what purpose they serve.See also the following articles on my blog:
The text was updated successfully, but these errors were encountered: