Skip to content

Commit

Permalink
[#1909] Improvement:In HiveCatalogOperations.java it may be better to…
Browse files Browse the repository at this point in the history
… use java.nio.file.Files (#3519)

### What changes were proposed in this pull request?

Change to use java.nio.file.Files#delete.

### Why are the changes needed?

When java.nio.file.Files#delete fails, it returns one of a series of
exception types to better indicate the cause of the failure.
#1909 

### Does this PR introduce _any_ user-facing change?

NO

---------

Co-authored-by: liujh <[email protected]>
  • Loading branch information
liujinhui1994 and liujh authored Jun 3, 2024
1 parent c626d10 commit c8dabb7
Showing 1 changed file with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
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;
Expand Down Expand Up @@ -154,19 +156,23 @@ private void initKerberosIfNecessary(Map<String, String> conf, Configuration had
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 =
Expand All @@ -185,9 +191,11 @@ private void initKerberosIfNecessary(Map<String, String> conf, Configuration had
.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);
Expand All @@ -207,7 +215,8 @@ private void initKerberosIfNecessary(Map<String, String> conf, Configuration had
refreshKerberosConfig();
KerberosName.resetDefaultRealm();
UserGroupInformation.setConfiguration(hadoopConf);
UserGroupInformation.loginUserFromKeytab(catalogPrincipal, keytabFile.getAbsolutePath());
UserGroupInformation.loginUserFromKeytab(
catalogPrincipal, keytabPath.toAbsolutePath().toString());

UserGroupInformation kerberosLoginUgi = UserGroupInformation.getCurrentUser();

Expand Down Expand Up @@ -279,9 +288,13 @@ public void close() {
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);
}
}
}

Expand Down

0 comments on commit c8dabb7

Please sign in to comment.