risingwave_ctl/cmd_impl/hummock/
compaction_group.rs1use std::collections::{HashMap, HashSet};
16
17use comfy_table::{Row, Table};
18use itertools::Itertools;
19use risingwave_hummock_sdk::compaction_group::StateTableId;
20use risingwave_hummock_sdk::{CompactionGroupId, HummockContextId};
21use risingwave_pb::hummock::compact_task::TaskStatus;
22use risingwave_pb::hummock::rise_ctl_update_compaction_config_request::CompressionAlgorithm;
23use risingwave_pb::hummock::rise_ctl_update_compaction_config_request::mutable_config::MutableConfig;
24
25use crate::CtlContext;
26
27pub async fn list_compaction_group(context: &CtlContext) -> anyhow::Result<()> {
28 let meta_client = context.meta_client().await?;
29 let result = meta_client.risectl_list_compaction_group().await?;
30 println!("{:#?}", result);
31 Ok(())
32}
33
34pub async fn update_compaction_config(
35 context: &CtlContext,
36 ids: Vec<CompactionGroupId>,
37 configs: Vec<MutableConfig>,
38) -> anyhow::Result<()> {
39 let meta_client = context.meta_client().await?;
40 meta_client
41 .risectl_update_compaction_config(ids.as_slice(), configs.as_slice())
42 .await?;
43 println!(
44 "Succeed: update compaction groups {:#?} with configs {:#?}.",
45 ids, configs
46 );
47 Ok(())
48}
49
50#[allow(clippy::too_many_arguments)]
51pub fn build_compaction_config_vec(
52 max_bytes_for_level_base: Option<u64>,
53 max_bytes_for_level_multiplier: Option<u64>,
54 max_compaction_bytes: Option<u64>,
55 sub_level_max_compaction_bytes: Option<u64>,
56 level0_tier_compact_file_number: Option<u64>,
57 target_file_size_base: Option<u64>,
58 compaction_filter_mask: Option<u32>,
59 max_sub_compaction: Option<u32>,
60 level0_stop_write_threshold_sub_level_number: Option<u64>,
61 level0_sub_level_compact_level_count: Option<u32>,
62 max_space_reclaim_bytes: Option<u64>,
63 level0_max_compact_file_number: Option<u64>,
64 level0_overlapping_sub_level_compact_level_count: Option<u32>,
65 enable_emergency_picker: Option<bool>,
66 tombstone_reclaim_ratio: Option<u32>,
67 compress_algorithm: Option<CompressionAlgorithm>,
68 max_l0_compact_level: Option<u32>,
69 sst_allowed_trivial_move_min_size: Option<u64>,
70 disable_auto_group_scheduling: Option<bool>,
71 max_overlapping_level_size: Option<u64>,
72 sst_allowed_trivial_move_max_count: Option<u32>,
73 emergency_level0_sst_file_count: Option<u32>,
74 emergency_level0_sub_level_partition: Option<u32>,
75 level0_stop_write_threshold_max_sst_count: Option<u32>,
76 level0_stop_write_threshold_max_size: Option<u64>,
77) -> Vec<MutableConfig> {
78 let mut configs = vec![];
79 if let Some(c) = max_bytes_for_level_base {
80 configs.push(MutableConfig::MaxBytesForLevelBase(c));
81 }
82 if let Some(c) = max_bytes_for_level_multiplier {
83 configs.push(MutableConfig::MaxBytesForLevelMultiplier(c));
84 }
85 if let Some(c) = max_compaction_bytes {
86 configs.push(MutableConfig::MaxCompactionBytes(c));
87 }
88 if let Some(c) = sub_level_max_compaction_bytes {
89 configs.push(MutableConfig::SubLevelMaxCompactionBytes(c));
90 }
91 if let Some(c) = level0_tier_compact_file_number {
92 configs.push(MutableConfig::Level0TierCompactFileNumber(c));
93 }
94 if let Some(c) = target_file_size_base {
95 configs.push(MutableConfig::TargetFileSizeBase(c));
96 }
97 if let Some(c) = compaction_filter_mask {
98 configs.push(MutableConfig::CompactionFilterMask(c));
99 }
100 if let Some(c) = max_sub_compaction {
101 configs.push(MutableConfig::MaxSubCompaction(c));
102 }
103 if let Some(c) = level0_stop_write_threshold_sub_level_number {
104 configs.push(MutableConfig::Level0StopWriteThresholdSubLevelNumber(c));
105 }
106 if let Some(c) = level0_sub_level_compact_level_count {
107 configs.push(MutableConfig::Level0SubLevelCompactLevelCount(c));
108 }
109 if let Some(c) = max_space_reclaim_bytes {
110 configs.push(MutableConfig::MaxSpaceReclaimBytes(c))
111 }
112 if let Some(c) = level0_max_compact_file_number {
113 configs.push(MutableConfig::Level0MaxCompactFileNumber(c))
114 }
115 if let Some(c) = level0_overlapping_sub_level_compact_level_count {
116 configs.push(MutableConfig::Level0OverlappingSubLevelCompactLevelCount(c))
117 }
118 if let Some(c) = enable_emergency_picker {
119 configs.push(MutableConfig::EnableEmergencyPicker(c))
120 }
121 if let Some(c) = tombstone_reclaim_ratio {
122 configs.push(MutableConfig::TombstoneReclaimRatio(c))
123 }
124 if let Some(c) = compress_algorithm {
125 configs.push(MutableConfig::CompressionAlgorithm(c))
126 }
127 if let Some(c) = max_l0_compact_level {
128 configs.push(MutableConfig::MaxL0CompactLevelCount(c))
129 }
130 if let Some(c) = sst_allowed_trivial_move_min_size {
131 configs.push(MutableConfig::SstAllowedTrivialMoveMinSize(c))
132 }
133 if let Some(c) = disable_auto_group_scheduling {
134 configs.push(MutableConfig::DisableAutoGroupScheduling(c))
135 }
136 if let Some(c) = max_overlapping_level_size {
137 configs.push(MutableConfig::MaxOverlappingLevelSize(c))
138 }
139 if let Some(c) = sst_allowed_trivial_move_max_count {
140 configs.push(MutableConfig::SstAllowedTrivialMoveMaxCount(c))
141 }
142 if let Some(c) = emergency_level0_sst_file_count {
143 configs.push(MutableConfig::EmergencyLevel0SstFileCount(c))
144 }
145 if let Some(c) = emergency_level0_sub_level_partition {
146 configs.push(MutableConfig::EmergencyLevel0SubLevelPartition(c))
147 }
148 if let Some(c) = level0_stop_write_threshold_max_sst_count {
149 configs.push(MutableConfig::Level0StopWriteThresholdMaxSstCount(c))
150 }
151 if let Some(c) = level0_stop_write_threshold_max_size {
152 configs.push(MutableConfig::Level0StopWriteThresholdMaxSize(c))
153 }
154
155 configs
156}
157
158pub async fn split_compaction_group(
159 context: &CtlContext,
160 group_id: CompactionGroupId,
161 table_ids_to_new_group: &[StateTableId],
162 partition_vnode_count: u32,
163) -> anyhow::Result<()> {
164 let meta_client = context.meta_client().await?;
165 let new_group_id = meta_client
166 .split_compaction_group(group_id, table_ids_to_new_group, partition_vnode_count)
167 .await?;
168 println!(
169 "Succeed: split compaction group {}. tables {:#?} are moved to new group {}.",
170 group_id, table_ids_to_new_group, new_group_id
171 );
172 Ok(())
173}
174
175pub async fn list_compaction_status(context: &CtlContext, verbose: bool) -> anyhow::Result<()> {
176 let meta_client = context.meta_client().await?;
177 let (status, assignment, progress) = meta_client.risectl_list_compaction_status().await?;
178 if !verbose {
179 let mut table = Table::new();
180 table.set_header({
181 let mut row = Row::new();
182 row.add_cell("Compaction Group".into());
183 row.add_cell("Level".into());
184 row.add_cell("Task Count".into());
185 row.add_cell("Tasks".into());
186 row
187 });
188 for s in status {
189 let cg_id = s.compaction_group_id;
190 for l in s.level_handlers {
191 let level = l.level;
192 let mut task_ids = HashSet::new();
193 for t in l.tasks {
194 task_ids.insert(t.task_id);
195 }
196 let mut row = Row::new();
197 row.add_cell(cg_id.into());
198 row.add_cell(level.into());
199 row.add_cell(task_ids.len().into());
200 row.add_cell(
201 task_ids
202 .into_iter()
203 .sorted()
204 .map(|t| t.to_string())
205 .join(",")
206 .into(),
207 );
208 table.add_row(row);
209 }
210 }
211 println!("{table}");
212
213 let mut table = Table::new();
214 table.set_header({
215 let mut row = Row::new();
216 row.add_cell("Hummock Context".into());
217 row.add_cell("Task Count".into());
218 row.add_cell("Tasks".into());
219 row
220 });
221 let mut assignment_lite: HashMap<HummockContextId, Vec<u64>> = HashMap::new();
222 for a in assignment {
223 assignment_lite
224 .entry(a.context_id)
225 .or_default()
226 .push(a.compact_task.unwrap().task_id);
227 }
228 for (k, v) in assignment_lite {
229 let mut row = Row::new();
230 row.add_cell(k.into());
231 row.add_cell(v.len().into());
232 row.add_cell(
233 v.into_iter()
234 .sorted()
235 .map(|t| t.to_string())
236 .join(",")
237 .into(),
238 );
239 table.add_row(row);
240 }
241 println!("{table}");
242
243 let mut table = Table::new();
244 table.set_header({
245 let mut row = Row::new();
246 row.add_cell("Task".into());
247 row.add_cell("Num SSTs Sealed".into());
248 row.add_cell("Num SSTs Uploaded".into());
249 row.add_cell("Num Progress Key".into());
250 row.add_cell("Num Pending Read IO".into());
251 row.add_cell("Num Pending Write IO".into());
252 row
253 });
254 for p in progress {
255 let mut row = Row::new();
256 row.add_cell(p.task_id.into());
257 row.add_cell(p.num_ssts_sealed.into());
258 row.add_cell(p.num_ssts_uploaded.into());
259 row.add_cell(p.num_progress_key.into());
260 row.add_cell(p.num_pending_read_io.into());
261 row.add_cell(p.num_pending_write_io.into());
262 table.add_row(row);
263 }
264 println!("{table}");
265 } else {
266 println!("--- LSMtree Status ---");
267 println!("{:#?}", status);
268 println!("--- Task Assignment ---");
269 println!("{:#?}", assignment);
270 println!("--- Task Progress ---");
271 println!("{:#?}", progress);
272 }
273 Ok(())
274}
275
276pub async fn get_compaction_score(
277 context: &CtlContext,
278 id: CompactionGroupId,
279) -> anyhow::Result<()> {
280 let meta_client = context.meta_client().await?;
281 let scores = meta_client.get_compaction_score(id).await?;
282 let mut table = Table::new();
283 table.set_header({
284 let mut row = Row::new();
285 row.add_cell("Select Level".into());
286 row.add_cell("Target Level".into());
287 row.add_cell("Type".into());
288 row.add_cell("Score".into());
289 row
290 });
291 for s in scores.into_iter().sorted_by(|a, b| {
292 a.select_level
293 .cmp(&b.select_level)
294 .then_with(|| a.target_level.cmp(&b.target_level))
295 }) {
296 let mut row = Row::new();
297 row.add_cell(s.select_level.into());
298 row.add_cell(s.target_level.into());
299 row.add_cell(s.picker_type.into());
300 row.add_cell(s.score.into());
301 table.add_row(row);
302 }
303 println!("{table}");
304 Ok(())
305}
306
307pub async fn cancel_compact_task(context: &CtlContext, task_id: u64) -> anyhow::Result<()> {
308 let meta_client = context.meta_client().await?;
309 let ret = meta_client
310 .cancel_compact_task(task_id, TaskStatus::ManualCanceled)
311 .await?;
312 println!("cancel_compact_task {} ret {:?}", task_id, ret);
313
314 Ok(())
315}
316
317pub async fn merge_compaction_group(
318 context: &CtlContext,
319 left_group_id: CompactionGroupId,
320 right_group_id: CompactionGroupId,
321) -> anyhow::Result<()> {
322 let meta_client = context.meta_client().await?;
323 meta_client
324 .merge_compaction_group(left_group_id, right_group_id)
325 .await?;
326 Ok(())
327}