Skip to content

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

[#13991] fix(hadoop-common): Stop the Kerberos ticket refresh when the HDFS file system closes (#13995)

YangJiee1ae4c219311
diff --git a/catalogs/hadoop-common/build.gradle.kts b/catalogs/hadoop-common/build.gradle.kts
index 9fe27c2b76..ef0a8d1c2b 100644
--- a/catalogs/hadoop-common/build.gradle.kts
+++ b/catalogs/hadoop-common/build.gradle.kts
@@ -41,6 +41,8 @@ dependencies {
testImplementation(project(":common"))
testImplementation(libs.junit.jupiter.api)
+ testImplementation(libs.mockito.core)
+ testImplementation(libs.mockito.inline)
testImplementation(libs.junit.jupiter.params)
testRuntimeOnly(libs.junit.jupiter.engine)
diff --git a/catalogs/hadoop-common/src/main/java/org/apache/gravitino/catalog/hadoop/fs/HDFSFileSystemProxy.java b/catalogs/hadoop-common/src/main/java/org/apache/gravitino/catalog/hadoop/fs/HDFSFileSystemProxy.java
index fff38762c9..b87ce3b456 100644
--- a/catalogs/hadoop-common/src/main/java/org/apache/gravitino/catalog/hadoop/fs/HDFSFileSystemProxy.java
+++ b/catalogs/hadoop-common/src/main/java/org/apache/gravitino/catalog/hadoop/fs/HDFSFileSystemProxy.java
@@ -31,6 +31,7 @@ import java.lang.reflect.Method;
import java.security.PrivilegedExceptionAction;
import java.util.Locale;
import java.util.Map;
+import javax.annotation.Nullable;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
@@ -44,13 +45,18 @@ import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.UserGroupInformation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/** A FileSystem wrapper that runs all operations under a specific UGI (UserGroupInformation). */
public class HDFSFileSystemProxy implements MethodInterceptor {
+ private static final Logger LOG = LoggerFactory.getLogger(HDFSFileSystemProxy.class);
+
public static final String GRAVITINO_KEYTAB_FORMAT = "keytabs/gravitino-%s";
private static final String GRAVITINO_ID_KEY = "gravitino.identifier";
+ @Nullable private KerberosClient kerberosClient;
protected UserGroupInformation initUgi;
private FileSystem fs;
private Configuration configuration;
@@ -99,8 +105,10 @@ public class HDFSFileSystemProxy implements MethodInterceptor {
() -> FileSystem.newInstance(path.toUri(), conf));
} catch (GravitinoRuntimeException e) {
+ close();
throw e;
} catch (Exception e) {
+ close();
throw new GravitinoRuntimeException(e, "Failed to create HDFS FileSystem with UGI: %s", path);
}
}
@@ -165,7 +173,16 @@ public class HDFSFileSystemProxy implements MethodInterceptor {
});
}
- private void close() {}
+ private synchronized void close() {
+ if (kerberosClient != null) {
+ try {
+ kerberosClient.close();
+ } catch (Exception e) {
+ LOG.warn("Failed to stop the Kerberos ticket refresh for {}", kerberosRealm, e);
+ }
+ kerberosClient = null;
+ }
+ }
private UserGroupInformation initKerberosUgi(
Map<String, String> properties, Configuration configuration) {
@@ -188,6 +205,8 @@ public class HDFSFileSystemProxy implements MethodInterceptor {
false,
configuration);
UserGroupInformation ugi = client.login(keytabFile.getAbsolutePath());
+ // login() starts the ticket refresh, so record the client before anything else can fail.
+ this.kerberosClient = client;
this.kerberosRealm = client.getRealm();
return ugi;
} catch (IOException e) {
diff --git a/catalogs/hadoop-common/src/test/java/org/apache/gravitino/catalog/hadoop/fs/TestHDFSFileSystemProxy.java b/catalogs/hadoop-common/src/test/java/org/apache/gravitino/catalog/hadoop/fs/TestHDFSFileSystemProxy.java
new file mode 100644
index 0000000000..93feb2af86
--- /dev/null
+++ b/catalogs/hadoop-common/src/test/java/org/apache/gravitino/catalog/hadoop/fs/TestHDFSFileSystemProxy.java
@@ -0,0 +1,110 @@
+/*
+ * 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.hadoop.fs;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.ImmutableMap;
+import java.io.File;
+import java.util.Map;
+import org.apache.gravitino.catalog.hadoop.auth.KerberosAuthUtils;
+import org.apache.gravitino.catalog.hadoop.auth.KerberosClient;
+import org.apache.gravitino.catalog.hadoop.fs.kerberos.AuthenticationConfig;
+import org.apache.gravitino.catalog.hadoop.fs.kerberos.KerberosConfig;
+import org.apache.gravitino.exceptions.GravitinoRuntimeException;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+
+/** Tests for {@link HDFSFileSystemProxy} close lifecycle. */
+public class TestHDFSFileSystemProxy {
+
+ private static final Map<String, String> KERBEROS_CONFIG =
+ ImmutableMap.of(
+ AuthenticationConfig.AUTH_TYPE_KEY, "kerberos",
+ KerberosConfig.PRINCIPAL_KEY, "[email protected]",
+ KerberosConfig.KEY_TAB_URI_KEY, "file:///tmp/test.keytab");
+
+ @Test
+ public void testClosingFileSystemStopsKerberosTicketRefresh() throws Exception {
+ KerberosClient client = mockLoggedInClient();
+ try (MockedStatic<KerberosClient> clients = mockStatic(KerberosClient.class);
+ MockedStatic<KerberosAuthUtils> authUtils = mockStatic(KerberosAuthUtils.class)) {
+ stubKerberosLogin(clients, authUtils, client);
+
+ FileSystem fs = new HDFSFileSystemProxy(new Path("file:///"), KERBEROS_CONFIG).getProxy();
+ verify(client, never()).close();
+
+ // Closing the FileSystem stops the ticket refresh that login started, and only once.
+ fs.close();
+ fs.close();
+ verify(client, times(1)).close();
+ }
+ }
+
+ @Test
+ public void testFailedFileSystemCreationAfterLoginStopsKerberosTicketRefresh() throws Exception {
+ KerberosClient client = mockLoggedInClient();
+ try (MockedStatic<KerberosClient> clients = mockStatic(KerberosClient.class);
+ MockedStatic<KerberosAuthUtils> authUtils = mockStatic(KerberosAuthUtils.class)) {
+ stubKerberosLogin(clients, authUtils, client);
+
+ // The login succeeds, then FileSystem.newInstance fails on an unknown scheme.
+ assertThrows(
+ GravitinoRuntimeException.class,
+ () -> new HDFSFileSystemProxy(new Path("nosuchfs:///"), KERBEROS_CONFIG));
+ verify(client, times(1)).close();
+ }
+ }
+
+ private static KerberosClient mockLoggedInClient() throws Exception {
+ KerberosClient client = mock(KerberosClient.class);
+ when(client.login(anyString())).thenReturn(UserGroupInformation.createRemoteUser("test"));
+ return client;
+ }
+
+ private static void stubKerberosLogin(
+ MockedStatic<KerberosClient> clients,
+ MockedStatic<KerberosAuthUtils> authUtils,
+ KerberosClient client) {
+ KerberosClient.Builder builder = mock(KerberosClient.Builder.class, Mockito.RETURNS_SELF);
+ when(builder.build()).thenReturn(client);
+ clients.when(() -> KerberosClient.builder(anyString(), any())).thenReturn(builder);
+ authUtils
+ .when(
+ () ->
+ KerberosAuthUtils.saveKeytabFromUri(
+ anyString(), any(), anyInt(), anyBoolean(), any()))
+ .thenReturn(new File("test.keytab"));
+ }
+}