Repository navigation
Docs: Add concurrent writes guide to write documentation #4282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jey1987-cmd
wants to merge
4
commits into
apache:main
Choose a base branch
from
jey1987-cmd:docs-concurrent-writes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+57
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -578,6 +578,63 @@ assert upd.rows_inserted == 1 | |
|
|
||
| PyIceberg will automatically detect which rows need to be updated, inserted or can simply be ignored. | ||
|
|
||
| ### Concurrent writes | ||
|
|
||
| Several writers can commit to the same table at the same time. Iceberg uses optimistic concurrency: each writer prepares its changes against the table state it loaded, and the catalog only accepts the commit if the table has not changed in the meantime. If another writer committed first, the catalog rejects the commit with a `CommitFailedException`. | ||
|
|
||
| What happens next depends on the operation: | ||
|
|
||
| | Operation | When a concurrent commit is detected | | ||
| | ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| | `append` | Retried automatically. Appends are not validated against concurrent changes, so the retry succeeds once the commit lands on the latest table state. | | ||
| | `overwrite`, `delete` | Retried automatically, but first validated against the commits made since the table was loaded. A real conflict raises `ValidationException` and is not retried. | | ||
| | Metadata-only changes | Not retried. Transactions that do not write data, for example a schema update on its own, raise `CommitFailedException` on the first conflict. | | ||
|
|
||
| #### How a retry works | ||
|
|
||
| After a `CommitFailedException`, PyIceberg refreshes the table metadata, rebuilds the snapshot against the latest table state, and tries again with exponential backoff. Once the retries are exhausted, the last `CommitFailedException` is raised. A transaction that failed to commit cannot be reused; create a new one to try again. | ||
|
|
||
| The retry settings are described in [Commit retry options](configuration.md#commit-retry-options). | ||
|
|
||
| #### Isolation levels for `overwrite` and `delete` | ||
|
|
||
| Before retrying an `overwrite` or `delete`, PyIceberg checks the commits made since the table was loaded. Which conflicts count depends on the isolation level: `overwrite` uses [`write.update.isolation-level`](configuration.md#isolation-level-options) and `delete` uses [`write.delete.isolation-level`](configuration.md#isolation-level-options): | ||
|
|
||
| - Under `serializable` (the default), data files added concurrently that match the operation's filter cause a `ValidationException`. | ||
| - Under `snapshot`, concurrent appends are accepted and only conflicting deletes are rejected. | ||
| - Under either level, a concurrent delete of the same data files, or delete files added concurrently, is a conflict. Currently, any delete file committed concurrently counts as a conflict for a filtered `overwrite` or `delete`, even in an unrelated partition. | ||
|
|
||
| #### Handling failures | ||
|
|
||
| ```python | ||
| from pyiceberg.exceptions import CommitFailedException, ValidationException | ||
| from pyiceberg.expressions import EqualTo | ||
|
|
||
| try: | ||
| tbl.overwrite(df, overwrite_filter=EqualTo("city", "Paris")) | ||
| except ValidationException: | ||
| # A concurrent commit conflicts with this overwrite. Reload the table and decide | ||
| # whether the overwrite is still valid before running it again. | ||
| tbl = catalog.load_table("default.cities") | ||
| except CommitFailedException: | ||
| # The table kept changing and the retries were exhausted. Retrying later, or | ||
| # raising `commit.retry.num-retries`, may help. | ||
| tbl = catalog.load_table("default.cities") | ||
|
Comment on lines
+613
to
+622
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both branches only reload the table and then swallow the exception, so if someone copies this as is, the write is silently dropped 🤔 Should we re-raise, or show the overwrite being run again after the reload? |
||
| ``` | ||
|
|
||
| To change the retry behavior for a table, set the properties on it: | ||
|
|
||
| ```python | ||
| with tbl.transaction() as transaction: | ||
| transaction.set_properties({"commit.retry.num-retries": "8"}) | ||
| ``` | ||
|
|
||
| #### Reducing conflicts | ||
|
|
||
| - Commit fewer, larger appends instead of many small ones. Every commit is another chance for writers to collide, and each writer has to refresh the table before retrying. | ||
| - Avoid having several writers overwrite or delete in the same partitions. Appends from many writers are cheap to retry; overwrites are validated and can fail. | ||
| - Use `snapshot` isolation if concurrent appends to the partitions you overwrite are acceptable for your workload. | ||
|
|
||
| ## Inspecting tables | ||
|
|
||
| To explore the table metadata, tables can be inspected. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.