mod raft

module raft

Leader consensus over exploration decrees between server brains. Leader consensus over exploration decrees.

The per-chain server brains need one of themselves to decide where the ensemble goes next: which seam gets a bridge, which community each chain works, when an exploration policy changes. A decree is only safe to act on when a majority agrees it is part of the shared history, which is the raft guarantee: elected leadership, an append-only replicated log, and commitment by majority match with the current-term restriction that closes the stale-commit hazard.

The node here is pure state and arithmetic: no clock, no socket, no randomness. The caller supplies logical time to RaftNode::tick and delivers messages to RaftNode::receive; both return the messages to send, and any transport that carries them faithfully (an in-memory bus in tests, nng between processes) yields the same transcripts. Election timeouts stagger deterministically by node identity, so a quiet cluster elects the same leader every replay.

Modules

Types

type NodeId

Node identity inside one consensus group.

type Term

Election term.

Functions

fn decree_members(local: NodeId, peers: &[NodeId]) -> Vec<NodeId>

Sorted identities participating in one consensus group.

Peer lists exclude the local node and may use absolute campaign replica identities, so group membership cannot be reconstructed as 0..len.

Enums

enum RaftMessage

Messages between raft nodes.

RequestVote

A candidate asks for a vote.

term: Term

Candidate’s term.

last_log_index: u64

Index of the candidate’s last log entry.

last_log_term: Term

Term of the candidate’s last log entry.

VoteReply

A voter answers a vote request.

term: Term

Voter’s current term.

granted: bool

Whether the vote was granted.

AppendEntries

Leader replication and heartbeat.

term: Term

Leader’s term.

prev_log_index: u64

Index of the entry preceding entries.

prev_log_term: Term

Term of the entry preceding entries.

entries: Vec<Decree>

Entries to append; empty for a heartbeat.

leader_commit: u64

Leader’s commit index.

AppendReply

Follower answers replication.

term: Term

Follower’s current term.

success: bool

Whether the append matched.

match_index: u64

Highest index known replicated on the follower when successful; the follower’s log length hint otherwise.

enum Role

The three raft roles.

Follower

Accepts decrees from the current leader.

Candidate

Standing for election.

Leader

Replicates decrees and answers proposals.

Structs and Unions

struct Decree

One replicated decree: an opaque payload the state machine above interprets (the wire format is the capnp decree, never inspected here).

term: Term

Term the decree was proposed in.

payload: Vec<u8>

Opaque decree payload.

struct NotLeader

Proposing on a node that is not the leader.

struct RaftNode

One raft node’s complete state.

Implementations

impl RaftNode

Functions

fn id(&self) -> NodeId

This node’s identity.

fn leader_hint(&self) -> Option<NodeId>

The node this one believes leads, when any.

fn new(id: NodeId, mut peers: Vec<NodeId>, base_timeout: u64, stagger: u64) -> Self

A follower with a deterministic, identity-staggered election timeout: base + id * stagger ticks of silence start an election, so no two quiet nodes fire together and the same configuration elects the same first leader on every replay. The supplied peer roster is normalized as a set excluding id.

fn propose(&mut self, payload: Vec<u8>) -> Result<u64, NotLeader>

Propose a decree; only the leader accepts. Returns the log index the decree occupies.

fn receive(&mut self, from: NodeId, message: RaftMessage, now: u64) -> Vec<(NodeId, RaftMessage)>

Deliver one message; returns messages to send.

Senders outside the immutable peer roster are ignored before their term, vote, or log contents can affect the replicated state machine.

fn role(&self) -> Role

Current role.

fn take_committed(&mut self) -> Vec<Decree>

Committed decrees not yet handed to the state machine. Each decree is delivered exactly once, in log order.

fn term(&self) -> Term

Current term.

fn tick(&mut self, now: u64) -> Vec<(NodeId, RaftMessage)>

Advance logical time; returns messages to send.