Persisted claims
Queues

Persisted claims

How a queue with persisted claims records each claim as a row, so claims survive a seat's end.

With claims: persisted, every claim is also a row in the dispatch keyspace, written before the claim is handed to its worker. So a claim outlives the seat that dealt it: the next seat honors it to its deadline instead of dealing the message again. A row written by an older seat never replaces one written by a newer seat, which stops a replaced seat from settling or retrying a claim it no longer owns.

Where it lives

FileWhat it holds
internal/queue/claims.goThe claim writer, takeover reads, and the fencing rules
internal/queue/notice.goNotices of acks, nacks, and extends as they are read
internal/metadata/queuedispatch.goClaim rows and their compare-and-set

The data

A claim row is keyed queue/{queue}/r/{range}/claim/{position}, with the position zero-padded to 20 digits.

FieldMeaning
Attempt, Epoch, DeadlineThe claim as dealt: its attempt, the epoch of the seat that dealt it, its deadline
WorkerIDThe worker that holds it
Settled, CarriedOnThe claim is done; and it was failed onward by a due key, so an ack of it is refused
Dealt, TTL, UnworkedWhen it was dealt, its TTL, and whether its worker handed it back unattempted
SeatEpochThe epoch of the seat that last wrote the row. It differs from Epoch once a later seat settles or extends a claim it took over.

A write names the version it expects. A mismatch, or a row that is gone, is ErrClaimGone.

Writing claims

New claims, up to 1,024 ahead of their rows Write the rows together Stored Rows back Claim Ack Settle in the next write Write the settled row Ack answered once its row is back Seat Claim writer Dispatch keyspace Worker
A persisted claim. The worker gets its claim only after its row is stored.
  1. At takeover the seat starts a claim writer, which keeps one write of claim rows in flight at a time. Rows that need writing meanwhile, later deals, acks, and claims failed with a due key, wait and go together as the next write: up to 1,024 rows, settled rows first. A slower write makes the next one larger.
  2. The seat keeps taking claims while a write is in flight, at most 1,024 ahead of their rows, and the queue's concurrency counts them. A claim is handed to its worker when its row is back. If a write of new claims fails, the seat gives them back with their credits and takes none until its next save.
  3. The seat takes an ack at once, puts its row in the next write, and keeps dealing while the caller waits. Until the row is back, the claim belongs to that ack: another settle is refused CLAIM_GONE, and its deadline does not fail it. If the store refuses an ack's row, the seat writes it again at each save and answers the ack once it is stored. An answered ack survives the seat's end.
  4. The rows of failing claims and of extends are written on the seat's own goroutine, not by the writer. A failing claim's row is written before its due key, so only the seat whose write stands retries the message.

Every ack, nack, and extend notifies its seats the moment it is read, from a session frame or a unary call, in either claims mode. A claim whose deadline passes while such a call is on its way is held for it and checked again at each save. So a call read in time is honored even if the seat takes it late. After each save that moved the frontier, one range delete removes the rows below it.

Takeover

A seat taking a range over reads the claim rows from its frontier on, 512 per page:

  • A settled row settles its position.
  • A standing row, a claim still out, is held with its original attempt, epoch, and deadline, the deadline capped at the claim's max deadline. Its token still works, because a claim is found by position, attempt, and the epoch it was dealt under.
  • A row with a later SeatEpoch than this seat's means it was already replaced, and it ends.

Fencing a replaced seat

A replaced seat can deal for a while before it learns so. Each write therefore checks the row it finds:

internal/queue/claims.go
			switch {
			case row.Err != nil:
				f.written[i].Err = row.Err
			case !row.Found || row.SeatEpoch > s.epoch:
				f.lost = s.supersededAt(row)

				return
			case row.Settled:
				f.written[i], f.done[i] = metadata.ClaimWritten{}, true
				f.carried = f.carried || row.CarriedOn
			default:
				w := f.writes[i]
				w.Expect = row.Version
				writes, again = append(writes, w), append(again, i)
			}
  • A new claim's row is written only if the position holds the version the seat expects: no row, or the row an earlier claim left. A refused write reads the row, at most 3 rounds for rows that keep changing.
  • A row a later seat wrote, or a row gone from a position this seat has not settled, means the seat was replaced. It hands over nothing of that write and ends.
  • A settled row settles the position, and nothing is dealt.
  • An earlier seat's standing row is taken over by compare-and-set, and the message is dealt. Two workers can then hold it, but only the later claim settles it. If the earlier seat took an ack first, the row is found settled instead.
  • A nack whose row a later seat wrote is refused SEAT_MOVED. An ack of a row settled as CarriedOn is refused CLAIM_GONE, because the message is dealt again, and the seat wakes its fire to look for the key another seat left.

Crash points

Crash afterState left behindWhat the re-run does
A claim row is stored, before the hand-overA standing row with no workerThe next seat holds it to its deadline, then deals it again
An ack's row is writtenA settled rowThe next seat settles the position from the row
An ack is taken, before its rowA standing claimThe worker can settle it at the next seat
A failing claim's row, before its due keyA row standing past its deadlineThe next seat fails it at once
The due key, before the position or the row settlesTwo seats may each write a due keyThe message is delivered twice, never lost
The frontier save, before the sweep of rows below itRows below the frontierTakeover skips them, and the next save sweeps them

Two races remain, and each delivers a message twice without losing it. A seat dies between writing its due key and settling its row, or takes longer than a save to settle it, and its successor also writes a due key. Or a replaced seat writes a due key for a delayed message its successor already dealt, and the key still fires when the successor's fire next looks.

Nodes built before commit cd3d90b3 write no seat epoch and no CarriedOn. They also write new claim rows without an expected version, and fail claims without writing the row first. During a rolling upgrade these rules hold only between upgraded nodes. See Upgrades.

Concurrency

The writer is one goroutine with one write in flight. The seat's goroutine owns the claims and writes the rows of failing and extended claims itself. Two seats can briefly share a range while the ring moves. The row's SeatEpoch and the expected-version write decide every claim between them.

Bounds and signals

BoundValue
Rows in one write1,024
Claims taken ahead of their rows1,024
Time one write has3 seconds
Rows read per takeover page512
Rounds against a changing row3

Each persisted delivery and each ack waits for one metadata store write. If the store is unreachable, delivery stops.

Signals:

  • nova.queue.claim_rows.unwritten, nova.queue.claims.expired, nova.queue.claim.age
  • Log queue: claims could not be recorded; their messages are dealt at the next try
  • Log queue: settled claims could not be recorded; they are written again at the next save, and their acks wait
  • Log queue: sweeping claim rows failed; the next save sweeps

Tests

  • Crashes: TestASeatLostWithClaimRowsOnTheirWay, TestAnAckLostWithItsSeatIsDealtAgainAtTheDeadline, TestAnAckOnItsWayWhenItsSeatIsLost in internal/queue/claims_test.go.
  • Fencing: TestASupersededSeatsClaimRowLosesToItsSuccessors, TestAnAckOfAClaimTheEarlierSeatCarriedOnIsNotAnsweredAsMade, TestANackAtASupersededSeatIsRefused, TestASuccessorLostAfterTakingARowOverLeavesItToTheNext in fence_test.go.
  • Rows: TestClaimRowsValidateTheAck in internal/metadata/queues_test.go.
  • Slow store: TestAcksSentInTimeAreHonouredOverASlowStore in internal/transport/grpc/queue/work_slow_test.go.

Go deeper

On this page