coufon commented on code in PR #7412:
URL: https://github.com/apache/iceberg/pull/7412#discussion_r1180627744


##########
gcp/src/main/java/org/apache/iceberg/gcp/biglake/BigLakeTableOperations.java:
##########
@@ -0,0 +1,229 @@
+/*
+ * 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.gcp.biglake;
+
+import com.google.api.gax.rpc.AbortedException;
+import com.google.cloud.bigquery.biglake.v1.HiveTableOptions;
+import com.google.cloud.bigquery.biglake.v1.HiveTableOptions.SerDeInfo;
+import com.google.cloud.bigquery.biglake.v1.HiveTableOptions.StorageDescriptor;
+import com.google.cloud.bigquery.biglake.v1.Table;
+import com.google.cloud.bigquery.biglake.v1.TableName;
+import java.util.Map;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.common.StatsSetupConst;
+import org.apache.iceberg.BaseMetastoreTableOperations;
+import org.apache.iceberg.SnapshotSummary;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.exceptions.CommitFailedException;
+import org.apache.iceberg.exceptions.CommitStateUnknownException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Handles BigLake table operations. */
+public final class BigLakeTableOperations extends BaseMetastoreTableOperations 
{
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(BigLakeTableOperations.class);
+
+  private final Configuration conf;
+  private final BigLakeClient client;
+  private final FileIO fileIO;
+  private final TableName tableName;
+
+  BigLakeTableOperations(
+      Configuration conf, BigLakeClient client, FileIO fileIO, TableName 
tableName) {
+    this.conf = conf;
+    this.client = client;
+    this.fileIO = fileIO;
+    this.tableName = tableName;
+  }
+
+  // The doRefresh method should provide implementation on how to get the 
metadata location
+  @Override
+  public void doRefresh() {
+    // Must default to null.
+    String metadataLocation = null;
+    try {
+      HiveTableOptions hiveOptions = 
client.getTable(tableName).getHiveOptions();
+      if (!hiveOptions.containsParameters(METADATA_LOCATION_PROP)) {
+        throw new ValidationException(
+            "Table %s is not a valid Iceberg table, metadata location not 
found", tableName());
+      }
+      metadataLocation = 
hiveOptions.getParametersOrThrow(METADATA_LOCATION_PROP);
+    } catch (NoSuchTableException e) {
+      if (currentMetadataLocation() != null) {
+        // Re-throws the exception because the table must exist in this case.
+        throw e;
+      }
+    }
+    refreshFromMetadataLocation(metadataLocation);
+  }
+
+  // The doCommit method should provide implementation on how to update with 
metadata location
+  // atomically
+  @Override
+  public void doCommit(TableMetadata base, TableMetadata metadata) {
+    boolean isNewTable = base == null;
+    String newMetadataLocation = writeNewMetadataIfRequired(isNewTable, 
metadata);
+
+    CommitStatus commitStatus = CommitStatus.FAILURE;
+    try {
+      if (isNewTable) {
+        createTable(newMetadataLocation, metadata);
+      } else {
+        updateTable(base.metadataFileLocation(), newMetadataLocation, 
metadata);
+      }
+      commitStatus = CommitStatus.SUCCESS;
+    } catch (CommitFailedException | CommitStateUnknownException e) {
+      throw e;
+    } catch (Throwable e) {
+      commitStatus = checkCommitStatus(newMetadataLocation, metadata);
+      if (commitStatus == CommitStatus.FAILURE) {
+        throw new CommitFailedException(e, "Failed to commit");
+      }
+      if (commitStatus == CommitStatus.UNKNOWN) {
+        throw new CommitStateUnknownException(e);
+      }
+    } finally {
+      try {
+        if (commitStatus == 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);
+      }
+    }
+  }
+
+  @Override
+  public String tableName() {
+    return String.format(
+        "%s.%s.%s", tableName.getCatalog(), tableName.getDatabase(), 
tableName.getTable());
+  }
+
+  @Override
+  public FileIO io() {
+    return fileIO;
+  }
+
+  private void createTable(String newMetadataLocation, TableMetadata metadata) 
{
+    LOG.debug("Creating a new Iceberg table: {}", tableName());
+    client.createTable(tableName, makeNewTable(metadata, newMetadataLocation));
+  }
+
+  /** Update table properties with concurrent update detection using etag. */
+  private void updateTable(
+      String oldMetadataLocation, String newMetadataLocation, TableMetadata 
metadata) {
+    Table table = client.getTable(tableName);
+    String etag = table.getEtag();
+    if (etag.isEmpty()) {
+      throw new ValidationException(
+          "Etag of legacy table %s is empty, manually update the table by 
BigLake API or"
+              + " recreate and retry",
+          tableName());
+    }
+    HiveTableOptions options = table.getHiveOptions();
+
+    // If `metadataLocationFromMetastore` is different from metadata location 
of base, it means
+    // someone has updated metadata location in metastore, which is a conflict 
update.
+    String metadataLocationFromMetastore =
+        options.getParametersOrDefault(METADATA_LOCATION_PROP, "");
+    if (!metadataLocationFromMetastore.isEmpty()
+        && !metadataLocationFromMetastore.equals(oldMetadataLocation)) {
+      throw new CommitFailedException(
+          "Base metadata location '%s' is not same as the current table 
metadata location '%s' for"
+              + " %s.%s",
+          oldMetadataLocation,
+          metadataLocationFromMetastore,
+          tableName.getDatabase(),
+          tableName.getTable());
+    }
+
+    try {
+      client.updateTableParameters(
+          tableName, buildTableParameters(newMetadataLocation, metadata), 
etag);
+    } catch (AbortedException e) {
+      if (e.getMessage().toLowerCase().contains("etag mismatch")) {
+        throw new CommitFailedException(
+            "Updating table failed due to conflict updates (etag mismatch)");
+      }
+    }
+  }
+
+  private Table makeNewTable(TableMetadata metadata, String 
metadataFileLocation) {
+    Table.Builder builder = Table.newBuilder().setType(Table.Type.HIVE);
+    builder
+        .getHiveOptionsBuilder()

Review Comment:
   Another way to use BigLake Metastore for Iceberg tables is by installing an 
HMS proxy exposing a local 9083 Thrift port. This proxy reads from the 
Metastore API and returns HMS tables. The benefit of this approach is that it 
works for all data engines that support HMS (without having to write a catalog 
plugin). We populate these fields because the Hive Iceberg catalog populates 
them. It makes these fields stored in BigLake Metastore, so the HMS proxy path 
can return exactly the same table as HMS does.
   
   We received requests to develop more Iceberg catalog plugins (e.g., for 
Trino) for BigLake Metastore. After we have these catalog plugins, maybe we 
don't need to keep this HMS path any more. If it is OK, I'd like to keep the 
code, and we can remove them in future.



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to