openraft 0.10.2

Advanced Raft consensus
Documentation
use std::time::Duration;

use crate::RaftTypeConfig;
use crate::core::heartbeat::errors::HeartbeatFailure;
use crate::raft::AppendResult;

/// The heartbeats to one target that failed in a row, and the rule that logs them.
///
/// Only the first failure in a row logs a warning, so that a target that is down, and fails every
/// heartbeat, logs one warning instead of one per heartbeat. The next reply in time ends the
/// failures, and logs how many there were.
pub(crate) struct FailureLog<'a, C>
where C: RaftTypeConfig
{
    pub(crate) id: &'a C::NodeId,

    pub(crate) target: &'a C::NodeId,

    /// The heartbeats in a row that got no reply in time, since the target last replied in time.
    pub(crate) failed_in_row: &'a mut u64,
}

impl<C> FailureLog<'_, C>
where C: RaftTypeConfig
{
    /// Counts a failed heartbeat, and logs it: the first failure in a row at warn level, and the
    /// others at debug level.
    pub(crate) fn failed(&mut self, failure: HeartbeatFailure<C>) {
        *self.failed_in_row += 1;
        if *self.failed_in_row == 1 {
            tracing::warn!(
                "id={} target={} heartbeat failed; until the target replies in time, failures log at debug level: {}",
                self.id,
                self.target,
                failure
            );
        } else {
            tracing::debug!(
                "id={} target={} heartbeat failed again, {} in a row: {}",
                self.id,
                self.target,
                self.failed_in_row,
                failure
            );
        }
    }

    /// Counts and logs a heartbeat that ended in time, and returns its reply, if it got one.
    pub(crate) fn ended(&mut self, result: Result<AppendResult<C>, HeartbeatFailure<C>>) -> Option<AppendResult<C>> {
        match result {
            Ok(reply) => {
                self.replied();
                Some(reply)
            }
            Err(failure) => {
                self.failed(failure);
                None
            }
        }
    }

    /// Counts and logs a heartbeat that failed at its deadline, and ended `elapsed` after it was
    /// sent, and returns its late reply, if it got one.
    ///
    /// No heartbeat went out while this one was in flight, so each `interval` it took after the
    /// first counts as another failed heartbeat, as if each of them had failed on its own.
    pub(crate) fn ended_late(
        &mut self,
        result: Result<AppendResult<C>, HeartbeatFailure<C>>,
        elapsed: Duration,
        interval: Duration,
    ) -> Option<AppendResult<C>> {
        *self.failed_in_row += (elapsed.div_duration_f64(interval) as u64).saturating_sub(1);

        match &result {
            Ok(_) => tracing::debug!(
                "id={} target={} a late reply arrived, {:?} after the heartbeat was sent",
                self.id,
                self.target,
                elapsed
            ),
            Err(failure) => tracing::debug!(
                "id={} target={} the heartbeat ended without a reply, {:?} after it was sent: {}",
                self.id,
                self.target,
                elapsed,
                failure
            ),
        }
        result.ok()
    }

    /// Ends the failures in a row at a reply in time, and logs how many there were: at debug
    /// level after a single failure, so that an isolated failure, which a loaded node sees often,
    /// logs its warning and nothing else above debug level.
    fn replied(&mut self) {
        match *self.failed_in_row {
            0 => {}
            1 => tracing::debug!(
                "id={} target={} the target replied after 1 failed heartbeat",
                self.id,
                self.target
            ),
            n => tracing::info!(
                "id={} target={} the target replied after {} failed heartbeats",
                self.id,
                self.target,
                n
            ),
        }
        *self.failed_in_row = 0;
    }
}