risingwave_meta/hummock/model/
compaction_group_config.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
15use std::sync::Arc;
16
17use risingwave_hummock_sdk::CompactionGroupId;
18use risingwave_pb::hummock::CompactionConfig;
19
20#[derive(Debug, Default, Clone, PartialEq)]
21pub struct CompactionGroup {
22    pub group_id: CompactionGroupId,
23    pub compaction_config: Arc<CompactionConfig>,
24}
25
26impl CompactionGroup {
27    pub fn new(
28        group_id: impl Into<CompactionGroupId>,
29        compaction_config: CompactionConfig,
30    ) -> Self {
31        Self {
32            group_id: group_id.into(),
33            compaction_config: Arc::new(compaction_config),
34        }
35    }
36
37    pub fn group_id(&self) -> CompactionGroupId {
38        self.group_id
39    }
40
41    pub fn compaction_config(&self) -> Arc<CompactionConfig> {
42        self.compaction_config.clone()
43    }
44}
45
46impl From<&risingwave_pb::hummock::CompactionGroup> for CompactionGroup {
47    fn from(compaction_group: &risingwave_pb::hummock::CompactionGroup) -> Self {
48        Self {
49            group_id: compaction_group.id,
50            compaction_config: Arc::new(
51                compaction_group
52                    .compaction_config
53                    .as_ref()
54                    .cloned()
55                    .unwrap(),
56            ),
57        }
58    }
59}
60
61impl From<&CompactionGroup> for risingwave_pb::hummock::CompactionGroup {
62    fn from(compaction_group: &CompactionGroup) -> Self {
63        Self {
64            id: compaction_group.group_id,
65            compaction_config: Some(compaction_group.compaction_config.as_ref().clone()),
66        }
67    }
68}
69
70impl CompactionGroup {
71    pub fn max_estimated_group_size(&self) -> u64 {
72        let max_level = self.compaction_config.max_level as usize;
73        let base_level_size = self.compaction_config.max_bytes_for_level_base;
74        let level_multiplier = self.compaction_config.max_bytes_for_level_multiplier;
75
76        fn size_for_levels(level_index: usize, base_size: u64, multiplier: u64) -> u64 {
77            if level_index == 0 {
78                base_size
79            } else {
80                base_size * multiplier.pow(level_index as u32)
81            }
82        }
83
84        (0..max_level)
85            .map(|level_index| size_for_levels(level_index, base_level_size, level_multiplier))
86            .sum()
87    }
88}