mchades commented on code in PR #14407:
URL: https://github.com/apache/gravitino/pull/14407#discussion_r4667042831


##########
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:
   **[P2] Keep reusable keytab paths tied to stable contents**
   
   This move preserves the local symlink. Its name represents the target 
contents at creation, but the running proxy still follows the link on ticket 
renewal. After a normal rotation, the JDK can cache different contents under 
the same staging path, breaking the assumption used when that slot is later 
reused. The current rotation test closes each proxy before changing the source. 
Could live links use a separate reuse policy that preserves renewal and keeps 
the path's cached contents consistent?



-- 
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]

Reply via email to