Skip to main content

risingwave_meta/hummock/compaction/picker/
mod.rs

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