Ledger recovery

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

par [absorb and probe] acquire the writer lease read the last-writer record and its unindexed ledgers revoke the old head (CAS on its mint counter) take the recovery claim fence from the checkpoint cut PublishCut (CAS) late create poison, so its ledger halts read the footers of [checkpoint, cut) span rows, checkpoint CAS per 32 slots delete garbage in (cut, cut + window] last checkpoint CAS and mark Indexed record itself as the last writer open the stream Old leader Successor Metadata store Bucket set

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

CodeWhat it owns
internal/storage/indexer/recovery.goRecoverer.Recover: revoke, claim, fence, absorb, probe, mark Indexed
internal/storage/indexer/recoverygroup.goRecoveryGroup: one walk per ledger per node, shared by every caller
internal/storage/indexer/predecessor.goPredecessorJudge: recover, wait, or pass, for each unindexed ledger
internal/storage/wal/gate.goThe open gate, which recovers a stream's predecessor before an open passes
internal/star/vacanttail.goThe same judgment for a read that needs the end of a stream nobody leads
internal/auditor/recovery.goThe recovery duty, for ledgers that no open or read asks for
internal/coord/decommission.goDecommission, which recovers a drained star's open ledgers first

The data

KeyKeyspaceWhat it holds
ledgers/<star>/<epoch>streamsThe ledger record: state (OPEN, FENCED, INDEXED), checkpoint, and cut
ledgers/<star>streamsThe mint counter, whose per-class writer record carries revoked
recovery/<star>/<epoch>queuesThe recovery claim, a session-bound record: one recoverer at a time
due/recovery/<shard>/<readyAtMs>~<star>~<epoch>queuesA recovery marker: the duty's work item
streams/<id>/writerstreamsThe last-writer record: the star and epoch that last opened the stream
streams/<id>/releasestreamsThe 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:

  1. Revoke the head. If the ledger is still its class's head, recovery sets revoked on the star's writer record by CAS. The old process can no longer roll past the fence.
  2. Take the claim. It creates recovery/<star>/<epoch> as a session-bound record. When another recoverer holds it, Recover returns ErrRecoveryClaimed, and the caller decides whether to wait.
  3. 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.
  4. Fence, or adopt the cut. A recorded cut is used as it is. Otherwise recovery fences from the checkpoint and publishes the cut.
  5. 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.
  6. Mark Indexed. The batch that reaches the cut waits for the probe, then advances the checkpoint and marks the ledger Indexed in one write.
internal/storage/indexer/recovery.go
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.

CallerWhenWhat it waits for
The open gateA node opens a stream whose last writer was another star, or its own earlier processThe walk, within the opener's deadline
A vacant readA tail check, a page that reaches the end, a fence, or a subscription at the end, on a stream nobody leadsThe walk, within the reader's deadline; a subscription waits at most 1 s, then polls
A closing nodenovad shutting down, for its own unindexed ledgersNothing; it announces markers and nudges its peers
Decommissionnova node decommission on a star that is drained and leads nothingThe walk, before the declaration
The recovery dutyA recovery marker falls dueThe walk; the marker stays until the ledger is Indexed

The open gate

Before a node opens a stream, OpenStream runs two gates:

  1. 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.
  2. 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:

internal/storage/indexer/predecessor.go
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:

  1. Resolves a marker whose ledger record is gone: a burned mint, or a ledger reaped at decommission.
  2. 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.
  3. 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 it for the first.
  4. 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. Join waits for room within its caller's deadline, and Kick drops 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 afterState left behindWhat the re-run does
The revokeA revoked head; no claimRevokes again (a no-op) and continues
The claimA claim on a dead sessionThe claim disappears when the session expires; the next caller takes it
The fence, before PublishCutPoison at the cut; no cut recordedFences again, finds the same cut, and publishes it
Some absorb batchesRows and a checkpoint partway to the cutStarts at the recorded checkpoint
All batches but the lastEvery row except the last batch's; the probe may be unfinishedProbes again, then commits the last batch and marks Indexed
Marking IndexedNothing left to doReads 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 MintNext is 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

BoundValue
Slots per absorb batch32
Footer fetches per walk8 at once
Garbage probes per walk16 at once, paced by the class's delete budget
Walks per node32 at once
A live head's deferral5 minutes
The close-time announce and nudge3 seconds
SignalWhat it shows
indexer: ledger indexedOne recovery finished, with its cut and duration
wal: opens held until recovery covers a prior ledgerThe open gate started refusing opens; wal: the open gate cleared; a prior ledger recovered ends the episode
nova.wal.gate.holdsOpens 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.reapedSlot copies deleted above a cut
novad: close nudged peers to recoverA 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.tla models the open gate, including StreamLeadership.mustfail-gate.cfg and StreamLeadership.mustfail-refgap.cfg.

See also

On this page