mito2/compaction/
memory_manager.rs

1// Copyright 2023 Greptime 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 common_memory_manager::{MemoryGuard, MemoryManager, MemoryMetrics};
16
17use crate::metrics::{
18    COMPACTION_MEMORY_IN_USE, COMPACTION_MEMORY_LIMIT, COMPACTION_MEMORY_REJECTED,
19};
20
21/// Compaction-specific memory metrics implementation.
22#[derive(Clone, Copy, Debug, Default)]
23pub struct CompactionMemoryMetrics;
24
25impl MemoryMetrics for CompactionMemoryMetrics {
26    fn set_limit(&self, bytes: i64) {
27        COMPACTION_MEMORY_LIMIT.set(bytes);
28    }
29
30    fn set_in_use(&self, bytes: i64) {
31        COMPACTION_MEMORY_IN_USE.set(bytes);
32    }
33
34    fn inc_rejected(&self, reason: &str) {
35        COMPACTION_MEMORY_REJECTED
36            .with_label_values(&[reason])
37            .inc();
38    }
39}
40
41/// Compaction memory manager.
42pub type CompactionMemoryManager = MemoryManager<CompactionMemoryMetrics>;
43
44/// Compaction memory guard.
45pub type CompactionMemoryGuard = MemoryGuard<CompactionMemoryMetrics>;
46
47/// Helper to construct a compaction memory manager without passing metrics explicitly.
48pub fn new_compaction_memory_manager(limit_bytes: u64) -> CompactionMemoryManager {
49    CompactionMemoryManager::new(limit_bytes, CompactionMemoryMetrics)
50}