Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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."""
Expand Down
103 changes: 103 additions & 0 deletions tests/table/test_commit_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down