InMemoryRaftStorage

An in-memory RaftStorage implementation.

State is held entirely in process memory — term, vote, and log are all lost on process exit. This makes it suitable for:

  • Tests — fast, zero-setup, deterministic.

  • Ephemeral players — nodes that rejoin the cluster fresh on restart rather than recovering from durable state (they simply catch up via log replication).

Production servers should inject a persistent RaftStorage instead, to guarantee Raft's durability properties across restarts. kuilt ships one: DurableStoreRaftStorage, which keeps all of this state in a us.tractat.kuilt.store.DurableStore — the platform's crash-safe file or IndexedDB store — and is opened with DurableStoreRaftStorage.open(FileChannelDurableStore(nodeDirectory)). A consumer with a store of its own (SQLite, say) can implement RaftStorage directly and bind RaftStorageConformanceSuite to check it.

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
open suspend override fun appendEntries(entries: List<LogEntry>)

Appends entries to the end of the persistent log.

Link copied to clipboard
open suspend override fun discardLogPrefix(throughIndex: Long)

Removes all log entries with index <= throughIndex. Idempotent; tolerates a floor below the first retained entry.

Link copied to clipboard
open suspend override fun entries(fromIndex: Long = 0): List<LogEntry>

Returns all log entries with index >= fromIndex.

Link copied to clipboard
open suspend override fun leaderForTerm(): LeaderForTerm?

Returns the leader this node established for some term, or null if it has never established one. See saveLeaderForTerm.

Link copied to clipboard
open suspend override fun loadSnapshot(): StoredSnapshot?

Returns the stored snapshot, or null if none has been saved.

Link copied to clipboard
open suspend override fun saveLeaderForTerm(term: Long, leaderId: NodeId)

Persists leaderId as the node §5.2 established as leader of term, replacing any previously stored record.

Link copied to clipboard
open suspend override fun saveSnapshot(meta: SnapshotMeta, state: ByteArray)

Persists state as the snapshot covering all entries with index <= meta.lastIncludedIndex.

Link copied to clipboard
open suspend override fun saveTerm(term: Long)

Persists term as the latest observed term.

Link copied to clipboard
open suspend override fun saveTermAndVotedFor(term: Long, votedFor: NodeId?)

Atomically persists term and votedFor in a single durable write.

Link copied to clipboard
open suspend override fun saveVotedFor(nodeId: NodeId?)

Persists nodeId as the node voted for in the current term.

Link copied to clipboard
open suspend override fun term(): Long

Returns the latest term this node has observed.

Link copied to clipboard
open suspend override fun truncateFrom(index: Long)

Removes all log entries with index >= [index].

Link copied to clipboard
open suspend override fun votedFor(): NodeId?

Returns the NodeId this node voted for in the current term, or null if it has not yet voted.