Skip to main content

waratah_storage/
metrics.rs

1//! Operational LSM counters for Describe / cluster HTTP (not planner sketches).
2
3/// Approximate RocksDB sizes and compaction activity.
4///
5/// Memory backends omit this struct entirely. Fields stay `None` when a
6/// property is missing from the engine.
7#[derive(Debug, Clone, Default, PartialEq, Eq)]
8pub struct LsmMetrics {
9    /// `rocksdb.estimate-live-data-size`.
10    pub estimate_live_data_bytes: Option<u64>,
11    /// `rocksdb.estimate-num-keys`.
12    pub estimate_num_keys: Option<u64>,
13    /// `rocksdb.total-sst-files-size`.
14    pub total_sst_bytes: Option<u64>,
15    /// `rocksdb.cur-size-all-mem-tables`.
16    pub memtable_bytes: Option<u64>,
17    /// `rocksdb.num-running-compactions`.
18    pub num_running_compactions: Option<u64>,
19    /// `rocksdb.num-running-flushes`.
20    pub num_running_flushes: Option<u64>,
21    /// Process-wide block cache capacity (all CFs share one cache).
22    pub block_cache_capacity_bytes: Option<u64>,
23    /// Bytes currently charged to cached blocks (data + pinned index/filter).
24    pub block_cache_usage_bytes: Option<u64>,
25}
26
27#[cfg(test)]
28mod tests {
29    use crate::{GraphName, NamedNode, PartitionAxis, Quad, Storage};
30
31    #[test]
32    fn memory_backend_omits_lsm_metrics() {
33        let store = Storage::memory(PartitionAxis::S);
34        assert_eq!(store.lsm_metrics(), None);
35    }
36
37    #[test]
38    fn rocksdb_lsm_metrics_sum_hexastore_cfs() {
39        let dir = tempfile::tempdir().unwrap();
40        let store = Storage::open_rocksdb(dir.path(), PartitionAxis::S).unwrap();
41        let quad = Quad::new(
42            NamedNode::new("http://ex/s").unwrap(),
43            NamedNode::new("http://ex/p").unwrap(),
44            NamedNode::new("http://ex/o").unwrap(),
45            GraphName::DefaultGraph,
46        );
47        store.insert_quad(&quad).unwrap();
48        store.flush_memtables().unwrap();
49        store.compact_range();
50        let metrics = store.lsm_metrics().expect("rocksdb reports LsmMetrics");
51        assert!(
52            metrics.estimate_num_keys.unwrap_or(0) >= 2,
53            "SPOG+SOPG keys after flush: {metrics:?}"
54        );
55        assert!(
56            metrics.total_sst_bytes.unwrap_or(0) > 0,
57            "SST bytes after flush: {metrics:?}"
58        );
59    }
60}