LuciferYang commented on code in PR #14407:
URL: https://github.com/apache/gravitino/pull/14407#discussion_r4675872494
##########
catalogs/hadoop-common/src/main/java/org/apache/gravitino/catalog/hadoop/fs/HDFSFileSystemProxy.java:
##########
@@ -182,6 +340,108 @@ private synchronized void close() {
}
kerberosClient = null;
}
+ if (keytabFile != null) {
+ try {
+ Files.deleteIfExists(keytabFile.toPath());
+ LIVE_KEYTAB_FILES.remove(keytabFile);
+ releaseKeytabSlot();
+ } 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.
+ private synchronized File moveToKeytabName(String identifier, String
keytabUri, File fetched)
+ throws IOException {
+ 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);
Review Comment:
Done. A proxy records the keytab's modification time, through the link, when
it names the file. On close, if the time changed, renewals may have read other
keys through that name, so its slot is retired instead of released and the name
is not reused in this JVM. Renewals still follow the link, and an unchanged
keytab keeps its name for the next proxy, also after a catalog reload.
`testNameOfALinkReplacedWhileItsProxyWasOpenIsNotReused` keeps a proxy open,
rotates the source to KVNO 2 with a new mtime, reads it through the link as a
renewal would, writes KVNO 1 back with that mtime and closes the proxy; the
next proxy gets another name and reads KVNO 1. With the slot released, it
reused the name and read KVNO 2.
--
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]