1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
use std::marker::PhantomData;
use std::time::Duration;
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::Histogram;
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.2", change = "Make record_log_stage*() crate-private")]
#[since(version = "0.10.2", change = "Add the re-arm wake and metrics send counters")]
#[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>,
/// Count of the times `RaftCore` woke the heartbeat timer's task to re-arm it.
///
/// `RaftCore` wakes the task when it sets a deadline the task could otherwise sleep past: one
/// earlier than the stored deadline, or any deadline when the stored one has passed or is
/// `None`. The deadline set after a heartbeat fires is one such wake, because the stored
/// deadline has just passed. The fires themselves are the `NotificationName::Tick` entries of
/// [`Self::notification_counts`].
///
/// A deadline moved later is stored without a wake. The task reads it when its timer expires
/// at the stored deadline, and that wake is not counted.
#[since(version = "0.10.2")]
pub heartbeat_rearm_wakes: u64,
/// Count of the times `RaftCore` woke the election timer's task to re-arm it.
///
/// The fires of this timer are the `NotificationName::ElectionDeadlineReached` entries of
/// [`Self::notification_counts`]. See [`Self::heartbeat_rearm_wakes`] for when `RaftCore`
/// wakes a timer's task.
#[since(version = "0.10.2")]
pub election_rearm_wakes: u64,
/// Count of the times `RaftCore` woke the pending-read timer's task to re-arm it.
///
/// The fires of this timer are the `NotificationName::PendingReadDeadlineReached` entries of
/// [`Self::notification_counts`]. See [`Self::heartbeat_rearm_wakes`] for when `RaftCore`
/// wakes a timer's task.
#[since(version = "0.10.2")]
pub pending_read_rearm_wakes: u64,
/// Count of the `RaftMetrics` sends: one per metrics report, whether or not a field changed.
#[since(version = "0.10.2")]
pub metrics_sends: u64,
/// Count of the `RaftDataMetrics` sends: the metrics reports that changed one of its fields.
#[since(version = "0.10.2")]
pub data_metrics_sends: u64,
/// Count of the `RaftServerMetrics` sends: the metrics reports that changed one of its fields.
#[since(version = "0.10.2")]
pub server_metrics_sends: 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(), 0)
}
}
impl<C> RuntimeStats<C>
where C: RaftTypeConfig
{
/// Create the statistics of a node whose state machine has applied the logs before
/// `log_stage_begin`, such as at startup: their stages were not observed, so the log stages
/// start at that index.
pub(crate) fn new(config: &Config, log_stage_begin: u64) -> Self {
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],
heartbeat_rearm_wakes: 0,
election_rearm_wakes: 0,
pending_read_rearm_wakes: 0,
metrics_sends: 0,
data_metrics_sends: 0,
server_metrics_sends: 0,
log_stage: LogStages::new(config.log_stage_capacity(), log_stage_begin),
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 logs before index `right` reached `stage` now.
///
/// `right` is the exclusive end of the batch that newly reached `stage`: pass the index of its
/// last log plus one. Entry 0 alone is recorded with `right = 1`, and a batch ending at index 7
/// with `right = 8`.
///
/// Without the `runtime-stats` feature, it does nothing.
pub(crate) fn record_log_stage_now(&mut self, stage: Stage, right: u64) {
#[cfg(feature = "runtime-stats")]
self.record_log_stage(stage, right, C::now());
#[cfg(not(feature = "runtime-stats"))]
{
let _ = (stage, right);
}
}
/// Record that the logs before index `right` reached `stage` at `now`.
///
/// `right` is the exclusive end of the batch, as for [`Self::record_log_stage_now`]: the index
/// of the batch's last log plus one.
///
/// Without the `runtime-stats` feature, it does nothing.
pub(crate) fn record_log_stage(&mut self, stage: Stage, right: u64, now: InstantOf<C>) {
#[cfg(feature = "runtime-stats")]
self.log_stage.record_stage(stage, right, now);
#[cfg(not(feature = "runtime-stats"))]
{
let _ = (stage, right, now);
}
}
/// Discard the recorded log stages of the logs from index `since` on, which a truncation
/// removed from the log.
///
/// Without the `runtime-stats` feature, it does nothing.
pub(crate) fn truncate_log_stages(&mut self, since: u64) {
#[cfg(feature = "runtime-stats")]
self.log_stage.truncate(since);
#[cfg(not(feature = "runtime-stats"))]
{
let _ = since;
}
}
/// Exclude the logs in `range` from the log stages, such as the logs an installed snapshot
/// covers beyond the applied ones: their stages were not observed.
///
/// Without the `runtime-stats` feature, it does nothing.
pub(crate) fn exclude_log_stages(&mut self, range: std::ops::Range<u64>) {
#[cfg(feature = "runtime-stats")]
self.log_stage.exclude(range);
#[cfg(not(feature = "runtime-stats"))]
{
let _ = range;
}
}
/// 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.
///
/// The percentiles are computed when calling this method, and the returned
/// `RuntimeStatsDisplay` formats them into text each time it is displayed. It reads no clock,
/// so it can be formatted any number of times, also after the runtime has exited.
///
/// 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(),
}
}
}