risingwave_meta/hummock/compaction/selector/
tombstone_compaction_selector.rs1use std::collections::HashMap;
16
17use risingwave_hummock_sdk::HummockCompactionTaskId;
18use risingwave_pb::hummock::compact_task;
19
20use super::{CompactionSelector, DynamicLevelSelectorCore};
21use crate::hummock::compaction::picker::{
22 TombstoneReclaimCompactionPicker, TombstoneReclaimPickerState,
23};
24use crate::hummock::compaction::selector::CompactionSelectorContext;
25use crate::hummock::compaction::{CompactionTask, create_compaction_task, create_overlap_strategy};
26
27#[derive(Default)]
28pub struct TombstoneCompactionSelector {
29 state: HashMap<u64, TombstoneReclaimPickerState>,
30}
31
32impl CompactionSelector for TombstoneCompactionSelector {
33 fn pick_compaction(
34 &mut self,
35 task_id: HummockCompactionTaskId,
36 context: CompactionSelectorContext<'_>,
37 ) -> Option<CompactionTask> {
38 let CompactionSelectorContext {
39 group,
40 levels,
41 level_handlers,
42 developer_config,
43 ..
44 } = context;
45 if group.compaction_config.tombstone_reclaim_ratio == 0 {
46 return None;
48 }
49
50 let dynamic_level_core =
51 DynamicLevelSelectorCore::new(group.compaction_config.clone(), developer_config);
52 let ctx = dynamic_level_core.calculate_level_base_size(levels);
53 let picker = TombstoneReclaimCompactionPicker::new(
54 create_overlap_strategy(group.compaction_config.compaction_mode()),
55 group.compaction_config.tombstone_reclaim_ratio as u64,
56 group.compaction_config.tombstone_reclaim_ratio as u64 / 2,
57 );
58 let state = self.state.entry(group.group_id).or_default();
59 let compaction_input = picker.pick_compaction(levels, level_handlers, state)?;
60 compaction_input.add_pending_task(task_id, level_handlers);
61
62 Some(create_compaction_task(
63 group.compaction_config.as_ref(),
64 compaction_input,
65 ctx.base_level,
66 self.task_type(),
67 ))
68 }
69
70 fn name(&self) -> &'static str {
71 "TombstoneCompaction"
72 }
73
74 fn task_type(&self) -> compact_task::TaskType {
75 compact_task::TaskType::Tombstone
76 }
77}