From de8eace204fff3581ff464de187fb2cd00f9b9b5 Mon Sep 17 00:00:00 2001 From: Hauser Zhi Date: Tue, 1 Dec 2026 18:31:48 +0800 Subject: [PATCH] perf(scanner): skip redundant snapshot coverage checks Use the sealed frontier when available and validate only compacted receipts whose ranges can cover a missing frontier. Co-Authored-By: heihutu Co-Authored-By: zhi22915 Co-Authored-By: qunjiazhu --- crates/scanner/src/scanner_folder.rs | 18 ++++++++---------- .../directory_cursor/checkpoint.rs | 17 +++++++++++++---- .../checkpoint/storage_fixture.rs | 13 +++++++++++++ 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/crates/scanner/src/scanner_folder.rs b/crates/scanner/src/scanner_folder.rs index f2ca528a22..8f11615c8b 100644 --- a/crates/scanner/src/scanner_folder.rs +++ b/crates/scanner/src/scanner_folder.rs @@ -1693,16 +1693,14 @@ impl FolderScanner { snapshot.seal_scan_frontier(self.coverage_frontier.as_deref()).ok()?; // A resumed readdir can run before covered subtrees are copied. Keep // its durable receipt until this snapshot can represent that coverage. - let compacted_work = snapshot - .info - .scan_directory_work - .iter() - .filter_map(|directory| directory.progress(&snapshot.info)); - if self - .coverage_frontier - .as_deref() - .is_some_and(|frontier| !compacted_work.clone().any(|proof| proof.covers(frontier))) - && snapshot.info.scan_coverage_receipt.is_none() + if snapshot.info.scan_coverage_receipt.is_none() + && self.coverage_frontier.as_deref().is_some_and(|frontier| { + !snapshot + .info + .scan_directory_work + .iter() + .any(|directory| directory.covers_bound_path(&snapshot.info, frontier)) + }) { return None; } diff --git a/crates/scanner/src/scanner_folder/directory_cursor/checkpoint.rs b/crates/scanner/src/scanner_folder/directory_cursor/checkpoint.rs index 2f366f8126..f8ef368f28 100644 --- a/crates/scanner/src/scanner_folder/directory_cursor/checkpoint.rs +++ b/crates/scanner/src/scanner_folder/directory_cursor/checkpoint.rs @@ -206,13 +206,17 @@ impl DirectoryProgress { self.same_source(previous) && self.through >= previous.through } pub(crate) fn covers(&self, path: &str) -> bool { - path.strip_prefix(&self.parent) - .and_then(|tail| tail.strip_prefix('/')) - .and_then(|tail| tail.split('/').next()) - .is_some_and(|child| child <= self.through.as_str()) + compacted_child_covers(&self.parent, &self.through, path) } } +fn compacted_child_covers(parent: &str, through: &str, path: &str) -> bool { + path.strip_prefix(parent) + .and_then(|tail| tail.strip_prefix('/')) + .and_then(|tail| tail.split('/').next()) + .is_some_and(|child| child <= through) +} + impl DirectoryCheckpoint { /// Deserialize each replica's independent receipt, then intern equal /// verified immutable name pages across the bucket's active directories. @@ -320,6 +324,11 @@ impl DirectoryCheckpoint { let manifest = self.bound_manifest(info)?; Some(self.compacted_progress(physical, manifest, &done.through)) } + pub(crate) fn covers_bound_path(&self, info: &DataUsageCacheInfo, path: &str) -> bool { + self.aggregate + .as_ref() + .is_some_and(|done| compacted_child_covers(&self.parent, &done.through, path) && self.is_bound_to(info)) + } /// A display contribution contains only acknowledged compacted children. pub(crate) fn display_usage(&self, info: &DataUsageCacheInfo) -> Option<(DirectoryProgress, &DataUsageEntry, Option)> { Some((self.progress(info)?, &self.aggregate.as_ref()?.value, self.measured_at)) diff --git a/crates/scanner/src/scanner_folder/directory_cursor/checkpoint/storage_fixture.rs b/crates/scanner/src/scanner_folder/directory_cursor/checkpoint/storage_fixture.rs index 158e2a6e57..c6b2de9ed8 100644 --- a/crates/scanner/src/scanner_folder/directory_cursor/checkpoint/storage_fixture.rs +++ b/crates/scanner/src/scanner_folder/directory_cursor/checkpoint/storage_fixture.rs @@ -121,10 +121,23 @@ async fn display_progress_measurement_metadata_keeps_version_one_checkpoint_comp .expect("same validated receipt provides display usage"); assert!(compacted.extends(&display) && display.extends(&compacted)); assert_eq!(value.objects, 2); + let covered = "bucket/obj-000001/nested"; + assert!(recovered.covers_bound_path(&info, covered)); + assert!(!recovered.covers_bound_path(&info, "bucket/obj-000002/nested")); + assert!(!recovered.covers_bound_path(&info, "bucket-sibling/obj-000001")); + let other_incarnation = DataUsageCacheInfo { + scan_identity: info.scan_identity.map(|identity| DataUsageScanIdentity { + bucket_incarnation: uuid::Uuid::from_u128(2), + ..identity + }), + ..info.clone() + }; + assert!(!recovered.covers_bound_path(&other_incarnation, covered)); let mut damaged = recovered; damaged.digest[0] ^= 1; assert!(matches!(damaged.scheduling_progress(&info), (None, None))); + assert!(!damaged.covers_bound_path(&info, covered)); assert!(damaged.display_usage(&info).is_none()); }