Skip to content

feat(table): Implement snapshot expiration - #401

Merged
zeroshade merged 12 commits into
apache:mainfrom
agnosticeng:expire-snapshots
Aug 5, 2025
Merged

zeroshade merged 12 commits into
apache:mainfrom
agnosticeng:expire-snapshots

Conversation

@arnaudbriche

@arnaudbriche arnaudbriche commented Apr 22, 2025 •

Copy link
Copy Markdown
Contributor

Hi @zeroshade,

As discussed on a previous PR, I'm trying to implement snapshots expiration now.
This is not complete, but I wanted to discuss it with you before I further work on it.

If the design looks ok to you, here are a few technical questions regarding snapshot expiration:

  • currently, the snapshots field of the produced metadata file looks ok (expired snapshots are not there), but the snapshot-log field still contains entries for every operation since table creation. Is is expected ? I don't find an appropriate answer in the spec.
  • should we handle expired data and metadata files deletion here ?
  • the produced metadata file does not contains any new snapshot (not snapshot is created for the expire snapshots operation). I cannot find out what the spec says about it. Most probably a new snapshot must be created.

References

Comment thread table/transaction.go Resolved

@zeroshade zeroshade 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.

Looks good! other than a few comments, the only thing missing here is some tests. Can you add some tests please?

@arnaudbriche

arnaudbriche commented May 2, 2025 •

Copy link
Copy Markdown
Contributor Author

Hi @zeroshade

Sure, I'll add tests.
Regarding these 3 points, can you provide guidance/opinions ?

  • currently, the snapshots field of the produced metadata file looks ok (expired snapshots are not there), but the snapshot-log field still contains entries for every operation since table creation. Is is expected ? I don't find an appropriate answer in the spec.
  • should we handle expired data and metadata files deletion here ?
  • the produced metadata file does not contains any new snapshot (not snapshot is created for the expire snapshots operation). I cannot find out what the spec says about it. Most probably a new snapshot must be created.

If we decide to handle file removal as part of ExpireSnapshots, we would have to postpone the actual deletion after the commit is successful. I don't see any easy way to do it.

@zeroshade

Copy link
Copy Markdown
Member

@kevinjqliu @Fokko do either of you know the answers to @arnaudbriche's questions here you both have more experience with the spec side of things than I do as far as what makes sense for those situations.

@arnaudbriche

arnaudbriche commented May 2, 2025 •

Copy link
Copy Markdown
Contributor Author

I just added a test and implemented snapshot-log pruning because it's growing unbounded otherwise.

@arnaudbriche

arnaudbriche commented May 2, 2025 •

Copy link
Copy Markdown
Contributor Author

Regarding file deletion, maybe we can add a new method to the Update interface where we can put some post-commit code.
What do you think @zeroshade ?

Comment thread table/metadata.go Resolved
Comment thread table/transaction.go Resolved
@zeroshade

Copy link
Copy Markdown
Member

Regarding file deletion, maybe we can add a new method to the Update interface where we can put some post-commit code.

It's a good question. If we have the table, then we have the file system interface so that we can remove the files. It might make sense for us to simply have an option to tell it whether or not to delete the files (rather than allow generic post-commit code).

@kevinjqliu

kevinjqliu commented May 9, 2025 •

Copy link
Copy Markdown
Contributor

currently, the snapshots field of the produced metadata file looks ok (expired snapshots are not there), but the snapshot-log field still contains entries for every operation since table creation. Is is expected ? I don't find an appropriate answer in the spec.

the snapshot-log should be cleaned up as part of expiration. From https://iceberg.apache.org/spec/#table-metadata-fields, in the snapshot-log entry of the table.
"""
A list (optional) of timestamp and snapshot ID pairs that encodes changes to the current snapshot for the table. Each time the current-snapshot-id is changed, a new entry should be added with the last-updated-ms and the new current-snapshot-id. When snapshots are expired from the list of valid snapshots, all entries before a snapshot that has expired should be removed.
"""

