risingwave_meta/hummock/compaction/picker/
mod.rs

1// Copyright 2025 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
15mod base_level_compaction_picker;
16mod emergency_compaction_picker;
17mod intra_compaction_picker;
18mod manual_compaction_picker;
19mod min_overlap_compaction_picker;
20mod space_reclaim_compaction_picker;
21mod tier_compaction_picker;
22mod tombstone_reclaim_compaction_picker;
23mod trivial_move_compaction_picker;
24mod ttl_reclaim_compaction_picker;
25
26mod compaction_task_validator;
27mod vnode_watermark_picker;
28
29pub use base_level_compaction_picker::LevelCompactionPicker;
30pub use compaction_task_validator::{CompactionTaskValidator, ValidationRuleType};
31pub use emergency_compaction_picker::EmergencyCompactionPicker;
32pub use intra_compaction_picker::IntraCompactionPicker;
33pub use manual_compaction_picker::ManualCompactionPicker;
34pub use min_overlap_compaction_picker::MinOverlappingPicker;
35use risingwave_hummock_sdk::level::{InputLevel, Levels};
36pub use space_reclaim_compaction_picker::{SpaceReclaimCompactionPicker, SpaceReclaimPickerState};
37pub use tier_compaction_picker::TierCompactionPicker;
38pub use tombstone_reclaim_compaction_picker::{
39    TombstoneReclaimCompactionPicker, TombstoneReclaimPickerState,
40};
41pub use trivial_move_compaction_picker::TrivialMovePicker;
42pub use ttl_reclaim_compaction_picker::{TtlPickerState, TtlReclaimCompactionPicker};
43pub use vnode_watermark_picker::VnodeWatermarkCompactionPicker;
44
45use crate::hummock::level_handler::LevelHandler;
46
47#[derive(Default, Debug)]
48pub struct LocalPickerStatistic {
49    pub skip_by_write_amp_limit: u64,
50    pub skip_by_count_limit: u64,
51    pub skip_by_pending_files: u64,
52    pub skip_by_overlapping: u64,
53}
54
55#[derive(Default, Debug)]
56pub struct CompactionInput {
57    pub input_levels: Vec<InputLevel>,
58    pub target_level: usize,
59    pub target_sub_level_id: u64,
60    pub select_input_size: u64,
61    pub target_input_size: u64,
62    pub total_file_count: u64,
63    pub vnode_partition_count: u32,
64}
65
66impl CompactionInput {
67    pub fn add_pending_task(&self, task_id: u64, level_handlers: &mut [LevelHandler]) {
68        let mut has_l0 = false;
69        for level in &self.input_levels {
70            if level.level_idx != 0 {
71                level_handlers[level.level_idx as usize].add_pending_task(
72                    task_id,
73                    self.target_level,
74                    &level.table_infos,
75                );
76            } else {
77                has_l0 = true;
78            }
79        }
80        if has_l0 {
81            let table_infos = self
82                .input_levels
83                .iter()
84                .filter(|level| level.level_idx == 0)
85                .flat_map(|level| level.table_infos.iter());
86            level_handlers[0].add_pending_task(task_id, self.target_level, table_infos);
87        }
88    }
89}
90
91pub trait CompactionPicker {
92    fn pick_compaction(
93        &mut self,
94        levels: &Levels,
95        level_handlers: &[LevelHandler],
96        stats: &mut LocalPickerStatistic,
97    ) -> Option<CompactionInput>;
98}