joyhaldar commented on code in PR #15308:
URL: https://github.com/apache/iceberg/pull/15308#discussion_r2815268662


##########
bigquery/src/main/java/org/apache/iceberg/gcp/bigquery/BigQueryTableOperations.java:
##########
@@ -82,48 +84,49 @@ public void doRefresh() {
   // atomically
   @Override
   public void doCommit(TableMetadata base, TableMetadata metadata) {
-    String newMetadataLocation =
-        base == null && metadata.metadataFileLocation() != null
-            ? metadata.metadataFileLocation()
-            : writeNewMetadata(metadata, currentVersion() + 1);
-    BaseMetastoreOperations.CommitStatus commitStatus =
-        BaseMetastoreOperations.CommitStatus.FAILURE;
+    CommitStatus commitStatus = CommitStatus.FAILURE;
+    RetryDetector retryDetector = new RetryDetector();
+
+    String newMetadataLocation = null;
     try {
-      if (base == null) {
-        createTable(newMetadataLocation, metadata);
+      boolean newTable = base == null;
+      newMetadataLocation = writeNewMetadataIfRequired(newTable, metadata);
+
+      if (newTable) {
+        createTable(newMetadataLocation, metadata, retryDetector);
       } else {
-        updateTable(newMetadataLocation, metadata);
+        updateTable(newMetadataLocation, metadata, retryDetector);
       }
-      commitStatus = BaseMetastoreOperations.CommitStatus.SUCCESS;
-    } catch (CommitFailedException | CommitStateUnknownException e) {
+      commitStatus = CommitStatus.SUCCESS;
+    } catch (CommitFailedException e) {
       throw e;
     } catch (Throwable e) {
       LOG.error("Exception thrown on commit: ", e);
-      if (e instanceof AlreadyExistsException) {
-        throw e;
+      boolean isAlreadyExistsException = e instanceof AlreadyExistsException;
+      boolean isRuntimeIOException = e instanceof RuntimeIOException;
+
+      // If retries occurred, an earlier attempt may have succeeded. If we got 
a
+      // RuntimeIOException, we have no way of knowing if the request reached 
the server.
+      // In either case, check whether the commit actually succeeded.
+      if (isRuntimeIOException || retryDetector.retried()) {
+        LOG.warn(
+            "Received unexpected failure when committing to {}, validating if 
commit ended up succeeding.",
+            tableName(),
+            e);
+        commitStatus = checkCommitStatus(newMetadataLocation, metadata);
       }
-      commitStatus =
-          BaseMetastoreOperations.CommitStatus.valueOf(
-              checkCommitStatus(newMetadataLocation, metadata).name());
-      if (commitStatus == BaseMetastoreOperations.CommitStatus.FAILURE) {
+
+      if (commitStatus == CommitStatus.FAILURE) {
+        if (isAlreadyExistsException) {
+          throw new AlreadyExistsException(e, "Table already exists: %s", 
tableName());
+        }
         throw new CommitFailedException(e, "Failed to commit");
       }
-      if (commitStatus == BaseMetastoreOperations.CommitStatus.UNKNOWN) {
+      if (commitStatus == CommitStatus.UNKNOWN) {
         throw new CommitStateUnknownException(e);
       }
     } finally {
-      try {
-        if (commitStatus == BaseMetastoreOperations.CommitStatus.FAILURE) {
-          LOG.warn("Failed to commit updates to table {}", tableName());
-          io().deleteFile(newMetadataLocation);
-        }
-      } catch (RuntimeException e) {
-        LOG.error(
-            "Failed to cleanup metadata file at {} for table {}",
-            newMetadataLocation,
-            tableName(),
-            e);
-      }
+      cleanupMetadata(commitStatus, newMetadataLocation);

Review Comment:
   Extracted cleanup logic into a separate method `cleanupMetadata`.



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

Reply via email to