Skip to content

Commit f84f371

Browse files
kotwal-itproclaude
andcommitted
Finalize every transaction before refreshing tables after a multi-table commit
A refresh that failed after a successful commit left the remaining transactions pending and raised as if the commit had failed. Finalize all committed transactions first, then refresh each table and log a refresh failure instead of raising it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011ZBQVsXRmKT8WXDr2qSfbd
1 parent 16c642b commit f84f371

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

‎pyiceberg/catalog/rest/__init__.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,8 @@ def commit_transactions(self, *transactions: Transaction) -> None:
16881688
16891689
Sends the staged changes of every transaction in one request to the
16901690
``transactions/commit`` endpoint: either all of them are applied, or none. Each
1691-
table is refreshed after a successful commit. Unlike ``Transaction.commit_transaction``,
1691+
table is refreshed after a successful commit; a refresh that fails is logged and does
1692+
not fail the commit, which has already been applied. Unlike ``Transaction.commit_transaction``,
16921693
a conflict is not retried: re-stage the changes and commit again.
16931694
16941695
Args:
@@ -1758,6 +1759,18 @@ def commit_transactions(self, *transactions: Transaction) -> None:
17581759

17591760
for transaction in staged:
17601761
transaction._multi_table_commit_succeeded() # pylint: disable=W0212
1762+
# The commit has landed. A failed refresh only leaves a table handle stale, so it is
1763+
# logged instead of raised, which would suggest the commit itself had failed.
1764+
for transaction in staged:
1765+
table = transaction._table # pylint: disable=W0212
1766+
try:
1767+
table.refresh()
1768+
except Exception:
1769+
logger.warning(
1770+
"Committed changes to %s, but refreshing the table failed; call refresh() to load the new metadata",
1771+
".".join(table.name()),
1772+
exc_info=True,
1773+
)
17611774

17621775
@retry(**_RETRY_ARGS)
17631776
def _post_commit_transaction(self, request: CommitTransactionRequest) -> None:

‎pyiceberg/table/__init__.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,12 +1320,15 @@ def _changes_for_multi_table_commit(self) -> tuple[tuple[TableRequirement, ...],
13201320
return self._requirements, self._updates
13211321

13221322
def _multi_table_commit_succeeded(self) -> None:
1323-
"""Clean up after a multi-table commit that included this transaction succeeded."""
1323+
"""Clean up after a multi-table commit that included this transaction succeeded.
1324+
1325+
The table is not refreshed here: the catalog refreshes it after every transaction
1326+
of the commit is finalized, so a refresh failure cannot leave one pending.
1327+
"""
13241328
self._cleanup_uncommitted_manifests()
13251329
self._snapshot_producers = []
13261330
self._updates = ()
13271331
self._requirements = ()
1328-
self._table.refresh()
13291332

13301333
def _multi_table_commit_failed(self, outcome_known: bool) -> None:
13311334
"""Handle a multi-table commit that included this transaction and did not succeed.

‎tests/catalog/test_rest.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from __future__ import annotations
1919

2020
import base64
21+
import logging
2122
import os
2223
import pickle
2324
import platform
@@ -4169,6 +4170,38 @@ def test_commit_transactions_sends_one_request(
41694170
assert orders_tx._updates == () and items_tx._updates == ()
41704171

41714172

4173+
def test_commit_transactions_finalizes_every_transaction_when_a_refresh_fails(
4174+
rest_mock: Mocker, example_table_metadata_with_snapshot_v1_rest_json: dict[str, Any], caplog: pytest.LogCaptureFixture
4175+
) -> None:
4176+
catalog, (orders, items) = _load_tables_for_multi_table_commit(rest_mock, example_table_metadata_with_snapshot_v1_rest_json)
4177+
commit = rest_mock.post(f"{TEST_URI}v1/transactions/commit", status_code=204, request_headers=TEST_HEADERS)
4178+
orders_reload = rest_mock.get(
4179+
f"{TEST_URI}v1/namespaces/sales/tables/orders",
4180+
json={"error": {"message": "unavailable", "type": "ServiceUnavailableException", "code": 503}},
4181+
status_code=503,
4182+
request_headers=TEST_HEADERS,
4183+
)
4184+
items_reload = rest_mock.get(
4185+
f"{TEST_URI}v1/namespaces/sales/tables/items",
4186+
json=example_table_metadata_with_snapshot_v1_rest_json,
4187+
status_code=200,
4188+
request_headers=TEST_HEADERS,
4189+
)
4190+
4191+
orders_tx = orders.transaction().set_properties(owner="sales")
4192+
items_tx = items.transaction().set_properties(owner="sales")
4193+
with caplog.at_level(logging.WARNING, logger="pyiceberg.catalog.rest"):
4194+
# The commit was applied, so a failed refresh does not raise.
4195+
catalog.commit_transactions(orders_tx, items_tx)
4196+
4197+
assert commit.call_count == 1
4198+
# Both transactions are finalized, and the other table is still refreshed.
4199+
assert orders_tx._updates == () and items_tx._updates == ()
4200+
assert not orders_tx._failed and not items_tx._failed
4201+
assert orders_reload.called and items_reload.called
4202+
assert "refreshing the table failed" in caplog.text
4203+
4204+
41724205
def test_catalog_transaction_commits_on_exit(
41734206
rest_mock: Mocker, example_table_metadata_with_snapshot_v1_rest_json: dict[str, Any]
41744207
) -> None:

0 commit comments

Comments
 (0)