Epochs and fencing
How Nova numbers a node's ledgers and ends one so a stale writer can never commit to it.
An epoch is a ledger's number. Fencing ends a ledger at a cut: the first slot that can never commit. Every slot below the cut is committed, and no slot at or above it is ever acknowledged. Every process that fences the same ledger finds the same cut. Object storage cannot tell a live writer from a stale one, so the fence works through the slots themselves.
A ledger is a run of slots written by one writer. A slot is committed when W of the K buckets in its bucket set hold its flight. See WAL, slots and ledgers for the slot protocol.
Where it lives
| Code | What it owns |
|---|---|
internal/storage/loglet/fence.go | Fence: the walk that decides each slot and returns the cut |
internal/storage/loglet/seek.go | seekFrontier: a fast estimate of where the ledger ends |
internal/storage/loglet/observe.go | How one slot looks in each bucket: empty, flight, poison, or unreachable |
internal/metadata/ledgers.go | Mint, MintNext, PublishCut, Cut |
internal/metadata/ledgerwriter.go | The writer record of each class: RevokeClassHead, Superseded, Standing |
loglet never touches the metadata store. It computes a cut, and its caller records it.
Creating a ledger
A star is one running novad process, named by its star ID. Each star has one mint counter at ledgers/<star> in the streams keyspace. Both classes' WALs on the star take their epochs from it, so their epochs interleave. The counter records each class's head, its newest epoch, separately.
message LedgerMint {
uint64 last_epoch = 1;
map<uint32, uint64> last_epoch_by_class = 2; // the head of each class
map<uint32, LedgerWriter> writers = 3; // writer authority, per class
bool retired = 4; // decommissioned: no more mints
}
message LedgerWriter {
uint64 generation = 1; // first epoch of this process's writer
bool revoked = 2; // recovery took the head
Ledger pending = 3; // the head's record, reserved with its number
}A mint is one CAS on the counter. It raises last_epoch, makes the new epoch its class's head, and reserves the new ledger's record inside the counter. It then creates the record at ledgers/<star>/<epoch> with a create-only put. If the process stops between the two writes, the next reader of the counter creates the reserved record before it mints or recovers. Every mint also announces a recovery marker for each of the star's unindexed ledgers.
There are two ways to mint:
Mintstarts a new writer. A process calls it once, at its first use of the WAL. It sets a newgenerationfor the class.MintNextis the in-place roll. It is refused withErrWriterDeposedwhen the class's head is no longer the epoch the roller held, or when recovery has revoked that head. An old process can therefore never roll back into service.
A retired counter refuses both with ErrStarRetired. A ledger never resumes once it stops writing. Epoch numbers mean nothing across stars. Within one star, an epoch is judged only against its own class's head.
Fencing a ledger
A fence ends a ledger. Three callers fence:
- A WAL starting up fences its own unindexed ledgers of the same class. A ledger minted with another bucket set is left to the recovery duty, which resolves its buckets from the record.
- A WAL whose ledger halted fences it before it rolls. See When a ledger halts.
- Ledger recovery fences a ledger that a stopped or departed writer left open. Recovery first revokes the class head, so that writer cannot roll past the fence.
A caller that finds a cut already recorded uses it and skips the fence.
Fence runs in two phases.
Seek. It probes for the first empty slot, starting at the checkpoint and doubling the distance until a probe finds nothing, then narrows the gap by binary search. A slot counts as present if any bucket holds it. A writer never assigns more than window slots ahead of its releases, so every slot below the first empty one, less the window, is committed. The careful phase starts there. A long ledger costs a logarithmic number of probes, and the fence never reads the committed prefix.
Decide each slot. From that start, Fence reads the slot's envelope in all K buckets and decides:
switch {
case view.count(occPoison) >= blocking:
return verdictCut, nil // write-free on a re-fence
case view.count(occFlight) >= cfg.Quorum:
return verdictAdvance, nil
case view.count(occFlight) >= 1:
// Undecided: repair before poison.
progressed, err := repairSlot(ctx, cfg, key, view)
// ...
default:
// Poison one empty bucket, in the set's fixed order.
wrote, err := poisonOne(ctx, cfg, key, view, nonce, s)
// ...
}- Committed. W or more buckets hold the flight. The slot is part of the ledger, and the walk moves on.
- Repair. At least one bucket holds the flight, but fewer than W do. The fence copies the whole object into every empty bucket with
PutIfAbsent, then looks again. A ledger has one writer, so a slot has only one possible content, and copying it is safe. - Poison. No reachable bucket holds a flight. The fence writes a poison envelope into the first empty bucket, in the set's fixed order, then looks again. One write per look matters: a writer racing into the slot turns the next look into a repair.
- Cut. K−W+1 buckets hold poison. This blocking set leaves too few buckets for W copies, so the slot can never commit. It is the cut.
The fence prefers completing a slot to ending it: a flight that reached even one bucket is repaired.
When a bucket does not answer
A slot's fate can depend on a bucket the fence cannot reach. With K=3 and W=2, one flight copy, one poison, and one silent bucket decide nothing: the silent bucket may hold the second flight copy, or the second poison. The fence does not guess. It waits 50 ms, looks again, and keeps trying while its context lives. When the context ends, it returns ErrUndecided, which the caller reports and retries.
Publishing the cut
PublishCut records the cut on the ledger record by CAS and moves an open ledger to FENCED. Every fencer computes the same cut, so publishing an equal cut again does nothing. A different cut returns ErrCutsConflict. That cannot happen between correct fencers, so it is reported as corruption and never retried. A later caller reads the recorded cut with Cut instead of fencing again. Presence of the field is the signal, because an empty ledger's cut is 0.
Slots above the cut can still hold objects. Uploads ran in parallel, so a slot past the cut may even hold W copies. None of them can be acknowledged, because acknowledgements release in slot order. They lie within (cut, cut + window], and ledger recovery deletes them. Poison stays until star reclaim runs after the star is decommissioned, because an isolated stale writer may send a create much later. Even star reclaim keeps the poison of the star's last ledger in each class: a cut-off process of that star may still hold that ledger's writer, and the poison refuses the next slot it lands.
Why the cut is safe
Let B = K−W+1, the size of the blocking set.
- An acknowledged slot is never poisoned out. It holds W copies already. At most K−W buckets are left for poison, fewer than B, so the fence must include it.
- A blocked slot never commits. With B poison markers in place, at most W−1 buckets remain for the flight. Ordered acknowledgement then also stops every later slot, even one whose uploads finished.
- Every fencer finds the same cut. Committed and dead are permanent states, because every write is a create that cannot overwrite. Two fencers, or a fencer and a writer, race one bucket at a time and end in the same state. Another fencer can finish an interrupted walk.
- The committed slots have no gaps. The walk moves only past committed slots, so
[0, cut)is dense.
With K=1 and W=1, one poison ends the ledger. A writer that loses its create to that poison halts.
Records at or above the cut
A record at or above the cut was never acknowledged. Two cases follow:
- A successor takes over. It reuses those sequence numbers, and the producer resends its unacknowledged batches. Sequence numbers stay dense.
- The same process rolls. It still holds those flights, so it republishes them into its next ledger. They are acknowledged late.
Flights below the cut are recovered from the old ledger. No sequence number is covered twice.
Crash points
| Crash after | State left behind | What the re-run does |
|---|---|---|
| Some poison writes | Poison in fewer than B buckets of the frontier slot | Reads the same view, then repairs or poisons where it stopped |
The blocking set lands, before PublishCut | A dead slot; no cut recorded | Finds B poisons at once, a write-free cut, and publishes it |
PublishCut | The record is FENCED with its cut | Reads the cut and skips the fence |
RevokeClassHead, before the fence | A revoked head that can no longer roll | Fences as usual; the old writer's roll is refused |
Concurrency
Fencers and the old writer run at the same time. Three things keep them safe:
- Creates only. Flight copies, repairs, and poison are all
PutIfAbsent. No write replaces another. - One write per look. The fence writes at most one bucket between two reads of the slot, so a racing writer or fencer is seen before the next write.
- Recovery holds a claim so that only one recoverer walks a ledger at a time. Correctness does not depend on it: two fences of the same ledger would still find the same cut.
Bounds and signals
The seek's stride doubles up to 2³², then grows linearly. The garbage that recovery must probe is bounded by the window, 128 slots by default. nova.auditor.garbage.reaped counts the slot copies recovery deleted above a cut, and indexer: recovery reaped garbage slots above the cut logs each one. A fence that cannot decide returns loglet: fence undecided. Recovery markers then stay due, so nova.auditor.due.age{queue="recovery"} grows; alert on it. See the metrics catalogue.
Producer tokens and storage fencing
Two kinds of fencing exist, and they protect different things.
The Fence API installs a producer's fencing token on a stream. Opens and one-shot appends must present the same token. It enforces an application's single-writer rule: optional on any streams, required on fenced streams.
Ledger fencing protects storage. It stops a stale node from reaching the copy quorum, whatever its lease or producer token says. A writer lease only names a stream's leader. See Leadership and leases.
Tests
internal/storage/loglet:TestFenceCutsAtTheFirstFreeSlot,TestFenceRepairsUndecidedSlotsToTheQuorum,TestFenceCutsWhereTheQuorumIsImpossible,TestFenceBlocksWhenTheBalanceIsUnreachable,TestFenceChasesARacingIncumbent,TestFenceIsIdempotentAndWriteFreeOnReRun,TestFenceSeeksLongLedgersInLogarithmicProbes,TestWalkAndFenceRejectMisaddressedEnvelopes.internal/storage/wal:TestRecoveredIncumbentCannotRollBackIntoService,TestVerifyHeadRefusesARevokedHead,TestRollSurvivesASiblingModeMint.tlaplus/Ledger.tlachecks the blocking-set argument, including configurations that must fail (Ledger.mustfail-blocking.cfg,Ledger.mustfail-ackorder.cfg).