use crate::ids::{LogIndex, NodeId, Term};
use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConfigError {
ZeroValue {
field: &'static str,
},
ElectionTimeoutRatio {
min_election_timeout: u64,
heartbeat_interval: u64,
required_min: u64,
},
ElectionRangeRatio {
min_election_timeout: u64,
max_election_timeout: u64,
required_min_max: u64,
},
ElectionRangeOrder {
min_election_timeout: u64,
max_election_timeout: u64,
},
EmptyVoters,
OverlappingMember {
node: NodeId,
},
}
impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ZeroValue { field } => {
write!(f, "config field {field} must be positive, got 0")
}
Self::ElectionTimeoutRatio {
min_election_timeout,
heartbeat_interval,
required_min,
} => write!(
f,
"min_election_timeout {min_election_timeout} is below 10 * heartbeat_interval \
{heartbeat_interval}; required minimum is {required_min}"
),
Self::ElectionRangeRatio {
min_election_timeout,
max_election_timeout,
required_min_max,
} => write!(
f,
"max_election_timeout {max_election_timeout} is below 2 * min_election_timeout \
{min_election_timeout}; required minimum is {required_min_max}"
),
Self::ElectionRangeOrder {
min_election_timeout,
max_election_timeout,
} => write!(
f,
"max_election_timeout {max_election_timeout} must exceed \
min_election_timeout {min_election_timeout}"
),
Self::EmptyVoters => {
write!(f, "cluster configuration must keep at least one voter")
}
Self::OverlappingMember { node } => {
write!(
f,
"node {node} cannot be both a voter and a learner in the same configuration"
)
}
}
}
}
impl std::error::Error for ConfigError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProtocolError {
ConfigChangePending {
last_config_index: LogIndex,
commit_index: LogIndex,
},
LeaderNoopUncommitted {
noop_index: LogIndex,
commit_index: LogIndex,
},
LearnerNotCaughtUp {
node: NodeId,
match_index: LogIndex,
required: LogIndex,
},
AlreadyVoter {
node: NodeId,
},
NotLearner {
node: NodeId,
},
NotMember {
node: NodeId,
},
LastVoterRemoval {
node: NodeId,
},
TooManyUncommitted {
uncommitted: usize,
limit: usize,
},
}
impl fmt::Display for ProtocolError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ConfigChangePending {
last_config_index,
commit_index,
} => write!(
f,
"config change rejected: entry at {last_config_index} is still \
uncommitted (commit {commit_index})"
),
Self::LeaderNoopUncommitted {
noop_index,
commit_index,
} => write!(
f,
"config change rejected: leader NoOp at {noop_index} has not \
committed (commit {commit_index})"
),
Self::LearnerNotCaughtUp {
node,
match_index,
required,
} => write!(
f,
"learner {node} cannot be promoted: match {match_index} is below \
the required {required}"
),
Self::AlreadyVoter { node } => {
write!(f, "node {node} is already a voter")
}
Self::NotLearner { node } => {
write!(f, "node {node} is not a learner")
}
Self::NotMember { node } => {
write!(f, "node {node} is not a cluster member")
}
Self::LastVoterRemoval { node } => {
write!(
f,
"removing node {node} would leave the cluster with no voters"
)
}
Self::TooManyUncommitted { uncommitted, limit } => {
write!(
f,
"proposal rejected: {uncommitted} uncommitted entries reach \
max_uncommitted_entries limit {limit}"
)
}
}
}
}
impl std::error::Error for ProtocolError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SnapshotError {
ChunkOffset {
last_included_index: LogIndex,
last_included_term: Term,
expected_offset: u64,
received_offset: u64,
},
}
impl fmt::Display for SnapshotError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ChunkOffset {
last_included_index,
last_included_term,
expected_offset,
received_offset,
} => write!(
f,
"snapshot chunk for ({last_included_index}, {last_included_term}) \
arrived at offset {received_offset}, expected offset {expected_offset}"
),
}
}
}
impl std::error::Error for SnapshotError {}