HLClock v1.0.0 HLClock.Timestamp View Source

HLC Timestamp

Implements the necessary components of the HLC tuple (i.e. logical time and logical counter) with extension to support node ids to provide unique timestamps even in cases where time and counter are the same

Binary representations assume big endianness for interop simplicity with other languages/representations.

Link to this section Summary

Functions

Determines if the clock's timestamp "happened before" a different timestamp

Exhaustive comparison of two timestamps: precedence is in order of time component, logical counter, and finally node identifier

Construct a Timestamp from the binary representation

Pack the rich Timestamp struct as a 128 bit byte array

Recover a Timestamp from the string representation.

Construct a timestamp from its principal components: logical time (initially node's physical time), logical counter (initally zero), and the node id

Given the current timestamp for this node and a provided remote timestamp, perform the merge of both logical time and logical counters. Returns the new current timestamp for the local node

Generate a single HLC Timestamp for sending to other nodes or local causality tracking

Create a millisecond granularity DateTime struct representing the logical time portion of the Timestamp.

Return the logical, monotonic time portion. Unlike System.monotonic_time, if timestamps are regularly exchanged with other nodes and/or clients, this monotonic timestamp will represent a cluster wide monotonic value.

Link to this section Types

Link to this type

t()

View Source
t() :: %HLClock.Timestamp{
  counter: integer(),
  node_id: integer(),
  time: integer()
}

Link to this section Functions

Determines if the clock's timestamp "happened before" a different timestamp

Exhaustive comparison of two timestamps: precedence is in order of time component, logical counter, and finally node identifier

Construct a Timestamp from the binary representation

Pack the rich Timestamp struct as a 128 bit byte array

48 bits - Physical time 16 bits - Logical time 64 bits - Node ID

Recover a Timestamp from the string representation.

Link to this function

new(time, counter, node_id \\ 0)

View Source

Construct a timestamp from its principal components: logical time (initially node's physical time), logical counter (initally zero), and the node id

Link to this function

recv(local, remote, physical_time, max_drift)

View Source

Given the current timestamp for this node and a provided remote timestamp, perform the merge of both logical time and logical counters. Returns the new current timestamp for the local node

Link to this function

send(map, pt, max_drift)

View Source

Generate a single HLC Timestamp for sending to other nodes or local causality tracking

Create a millisecond granularity DateTime struct representing the logical time portion of the Timestamp.

Given that this representation looses the logical counter and node information, it should be used as a reference only. Including the counter in the DateTime struct would create absurd but still ordered timestamps.

Example

iex> _t0 = HLClock.Timestamp.new(1410652800000, 0, 0)
%HLClock.Timestamp{counter: 0, node_id: 0, time: 1410652800000}

...> << time_and_counter :: size(64), _ :: size(64) >> = encoded
<<1, 72, 113, 117, 132, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>

...> DateTime.from_unix(time_and_counter, :microsecond)
{:ok, #DateTime<4899-07-30 06:31:40.800000Z>}

Return the logical, monotonic time portion. Unlike System.monotonic_time, if timestamps are regularly exchanged with other nodes and/or clients, this monotonic timestamp will represent a cluster wide monotonic value.