Repository navigation
Description
Context
This is the single highest-safety gap in the distributed layer. currentTerm and votedFor in ConsensusEngine are plain struct fields with no persistence. On any crash or clean restart, currentTerm resets to 0 and votedFor to "". This violates Raft's fundamental safety guarantee (§5.4.1): a restarted node can vote for a stale candidate, allowing two simultaneous leaders and data loss.
This issue introduces the interface and an in-memory default. The bbolt-backed production implementation is A3-1.
What to implement
Add to internal/distributed/log/interface.go:
// PersistentState stores the two Raft fields that must survive restarts.
// SAFETY: MemoryPersistentState provides no durability — replace with
// BboltPersistentState (see A3-1) before running in production.
type PersistentState interface {
LoadTerm() (term uint64, votedFor string, err error)
SaveTerm(term uint64, votedFor string) error
}Add MemoryPersistentState to internal/distributed/log/memory.go.
Inject PersistentState into ConsensusEngine via NewConsensusEngine, defaulting to log.NewMemoryPersistentState().
Replace all direct writes to ce.currentTerm / ce.votedFor in:
startElection(line ~350)handleNetworkRequestVote(line ~563)handleNetworkRequestVoteResp(line ~616)
with ce.ps.SaveTerm(newTerm, newVotedFor) + update the struct fields from the return.
Call ce.ps.LoadTerm() in Start() before starting the election timer.
Acceptance criteria
PersistentStateinterface defined alongsideConsensusLogin the same packageMemoryPersistentStatepasses unit test covering Save/Load round-trip and concurrent safetyConsensusEngine.Startrestores term/votedFor from persistent state- A new test verifies: create engine, save term=5/votedFor="node-b", create new engine on same
PersistentState,Start(), verifyGetCurrentTerm() == 5 - All existing distributed tests pass with
-race
Sequencing
Depends on A1-1 (same log package). Must land before A3-1 (which adds the bbolt implementation of this interface).
Activity
scttfrdmn commentedon Aug 3, 2026
Closing as not-the-direction, per the decision on #169.
Per §5 of
docs/design/conditional-writes-vs-raft.md, this is the largest single saving of adopting CAS — and it was the only issue in the series labeledpriority: critical.The title states the premise exactly: "Raft safety prerequisite." It is a prerequisite for Raft, and Raft is not being built.
PersistentStateexists so a restarted node cannot forget a vote it cast and thereby elect a second leader in one term. That failure mode requires the node to be the authority on something. Here it is not: per §1, no node holds authoritative state, so a node that restarts and forgets everything has lost nothing that mattered — the state is in a service with 11 nines of durability that every node can reach.Also closing #150, which would have implemented this interface in bbolt, for the same reason.
Reference: https://github.com/scttfrdmn/objectfs/blob/main/docs/design/conditional-writes-vs-raft.md
docs: record the CAS decision where the code claims otherwise (#286)
docs: the Raft closure list is five, not four (#287)