Skip to main content

rustfs_s3select_api/
metrics.rs

1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use arc_swap::ArcSwap;
16use std::sync::{
17    Arc,
18    atomic::{AtomicU64, Ordering},
19};
20
21#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
22pub struct SelectInputMetricsSnapshot {
23    pub bytes_scanned: u64,
24    pub bytes_processed: u64,
25}
26
27#[derive(Debug)]
28pub struct SelectInputMetrics {
29    active: ArcSwap<SelectInputMetricBank>,
30}
31
32#[derive(Debug, Default)]
33struct SelectInputMetricBank {
34    uncompressed_bytes: AtomicU64,
35    compressed_bytes_scanned: AtomicU64,
36    compressed_bytes_processed: AtomicU64,
37}
38
39#[derive(Clone, Debug)]
40pub(crate) struct SelectInputMetricsRecorder {
41    bank: Arc<SelectInputMetricBank>,
42}
43
44impl Default for SelectInputMetrics {
45    fn default() -> Self {
46        Self {
47            active: ArcSwap::from_pointee(SelectInputMetricBank::default()),
48        }
49    }
50}
51
52impl SelectInputMetrics {
53    pub fn snapshot(&self) -> SelectInputMetricsSnapshot {
54        let bank = self.active.load();
55        let uncompressed_bytes = bank.uncompressed_bytes.load(Ordering::Relaxed);
56        SelectInputMetricsSnapshot {
57            bytes_scanned: uncompressed_bytes.saturating_add(bank.compressed_bytes_scanned.load(Ordering::Relaxed)),
58            bytes_processed: uncompressed_bytes.saturating_add(bank.compressed_bytes_processed.load(Ordering::Relaxed)),
59        }
60    }
61
62    pub(crate) fn recorder(&self) -> SelectInputMetricsRecorder {
63        SelectInputMetricsRecorder {
64            bank: self.active.load_full(),
65        }
66    }
67
68    /// Publishes a fresh bank so late planner writes remain isolated.
69    pub fn reset(&self) {
70        self.active.store(Arc::new(SelectInputMetricBank::default()));
71    }
72}
73
74impl SelectInputMetricsRecorder {
75    pub(crate) fn record_uncompressed(&self, bytes: usize) {
76        saturating_add(&self.bank.uncompressed_bytes, bytes);
77    }
78
79    pub(crate) fn record_scanned(&self, bytes: usize) {
80        saturating_add(&self.bank.compressed_bytes_scanned, bytes);
81    }
82
83    pub(crate) fn record_processed(&self, bytes: usize) {
84        saturating_add(&self.bank.compressed_bytes_processed, bytes);
85    }
86}
87
88fn saturating_add(counter: &AtomicU64, bytes: usize) {
89    let increment = u64::try_from(bytes).unwrap_or(u64::MAX);
90    let _ = counter.try_update(Ordering::Relaxed, Ordering::Relaxed, |current| Some(current.saturating_add(increment)));
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    #[test]
98    fn records_uncompressed_input_at_both_boundaries() {
99        let metrics = SelectInputMetrics::default();
100        metrics.recorder().record_uncompressed(7);
101
102        assert_eq!(
103            metrics.snapshot(),
104            SelectInputMetricsSnapshot {
105                bytes_scanned: 7,
106                bytes_processed: 7,
107            }
108        );
109    }
110
111    #[test]
112    fn counters_saturate_instead_of_wrapping() {
113        let metrics = SelectInputMetrics::default();
114        metrics
115            .active
116            .load()
117            .uncompressed_bytes
118            .store(u64::MAX - 1, Ordering::Relaxed);
119
120        metrics.recorder().record_uncompressed(2);
121
122        assert_eq!(metrics.snapshot().bytes_scanned, u64::MAX);
123        assert_eq!(metrics.snapshot().bytes_processed, u64::MAX);
124    }
125
126    #[test]
127    fn compressed_boundaries_are_counted_independently() {
128        let metrics = SelectInputMetrics::default();
129        let recorder = metrics.recorder();
130        recorder.record_scanned(39);
131        recorder.record_processed(19);
132
133        assert_eq!(
134            metrics.snapshot(),
135            SelectInputMetricsSnapshot {
136                bytes_scanned: 39,
137                bytes_processed: 19,
138            }
139        );
140    }
141
142    #[test]
143    fn reset_clears_schema_inference_bytes() {
144        let metrics = SelectInputMetrics::default();
145        let planning = metrics.recorder();
146        planning.record_uncompressed(9);
147
148        metrics.reset();
149        planning.record_uncompressed(5);
150        let execution = metrics.recorder();
151        execution.record_uncompressed(3);
152
153        assert_eq!(
154            metrics.snapshot(),
155            SelectInputMetricsSnapshot {
156                bytes_scanned: 3,
157                bytes_processed: 3,
158            }
159        );
160    }
161}