RussellSpitzer commented on code in PR #1285:
URL: https://github.com/apache/polaris/pull/1285#discussion_r2060718281


##########
service/common/src/main/java/org/apache/polaris/service/catalog/iceberg/IcebergCatalogHandler.java:
##########
@@ -762,7 +777,190 @@ public LoadTableResponse updateTable(
     if (isStaticFacade(catalog)) {
       throw new BadRequestException("Cannot update table on static-facade 
external catalogs.");
     }
-    return CatalogHandlers.updateTable(baseCatalog, tableIdentifier, 
applyUpdateFilters(request));
+    // TODO: pending discussion if table property is right way, or a writer 
specific knob is
+    // required.
+    return updateTableWithRollback(baseCatalog, tableIdentifier, 
applyUpdateFilters(request));
+  }
+
+  // TODO: Clean this up when CatalogHandler become extensible.
+  // Copy of CatalogHandler#update
+  private static LoadTableResponse updateTableWithRollback(
+      Catalog catalog, TableIdentifier ident, UpdateTableRequest request) {
+    Schema EMPTY_SCHEMA = new Schema(new Types.NestedField[0]);
+    TableMetadata finalMetadata;
+    if (isCreate(request)) {
+      Transaction transaction =
+          catalog.buildTable(ident, EMPTY_SCHEMA).createOrReplaceTransaction();
+      if (!(transaction instanceof BaseTransaction)) {
+        throw new IllegalStateException(
+            "Cannot wrap catalog that does not produce BaseTransaction");
+      }
+
+      BaseTransaction baseTransaction = (BaseTransaction) transaction;
+      finalMetadata = create(baseTransaction.underlyingOps(), request);
+    } else {
+      Table table = catalog.loadTable(ident);
+      if (!(table instanceof BaseTable)) {
+        throw new IllegalStateException("Cannot wrap catalog that does not 
produce BaseTable");
+      }
+
+      TableOperations ops = ((BaseTable) table).operations();
+      finalMetadata = commit(ops, request);
+    }
+
+    return 
LoadTableResponse.builder().withTableMetadata(finalMetadata).build();
+  }
+
+  // TODO: Clean this up when CatalogHandler become extensible.
+  // Copy of CatalogHandler#create
+  private static TableMetadata create(TableOperations ops, UpdateTableRequest 
request) {
+    request.requirements().forEach((requirement) -> 
requirement.validate(ops.current()));
+    Optional<Integer> formatVersion =
+        request.updates().stream()
+            .filter((update) -> update instanceof 
MetadataUpdate.UpgradeFormatVersion)
+            .map((update) -> ((MetadataUpdate.UpgradeFormatVersion) 
update).formatVersion())
+            .findFirst();
+    TableMetadata.Builder builder =
+        (TableMetadata.Builder)
+            formatVersion
+                .map(TableMetadata::buildFromEmpty)
+                .orElseGet(TableMetadata::buildFromEmpty);
+    request.updates().forEach((update) -> update.applyTo(builder));
+    ops.commit((TableMetadata) null, builder.build());
+    return ops.current();
+  }
+
+  @VisibleForTesting
+  // TODO: Clean this up when CatalogHandler become extensible.
+  // Copy of CatalogHandler#commit
+  public static TableMetadata commit(TableOperations ops, UpdateTableRequest 
request) {
+    AtomicBoolean isRetry = new AtomicBoolean(false);
+
+    try {
+      Tasks.foreach(new TableOperations[] {ops})
+          .retry(4)
+          .exponentialBackoff(100L, 60000L, 1800000L, (double) 2.0F)
+          .onlyRetryOn(CommitFailedException.class)
+          .run(
+              (taskOps) -> {
+                TableMetadata base = isRetry.get() ? taskOps.refresh() : 
taskOps.current();
+                isRetry.set(true);
+                // Prev PR: https://github.com/apache/iceberg/pull/5888
+                boolean rollbackCompaction =
+                    PropertyUtil.propertyAsBoolean(
+                        taskOps.current().properties(), 
ROLLBACK_REPLACE_ENABLED_PROPERTY, false);
+
+                TableMetadata.Builder metadataBuilder = 
TableMetadata.buildFrom(base);
+                TableMetadata newBase = base;
+                try {
+                  request.requirements().forEach((requirement) -> 
requirement.validate(base));
+                } catch (CommitFailedException e) {
+                  if (!rollbackCompaction) {
+                    throw new ValidationFailureException(e);
+                  }
+                  // Since snapshot has already been created at the client end.

Review Comment:
   This comment is a little confusing to me. I'm not sure what exactly we are 
doing here.
   
   I may have missed a step here but I feel like we should have checked that 
this is an AddSnapshot request and not a commit for some other request type. 
For example I don't want us to go down this path just because we had a new 
property added to the table.
   findAddSnapshot



-- 
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]

Reply via email to