pub struct HybridClock {
pub id: String,
pub physical_ms: u64,
pub logical: u32,
}Expand description
Hybrid Logical Clock for causal + real-time ordering across distributed nodes.
R-01: Combines wall-clock time (physical_ms) with a logical counter. The physical component captures when an event happened in real time. The logical component orders events that happen within the same millisecond.
Based on Kulkarni, Demirbas, Madeppa, Avva & Leone (2014). Used in production by CockroachDB for MVCC timestamps.
Rules:
- On local event: physical = max(old_physical, wall_clock). If physical advanced → logical = 0. Else → logical += 1.
- On merge: physical = max(local, remote, wall_clock). Logical follows the same advancement/increment rule.
- Total order: (physical, logical, id). Higher physical wins. Same physical → higher logical wins. Both equal → lower id wins.
Fields§
§id: StringUnique identifier of the instance that owns this clock
physical_ms: u64Physical time in milliseconds since Unix epoch
logical: u32Logical counter — incremented when physical time doesn’t advance
Implementations§
Source§impl HybridClock
impl HybridClock
Sourcepub fn with_values(
id: impl Into<String>,
physical_ms: u64,
logical: u32,
) -> Self
pub fn with_values( id: impl Into<String>, physical_ms: u64, logical: u32, ) -> Self
Create a clock with explicit values (for tests and deserialization).
Sourcepub fn tick(&mut self) -> (u64, u32)
pub fn tick(&mut self) -> (u64, u32)
Increment the clock for a local event. Returns (physical_ms, logical) after advancement.
Sourcepub fn merge(&mut self, remote: &HybridClock) -> (u64, u32)
pub fn merge(&mut self, remote: &HybridClock) -> (u64, u32)
Merge with a remote clock. physical = max(local, remote, wall_clock). If physical advanced past both → logical = 0. If tied with one side → logical = max of tied sides + 1.
Sourcepub fn cmp_order(&self, other: &HybridClock) -> Ordering
pub fn cmp_order(&self, other: &HybridClock) -> Ordering
Compare two clocks for total ordering. Higher physical wins. Same physical → higher logical wins. Both equal → lower id wins (deterministic tiebreaker).
Trait Implementations§
Source§impl Clone for HybridClock
impl Clone for HybridClock
Source§fn clone(&self) -> HybridClock
fn clone(&self) -> HybridClock
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more