should we handle expired data and metadata files deletion here ?

From the ExpireSnapshots javadoc, it looks you can optionally remove the data and metadata files, but its set to true by default

the produced metadata file does not contains any new snapshot (not snapshot is created for the expire snapshots operation). I cannot find out what the spec says about it. Most probably a new snapshot must be created.

Also from the java docs for ExpireSnapshots,
"""
This API accumulates snapshot deletions and commits the new list to the table. This API does not allow deleting the current snapshot.

When committing, these changes will be applied to the latest table metadata. Commit conflicts will be resolved by applying the changes to the new latest metadata and reattempting the commit.
"""

A new snapshot should be created reflecting the newest table update, i.e. old snapshots removed

Hope this helps! Happy to help point to any other questions :)

@arnaudbriche arnaudbriche changed the title Basic version of expireSnapshots operation. Implement snapshot expiration May 14, 2025
@arnaudbriche

arnaudbriche commented May 14, 2025 •

Copy link
Copy Markdown
Contributor Author

A new snapshot should be created reflecting the newest table update, i.e. old snapshots removed

Thx for all the answers @kevinjqliu.
I implemented unreachable files deletion and snapshot log pruning as specified in your reply.

I have a hard time figuring out how a new snapshot could be created, I we technically don't write , update or delete any data from the table here.
Manifest would be exactly the same as current snapshot's one.

@arnaudbriche
arnaudbriche requested a review from zeroshade May 18, 2025 18:55
Comment thread table/metadata.go
Comment on lines -190 to +197
b.schemaList = metadata.Schemas()
b.schemaList = slices.Clone(metadata.Schemas())
b.currentSchemaID = metadata.CurrentSchema().ID
b.specs = metadata.PartitionSpecs()
b.specs = slices.Clone(metadata.PartitionSpecs())
b.defaultSpecID = metadata.DefaultPartitionSpec()
b.lastPartitionID = metadata.LastPartitionSpecID()
b.props = metadata.Properties()
b.snapshotList = metadata.Snapshots()
b.sortOrderList = metadata.SortOrders()
b.props = maps.Clone(metadata.Properties())
b.snapshotList = slices.Clone(metadata.Snapshots())
b.sortOrderList = slices.Clone(metadata.SortOrders())

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.

I wonder if there's a way we could lazily do this cloning? Avoid performing the copies until we actually need to?

Comment thread table/transaction.go Resolved
Comment thread table/updates.go Resolved
Comment thread table/updates.go Resolved

@zeroshade zeroshade 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.

This is shaping up nicely! Just a few questions left.

Comment thread internal/coalesce.go Resolved
@zeroshade

Copy link
Copy Markdown
Member

@arnaudbriche any updates?

@arnaudbriche

arnaudbriche commented Jun 26, 2025 •

Copy link
Copy Markdown
Contributor Author

@zeroshade Sorry, I've been 100% focused on something else these past weeks.
I just synced from main and made some of the requested changes.

@zeroshade zeroshade changed the title Implement snapshot expiration feat(table): Implement snapshot expiration Jun 27, 2025
Comment thread table/transaction.go Resolved
Comment thread table/updates.go Resolved
@arnaudbriche
arnaudbriche force-pushed the expire-snapshots branch 2 times, most recently from 313d23e to dfe4601 Compare July 15, 2025 08:39
@arnaudbriche
arnaudbriche requested a review from zeroshade July 15, 2025 08:40
@zeroshade

Copy link
Copy Markdown
Member

@arnaudbriche looks like one of the tests are failing:

=== RUN   TestTableWriting/TestExpireSnapshots
    table_test.go:933: 
        	Error Trace:	/home/runner/work/iceberg-go/iceberg-go/table/table_test.go:933
        	Error:      	Received unexpected error:
        	            	cannot remove main branch's snapshot ref
        	Test:       	TestTableWriting/TestExpireSnapshots

@zeroshade zeroshade 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.

Looks good to me! Thanks for this!

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.

4 participants