yuqi1129 commented on code in PR #8696:
URL: https://github.com/apache/gravitino/pull/8696#discussion_r2432008555


##########
iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/cache/LocalMetadataCache.java:
##########
@@ -0,0 +1,86 @@
+/*
+ *  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.gravitino.iceberg.common.cache;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class LocalMetadataCache extends BaseMetadataCache {
+  public static final Logger LOG = 
LoggerFactory.getLogger(LocalMetadataCache.class);
+  private Cache<TableIdentifier, TableMetadata> tableMetadataCache;
+
+  @Override
+  public void initialize(
+      int capacity,
+      int expireMinutes,
+      Map<String, String> catalogProperties,
+      SupportsMetadataLocation supportsMetadataLocation) {
+    super.initialize(supportsMetadataLocation);
+    this.tableMetadataCache =

Review Comment:
   I'm wondering if a table meta was modified outside the Gravitino server, can 
the Gravitino detect the change and then invalidate the cache? 



##########
core/src/main/java/org/apache/gravitino/utils/ClassUtils.java:
##########
@@ -27,4 +27,12 @@ public static <T> T loadClass(String className) {
       throw new RuntimeException(e);
     }
   }
+
+  public static <T> T loadClass(String className, ClassLoader classLoader) {

Review Comment:
   The method name `loadAndGetInstance` should be more proper. 



##########
iceberg/iceberg-common/src/main/java/org/apache/iceberg/hive/HiveCatalogWithMetadataLocation.java:
##########
@@ -0,0 +1,80 @@
+/*
+ *  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.hive;
+
+import java.lang.reflect.Field;
+import java.util.Map;
+import org.apache.gravitino.iceberg.common.ClosableHiveCatalog;
+import org.apache.gravitino.iceberg.common.cache.SupportsMetadataLocation;
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.iceberg.BaseMetastoreTableOperations;
+import org.apache.iceberg.ClientPool;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.NoSuchIcebergTableException;
+import org.apache.thrift.TException;
+
+public class HiveCatalogWithMetadataLocation extends ClosableHiveCatalog
+    implements SupportsMetadataLocation {
+  private ClientPool<IMetaStoreClient, TException> metaClients;
+  private String catalogName;
+
+  @Override
+  public void initialize(String name, Map<String, String> properties) {
+    super.initialize(name, properties);
+    loadFields();
+  }
+
+  @Override
+  public String metadataLocation(TableIdentifier tableIdentifier) {
+    String dbName = tableIdentifier.namespace().level(0);
+    String tableName = tableIdentifier.name();
+    String fullName = catalogName + "." + dbName + "." + tableName;
+
+    try {
+      Table table = metaClients.run(client -> client.getTable(dbName, 
tableName));
+      String tableType = 
table.getParameters().get(BaseMetastoreTableOperations.TABLE_TYPE_PROP);
+      NoSuchIcebergTableException.check(
+          tableType != null
+              && 
tableType.equalsIgnoreCase(BaseMetastoreTableOperations.ICEBERG_TABLE_TYPE_VALUE),
+          "Not an iceberg table: %s (type=%s)",
+          fullName,
+          tableType);
+      return table.getParameters().get(METADATA_LOCATION_PROP);
+    } catch (Exception e) {
+      return null;
+    }
+  }
+
+  private void loadFields() {
+    try {
+      Class<?> baseClass = HiveCatalog.class;
+      Field catalogNameField = baseClass.getDeclaredField("name");
+      catalogNameField.setAccessible(true);
+      this.catalogName = (String) catalogNameField.get(this);
+
+      Field clientsField = baseClass.getDeclaredField("clients");
+      clientsField.setAccessible(true);
+      this.metaClients = (ClientPool<IMetaStoreClient, TException>) 
clientsField.get(this);

Review Comment:
   You can use FieldUtils to simplify the code here.



##########
iceberg/iceberg-common/src/main/java/org/apache/iceberg/hive/HiveCatalogWithMetadataLocation.java:
##########
@@ -0,0 +1,80 @@
+/*
+ *  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.hive;
+
+import java.lang.reflect.Field;
+import java.util.Map;
+import org.apache.gravitino.iceberg.common.ClosableHiveCatalog;
+import org.apache.gravitino.iceberg.common.cache.SupportsMetadataLocation;
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.iceberg.BaseMetastoreTableOperations;
+import org.apache.iceberg.ClientPool;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.NoSuchIcebergTableException;
+import org.apache.thrift.TException;
+
+public class HiveCatalogWithMetadataLocation extends ClosableHiveCatalog
+    implements SupportsMetadataLocation {
+  private ClientPool<IMetaStoreClient, TException> metaClients;
+  private String catalogName;
+
+  @Override
+  public void initialize(String name, Map<String, String> properties) {
+    super.initialize(name, properties);
+    loadFields();
+  }
+
+  @Override
+  public String metadataLocation(TableIdentifier tableIdentifier) {
+    String dbName = tableIdentifier.namespace().level(0);
+    String tableName = tableIdentifier.name();
+    String fullName = catalogName + "." + dbName + "." + tableName;
+
+    try {
+      Table table = metaClients.run(client -> client.getTable(dbName, 
tableName));
+      String tableType = 
table.getParameters().get(BaseMetastoreTableOperations.TABLE_TYPE_PROP);
+      NoSuchIcebergTableException.check(
+          tableType != null
+              && 
tableType.equalsIgnoreCase(BaseMetastoreTableOperations.ICEBERG_TABLE_TYPE_VALUE),
+          "Not an iceberg table: %s (type=%s)",
+          fullName,
+          tableType);

Review Comment:
   Using `NoSuchIcebergTableException` to check the condition is unnecessary, 
why not use the following code directly 
   
   
   ```java
   if (tableType == null || 
!tableType.equalsIgnoreCase(BaseMetastoreTableOperations.ICEBERG_TABLE_TYPE_VALUE))
 {
      return null;
   }
   ```



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