Skip to content

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

[#1909] Improvement:In HiveCatalogOperations.java it may be better to use java.nio.file.Files (#3519)

liujinhuic8dabb745e7c
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
index 7f882cccab..c9cbb6d19b 100644
--- a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
@@ -48,10 +48,12 @@ import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
-import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.Method;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
@@ -154,19 +156,23 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
if (UserGroupInformation.AuthenticationMethod.KERBEROS
== SecurityUtil.getAuthenticationMethod(hadoopConf)) {
try {
- File keytabsDir = new File("keytabs");
- if (!keytabsDir.exists()) {
+ Path keytabsPath = Paths.get("keytabs");
+ if (!Files.exists(keytabsPath)) {
// Ignore the return value, because there exists many Hive catalog operations making
// this directory.
- keytabsDir.mkdir();
+ Files.createDirectory(keytabsPath);
}
// The id of entity is a random unique id.
- File keytabFile = new File(String.format(GRAVITINO_KEYTAB_FORMAT, info.id()));
- keytabFile.deleteOnExit();
- if (keytabFile.exists() && !keytabFile.delete()) {
- throw new IllegalStateException(
- String.format("Fail to delete keytab file %s", keytabFile.getAbsolutePath()));
+ Path keytabPath = Paths.get(String.format(GRAVITINO_KEYTAB_FORMAT, info.id()));
+ keytabPath.toFile().deleteOnExit();
+ if (Files.exists(keytabPath)) {
+ try {
+ Files.delete(keytabPath);
+ } catch (IOException e) {
+ throw new IllegalStateException(
+ String.format("Fail to delete keytab file %s", keytabPath.toAbsolutePath()), e);
+ }
}
String keytabUri =
@@ -185,9 +191,11 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
.catalogPropertiesMetadata()
.getOrDefault(conf, HiveCatalogPropertiesMeta.FETCH_TIMEOUT_SEC);
- FetchFileUtils.fetchFileFromUri(keytabUri, keytabFile, fetchKeytabFileTimeout, hadoopConf);
+ FetchFileUtils.fetchFileFromUri(
+ keytabUri, keytabPath.toFile(), fetchKeytabFileTimeout, hadoopConf);
- hiveConf.setVar(ConfVars.METASTORE_KERBEROS_KEYTAB_FILE, keytabFile.getAbsolutePath());
+ hiveConf.setVar(
+ ConfVars.METASTORE_KERBEROS_KEYTAB_FILE, keytabPath.toAbsolutePath().toString());
String catalogPrincipal =
(String) propertiesMetadata.catalogPropertiesMetadata().getOrDefault(conf, PRINCIPAL);
@@ -207,7 +215,8 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
refreshKerberosConfig();
KerberosName.resetDefaultRealm();
UserGroupInformation.setConfiguration(hadoopConf);
- UserGroupInformation.loginUserFromKeytab(catalogPrincipal, keytabFile.getAbsolutePath());
+ UserGroupInformation.loginUserFromKeytab(
+ catalogPrincipal, keytabPath.toAbsolutePath().toString());
UserGroupInformation kerberosLoginUgi = UserGroupInformation.getCurrentUser();
@@ -279,9 +288,13 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
checkTgtExecutor = null;
}
- File keytabFile = new File(String.format(GRAVITINO_KEYTAB_FORMAT, info.id()));
- if (keytabFile.exists() && !keytabFile.delete()) {
- LOG.error("Fail to delete key tab file {}", keytabFile.getAbsolutePath());
+ Path keytabPath = Paths.get(String.format(GRAVITINO_KEYTAB_FORMAT, info.id()));
+ if (Files.exists(keytabPath)) {
+ try {
+ Files.delete(keytabPath);
+ } catch (IOException e) {
+ LOG.error("Fail to delete key tab file {}", keytabPath.toAbsolutePath(), e);
+ }
}
}