use std::fmt;
use display_more::DisplaySliceExt;
use openraft_macros::since;
use crate::RaftTypeConfig;
use crate::raft::ConflictHint;
use crate::raft::StreamAppendError;
use crate::type_config::alias::LogIdOf;
use crate::type_config::alias::VoteOf;
#[since(version = "0.10.0")]
pub type AppendResult<C> = Result<Option<LogIdOf<C>>, AppendError<C>>;
#[since(version = "0.10.0")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AppendError<C>
where C: RaftTypeConfig
{
Conflict {
log_id: LogIdOf<C>,
hint: Option<ConflictHint<C>>,
},
HigherVote(VoteOf<C>),
}
impl<C> fmt::Display for AppendError<C>
where C: RaftTypeConfig
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AppendError::Conflict { log_id, hint } => {
write!(f, "Conflict({})", log_id)?;
if let Some(hint) = hint {
write!(f, ", hint: {}", hint.log_ids.display())?;
}
Ok(())
}
AppendError::HigherVote(vote) => write!(f, "HigherVote({})", vote),
}
}
}
impl<C> From<StreamAppendError<C>> for AppendError<C>
where C: RaftTypeConfig
{
fn from(error: StreamAppendError<C>) -> Self {
match error {
StreamAppendError::Conflict(log_id) => AppendError::Conflict { log_id, hint: None },
StreamAppendError::HigherVote(vote) => AppendError::HigherVote(vote),
}
}
}
impl<C> From<AppendError<C>> for StreamAppendError<C>
where C: RaftTypeConfig
{
fn from(error: AppendError<C>) -> Self {
match error {
AppendError::Conflict { log_id, hint: _ } => StreamAppendError::Conflict(log_id),
AppendError::HigherVote(vote) => StreamAppendError::HigherVote(vote),
}
}
}
#[cfg(test)]
mod tests {
use crate::Vote;
use crate::engine::testing::UTConfig;
use crate::engine::testing::log_id;
use crate::raft::AppendError;
use crate::raft::ConflictHint;
use crate::raft::StreamAppendError;
#[test]
fn stream_append_error_converts_without_a_hint() {
let conflict = StreamAppendError::<UTConfig>::Conflict(log_id(1, 1, 5));
let expected = AppendError::Conflict {
log_id: log_id(1, 1, 5),
hint: None,
};
let converted = AppendError::from(conflict);
assert_eq!(expected, converted);
let higher = StreamAppendError::<UTConfig>::HigherVote(Vote::new(2, 1));
let expected = AppendError::HigherVote(Vote::new(2, 1));
let converted = AppendError::from(higher);
assert_eq!(expected, converted);
}
#[test]
fn conversion_to_stream_append_error_drops_the_hint() {
let hint = ConflictHint::new(vec![log_id(1, 1, 3)]);
let conflict = AppendError::<UTConfig>::Conflict {
log_id: log_id(1, 1, 5),
hint: Some(hint),
};
let expected = StreamAppendError::Conflict(log_id(1, 1, 5));
let converted = StreamAppendError::from(conflict);
assert_eq!(expected, converted);
}
#[test]
fn display_includes_the_hint() {
let hint = ConflictHint::new(vec![log_id(1, 1, 3)]);
let conflict = AppendError::<UTConfig>::Conflict {
log_id: log_id(1, 1, 5),
hint: Some(hint),
};
let without_hint = AppendError::<UTConfig>::Conflict {
log_id: log_id(1, 1, 5),
hint: None,
};
let conflict_text = conflict.to_string();
assert_eq!("Conflict(T1-N1.5), hint: [T1-N1.3]", conflict_text);
let without_hint_text = without_hint.to_string();
assert_eq!("Conflict(T1-N1.5)", without_hint_text);
}
}