openraft 0.10.0-alpha.18

Advanced Raft consensus
Documentation
use std::fmt;

use display_more::DisplayOptionExt;

use crate::RaftTypeConfig;
use crate::display_ext::DisplayInstantExt;
use crate::type_config::alias::InstantOf;
use crate::type_config::alias::LogIdOf;

/// The information for broadcasting a heartbeat.
#[derive(Debug, Clone)]
#[derive(PartialEq, Eq)]
pub(crate) struct HeartbeatEvent<C>
where C: RaftTypeConfig
{
    /// The timestamp when this heartbeat is sent.
    ///
    /// The Leader uses this sending time to calculate the quorum acknowledge time, but not the
    /// receiving timestamp.
    pub(crate) time: InstantOf<C>,

    /// The last known matching log id that has been confirmed replicated to the target follower.
    ///
    /// This is used as `prev_log_id` in heartbeat AppendEntries to ensure the follower has this
    /// log id, avoiding false conflict responses that could be misinterpreted as log reversion.
    pub(crate) matching: Option<LogIdOf<C>>,

    /// The last known committed log id of the Leader.
    ///
    /// When there are no new logs to replicate, the Leader sends a heartbeat to replicate committed
    /// log id to followers to update their committed log id.
    pub(crate) committed: Option<LogIdOf<C>>,
}

impl<C> fmt::Display for HeartbeatEvent<C>
where C: RaftTypeConfig
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "(time={}, matching: {}, committed: {})",
            self.time.display(),
            self.matching.display(),
            self.committed.display()
        )
    }
}