use std::error::Error;
use std::fmt;
use display_more::DisplayOptionExt;
use validit::Valid;
use validit::Validate;
use validit::less_equal;
use crate::RaftTypeConfig;
use crate::raft_state::IOId;
use crate::raft_state::io_state::io_progress::IOProgress;
use crate::raft_state::io_state::log_io_id::LogIOId;
use crate::raft_state::io_state::monotonic::MonotonicIncrease;
use crate::type_config::alias::LogIdOf;
use crate::type_config::alias::VoteOf;
pub(crate) mod io_id;
pub(crate) mod io_progress;
pub(crate) mod log_io_id;
pub(crate) mod monotonic;
#[derive(Debug, Clone)]
#[derive(PartialEq, Eq)]
pub(crate) struct IOState<C>
where C: RaftTypeConfig
{
building_snapshot: bool,
pub(crate) log_progress: Valid<IOProgress<IOId<C>>>,
pub(crate) apply_progress: Valid<IOProgress<LogIdOf<C>>>,
pub(crate) snapshot: Valid<IOProgress<LogIdOf<C>>>,
pub(crate) cluster_committed: MonotonicIncrease<LogIOId<C>>,
pub(crate) purged: Valid<IOProgress<LogIdOf<C>>>,
}
const LOG_PROGRESS_NAME: &str = "LogIO";
const APPLY_PROGRESS_NAME: &str = "Apply";
const SNAPSHOT_PROGRESS_NAME: &str = "Snapshot";
const PURGE_PROGRESS_NAME: &str = "Purge";
impl<C> Default for IOState<C>
where C: RaftTypeConfig
{
fn default() -> Self {
Self {
building_snapshot: false,
log_progress: new_progress(None, "xx", LOG_PROGRESS_NAME),
apply_progress: new_progress(None, "xx", APPLY_PROGRESS_NAME),
snapshot: new_progress(None, "xx", SNAPSHOT_PROGRESS_NAME),
cluster_committed: MonotonicIncrease::default(),
purged: new_progress(None, "xx", PURGE_PROGRESS_NAME),
}
}
}
impl<C> Validate for IOState<C>
where C: RaftTypeConfig
{
fn validate(&self) -> Result<(), Box<dyn Error>> {
self.log_progress.validate()?;
self.apply_progress.validate()?;
self.snapshot.validate()?;
less_equal!(self.snapshot.submitted(), self.apply_progress.submitted());
self.purged.validate()?;
less_equal!(self.purged.flushed(), self.snapshot.flushed());
Ok(())
}
}
impl<C> IOState<C>
where C: RaftTypeConfig
{
pub(crate) fn new(
id: &str,
vote: &VoteOf<C>,
applied: Option<LogIdOf<C>>,
snapshot: Option<LogIdOf<C>>,
purged: Option<LogIdOf<C>>,
) -> Self {
Self {
building_snapshot: false,
log_progress: new_progress(Some(IOId::new(vote)), id, LOG_PROGRESS_NAME),
apply_progress: new_progress(applied, id, APPLY_PROGRESS_NAME),
snapshot: new_progress(snapshot, id, SNAPSHOT_PROGRESS_NAME),
cluster_committed: MonotonicIncrease::default(),
purged: new_progress(purged, id, PURGE_PROGRESS_NAME),
}
}
pub(crate) fn applied(&self) -> Option<&LogIdOf<C>> {
self.apply_progress.flushed()
}
pub(crate) fn snapshot(&self) -> Option<&LogIdOf<C>> {
self.snapshot.flushed()
}
pub(crate) fn set_building_snapshot(&mut self, building: bool) {
self.building_snapshot = building;
}
pub(crate) fn building_snapshot(&self) -> bool {
self.building_snapshot
}
pub(crate) fn calculate_local_committed(&self) -> Option<LogIdOf<C>> {
let local_committed = self.do_calculate_local_committed();
tracing::debug!(
"{}, cluster_committed: {}, accepted: {}, local_committed: {}",
func_name!(),
self.cluster_committed.value().display(),
self.log_progress.accepted().display(),
local_committed.display()
);
local_committed
}
fn do_calculate_local_committed(&self) -> Option<LogIdOf<C>> {
let cluster_committed = self.cluster_committed.value()?;
let accepted = self.log_progress.accepted()?;
let reporting_leader_id = cluster_committed.committed_vote().committed_leader_id();
let committed_log_id = cluster_committed.last_log_id()?;
let committed_log_leader_id = committed_log_id.committed_leader_id();
let required_leader_id = std::cmp::max(&reporting_leader_id, committed_log_leader_id);
let accepted_leader_id = accepted.committed_leader_id()?;
if &accepted_leader_id >= required_leader_id {
let accepted_log_id = accepted.last_log_id()?;
Some(std::cmp::min(accepted_log_id, committed_log_id).clone())
} else {
None
}
}
}
fn new_progress<T>(initial_value: Option<T>, id: impl ToString, name: &'static str) -> Valid<IOProgress<T>>
where T: PartialOrd + fmt::Debug + fmt::Display + Clone {
Valid::new(IOProgress::new_synchronized(initial_value, id, name))
}
#[cfg(test)]
mod tests {
use super::IOState;
use crate::engine::testing::UTConfig;
use crate::engine::testing::log_id;
use crate::raft_state::IOId;
use crate::raft_state::io_state::log_io_id::LogIOId;
use crate::type_config::alias::VoteOf;
use crate::vote::raft_vote::RaftVoteExt;
#[test]
fn test_local_committed_requires_both_votes() {
for (commit_term, log_term, accepted_term, expected) in [
(1, 3, 1, None),
(3, 1, 1, None),
(1, 3, 3, Some(log_id(3, 0, 13))),
(3, 1, 3, Some(log_id(1, 0, 13))),
] {
let mut state = IOState::<UTConfig>::default();
let accepted_vote = VoteOf::<UTConfig>::new_committed(accepted_term, 0).to_committed();
let accepted = IOId::new_log_io(accepted_vote, Some(log_id(accepted_term, 0, 14)));
state.log_progress.accept(accepted);
let commit_vote = VoteOf::<UTConfig>::new_committed(commit_term, 0).to_committed();
let committed = LogIOId::new(commit_vote, Some(log_id(log_term, 0, 13)));
state.cluster_committed.try_update(committed).unwrap();
let actual = state.calculate_local_committed();
assert_eq!(expected, actual);
}
}
#[test]
fn test_purged_tracks_pending_io() {
let mut state = IOState::<UTConfig>::default();
let completed = log_id(1, 0, 2);
let submitted = log_id(1, 0, 3);
let accepted = log_id(1, 0, 4);
state.purged.try_update_all(completed);
state.purged.accept(accepted);
state.purged.submit(submitted);
let purged = state.purged.flushed();
assert_eq!(Some(&completed), purged);
state.purged.flush(submitted);
let purged = state.purged.flushed();
assert_eq!(Some(&submitted), purged);
let actual_accepted = state.purged.accepted();
assert_eq!(Some(&accepted), actual_accepted);
}
}