simraft_core/error.rs
1//! Domain error types for simraft-core.
2//!
3//! Every error carries concrete diagnostic context (the offending values,
4//! the required bounds, the conflicting fields), never a bare variant
5//! name. See invariant E-01 in docs/INVARIANTS.md.
6
7use crate::ids::{LogIndex, NodeId, Term};
8use core::fmt;
9
10/// A cluster configuration failed validation.
11///
12/// Returned by `Config::validate` and by `Node::new`. Each variant names
13/// the fields involved and the values that conflict, so an operator can
14/// fix the configuration without reading the validation code.
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum ConfigError {
17 /// A field that must be positive was zero.
18 ZeroValue {
19 /// The field that was zero.
20 field: &'static str,
21 },
22 /// `min_election_timeout` is below `10 * heartbeat_interval`.
23 ElectionTimeoutRatio {
24 /// The configured minimum election timeout, in ticks.
25 min_election_timeout: u64,
26 /// The configured heartbeat interval, in ticks.
27 heartbeat_interval: u64,
28 /// The smallest legal minimum election timeout for this heartbeat
29 /// interval, in ticks.
30 required_min: u64,
31 },
32 /// `max_election_timeout` is below `2 * min_election_timeout`.
33 ElectionRangeRatio {
34 /// The configured minimum election timeout, in ticks.
35 min_election_timeout: u64,
36 /// The configured maximum election timeout, in ticks.
37 max_election_timeout: u64,
38 /// The smallest legal maximum election timeout, in ticks.
39 required_min_max: u64,
40 },
41 /// `max_election_timeout` does not exceed `min_election_timeout`, so
42 /// the randomized election window is empty.
43 ElectionRangeOrder {
44 /// The configured minimum election timeout, in ticks.
45 min_election_timeout: u64,
46 /// The configured maximum election timeout, in ticks.
47 max_election_timeout: u64,
48 },
49 /// A proposed cluster configuration has no voters.
50 EmptyVoters,
51}
52
53impl fmt::Display for ConfigError {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 match self {
56 Self::ZeroValue { field } => {
57 write!(f, "config field {field} must be positive, got 0")
58 }
59 Self::ElectionTimeoutRatio {
60 min_election_timeout,
61 heartbeat_interval,
62 required_min,
63 } => write!(
64 f,
65 "min_election_timeout {min_election_timeout} is below 10 * heartbeat_interval \
66 {heartbeat_interval}; required minimum is {required_min}"
67 ),
68 Self::ElectionRangeRatio {
69 min_election_timeout,
70 max_election_timeout,
71 required_min_max,
72 } => write!(
73 f,
74 "max_election_timeout {max_election_timeout} is below 2 * min_election_timeout \
75 {min_election_timeout}; required minimum is {required_min_max}"
76 ),
77 Self::ElectionRangeOrder {
78 min_election_timeout,
79 max_election_timeout,
80 } => write!(
81 f,
82 "max_election_timeout {max_election_timeout} must exceed \
83 min_election_timeout {min_election_timeout}"
84 ),
85 Self::EmptyVoters => {
86 write!(f, "cluster configuration must keep at least one voter")
87 }
88 }
89 }
90}
91
92impl std::error::Error for ConfigError {}
93
94/// A protocol-level rejection with the deciding values named.
95///
96/// The request was well formed but the protocol rules forbid it in
97/// the current state. Every variant names the values that decided
98/// the rejection (invariant E-01, docs/ARCHITECTURE.md section 13).
99#[derive(Debug, Clone, PartialEq, Eq)]
100pub enum ProtocolError {
101 /// A configuration change was proposed while an earlier one is
102 /// still uncommitted (invariant M-01).
103 ConfigChangePending {
104 /// The log index of the newest, still uncommitted config entry.
105 last_config_index: LogIndex,
106 /// The leader's commit index when the change was rejected.
107 commit_index: LogIndex,
108 },
109 /// A configuration change was proposed before the leader's
110 /// current-term `NoOp` entry committed (invariant M-02).
111 LeaderNoopUncommitted {
112 /// The index of the leader's election `NoOp` entry.
113 noop_index: LogIndex,
114 /// The leader's commit index when the change was rejected.
115 commit_index: LogIndex,
116 },
117 /// A learner was proposed for promotion before it had replicated
118 /// through the leader's commit index (invariant M-04).
119 LearnerNotCaughtUp {
120 /// The learner proposed for promotion.
121 node: NodeId,
122 /// The learner's match index on the leader.
123 match_index: LogIndex,
124 /// The index the learner must reach before promotion.
125 required: LogIndex,
126 },
127 /// An `AddLearner` or `PromoteVoter` named a node that is already
128 /// a voter.
129 AlreadyVoter {
130 /// The node named by the change.
131 node: NodeId,
132 },
133 /// A `PromoteVoter` named a node that is not a learner.
134 NotLearner {
135 /// The node named by the change.
136 node: NodeId,
137 },
138 /// A change named a node that is not a member in either role.
139 NotMember {
140 /// The node named by the change.
141 node: NodeId,
142 },
143 /// A `RemoveVoter` named the last remaining voter; a cluster may
144 /// shrink to one voter, never to none.
145 LastVoterRemoval {
146 /// The node named by the change.
147 node: NodeId,
148 },
149}
150
151impl fmt::Display for ProtocolError {
152 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153 match self {
154 Self::ConfigChangePending {
155 last_config_index,
156 commit_index,
157 } => write!(
158 f,
159 "config change rejected: entry at index {last_config_index} is still \
160 uncommitted (commit index {commit_index})"
161 ),
162 Self::LeaderNoopUncommitted {
163 noop_index,
164 commit_index,
165 } => write!(
166 f,
167 "config change rejected: leader NoOp at index {noop_index} has not \
168 committed (commit index {commit_index})"
169 ),
170 Self::LearnerNotCaughtUp {
171 node,
172 match_index,
173 required,
174 } => write!(
175 f,
176 "learner {node} cannot be promoted: match index {match_index} is below \
177 the required index {required}"
178 ),
179 Self::AlreadyVoter { node } => {
180 write!(f, "node {node} is already a voter")
181 }
182 Self::NotLearner { node } => {
183 write!(f, "node {node} is not a learner")
184 }
185 Self::NotMember { node } => {
186 write!(f, "node {node} is not a cluster member")
187 }
188 Self::LastVoterRemoval { node } => {
189 write!(
190 f,
191 "removing node {node} would leave the cluster with no voters"
192 )
193 }
194 }
195 }
196}
197
198impl std::error::Error for ProtocolError {}
199
200/// A snapshot transfer failed. The variant names the snapshot identity
201/// and the offsets that decided the failure (docs/ARCHITECTURE.md
202/// section 13).
203#[derive(Debug, Clone, PartialEq, Eq)]
204pub enum SnapshotError {
205 /// A chunk arrived at an offset other than the number of bytes
206 /// already buffered for its snapshot identity (invariant N-02).
207 ChunkOffset {
208 /// The snapshot's last included index.
209 last_included_index: LogIndex,
210 /// The snapshot's last included term.
211 last_included_term: Term,
212 /// The offset the follower was ready to accept.
213 expected_offset: u64,
214 /// The offset the chunk actually carried.
215 received_offset: u64,
216 },
217}
218
219impl fmt::Display for SnapshotError {
220 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
221 match self {
222 Self::ChunkOffset {
223 last_included_index,
224 last_included_term,
225 expected_offset,
226 received_offset,
227 } => write!(
228 f,
229 "snapshot chunk for (index {last_included_index}, term {last_included_term}) \
230 arrived at offset {received_offset}, expected offset {expected_offset}"
231 ),
232 }
233 }
234}
235
236impl std::error::Error for SnapshotError {}