diff --git a/src/mito2/src/compaction/compactor.rs b/src/mito2/src/compaction/compactor.rs index ca381c9df5c..4ba901dc6ea 100644 --- a/src/mito2/src/compaction/compactor.rs +++ b/src/mito2/src/compaction/compactor.rs @@ -396,6 +396,16 @@ impl CompactionRegionOpener { } impl CompactionRegion { + /// Estimates the memory admission reserves for merging `unit`, with the read parallelism and + /// merge mode the merge uses. + pub(crate) fn estimated_memory_bytes(&self, unit: &CompactionUnit) -> u64 { + let options = &self.current_version.options; + unit.estimated_memory_bytes( + compaction_output_parallelism(&self.region_options, &self.engine_config), + !options.append_mode && options.merge_mode() == MergeMode::LastNonNull, + ) + } + /// Get the file purger of the compaction region. pub fn file_purger(&self) -> Option> { self.file_purger.clone() @@ -595,6 +605,16 @@ fn sst_info_from_file_meta(meta: &FileMeta) -> SstInfo { } } +/// Parallelism of encoding an output, which is also the parallelism of reading its inputs. +/// Admission sizes the read-ahead with it, so every caller must use this function. +pub(crate) fn compaction_output_parallelism( + region_options: &RegionOptions, + engine_config: &MitoConfig, +) -> usize { + region_options + .compaction_output_parallelism_or(engine_config.experimental_compaction_output_parallelism) +} + /// `[MergeOutput]` represents the output of merging SST files. #[derive(Default, Debug, Serialize, Deserialize)] pub struct MergeOutput { @@ -1006,6 +1026,8 @@ impl DefaultCompactor { compaction_time_window: Some(unit.time_window_size), ..Default::default() }; + // Outputs are merged one at a time; `CompactionUnit::estimated_memory_bytes` reserves + // only the largest output. for output in &unit.outputs { if self.cancel_token.is_cancelled() { return error::CompactionCancelledSnafu.fail(); @@ -1023,13 +1045,10 @@ impl DefaultCompactor { // Narrowed by the decoded sizes of the inputs once their footers are read. row_group_size: context.region_options.row_group_size(), float_field_encoding: context.region_options.float_field_encoding, - encode_parallelism: context - .region_options - .compaction_output_parallelism_or( - context - .engine_config - .experimental_compaction_output_parallelism, - ), + encode_parallelism: compaction_output_parallelism( + &context.region_options, + &context.engine_config, + ), encode_runtime: EncodeRuntime::Compact, }, self.cancel_token.clone(), @@ -1104,13 +1123,10 @@ where // Narrowed by the decoded sizes of the inputs once their footers are read. row_group_size: compaction_region.region_options.row_group_size(), float_field_encoding: compaction_region.region_options.float_field_encoding, - encode_parallelism: compaction_region - .region_options - .compaction_output_parallelism_or( - compaction_region - .engine_config - .experimental_compaction_output_parallelism, - ), + encode_parallelism: compaction_output_parallelism( + &compaction_region.region_options, + &compaction_region.engine_config, + ), encode_runtime: EncodeRuntime::Compact, }; let merger = self.merger.clone(); diff --git a/src/mito2/src/compaction/reader.rs b/src/mito2/src/compaction/reader.rs index 32841126968..544da1c5e9b 100644 --- a/src/mito2/src/compaction/reader.rs +++ b/src/mito2/src/compaction/reader.rs @@ -236,7 +236,7 @@ pub(crate) fn json2_rewrite_targets( const MAX_CONCURRENT_METADATA_READS: usize = 8; /// Number of merged batches buffered between the merge task and the SST writer. -const MERGED_BATCH_CHANNEL_SIZE: usize = 2; +pub(crate) const MERGED_BATCH_CHANNEL_SIZE: usize = 2; /// Reads and merges `source` on a separate task, so the SST writer can encode /// merged batches while the next ones are being read and merged. diff --git a/src/mito2/src/compaction/scheduler/local.rs b/src/mito2/src/compaction/scheduler/local.rs index d938267cda9..8872a8d2ea8 100644 --- a/src/mito2/src/compaction/scheduler/local.rs +++ b/src/mito2/src/compaction/scheduler/local.rs @@ -287,9 +287,13 @@ impl CompactionScheduler { cancellation_requested: false, }; let context = CompactionContext::try_from(®ion)?; + let estimated_memory: Vec<_> = units + .iter() + .map(|unit| region.estimated_memory_bytes(unit)) + .collect(); // Planning is complete; unit tasks must not pin the other units' inputs. drop(region); - for unit in units { + for (unit, estimated_memory_bytes) in units.into_iter().zip(estimated_memory) { let files = reserved.for_inputs(&unit.inputs); let id = Self::next_plan_id(&mut self.next_plan_id); let execution = CompactionExecution::new(id, files); @@ -308,7 +312,7 @@ impl CompactionScheduler { compaction_context: context.clone(), request_sender: self.request_sender.clone(), listener: self.listener.clone(), - estimated_memory_bytes: unit.estimated_memory_bytes(), + estimated_memory_bytes, unit, memory_manager: self.memory_manager.clone(), memory_policy: self.memory_policy, diff --git a/src/mito2/src/compaction/scheduler/planning.rs b/src/mito2/src/compaction/scheduler/planning.rs index dc8175559a4..cdd2ed15b8e 100644 --- a/src/mito2/src/compaction/scheduler/planning.rs +++ b/src/mito2/src/compaction/scheduler/planning.rs @@ -611,7 +611,8 @@ impl CompactionScheduler { .fail(); }; units.retain(|unit| { - let estimated_bytes = unit.estimated_memory_bytes(); + let estimated_bytes = +compaction_region.estimated_memory_bytes(unit); let Some(limit_bytes) = self.exceeds_compaction_memory_limit(estimated_bytes) else { return true }; COMPACTION_MEMORY_REJECTED .with_label_values(&["oversized"]) diff --git a/src/mito2/src/compaction/unit.rs b/src/mito2/src/compaction/unit.rs index 48fdcdf5c62..b1dcd629ed7 100644 --- a/src/mito2/src/compaction/unit.rs +++ b/src/mito2/src/compaction/unit.rs @@ -14,9 +14,12 @@ use std::collections::{BTreeMap, HashMap, HashSet}; +use crate::batch_size::estimate_batch_size; use crate::compaction::CompactionOutput; use crate::compaction::picker::PickerOutput; -use crate::sst::file::FileHandle; +use crate::compaction::reader::MERGED_BATCH_CHANNEL_SIZE; +use crate::read::seq_scan::compaction_input_channel_size; +use crate::sst::file::{FileHandle, FileMeta}; /// A complete input dependency group, merged and published atomically. /// @@ -120,21 +123,144 @@ impl CompactionUnit { .fold(0u64, |total, file| total.saturating_add(file.size())) } - /// Estimates merge memory once per input, even when several outputs read it. - pub(crate) fn estimated_memory_bytes(&self) -> u64 { - if self.outputs.is_empty() { - return 0; + /// Estimates the memory budget of merging this unit. Outputs are merged one after another, + /// so the unit needs the budget of its largest output. + /// `retains_dedup_batch` is set when the merge deduplicates in `LastNonNull` mode, which keeps + /// the previous output batch until the next one shows whether its last row is updated. + pub(crate) fn estimated_memory_bytes( + &self, + read_parallelism: usize, + retains_dedup_batch: bool, + ) -> u64 { + self.outputs + .iter() + .map(|output| { + output_memory_bytes(&output.inputs, read_parallelism, retains_dedup_batch) + }) + .max() + .unwrap_or(0) + } +} + +/// Returns the bytes of the largest row group of a file once decoded. Dictionary encoding hides +/// decoded values from the uncompressed size; files written before the decoded size was recorded +/// report 0 for it. +fn max_row_group_bytes(meta: &FileMeta) -> u64 { + meta.max_row_group_uncompressed_size + .max(meta.max_row_group_decoded_size) +} + +/// Row group size of a file, approximated from its file metadata. +struct RowGroupShape { + /// Decoded bytes of the largest row group. + bytes: u64, + /// Average rows of a row group; the metadata keeps no per-row-group row counts. + rows: u64, + num_row_groups: u64, +} + +impl RowGroupShape { + fn of(meta: &FileMeta) -> Option { + let bytes = max_row_group_bytes(meta); + if bytes == 0 || meta.num_rows == 0 || meta.num_row_groups == 0 { + return None; } - // Dictionary encoding hides decoded values from the uncompressed size; files written - // before the decoded size was recorded report 0 for it. - self.inputs.iter().fold(0u64, |total, file| { - let meta = file.meta_ref(); - total.saturating_add( - meta.max_row_group_uncompressed_size - .max(meta.max_row_group_decoded_size), - ) + Some(Self { + bytes, + rows: (meta.num_rows / meta.num_row_groups).max(1), + num_row_groups: meta.num_row_groups, }) } + + fn row_width(&self) -> u64 { + self.bytes.div_ceil(self.rows) + } +} + +/// Estimates the peak memory of merging `inputs` into one output, following the read path: +/// +/// - Every input reader holds its current row group. +/// - With a read parallelism above 1, every input is read in its own task and also holds +/// batches ahead of the merge: a full channel, one batch being sent and one in the merge. A +/// batch never spans row groups, and the batches of an input never exceed the whole file. +/// - Inputs read serially hold the batch the merge is consuming. +/// - The merge retains consumed input batches until it emits an output batch. +/// - `LastNonNull` dedup keeps the previous output batch. +/// - Merged batches pass a channel to the writer, with one more batch on each side of it. +/// +/// The writer's row group buffer, index builders and the batch dedup concatenates when a key +/// spans two batches are not covered. +fn output_memory_bytes( + inputs: &[FileHandle], + read_parallelism: usize, + retains_dedup_batch: bool, +) -> u64 { + let shapes: Vec<_> = inputs + .iter() + .filter_map(|file| RowGroupShape::of(file.meta_ref())) + .collect(); + let row_group_bytes = inputs.iter().fold(0u64, |total, file| { + total.saturating_add(max_row_group_bytes(file.meta_ref())) + }); + if shapes.is_empty() { + return row_group_bytes; + } + // Compaction reads every input of an output with one batch size, picked from the widest row. + // The reader also sees inputs without statistics in `FileMeta`; those are not sized here, so + // with mixed inputs the batch size is approximate. + let batch_rows = + estimate_batch_size(shapes.iter().map(|shape| (shape.rows, shape.bytes))) as u64; + let merges = inputs.len() > 1; + // Matches the condition under which `SeqScan` spawns a task per input. + let batches_per_input = if read_parallelism > 1 && merges { + compaction_input_channel_size() as u64 + 2 + } else { + 1 + }; + let input_batch_bytes = shapes.iter().fold(0u64, |total, shape| { + let batch_bytes = batch_rows.min(shape.rows).saturating_mul(shape.row_width()); + let file_bytes = shape.bytes.saturating_mul(shape.num_row_groups); + total.saturating_add( + batches_per_input + .saturating_mul(batch_bytes) + .min(file_bytes), + ) + }); + // The merge holds one output batch when it merges, `LastNonNull` dedup holds one more, and the + // channel to the writer holds the rest. A merged batch has `batch_rows` rows; a single input passes its batches through, and + // those never span row groups. + let output_batches = + MERGED_BATCH_CHANNEL_SIZE as u64 + 2 + u64::from(merges) + u64::from(retains_dedup_batch); + let rows_per_batch = if merges { + batch_rows + } else { + batch_rows.min(shapes[0].rows) + }; + let downstream_bytes = + widest_rows_bytes(&shapes, output_batches.saturating_mul(rows_per_batch)); + + row_group_bytes + .saturating_add(input_batch_bytes) + .saturating_add(downstream_bytes) +} + +/// Bounds the bytes of `rows` distinct rows drawn from the inputs: the widest rows first, and no +/// input gives more rows than it has. Batches past the readers hold distinct rows, so one wide +/// row does not make every row of those batches wide. +fn widest_rows_bytes(shapes: &[RowGroupShape], rows: u64) -> u64 { + let mut by_width: Vec<_> = shapes.iter().collect(); + by_width.sort_unstable_by_key(|shape| std::cmp::Reverse(shape.row_width())); + let mut remaining = rows; + let mut bytes = 0u64; + for shape in by_width { + let taken = remaining.min(shape.rows.saturating_mul(shape.num_row_groups)); + bytes = bytes.saturating_add(taken.saturating_mul(shape.row_width())); + remaining -= taken; + if remaining == 0 { + break; + } + } + bytes } /// Finds a dependency group's representative while shortening the parent chain. @@ -152,6 +278,7 @@ mod tests { use super::*; use crate::compaction::test_util::{new_file_handle, new_file_handle_with_size_and_sequence}; + use crate::test_util::new_noop_file_purger; #[test] fn test_estimated_memory_counts_decoded_row_groups() { @@ -173,7 +300,10 @@ mod tests { ..Default::default() }) .unwrap(); - assert_eq!((128 << 20) + (4 << 20), units[0].estimated_memory_bytes()); + assert_eq!( + (128 << 20) + (4 << 20), + units[0].estimated_memory_bytes(1, false) + ); } /// Builds an L1 output that preserves deletion markers for grouping tests. @@ -266,6 +396,89 @@ mod tests { assert_eq!(files[0].file_id(), units[2].inputs[0].file_id()); } + #[test] + fn test_memory_follows_the_merge_of_each_output() { + const MIB: u64 = 1024 * 1024; + let file = |row_group_bytes: u64, rows_per_row_group: u64, num_row_groups: u64| { + FileHandle::new( + FileMeta { + file_id: FileId::random(), + max_row_group_uncompressed_size: row_group_bytes, + num_rows: rows_per_row_group * num_row_groups, + num_row_groups, + ..Default::default() + }, + new_noop_file_purger(), + ) + }; + let unit = |outputs: Vec| { + let mut units = CompactionUnit::from_picker(PickerOutput { + outputs, + ..Default::default() + }) + .unwrap(); + assert_eq!(1, units.len()); + units.pop().unwrap() + }; + // Rows of 512 bytes are read in 8192-row batches of 4 MiB. Each input holds a 50 MiB row + // group and the batch the merge consumes; the merge retains one 4 MiB output batch and 4 + // more sit between the merge and the writer. + let wide_groups = || file(50 * MIB, 100 * 1024, 10); + let (a, b, c) = (wide_groups(), wide_groups(), wide_groups()); + let serial = 2 * 50 * MIB + 2 * 4 * MIB + 4 * MIB + 16 * MIB; + assert_eq!( + serial, + unit(vec![output(vec![a.clone(), b.clone()])]).estimated_memory_bytes(1, false) + ); + // `LastNonNull` dedup holds back one more output batch. + assert_eq!( + serial + 4 * MIB, + unit(vec![output(vec![a.clone(), b.clone()])]).estimated_memory_bytes(1, true) + ); + // Read in parallel, each input also holds the batches read ahead of the merge. + let parallel = 2 * 50 * MIB + 2 * 16 * MIB + 4 * MIB + 16 * MIB; + assert_eq!( + parallel, + unit(vec![output(vec![a.clone(), b.clone()])]).estimated_memory_bytes(4, false) + ); + // Outputs sharing an input run one after another, so the unit needs one output's budget. + assert_eq!( + parallel, + unit(vec![output(vec![a.clone(), b]), output(vec![a, c])]) + .estimated_memory_bytes(4, false) + ); + + // Rows of 1 MiB make 64-row batches, but an input batch stops at its 4-row row group. + // The merge output still holds 64 rows. + let narrow_groups = || file(4 * MIB, 4, 1000); + let (d, e) = (narrow_groups(), narrow_groups()); + assert_eq!( + 2 * 4 * MIB + 2 * 16 * MIB + 64 * MIB + 4 * 64 * MIB, + unit(vec![output(vec![d.clone(), e])]).estimated_memory_bytes(4, false) + ); + // A single input is read without read-ahead or merge, so its batches stay row groups. + assert_eq!( + 4 * MIB + 4 * MIB + 4 * 4 * MIB, + unit(vec![output(vec![d])]).estimated_memory_bytes(4, false) + ); + + // One 1 MiB row merged with 1 KiB rows: of the 320 rows past the readers, only one is + // wide. + let (wide_row, narrow_rows) = (file(MIB, 1, 1), file(MIB, 1024, 512)); + assert_eq!( + 2 * MIB + MIB + 64 * 1024 + MIB + 319 * 1024, + unit(vec![output(vec![wide_row, narrow_rows])]).estimated_memory_bytes(1, false) + ); + + // Two one-row files of 8 KiB: a batch holds both rows at most, and the batches past + // the readers never hold more than the two rows. + let (f, g) = (file(8 * 1024, 1, 1), file(8 * 1024, 1, 1)); + assert_eq!( + 2 * 8 * 1024 + 2 * 8 * 1024 + 2 * 8 * 1024, + unit(vec![output(vec![f, g])]).estimated_memory_bytes(4, false) + ); + } + #[test] fn test_compaction_unit_expiration_inputs() { let file = new_file_handle(FileId::random(), 0, 100, 0); @@ -285,7 +498,7 @@ mod tests { assert_eq!(1, units.len()); assert_eq!(1, units[0].inputs.len()); assert!(units[0].outputs.is_empty()); - assert_eq!(0, units[0].estimated_memory_bytes()); + assert_eq!(0, units[0].estimated_memory_bytes(1, false)); assert!( CompactionUnit::from_picker(PickerOutput::default()) .unwrap() diff --git a/src/mito2/src/read/seq_scan.rs b/src/mito2/src/read/seq_scan.rs index 6d996efabd6..e37cb080465 100644 --- a/src/mito2/src/read/seq_scan.rs +++ b/src/mito2/src/read/seq_scan.rs @@ -219,7 +219,7 @@ impl SeqScan { partition_ranges.len(), sources.len() ); - let channel_size = compute_parallel_channel_size(DEFAULT_READ_BATCH_SIZE); + let channel_size = compaction_input_channel_size(); let parallelism = stream_ctx.input.compaction_read_parallelism; if parallelism > 1 && sources.len() > 1 { // More permits than inputs cannot increase concurrency. Clamping also @@ -743,6 +743,11 @@ impl fmt::Debug for SeqScan { } } +/// Channel capacity of each compaction input read in its own task. +pub(crate) fn compaction_input_channel_size() -> usize { + compute_parallel_channel_size(DEFAULT_READ_BATCH_SIZE) +} + /// Builds flat sources for the partition range and push them to the `sources` vector. /// Returns the estimated rows per batch after splitting if splitting is applied, or `None`. pub(crate) async fn build_flat_sources(