Skip to content

A1-3: Add PersistentState interface — Raft safety prerequisite #130

Closed as not planned

Description

@scttfrdmn

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

  • PersistentState interface defined alongside ConsensusLog in the same package
  • MemoryPersistentState passes unit test covering Save/Load round-trip and concurrent safety
  • ConsensusEngine.Start restores term/votedFor from persistent state
  • A new test verifies: create engine, save term=5/votedFor="node-b", create new engine on same PersistentState, Start(), verify GetCurrentTerm() == 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

  1. added
    type: bug
    priority: critical
    area: distributed
    on Apr 7, 2026
  2. added
    effort: medium
    on Aug 3, 2026
  3. scttfrdmn commented on Aug 3, 2026

    @scttfrdmn
    OwnerAuthor

    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 labeled priority: critical.

    The title states the premise exactly: "Raft safety prerequisite." It is a prerequisite for Raft, and Raft is not being built. PersistentState exists 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

  4. added
    resolution: wontfix
    on Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: distributedeffort: mediumpriority: criticalresolution: wontfixtype: bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions