Grove.VectorClock (Grove v0.1.1)
View SourceVector clock implementation for causality tracking.
A vector clock is a mechanism for tracking the causal ordering of events in a distributed system. Each replica maintains a counter, and the vector of all counters can be compared to determine if one event happened before another, or if they are concurrent.
Example
iex> vc = Grove.VectorClock.new()
iex> vc = Grove.VectorClock.increment(vc, :node_a)
iex> vc = Grove.VectorClock.increment(vc, :node_a)
iex> Grove.VectorClock.get(vc, :node_a)
2
Summary
Functions
Returns the list of actors in the vector clock.
Compares two vector clocks.
Checks if two vector clocks are concurrent (neither happened before the other).
Checks if vc1 descends from (happened after or equal to) vc2.
Dominates check - returns true if vc1 dominates vc2.
Gets the counter value for the given actor.
Increments the counter for the given actor.
Merges two vector clocks by taking the pointwise maximum.
Creates a new empty vector clock.
Checks if vc1 strictly descends from (happened after) vc2.
Types
@type actor() :: term()
@type t() :: %{required(actor()) => non_neg_integer()}
Functions
Returns the list of actors in the vector clock.
Compares two vector clocks.
Returns:
:beforeif vc1 happened before vc2 (vc1 < vc2):afterif vc1 happened after vc2 (vc1 > vc2):equalif vc1 equals vc2:concurrentif vc1 and vc2 are concurrent (neither happened before the other)
Checks if two vector clocks are concurrent (neither happened before the other).
Checks if vc1 descends from (happened after or equal to) vc2.
Returns true if every counter in vc2 is less than or equal to the corresponding counter in vc1.
Dominates check - returns true if vc1 dominates vc2.
vc1 dominates vc2 if all counters in vc1 are >= counters in vc2, and at least one counter is strictly greater.
@spec get(t(), actor()) :: non_neg_integer()
Gets the counter value for the given actor.
Returns 0 if the actor is not present.
Increments the counter for the given actor.
Merges two vector clocks by taking the pointwise maximum.
@spec new() :: t()
Creates a new empty vector clock.
Checks if vc1 strictly descends from (happened after) vc2.