Skip to content
Merged
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
8 changes: 4 additions & 4 deletions pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Comment thread
smaheshwar-pltr marked this conversation as resolved.
Resolved

def _cleanup_uncommitted_manifests(self) -> None:
"""Clean up manifests from failed retry attempts after a successful commit."""
Expand Down
91 changes: 89 additions & 2 deletions tests/table/test_commit_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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

Expand Down Expand Up @@ -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")
Expand Down