export

suspend fun export(span: SpanRecord): ExportResult

Export one span: insert it into the CRDT and durably flush to store.

If a WarpCausalClock was supplied, an unstamped span is auto-stamped with causal context before insert (#846) — an explicit SpanRecord.causalStamp always wins. Until the clock's recovery read has landed (refused, or never made), each export makes that read first, or skips it inside the clock's backoff window as the span read backs off (see recover); until it lands the span is saved unstamped and the clock's entry is not written, so no dot already in the store is minted again (#3390). The turn then counts in health per work key: the span's write as it went, and the skipped clock write as a failed turn the first clock write that lands resolves (#3426).

An explicit stamp of this replica's, its own dot or a predecessor it names, raises the clock's floor when the span is added, so the clock never mints that dot itself (#3435). Its seq is taken as given: a stamp claiming Long.MAX_VALUE leaves the clock ClockTick.Exhausted.

Every write carries this replica's high-water mark in the span bytes, at least every dot the clock has minted and every own dot the set has held. A restart whose clock entry is corrupt floors the clock above it, so a dot whose span was since evicted or cleared is not minted again (#3435).

The span is published only after its writes return (#3517)

The whole turn runs inside an internal write section: the stamp, the add and the eviction are built on a candidate set, the candidate is written, and only once both writes have returned is it joined into the set snapshot and merge read. So no dot this turn mints is visible before the store holds it. The span carries two of this replica's dots, its CausalStamp's and the one the ORSet mints for the add, and either one shown to a peer ahead of its write could be minted again by a restart whose store never held it. Two spans sharing the stamp's dot conflate their causal links, and two adds sharing the set's dot make a merge drop both spans.

A failed or cancelled turn publishes nothing, so memory never holds a span the turn could not write, and there is nothing to roll back. It does keep the set dot it minted, privately, so the next span never shares it with a failed write that commits late; the next span's write carries that dot. The clock is not rolled back either: its sequence stays advanced and its frontier keeps the unpublished dot, so the next stamp can name a predecessor that never resolves. That loses a real edge too: WarpCausalClock.tick collapses the frontier, so the next span's only path to earlier spans ran through that dot. The lost edge is tracked by #3028.

A failed or cancelled write may already have committed, or may commit later. The store then holds a span memory never published, until the next write replaces the snapshot. A restart in that window recovers it, and retrying the original unstamped span mints another dot and creates a second stamped copy.

Crash-window invariant (#1053)

The clock is persisted before the span's durable write, establishing the invariant the durable clock seq is always ≥ every durable span's dot. A crash in the two-write window can then only lose the span (retried on the next export) — it can never strand a persisted span at a dot the recovered clock would re-mint. Persisting the span first (the reverse order) leaves that hole: span at seq=N durable, clock still <N, and the next WarpCausalClock.tick on restart re-mints N, violating the clock's uniqueness guarantee.

The invariant holds unconditionally, including under concurrent export() on a multi-threaded dispatcher: the clock persist and the span write are serialized as one ordered unit by an internal coroutine Mutex, so the last durable clock write always reflects a seq ≥ every durable span's dot rather than being clobbered by an older concurrent snapshot. The candidate is built from the latest set inside that section, so a concurrent add is never dropped by a stale snapshot either, and a merge that joins a remote set while the writes are in flight is kept, since publishing joins rather than replaces.

Until a read of the stored spans has landed, the export makes that read first inside the write section (#3390), or refuses without it inside the backoff window recover describes (#3436). While the store refuses the read, the export returns ExportResult.Failure having changed nothing, the clock included: its span is in neither memory nor the store, so the caller's retry is its only copy. The read that lands joins the stored set and raises the clock above every dot of this replica's it found (#3407), and the export then builds, writes and publishes its span as any other does.

Returns ExportResult.Success after the durable write returns, with the span in snapshot. A failure in that section returns ExportResult.Failure, and so does a CancellationException the store mints at this live caller from its own withTimeout: only the caller's own cancellation propagates (#3388). Neither failure nor cancellation establishes that the span was not persisted. See DurableStore.write for the indeterminate commit outcome.

When the buffer is full, the eviction policy picks a victim among the spans already held and the new span is inserted, so this export's own eviction never takes it, and the bytes it writes always carry it. A later export's eviction can take it.

An add the set refuses changes nothing. Past the set's own clock ceiling, which a merge of a peer's set naming this replica's dot near Long.MAX_VALUE can bring within reach, the add throws ArithmeticException (#3429). The export then evicts, stamps and writes nothing, counts one failed turn of its span work in health, and returns that exception as the ExportResult.Failure's cause (#3441). The set's context never moves back, so once it is at the ceiling every export is refused this way.