Durable

data class Durable(val clientId: ClientId, val issuedThrough: Long) : ClientIdentity

Use the caller-supplied stable clientId and the serial floor issuedThrough, both persisted by the caller across restarts. Replaying the same requestId on a post-crash retry yields cross-crash exactly-once. A serial under this id above everything the node knows of is an operational error and surfaces as a ClientIdCollisionException rather than a silent re-mint.

issuedThrough is the highest requestId this id has issued, across every incarnation, or 0 for an id that has issued nothing. The node takes any serial at or below it as its own, so its own history is not a collision however it arrives: replayed from retained storage, sent by replication to a node that kept no log (clusterClient keeps its log in memory), or committed by a leader after the crash that lost the forward. Nothing in the node's own storage can stand in for it, since a snapshot can cut the last entry under the id and a forward can be in flight when the process dies.

Where it must be durable: your own store, written ahead. Commit each new requestId as the new floor before calling propose(command, requestId), and read it back on every boot. The other order leaves the stale floor described below. A retry resends the same bytes only if you stored them, so keep the command in the same record as its serial: nothing else hands them back after a crash.

One command at a time: a higher serial abandons every lower one. Do not issue a serial while a lower one is unresolved, meaning its propose has not returned. ClientSessionTable keeps one high-water serial per id, so once a higher serial commits, a retry of a lower one returns success, commits, and is dropped. Nothing throws, and it takes no crash: a caller's own timeout does the same. The rule covers the serial in hand when the process died, whether or not its call ever began: the floor makes that serial safe for the id, not for the command, and a gap in the serials protects nothing. So retry it first, with the bytes you stored, and only then issue the next. Issuing the next instead gives that command up without learning its fate: it applied if it committed first, and no retry brings it back. That gap is tracked by #3107.

A stale floor, one behind what the id really issued, is not caught for you. Three things can follow, and the node cannot tell any of them from a second writer:

  • An own serial above the floor reaches the node, and it fails with ClientIdCollisionException. It does so on every boot that walks that serial.

  • A propose raises the node's mark over that serial first, so nothing throws, and the call reuses a serial. A leader still holding the old entry refuses other bytes with RequestIdConflictException. Any other leader appends them and the call returns success, yet a consumer's ClientSessionTable drops the command.

  • A snapshot already covers that serial, so the node never walks it: its own snapshot, or one the voters install on a node that is behind them or kept no log. It boots clean, and the next serial above the floor is a reuse, with the same two endings as above.

So a clean boot does not prove a floor true. A floor set too high only leaves a gap in the serials, but widens the blind spot below.

propose(command) is refused under this identity, with an AutoSerialRefusedException before anything is drawn or sent. A serial the node drew for itself cannot be on disk before it is issued, so it could never be covered by a floor: the crash in between reads as a collision on catch-up, or reuses the serial and loses the next command silently. Use propose(command, requestId), or Auto where at-least-once is enough.

A second writer is caught only above the mark, and only in an entry this node walks. The mark is the higher of issuedThrough and whatever this incarnation has proposed. Another writer's serial above it throws, on every boot that walks it, since the retained log no longer raises the mark. Once a snapshot covers that serial, a node catching up from the snapshot never sees it: it boots clean, and its next serial can reuse the other writer's. A serial at or below the mark goes unseen too, and so does a serial two writers draw at once: propose returns success and a consumer's ClientSessionTable drops the command. That gap is tracked by #3081.

Throws

Samples

// The floor lives in your own store, beside the id, with the command it was issued for. They share
// one record because a store commits one key at a time. A sample uses the in-memory store.
val store: DurableStore = InMemoryDurableStore()
val lastIssued = StoreKey("client/last-issued")
suspend fun readFloor(): Long = store.read(lastIssued)?.decodeToString()?.substringBefore(' ')?.toLong() ?: 0L
suspend fun readCommand(): String? = store.read(lastIssued)?.decodeToString()?.substringAfter(' ')

// Boot: read the floor and hand it over — `scope.raftNode(cluster, transport, storage, identity = identity)`.
val identity = ClientIdentity.Durable(ClientId("checkout-client"), issuedThrough = readFloor())

// A new command, once the last one has returned. Read the floor afresh each time: `identity` is
// fixed at boot, so taking it from there would reuse a serial from the second command on.
// Commit the serial and the bytes FIRST, then `node.propose(command, requestId)`.
val requestId = readFloor() + 1
store.write(lastIssued, "$requestId charge-card".encodeToByteArray())

// After a crash the floor covers that serial, so its entry coming back is not a collision. Its
// outcome is unknown, so retry this serial with these bytes BEFORE issuing another: a higher serial
// abandons this one, and a retry after that is acknowledged and then dropped.
val restarted = ClientIdentity.Durable(identity.clientId, issuedThrough = readFloor())
check(restarted.issuedThrough == requestId)
check(readCommand() == "charge-card")

Constructors

Link copied to clipboard
constructor(clientId: ClientId, issuedThrough: Long)

Properties

Link copied to clipboard
Link copied to clipboard