1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/// All possible states of a Raft node.
#[derive(Debug, Clone, Copy, Default)]
#[derive(PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ServerState {
/// The node has no campaign, read voter, or write voter role in the effective membership.
///
/// Uninitialized and removed nodes also use this passive state.
#[default]
Learner,
/// The node is in the effective membership, is not classified as a learner, and is neither a
/// candidate nor a leader.
///
/// This state does not imply that the node has [`NodeRole::ReceiveLogs`]. For example, a read
/// voter that is not a write voter can be a follower without receiving logs.
///
/// [`NodeRole::ReceiveLogs`]: crate::membership::NodeRole::ReceiveLogs
Follower,
/// The node is campaigning to become the cluster leader.
Candidate,
/// The node is the Raft cluster leader.
Leader,
/// The Raft node is shutting down.
Shutdown,
}
impl ServerState {
/// Check if currently in learner state.
pub fn is_learner(&self) -> bool {
matches!(self, Self::Learner)
}
/// Check if currently in follower state.
pub fn is_follower(&self) -> bool {
matches!(self, Self::Follower)
}
/// Check if currently in candidate state.
pub fn is_candidate(&self) -> bool {
matches!(self, Self::Candidate)
}
/// Check if currently in leader state.
pub fn is_leader(&self) -> bool {
matches!(self, Self::Leader)
}
}