Unverified Commit a04ebd7d authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

turbo-persistence: don't shrink the shard count before the first bottom merge (#100424)

## What

Defer shrinking shard counts of turbo-persistence families until there
has been a bottom merge. This reduces the maximum sizes of initial
compaction jobs at the cost of slightly more SST files until that
happens. The benefit is more parallelism and smaller job sizes for
initial bottom runs.

## Why

A 'bottom' merge is when we have performed a 'full compaction' of a
single shard of a family (aka table). Families are divided into shards
to accelerate lookups and reduce the size of compaction jobs. Part of
the compaction strategy is to occasionally compress the full shard into
a 'bottom' a set of hot and cold files that do not overlap (well hot and
cold can overlap, but hots cannot overlap with hots and colds cannot
with colds).

Before a family has a 'bottom', its shard count can shrink. Commits
compute the count too, so after a small first commit (e.g. the first
snapshot in dev) the next commits already wrote files spanning all of
the family's initial shards. Such files tie every shard into one
component, so the first bottom merge of the family was a single job over
all its files. e.g. for next-site TaskData went from 8 to 4 shards and
TaskMeta from 4 to 1, doubling and quadrupling the job sizes.

Now the count only grows until the family has bottom runs. Shrinking
afterwards is free: the bottom runs of the former shards are disjoint,
so together they are the bottom run of the combined shard and nothing is
rewritten. The cost is a less than optimal shard count, and so more,
smaller commit files, until the first compaction that is triggered by
our normal triggers. The benefit is more parallelism in those initial
bottom run jobs

