Skip to main content

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