use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
use crate::named::Named;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Instant {
pub physical_ms: u64,
pub logical: u16,
}
impl Instant {
#[must_use]
pub fn new(physical_ms: u64, logical: u16) -> Self {
Self {
physical_ms,
logical,
}
}
#[must_use]
pub const fn zero() -> Self {
Self {
physical_ms: 0,
logical: 0,
}
}
#[must_use]
pub const fn from_ms(physical_ms: u64) -> Self {
Self {
physical_ms,
logical: 0,
}
}
#[must_use]
pub fn to_packed(&self) -> u64 {
let phys = self.physical_ms & ((1u64 << 48) - 1);
(phys << 16) | u64::from(self.logical)
}
#[must_use]
pub fn from_packed(packed: u64) -> Self {
let logical = (packed & 0xFFFF) as u16;
let physical_ms = packed >> 16;
Self {
physical_ms,
logical,
}
}
#[must_use]
pub fn to_bytes(&self) -> [u8; 8] {
self.to_packed().to_be_bytes()
}
#[must_use]
pub fn causally_after(&self, other: &Self) -> bool {
(self.physical_ms, self.logical) > (other.physical_ms, other.logical)
}
#[must_use]
pub fn tick(now: Self, previous: Self) -> Self {
if now.causally_after(&previous) || now == previous {
if now == previous {
Self {
physical_ms: now.physical_ms,
logical: now.logical.saturating_add(1),
}
} else {
now
}
} else {
Self {
physical_ms: previous.physical_ms,
logical: previous.logical.saturating_add(1),
}
}
}
}
impl PartialOrd for Instant {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Instant {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
(self.physical_ms, self.logical).cmp(&(other.physical_ms, other.logical))
}
}
impl std::fmt::Display for Instant {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}.{:05}", self.physical_ms, self.logical)
}
}
crate::impl_fingerprint!(Instant);
pub trait Clock: Named + Send + Sync {
fn now(&self) -> Instant;
fn tick(&self, observed: Instant) -> Instant {
let now = self.now();
Instant::tick(now.max(observed), observed)
}
fn unix_secs(&self) -> u64 {
self.now().physical_ms / 1000
}
fn unix_ms(&self) -> u64 {
self.now().physical_ms
}
}
#[derive(Default, Clone, Copy)]
pub struct WallClock;
crate::define_named!(WallClock, "wall");
impl Clock for WallClock {
fn now(&self) -> Instant {
let physical_ms = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |d| u64::try_from(d.as_millis()).unwrap_or(u64::MAX));
Instant {
physical_ms,
logical: 0,
}
}
}
pub struct FrozenClock {
physical_ms: AtomicU64,
logical: AtomicU64, }
crate::define_named!(FrozenClock, "frozen");
impl FrozenClock {
#[must_use]
pub fn at(physical_ms: u64) -> Self {
Self {
physical_ms: AtomicU64::new(physical_ms),
logical: AtomicU64::new(0),
}
}
pub fn advance(&self, ms: u64) {
self.physical_ms.fetch_add(ms, Ordering::SeqCst);
self.logical.store(0, Ordering::SeqCst);
}
pub fn tick_logical(&self) {
self.logical.fetch_add(1, Ordering::SeqCst);
}
pub fn reset(&self) {
self.physical_ms.store(0, Ordering::SeqCst);
self.logical.store(0, Ordering::SeqCst);
}
}
impl Default for FrozenClock {
fn default() -> Self {
Self::at(0)
}
}
impl Clock for FrozenClock {
fn now(&self) -> Instant {
Instant {
physical_ms: self.physical_ms.load(Ordering::SeqCst),
logical: u16::try_from(self.logical.load(Ordering::SeqCst)).unwrap_or(u16::MAX),
}
}
}
#[derive(Default)]
pub struct LogicalClock {
counter: AtomicU64,
}
crate::define_named!(LogicalClock, "logical");
impl LogicalClock {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn peek(&self) -> u64 {
self.counter.load(Ordering::SeqCst)
}
}
impl Clock for LogicalClock {
fn now(&self) -> Instant {
let n = self.counter.fetch_add(1, Ordering::SeqCst);
Instant {
physical_ms: 0,
logical: (n & 0xFFFF) as u16,
}
}
}
pub struct HlcClock {
wall: WallClock,
last: std::sync::Mutex<Instant>,
}
crate::define_named!(HlcClock, "hlc");
impl Default for HlcClock {
fn default() -> Self {
Self::new()
}
}
impl HlcClock {
#[must_use]
pub fn new() -> Self {
let wall = WallClock;
let initial = wall.now();
Self {
wall,
last: std::sync::Mutex::new(initial),
}
}
}
impl Clock for HlcClock {
fn now(&self) -> Instant {
let wall_now = self.wall.now();
let mut last = self.last.lock().unwrap();
*last = Instant::tick(wall_now, *last);
*last
}
fn tick(&self, observed: Instant) -> Instant {
let wall_now = self.wall.now();
let mut last = self.last.lock().unwrap();
let candidate = wall_now.max(observed).max(*last);
*last = if candidate == *last || candidate == observed || candidate == wall_now {
Instant {
physical_ms: candidate.physical_ms,
logical: candidate.logical.saturating_add(1),
}
} else {
candidate
};
*last
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn instant_total_order_lexicographic() {
let a = Instant::new(100, 5);
let b = Instant::new(100, 6);
let c = Instant::new(101, 0);
assert!(b > a);
assert!(c > b);
assert!(c > a);
}
#[test]
fn instant_zero_is_minimal() {
assert!(Instant::new(1, 0) > Instant::zero());
assert!(Instant::new(0, 1) > Instant::zero());
}
#[test]
fn instant_from_ms_yields_logical_zero() {
let i = Instant::from_ms(42_000);
assert_eq!(i.physical_ms, 42_000);
assert_eq!(i.logical, 0);
assert_eq!(i, Instant::new(42_000, 0));
}
#[test]
fn instant_from_ms_zero_equals_zero() {
assert_eq!(Instant::from_ms(0), Instant::zero());
}
#[test]
fn instant_pack_unpack_round_trip() {
for i in [
Instant::zero(),
Instant::new(1, 0),
Instant::new(0, 1),
Instant::new(1000, 5),
Instant::new(u64::MAX >> 16, u16::MAX),
] {
assert_eq!(Instant::from_packed(i.to_packed()), i);
}
}
#[test]
fn instant_to_bytes_is_eight() {
let i = Instant::new(100, 5);
assert_eq!(i.to_bytes().len(), 8);
}
#[test]
fn instant_causally_after_strict() {
let a = Instant::new(10, 0);
let b = Instant::new(10, 0);
assert!(!a.causally_after(&b));
assert!(!b.causally_after(&a));
}
#[test]
fn instant_tick_advances_wall_clock_when_strictly_after() {
let prev = Instant::new(100, 5);
let now = Instant::new(200, 0);
let next = Instant::tick(now, prev);
assert_eq!(next, now);
}
#[test]
fn instant_tick_bumps_logical_on_tie() {
let prev = Instant::new(100, 5);
let now = Instant::new(100, 5);
let next = Instant::tick(now, prev);
assert_eq!(next, Instant::new(100, 6));
}
#[test]
fn instant_tick_bumps_logical_when_wall_goes_backward() {
let prev = Instant::new(200, 3);
let now = Instant::new(100, 0); let next = Instant::tick(now, prev);
assert_eq!(next.physical_ms, 200);
assert_eq!(next.logical, 4);
}
#[test]
fn instant_display_renders_dot_separated() {
let i = Instant::new(1234, 42);
assert_eq!(format!("{i}"), "1234.00042");
}
#[test]
fn instant_serde_round_trips() {
let i = Instant::new(1234, 56);
let bytes = serde_json::to_vec(&i).unwrap();
let back: Instant = serde_json::from_slice(&bytes).unwrap();
assert_eq!(back, i);
}
#[test]
fn instant_fingerprint_deterministic() {
use crate::Fingerprint;
let i = Instant::new(1, 2);
assert_eq!(i.fingerprint(), i.fingerprint());
}
#[test]
fn wallclock_now_returns_nonzero_recent_time() {
let w = WallClock;
let now = w.now();
assert!(now.physical_ms > 1_500_000_000_000);
assert_eq!(now.logical, 0);
}
#[test]
fn wallclock_named() {
assert_eq!(WallClock.name(), "wall");
}
#[test]
fn frozen_clock_returns_fixed_instant() {
let c = FrozenClock::at(1000);
assert_eq!(c.now(), Instant::new(1000, 0));
assert_eq!(c.now(), c.now());
}
#[test]
fn frozen_clock_advance_mutates_physical() {
let c = FrozenClock::at(100);
c.advance(50);
assert_eq!(c.now(), Instant::new(150, 0));
}
#[test]
fn frozen_clock_tick_logical_bumps_counter() {
let c = FrozenClock::at(100);
c.tick_logical();
c.tick_logical();
assert_eq!(c.now(), Instant::new(100, 2));
}
#[test]
fn frozen_clock_reset_zeroes_state() {
let c = FrozenClock::at(100);
c.advance(50);
c.tick_logical();
c.reset();
assert_eq!(c.now(), Instant::zero());
}
#[test]
fn frozen_clock_named() {
assert_eq!(FrozenClock::at(0).name(), "frozen");
}
#[test]
fn logical_clock_advances_on_now() {
let c = LogicalClock::new();
let a = c.now();
let b = c.now();
assert!(b > a);
}
#[test]
fn logical_clock_peek_doesnt_advance() {
let c = LogicalClock::new();
let _ = c.now(); let p1 = c.peek();
let p2 = c.peek();
assert_eq!(p1, p2);
}
#[test]
fn logical_clock_named() {
assert_eq!(LogicalClock::new().name(), "logical");
}
#[test]
fn hlc_clock_now_is_monotonic() {
let c = HlcClock::new();
let a = c.now();
let b = c.now();
assert!(b > a);
}
#[test]
fn hlc_clock_tick_merges_with_observed() {
let c = HlcClock::new();
let observed = Instant::new(u64::MAX >> 16, 0); let merged = c.tick(observed);
assert!(merged > observed);
}
#[test]
fn hlc_clock_named() {
assert_eq!(HlcClock::new().name(), "hlc");
}
#[test]
fn clock_trait_object_dispatch() {
let clocks: Vec<Box<dyn Clock>> = vec![
Box::new(WallClock),
Box::new(FrozenClock::at(100)),
Box::new(LogicalClock::new()),
Box::new(HlcClock::new()),
];
for c in &clocks {
let _ = c.now();
let _ = c.name();
}
}
}