vector_clock v0.1.0 VectorClock

Elixir implementation of vector clocks.

About

Vector clocks are used in distributed systems as a way of maintaining a logical ordering of events. A vector clock consists of a list of dots, which each dot representing a node in a distributed system. A dot consists of an identifier for the node, it’s current count, and a timestamp from the last time it was incremented. When a node sends an event to another node it increments the it’s dot in it’s vector clock and sends the clock along side the message. A node receiving a message can determine whether it has seen the effect of that message already by comparing it’s vector clock with the received vector clock.

Source

Based on the erlang version from :riak_core.

Summary

Functions

Get all nodes in the vector clock

Check if vector clock va is a descendent of vector clock vb

Check whether a vector clock decends from a given dot

Checks if vector clock va strictly dominates vector clock vb

Compares vector clocks for equality

Create a new empty vector clock

Create a new vectory clock with an initial dot

Get the counter value from a vector clock for a specific node

Get the dot entry from a vector clock for a specific node

Get the timestamp value from a vector clock for a specific node

Increment the vector clock at node

Increment the vector clock at node

Combines a list of vector clocks into their least possible common descendant

Prunes a vector clock based on various parameters

Converts a dot to a pure dot, for when timestamp data is not needed

Current timestamp for a vector clock

Checks if the given argument is a valid dot

Types

counter()
counter() :: integer
dot()
dot
pure_dot()
t()
t
timestamp()
timestamp() :: integer
vclock_node()
vclock_node() :: term

Functions

all_nodes(vclock)
all_nodes(t) :: [vclock_node]

Get all nodes in the vector clock.

descends(va, vb)
descends(t, t) :: boolean

Check if vector clock va is a descendent of vector clock vb.

descends_dot(vclock, dot)
descends_dot(t, dot) :: boolean

Check whether a vector clock decends from a given dot.

dominates(va, vb)
dominates(t, t) :: boolean

Checks if vector clock va strictly dominates vector clock vb.

A vector clock is said to dominate another when it’s clock represents a later logical time than the other.

equal?(va, vb)
equal?(t, t) :: boolean

Compares vector clocks for equality.

fresh()
fresh() :: t

Create a new empty vector clock.

fresh(node, count)
fresh(vclock_node, counter) :: t

Create a new vectory clock with an initial dot.

get_counter(vclock, node)
get_counter(t, vclock_node) :: counter

Get the counter value from a vector clock for a specific node.

get_dot(vclock, node)
get_dot(t, vclock_node) :: {:ok, dot} | {:error, :not_found}

Get the dot entry from a vector clock for a specific node.

get_timestamp(vclock, node)
get_timestamp(t, vclock_node) :: timestamp | nil

Get the timestamp value from a vector clock for a specific node.

increment(vclock, node)
increment(t, vclock_node) :: t

Increment the vector clock at node.

increment(vclock, node, timestamp)
increment(t, vclock_node, timestamp) :: t

Increment the vector clock at node.

merge(list)
merge([t]) :: t

Combines a list of vector clocks into their least possible common descendant.

prune(vclock, now, opts \\ [])
prune(t, timestamp, Keyword.t) :: t

Prunes a vector clock based on various parameters.

Vector clocks get pruned when they are either considered too large or when the top-most dot is too old. Entries are removed one-by-one off the top until neither of the two conditions are met.

Options

  • :small_vclock - max size for a vector clock to be not pruned.
  • :young_vclock - max difference between now and the timestamp on the latest dot for a vector clock to not be pruned.
  • :big_vclock - vector clocks larger than this will be pruned.
  • :old_vclock - max difference between now and the timestamp on the latest dot for it to get pruned.
pure_dot(dot)
pure_dot(dot) :: pure_dot

Converts a dot to a pure dot, for when timestamp data is not needed.

timestamp()
timestamp() :: timestamp

Current timestamp for a vector clock.

valid_dot?(arg1)
valid_dot?(term) :: boolean

Checks if the given argument is a valid dot.