mchades commented on code in PR #14407:
URL: https://github.com/apache/gravitino/pull/14407#discussion_r4675987538
##########
catalogs/hadoop-common/src/main/java/org/apache/gravitino/catalog/hadoop/fs/HDFSFileSystemProxy.java:
##########
@@ -182,6 +343,130 @@ private synchronized void close() {
}
kerberosClient = null;
}
+ if (keytabFile != null) {
+ try {
+ boolean unchanged = keytabUnchanged();
+ Files.deleteIfExists(keytabFile.toPath());
+ LIVE_KEYTAB_FILES.remove(keytabFile);
+ if (unchanged) {
+ releaseKeytabSlot();
+ } else {
+ // A local keytab changed while renewals read it through the link,
so the JDK may keep
+ // other keys for this name. It is not reused.
+ keytabSlotPrefix = null;
+ }
+ } catch (IOException | RuntimeException e) {
+ LOG.warn("Failed to delete the keytab file {}", keytabFile, e);
+ // Deleted at exit instead. The shutdown hook would hand it over too,
but a catalog class
+ // loader removes that hook when it is closed. Only failed deletes get
here.
+ deleteOnExit(keytabFile);
+ // Its name stays taken while the file may still be there.
+ keytabSlotPrefix = null;
+ }
+ keytabFile = null;
+ }
+ }
+
+ private synchronized void recordKeytabFile(File file) {
+ File previous = keytabFile;
+ keytabFile = file;
+ if (KEYTAB_CLEANER.get() == null) {
+ Thread cleaner =
+ new Thread(HDFSFileSystemProxy::deleteLiveKeytabFilesOnExit,
"gravitino-keytab-cleaner");
+ // A catalog class loader removes, when it is closed, the hooks whose
context class loader is
+ // its own.
+
cleaner.setContextClassLoader(HDFSFileSystemProxy.class.getClassLoader());
+ if (KEYTAB_CLEANER.compareAndSet(null, cleaner)) {
+ try {
+ Runtime.getRuntime().addShutdownHook(cleaner);
+ } catch (IllegalStateException e) {
+ // The JVM is shutting down, so no hook will hand the files over.
+ deleteLiveKeytabFilesOnExit();
+ }
+ }
+ }
+ // The hook sets the flag and goes through the set under the same
LIVE_KEYTAB_FILES lock, so
+ // either the hook sees the file or this method sees the flag.
+ synchronized (LIVE_KEYTAB_FILES) {
+ if (previous != null) {
+ LIVE_KEYTAB_FILES.remove(previous);
+ }
+ LIVE_KEYTAB_FILES.add(file);
+ if (keytabFilesHandedOver) {
+ deleteOnExit(file);
+ }
+ }
+ }
+
+ // Takes the lowest free name for the identifier, the keytab URI and its
content, and moves the
+ // fetched file there. See KeytabNames for when a name is reused, and
close() for when it is not.
+ private synchronized File moveToKeytabName(String identifier, String
keytabUri, File fetched)
+ throws IOException {
+ // Read first, so that a change while the content is read is seen on close
too.
+ keytabModified = Files.getLastModifiedTime(fetched.toPath());
+ String prefix = keytabNamePrefix(identifier, keytabUri, fetched);
+ int slot = KeytabNames.reserve(prefix);
+ keytabSlotPrefix = prefix;
+ keytabSlot = slot;
+ File file =
+ new File(
+ fetched.getParentFile(),
+ new File(String.format(GRAVITINO_KEYTAB_FORMAT, prefix + "-" +
slot)).getName());
+ // Moves a symbolic link itself, so the file still follows a local keytab.
+ Files.move(fetched.toPath(), file.toPath(),
StandardCopyOption.REPLACE_EXISTING);
+ recordKeytabFile(file);
+ return file;
+ }
+
+ private static String keytabNamePrefix(String identifier, String keytabUri,
File fetched)
+ throws IOException {
+ if (Files.isRegularFile(fetched.toPath())) {
+ try (InputStream in = Files.newInputStream(fetched.toPath())) {
+ byte[] content = ByteStreams.toByteArray(ByteStreams.limit(in,
MAX_KEYTAB_NAME_BYTES + 1));
+ if (content.length <= MAX_KEYTAB_NAME_BYTES) {
+ return KeytabNames.prefix(identifier, keytabUri, content);
+ }
+ }
+ }
+ return identifier + "-" + KeytabNames.token() + "-" + UUID.randomUUID();
+ }
+
+ // Whether the keytab, which may be a link to a local file, has kept its
modification time since
+ // it was named. Otherwise the JDK may have read other keys through the name.
+ private synchronized boolean keytabUnchanged() {
+ if (keytabModified == null) {
+ return true;
+ }
+ try {
+ return
keytabModified.equals(Files.getLastModifiedTime(keytabFile.toPath()));
Review Comment:
**[P2] Keep keytab cleanup independent of the source mount**
`getLastModifiedTime()` follows the staging symlink for a `file://` keytab.
If the source is on an unresponsive mount, this metadata read can block
`close()` before it removes the local staging link.
`FilesetCatalogOperations.close()` closes completed cache entries inline, so
this can also stall cleanup of the other file systems. Can the close path avoid
waiting on the keytab source?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]