kvbm-logical 1.3.0-dev.1

Logical layer for KVBM (Key-Value Buffer Manager), managing block metadata, allocation, and eviction policies.
Documentation
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Custom `prometheus::core::Collector` that reads raw atomics at scrape time.
//!
//! External labels (e.g. `instance_id`, `worker_id`) are appended at collection time,
//! not baked in at metric creation time.

use std::sync::{Arc, RwLock};

use prometheus::core::{Collector, Desc};
use prometheus::proto::{Gauge, LabelPair, Metric, MetricFamily, MetricType};

use super::pool_metrics::BlockPoolMetrics;

/// Metric definitions: (name, help, type).
const COUNTER_DEFS: &[(&str, &str)] = &[
    (
        "kvbm_allocations_total",
        "Total blocks allocated from pools",
    ),
    (
        "kvbm_allocations_from_reset_total",
        "Total blocks allocated from the reset pool",
    ),
    (
        "kvbm_evictions_total",
        "Total blocks evicted from inactive pool",
    ),
    (
        "kvbm_registrations_total",
        "Total blocks registered (CompleteBlock -> ImmutableBlock)",
    ),
    (
        "kvbm_duplicate_blocks_total",
        "Total duplicate blocks created (Allow policy)",
    ),
    (
        "kvbm_registration_dedup_total",
        "Total block registrations deduplicated (Reject policy)",
    ),
    (
        "kvbm_stagings_total",
        "Total MutableBlock -> CompleteBlock transitions",
    ),
    (
        "kvbm_match_hashes_requested_total",
        "Total hashes requested in match_blocks calls",
    ),
    (
        "kvbm_match_blocks_returned_total",
        "Total blocks returned from match_blocks calls",
    ),
    (
        "kvbm_scan_hashes_requested_total",
        "Total hashes requested in scan_matches calls",
    ),
    (
        "kvbm_scan_blocks_returned_total",
        "Total blocks returned from scan_matches calls",
    ),
    (
        "kvbm_eager_primary_to_inactive_total",
        "Lookup-driven Primary→Inactive transitions \
         (race-window branch — sustained non-zero rate is a hot-contention signal)",
    ),
    (
        "kvbm_allocate_atomic_rollback_total",
        "Atomic-allocation rollbacks due to inactive backend under-allocation \
         (should be 0 in production; non-zero indicates a backend invariant violation)",
    ),
    (
        "kvbm_release_primary_noop_total",
        "ImmutableBlockInner drop transitions that no-op'd because a concurrent \
         lookup already eagerly transitioned the slot",
    ),
    (
        "kvbm_release_duplicate_noop_total",
        "Duplicate-block drop transitions that no-op'd due to slot-identity mismatch",
    ),
];

const GAUGE_DEFS: &[(&str, &str)] = &[
    (
        "kvbm_inflight_mutable",
        "Current MutableBlocks held outside pool",
    ),
    (
        "kvbm_inflight_immutable",
        "Current ImmutableBlocks held outside pool",
    ),
    ("kvbm_reset_pool_size", "Current reset pool size"),
    ("kvbm_inactive_pool_size", "Current inactive pool size"),
];

/// Aggregates metrics from multiple `BlockPoolMetrics` sources and exports
/// them as Prometheus `MetricFamily` protos with per-pool-type labels.
#[derive(Clone)]
pub struct MetricsAggregator {
    inner: Arc<Inner>,
}

struct Inner {
    sources: RwLock<Vec<Arc<BlockPoolMetrics>>>,
    external_labels: RwLock<Vec<(String, String)>>,
    descs: Vec<Desc>,
}

impl MetricsAggregator {
    /// Create a new `MetricsAggregator`.
    pub fn new() -> Self {
        let mut descs = Vec::with_capacity(COUNTER_DEFS.len() + GAUGE_DEFS.len());
        for (name, help) in COUNTER_DEFS {
            descs.push(
                Desc::new(
                    name.to_string(),
                    help.to_string(),
                    vec!["pool".to_string()],
                    Default::default(),
                )
                .expect("valid desc"),
            );
        }
        for (name, help) in GAUGE_DEFS {
            descs.push(
                Desc::new(
                    name.to_string(),
                    help.to_string(),
                    vec!["pool".to_string()],
                    Default::default(),
                )
                .expect("valid desc"),
            );
        }

        Self {
            inner: Arc::new(Inner {
                sources: RwLock::new(Vec::new()),
                external_labels: RwLock::new(Vec::new()),
                descs,
            }),
        }
    }

    /// Register a `BlockPoolMetrics` source (called by `BlockManager::build()`).
    pub fn register_source(&self, source: Arc<BlockPoolMetrics>) {
        self.inner
            .sources
            .write()
            .expect("sources lock poisoned")
            .push(source);
    }

    /// Set external labels appended at scrape time (e.g. `instance_id`, `worker_id`).
    pub fn set_external_labels(&self, labels: Vec<(String, String)>) {
        *self
            .inner
            .external_labels
            .write()
            .expect("external_labels lock poisoned") = labels;
    }

    /// Register this collector with a `prometheus::Registry`.
    pub fn register_with(&self, registry: &prometheus::Registry) -> Result<(), prometheus::Error> {
        registry.register(Box::new(self.clone()))
    }
}

impl Default for MetricsAggregator {
    fn default() -> Self {
        Self::new()
    }
}

impl Collector for MetricsAggregator {
    fn desc(&self) -> Vec<&Desc> {
        self.inner.descs.iter().collect()
    }

    fn collect(&self) -> Vec<MetricFamily> {
        let sources = self.inner.sources.read().expect("sources lock poisoned");
        let ext_labels = self
            .inner
            .external_labels
            .read()
            .expect("external_labels lock poisoned");

        let mut families: Vec<MetricFamily> = Vec::new();

        for source in sources.iter() {
            let snap = source.snapshot();
            let pool_label = source.type_label();

            let mut base_labels: Vec<LabelPair> = Vec::with_capacity(1 + ext_labels.len());
            let mut pool_lp = LabelPair::default();
            pool_lp.set_name("pool".to_string());
            pool_lp.set_value(pool_label.to_string());
            base_labels.push(pool_lp);
            for (k, v) in ext_labels.iter() {
                let mut lp = LabelPair::default();
                lp.set_name(k.clone());
                lp.set_value(v.clone());
                base_labels.push(lp);
            }

            // Counter values in order matching COUNTER_DEFS
            let counter_values: [u64; 15] = [
                snap.allocations,
                snap.allocations_from_reset,
                snap.evictions,
                snap.registrations,
                snap.duplicate_blocks,
                snap.registration_dedup,
                snap.stagings,
                snap.match_hashes_requested,
                snap.match_blocks_returned,
                snap.scan_hashes_requested,
                snap.scan_blocks_returned,
                snap.eager_primary_to_inactive_total,
                snap.allocate_atomic_rollback_total,
                snap.release_primary_noop_total,
                snap.release_duplicate_noop_total,
            ];

            for (i, (name, help)) in COUNTER_DEFS.iter().enumerate() {
                let mut m = Metric::default();
                m.set_label(base_labels.clone());
                let mut c = prometheus::proto::Counter::default();
                c.set_value(counter_values[i] as f64);
                m.set_counter(c);

                let mut mf = MetricFamily::default();
                mf.set_name(name.to_string());
                mf.set_help(help.to_string());
                mf.set_field_type(MetricType::COUNTER);
                mf.set_metric(vec![m]);
                families.push(mf);
            }

            // Gauge values in order matching GAUGE_DEFS
            let gauge_values: [i64; 4] = [
                snap.inflight_mutable,
                snap.inflight_immutable,
                snap.reset_pool_size,
                snap.inactive_pool_size,
            ];

            for (i, (name, help)) in GAUGE_DEFS.iter().enumerate() {
                let mut m = Metric::default();
                m.set_label(base_labels.clone());
                let mut g = Gauge::default();
                g.set_value(gauge_values[i] as f64);
                m.set_gauge(g);

                let mut mf = MetricFamily::default();
                mf.set_name(name.to_string());
                mf.set_help(help.to_string());
                mf.set_field_type(MetricType::GAUGE);
                mf.set_metric(vec![m]);
                families.push(mf);
            }
        }

        // Merge families with the same name (when multiple sources)
        if sources.len() > 1 {
            let mut merged: Vec<MetricFamily> = Vec::new();
            for mut family in families {
                if let Some(existing) = merged.iter_mut().find(|f| f.name() == family.name()) {
                    existing.mut_metric().extend(family.take_metric());
                } else {
                    merged.push(family);
                }
            }
            merged
        } else {
            families
        }
    }
}

