Skip to main content

Module raft

Module raft 

Source
Expand description

A draft Raft replica that keeps the journals of an AtomicJournalStore.

This is the algorithm etcd runs behind EtcdJournalStore, written as a pure state machine in the style of etcd’s raft package: the node reads no clock, opens no socket and starts no thread. The caller passes the local time to RaftNode::tick and RaftNode::step, delivers the messages from RaftNode::take_messages over any transport, and persists RaftNode::hard_state and the log before sending them. That makes the same code usable in a real deployment and in a deterministic simulation, where tests drive it with skewed and jumping clocks, lost and reordered messages, partitions and restarts (engine_raft_simulation_tests.rs).

Safety (election safety, log matching, leader completeness and state machine safety) never depends on a clock: time only decides when a node starts an election or a leader sends heartbeats. Clocks matter for liveness, and two etcd options keep a cluster live under faults that a plain Raft does not survive:

  • PreVote — a node first asks whether it could win before raising its term, so a member that can send but not receive cannot disrupt a healthy leader over and over.
  • CheckQuorum — a leader that has not heard from a majority within an election timeout steps down, and followers that heard from a leader within that timeout refuse votes (leader stickiness). A leader that can send but not receive therefore stops blocking an election.

Reads are linearizable because they go through the log (as etcd’s older quorum reads did): a read returns the state at its position in the committed order. No proposal is retried; a proposal lost on the way to the leader simply never commits, and the caller’s deadline reports an unknown outcome, as AtomicJournalStore requires.

This is a draft for users to try: membership changes, snapshots/log compaction and ReadIndex/lease reads are not implemented.

Structs§

JournalState
The replicated journals, as AtomicJournalStore exposes them.
RaftConfig
RaftEntry
RaftEnvelope
RaftHardState
What must survive a restart: the term, the vote and the log.
RaftNode

Enums§

RaftCommand
RaftMessage
RaftOutcome
RaftRole