Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#8999]Improvement(iceberg-common): Add closing of the clientPool in ClosableHiveCatalog.close() (#9000)

Xiaojian Sun73e9003b91df
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ClosableHiveCatalog.java b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ClosableHiveCatalog.java
index 6160d38a7d..88db666d83 100644
--- a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ClosableHiveCatalog.java
+++ b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ClosableHiveCatalog.java
@@ -22,7 +22,9 @@ package org.apache.gravitino.iceberg.common;
import com.google.common.collect.Lists;
import java.io.Closeable;
import java.io.IOException;
+import java.lang.reflect.Field;
import java.util.List;
+import org.apache.gravitino.iceberg.common.utils.IcebergHiveCachedClientPool;
import org.apache.iceberg.hive.HiveCatalog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -49,6 +51,11 @@ public class ClosableHiveCatalog extends HiveCatalog implements Closeable {
public void close() throws IOException {
// Do clean up work here. We need a mechanism to close the HiveCatalog; however, HiveCatalog
// doesn't implement the Closeable interface.
+
+ // First, close the internal HiveCatalog client pool to prevent resource leaks
+ closeInternalClientPool();
+
+ // Then close any additional resources added via addResource()
resources.forEach(
resource -> {
try {
@@ -60,4 +67,30 @@ public class ClosableHiveCatalog extends HiveCatalog implements Closeable {
}
});
}
+
+ /**
+ * Close the internal HiveCatalog client pool using reflection. This is necessary because
+ * HiveCatalog doesn't provide a public API to close its client pool. We need to avoid closing
+ * IcebergHiveCachedClientPool twice (once here and once in resources list).
+ */
+ private void closeInternalClientPool() {
+ try {
+ Field clientsField = HiveCatalog.class.getDeclaredField("clients");
+ clientsField.setAccessible(true);
+ Object clientPool = clientsField.get(this);
+
+ if (clientPool != null && clientPool instanceof AutoCloseable) {
+ // Only close if it's NOT IcebergHiveCachedClientPool
+ if (!(clientPool instanceof IcebergHiveCachedClientPool)) {
+ ((AutoCloseable) clientPool).close();
+ LOGGER.info(
+ "Closed HiveCatalog internal client pool: {}", clientPool.getClass().getSimpleName());
+ }
+ }
+ } catch (NoSuchFieldException e) {
+ LOGGER.warn("Could not find 'clients' field in HiveCatalog, skipping cleanup", e);
+ } catch (Exception e) {
+ LOGGER.warn("Failed to close HiveCatalog internal client pool", e);
+ }
+ }
}
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java
index e8ad09d4bf..bc9f7c0c09 100644
--- a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java
+++ b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java
@@ -280,7 +280,8 @@ public class IcebergCatalogWrapper implements AutoCloseable {
@Override
public void close() throws Exception {
if (catalog instanceof AutoCloseable) {
- // JdbcCatalog and WrappedHiveCatalog need close.
+ // JdbcCatalog and ClosableHiveCatalog implement AutoCloseable and will handle their own
+ // cleanup
((AutoCloseable) catalog).close();
}
metadataCache.close();
@@ -296,9 +297,6 @@ public class IcebergCatalogWrapper implements AutoCloseable {
closeMySQLCatalogResource();
} else if (catalogUri != null && catalogUri.contains("postgresql")) {
closePostgreSQLCatalogResource();
- } else if (catalogBackend.equals(IcebergCatalogBackend.HIVE)) {
- // TODO(yuqi) add close for other catalog types such Hive catalog, for more, please refer to
- // https://github.com/apache/gravitino/pull/2548/commits/ab876b69b7e094bbd8c174d48a2365a18ed5176d
}
}