Skip to content

Commit

Permalink
feat(pageserver): support dry-run for gc-compaction, add statistics (#…
Browse files Browse the repository at this point in the history
…8557)

Add dry-run mode that does not produce any image layer + delta layer. I
will use this code to do some experiments and see how much space we can
reclaim for tenants on staging. Part of
#8002

* Add dry-run mode that runs the full compaction process without
updating the layer map. (We never call finish on the writers and the
files will be removed before exiting the function).
* Add compaction statistics and print them at the end of compaction.

---------

Signed-off-by: Alex Chi Z <[email protected]>
  • Loading branch information
skyzh authored and jcsp committed Aug 12, 2024
1 parent 3b4b9c1 commit 83afea3
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 12 deletions.
56 changes: 47 additions & 9 deletions pageserver/src/tenant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6899,7 +6899,10 @@ mod tests {
}

let cancel = CancellationToken::new();
tline.compact_with_gc(&cancel, &ctx).await.unwrap();
tline
.compact_with_gc(&cancel, EnumSet::new(), &ctx)
.await
.unwrap();

for (idx, expected) in expected_result.iter().enumerate() {
assert_eq!(
Expand Down Expand Up @@ -6993,7 +6996,10 @@ mod tests {
guard.cutoffs.time = Lsn(0x40);
guard.cutoffs.space = Lsn(0x40);
}
tline.compact_with_gc(&cancel, &ctx).await.unwrap();
tline
.compact_with_gc(&cancel, EnumSet::new(), &ctx)
.await
.unwrap();

Ok(())
}
Expand Down Expand Up @@ -7327,7 +7333,10 @@ mod tests {
}

let cancel = CancellationToken::new();
tline.compact_with_gc(&cancel, &ctx).await.unwrap();
tline
.compact_with_gc(&cancel, EnumSet::new(), &ctx)
.await
.unwrap();

for idx in 0..10 {
assert_eq!(
Expand All @@ -7353,7 +7362,10 @@ mod tests {
guard.cutoffs.time = Lsn(0x40);
guard.cutoffs.space = Lsn(0x40);
}
tline.compact_with_gc(&cancel, &ctx).await.unwrap();
tline
.compact_with_gc(&cancel, EnumSet::new(), &ctx)
.await
.unwrap();

Ok(())
}
Expand Down Expand Up @@ -7898,11 +7910,28 @@ mod tests {
verify_result().await;

let cancel = CancellationToken::new();
tline.compact_with_gc(&cancel, &ctx).await.unwrap();
let mut dryrun_flags = EnumSet::new();
dryrun_flags.insert(CompactFlags::DryRun);

tline
.compact_with_gc(&cancel, dryrun_flags, &ctx)
.await
.unwrap();
// We expect layer map to be the same b/c the dry run flag, but we don't know whether there will be other background jobs
// cleaning things up, and therefore, we don't do sanity checks on the layer map during unit tests.
verify_result().await;

tline
.compact_with_gc(&cancel, EnumSet::new(), &ctx)
.await
.unwrap();
verify_result().await;

// compact again
tline.compact_with_gc(&cancel, &ctx).await.unwrap();
tline
.compact_with_gc(&cancel, EnumSet::new(), &ctx)
.await
.unwrap();
verify_result().await;

// increase GC horizon and compact again
Expand All @@ -7912,11 +7941,17 @@ mod tests {
guard.cutoffs.time = Lsn(0x38);
guard.cutoffs.space = Lsn(0x38);
}
tline.compact_with_gc(&cancel, &ctx).await.unwrap();
tline
.compact_with_gc(&cancel, EnumSet::new(), &ctx)
.await
.unwrap();
verify_result().await; // no wals between 0x30 and 0x38, so we should obtain the same result

// not increasing the GC horizon and compact again
tline.compact_with_gc(&cancel, &ctx).await.unwrap();
tline
.compact_with_gc(&cancel, EnumSet::new(), &ctx)
.await
.unwrap();
verify_result().await;

Ok(())
Expand Down Expand Up @@ -8097,7 +8132,10 @@ mod tests {
verify_result().await;

let cancel = CancellationToken::new();
branch_tline.compact_with_gc(&cancel, &ctx).await.unwrap();
branch_tline
.compact_with_gc(&cancel, EnumSet::new(), &ctx)
.await
.unwrap();

verify_result().await;

Expand Down
8 changes: 8 additions & 0 deletions pageserver/src/tenant/storage_layer/image_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,10 @@ struct ImageLayerWriterInner {
}

impl ImageLayerWriterInner {
fn size(&self) -> u64 {
self.tree.borrow_writer().size() + self.blob_writer.size()
}

///
/// Start building a new image layer.
///
Expand Down Expand Up @@ -1044,6 +1048,10 @@ impl ImageLayerWriter {
.finish(timeline, ctx, Some(end_key))
.await
}

pub(crate) fn size(&self) -> u64 {
self.inner.as_ref().unwrap().size()
}
}

impl Drop for ImageLayerWriter {
Expand Down
1 change: 1 addition & 0 deletions pageserver/src/tenant/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ pub(crate) enum CompactFlags {
ForceRepartition,
ForceImageLayerCreation,
EnhancedGcBottomMostCompaction,
DryRun,
}

impl std::fmt::Debug for Timeline {
Expand Down
Loading

0 comments on commit 83afea3

Please sign in to comment.