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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors
use std::sync::atomic::{AtomicUsize, Ordering};
/// A trait used by the index to report metrics
///
/// Callers can implement this trait to collect metrics
pub trait MetricsCollector: Send + Sync {
/// Record partition loads
///
/// Many indices consist of partitions that may need to be loaded
/// into cache. For example, an inverted index or ngram index has a
/// posting list for each token.
///
/// In the ideal case, these shards are in the cache and will not need
/// to be loaded from disk. This method should not be called if the
/// shard is in the cache.
fn record_parts_loaded(&self, num_parts: usize);
/// Record a shard load
fn record_part_load(&self) {
self.record_parts_loaded(1);
}
/// Record an index load
///
/// This should be called when a scalar index is loaded from storage.
/// It should not be called if the index is already in memory.
fn record_index_loads(&self, num_indexes: usize);
/// Record an index load
fn record_index_load(&self) {
self.record_index_loads(1);
}
/// Record the number of "comparisons" made by the index
///
/// What exactly constitutes a comparison depends on the index type.
/// For example, a B-tree index may make comparisons while searching for a value.
/// On the other hand, a bitmap index makes comparisons when computing the intersection
/// of two bitmaps.
///
/// The goal is to provide some visibility into the compute cost of the search
fn record_comparisons(&self, num_comparisons: usize);
}
/// A no-op metrics collector that does nothing
pub struct NoOpMetricsCollector;
impl MetricsCollector for NoOpMetricsCollector {
fn record_parts_loaded(&self, _num_parts: usize) {}
fn record_index_loads(&self, _num_indexes: usize) {}
fn record_comparisons(&self, _num_comparisons: usize) {}
}
#[derive(Default)]
pub struct LocalMetricsCollector {
pub parts_loaded: AtomicUsize,
pub index_loads: AtomicUsize,
pub comparisons: AtomicUsize,
}
impl LocalMetricsCollector {
pub fn dump_into(self, other: &dyn MetricsCollector) {
other.record_parts_loaded(self.parts_loaded.load(Ordering::Relaxed));
other.record_index_loads(self.index_loads.load(Ordering::Relaxed));
other.record_comparisons(self.comparisons.load(Ordering::Relaxed));
}
}
impl MetricsCollector for LocalMetricsCollector {
fn record_parts_loaded(&self, num_parts: usize) {
self.parts_loaded.fetch_add(num_parts, Ordering::Relaxed);
}
fn record_index_loads(&self, num_indexes: usize) {
self.index_loads.fetch_add(num_indexes, Ordering::Relaxed);
}
fn record_comparisons(&self, num_comparisons: usize) {
self.comparisons
.fetch_add(num_comparisons, Ordering::Relaxed);
}
}