Class RaftMetadataStore
java.lang.Object
com.loomcache.server.persistence.RaftMetadataStore
Persistent storage for Raft metadata: currentTerm, votedFor, and the
highest commit index durably represented in the local state machine.
Per Raft paper §5.2, these fields MUST be persisted to durable storage before responding to any RPC. Without persistence, a crashed node restarts at term 0 and may vote twice in the same term, violating Election Safety.
Implementation uses atomic file replacement (write-temp, fsync, rename) to ensure crash-safe updates. The file format is a simple text file:
currentTerm=<long> votedFor=<string|null> commitIndex=<long>
- Since:
- 1.5
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRecovered Raft metadata. -
Constructor Summary
ConstructorsConstructorDescriptionRaftMetadataStore(String nodeId, Path dir) Creates a metadata store in the given directory. -
Method Summary
Modifier and TypeMethodDescriptionload()Load persisted metadata from disk.voidPersist currentTerm and votedFor atomically to disk with fsync.voidPersist currentTerm, votedFor, and commitIndex atomically to disk with fsync.
-
Constructor Details
-
RaftMetadataStore
-
-
Method Details
-
persist
Persist currentTerm and votedFor atomically to disk with fsync. This MUST be called before responding to any vote request or term change.- Parameters:
currentTerm- the current term (must be non-negative)votedFor- the candidate voted for in this term, or null if no vote cast- Throws:
IOException- if the write or fsync fails
-
persist
public void persist(long currentTerm, @Nullable String votedFor, long commitIndex) throws IOException Persist currentTerm, votedFor, and commitIndex atomically to disk with fsync.- Parameters:
currentTerm- the current term (must be non-negative)votedFor- the candidate voted for in this term, or null if no vote castcommitIndex- the highest committed index durably represented locally- Throws:
IOException- if the write or fsync fails
-
load
Load persisted metadata from disk.- Returns:
- the recovered metadata, or defaults (term=0, votedFor=null, commitIndex=0) if no file exists
- Throws:
IOException- if the file exists but cannot be read or parsed
-