RaftEx.Server (raft_ex v0.1.0)

View Source

Core Raft server logic: leader election, log replication, and state machine application.

This module is a pure-functional layer — it receives messages, computes the next state and a list of effects, and returns them to the caller (RaftEx.ServerProc), which owns the OTP process and actually executes the effects.

Server state map keys

KeyDescription
:cfgRaftEx.Server.Config struct (immutable mostly)
:leader_idCurrent known leader, or nil
:current_termRaft term counter
:cluster%{server_id => peer_state}
:cluster_change_permittedWhether a cluster change may be appended now
:cluster_index_term{index, term} of the last cluster-change entry
:voted_forServer voted for in current term, or nil
:membershipThis server's own membership role
:commit_indexHighest log index known to be committed
:last_appliedHighest index applied to the state machine
:persisted_last_appliedValue of :last_applied written to durable meta
:logRaftEx.Log state
:machine_stateUser state machine state
:aux_stateAuxiliary state machine state
:query_indexMonotonic index for consistent query tracking
:queries_waiting_heartbeatsQueue of {query_index, query_ref} tuples
:pending_consistent_queriesQueries queued awaiting cluster-change perm

Summary

Functions

checkpoint(index, mac_state, state)

@spec checkpoint(non_neg_integer(), term(), map()) :: {map(), list()}

clear_leader_id(state)

current_term(map)

fetch_term(idx, state)

@spec fetch_term(non_neg_integer(), map()) :: {non_neg_integer() | nil, map()}

get_membership(state)

get_membership(cluster, state)

handle_aux(raft_state, type, cmd, state)

@spec handle_aux(atom(), term(), term(), map()) :: {atom(), map(), list()}

handle_state_enter(raft_state, state0)

@spec handle_state_enter(atom(), map()) :: {map(), list()}

id(map)

init(config)

@spec init(map()) :: map()

Build the initial server state from a configuration map.

is_fully_persisted?(map)

is_new?(map)

leader_id(state)

log_id(map)

log_tick(state)

@spec log_tick(map()) :: map()

machine(map)

machine_query(query_fun, map)

machine_version(map)

make_rpcs(state)

@spec make_rpcs(map()) :: {map(), list()}

Build AppendEntries RPCs for all peers (used on heartbeat / tick).

overview(state)

peers(map)

persist_last_applied(state)

@spec persist_last_applied(map()) :: map()

Persist last_applied to durable meta if it has advanced.

promote_checkpoint(index, state)

@spec promote_checkpoint(non_neg_integer(), map()) :: {map(), list()}

recover(state0)

@spec recover(map()) :: map()

Replay committed log entries onto the state machine during startup.

state_query(arg1, state)

system_config(map)

terminate(state, reason)

@spec terminate(map(), term()) :: :ok

tick(map)

@spec tick(map()) :: list()

transform_for_partial_read(idx, term, cmd)

uid(map)

update_peer(peer_id, update, state)

update_release_cursor(index, mac_state, opts, state)

@spec update_release_cursor(non_neg_integer(), term(), map(), map()) ::
  {map(), list()}