-
Notifications
You must be signed in to change notification settings - Fork 636
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
Replace chrono::now() with Instant::now() #5097
Conversation
@bowenwang1996 @mfornet can you take a look? |
https://passcod.name/technical/no-time-for-chrono.html also feels relevant. Can we go as far (in subsequent PRs) as removing |
@matklad If you look at the documentation of Maybe, we should switch back to |
Yeah, that's a good point. To clarify, the doc say that the underlying OS functionality doesn't always live up to its promise, and so, for that reason, on certain platforms Rust's stdlib tries to paper over this in user-space. The problem with that strategy though is that, with virtualization, you don't actually know which platform you are running on top. Eg, x86 Linux implements monotonic time correctly, but if it is run in a VM in windows host, the windows can break things. This was observed in tokio-rs/tokio#3619 and is being fixed in rust-lang/rust#89926. Technically, that is a bug in Linux kernel and/or hypervisor implementations, but this indeed is observed in practice . I don't think using pub fn now() -> Instance {
// furious hand-waving
static LAST: Mutex<Instance> = ...
let now = Instance::now();
let now = now.max(*LAST.lock());
*LAST.lock() = now;
now
} Practically though, it seems that this problem shouldn't be something that we care about. That is, the amount of setups where time goes backwards despite stdlib's mitigations should be petty small, and, if time is unreliable in your setup, you should fix the setup, as we do rely on time significantly. @bowenwang1996 do you remember more details about https://near.zulipchat.com/#narrow/stream/295302-dev-general/topic/general? Was it some production environment, or just some weird ad-hoc setup? |
@matklad The main reason we use For some reason Any ideas?
|
Here is a part of documentation from
It looks like you are meant to use instant just to compare a relative time, which is completely fine, but we need to be able to convert time, for example to unix timestamp and back. That's not what I think we can't simply replace @matklad What do you think? |
@matklad Are you sure the link is correct? It does not link to an issue related to time |
Yeah, that's what I am getting at:
I feel relatively strongly that we need to stick to the simplest model of time we can, that is, to
Yeah, if we need that, we do need SystemTime in those parts that use such functionality. Could you point where we need this, though? I want to understand whether we fundamentally need to know absolute time, or whether this is something which can be replaced by nearcore/core/primitives/src/block_header.rs Lines 29 to 31 in af64bfb
we definitely want timestamp (so, But for, eg nearcore/chain/network-primitives/src/types.rs Lines 657 to 659 in af64bfb
it seems that we want to use |
Oh, while looking at it, I found a case where we very well may panic: nearcore/chain/network/src/peer_store.rs Lines 241 to 245 in af64bfb
Here, the If this code returns an error, we will panic at the call-site, where we unwrap the thing. |
@matklad Some data structures that we store in DB have timestamps. That's the main reason why we can't use instant.
|
This struct is part of our protocol. We need to serialize it and send it over the network.
|
Doh, I suspected something like this, and specifically looked for it, and somehow still missed Yeah, so this is the case where we need
SystemTime does implement https://docs.rs/serde/1.0.130/src/serde/ser/impls.rs.html#611-625 It can implement BorshSerialize as well, although that would need to use So my current understanding is:
|
* Replace chrono::now() with Instant::now() * refactor * refactor Co-authored-by: Piotr Mikulski <[email protected]>
We shouldn't be using non monotonous time with chrono::Now()
Closes #5096
Moving changes out of #5089