Skip to main content

kaya_raft/
message.rs

1use crate::{
2    log::LogEntry,
3    types::{LogIndex, NodeId, Term},
4};
5
6/// RequestVote RPC — sent by a candidate to request a vote.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct VoteRequest {
9    pub term: Term,
10    pub candidate_id: NodeId,
11    pub last_log_index: LogIndex,
12    pub last_log_term: Term,
13}
14
15/// RequestVote RPC response.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct VoteResponse {
18    pub term: Term,
19    pub vote_granted: bool,
20}
21
22/// AppendEntries RPC — sent by the leader for heartbeats and log replication.
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct AppendRequest {
25    pub term: Term,
26    pub leader_id: NodeId,
27    pub prev_log_index: LogIndex,
28    pub prev_log_term: Term,
29    pub entries: Vec<LogEntry>,
30    pub leader_commit: LogIndex,
31}
32
33/// AppendEntries RPC response.
34#[derive(Debug, Clone, PartialEq, Eq)]
35pub struct AppendResponse {
36    pub term: Term,
37    pub success: bool,
38    /// Highest log index stored on the responder after this RPC.
39    /// On failure: the follower's current `last_index` (back-off hint for leader).
40    pub match_index: LogIndex,
41}
42
43/// All messages exchanged between Raft nodes.
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub enum Message {
46    VoteRequest(VoteRequest),
47    VoteResponse(VoteResponse),
48    AppendRequest(AppendRequest),
49    AppendResponse(AppendResponse),
50    InstallSnapshotRequest(InstallSnapshotRequest),
51    InstallSnapshotResponse(InstallSnapshotResponse),
52    // Dynamic membership (prototype scaffolding)
53    ConfigChangeRequest(ConfigChangeRequest),
54    ConfigChangeResponse(ConfigChangeResponse),
55}
56
57/// Simple membership change request (prototype; full joint consensus later).
58#[derive(Debug, Clone, PartialEq, Eq)]
59pub struct ConfigChangeRequest {
60    pub term: Term,
61    pub old_peers: Vec<NodeId>,
62    pub new_peers: Vec<NodeId>,
63}
64
65#[derive(Debug, Clone, PartialEq, Eq)]
66pub struct ConfigChangeResponse {
67    pub term: Term,
68    pub success: bool,
69}
70
71/// Sent by leader to install a snapshot on a follower that is far behind
72/// or is a new node (Raft §7).
73#[derive(Debug, Clone, PartialEq, Eq)]
74pub struct InstallSnapshotRequest {
75    pub term: Term,
76    pub leader_id: NodeId,
77    pub last_included_index: LogIndex,
78    pub last_included_term: Term,
79    /// Opaque bytes representing the state machine snapshot at `last_included_index`.
80    pub data: Vec<u8>,
81}
82
83#[derive(Debug, Clone, PartialEq, Eq)]
84pub struct InstallSnapshotResponse {
85    pub term: Term,
86    pub success: bool,
87}
88
89/// A directed message between two Raft nodes.
90#[derive(Debug, Clone, PartialEq, Eq)]
91pub struct Envelope {
92    pub from: NodeId,
93    pub to: NodeId,
94    pub message: Message,
95}
96
97impl Envelope {
98    pub fn new(from: NodeId, to: NodeId, message: Message) -> Self {
99        Self { from, to, message }
100    }
101}