Ledger recovery
How Nova indexes a stopped ledger's acknowledged records before anyone serves the streams it held.
Ledger recovery turns a stopped ledger into metadata. It fences the ledger, writes references for every committed slot below the cut (the first slot that can never commit), and deletes the garbage above it: flights that landed past the cut and can never be acknowledged. Then it marks the ledger Indexed. A node serves a stream only after every record the stream's previous leader acknowledged has a reference, and recovery writes the references that leader never wrote. It reads footers and writes metadata, and it moves no record bytes.
A leader acknowledges a record before the indexer records where it is. If the leader stops in between, only its ledger in object storage knows. Recovery reads that ledger back. See WAL, slots and ledgers for the ledger and Epochs and fencing for the fence.
Taking over after a failure
A failover takes the metadata store's session timeout plus the walk. The timeout (--oxia-session-timeout) is 10 seconds with external oxia and 15 seconds with embedded oxia. The walk usually fits well inside the SDK's 30-second retry budget.
Where it lives
| Code | What it owns |
|---|---|
internal/storage/indexer/recovery.go | Recoverer.Recover: revoke, claim, fence, absorb, probe, mark Indexed |
internal/storage/indexer/recoverygroup.go | RecoveryGroup: one walk per ledger per node, shared by every caller |
internal/storage/indexer/predecessor.go | PredecessorJudge: recover, wait, or pass, for each unindexed ledger |
internal/storage/wal/gate.go | The open gate, which recovers a stream's predecessor before an open passes |
internal/star/vacanttail.go | The same judgment for a read that needs the end of a stream nobody leads |
internal/auditor/recovery.go | The recovery duty, for ledgers that no open or read asks for |
internal/coord/decommission.go | Decommission, which recovers a drained star's open ledgers first |
The data
| Key | Keyspace | What it holds |
|---|---|---|
ledgers/<star>/<epoch> | streams | The ledger record: state (OPEN, FENCED, INDEXED), checkpoint, and cut |
ledgers/<star> | streams | The mint counter, whose per-class writer record carries revoked |
recovery/<star>/<epoch> | queues | The recovery claim, a session-bound record: one recoverer at a time |
due/recovery/<shard>/<readyAtMs>~<star>~<epoch> | queues | A recovery marker: the duty's work item |
streams/<id>/writer | streams | The last-writer record: the star and epoch that last opened the stream |
streams/<id>/release | streams | The clean-release record a departing leader leaves; see Leadership and leases |
FENCED means the cut is published but references are still owed. A fenced ledger counts as unindexed. INDEXED is final.
Recovering a stopped ledger
Recover drives one ledger to Indexed. It is the same code for every caller:
- Revoke the head. If the ledger is still its class's head, recovery sets
revokedon the star's writer record by CAS. The old process can no longer roll past the fence. - Take the claim. It creates
recovery/<star>/<epoch>as a session-bound record. When another recoverer holds it,RecoverreturnsErrRecoveryClaimed, and the caller decides whether to wait. - Read the ledger under the claim. No other recoverer moves the checkpoint now. The bucket set comes from the record's class and zones, so any node can recover a ledger minted with other settings.
- Fence, or adopt the cut. A recorded cut is used as it is. Otherwise recovery fences from the checkpoint and publishes the cut.
- Absorb and probe, side by side. One goroutine walks
[checkpoint, cut)in batches of 32 slots, reading footers 8 at a time, and absorbs each batch. The other deletes flights in(cut, cut + window], 16 copies at a time, under the class's delete budget. Poison stays. - Mark Indexed. The batch that reaches the cut waits for the probe, then advances the checkpoint and marks the ledger Indexed in one write.
cut, err := s.fenceOrAdoptCut(ctx, status, cfg)
if err != nil {
return err
}
eg.Go(func() (err error) {
indexed, err = s.absorbThroughCut(walkCtx, absorber, cfg, status.Checkpoint, cut, probed)
return err
})
eg.Go(func() error {
err := s.probeGarbage(walkCtx, status.Constants.Class, cfg, cut)
probed <- err
return err
})Every slot below a recorded cut must exist. The walk refuses a gap with missing committed slot, since a gap there is corruption. Absorb is the indexer's code: Indexing acknowledged records explains it.
Who starts a recovery
Five callers ask for recovery. They all join the node's one RecoveryGroup, so a ledger is walked at most once per node however many callers ask.
| Caller | When | What it waits for |
|---|---|---|
| The open gate | A node opens a stream whose last writer was another star, or its own earlier process | The walk, within the opener's deadline |
| A vacant read | A tail check, a page that reaches the end, a fence, or a subscription at the end, on a stream nobody leads | The walk, within the reader's deadline; a subscription waits at most 1 s, then polls |
| A closing node | novad shutting down, for its own unindexed ledgers | Nothing; it announces markers and nudges its peers |
| Decommission | nova node decommission on a star that is drained and leads nothing | The walk, before the declaration |
| The recovery duty | A recovery marker falls due | The walk; the marker stays until the ledger is Indexed |
The open gate
Before a node opens a stream, OpenStream runs two gates:
- The self gate recovers this node's own ledgers of the same class below its current epoch. A restart's acknowledged but unabsorbed records are covered this way.
- The predecessor gate decides who wrote the stream last. A clean-release record stamped by the last writer passes at once. Otherwise the last writer is read from
streams/<id>/writer. A stream older than that record falls back to the builder of its newest segment.
The gate then judges each unindexed ledger of that predecessor:
switch {
case standing.Superseded:
return VerdictRecover, standing.Class, nil
case standing.RestartedHead(p.Since):
return VerdictPass, 0, nil
case p.Alive:
return VerdictDefer, 0, nil
}- A superseded ledger, below its class's head or with its head revoked, can never append again. It is recovered whatever the registry says.
- A head that a later start of the star minted holds nothing of this stream, because that process never opened it. It passes.
- A head of a star that is alive in the registry defers the open with
ErrPredecessorRecovering, a retryable error. The registry can still show a star that just died, so liveness is never taken as proof of coverage. - A head of an absent star is recovered once the mint grace has passed: the session timeout, counted from the ledger's mint. A star mints before it registers, so a younger head may belong to a star that is still starting.
A star's ledgers are recovered in mint order within each class. The two classes run side by side, since a stream lives in one class. After doing any work, the gate scans the star's unindexed ledgers again, because a roll may have won before the revoke. When the gate passes, the node writes itself to streams/<id>/writer before it admits the first append. A crash after that point always leaves a predecessor to judge.
The recovery duty
The auditor's recovery duty recovers the ledgers nothing else asks for. Its markers are due/recovery/<shard>/<readyAtMs>~<star>~<epoch>, read in time order. Every mint announces a marker, due at once, for each of the star's unindexed ledgers. A closing star and the open gate announce them too.
For each due marker, the duty:
- Resolves a marker whose ledger record is gone: a burned mint, or a ledger reaped at decommission.
- Defers the class head of a live star by 5 minutes. For an absent star still inside the mint grace, it defers until the grace ends.
- Recovers everything else: a superseded ledger, or a dead star's head. It logs
auditor: recovering a superseded epoch; its successor's gate waits on itfor the first. - Leaves the marker when another recoverer holds the claim, and removes it once the ledger is Indexed.
Decommission handles the case the duty defers. A star that is drained and leads nothing never appends again, so nova node decommission recovers its open heads directly, then checks again that none is left. With --force, or for a star that is gone, that step is skipped, and the duty recovers the heads once the star's session has expired.
The last-writer record
A star can fail after it acknowledges records but before absorb writes the stream's first reference. The newest segment then names an older builder, or none. The last-writer record closes that gap: every open writes streams/<id>/writer before its first acknowledgement. The gate reads it before the segment builder, which can lag one absorb pass behind. A takeover therefore finds the ledger holding the acknowledged records even when no reference exists yet.
The recovery group
RecoveryGroup runs one walk per (star, epoch) on its own lifetime:
- A walk outlives the caller that started it. A caller that gives up stops waiting, and the walk goes on.
- Every caller for the same ledger shares that one walk and its result.
- At most 32 walks run at once on a node.
Joinwaits for room within its caller's deadline, andKickdrops the extra requests. - While another node holds the claim, the walk asks again every second and polls the ledger's Indexed state every 100 ms. It returns as soon as the other recoverer finishes.
Crash points
| Crash after | State left behind | What the re-run does |
|---|---|---|
| The revoke | A revoked head; no claim | Revokes again (a no-op) and continues |
| The claim | A claim on a dead session | The claim disappears when the session expires; the next caller takes it |
The fence, before PublishCut | Poison at the cut; no cut recorded | Fences again, finds the same cut, and publishes it |
| Some absorb batches | Rows and a checkpoint partway to the cut | Starts at the recorded checkpoint |
| All batches but the last | Every row except the last batch's; the probe may be unfinished | Probes again, then commits the last batch and marks Indexed |
| Marking Indexed | Nothing left to do | Reads Indexed and returns |
The ledger leaves its star's unindexed set only through the final write that marks it Indexed. Until then, every later open of a stream that star wrote judges it again, and the duty's marker stays.
Concurrency
- The claim is a run lock. It keeps two recoverers from walking one ledger at once. Correctness does not depend on it: fences agree, absorbs converge, and the checkpoint moves by CAS.
- A revoked head cannot roll. The old process's
MintNextis refused, so it cannot republish its flights into a new ledger behind the fence. - The old writer hits poison. Its next create at the cut fails, and its ledger halts.
- The gate rescans. A roll can win its mint just before the revoke lands. The gate scans again after any recovery and passes only when a scan finds nothing left to do.
Bounds and signals
| Bound | Value |
|---|---|
| Slots per absorb batch | 32 |
| Footer fetches per walk | 8 at once |
| Garbage probes per walk | 16 at once, paced by the class's delete budget |
| Walks per node | 32 at once |
| A live head's deferral | 5 minutes |
| The close-time announce and nudge | 3 seconds |
| Signal | What it shows |
|---|---|
indexer: ledger indexed | One recovery finished, with its cut and duration |
wal: opens held until recovery covers a prior ledger | The open gate started refusing opens; wal: the open gate cleared; a prior ledger recovered ends the episode |
nova.wal.gate.holds | Opens refused while a prior ledger waits for recovery |
nova.auditor.due.age{queue="recovery"} | How long the oldest due recovery marker has waited |
nova.auditor.garbage.reaped | Slot copies deleted above a cut |
novad: close nudged peers to recover | A closing node announced its ledgers and asked its peers to walk them |
Tests
internal/storage/wal:TestTakeoverRoundTrips,TestTakeoverRecoversEachClassSideBySide,TestRecoveryCrashEnumerationConverges,TestRecoveryMarksIndexedOnlyWithTheReapDone,TestRecoveryReportsMissingSlotBelowRecordedCut,TestOpenDefersOnAnAliveUnfinishedRole,TestOpenGatesOnTheLastWriterBeforeTheFirstSegment,TestOpenSparesAnUnregisteredHeadWithinTheMintGrace,TestTakeoverRecoversARollThatWonBeforeRevocation.internal/storage/indexer:TestRecoveryGroupSingleFlight,TestRecoveryGroupBoundsConcurrentRecoveryAfterKickStorm,TestRecoveryGroupJoinHonorsCallerContext,TestRecoverInOrderKeepsTheClassesIndependent.tlaplus/StreamLeadership.tlamodels the open gate, includingStreamLeadership.mustfail-gate.cfgandStreamLeadership.mustfail-refgap.cfg.