openraft 0.10.1

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

use base2histogram::Histogram;
use openraft_macros::since;

use crate::Config;
#[cfg(feature = "runtime-stats")]
use crate::Instant;
use crate::RaftTypeConfig;
use crate::core::NotificationName;
use crate::core::raft_msg::RaftMsgName;
use crate::core::runtime_stats::DisplayMode;
use crate::core::runtime_stats::RuntimeStatsDisplay;
use crate::core::runtime_stats::log_stage::LogStageHistograms;
use crate::core::runtime_stats::log_stage::LogStages;
use crate::core::stage::Stage;
use crate::engine::CommandName;
#[cfg(feature = "runtime-stats")]
use crate::type_config::TypeConfigExt;
use crate::type_config::alias::InstantOf;

/// Runtime statistics for Raft operations.
///
/// This is a volatile structure that is not persisted. It accumulates
/// statistics from the time the Raft node starts.
#[since(version = "0.10.0", change = "Add core_busy_histogram")]
#[since(version = "0.10.0", change = "Add core_busy_duration snapshot")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RuntimeStats<C>
where C: RaftTypeConfig
{
    /// Cumulative wall-clock time spent processing work in the core event loop.
    ///
    /// Includes awaits and scheduler delays during processing, but excludes the
    /// top-level wait for incoming events. This measures loop occupancy, not CPU time.
    /// Updated before the core waits for incoming events. A snapshot includes
    /// completed processing intervals; the current interval is included after
    /// the core next reaches that wait. Fetching a snapshot requires core progress.
    #[since(version = "0.10.0")]
    pub core_busy_duration: Duration,

    /// Distribution of wall-clock time, in microseconds, of one core-loop turn.
    ///
    /// Each sample is one completed interval added to [`Self::core_busy_duration`].
    /// The turn in progress is recorded when the loop next reaches the wait.
    #[since(version = "0.10.0")]
    pub core_busy_histogram: Histogram,

    /// Histogram of the number of log entries the state machine receives in one `apply()` call.
    ///
    /// One sample is recorded per [`RaftStateMachine::apply()`] call. The state-machine worker
    /// merges queued apply commands into one batch, so a sample may cover several commands; it
    /// shows the batch size the state machine's I/O actually sees.
    ///
    /// [`RaftStateMachine::apply()`]: crate::storage::RaftStateMachine::apply
    #[since(version = "0.10.1", change = "one sample per `apply()` call, not per command")]
    pub apply_batch: Histogram,

    /// Histogram tracking the distribution of log entry counts when appending to storage.
    ///
    /// This tracks how many log entries are included in each AppendEntries command
    /// submitted to the storage layer, helping identify write batch patterns and storage I/O
    /// efficiency.
    pub append_batch: Histogram,

    /// Histogram tracking the distribution of log entry counts in replication RPCs.
    ///
    /// This tracks how many log entries are included in each AppendEntries RPC
    /// sent to followers during replication, helping identify replication batch patterns.
    pub replicate_batch: Histogram,

    /// Histogram tracking the distribution of RaftMsg counts processed in a batch.
    ///
    /// This tracks how many RaftMsg are processed together before calling
    /// `run_engine_commands()`, helping identify message batching efficiency.
    pub raft_msg_per_run: Histogram,

    /// Histogram tracking the distribution of client write entries merged per batch.
    ///
    /// This tracks how many client write entries are merged together in each
    /// `RaftMsg::ClientWrite` after batching, helping identify batching efficiency.
    pub write_batch: Histogram,

    /// Histogram tracking the budget for RaftMsg processing.
    ///
    /// This tracks the maximum number of RaftMsg allowed to process in each
    /// `process_raft_msg()` call, helping understand load balancing behavior.
    pub raft_msg_budget: Histogram,

    /// Histogram tracking the budget for Notification processing.
    ///
    /// This tracks the maximum number of Notifications allowed to process in each
    /// `process_notification()` call, helping understand load balancing behavior.
    pub notification_budget: Histogram,

    /// Histogram tracking RaftMsg budget utilization in permille (0-1000).
    ///
    /// This tracks `processed * 1000 / budget` for each `process_raft_msg()` call,
    /// where 1000 means 100% utilization. Helps identify if the budget is well-tuned.
    pub raft_msg_usage_permille: Histogram,

    /// Histogram tracking Notification budget utilization in permille (0-1000).
    ///
    /// This tracks `processed * 1000 / budget` for each `process_notification()` call,
    /// where 1000 means 100% utilization. Helps identify if the budget is well-tuned.
    pub notification_usage_permille: Histogram,

    /// Count of each command type executed.
    ///
    /// This tracks how many times each command type has been executed,
    /// useful for understanding workload patterns and debugging.
    /// Indexed by `CommandName::index()`.
    pub command_counts: Vec<u64>,

    /// Count of each RaftMsg type received.
    ///
    /// This tracks how many times each RaftMsg type has been received,
    /// useful for understanding API usage patterns and debugging.
    /// Indexed by `stats::RaftMsgName::index()`.
    pub raft_msg_counts: Vec<u64>,

    /// Count of each Notification type received.
    ///
    /// This tracks how many times each Notification type has been received,
    /// useful for understanding internal message patterns and debugging.
    /// Indexed by `NotificationName::index()`.
    pub notification_counts: Vec<u64>,

    /// Per-entry timestamp tracking across 6 lifecycle stages
    /// (Proposed, Received, Submitted, Persisted, Committed, Applied).
    ///
    /// Uses a fixed-capacity ring buffer per stage.
    /// Stage-to-stage gaps reveal where latency accumulates
    /// (channel queueing, storage I/O, replication, state machine apply).
    pub log_stage: LogStages<InstantOf<C>>,

    /// Precomputed stage-to-stage duration histograms derived from [`log_stage`](Self::log_stage).
    ///
    /// Rebuilt on demand via [`build_log_stage_histograms()`](Self::build_log_stage_histograms).
    pub log_stage_histograms: LogStageHistograms,

    _phantom: PhantomData<C>,
}

