Repository navigation
feat(transaction): remove statistics file metadata for expired snapshots - #2667
amogh-jahagirdar merged 5 commits into
Conversation
When ExpireSnapshotsAction expires snapshots, also drop the statistics and partition-statistics metadata entries tied to each expired snapshot, mirroring Java RemoveSnapshots.removeSnapshots. This is metadata-only: the puffin files those entries point at are deleted by the higher-level file-cleanup operation.
viirya
left a comment
There was a problem hiding this comment.
Verified the design holds up. The key subtlety: commit() builds the updates vec and hands it straight to ActionCommit::new(...), so these RemoveStatistics / RemovePartitionStatistics updates do not go through TableMetadataBuilder::remove_statistics (which is itself only-when-present, gated on if previous.is_some()). That means the explicit statistics_for_snapshot(...).is_some() / partition_statistics_for_snapshot(...).is_some() guards here are necessary, not redundant — without them, expiring a snapshot with no statistics would emit a spurious RemoveStatistics. This matches Java RemoveSnapshots, which records a removal only when an entry exists.
test_only_present_statistics_variant_is_removed pins exactly that invariant (snapshot with statistics but no partition-statistics → only RemoveStatistics emitted), and the three tests together cover present-both / present-neither / present-one. Metadata-only scope (puffin file deletion left to file-cleanup) is the right boundary and mirrors Java's FileCleanupStrategy split.
Pulled the branch and ran it: all 32 transaction::expire_snapshots tests pass, clippy + rustfmt clean.
LGTM. (Note this stacks on #2591, which should land first.)
…ts-prune-statistics
…ts-prune-statistics
laskoviymishka
left a comment
There was a problem hiding this comment.
This is good housekeeping, expiring a snapshot and leaving its statistics pointer behind is exactly the kind of dangling metadata worth cleaning up, and guarding each push on statistics_for_snapshot / partition_statistics_for_snapshot being present is the right move given the retry path re-runs commit() against refreshed metadata. I checked the parity note against TableMetadata.Builder.removeSnapshots in apache/iceberg and it's accurate, so the comment stands as written.
The implementation is correct and the tests are solid, so this is good to land as-is. The notes below are things I'd be just as happy to see in a follow-up or if you wish inside this PR:
- update ordering: the stats removals land between the
RemoveSnapshotRefupdates and theRemoveSnapshotspush. It's correct since these touch disjoint maps, but refs → snapshots → stats reads more naturally and is marginally safer for a strict server validating updates in sequence. - the three new tests all expire a single snapshot through the retain/expire-older path; the ref-aging expiry path reaches the same loop through a different
plan()branch and is worth a case, along with a multi-snapshot assertion (removed_statistics == vec![1, 2]). table_with_statsduplicates the snapshot/ref wiring fromtable_with; sharing it keeps the two from drifting.
None of this blocks — happy to see it land and pick these up in a follow-up.
| snapshot_id: *snapshot_id, | ||
| }); | ||
| } | ||
| } |
There was a problem hiding this comment.
We end up emitting the stats removals between the RemoveSnapshotRef updates and the RemoveSnapshots push, so the sequence reads [RemoveSnapshotRef*, RemoveStatistics*, RemovePartitionStatistics*, RemoveSnapshots]. It's correct since these touch disjoint maps, but I'd find it cleaner to read as refs → snapshots → stats, and it's a touch safer against a strict server that validates updates in sequence.
Moving this loop to after the RemoveSnapshots push gets that ordering for free — fine as a follow-up. wdyt?
There was a problem hiding this comment.
Done in 184ea96 — the stats removals are now appended after the RemoveSnapshots push, so the sequence reads refs -> snapshots -> stats. Built the stats updates into a temp vec first (to read the ids before they're moved into RemoveSnapshots) then extend afterwards, so no clone.
| // `RemoveSnapshots.removeSnapshots`. This only rewrites metadata; the puffin files those | ||
| // entries point at are deleted by the higher-level file-cleanup maintenance operation. | ||
| for snapshot_id in &plan.ids_to_remove { | ||
| if metadata.statistics_for_snapshot(*snapshot_id).is_some() { |
There was a problem hiding this comment.
The three new tests all expire exactly one snapshot via retain_last/expire_older_than_ms. Two cases I'd like to see (follow-up is fine): one where a snapshot is expired purely because its sole ref aged out (extend test_ref_aging_drops_old_tag_and_expires_its_snapshot with stats_file(1) and assert removed_statistics == vec![1]), and a 3-snapshot chain where two expire carrying stats, asserting removed_statistics == vec![1, 2].
The ref-aging path is the one I most want covered — it reaches this loop through a different plan() branch than the retain/expire-older cases.
There was a problem hiding this comment.
Added both in 184ea96: test_ref_aging_expiry_drops_statistics (snapshot 1 kept alive only by an aged-out tag, asserting removed_statistics == [1] — reaches the loop via the ref-aging plan() branch) and test_multiple_expired_snapshots_drop_their_statistics (1 -> 2 -> 3 chain, two expire, asserting removed_statistics == [1, 2]).
| } | ||
|
|
||
| /// Like [`table_with`], but also attaches statistics and partition-statistics files. | ||
| fn table_with_stats( |
There was a problem hiding this comment.
This duplicates the snapshot/ref wiring from table_with verbatim. I'd build on top of table_with (or share the snapshot/ref loop) and just layer the stats on after, so the two don't silently diverge if that wiring changes later.
There was a problem hiding this comment.
Done in 184ea96 — table_with_stats now calls table_with for the snapshot/ref wiring and just layers stats on top, so the two can't drift.
| // Drop the statistics metadata tied to each expired snapshot, mirroring Java | ||
| // `RemoveSnapshots.removeSnapshots`. This only rewrites metadata; the puffin files those | ||
| // entries point at are deleted by the higher-level file-cleanup maintenance operation. | ||
| for snapshot_id in &plan.ids_to_remove { |
There was a problem hiding this comment.
Minor, but binding for &snapshot_id in &plan.ids_to_remove drops the repeated *snapshot_id derefs at the use sites and reads a bit cleaner. clippy may nudge on this too.
There was a problem hiding this comment.
Done in 184ea96 — switched to for &snapshot_id in &plan.ids_to_remove, dropping the *snapshot_id derefs.
…, dedup helper - Emit RemoveStatistics/RemovePartitionStatistics after the RemoveSnapshots push so updates read refs -> snapshots -> stats. - Add a ref-aging expiry test (reaches the stats loop via a different plan() branch) and a multi-snapshot test asserting removed_statistics == [1, 2]. - Build table_with_stats on top of table_with so the snapshot/ref wiring can't drift.
|
Thanks for the careful review @laskoviymishka, and @viirya for verifying the design! Pushed 184ea96 addressing all four notes:
All 34 |
| // Drop the statistics metadata tied to each expired snapshot, mirroring Java | ||
| // `RemoveSnapshots.removeSnapshots`. This only rewrites metadata; the puffin files those | ||
| // entries point at are deleted by the higher-level file-cleanup maintenance operation. | ||
| for snapshot_id in &plan.ids_to_remove { |
There was a problem hiding this comment.
Minor: I think the following is a more idiomatic way:
for &snapshot_id in &plan.ids_to_remove {
updates.extend(
metadata
.statistics_for_snapshot(snapshot_id)
.is_some()
.then_some(TableUpdate::RemoveStatistics { snapshot_id }),
);
updates.extend(
metadata
.partition_statistics_for_snapshot(snapshot_id)
.is_some()
.then_some(TableUpdate::RemovePartitionStatistics { snapshot_id }),
);
}
There was a problem hiding this comment.
Thanks for the suggestion. Updated
| // Drop the statistics metadata tied to each expired snapshot, mirroring Java | ||
| // `RemoveSnapshots.removeSnapshots`. This only rewrites metadata; the puffin files those | ||
| // entries point at are deleted by the higher-level file-cleanup maintenance operation. | ||
| // Built before the `RemoveSnapshots` push so the ids can be read without cloning, but | ||
| // appended after it to keep the update order refs -> snapshots -> stats. |
There was a problem hiding this comment.
We should try and keep comments less dense imo,
// Drop statistics metadata for expired snapshots
// This only updates metadata; puffin files are cleaned up separately.
We don't need to comment the ordering details
There was a problem hiding this comment.
Makes sense, shortened
Use bool::then_some + extend instead of if-is_some-push, and shorten the block comment per review.
|
Thanks @dhruvarya-db ! and thank you @laskoviymishka @viirya for reviewing |
Which issue does this PR close?
Part of #2145. Extends #2591 (the
ExpireSnapshotsAction) and #2664 (thehistory.expire.*defaults).What changes are included in this PR?
When
ExpireSnapshotsActionexpires snapshots, it now also drops the statistics and partition-statistics metadata entries tied to each expired snapshot, mirroring JavaRemoveSnapshots(TableMetadata.Builder.removeSnapshots, which callsremoveStatistics/removePartitionStatisticsfor each removed snapshot). For every expired snapshot that has a statistics entry,commit()emits aRemoveStatisticsupdate; likewiseRemovePartitionStatisticsfor a partition-statistics entry. Only entries that actually exist produce an update, matching Java's behavior of recording a change only when an entry is present.This is metadata-only, physical deletion of the puffin files those entries point at is part of the file-cleanup maintenance operation (mirroring Java's
FileCleanupStrategy), not this PR.Are these changes tested?
Yes. New unit tests cover: an expired snapshot's statistics and partition-statistics entries are removed while a retained snapshot keeps its own; an expired snapshot with no statistics emits no removal; and only the statistics variant that is actually present is removed.