Also, an intermediate merge that happens takes all files of a shard
without a bottom run now writes it as the shard's bottom run, since it
rewrites just as much.
parent 5442f176
...@@ -180,9 +180,13 @@ fn components<T: Compactable>(compactables: &[T], shard_bits: ShardBits) -> Vec< ...@@ -180,9 +180,13 @@ fn components<T: Compactable>(compactables: &[T], shard_bits: ShardBits) -> Vec<
/// `size_ratio_percent` larger than the files taken so far, but at least the two newest. It always /// `size_ratio_percent` larger than the files taken so far, but at least the two newest. It always
/// takes the newest files, so its output can be placed after all files, and the older files it /// takes the newest files, so its output can be placed after all files, and the older files it
/// leaves out stay older than it. /// leaves out stay older than it.
///
/// `above_is_all_shard_files` is true when the shard has no bottom run, so `above` holds all of its
/// files. A merge that takes all of them then writes the shard's bottom run.
fn intermediate_candidate<T: Compactable>( fn intermediate_candidate<T: Compactable>(
compactables: &[T], compactables: &[T],
above: Vec<usize>, above: Vec<usize>,
above_is_all_shard_files: bool,
config: &CompactConfig, config: &CompactConfig,
) -> Candidate { ) -> Candidate {
let priority = above.len() as f32 / config.max_files_above_bottom.max(1) as f32; let priority = above.len() as f32 / config.max_files_above_bottom.max(1) as f32;
...@@ -213,7 +217,9 @@ fn intermediate_candidate<T: Compactable>( ...@@ -213,7 +217,9 @@ fn intermediate_candidate<T: Compactable>(
priority, priority,
job: MergeJob { job: MergeJob {
members: above, members: above,
bottom: false, // Merging all files of a shard without a bottom run rewrites as much as a bottom merge,
// so make the result its bottom run.
bottom: above_is_all_shard_files && first == 0,
}, },
} }
} }
...@@ -294,9 +300,20 @@ fn plan_family<T: Compactable>( ...@@ -294,9 +300,20 @@ fn plan_family<T: Compactable>(
// merges every shard anyway, and then only the quota limits the rewrites. // merges every shard anyway, and then only the quota limits the rewrites.
let overdue = limit > 0.0 let overdue = limit > 0.0
&& (amplification > 2.0 * limit || above.len() > 2 * config.max_files_above_bottom); && (amplification > 2.0 * limit || above.len() > 2 * config.max_files_above_bottom);
bottom_candidates.push((candidate, bottom_bytes + above_bytes, above, overdue)); bottom_candidates.push((
candidate,
bottom_bytes + above_bytes,
above,
overdue,
bottom.is_empty(),
));
} else if above.len() > config.max_files_above_bottom.max(1) { } else if above.len() > config.max_files_above_bottom.max(1) {
intermediate_candidates.push(intermediate_candidate(compactables, above, config)); intermediate_candidates.push(intermediate_candidate(
compactables,
above,
/* above_is_all_shard_files= */ bottom.is_empty(),
config,
));
} }
} }
...@@ -311,7 +328,7 @@ fn plan_family<T: Compactable>( ...@@ -311,7 +328,7 @@ fn plan_family<T: Compactable>(
u64::MAX u64::MAX
}; };
let mut result = Vec::new(); let mut result = Vec::new();
for (candidate, cost, above, overdue) in bottom_candidates { for (candidate, cost, above, overdue, above_is_all_shard_files) in bottom_candidates {
// The first job always runs, even when the quota is 0, and we keep scheduling jobs while // The first job always runs, even when the quota is 0, and we keep scheduling jobs while
// the quota isn't reached, so that we don't pathologically rewrite too little. // the quota isn't reached, so that we don't pathologically rewrite too little.
if result.is_empty() || rewrite_target > 0 || overdue { if result.is_empty() || rewrite_target > 0 || overdue {
...@@ -320,7 +337,12 @@ fn plan_family<T: Compactable>( ...@@ -320,7 +337,12 @@ fn plan_family<T: Compactable>(
} else if above.len() > config.max_files_above_bottom.max(1) { } else if above.len() > config.max_files_above_bottom.max(1) {
// The quota is reached, but an intermediate merge of the same shard is cheap and // The quota is reached, but an intermediate merge of the same shard is cheap and
// still bounds the number of files a lookup consults. // still bounds the number of files a lookup consults.
intermediate_candidates.push(intermediate_candidate(compactables, above, config)); intermediate_candidates.push(intermediate_candidate(
compactables,
above,
above_is_all_shard_files,
config,
));
} }
} }
intermediate_candidates.sort_by(|a, b| b.priority.total_cmp(&a.priority)); intermediate_candidates.sort_by(|a, b| b.priority.total_cmp(&a.priority));
...@@ -627,6 +649,24 @@ mod tests { ...@@ -627,6 +649,24 @@ mod tests {
assert_eq!(plan(&files, 2, &test_config()), vec![(vec![0, 2, 3], true)]); assert_eq!(plan(&files, 2, &test_config()), vec![(vec![0, 2, 3], true)]);
} }
#[test]
fn test_merging_all_files_of_a_shard_without_bottom_run_makes_a_bottom_run() {
// Too few bytes for a bottom merge, but too many files: the intermediate merge takes all of
// them, so it writes the shard's bottom run.
let files = (0..5)
.map(|_| file(0, 0, 10, false, true))
.collect::<Vec<_>>();
let config = CompactConfig {
min_bottom_merge_bytes: 1000,
..test_config()
};
assert_eq!(plan(&files, 0, &config), vec![(vec![0, 1, 2, 3, 4], true)]);
// With a bottom run, it stays an intermediate merge.
let mut files = vec![file(0, 0, 10, true, false)];
files.extend((0..5).map(|_| file(0, 0, 10, false, true)));
assert_eq!(plan(&files, 0, &config), vec![(vec![1, 2, 3, 4, 5], false)]);
}
#[test] #[test]
fn test_merge_job_limit_across_families() { fn test_merge_job_limit_across_families() {
let a = [file(0, 0, 100, false, true), file(0, 0, 100, false, true)]; let a = [file(0, 0, 100, false, true), file(0, 0, 100, false, true)];
......
...@@ -2642,7 +2642,7 @@ fn rebuild_shard_index<const FAMILIES: usize>( ...@@ -2642,7 +2642,7 @@ fn rebuild_shard_index<const FAMILIES: usize>(
/// `FamilyConfig::initial_shard_bits`, is recorded in every meta file, and follows the size of the /// `FamilyConfig::initial_shard_bits`, is recorded in every meta file, and follows the size of the
/// bottom runs, which is about the size of the live data after compaction (see /// bottom runs, which is about the size of the live data after compaction (see
/// [`ShardBits::maybe_reshard`]). Before the first bottom merge, it follows the size of all files, /// [`ShardBits::maybe_reshard`]). Before the first bottom merge, it follows the size of all files,
/// so that the first bottom merges already split at a fitting shard count. /// but only grows.
fn shard_bits<const FAMILIES: usize>( fn shard_bits<const FAMILIES: usize>(
config: &DbConfig<FAMILIES>, config: &DbConfig<FAMILIES>,
meta_files_by_family: &[Vec<MetaFile>; FAMILIES], meta_files_by_family: &[Vec<MetaFile>; FAMILIES],
...@@ -2672,11 +2672,17 @@ fn shard_bits<const FAMILIES: usize>( ...@@ -2672,11 +2672,17 @@ fn shard_bits<const FAMILIES: usize>(
} }
// Before the first bottom merge there is no bottom run, so use the size of all files. They // Before the first bottom merge there is no bottom run, so use the size of all files. They
// are only the first commits, so they hold little garbage. // are only the first commits, so they hold little garbage.
let estimated_bytes = if covered_shards.is_empty() { //
all_bytes // But don't shrink yet. The first bottom merges rewrite the whole family, and shrinking
} else { // would combine the files of multiple shards into one large job instead of several
bottom_bytes.saturating_mul(u64::from(current.count())) / covered_shards.len() as u64 // small ones that run in parallel. Once the shards have bottom runs, shrinking is
}; // free: the disjoint bottom runs of the former shards together are the bottom run
// of the combined shard.
if covered_shards.is_empty() {
return current.maybe_grow(all_bytes, config.target_shard_size);
}
let estimated_bytes =
bottom_bytes.saturating_mul(u64::from(current.count())) / covered_shards.len() as u64;
current.maybe_reshard(estimated_bytes, config.target_shard_size) current.maybe_reshard(estimated_bytes, config.target_shard_size)
}) })
} }
......
...@@ -78,6 +78,11 @@ impl ShardBits { ...@@ -78,6 +78,11 @@ impl ShardBits {
} }
bits bits
} }
/// Like [`Self::maybe_reshard`], but never shrinks.
pub fn maybe_grow(self, bytes: u64, target_shard_size: u64) -> Self {
self.maybe_reshard(bytes, target_shard_size).max(self)
}
} }
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
...@@ -181,6 +186,21 @@ mod tests { ...@@ -181,6 +186,21 @@ mod tests {
} }
} }
#[test]
fn test_maybe_grow_never_shrinks() {
const MB: u64 = 1024 * 1024;
let bits = ShardBits::new(3);
// 8 shards of 10 MB each would shrink, but don't when only growing.
assert!(bits.maybe_reshard(80 * MB, 256 * MB) < bits);
assert_eq!(bits.maybe_grow(80 * MB, 256 * MB), bits);
// 8 shards of 1 GB each grow either way.
assert_eq!(
bits.maybe_grow(8 * 1024 * MB, 256 * MB),
bits.maybe_reshard(8 * 1024 * MB, 256 * MB)
);
assert!(bits.maybe_grow(8 * 1024 * MB, 256 * MB) > bits);
}
#[test] #[test]
fn test_one_more_bit_splits_shards() { fn test_one_more_bit_splits_shards() {
let coarse = ShardBits::new(2).range(3); let coarse = ShardBits::new(2).range(3);
......
...@@ -3176,12 +3176,21 @@ fn initial_shard_bits_are_a_starting_value(#[case] mmap: bool) -> Result<()> { ...@@ -3176,12 +3176,21 @@ fn initial_shard_bits_are_a_starting_value(#[case] mmap: bool) -> Result<()> {
// The first commit has nothing to size the shards by, so it uses the initial count. // The first commit has nothing to size the shards by, so it uses the initial count.
commit()?; commit()?;
assert_eq!(newest_shard_bits()?, 2); assert_eq!(newest_shard_bits()?, 2);
// Without a bottom run yet, the next commit sizes the shards by all files: the tiny family // Without a bottom run yet, the count doesn't shrink, so that the first bottom merges are one
// shrinks to a single shard, far below the initial count. // per shard instead of a single large one, even though the tiny family fits in one shard.
commit()?; commit()?;
assert_eq!(newest_shard_bits()?, 0); assert_eq!(newest_shard_bits()?, 2);
// Once compacted, the bottom run is as tiny, so the count stays. // The first bottom merges are split at the initial count.
db.full_compact()?; db.full_compact()?;
let bottom_files = db
.meta_info()?
.iter()
.flat_map(|meta| &meta.entries)
.filter(|entry| entry.flags.bottom())
.count();
assert_eq!(bottom_files, 4);
// With bottom runs, the tiny family shrinks to a single shard. That doesn't rewrite anything:
// the four bottom runs together are its bottom run.
commit()?; commit()?;
assert_eq!(newest_shard_bits()?, 0); assert_eq!(newest_shard_bits()?, 0);
for key in 0..1000u32 { for key in 0..1000u32 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment