Grove.HybridLogicalClock (Grove v0.1.1)
View SourceA Hybrid Logical Clock (HLC) combining physical and logical time.
HLCs provide timestamps that:
- Are always monotonically increasing
- Stay close to physical wall-clock time
- Handle clock drift between nodes
- Are totally ordered (with node ID as tiebreaker)
Structure
- time: Physical time in milliseconds
- counter: Logical counter for events at same millisecond
- node: Node identifier for tiebreaking
Advantages over Lamport Clocks
- Timestamps correlate with real time (useful for debugging)
- Bounded divergence from wall clock
- Better semantics for Last-Write-Wins registers
Example
iex> hlc = Grove.HybridLogicalClock.new(:node_a)
iex> hlc = Grove.HybridLogicalClock.tick(hlc)
iex> hlc.time > 0
trueReferences
Based on "Logical Physical Clocks and Consistent Snapshots in Globally Distributed Databases" by Kulkarni et al.
Summary
Functions
Returns true if hlc1 happened after hlc2.
Returns true if hlc1 happened before hlc2.
Compares two HLCs for ordering.
Creates an HLC from a tuple.
Merges two HLCs, returning the one that happened later.
Creates a new HLC for the given node.
Records a local event, advancing the clock.
Returns the timestamp as a comparable tuple.
Updates the local clock upon receiving a remote timestamp.
Types
@type node_id() :: term()
@type t() :: %Grove.HybridLogicalClock{ counter: non_neg_integer(), node: node_id(), time: non_neg_integer() }
Functions
Returns true if hlc1 happened after hlc2.
Returns true if hlc1 happened before hlc2.
Compares two HLCs for ordering.
Returns:
:beforeif hlc1 < hlc2:afterif hlc1 > hlc2:equalif they are identical
@spec from_tuple({non_neg_integer(), non_neg_integer(), node_id()}) :: t()
Creates an HLC from a tuple.
Merges two HLCs, returning the one that happened later.
Creates a new HLC for the given node.
Records a local event, advancing the clock.
If physical time has advanced, reset counter. Otherwise increment counter.
@spec to_tuple(t()) :: {non_neg_integer(), non_neg_integer(), node_id()}
Returns the timestamp as a comparable tuple.
Useful for sorting or storing.
Updates the local clock upon receiving a remote timestamp.
Takes the maximum of local time, remote time, and physical time. Adjusts the counter appropriately.