barrel_hlc (barrel_docdb v1.0.0)

View Source

Hybrid Logical Clock utilities for barrel_docdb

HLC timestamps are used for ordering changes across distributed nodes. They combine physical wall clock time with a logical counter to ensure causality while maintaining a close relationship to real time.

Format: {WallTime, Logical} where WallTime is in milliseconds and Logical is a counter that increments when events happen in the physical clock's future.

Summary

Types

HLC clock process reference.

HLC timestamp with wall_time and logical components.

Functions

Compare two timestamps. Returns lt if TS1 is before TS2, eq if equal, gt if TS1 is after TS2.

Decode binary to timestamp.

Encode timestamp to binary for storage. Uses big-endian encoding to maintain lexicographic sort order. Format: 12-byte binary with WallTime (64-bit) and Logical (32-bit).

Check if two timestamps are equal.

Alias for decode/1 for consistency with other modules.

Parse timestamp from string.

A timestamp at a given wall time (milliseconds since epoch) with a zero logical component. Used as a range cutoff (sorts before every real timestamp of that wall time).

Get the current global HLC timestamp without advancing the clock.

Check if TS1 is less than TS2.

Maximum timestamp value (for range scans).

Sync HLC from a base64-encoded HTTP header value. Silently ignores undefined or invalid values.

Minimum timestamp value (for range scans).

Generate a new global HLC timestamp, advancing the clock. Use this when generating a new event that needs to be ordered.

Generate a new timestamp for a local event using global clock.

Generate a new timestamp using a specific clock instance.

Start a new HLC clock with default settings. Uses physical system clock and no offset limit.

Start a new HLC clock with maximum offset limit. MaxOffset is the maximum allowed clock skew in milliseconds. A value of 0 disables offset checking.

Stop the HLC clock.

Synchronize with a remote HLC timestamp. Call this when receiving data from another node to maintain causality. Returns {ok, NewTimestamp} or {error, clock_skew}.

Get the current global timestamp without advancing the clock.

Get the current timestamp from a specific clock without advancing it.

Alias for encode/1 for consistency with other modules.

Convert timestamp to human-readable string. Format: "WallTime:Logical" (e.g., "1609459200000:42")

Update the global clock with a remote timestamp. Rejects non-timestamp input at the API boundary so a misuse cannot crash the global barrel_hlc_clock gen_server.

Update a specific clock with a remote timestamp.

Extract wall_time (milliseconds since epoch) from a timestamp.

Types

clock/0

-type clock() :: hlc:clock().

HLC clock process reference.

timestamp/0

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

HLC timestamp with wall_time and logical components.

Functions

compare(Timestamp, _)

-spec compare(timestamp(), timestamp()) -> lt | eq | gt.

Compare two timestamps. Returns lt if TS1 is before TS2, eq if equal, gt if TS1 is after TS2.

decode(_)

-spec decode(binary()) -> timestamp().

Decode binary to timestamp.

encode(Timestamp)

-spec encode(timestamp()) -> binary().

Encode timestamp to binary for storage. Uses big-endian encoding to maintain lexicographic sort order. Format: 12-byte binary with WallTime (64-bit) and Logical (32-bit).

equal(TS1, TS2)

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

Check if two timestamps are equal.

from_binary(Bin)

-spec from_binary(binary()) -> timestamp().

Alias for decode/1 for consistency with other modules.

from_string(Bin)

-spec from_string(binary()) -> timestamp() | {error, invalid_timestamp}.

Parse timestamp from string.

from_wall_time(WallMs)

-spec from_wall_time(non_neg_integer()) -> timestamp().

A timestamp at a given wall time (milliseconds since epoch) with a zero logical component. Used as a range cutoff (sorts before every real timestamp of that wall time).

get_hlc()

-spec get_hlc() -> timestamp().

Get the current global HLC timestamp without advancing the clock.

less(TS1, TS2)

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

Check if TS1 is less than TS2.

max()

-spec max() -> timestamp().

Maximum timestamp value (for range scans).

maybe_sync_from_header(HlcBase64)

-spec maybe_sync_from_header(binary() | undefined) -> ok.

Sync HLC from a base64-encoded HTTP header value. Silently ignores undefined or invalid values.

min()

-spec min() -> timestamp().

Minimum timestamp value (for range scans).

new_hlc()

-spec new_hlc() -> timestamp().

Generate a new global HLC timestamp, advancing the clock. Use this when generating a new event that needs to be ordered.

now()

-spec now() -> timestamp().

Generate a new timestamp for a local event using global clock.

now(Clock)

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

Generate a new timestamp using a specific clock instance.

start_link()

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

Start a new HLC clock with default settings. Uses physical system clock and no offset limit.

start_link(MaxOffset)

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

Start a new HLC clock with maximum offset limit. MaxOffset is the maximum allowed clock skew in milliseconds. A value of 0 disables offset checking.

stop(Clock)

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

Stop the HLC clock.

sync_hlc(RemoteTS)

-spec sync_hlc(timestamp()) -> {ok, timestamp()} | {error, clock_skew}.

Synchronize with a remote HLC timestamp. Call this when receiving data from another node to maintain causality. Returns {ok, NewTimestamp} or {error, clock_skew}.

timestamp()

-spec timestamp() -> timestamp().

Get the current global timestamp without advancing the clock.

timestamp(Clock)

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

Get the current timestamp from a specific clock without advancing it.

to_binary(TS)

-spec to_binary(timestamp()) -> binary().

Alias for encode/1 for consistency with other modules.

to_string(Timestamp)

-spec to_string(timestamp()) -> binary().

Convert timestamp to human-readable string. Format: "WallTime:Logical" (e.g., "1609459200000:42")

update(Timestamp)

-spec update(timestamp()) -> {ok, timestamp()} | {error, clock_skew}.

Update the global clock with a remote timestamp. Rejects non-timestamp input at the API boundary so a misuse cannot crash the global barrel_hlc_clock gen_server.

update(Clock, RemoteTS)

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

Update a specific clock with a remote timestamp.

wall_time(Timestamp)

-spec wall_time(timestamp()) -> non_neg_integer().

Extract wall_time (milliseconds since epoch) from a timestamp.