aarondb/raft_runtime

raft_runtime — deterministic durable Raft protocol model

A transport-free reference runtime. Adapters persist HardState and deliver authenticated RPCs; this module makes protocol decisions and never claims network durability on its own.

Types

pub type HardState {
  HardState(
    term: Int,
    voted_for: option.Option(String),
    commit_index: Int,
  )
}

Constructors

  • HardState(
      term: Int,
      voted_for: option.Option(String),
      commit_index: Int,
    )
pub type LogEntry {
  LogEntry(term: Int, command: String)
}

Constructors

  • LogEntry(term: Int, command: String)
pub type LogIndex =
  Int
pub type Member {
  Voter(id: String)
  Learner(id: String)
}

Constructors

  • Voter(id: String)
  • Learner(id: String)
pub type NodeId =
  String

The durable recovery boundary. Persist this value atomically before exposing the corresponding reply or applying any command.

pub type Persisted {
  Persisted(
    hard: HardState,
    log: List(LogEntry),
    snapshot: option.Option(Snapshot),
    last_applied: Int,
  )
}

Constructors

pub type Reply {
  VoteGranted(term: Int, granted: Bool)
  AppendAccepted(term: Int, matched: Int)
  AppendRejected(term: Int, next_index: Int)
  SnapshotAccepted(term: Int, index: Int)
  ReadIndexAccepted(term: Int, index: Int)
  StaleTerm(term: Int)
}

Constructors

  • VoteGranted(term: Int, granted: Bool)
  • AppendAccepted(term: Int, matched: Int)
  • AppendRejected(term: Int, next_index: Int)
  • SnapshotAccepted(term: Int, index: Int)
  • ReadIndexAccepted(term: Int, index: Int)
  • StaleTerm(term: Int)
pub type Role {
  Follower
  Candidate
  Leader
}

Constructors

  • Follower
  • Candidate
  • Leader
pub type Rpc {
  RequestVote(
    term: Int,
    candidate: String,
    last_index: Int,
    last_term: Int,
  )
  AppendEntries(
    term: Int,
    leader: String,
    prev_index: Int,
    prev_term: Int,
    entries: List(LogEntry),
    leader_commit: Int,
  )
  InstallSnapshot(term: Int, leader: String, snapshot: Snapshot)
  ReadIndex(term: Int, leader: String, committed: Int)
}

Constructors

  • RequestVote(
      term: Int,
      candidate: String,
      last_index: Int,
      last_term: Int,
    )
  • AppendEntries(
      term: Int,
      leader: String,
      prev_index: Int,
      prev_term: Int,
      entries: List(LogEntry),
      leader_commit: Int,
    )
  • InstallSnapshot(term: Int, leader: String, snapshot: Snapshot)
  • ReadIndex(term: Int, leader: String, committed: Int)
pub type Snapshot {
  Snapshot(index: Int, term: Int, state: String)
}

Constructors

  • Snapshot(index: Int, term: Int, state: String)
pub type State {
  State(
    node: String,
    role: Role,
    hard: HardState,
    members: List(Member),
    log: List(LogEntry),
    last_applied: Int,
    leader: option.Option(String),
    snapshot: option.Option(Snapshot),
  )
}

Constructors

Values

pub fn add_learner(state: State, id: String) -> State
pub fn apply_committed(state: State) -> Result(State, String)
pub fn bootstrap_leader(state: State) -> State

The only automatic bootstrap: a one-voter cluster. Multi-node bootstrap needs externally authenticated member configuration.

pub fn commit_quorum(
  state: State,
  index: Int,
  replicated: Int,
) -> State

A leader may commit an index only after caller evidence proves current-term quorum replication.

pub fn compact(
  state: State,
  index: Int,
  state_image: String,
) -> Result(State, String)
pub fn handle(state: State, rpc: Rpc) -> #(State, Reply)
pub fn last_index(state: State) -> Int
pub fn last_term(state: State) -> Int
pub fn new(node: String, members: List(Member)) -> State
pub fn persist(state: State) -> Persisted
pub fn promote_voter(state: State, id: String) -> State

Promotion is deliberately explicit so an adapter can make it a committed joint-consensus configuration entry rather than a local mutation.

pub fn quorum(state: State) -> Int
pub fn recover(
  node: String,
  members: List(Member),
  saved: Persisted,
) -> State

Restores only durable state. Leadership never survives recovery; the node must establish a current-term quorum again.

pub fn start_election(state: State) -> State

Begins an election only for voting members. Persist the returned hard state before dispatching vote RPCs.

pub fn win_election(state: State, granted_votes: Int) -> State

Turns a candidate into leader only from explicit, same-term quorum evidence.

Search Document