Skip to content

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

[#4116] improvement(Iceberg-catalog): Close resource in KerberosClient when catalog is dropped. (#4117)

Qi Yuba75c86e5683
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/authentication/kerberos/KerberosClient.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/authentication/kerberos/KerberosClient.java
index dd776b5f6a..d717906ff2 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/authentication/kerberos/KerberosClient.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/authentication/kerberos/KerberosClient.java
@@ -22,12 +22,12 @@ package org.apache.gravitino.catalog.lakehouse.iceberg.authentication.kerberos;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
-import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
@@ -36,20 +36,31 @@ import org.apache.hadoop.security.authentication.util.KerberosName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class KerberosClient {
+public class KerberosClient implements Closeable {
private static final Logger LOG = LoggerFactory.getLogger(KerberosClient.class);
private final ScheduledThreadPoolExecutor checkTgtExecutor;
private final Map<String, String> conf;
private final Configuration hadoopConf;
+ private String realm;
public KerberosClient(Map<String, String> conf, Configuration hadoopConf) {
this.conf = conf;
this.hadoopConf = hadoopConf;
- this.checkTgtExecutor = new ScheduledThreadPoolExecutor(1, getThreadFactory("check-tgt"));
+ this.checkTgtExecutor =
+ new ScheduledThreadPoolExecutor(
+ 1,
+ new ThreadFactoryBuilder()
+ .setDaemon(true)
+ .setNameFormat("check-Iceberg-Hive-tgt-%d")
+ .build());
}
- public String login(String keytabFilePath) throws IOException {
+ public String getRealm() {
+ return realm;
+ }
+
+ public void login(String keytabFilePath) throws IOException {
KerberosConfig kerberosConfig = new KerberosConfig(conf);
// Check the principal and keytab file
@@ -81,7 +92,7 @@ public class KerberosClient {
checkInterval,
TimeUnit.SECONDS);
- return principalComponents.get(1);
+ this.realm = principalComponents.get(1);
}
public File saveKeyTabFileFromUri(Long catalogId) throws IOException {
@@ -115,7 +126,10 @@ public class KerberosClient {
return keytabFile;
}
- private static ThreadFactory getThreadFactory(String factoryName) {
- return new ThreadFactoryBuilder().setDaemon(true).setNameFormat(factoryName + "-%d").build();
+ @Override
+ public void close() throws IOException {
+ if (checkTgtExecutor != null) {
+ checkTgtExecutor.shutdownNow();
+ }
}
}
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/catalog/ClosableHiveCatalog.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/catalog/ClosableHiveCatalog.java
new file mode 100644
index 0000000000..863f9b5733
--- /dev/null
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/catalog/ClosableHiveCatalog.java
@@ -0,0 +1,63 @@
+/*
+ * 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.catalog.lakehouse.iceberg.catalog;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.List;
+import org.apache.commons.compress.utils.Lists;
+import org.apache.iceberg.hive.HiveCatalog;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * ClosableHiveCatalog is a wrapper class to wrap Iceberg HiveCatalog to do some clean-up work like
+ * closing resources.
+ */
+public class ClosableHiveCatalog extends HiveCatalog implements Closeable {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ClosableHiveCatalog.class);
+
+ private final List<Closeable> resources = Lists.newArrayList();
+
+ public ClosableHiveCatalog() {
+ super();
+ }
+
+ public void addResource(Closeable resource) {
+ resources.add(resource);
+ }
+
+ @Override
+ 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.
+ resources.forEach(
+ resource -> {
+ try {
+ if (resource != null) {
+ resource.close();
+ }
+ } catch (Exception e) {
+ LOGGER.warn("Failed to close resource: {}", resource, e);
+ }
+ });
+ }
+}
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/ops/IcebergTableOps.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/ops/IcebergTableOps.java
index 54791c20d7..d99e5f906f 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/ops/IcebergTableOps.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/ops/IcebergTableOps.java
@@ -160,7 +160,7 @@ public class IcebergTableOps implements AutoCloseable {
@Override
public void close() throws Exception {
if (catalog instanceof AutoCloseable) {
- // JdbcCatalog need close.
+ // JdbcCatalog and WrappedHiveCatalog need close.
((AutoCloseable) catalog).close();
}
@@ -176,7 +176,8 @@ public class IcebergTableOps implements AutoCloseable {
} else if (catalogUri != null && catalogUri.contains("postgresql")) {
closePostgreSQLCatalogResource();
} else if (catalogType.equalsIgnoreCase(IcebergCatalogBackend.HIVE.name())) {
- // TODO(yuqi) add close for other catalog types such Hive catalog
+ // 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
}
}
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/utils/IcebergCatalogUtil.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/utils/IcebergCatalogUtil.java
index 807cdb7cda..4860ee1b3a 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/utils/IcebergCatalogUtil.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/utils/IcebergCatalogUtil.java
@@ -32,6 +32,7 @@ import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergConfig;
import org.apache.gravitino.catalog.lakehouse.iceberg.authentication.AuthenticationConfig;
import org.apache.gravitino.catalog.lakehouse.iceberg.authentication.kerberos.HiveBackendProxy;
import org.apache.gravitino.catalog.lakehouse.iceberg.authentication.kerberos.KerberosClient;
+import org.apache.gravitino.catalog.lakehouse.iceberg.catalog.ClosableHiveCatalog;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.HdfsConfiguration;
import org.apache.iceberg.CatalogProperties;
@@ -58,7 +59,7 @@ public class IcebergCatalogUtil {
}
private static HiveCatalog loadHiveCatalog(Map<String, String> properties) {
- HiveCatalog hiveCatalog = new HiveCatalog();
+ ClosableHiveCatalog hiveCatalog = new ClosableHiveCatalog();
HdfsConfiguration hdfsConfiguration = new HdfsConfiguration();
properties.forEach(hdfsConfiguration::set);
IcebergConfig icebergConfig = new IcebergConfig(properties);
@@ -77,10 +78,11 @@ public class IcebergCatalogUtil {
hiveCatalog.setConf(hdfsConfiguration);
hiveCatalog.initialize(icebergCatalogName, properties);
- String realm = initKerberosAndReturnRealm(properties, hdfsConfiguration);
+ KerberosClient kerberosClient = initKerberosAndReturnClient(properties, hdfsConfiguration);
+ hiveCatalog.addResource(kerberosClient);
if (authenticationConfig.isImpersonationEnabled()) {
HiveBackendProxy proxyHiveCatalog =
- new HiveBackendProxy(resultProperties, hiveCatalog, realm);
+ new HiveBackendProxy(resultProperties, hiveCatalog, kerberosClient.getRealm());
return proxyHiveCatalog.getProxy();
}
@@ -91,13 +93,14 @@ public class IcebergCatalogUtil {
}
}
- private static String initKerberosAndReturnRealm(
+ private static KerberosClient initKerberosAndReturnClient(
Map<String, String> properties, Configuration conf) {
try {
KerberosClient kerberosClient = new KerberosClient(properties, conf);
File keytabFile =
kerberosClient.saveKeyTabFileFromUri(Long.valueOf(properties.get("catalog_uuid")));
- return kerberosClient.login(keytabFile.getAbsolutePath());
+ kerberosClient.login(keytabFile.getAbsolutePath());
+ return kerberosClient;
} catch (IOException e) {
throw new RuntimeException("Failed to login with kerberos", e);
}