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 backed by SQLite, IndexedDB, or a similar crash-safe store to guarantee Raft's durability properties across restarts.

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 loadSnapshot(): StoredSnapshot?

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

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.