Raft and the control plane
The raftdb-raft state machine, how the watcher quorum uses it to choose one primary per group, the fence on the node, and what is not built.
raftdb-raft: a pure state machine#
raftdb-raft is the Raft consensus core, written in the shape of raft-rs without depending on it (decision D17; openraft and raft-rs were rejected for their size and dependencies). Node owns no clock, thread, socket or file. The caller drives it:
- Call
Node::tickat a fixed interval (for example every 100 ms) andNode::stepfor every message received. - On the leader, call
Node::proposeto add a command to the log. - Whenever something may have happened, call
Node::ready. It returns what must be persisted (a new hard state, a log truncation, new entries), then the messages to send, then the committed entries to apply. - Do those in that order, and call
Node::advancewith the sameReady.
Persist comes before send because a vote or an acknowledgement must never be seen by a peer before it is durable. If the process dies between ready and advance, nothing in that Ready happened. Recovery is Node::restart with the persisted hard state and entries.
What is implemented: pre-vote (a node that was cut off cannot disturb a healthy cluster when it returns), the election restriction (a candidate's log must be at least as up to date as a majority's), a no-op entry at the start of every term, commit of current-term entries only by counting replicas (Raft section 5.4.2), a leader that steps down when it hears from no majority for an election timeout, fast log backtracking, batching, learners (non-voting members), and a rank that a vote request can carry for data held outside the Raft log (Node::set_rank).
Not implemented yet: membership changes (voters and learners are fixed at creation), snapshots and log compaction (the log is a vector from index 1), and read leases.
Because the crate touches nothing outside itself, it runs inside a deterministic simulator with message loss, duplication, reordering, partitions and crashes, and checks after every step: at most one leader per term, log matching, state machine safety and leader completeness, monotonic terms, and applied entries in order without gaps. The simulator must report a violation when the protocol is deliberately broken (Faults). See Simulation tests.
Where it is used#
The server does not run Raft. An election inside the server (D42: Raft over a peer port, a vote needing at least as much data) was built, tested with kill -9 on three servers (first election 1.2 s), and then removed in D51 step C4. What stayed in the server is what must: strict synchronous replication, the fence (epoch and lease), the dirty flag, and the data-rank rule.
raftdb-raft now runs in raftdb-watch: each watcher is the agent of one node and a member of a Raft group; the Raft log holds only decrees Assign(node) and Revoke, the epoch of a decree is its log index, the leader alone decides, and the others send it reports. The decision rules are plain functions with unit tests. See Watcher and error reports.
The fence on a node#
A node started with control = true is read-only until a control account commands it (POST /v1/control/role with an epoch, a role and a lease of 100 to 120000 ms). It is writable only while it is primary and its lease has not run out; the watcher renews the lease at a third of its length or faster, so a node that is not renewed stops writing by itself. Epochs only grow, and becoming primary needs a higher epoch than the node has seen. The epoch and the dirty flag are kept in control.state in the data directory (fence.rs).
The rules found by testing (D51 step C2)#
Three flaws of the first design were found by the tests and are now rules with tests:
- Contact means caught up. An agent renews its node's lease only while its watcher is in contact with the quorum within one contact window; a follower counts an Append only if its committed log then reached the commit index in that Append.
- The wait after a revoke is the contact window plus the lease plus a margin, because an agent that has just lost the quorum can still renew for a short time.
- Data is ranked by (data epoch, log position), dirty nodes included. Positions compare only within one data epoch, as Raft compares the last log term first. Without this, after two primaries in a row the cluster could wait forever.
Results recorded in D51 (three servers and three watchers in one process, lease 1.5 s): failover of 5.6, 7.7 and 7.7 s over three runs; a cut-off primary stopped writing 2.0 to 2.3 s after the cut; the sampler never saw two writable nodes; no acknowledged write was lost. With the default 3 s lease the failover is about 12 s. The requirement S9 (S9_FAILOVER_SECONDS = 5 in raftdb-core) is therefore not met with the test timings; D51 says shorter leases would shorten it at the cost of more false failovers.
Table groups and partitions#
With groups = N each group of tables has its own epoch, lease and primary, and the watcher quorum decides one primary per group. Requests for a group led by another node are forwarded or refused with the primary's address. Partitioned tables place partition k in group (hash(db, T) + k) mod groups. See raftdb-server and the replication chapter.