rdblue commented on a change in pull request #1587:
URL: https://github.com/apache/iceberg/pull/1587#discussion_r526538697



##########
File path: 
nessie/src/main/java/org/apache/iceberg/nessie/NessieTableOperations.java
##########
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iceberg.nessie;
+
+import com.dremio.nessie.client.NessieClient;
+import com.dremio.nessie.error.NessieConflictException;
+import com.dremio.nessie.error.NessieNotFoundException;
+import com.dremio.nessie.model.Contents;
+import com.dremio.nessie.model.ContentsKey;
+import com.dremio.nessie.model.IcebergTable;
+import com.dremio.nessie.model.ImmutableIcebergTable;
+import java.util.Map;
+import org.apache.iceberg.BaseMetastoreTableOperations;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.exceptions.CommitFailedException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.io.FileIO;
+
+/**
+ * Nessie implementation of Iceberg TableOperations.
+ */
+public class NessieTableOperations extends BaseMetastoreTableOperations {
+
+  private final NessieClient client;
+  private final ContentsKey key;
+  private UpdateableReference reference;
+  private IcebergTable table;
+  private FileIO fileIO;
+
+  /**
+   * Create a nessie table operations given a table identifier.
+   */
+  public NessieTableOperations(
+      ContentsKey key,
+      UpdateableReference reference,
+      NessieClient client,
+      FileIO fileIO) {
+    this.key = key;
+    this.reference = reference;
+    this.client = client;
+    this.fileIO = fileIO;
+  }
+
+  @Override
+  protected void doRefresh() {
+    try {
+      reference.refresh();
+    } catch (NessieNotFoundException e) {
+      throw new RuntimeException("Failed to refresh as ref is no longer 
valid.", e);
+    }
+    String metadataLocation = null;
+    try {
+      Contents contents = client.getContentsApi().getContents(key, 
reference.getHash());
+      this.table = contents.unwrap(IcebergTable.class)
+          .orElseThrow(() ->
+              new IllegalStateException("Cannot refresh iceberg table: " +
+                  String.format("Nessie points to a non-Iceberg object for 
path: %s.", key)));
+      metadataLocation = table.getMetadataLocation();
+    } catch (NessieNotFoundException ex) {
+      if (currentMetadataLocation() != null) {
+        throw new NoSuchTableException(ex, "No such table %s", key);
+      }
+    }
+    refreshFromMetadataLocation(metadataLocation, 2);
+  }
+
+  @Override
+  protected void doCommit(TableMetadata base, TableMetadata metadata) {
+    reference.checkMutable();
+
+    String newMetadataLocation = writeNewMetadata(metadata, currentVersion() + 
1);
+
+    boolean threw = true;
+    try {
+      IcebergTable newTable = 
ImmutableIcebergTable.builder().metadataLocation(newMetadataLocation).build();
+      client.getContentsApi().setContents(key,
+                                          reference.getAsBranch().getName(),
+                                          reference.getHash(),
+                                          String.format("iceberg commit%s", 
applicationId()),
+                                          newTable);
+      threw = false;
+    } catch (NessieConflictException ex) {
+      String fixMsg = reference.isBranch() ?
+          String.format("Update the reference %s and try again", 
reference.getName()) :
+          String.format("Can't commit to the tag %s", reference.getName());
+      throw new CommitFailedException(ex, "Commit failed: Reference hash is 
out of date. %s", fixMsg);

Review comment:
       `CommitFailedException` is used to trigger a table refresh and a retry. 
Throwing it for `NessieConflictException` seems correct to me, but reading the 
error messages makes me less sure.
   
   If the ref is a tag, then we don't want to retry because it can't be 
updated, right? In that case, this should throw some other exception because 
the table is read-only.
   
   If the ref is a branch, then the ref should be refreshed using the normal 
retry logic so everything looks good. `doRefresh` will update the ref and then 
update the table content.




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

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