simraft_core/message.rs
1//! RPC messages between nodes, as structured values.
2//!
3//! The core never serializes. The canonical byte layout for these
4//! messages is specified in docs/ARCHITECTURE.md section 7.1 and is
5//! implemented by the simulator's packet layer and by future adapters.
6//! Message kinds are added per milestone: voting and `AppendEntries` at
7//! M1, `InstallSnapshot` at M3.
8
9use crate::ids::{LogIndex, Term};
10use crate::log::LogEntry;
11use crate::membership::ClusterConfig;
12
13/// One RPC message.
14///
15/// The sender and recipient travel in the envelope
16/// (`Event::Message::from` and `Action::Send::to`); every variant carries
17/// the term it was created under so stale senders are detectable from the
18/// message alone.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub enum Message {
21 /// A candidate asks for a vote in a real election.
22 RequestVote {
23 /// The candidate's term.
24 term: Term,
25 /// The candidate's last log index.
26 last_log_index: LogIndex,
27 /// The term of the candidate's last log entry.
28 last_log_term: Term,
29 },
30 /// The voter's answer to `RequestVote`.
31 RequestVoteResponse {
32 /// The voter's current term when it answered.
33 term: Term,
34 /// Whether the vote was granted.
35 vote_granted: bool,
36 },
37 /// A pre-candidate asks whether it would win an election for the
38 /// hypothetical term `term` (the sender's current term + 1), without
39 /// the sender changing any persistent state.
40 RequestPreVote {
41 /// The hypothetical future term.
42 term: Term,
43 /// The pre-candidate's last log index.
44 last_log_index: LogIndex,
45 /// The term of the pre-candidate's last log entry.
46 last_log_term: Term,
47 },
48 /// The voter's answer to `RequestPreVote`. Granting it changed no
49 /// persistent state on the voter and did not reset its timer.
50 RequestPreVoteResponse {
51 /// The voter's current term when it answered.
52 term: Term,
53 /// Whether the pre-vote was granted.
54 vote_granted: bool,
55 },
56 /// The leader replicates entries (or, with no entries, heartbeats)
57 /// to a follower.
58 AppendEntries {
59 /// The leader's term.
60 term: Term,
61 /// The index immediately before the first entry in this batch.
62 prev_log_index: LogIndex,
63 /// The term of the entry at `prev_log_index`.
64 prev_log_term: Term,
65 /// The entries to append, in index order.
66 entries: Vec<LogEntry>,
67 /// The leader's commit index.
68 leader_commit: LogIndex,
69 /// The leader's current `ReadIndex` round token (0 means none;
70 /// `ReadIndex` activates at M2).
71 read_round: u64,
72 },
73 /// The follower's answer to `AppendEntries`.
74 AppendEntriesResponse {
75 /// The follower's current term when it answered.
76 term: Term,
77 /// Whether the batch was accepted.
78 success: bool,
79 /// On success, the highest index the follower now holds
80 /// contiguously from this leader. Zero on failure.
81 match_index: LogIndex,
82 /// On failure, the first index of the conflicting term in the
83 /// follower's log, or its last index + 1 when its log is too
84 /// short. Zero when not applicable.
85 conflict_index: LogIndex,
86 /// On failure, the term at `conflict_index` in the follower's
87 /// log, or term 0 when the log was simply too short.
88 conflict_term: Term,
89 /// The highest read round echoed by the follower (`ReadIndex`, M2).
90 read_round: u64,
91 },
92 /// One chunk of a snapshot transfer (docs/ARCHITECTURE.md section
93 /// 7.4). Chunks of one snapshot identity arrive in strictly
94 /// increasing offset order; `done` marks the final chunk.
95 InstallSnapshot {
96 /// The leader's term.
97 term: Term,
98 /// The snapshot's last included index.
99 last_included_index: LogIndex,
100 /// The term of the entry at `last_included_index`.
101 last_included_term: Term,
102 /// The configuration in force at `last_included_index`.
103 config: ClusterConfig,
104 /// The byte offset of `data` within the snapshot payload.
105 offset: u64,
106 /// The chunk bytes.
107 data: Vec<u8>,
108 /// Whether this is the final chunk.
109 done: bool,
110 },
111 /// The follower's answer to an `InstallSnapshot` chunk.
112 InstallSnapshotResponse {
113 /// The follower's current term when it answered.
114 term: Term,
115 /// The snapshot's last included index once installed, or the
116 /// follower's snapshot base index while chunks are in flight.
117 match_index: LogIndex,
118 },
119}