Skip to content

feat(transaction): remove statistics file metadata for expired snapshots - #2667

Merged
amogh-jahagirdar merged 5 commits into
apache:mainfrom
dhruvarya-db:feat-expire-snapshots-prune-statistics
Jun 23, 2026
Merged

amogh-jahagirdar merged 5 commits into
apache:mainfrom
dhruvarya-db:feat-expire-snapshots-prune-statistics

Conversation

@dhruvarya-db

@dhruvarya-db dhruvarya-db commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Part of #2145. Extends #2591 (the ExpireSnapshotsAction) and #2664 (the history.expire.* defaults).

What changes are included in this PR?

When ExpireSnapshotsAction expires snapshots, it now also drops the statistics and partition-statistics metadata entries tied to each expired snapshot, mirroring Java RemoveSnapshots (TableMetadata.Builder.removeSnapshots, which calls removeStatistics/removePartitionStatistics for each removed snapshot). For every expired snapshot that has a statistics entry, commit() emits a RemoveStatistics update; likewise RemovePartitionStatistics for 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.

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 viirya left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

@dhruvarya-db

Copy link
Copy Markdown
Contributor Author

Thanks for the review @viirya ! Hi @blackmwk , any chance you could help merge this? The base PR has already been merged.

@laskoviymishka laskoviymishka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 RemoveSnapshotRef updates and the RemoveSnapshots push. 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_stats duplicates the snapshot/ref wiring from table_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,
});
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@dhruvarya-db

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review @laskoviymishka, and @viirya for verifying the design! Pushed 184ea96 addressing all four notes:

  • Update ordering: stats removals now land after the RemoveSnapshots push (refs -> snapshots -> stats).
  • Ref-aging coverage: added test_ref_aging_expiry_drops_statistics, which expires a snapshot purely because its sole ref aged out.
  • Multi-snapshot coverage: added test_multiple_expired_snapshots_drop_their_statistics asserting removed_statistics == [1, 2].
  • Helper dedup: table_with_stats now builds on table_with instead of duplicating the wiring.
  • Also took the for &snapshot_id cleanup.

All 34 transaction::expire_snapshots tests pass; clippy + rustfmt clean.

// 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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 }),
    );
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. Updated

@amogh-jahagirdar amogh-jahagirdar changed the title feat(transaction): prune statistics files for expired snapshots feat(transaction): cleanup statistics files for expired snapshots Jun 23, 2026
@amogh-jahagirdar amogh-jahagirdar changed the title feat(transaction): cleanup statistics files for expired snapshots feat(transaction): remove statistics file metadata for expired snapshots Jun 23, 2026
Comment on lines +325 to +329
// 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, shortened

Use bool::then_some + extend instead of if-is_some-push, and shorten the
block comment per review.
@amogh-jahagirdar

Copy link
Copy Markdown
Contributor

Thanks @dhruvarya-db ! and thank you @laskoviymishka @viirya for reviewing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants