You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "danielcweeks (via GitHub)" <gi...@apache.org> on 2023/04/27 23:44:31 UTC

[GitHub] [iceberg] danielcweeks commented on a diff in pull request #7412: GCP: Add Iceberg Catalog for GCP BigLake Metastore

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


##########
gcp/src/main/java/org/apache/iceberg/gcp/biglake/BigLakeCatalog.java:
##########
@@ -0,0 +1,405 @@
+/*
+ * 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.cloud.bigquery.biglake.v1.Catalog;
+import com.google.cloud.bigquery.biglake.v1.CatalogName;
+import com.google.cloud.bigquery.biglake.v1.Database;
+import com.google.cloud.bigquery.biglake.v1.DatabaseName;
+import com.google.cloud.bigquery.biglake.v1.HiveDatabaseOptions;
+import com.google.cloud.bigquery.biglake.v1.Table;
+import com.google.cloud.bigquery.biglake.v1.TableName;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.hadoop.conf.Configurable;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.iceberg.BaseMetastoreCatalog;
+import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.CatalogUtil;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableOperations;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.SupportsNamespaces;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.ServiceFailureException;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.hadoop.Util;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.base.Strings;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Streams;
+import org.apache.iceberg.util.LocationUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Iceberg BigLake Metastore (BLMS) Catalog implementation. */
+public final class BigLakeCatalog extends BaseMetastoreCatalog
+    implements SupportsNamespaces, Configurable {
+
+  // User provided properties.
+  // The endpoint of BigLake API. Optional, default to DEFAULT_BIGLAKE_SERVICE_ENDPOINT.
+  public static final String PROPERTIES_KEY_BIGLAKE_ENDPOINT = "blms_endpoint";
+  // The GCP project ID. Required.
+  public static final String PROPERTIES_KEY_GCP_PROJECT = "gcp_project";
+  // The GCP location (https://cloud.google.com/bigquery/docs/locations). Optional, default to
+  // DEFAULT_GCP_LOCATION.
+  public static final String PROPERTIES_KEY_GCP_LOCATION = "gcp_location";
+  // The BLMS catalog ID. It is the container resource of databases and tables.
+  // It links a BLMS catalog with this Iceberg catalog.
+  public static final String PROPERTIES_KEY_BLMS_CATALOG = "blms_catalog";
+
+  public static final String HIVE_METASTORE_WAREHOUSE_DIR = "hive.metastore.warehouse.dir";
+
+  public static final String DEFAULT_BIGLAKE_SERVICE_ENDPOINT = "biglake.googleapis.com:443";
+  public static final String DEFAULT_GCP_LOCATION = "us";
+
+  private static final Logger LOG = LoggerFactory.getLogger(BigLakeCatalog.class);
+
+  // The name of this Iceberg catalog plugin: spark.sql.catalog.<catalog_plugin>.
+  private String catalogPulginName;
+  private Map<String, String> catalogProperties;
+  private FileSystem fs;
+  private FileIO fileIO;
+  private Configuration conf;
+  private String projectId;
+  private String location;
+  // BLMS catalog ID and fully qualified name.
+  private String catalogId;
+  private CatalogName catalogName;
+  private BigLakeClient client;
+
+  // Must have a no-arg constructor to be dynamically loaded
+  // initialize(String name, Map<String, String> properties) will be called to complete
+  // initialization
+  public BigLakeCatalog() {}
+
+  @Override
+  public void initialize(String inputName, Map<String, String> properties) {
+    if (!properties.containsKey(PROPERTIES_KEY_GCP_PROJECT)) {

Review Comment:
   You should probably switch this to use `ValidationException.check(...)` or `Preconditions.check`.  The latter approach is more consistent with other initializations.



##########
gcp/src/main/java/org/apache/iceberg/gcp/biglake/BigLakeCatalog.java:
##########
@@ -0,0 +1,405 @@
+/*
+ * 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.cloud.bigquery.biglake.v1.Catalog;
+import com.google.cloud.bigquery.biglake.v1.CatalogName;
+import com.google.cloud.bigquery.biglake.v1.Database;
+import com.google.cloud.bigquery.biglake.v1.DatabaseName;
+import com.google.cloud.bigquery.biglake.v1.HiveDatabaseOptions;
+import com.google.cloud.bigquery.biglake.v1.Table;
+import com.google.cloud.bigquery.biglake.v1.TableName;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.hadoop.conf.Configurable;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.iceberg.BaseMetastoreCatalog;
+import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.CatalogUtil;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableOperations;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.SupportsNamespaces;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.ServiceFailureException;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.hadoop.Util;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.base.Strings;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Streams;
+import org.apache.iceberg.util.LocationUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Iceberg BigLake Metastore (BLMS) Catalog implementation. */
+public final class BigLakeCatalog extends BaseMetastoreCatalog
+    implements SupportsNamespaces, Configurable {
+
+  // User provided properties.
+  // The endpoint of BigLake API. Optional, default to DEFAULT_BIGLAKE_SERVICE_ENDPOINT.
+  public static final String PROPERTIES_KEY_BIGLAKE_ENDPOINT = "blms_endpoint";
+  // The GCP project ID. Required.
+  public static final String PROPERTIES_KEY_GCP_PROJECT = "gcp_project";

Review Comment:
   These properties appear to be duplicates of values in `GCPProperties`.  Please use that class instead for defining and accessing properties.



##########
gcp/src/main/java/org/apache/iceberg/gcp/biglake/BigLakeCatalog.java:
##########
@@ -0,0 +1,405 @@
+/*
+ * 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.cloud.bigquery.biglake.v1.Catalog;
+import com.google.cloud.bigquery.biglake.v1.CatalogName;
+import com.google.cloud.bigquery.biglake.v1.Database;
+import com.google.cloud.bigquery.biglake.v1.DatabaseName;
+import com.google.cloud.bigquery.biglake.v1.HiveDatabaseOptions;
+import com.google.cloud.bigquery.biglake.v1.Table;
+import com.google.cloud.bigquery.biglake.v1.TableName;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.hadoop.conf.Configurable;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.iceberg.BaseMetastoreCatalog;
+import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.CatalogUtil;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableOperations;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.SupportsNamespaces;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.ServiceFailureException;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.hadoop.Util;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.base.Strings;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Streams;
+import org.apache.iceberg.util.LocationUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Iceberg BigLake Metastore (BLMS) Catalog implementation. */
+public final class BigLakeCatalog extends BaseMetastoreCatalog
+    implements SupportsNamespaces, Configurable {
+
+  // User provided properties.
+  // The endpoint of BigLake API. Optional, default to DEFAULT_BIGLAKE_SERVICE_ENDPOINT.
+  public static final String PROPERTIES_KEY_BIGLAKE_ENDPOINT = "blms_endpoint";
+  // The GCP project ID. Required.
+  public static final String PROPERTIES_KEY_GCP_PROJECT = "gcp_project";
+  // The GCP location (https://cloud.google.com/bigquery/docs/locations). Optional, default to
+  // DEFAULT_GCP_LOCATION.
+  public static final String PROPERTIES_KEY_GCP_LOCATION = "gcp_location";
+  // The BLMS catalog ID. It is the container resource of databases and tables.
+  // It links a BLMS catalog with this Iceberg catalog.
+  public static final String PROPERTIES_KEY_BLMS_CATALOG = "blms_catalog";
+
+  public static final String HIVE_METASTORE_WAREHOUSE_DIR = "hive.metastore.warehouse.dir";
+
+  public static final String DEFAULT_BIGLAKE_SERVICE_ENDPOINT = "biglake.googleapis.com:443";
+  public static final String DEFAULT_GCP_LOCATION = "us";
+
+  private static final Logger LOG = LoggerFactory.getLogger(BigLakeCatalog.class);
+
+  // The name of this Iceberg catalog plugin: spark.sql.catalog.<catalog_plugin>.
+  private String catalogPulginName;
+  private Map<String, String> catalogProperties;
+  private FileSystem fs;
+  private FileIO fileIO;
+  private Configuration conf;
+  private String projectId;
+  private String location;
+  // BLMS catalog ID and fully qualified name.
+  private String catalogId;
+  private CatalogName catalogName;
+  private BigLakeClient client;
+
+  // Must have a no-arg constructor to be dynamically loaded
+  // initialize(String name, Map<String, String> properties) will be called to complete
+  // initialization
+  public BigLakeCatalog() {}
+
+  @Override
+  public void initialize(String inputName, Map<String, String> properties) {
+    if (!properties.containsKey(PROPERTIES_KEY_GCP_PROJECT)) {
+      throw new ValidationException("GCP project must be specified");
+    }
+    String propProjectId = properties.get(PROPERTIES_KEY_GCP_PROJECT);
+    String propLocation =
+        properties.getOrDefault(PROPERTIES_KEY_GCP_LOCATION, DEFAULT_GCP_LOCATION);
+    BigLakeClient newClient;
+    try {
+      newClient =
+          new BigLakeClientImpl(
+              properties.getOrDefault(
+                  PROPERTIES_KEY_BIGLAKE_ENDPOINT, DEFAULT_BIGLAKE_SERVICE_ENDPOINT),
+              propProjectId,
+              propLocation);
+    } catch (IOException e) {
+      throw new ServiceFailureException(e, "Creating BigLake client failed");
+    }
+    initialize(inputName, properties, propProjectId, propLocation, newClient);
+  }
+
+  @VisibleForTesting
+  void initialize(
+      String inputName,
+      Map<String, String> properties,
+      String propProjectId,
+      String propLocation,
+      BigLakeClient bigLakeClient) {
+    this.catalogPulginName = inputName;
+    this.catalogProperties = ImmutableMap.copyOf(properties);
+    this.projectId = propProjectId;
+    this.location = propLocation;
+    Preconditions.checkNotNull(bigLakeClient, "BigLake client must not be null");
+    this.client = bigLakeClient;
+
+    if (this.conf == null) {
+      LOG.warn("No Hadoop Configuration was set, using the default environment Configuration");
+      this.conf = new Configuration();
+    }
+
+    // Users can specify the BigLake catalog ID, otherwise catalog plugin will be used.
+    this.catalogId = properties.getOrDefault(PROPERTIES_KEY_BLMS_CATALOG, inputName);
+    this.catalogName = CatalogName.of(projectId, location, catalogId);
+    LOG.info("Use BigLake catalog: {}", catalogName.toString());
+
+    if (properties.containsKey(CatalogProperties.WAREHOUSE_LOCATION)) {
+      this.conf.set(
+          HIVE_METASTORE_WAREHOUSE_DIR,
+          LocationUtil.stripTrailingSlash(properties.get(CatalogProperties.WAREHOUSE_LOCATION)));
+    }
+
+    this.fs =
+        Util.getFs(
+            new Path(
+                LocationUtil.stripTrailingSlash(
+                    properties.get(CatalogProperties.WAREHOUSE_LOCATION))),
+            conf);
+
+    String fileIOImpl =
+        properties.getOrDefault(
+            CatalogProperties.FILE_IO_IMPL, "org.apache.iceberg.hadoop.HadoopFileIO");
+    this.fileIO = CatalogUtil.loadFileIO(fileIOImpl, properties, conf);
+  }
+
+  @Override
+  protected TableOperations newTableOps(TableIdentifier identifier) {
+    return new BigLakeTableOperations(
+        conf,
+        client,
+        fileIO,
+        getTableName(getDatabaseId(identifier.namespace()), /* tableId= */ identifier.name()));
+  }
+
+  @Override
+  protected String defaultWarehouseLocation(TableIdentifier identifier) {
+    String locationUri = getDatabase(identifier.namespace()).getHiveOptions().getLocationUri();
+    return String.format(
+        "%s/%s",
+        Strings.isNullOrEmpty(locationUri)
+            ? getDatabaseLocation(getDatabaseId(identifier.namespace()))
+            : locationUri,
+        identifier.name());
+  }
+
+  @Override
+  public List<TableIdentifier> listTables(Namespace namespace) {
+    // When deleting a BLMS catalog via `DROP NAMESPACE <catalog>`, this method is called for
+    // verifying catalog emptiness. `namespace` is empty in this case, we list databases in
+    // this catalog instead.
+    if (namespace.levels().length == 0) {
+      return Iterables.isEmpty(client.listDatabases(catalogName))
+          ? ImmutableList.of()
+          : ImmutableList.of(TableIdentifier.of("placeholder"));
+    }
+    return Streams.stream(client.listTables(getDatabaseName(namespace)))
+        .map(BigLakeCatalog::getTableIdentifier)
+        .collect(ImmutableList.toImmutableList());
+  }
+
+  @Override
+  public boolean dropTable(TableIdentifier identifier, boolean purge) {
+    TableOperations ops = newTableOps(identifier);
+    // TODO: to catch NotFoundException as in https://github.com/apache/iceberg/pull/5510.
+    TableMetadata lastMetadata = ops.current();
+    client.deleteTable(
+        getTableName(getDatabaseId(identifier.namespace()), /* tableId= */ identifier.name()));
+    if (purge && lastMetadata != null) {
+      CatalogUtil.dropTableData(ops.io(), lastMetadata);
+    }
+    return true;
+  }
+
+  @Override
+  public void renameTable(TableIdentifier from, TableIdentifier to) {
+    String fromDbId = getDatabaseId(from.namespace());
+    String toDbId = getDatabaseId(to.namespace());
+
+    if (!fromDbId.equals(toDbId)) {
+      throw new ValidationException("New table name must be in the same database");
+    }
+
+    client.renameTable(getTableName(fromDbId, from.name()), getTableName(toDbId, to.name()));
+  }
+
+  @Override
+  public void createNamespace(Namespace namespace, Map<String, String> metadata) {
+    if (namespace.levels().length == 0) {

Review Comment:
   I'm not sure this will map correctly to the spark catalog namespacing.  What is the namespacing for the catalog?
   
   Is it `<spark-catalog>.<biglake-catalog>.<database/schema>.<table>`?



##########
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;

Review Comment:
   We should avoid direct dependencies on Hadoop.  I don't why this is necessary as it does not appear to be used anywhere.



##########
gcp/src/main/java/org/apache/iceberg/gcp/biglake/BigLakeClient.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.cloud.bigquery.biglake.v1.Catalog;
+import com.google.cloud.bigquery.biglake.v1.CatalogName;
+import com.google.cloud.bigquery.biglake.v1.Database;
+import com.google.cloud.bigquery.biglake.v1.DatabaseName;
+import com.google.cloud.bigquery.biglake.v1.Table;
+import com.google.cloud.bigquery.biglake.v1.TableName;
+import java.util.Map;
+
+/** A client interface of Google BigLake service. */
+public interface BigLakeClient {

Review Comment:
   This doesn't need to be public



##########
gcp/src/main/java/org/apache/iceberg/gcp/biglake/BigLakeCatalog.java:
##########
@@ -0,0 +1,405 @@
+/*
+ * 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.cloud.bigquery.biglake.v1.Catalog;
+import com.google.cloud.bigquery.biglake.v1.CatalogName;
+import com.google.cloud.bigquery.biglake.v1.Database;
+import com.google.cloud.bigquery.biglake.v1.DatabaseName;
+import com.google.cloud.bigquery.biglake.v1.HiveDatabaseOptions;
+import com.google.cloud.bigquery.biglake.v1.Table;
+import com.google.cloud.bigquery.biglake.v1.TableName;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.hadoop.conf.Configurable;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.iceberg.BaseMetastoreCatalog;
+import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.CatalogUtil;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableOperations;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.SupportsNamespaces;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.ServiceFailureException;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.hadoop.Util;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.base.Strings;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Streams;
+import org.apache.iceberg.util.LocationUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Iceberg BigLake Metastore (BLMS) Catalog implementation. */
+public final class BigLakeCatalog extends BaseMetastoreCatalog
+    implements SupportsNamespaces, Configurable {

Review Comment:
   We should use `Configurable<Object>` and avoid direct dependencies on Hadoop.



##########
gcp/src/main/java/org/apache/iceberg/gcp/biglake/BigLakeCatalog.java:
##########
@@ -0,0 +1,405 @@
+/*
+ * 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.cloud.bigquery.biglake.v1.Catalog;
+import com.google.cloud.bigquery.biglake.v1.CatalogName;
+import com.google.cloud.bigquery.biglake.v1.Database;
+import com.google.cloud.bigquery.biglake.v1.DatabaseName;
+import com.google.cloud.bigquery.biglake.v1.HiveDatabaseOptions;
+import com.google.cloud.bigquery.biglake.v1.Table;
+import com.google.cloud.bigquery.biglake.v1.TableName;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.hadoop.conf.Configurable;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.iceberg.BaseMetastoreCatalog;
+import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.CatalogUtil;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableOperations;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.SupportsNamespaces;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.ServiceFailureException;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.hadoop.Util;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.base.Strings;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Streams;
+import org.apache.iceberg.util.LocationUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Iceberg BigLake Metastore (BLMS) Catalog implementation. */
+public final class BigLakeCatalog extends BaseMetastoreCatalog
+    implements SupportsNamespaces, Configurable {
+
+  // User provided properties.
+  // The endpoint of BigLake API. Optional, default to DEFAULT_BIGLAKE_SERVICE_ENDPOINT.
+  public static final String PROPERTIES_KEY_BIGLAKE_ENDPOINT = "blms_endpoint";
+  // The GCP project ID. Required.
+  public static final String PROPERTIES_KEY_GCP_PROJECT = "gcp_project";
+  // The GCP location (https://cloud.google.com/bigquery/docs/locations). Optional, default to
+  // DEFAULT_GCP_LOCATION.
+  public static final String PROPERTIES_KEY_GCP_LOCATION = "gcp_location";
+  // The BLMS catalog ID. It is the container resource of databases and tables.
+  // It links a BLMS catalog with this Iceberg catalog.
+  public static final String PROPERTIES_KEY_BLMS_CATALOG = "blms_catalog";
+
+  public static final String HIVE_METASTORE_WAREHOUSE_DIR = "hive.metastore.warehouse.dir";
+
+  public static final String DEFAULT_BIGLAKE_SERVICE_ENDPOINT = "biglake.googleapis.com:443";
+  public static final String DEFAULT_GCP_LOCATION = "us";
+
+  private static final Logger LOG = LoggerFactory.getLogger(BigLakeCatalog.class);
+
+  // The name of this Iceberg catalog plugin: spark.sql.catalog.<catalog_plugin>.
+  private String catalogPulginName;
+  private Map<String, String> catalogProperties;
+  private FileSystem fs;
+  private FileIO fileIO;
+  private Configuration conf;
+  private String projectId;
+  private String location;
+  // BLMS catalog ID and fully qualified name.
+  private String catalogId;
+  private CatalogName catalogName;
+  private BigLakeClient client;
+
+  // Must have a no-arg constructor to be dynamically loaded
+  // initialize(String name, Map<String, String> properties) will be called to complete
+  // initialization
+  public BigLakeCatalog() {}
+
+  @Override
+  public void initialize(String inputName, Map<String, String> properties) {
+    if (!properties.containsKey(PROPERTIES_KEY_GCP_PROJECT)) {
+      throw new ValidationException("GCP project must be specified");
+    }
+    String propProjectId = properties.get(PROPERTIES_KEY_GCP_PROJECT);
+    String propLocation =
+        properties.getOrDefault(PROPERTIES_KEY_GCP_LOCATION, DEFAULT_GCP_LOCATION);
+    BigLakeClient newClient;
+    try {
+      newClient =
+          new BigLakeClientImpl(
+              properties.getOrDefault(
+                  PROPERTIES_KEY_BIGLAKE_ENDPOINT, DEFAULT_BIGLAKE_SERVICE_ENDPOINT),
+              propProjectId,
+              propLocation);
+    } catch (IOException e) {
+      throw new ServiceFailureException(e, "Creating BigLake client failed");
+    }
+    initialize(inputName, properties, propProjectId, propLocation, newClient);
+  }
+
+  @VisibleForTesting
+  void initialize(
+      String inputName,
+      Map<String, String> properties,
+      String propProjectId,
+      String propLocation,
+      BigLakeClient bigLakeClient) {
+    this.catalogPulginName = inputName;
+    this.catalogProperties = ImmutableMap.copyOf(properties);
+    this.projectId = propProjectId;
+    this.location = propLocation;
+    Preconditions.checkNotNull(bigLakeClient, "BigLake client must not be null");
+    this.client = bigLakeClient;
+
+    if (this.conf == null) {
+      LOG.warn("No Hadoop Configuration was set, using the default environment Configuration");
+      this.conf = new Configuration();
+    }
+
+    // Users can specify the BigLake catalog ID, otherwise catalog plugin will be used.
+    this.catalogId = properties.getOrDefault(PROPERTIES_KEY_BLMS_CATALOG, inputName);
+    this.catalogName = CatalogName.of(projectId, location, catalogId);
+    LOG.info("Use BigLake catalog: {}", catalogName.toString());
+
+    if (properties.containsKey(CatalogProperties.WAREHOUSE_LOCATION)) {
+      this.conf.set(
+          HIVE_METASTORE_WAREHOUSE_DIR,
+          LocationUtil.stripTrailingSlash(properties.get(CatalogProperties.WAREHOUSE_LOCATION)));
+    }
+
+    this.fs =
+        Util.getFs(
+            new Path(
+                LocationUtil.stripTrailingSlash(
+                    properties.get(CatalogProperties.WAREHOUSE_LOCATION))),
+            conf);
+
+    String fileIOImpl =
+        properties.getOrDefault(
+            CatalogProperties.FILE_IO_IMPL, "org.apache.iceberg.hadoop.HadoopFileIO");

Review Comment:
   `ResolvingFileIO` should be used as the default.



##########
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:
   Is this necessary?  These options don't apply to Iceberg tables.



##########
gcp/src/main/java/org/apache/iceberg/gcp/biglake/BigLakeClientImpl.java:
##########
@@ -0,0 +1,288 @@
+/*
+ * 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.PermissionDeniedException;
+import com.google.cloud.bigquery.biglake.v1.Catalog;
+import com.google.cloud.bigquery.biglake.v1.CatalogName;
+import com.google.cloud.bigquery.biglake.v1.CreateCatalogRequest;
+import com.google.cloud.bigquery.biglake.v1.CreateDatabaseRequest;
+import com.google.cloud.bigquery.biglake.v1.CreateTableRequest;
+import com.google.cloud.bigquery.biglake.v1.Database;
+import com.google.cloud.bigquery.biglake.v1.DatabaseName;
+import com.google.cloud.bigquery.biglake.v1.DeleteCatalogRequest;
+import com.google.cloud.bigquery.biglake.v1.DeleteDatabaseRequest;
+import com.google.cloud.bigquery.biglake.v1.DeleteTableRequest;
+import com.google.cloud.bigquery.biglake.v1.GetCatalogRequest;
+import com.google.cloud.bigquery.biglake.v1.GetDatabaseRequest;
+import com.google.cloud.bigquery.biglake.v1.GetTableRequest;
+import com.google.cloud.bigquery.biglake.v1.ListDatabasesRequest;
+import com.google.cloud.bigquery.biglake.v1.ListTablesRequest;
+import com.google.cloud.bigquery.biglake.v1.LocationName;
+import com.google.cloud.bigquery.biglake.v1.MetastoreServiceClient;
+import com.google.cloud.bigquery.biglake.v1.MetastoreServiceSettings;
+import com.google.cloud.bigquery.biglake.v1.RenameTableRequest;
+import com.google.cloud.bigquery.biglake.v1.Table;
+import com.google.cloud.bigquery.biglake.v1.TableName;
+import com.google.cloud.bigquery.biglake.v1.UpdateDatabaseRequest;
+import com.google.cloud.bigquery.biglake.v1.UpdateTableRequest;
+import com.google.protobuf.Empty;
+import com.google.protobuf.FieldMask;
+import java.io.IOException;
+import java.util.Map;
+import java.util.function.Supplier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.NotAuthorizedException;
+
+/** A client implementation of Google BigLake service. */
+public final class BigLakeClientImpl implements BigLakeClient {

Review Comment:
   This should be package protected



-- 
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-unsubscribe@iceberg.apache.org

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


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