DurableStoreRaftStorage
A RaftStorage that keeps a node's term, vote, established leader, log and snapshot in a DurableStore, so they are all still there after the process restarts.
This is the reference durable adapter — the answer to the "inject a persistent RaftStorage in production" line every other doc in this module points at. Give it whichever DurableStore the platform provides (FileChannelDurableStore on JVM/Android, NSFileManagerDurableStore on iOS/macOS, IndexedDbDurableStore in a browser) and the node comes back holding what it knew:
val storage = DurableStoreRaftStorage.open(FileChannelDurableStore(nodeDirectory))
val node = scope.raftNode(cluster, transport, storage)Samples
// One store per node. In production this is the platform's crash-safe implementation —
// FileChannelDurableStore(nodeDirectory), NSFileManagerDurableStore(nodeDirectory), or
// IndexedDbDurableStore.open(nodeDatabase). A sample uses the in-memory one.
val store: DurableStore = InMemoryDurableStore()
val storage = DurableStoreRaftStorage.open(store)
// Every mutator commits to the medium before it updates its own memory, so a term that has
// been saved is a term that survives — pass this to `scope.raftNode(cluster, transport, storage)`.
storage.saveTermAndVotedFor(term = 4L, votedFor = NodeId("node-a"))
storage.appendEntries(listOf(LogEntry(index = 1L, term = 4L, command = byteArrayOf(7, 8, 9))))
// A restart: a second handle onto the same store, decoding what the first one wrote.
val restarted = DurableStoreRaftStorage.open(store)
check(restarted.term() == 4L)
check(restarted.votedFor() == NodeId("node-a"))
check(restarted.entries().single().command.contentEquals(byteArrayOf(7, 8, 9)))Functions
Appends entries to the end of the persistent log.
Removes all log entries with index <= throughIndex. Idempotent; tolerates a floor below the first retained entry.
Returns the leader this node established for some term, or null if it has never established one. See saveLeaderForTerm.
Returns the stored snapshot, or null if none has been saved.
Persists state as the snapshot covering all entries with index <= meta.lastIncludedIndex.
Persists nodeId as the node voted for in the current term.
Removes all log entries with index >= [index].