impl<C> Default for RuntimeStats<C>
where C: RaftTypeConfig
{
    fn default() -> Self {
        Self::new(&Config::default())
    }
}

impl<C> RuntimeStats<C>
where C: RaftTypeConfig
{
    pub(crate) fn new(config: &Config) -> Self {
        let _ = config;
        Self {
            core_busy_duration: Duration::ZERO,
            core_busy_histogram: Histogram::<()>::new(),
            apply_batch: Histogram::<()>::new(),
            append_batch: Histogram::<()>::new(),
            replicate_batch: Histogram::<()>::new(),
            raft_msg_per_run: Histogram::<()>::new(),
            write_batch: Histogram::<()>::new(),
            raft_msg_budget: Histogram::<()>::new(),
            notification_budget: Histogram::<()>::new(),
            raft_msg_usage_permille: Histogram::<()>::new(),
            notification_usage_permille: Histogram::<()>::new(),
            command_counts: vec![0; CommandName::COUNT],
            raft_msg_counts: vec![0; RaftMsgName::COUNT],
            notification_counts: vec![0; NotificationName::COUNT],
            log_stage: LogStages::new(config.log_stage_capacity(), 0),
            log_stage_histograms: LogStageHistograms::new(),
            _phantom: PhantomData,
        }
    }

    /// Start one core-loop busy interval.
    ///
    /// Returns `None` when the `runtime-stats` feature is off.
    #[cfg(feature = "runtime-stats")]
    pub(crate) fn begin_core_busy(&self) -> Option<InstantOf<C>> {
        Some(C::now())
    }

    /// Start one core-loop busy interval.
    #[cfg(not(feature = "runtime-stats"))]
    pub(crate) fn begin_core_busy(&self) -> Option<InstantOf<C>> {
        None
    }

    /// Add one core-loop busy interval to [`Self::core_busy_duration`] and
    /// [`Self::core_busy_histogram`].
    ///
    /// The histogram sample is the elapsed time since `since`, in microseconds.
    /// `None` records nothing.
    pub(crate) fn end_core_busy(&mut self, since: Option<InstantOf<C>>) {
        #[cfg(feature = "runtime-stats")]
        {
            let Some(since) = since else {
                return;
            };
            let now = C::now();
            let elapsed = now.saturating_duration_since(since);
            self.core_busy_duration += elapsed;
            let micros = elapsed.as_micros() as u64;
            self.core_busy_histogram.record(micros);
        }

        #[cfg(not(feature = "runtime-stats"))]
        {
            let _ = since;
        }
    }

    /// Record the execution of a command.
    pub fn record_command(&mut self, name: CommandName) {
        self.command_counts[name.index()] += 1;
    }

    /// Record the receipt of a RaftMsg.
    pub fn record_raft_msg(&mut self, name: RaftMsgName) {
        self.raft_msg_counts[name.index()] += 1;
    }

    /// Record the receipt of a Notification.
    pub fn record_notification(&mut self, name: NotificationName) {
        self.notification_counts[name.index()] += 1;
    }

    /// Record that the log entry at `index` reached `stage` now.
    ///
    /// Without the `runtime-stats` feature, it does nothing.
    pub fn record_log_stage_now(&mut self, stage: Stage, index: u64) {
        #[cfg(feature = "runtime-stats")]
        self.record_log_stage(stage, index, C::now());

        #[cfg(not(feature = "runtime-stats"))]
        {
            let _ = (stage, index);
        }
    }

    /// Record that the log entry at `index` reached `stage` at `now`.
    ///
    /// Without the `runtime-stats` feature, it does nothing.
    pub fn record_log_stage(&mut self, stage: Stage, index: u64, now: InstantOf<C>) {
        #[cfg(feature = "runtime-stats")]
        self.log_stage.record_stage(stage, index, now);

        #[cfg(not(feature = "runtime-stats"))]
        {
            let _ = (stage, index, now);
        }
    }

    /// Compute `log_stage_histograms` from the recorded log stages.
    ///
    /// Without the `runtime-stats` feature, it does nothing.
    pub fn build_log_stage_histograms(&mut self) {
        #[cfg(feature = "runtime-stats")]
        {
            self.log_stage_histograms = self.log_stage.compute_histograms();
        }
    }

    /// Returns a displayable representation of the runtime statistics.
    ///
    /// All values are precomputed when calling this method, so the returned
    /// `RuntimeStatsDisplay` can be cheaply formatted multiple times.
    ///
    /// Use builder methods like `.human_readable()` to change the display mode.
    pub fn display(&self) -> RuntimeStatsDisplay<C> {
        RuntimeStatsDisplay {
            mode: DisplayMode::default(),
            core_busy_duration: self.core_busy_duration,
            core_busy_histogram: self.core_busy_histogram.percentile_stats(),
            apply_batch: self.apply_batch.percentile_stats(),
            append_batch: self.append_batch.percentile_stats(),
            replicate_batch: self.replicate_batch.percentile_stats(),
            raft_msg_per_run: self.raft_msg_per_run.percentile_stats(),
            write_batch: self.write_batch.percentile_stats(),
            raft_msg_budget: self.raft_msg_budget.percentile_stats(),
            notification_budget: self.notification_budget.percentile_stats(),
            raft_msg_usage_permille: self.raft_msg_usage_permille.percentile_stats(),
            notification_usage_permille: self.notification_usage_permille.percentile_stats(),
            command_counts: self.command_counts.clone(),
            raft_msg_counts: self.raft_msg_counts.clone(),
            notification_counts: self.notification_counts.clone(),
            log_stage_percentiles: self.log_stage_histograms.percentile_stats_array(),
            log_stage_histograms: self.log_stage_histograms.clone(),
            log_stages: self.log_stage.clone(),
        }
    }
}