WarpCausalClock

class WarpCausalClock(replica: ReplicaId)

Mints causal Dots for spans and tracks the current happens-before frontier.

Call tick once per span, in the order the spans are created: it allocates the span's own Dot and records the frontier observed so far as that span's predecessors. The frontier then collapses to the new dot, so the next span's predecessors point back here — building the happens-before chain a SpanLink is later derived from.

The clock is span-scoped: it advances on span events only. A predecessor dot can still fail to resolve: an export whose writes failed or an evicted span leaves a dot no span in the set holds, and inferCausalLinks skips it. Because tick collapses the frontier, that also drops the path through the missing span to its own predecessors; the lost edge is tracked by #3028. Cross-signal causality (a log or metric happening-before a span) is future work.

Recovery is mandatory, and enforced

A restart that reset seq to 0 would re-mint dots already used by earlier spans and silently corrupt causality. So a clock mints and persists only after a recover read has landed. Until then tick answers ClockTick.Unrecovered and persist answers ClockPersist.Unrecovered, minting and writing nothing. persist after the spans of a batch are durably exported. tick itself stays pure (non-suspending) so the lock stays tight; persistence is the caller's explicit step.

A refused read fails closed

A recover whose read the store refuses (an exception, or a cancellation the store minted from its own withTimeout) returns ClockRecovery.Refused. On a clock whose read had not landed, the durable high-water mark exists but is unknown, so the clock keeps refusing to mint a dot that may already be used or write a seq below the durable one (#3390). observe and frontier still work. The next recover whose read lands arms the clock and keeps what it observed meanwhile. A WarpSpanExporter retries that read on each export and clear and saves unstamped spans until it lands: causal edges are lost, a dot is never re-minted. Those retries back off as the exporter's own span read does (#3436): after k refused reads in a row the next 2^k − 1 turns that would read the clock skip it, k capped at 6. recover always reads.

A clock whose read has landed is never demoted by a refusal. This process is the only writer of the clock's entry, and it has minted only above the seq it read, so its own seq is at least the durable one. Demoting it would strand a dot already handed to a span whose turn has not persisted yet.

An undecodable entry is different: the bytes were read and cannot be decoded, so no later read can do better. The clock starts fresh, at seq=0 or at the floor below, and the next persist replaces the entry, so bad data never leaves it dead.

Fresh does not mean unused, because durable spans still carry this replica's old dots (#3407). So a WarpSpanExporter whose read of the stored spans lands raises the clock's floor before the turn mints anything, and a clock that has not read yet keeps the floor until its read lands. The floor is the larger of two numbers: the highest own-replica dot that read found, a span's stamp or a predecessor a span names, and the high-water mark the span bytes carry (#3435). Once this clock's read has landed, every span write carries its seq as it stood when the bytes were encoded, so the mark covers dots whose spans were since evicted past maxSpans or cleared; before then a write carries only the own dots the exporter has seen. A merge raises the floor from the remote set, and an export raises it from an explicit stamp of this replica's.

The floor misses a used dot in two cases. Both entries corrupt: it starts at zero. And a corrupt span entry over a dot a WarpSpanExporter.merge wrote durably ahead of any clock write: a merge never writes the clock, so the sound clock entry does not cover it. The floor takes every stored or merged dot's seq as given, so one claiming Long.MAX_VALUE leaves the clock ClockTick.Exhausted (#3432).

A dot no key holds yet cannot reach a peer to be handed back: WarpSpanExporter shows an export's span in WarpSpanExporter.snapshot only once the turn's clock and span writes have both returned (#3517). A clock dot a turn minted and never published leaves a gap in this clock's sequence, which nothing reads as missing. The set dot the same turn minted is not a gap: the exporter keeps it, and the next span's write carries it.

Thread-safety

seq, the frontier and the read's backoff window are one sealed state guarded by an explicit reentrant lock, so the clock is correct under a real multi-threaded dispatcher — not merely under a test dispatcher. limitedParallelism(1) confinement is BANNED (see CLAUDE.md).

Parameters

replica

the stable ReplicaId for this device/process; namespaces every dot.

Constructors

Link copied to clipboard
constructor(replica: ReplicaId)

Functions

Link copied to clipboard
fun frontier(): Set<Dot>

A snapshot of the current causal frontier.

Link copied to clipboard
fun observe(remote: Set<Dot>)

Fold a remote frontier (received from another replica via anti-entropy) into the local frontier, so the next tick records those remote dots as predecessors — the cross-replica causal path.

Link copied to clipboard
suspend fun persist(store: DurableStore): ClockPersist

Durably persist the current (seq, frontier) to store. Call after a batch of ticks is durably exported so a restart never re-mints a used dot.

Link copied to clipboard
suspend fun recover(store: DurableStore): ClockRecovery

Reload (seq, frontier) from store, arming the clock. Call once at startup: until a read lands, tick and persist refuse. A missing entry answers ClockRecovery.Recovered and an undecodable one ClockRecovery.Undecodable, and both arm the clock at seq=0, or at the floor a landed span read already raised (WarpSpanExporter, #3407). A read never moves seq backwards: an entry older than what this clock already minted is ignored.

Link copied to clipboard

Mint the next span's CausalStamp: a fresh Dot whose predecessors are the current frontier (empty on the very first tick). Advances the frontier to the new dot.