hlc (hlc v3.0.4)

View Source

hlc

implements the Hybrid Logical Clock outlined in "Logical Physical Clocks and Consistent Snapshots in Globally Distributed Databases", available online at http://www.cse.buffalo.edu/tech-reports/2014-04.pdf.

An hybrid logical clock is available as a linked process. Objects of this type model causality while maintaining a relation to physical time. Roughly speaking, timestamps consist of the largest wall clock time among all events, and a logical clock that ticks whenever an event happens in the future of the local physical clock.

Summary

Functions

compare if 2 timestamps are equal

returns the maximal offset allowed. A value of 0 means offset checking is disabled.

compare if one timestamps happen before the other

create a manually controlled physical clock

create a manually controlled physical clock and initialise it with a default ts.

returns a timestamp associated with an event from the local machine that may be sent to other members of the distributed network. This is the counterpart of Update, which is passed a timestamp received from another member of the distributed network.

timestamp in milliseconds

change the value of the manually controlled physical clock.

Sets the maximal offset from the physical clock that a call to Update may cause. A well-chosen value is large enough to ignore a reasonable amount of clock skew but will prevent ill-configured nodes from dramatically skewing the wall time of the clock into the future.

start a new hybrid logical clock with physical clock and maxoffset=0

start a new hybrid logical clock with a custom physical clock function.

start a new hybrid logical clock with a name. Clocks are always local

stop a clock

stop a manually controlled physical clock.

return a copy of the clock timestamp without adjusting it

takes a hybrid timestamp, usually originating from an event received from another member of a distributed system. The clock is updated and the hybrid timestamp associated to the receipt of the event returned. An error may only occur if offset checking is active and the remote timestamp was rejected due to clock offset, in which case the state of the clock will not have been altered. To timestamp events of local origin, use Now instead.

Types

clock/0

-type clock() :: pid() | atom().

clock_fun/0

-type clock_fun() :: fun(() -> non_neg_integer()).

timestamp/0

-type timestamp() :: #timestamp{wall_time :: term(), logical :: term()}.

Functions

equal(TS, _)

-spec equal(timestamp(), timestamp()) -> boolean().

compare if 2 timestamps are equal

get_maxoffset(Clock)

-spec get_maxoffset(clock()) -> non_neg_integer().

returns the maximal offset allowed. A value of 0 means offset checking is disabled.

less(Timestamp, _)

-spec less(timestamp(), timestamp()) -> boolean().

compare if one timestamps happen before the other

manual_clock()

-spec manual_clock() -> {pid(), fun()}.

create a manually controlled physical clock

manual_clock(TS0)

-spec manual_clock(integer()) -> {pid(), fun()}.

create a manually controlled physical clock and initialise it with a default ts.

now(Clock)

-spec now(clock()) -> timestamp().

returns a timestamp associated with an event from the local machine that may be sent to other members of the distributed network. This is the counterpart of Update, which is passed a timestamp received from another member of the distributed network.

physical_clock()

-spec physical_clock() -> non_neg_integer().

timestamp in milliseconds

set_manual_clock(Pid, TS)

-spec set_manual_clock(pid(), integer()) -> ok.

change the value of the manually controlled physical clock.

set_maxoffset(Offset, Clock)

-spec set_maxoffset(non_neg_integer(), clock()) -> ok.

Sets the maximal offset from the physical clock that a call to Update may cause. A well-chosen value is large enough to ignore a reasonable amount of clock skew but will prevent ill-configured nodes from dramatically skewing the wall time of the clock into the future.

A value of zero disables this safety feature. The default value for a new instance is zero.

start()

-spec start() -> {ok, clock()}.

start(ClockFun, MaxOffset)

-spec start(ClockFun :: clock_fun(), MaxOffset :: non_neg_integer()) -> {ok, clock()}.

start(Name, ClockFun, MaxOffset)

-spec start(Name :: atom(), ClockFun :: clock_fun(), MaxOffset :: non_neg_integer()) -> {ok, clock()}.

start_link()

-spec start_link() -> {ok, clock()}.

start a new hybrid logical clock with physical clock and maxoffset=0

start_link(ClockFun, MaxOffset)

-spec start_link(ClockFun :: clock_fun(), MaxOffset :: non_neg_integer()) -> {ok, clock()}.

start a new hybrid logical clock with a custom physical clock function.

start_link(Name, ClockFun, MaxOffset)

-spec start_link(Name :: atom(), ClockFun :: clock_fun(), MaxOffset :: non_neg_integer()) ->
                    {ok, clock()}.

start a new hybrid logical clock with a name. Clocks are always local

stop(Clock)

-spec stop(Clock :: clock()) -> ok | {error, term()}.

stop a clock

stop_manual_clock(Pid)

-spec stop_manual_clock(pid()) -> ok.

stop a manually controlled physical clock.

timestamp(Clock)

-spec timestamp(clock()) -> timestamp().

return a copy of the clock timestamp without adjusting it

update(Clock, RT)

-spec update(clock(), timestamp()) -> {ok, timestamp()} | {timeahead, timestamp()}.

takes a hybrid timestamp, usually originating from an event received from another member of a distributed system. The clock is updated and the hybrid timestamp associated to the receipt of the event returned. An error may only occur if offset checking is active and the remote timestamp was rejected due to clock offset, in which case the state of the clock will not have been altered. To timestamp events of local origin, use Now instead.