RaftEx.Server.RpcHandler (raft_ex v0.1.0)

View Source

RPC handler for Raft consensus protocol messages.

This module handles:

  • AppendEntries RPC and replies (log replication and heartbeats)
  • InstallSnapshot RPC and replies (snapshot transfer)
  • Heartbeat RPC and replies (consistent query support)
  • Log consistency checks and conflict resolution

AppendEntries Flow

  1. Leader sends AppendEntries with prev_log_index, prev_log_term, and entries
  2. Follower checks log consistency at prev_log_index
  3. If consistent, follower appends entries and replies with success
  4. If inconsistent, follower replies with failure and next_index hint
  5. Leader updates match_index and next_index for the peer

InstallSnapshot Flow

  1. Leader sends snapshot chunks when follower is too far behind
  2. Follower receives chunks and writes snapshot
  3. On completion, follower installs snapshot and truncates log
  4. Follower replies with last_index and last_term from snapshot

Heartbeat Flow

  1. Leader sends lightweight heartbeats for consistent queries
  2. Follower acknowledges with query_index
  3. Leader collects acknowledgements from majority
  4. Once majority acknowledges, query is safe to execute

Summary

Functions

Handle an AppendEntries reply from a follower.

Handle an incoming AppendEntries RPC from the leader.

Handle a Heartbeat reply from a follower.

Handle an incoming Heartbeat RPC from the leader.

Handle an InstallSnapshot reply from a follower.

Handle an incoming InstallSnapshot RPC from the leader.

Functions

handle_append_entries_reply(append_entries_reply, state)

@spec handle_append_entries_reply(RaftEx.Types.AppendEntriesReply.t(), map()) ::
  {map(), [term()]}

Handle an AppendEntries reply from a follower.

Returns {updated_state, effects}.

handle_append_entries_rpc(append_entries_rpc, state)

@spec handle_append_entries_rpc(RaftEx.Types.AppendEntriesRpc.t(), map()) ::
  {atom(), map(), [term()]}

Handle an incoming AppendEntries RPC from the leader.

Returns {next_state, updated_state, effects}.

handle_heartbeat_reply(heartbeat_reply, state)

@spec handle_heartbeat_reply(RaftEx.Types.HeartbeatReply.t(), map()) ::
  {map(), [term()]}

Handle a Heartbeat reply from a follower.

Returns {updated_state, effects}.

handle_heartbeat_rpc(heartbeat_rpc, state)

@spec handle_heartbeat_rpc(RaftEx.Types.HeartbeatRpc.t(), map()) :: {map(), [term()]}

Handle an incoming Heartbeat RPC from the leader.

Returns {updated_state, effects}.

handle_install_snapshot_reply(install_snapshot_result, state)

@spec handle_install_snapshot_reply(RaftEx.Types.InstallSnapshotResult.t(), map()) ::
  {map(), [term()]}

Handle an InstallSnapshot reply from a follower.

Returns {updated_state, effects}.

handle_install_snapshot_rpc(install_snapshot_rpc, state)

@spec handle_install_snapshot_rpc(RaftEx.Types.InstallSnapshotRpc.t(), map()) ::
  {atom(), map(), [term()]}

Handle an incoming InstallSnapshot RPC from the leader.

Returns {next_state, updated_state, effects}.