From f6f4a012b84c96f67dc627505267b79e3f13bf59 Mon Sep 17 00:00:00 2001 From: Sreesh Maheshwar Date: Sun, 22 Nov 2026 02:55:56 +0000 Subject: [PATCH] Detect a landed commit after another writer expires one of its snapshots A transaction that adds several snapshots counted a failed commit as landed only if all of them were in the refreshed table. No ref points at the earlier snapshots once the last one is committed on top, so another writer can expire them before the check. The transaction then deleted the files of the snapshot that landed, or committed it again and failed. A commit adds all of its snapshots or none, so check for any of them. Co-Authored-By: Claude Opus 5.5 --- pyiceberg/table/__init__.py | 8 +-- tests/table/test_commit_retry.py | 91 +++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 6 deletions(-) diff --git a/pyiceberg/table/__init__.py b/pyiceberg/table/__init__.py index 1c6d4c3a33..ac015dbce6 100644 --- a/pyiceberg/table/__init__.py +++ b/pyiceberg/table/__init__.py @@ -1276,7 +1276,7 @@ def commit_transaction(self) -> Table: return self._table def _attempt_landed(self) -> bool: - """Refresh the table and return whether the snapshots added by this transaction are present. + """Refresh the table and return whether a snapshot added by this transaction is present. Raises: CommitStateUnknownException: If the table cannot be refreshed, the outcome is unknown and @@ -1289,10 +1289,10 @@ def _attempt_landed(self) -> bool: f"Commit failed and the table could not be refreshed to check whether it landed: {e}" ) from e + # A commit adds all of its snapshots or none, so finding one is enough. Another writer may have + # expired the others, since no ref points at a snapshot once another is committed on top of it. added_snapshot_ids = {update.snapshot.snapshot_id for update in self._updates if isinstance(update, AddSnapshotUpdate)} - return bool(added_snapshot_ids) and all( - self._table.metadata.snapshot_by_id(snapshot_id) is not None for snapshot_id in added_snapshot_ids - ) + return any(self._table.metadata.snapshot_by_id(snapshot_id) is not None for snapshot_id in added_snapshot_ids) def _cleanup_uncommitted_manifests(self) -> None: """Clean up manifests from failed retry attempts after a successful commit.""" diff --git a/tests/table/test_commit_retry.py b/tests/table/test_commit_retry.py index 22f743c1c8..1ac5fa18ed 100644 --- a/tests/table/test_commit_retry.py +++ b/tests/table/test_commit_retry.py @@ -15,6 +15,7 @@ # specific language governing permissions and limitations # under the License. import uuid +from collections.abc import Callable from pathlib import Path from typing import Any from unittest.mock import patch @@ -1579,8 +1580,11 @@ def test_negative_wait_properties_do_not_mask_commit_failure(catalog: Catalog) - assert len(result) == 6 -def _commit_outcomes(catalog: Catalog, *outcomes: str) -> Any: - """Simulate a commit failing before ("conflict") or after ("lost") the catalog applied it.""" +def _commit_outcomes(catalog: Catalog, *outcomes: str, after_lost: Callable[[], None] | None = None) -> Any: + """Simulate a commit failing before ("conflict") or after ("lost") the catalog applied it. + + ``after_lost`` runs between a lost commit and its response, for example to make a concurrent commit. + """ real_commit = catalog.commit_table remaining = iter(outcomes) @@ -1590,6 +1594,8 @@ def commit(*args: Any, **kwargs: Any) -> Any: raise CommitFailedException("concurrent update") result = real_commit(*args, **kwargs) if outcome == "lost": + if after_lost is not None: + after_lost() raise CommitFailedException("response lost after the commit landed") return result @@ -1669,6 +1675,87 @@ def test_lost_response_on_empty_table_overwrite_before_any_retry_keeps_the_lande _assert_single_readable_snapshot(catalog, "default.lost_response_empty_overwrite_no_retries", [{"x": 1}, {"x": 2}, {"x": 3}]) +def test_lost_response_after_another_writer_expires_the_first_snapshot_keeps_the_landed_commit(catalog: Catalog) -> None: + """A landed commit is detected even if another writer expired one of its snapshots before the check.""" + import pyarrow as pa + + catalog.create_namespace("default") + identifier = "default.lost_response_first_snapshot_expired" + table = catalog.create_table(identifier, schema=_test_schema(), properties={TableProperties.COMMIT_NUM_RETRIES: "0"}) + + # Two appends add two snapshots, and once both are committed no ref points at the first + txn = table.transaction() + txn.append(pa.table({"x": [1]})) + first_snapshot_id = txn.table_metadata.current_snapshot_id + assert first_snapshot_id is not None + txn.append(pa.table({"x": [2]})) + + def expire_first_snapshot() -> None: + catalog.load_table(identifier).maintenance.expire_snapshots().by_id(first_snapshot_id).commit() + + with _commit_outcomes(catalog, "lost", after_lost=expire_first_snapshot): + txn.commit_transaction() + + assert sorted(catalog.load_table(identifier).scan().to_arrow()["x"].to_pylist()) == [1, 2] + + +def test_lost_response_after_another_writer_expires_the_first_snapshot_is_not_committed_twice(catalog: Catalog) -> None: + """With retries left, a landed commit whose first snapshot was expired is not committed again.""" + import pyarrow as pa + + catalog.create_namespace("default") + identifier = "default.lost_response_first_snapshot_expired_with_retries" + table = catalog.create_table( + identifier, + schema=_test_schema(), + properties={ + TableProperties.COMMIT_NUM_RETRIES: "1", + TableProperties.COMMIT_MIN_RETRY_WAIT_MS: "1", + TableProperties.COMMIT_MAX_RETRY_WAIT_MS: "2", + }, + ) + + txn = table.transaction() + txn.append(pa.table({"x": [1]})) + first_snapshot_id = txn.table_metadata.current_snapshot_id + assert first_snapshot_id is not None + txn.append(pa.table({"x": [2]})) + + def expire_first_snapshot() -> None: + catalog.load_table(identifier).maintenance.expire_snapshots().by_id(first_snapshot_id).commit() + + with _commit_outcomes(catalog, "lost", after_lost=expire_first_snapshot): + txn.commit_transaction() + + assert sorted(catalog.load_table(identifier).scan().to_arrow()["x"].to_pylist()) == [1, 2] + + +def test_lost_response_after_another_writer_expires_the_last_snapshot_keeps_the_landed_commit(catalog: Catalog) -> None: + """A landed commit is detected when only its first snapshot is left, after another writer commits on top.""" + import pyarrow as pa + + catalog.create_namespace("default") + identifier = "default.lost_response_last_snapshot_expired" + table = catalog.create_table(identifier, schema=_test_schema(), properties={TableProperties.COMMIT_NUM_RETRIES: "0"}) + + txn = table.transaction() + txn.append(pa.table({"x": [1]})) + txn.append(pa.table({"x": [2]})) + last_snapshot_id = txn.table_metadata.current_snapshot_id + assert last_snapshot_id is not None + + # Once another writer commits on top, no ref points at the transaction's last snapshot either + def append_on_top_and_expire_last_snapshot() -> None: + other = catalog.load_table(identifier) + other.append(pa.table({"x": [9]})) + other.maintenance.expire_snapshots().by_id(last_snapshot_id).commit() + + with _commit_outcomes(catalog, "lost", after_lost=append_on_top_and_expire_last_snapshot): + txn.commit_transaction() + + assert sorted(catalog.load_table(identifier).scan().to_arrow()["x"].to_pylist()) == [1, 2, 9] + + def test_unreadable_commit_outcome_raises_commit_state_unknown(catalog: Catalog) -> None: """When the table cannot be refreshed, the outcome is unknown and no file is deleted.""" catalog.create_namespace("default")