#[cfg(test)]
#[allow(deprecated)]
mod tests {
    use super::*;
    use prometheus::core::Collector;

    #[test]
    fn test_empty_aggregator_collects_nothing() {
        let agg = MetricsAggregator::new();
        let families = agg.collect();
        assert!(families.is_empty());
    }

    #[test]
    fn test_single_source_collect() {
        let agg = MetricsAggregator::new();
        let metrics = Arc::new(BlockPoolMetrics::new("G1".to_string()));

        metrics.inc_allocations(10);
        metrics.inc_evictions(3);
        metrics.set_reset_pool_size(42);

        agg.register_source(metrics);

        let families = agg.collect();
        assert_eq!(families.len(), COUNTER_DEFS.len() + GAUGE_DEFS.len());

        // Find allocations counter
        let alloc_family = families
            .iter()
            .find(|f| f.get_name() == "kvbm_allocations_total")
            .expect("should have allocations family");
        assert_eq!(alloc_family.get_field_type(), MetricType::COUNTER);
        let m = &alloc_family.get_metric()[0];
        assert_eq!(m.get_counter().value(), 10.0);
        assert_eq!(m.get_label()[0].get_name(), "pool");
        assert_eq!(m.get_label()[0].get_value(), "G1");

        // Find reset_pool_size gauge
        let reset_family = families
            .iter()
            .find(|f| f.get_name() == "kvbm_reset_pool_size")
            .expect("should have reset pool size family");
        assert_eq!(reset_family.get_field_type(), MetricType::GAUGE);
        assert_eq!(reset_family.get_metric()[0].get_gauge().value(), 42.0);
    }

    #[test]
    fn test_external_labels() {
        let agg = MetricsAggregator::new();
        let metrics = Arc::new(BlockPoolMetrics::new("G1".to_string()));
        agg.register_source(metrics);

        agg.set_external_labels(vec![
            ("instance_id".to_string(), "node-1".to_string()),
            ("worker_id".to_string(), "w0".to_string()),
        ]);

        let families = agg.collect();
        let alloc_family = families
            .iter()
            .find(|f| f.get_name() == "kvbm_allocations_total")
            .unwrap();
        let labels = alloc_family.get_metric()[0].get_label();
        assert_eq!(labels.len(), 3); // pool + 2 external
        assert_eq!(labels[1].get_name(), "instance_id");
        assert_eq!(labels[1].get_value(), "node-1");
        assert_eq!(labels[2].get_name(), "worker_id");
        assert_eq!(labels[2].get_value(), "w0");
    }

    #[test]
    fn test_multiple_sources_merged() {
        let agg = MetricsAggregator::new();

        let m1 = Arc::new(BlockPoolMetrics::new("G1".to_string()));
        let m2 = Arc::new(BlockPoolMetrics::new("G2".to_string()));

        m1.inc_allocations(5);
        m2.inc_allocations(10);

        agg.register_source(m1);
        agg.register_source(m2);

        let families = agg.collect();

        // Families should be merged by name
        let alloc_family = families
            .iter()
            .find(|f| f.get_name() == "kvbm_allocations_total")
            .expect("should have allocations family");
        assert_eq!(alloc_family.get_metric().len(), 2);

        let values: Vec<f64> = alloc_family
            .get_metric()
            .iter()
            .map(|m| m.get_counter().value())
            .collect();
        assert!(values.contains(&5.0));
        assert!(values.contains(&10.0));
    }

    #[test]
    fn test_register_with_prometheus_registry() {
        let agg = MetricsAggregator::new();
        let metrics = Arc::new(BlockPoolMetrics::new("G1".to_string()));
        metrics.inc_allocations(42);
        agg.register_source(metrics);

        let registry = prometheus::Registry::new();
        agg.register_with(&registry)
            .expect("should register successfully");

        let gathered = registry.gather();
        assert!(!gathered.is_empty());

        let alloc_family = gathered
            .iter()
            .find(|f| f.get_name() == "kvbm_allocations_total")
            .expect("should find allocations in gathered metrics");
        assert_eq!(alloc_family.get_metric()[0].get_counter().value(), 42.0);
    }

    #[test]
    fn test_descs_match_definitions() {
        let agg = MetricsAggregator::new();
        let descs = agg.desc();
        assert_eq!(descs.len(), COUNTER_DEFS.len() + GAUGE_DEFS.len());
    }
}