raft_log/raft_log/
stat.rs

1use std::fmt;
2use std::fmt::Formatter;
3
4use crate::disp::display_option::DebugOption;
5use crate::num::format_pad9_u64;
6use crate::raft_log::state_machine::raft_log_state::RaftLogState;
7use crate::ChunkId;
8use crate::Types;
9
10/// Statistics about a Raft log, including information about closed chunks, open
11/// chunk, and payload cache performance metrics.
12#[derive(Debug, Clone)]
13pub struct Stat<T>
14where T: Types
15{
16    /// Vector of statistics for closed (completed) chunks
17    pub closed_chunks: Vec<ChunkStat<T>>,
18    /// Statistics for the currently open (active) chunk
19    pub open_chunk: ChunkStat<T>,
20    /// The last evictable log id in the payload cache
21    pub payload_cache_last_evictable: Option<T::LogId>,
22    /// Current number of items in the payload cache
23    pub payload_cache_item_count: u64,
24    /// Maximum number of items allowed in the payload cache
25    pub payload_cache_max_item: u64,
26    /// Current size of the payload cache in bytes
27    pub payload_cache_size: u64,
28    /// Maximum capacity of the payload cache in bytes
29    pub payload_cache_capacity: u64,
30    /// Number of cache misses
31    pub payload_cache_miss: u64,
32    /// Number of cache hits
33    pub payload_cache_hit: u64,
34}
35
36impl<T> fmt::Display for Stat<T>
37where T: Types
38{
39    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
40        let lb = if f.alternate() { "\n" } else { "" };
41        let idt = if f.alternate() { "  " } else { "" };
42        write!(
43            f,
44            "Stat{{{lb} closed_chunks: [{lb}{idt}{}{lb} ],{lb} open_chunk: {},{lb} payload_cache:{{\
45            evictable: ..{:?},\
46            item/max: {} / {},\
47            size/cap: {} / {},\
48            miss: {},\
49            hit: {}\
50            }}{lb}\
51            }}",
52            self.closed_chunks
53                .iter()
54                .map(|c| format!("{}", c))
55                .collect::<Vec<String>>()
56                .join(&format!(",{lb}{idt}")),
57            self.open_chunk,
58            DebugOption(self.payload_cache_last_evictable.as_ref()),
59            format_pad9_u64(self.payload_cache_item_count),
60            format_pad9_u64(self.payload_cache_max_item),
61            format_pad9_u64(self.payload_cache_size),
62            format_pad9_u64(self.payload_cache_capacity),
63            format_pad9_u64(self.payload_cache_miss),
64            format_pad9_u64(self.payload_cache_hit)
65        )
66    }
67}
68
69/// Statistics about a single chunk in the Raft log
70#[derive(Debug, Clone)]
71pub struct ChunkStat<T>
72where T: Types
73{
74    /// Unique identifier for this chunk
75    pub chunk_id: ChunkId,
76    /// Number of records stored in this chunk
77    pub records_count: u64,
78    /// Global index of the first record in this chunk
79    pub global_start: u64,
80    /// Global index of the last record in this chunk plus one
81    pub global_end: u64,
82    /// Size of the chunk in bytes
83    pub size: u64,
84    /// Current state of the Raft log for this chunk
85    pub log_state: RaftLogState<T>,
86}
87
88impl<T> fmt::Display for ChunkStat<T>
89where T: Types
90{
91    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
92        write!(
93            f,
94            "ChunkStat({}){{records: {}, [{}, {}), size: {}, log_state: {:?}}}",
95            self.chunk_id,
96            self.records_count,
97            format_pad9_u64(self.global_start),
98            format_pad9_u64(self.global_end),
99            format_pad9_u64(self.size),
100            self.log_state
101        )
102    }
103}