risingwave_storage/hummock/shared_buffer/
mod.rs

1// Copyright 2022 RisingWave Labs
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
15pub(crate) const TEST_TABLE_ID: TableId = TableId::new(233);
16
17use std::fmt::Debug;
18use std::sync::Arc;
19
20use risingwave_common::metrics::{
21    LabelGuardedHistogram, LabelGuardedIntCounter, LabelGuardedIntGauge,
22    LazyLabelGuardedIntCounter, LazyLabelGuardedIntGauge,
23};
24use risingwave_pb::id::TableId;
25
26use crate::monitor::HummockStateStoreMetrics;
27
28pub mod shared_buffer_batch;
29
30pub(crate) struct TableMemoryMetrics {
31    imm_total_size: LabelGuardedIntGauge,
32    imm_count: LabelGuardedIntGauge,
33    pub write_batch_tuple_counts: LabelGuardedIntCounter,
34    pub write_batch_duration: LabelGuardedHistogram,
35    pub write_batch_size: LabelGuardedHistogram,
36    pub mem_table_spill_counts: LazyLabelGuardedIntCounter,
37    pub old_value_size: LazyLabelGuardedIntGauge,
38}
39
40impl TableMemoryMetrics {
41    pub(super) fn new(
42        metrics: &HummockStateStoreMetrics,
43        table_id: TableId,
44        is_replicated: bool,
45    ) -> Self {
46        let table_id_string = if is_replicated {
47            format!("{} replicated", table_id)
48        } else {
49            table_id.to_string()
50        };
51        let labels_vec = vec![table_id_string];
52        let labels = labels_vec.as_slice();
53        Self {
54            imm_total_size: metrics.per_table_imm_size.with_guarded_label_values(labels),
55            imm_count: metrics
56                .per_table_imm_count
57                .with_guarded_label_values(labels),
58            write_batch_tuple_counts: metrics
59                .write_batch_tuple_counts
60                .with_guarded_label_values(labels),
61            write_batch_duration: metrics
62                .write_batch_duration
63                .with_guarded_label_values(labels),
64            write_batch_size: metrics.write_batch_size.with_guarded_label_values(labels),
65            mem_table_spill_counts: metrics
66                .mem_table_spill_counts
67                .lazy_guarded_metrics(labels_vec.clone()),
68            old_value_size: metrics.old_value_size.lazy_guarded_metrics(labels_vec),
69        }
70    }
71
72    pub(super) fn for_test() -> Arc<Self> {
73        Self::new(&HummockStateStoreMetrics::unused(), TEST_TABLE_ID, false).into()
74    }
75
76    pub(super) fn inc_imm(&self, imm_size: usize) {
77        self.imm_total_size.add(imm_size as _);
78        self.imm_count.inc();
79    }
80
81    pub(super) fn dec_imm(&self, imm_size: usize) {
82        self.imm_total_size.sub(imm_size as _);
83        self.imm_count.dec();
84    }
85}
86
87impl Debug for TableMemoryMetrics {
88    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89        f.debug_struct("TableMemoryMetrics").finish()
90    }
91}