Fix time issues on Wasm (#2544)

This commit is contained in:
Rushmore Mushambi 2023-08-29 20:52:25 +02:00 committed by GitHub
parent e2cc94b2ad
commit d06ed3ad3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 7 deletions

View file

@ -29,8 +29,12 @@ use std::collections::HashMap;
use std::fmt; use std::fmt;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
#[cfg(not(target_arch = "wasm32"))]
use std::time::{SystemTime, UNIX_EPOCH};
use tracing::instrument; use tracing::instrument;
use tracing::trace; use tracing::trace;
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::{SystemTime, UNIX_EPOCH};
/// Used for cluster logic to move LQ data to LQ cleanup code /// Used for cluster logic to move LQ data to LQ cleanup code
/// Not a stored struct; Used only in this module /// Not a stored struct; Used only in this module
@ -552,9 +556,9 @@ impl Datastore {
// tick is called periodically to perform maintenance tasks. // tick is called periodically to perform maintenance tasks.
// This is called every TICK_INTERVAL. // This is called every TICK_INTERVAL.
pub async fn tick(&self) -> Result<(), Error> { pub async fn tick(&self) -> Result<(), Error> {
let now = std::time::SystemTime::now() let now = SystemTime::now().duration_since(UNIX_EPOCH).map_err(|e| {
.duration_since(std::time::UNIX_EPOCH) Error::Internal(format!("Clock may have gone backwards: {:?}", e.duration()))
.map_err(|e| Error::Internal(e.to_string()))?; })?;
let ts = now.as_secs(); let ts = now.as_secs();
self.tick_at(ts).await?; self.tick_at(ts).await?;
Ok(()) Ok(())

View file

@ -42,8 +42,11 @@ use std::fmt;
use std::fmt::Debug; use std::fmt::Debug;
use std::ops::Range; use std::ops::Range;
use std::sync::Arc; use std::sync::Arc;
#[cfg(not(target_arch = "wasm32"))]
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use uuid::Uuid; use uuid::Uuid;
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::{SystemTime, UNIX_EPOCH};
/// A set of undoable updates and requests against a dataset. /// A set of undoable updates and requests against a dataset.
#[allow(dead_code)] #[allow(dead_code)]
@ -1002,7 +1005,10 @@ impl Transaction {
pub(crate) fn clock(&self) -> Timestamp { pub(crate) fn clock(&self) -> Timestamp {
// Use a timestamp oracle if available // Use a timestamp oracle if available
let now: u128 = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis(); let now: u128 = match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(duration) => duration.as_millis(),
Err(error) => panic!("Clock may have gone backwards: {:?}", error.duration()),
};
Timestamp { Timestamp {
value: now as u64, value: now as u64,
} }

View file

@ -3,7 +3,10 @@
use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Mutex; use std::sync::Mutex;
#[cfg(not(target_arch = "wasm32"))]
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::{SystemTime, UNIX_EPOCH};
use super::{u16_u64_to_versionstamp, u64_to_versionstamp, u64_u16_to_versionstamp, Versionstamp}; use super::{u16_u64_to_versionstamp, u64_to_versionstamp, u64_u16_to_versionstamp, Versionstamp};
@ -144,9 +147,10 @@ fn now() -> Versionstamp {
#[allow(unused)] #[allow(unused)]
// Returns the number of seconds since the Unix Epoch (January 1st, 1970 at UTC). // Returns the number of seconds since the Unix Epoch (January 1st, 1970 at UTC).
fn secs_since_unix_epoch() -> u64 { fn secs_since_unix_epoch() -> u64 {
let start = SystemTime::now(); match SystemTime::now().duration_since(UNIX_EPOCH) {
let since_the_epoch = start.duration_since(UNIX_EPOCH).expect("Time went backwards"); Ok(duration) => duration.as_secs(),
since_the_epoch.as_secs() Err(error) => panic!("Clock may have gone backwards: {:?}", error.duration()),
}
} }
mod tests { mod tests {