From 9510ef6fe7803ba8034684c42789f926aea0c445 Mon Sep 17 00:00:00 2001 From: sikadisamadeisi <1436450784abc@gmail.com> Date: Tue, 1 Dec 2026 16:29:12 +0800 Subject: [PATCH] Detect landed commits via written manifests when snapshots were expired If another writer commits on top of a landed commit whose response was lost and expires all of its snapshots before _attempt_landed() runs, snapshot ID lookup misses it. Check newly added snapshots in the refreshed metadata for the attempt's written manifests so cleanup keeps live manifests and retries do not commit the data a second time. --- pyiceberg/table/__init__.py | 34 +++++++++- tests/table/test_commit_retry.py | 103 +++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 3 deletions(-) diff --git a/pyiceberg/table/__init__.py b/pyiceberg/table/__init__.py index 28a76fc3bb..67ab5f8625 100644 --- a/pyiceberg/table/__init__.py +++ b/pyiceberg/table/__init__.py @@ -1279,9 +1279,10 @@ def _attempt_landed(self) -> bool: """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 - the files written for the attempt must not be deleted. + CommitStateUnknownException: If the table or a newly added snapshot's manifest list cannot be + read, the outcome is unknown and the files written for the attempt must not be deleted. """ + known_snapshot_ids = {snapshot.snapshot_id for snapshot in self._table.metadata.snapshots} try: self._table.refresh() except Exception as e: @@ -1292,7 +1293,34 @@ def _attempt_landed(self) -> bool: # 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 any(self._table.metadata.snapshot_by_id(snapshot_id) is not None for snapshot_id in added_snapshot_ids) + if any(self._table.metadata.snapshot_by_id(snapshot_id) is not None for snapshot_id in added_snapshot_ids): + return True + + # Another writer may have committed on top and expired all of the attempt's snapshots, while still + # referencing the manifests written for the attempt. + written_manifests = {path for producer in self._snapshot_producers for path in producer._written_manifests} + if not written_manifests: + return False + + read_error: Exception | None = None + for snapshot in reversed(self._table.metadata.snapshots): + if snapshot.snapshot_id in known_snapshot_ids: + continue + try: + manifests = snapshot.manifests(self._table.io, self._table.encryption_manager) + except Exception as e: + if read_error is None: + read_error = e + continue + if any(manifest.manifest_path in written_manifests for manifest in manifests): + return True + + if read_error is not None: + raise CommitStateUnknownException( + f"Commit failed and a refreshed snapshot manifest list could not be read to check whether it landed: {read_error}" + ) from read_error + + return False 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 1ac5fa18ed..41b8ad0b0a 100644 --- a/tests/table/test_commit_retry.py +++ b/tests/table/test_commit_retry.py @@ -1756,6 +1756,109 @@ def append_on_top_and_expire_last_snapshot() -> None: assert sorted(catalog.load_table(identifier).scan().to_arrow()["x"].to_pylist()) == [1, 2, 9] +def test_lost_response_after_another_writer_expires_all_added_snapshots_keeps_the_landed_commit(catalog: Catalog) -> None: + """A landed commit is detected through its manifests even when all of its snapshots were expired.""" + import pyarrow as pa + + catalog.create_namespace("default") + identifier = "default.lost_response_all_snapshots_expired" + table = catalog.create_table(identifier, schema=_test_schema(), properties={TableProperties.COMMIT_NUM_RETRIES: "0"}) + + def append_on_top_and_expire() -> None: + other = catalog.load_table(identifier) + other.append(pa.table({"x": [9]})) + other.maintenance.expire_snapshots().by_id(other.snapshots()[0].snapshot_id).commit() + + with _commit_outcomes(catalog, "lost", after_lost=append_on_top_and_expire): + table.append(pa.table({"x": [1]})) + + assert sorted(catalog.load_table(identifier).scan().to_arrow()["x"].to_pylist()) == [1, 9] + + +def test_lost_response_after_another_writer_expires_all_added_snapshots_is_not_committed_twice(catalog: Catalog) -> None: + """With retries left, a landed commit whose snapshots were all expired is not committed again.""" + import pyarrow as pa + + catalog.create_namespace("default") + identifier = "default.lost_response_all_snapshots_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", + }, + ) + + def append_on_top_and_expire() -> None: + other = catalog.load_table(identifier) + other.append(pa.table({"x": [9]})) + other.maintenance.expire_snapshots().by_id(other.snapshots()[0].snapshot_id).commit() + + with _commit_outcomes(catalog, "lost", after_lost=append_on_top_and_expire): + table.append(pa.table({"x": [1]})) + + assert sorted(catalog.load_table(identifier).scan().to_arrow()["x"].to_pylist()) == [1, 9] + + +def test_lost_response_on_retry_after_expiring_all_added_snapshots_cleans_only_failed_attempt(catalog: Catalog) -> None: + """A retried commit that lands and has its snapshot expired keeps its manifest while cleaning earlier failed attempts.""" + import pyarrow as pa + + catalog.create_namespace("default") + identifier = "default.lost_response_retry_all_snapshots_expired" + table = catalog.create_table( + identifier, + schema=_test_schema(), + properties={ + TableProperties.COMMIT_NUM_RETRIES: "2", + TableProperties.COMMIT_MIN_RETRY_WAIT_MS: "1", + TableProperties.COMMIT_MAX_RETRY_WAIT_MS: "2", + }, + ) + + txn = table.transaction() + txn.append(pa.table({"x": [1]})) + first_attempt_manifests = list(txn._snapshot_producers[0]._written_manifests) + assert len(first_attempt_manifests) == 1 + + def append_on_top_and_expire() -> None: + other = catalog.load_table(identifier) + other.append(pa.table({"x": [9]})) + other.maintenance.expire_snapshots().by_id(other.snapshots()[0].snapshot_id).commit() + + with _commit_outcomes(catalog, "conflict", "lost", after_lost=append_on_top_and_expire): + txn.commit_transaction() + + assert not table.io.new_input(first_attempt_manifests[0]).exists() + assert sorted(catalog.load_table(identifier).scan().to_arrow()["x"].to_pylist()) == [1, 9] + + +def test_unreadable_manifest_list_when_checking_landed_commit_raises_commit_state_unknown(catalog: Catalog) -> None: + """When a refreshed snapshot's manifest list cannot be read, the outcome is unknown and no file is deleted.""" + import pyarrow as pa + + catalog.create_namespace("default") + identifier = "default.unknown_commit_outcome_manifest_list" + table = catalog.create_table(identifier, schema=_test_schema(), properties={TableProperties.COMMIT_NUM_RETRIES: "0"}) + + txn = table.transaction() + txn.append(pa.table({"x": [1]})) + written_manifest = txn._snapshot_producers[0]._written_manifests[0] + + other = catalog.load_table(identifier) + other.append(pa.table({"x": [9]})) + other_snapshot = other.current_snapshot() + assert other_snapshot is not None + table.io.delete(other_snapshot.manifest_list) + + with pytest.raises(CommitStateUnknownException): + txn.commit_transaction() + + assert table.io.new_input(written_manifest).exists() + + 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")