timothyw553 commented on code in PR #19024:
URL: https://github.com/apache/iceberg/pull/19024#discussion_r4727363424
##########
core/src/test/java/org/apache/iceberg/rest/TestRESTCatalog.java:
##########
@@ -2462,6 +2464,141 @@ public void
multipleDiffsAgainstMultipleTablesLastFails() {
assertThat(schema2.columns()).hasSize(1);
}
+ @Test
+ void singleTableTransactionAppliesOnTopOfConcurrentChange() {
+ Namespace namespace = Namespace.of("concurrentNamespace");
+ TableIdentifier identifier = TableIdentifier.of(namespace,
"concurrentTable");
+
+ if (requiresNamespaceCreate()) {
+ catalog().createNamespace(namespace);
+ }
+
+ Transaction transaction = catalog().buildTable(identifier,
SCHEMA).create().newTransaction();
+ transaction.updateProperties().set("transaction", "true").commit();
+
+ RESTCatalog racingCatalog =
+ catalogWithConcurrentCommit(
+ identifier, table -> table.updateProperties().set("concurrent",
"true").commit());
+
+ racingCatalog.commitTransaction(
+ TableCommit.create(
+ identifier,
+ ((BaseTransaction) transaction).startMetadata(),
+ ((BaseTransaction) transaction).currentMetadata()));
+
+ assertThat(catalog().loadTable(identifier).properties())
+ .containsEntry("transaction", "true")
+ .containsEntry("concurrent", "true");
+ }
+
+ @Test
+ void singleTableTransactionRevalidatesRequirementsAfterConcurrentChange() {
+ Namespace namespace = Namespace.of("concurrentNamespace");
+ TableIdentifier identifier = TableIdentifier.of(namespace,
"concurrentTable");
+
+ if (requiresNamespaceCreate()) {
+ catalog().createNamespace(namespace);
+ }
+
+ Transaction transaction = catalog().buildTable(identifier,
SCHEMA).create().newTransaction();
+ transaction.updateSchema().addColumn("transaction_col",
Types.LongType.get()).commit();
+
+ RESTCatalog racingCatalog =
+ catalogWithConcurrentCommit(
+ identifier,
+ table ->
+ table.updateSchema().addColumn("concurrent_col",
Types.LongType.get()).commit());
+
+ assertThatThrownBy(
+ () ->
+ racingCatalog.commitTransaction(
+ TableCommit.create(
+ identifier,
+ ((BaseTransaction) transaction).startMetadata(),
+ ((BaseTransaction) transaction).currentMetadata())))
+ .isInstanceOf(CommitFailedException.class)
+ .hasMessageContaining("Requirement failed");
+
+ Schema schema = catalog().loadTable(identifier).schema();
+ assertThat(schema.findField("concurrent_col")).isNotNull();
+ assertThat(schema.findField("transaction_col")).isNull();
+ }
+
+ @Test
+ void multiTableTransactionReportsUnknownStateAfterPartialCommit() {
+ Namespace namespace = Namespace.of("concurrentNamespace");
+ TableIdentifier committedIdentifier = TableIdentifier.of(namespace,
"committedTable");
+ TableIdentifier conflictingIdentifier = TableIdentifier.of(namespace,
"conflictingTable");
+
+ if (requiresNamespaceCreate()) {
+ catalog().createNamespace(namespace);
+ }
+
+ Transaction committedTransaction =
+ catalog().buildTable(committedIdentifier,
SCHEMA).create().newTransaction();
+ committedTransaction.updateProperties().set("transaction",
"true").commit();
+
+ Transaction conflictingTransaction =
+ catalog().buildTable(conflictingIdentifier,
SCHEMA).create().newTransaction();
+ conflictingTransaction
+ .updateSchema()
+ .addColumn("transaction_col", Types.LongType.get())
+ .commit();
+
+ RESTCatalog racingCatalog =
+ catalogWithConcurrentCommit(
+ conflictingIdentifier,
+ table ->
+ table.updateSchema().addColumn("concurrent_col",
Types.LongType.get()).commit());
+
+ assertThatThrownBy(
+ () ->
+ racingCatalog.commitTransaction(
+ TableCommit.create(
+ committedIdentifier,
+ ((BaseTransaction)
committedTransaction).startMetadata(),
+ ((BaseTransaction)
committedTransaction).currentMetadata()),
+ TableCommit.create(
+ conflictingIdentifier,
+ ((BaseTransaction)
conflictingTransaction).startMetadata(),
+ ((BaseTransaction)
conflictingTransaction).currentMetadata())))
+ .isInstanceOf(CommitStateUnknownException.class)
+ .hasMessageContaining(committedIdentifier.toString());
+
+ assertThat(catalog().loadTable(committedIdentifier).properties())
+ .containsEntry("transaction", "true");
+
assertThat(catalog().loadTable(conflictingIdentifier).schema().findField("transaction_col"))
Review Comment:
could we also assert that `concurrent_col` survived here, and add tests for
when a requirements-only table precedes a conflict or when the first table
conflicts? right now removing the `!tableChange.updates().isEmpty()` check
still passes all three tests.
```diff
-
assertThat(catalog().loadTable(conflictingIdentifier).schema().findField("transaction_col"))
- .isNull();
+ Schema schema = catalog().loadTable(conflictingIdentifier).schema();
+ assertThat(schema.findField("concurrent_col")).isNotNull();
+ assertThat(schema.findField("transaction_col")).isNull();
```
##########
core/src/test/java/org/apache/iceberg/rest/RESTCatalogAdapter.java:
##########
@@ -562,28 +563,52 @@ private static Map<String, String> defaultQueryParams() {
* atomicity, which is left to the implementation details of a REST server.
*/
private static void commitTransaction(Catalog catalog,
CommitTransactionRequest request) {
- List<Transaction> transactions = Lists.newArrayList();
+ List<Pair<TableOperations, UpdateTableRequest>> validatedChanges =
Lists.newArrayList();
for (UpdateTableRequest tableChange : request.tableChanges()) {
Table table = catalog.loadTable(tableChange.identifier());
if (table instanceof BaseTable) {
+ TableOperations ops = ((BaseTable) table).operations();
Transaction transaction =
- Transactions.newTransaction(
- tableChange.identifier().toString(), ((BaseTable)
table).operations());
- transactions.add(transaction);
+ Transactions.newTransaction(tableChange.identifier().toString(),
ops);
BaseTransaction.TransactionTable txTable =
(BaseTransaction.TransactionTable) transaction.table();
// this performs validations and makes temporary commits that are
in-memory
CatalogHandlers.commit(txTable.operations(), tableChange);
+ validatedChanges.add(Pair.of(ops, tableChange));
} else {
throw new IllegalStateException("Cannot wrap catalog that does not
produce BaseTable");
}
}
- // only commit if validations passed previously
- transactions.forEach(Transaction::commitTransaction);
+ // only commit if validations passed previously; on a concurrent change,
requirements are
+ // re-checked against the refreshed table before the updates are reapplied
+ List<TableIdentifier> committed = Lists.newArrayList();
+ for (Pair<TableOperations, UpdateTableRequest> change : validatedChanges) {
+ UpdateTableRequest tableChange = change.second();
+ try {
+ CatalogHandlers.commit(change.first(), tableChange);
+ } catch (CommitStateUnknownException e) {
Review Comment:
why do we need this separate `CommitStateUnknownException` catch? if a later
table throws `CommitStateUnknownException`, this skips wrapping it with the
earlier `committed` tables, whereas letting the `RuntimeException` branch
handle it keeps the original exception when `committed` is empty and adds the
committed tables otherwise.
```diff
- } catch (CommitStateUnknownException e) {
- throw e;
} catch (RuntimeException e) {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]