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


##########
catalogs/hadoop-common/src/main/java/org/apache/gravitino/catalog/hadoop/fs/HDFSFileSystemProxy.java:
##########
@@ -182,6 +246,83 @@ private synchronized void close() {
       }
       kerberosClient = null;
     }
+    if (keytabFile != null) {
+      try {
+        Files.deleteIfExists(keytabFile.toPath());
+        LIVE_KEYTAB_FILES.remove(keytabFile);
+      } 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.
+        try {
+          keytabFile.deleteOnExit();
+        } catch (RuntimeException ex) {
+          LOG.warn("Failed to delete the keytab file {} on exit", keytabFile, 
ex);
+        }
+      }
+      keytabFile = null;
+    }
+  }
+
+  private synchronized void recordKeytabFile(File file) {
+    if (keytabFile != null) {
+      LIVE_KEYTAB_FILES.remove(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) {
+          LOG.warn("The JVM is shutting down, {} is not deleted on exit", 
file);
+        }
+      }
+    }
+    LIVE_KEYTAB_FILES.add(file);

Review Comment:
   Done. The cleaner now sets `keytabFilesHandedOver` and hands the set over 
while holding the `LIVE_KEYTAB_FILES` lock. `recordKeytabFile` adds the file 
and reads the flag under the same lock, so a keytab recorded after the cleaner 
ran goes straight to `File.deleteOnExit()`. When `addShutdownHook()` throws 
`IllegalStateException`, the proxy runs the same handover itself, which also 
sets the flag, so its own file and every later one are registered. 
`testKeytabFileRecordedAfterTheCleanerRanIsDeletedOnExit` runs the cleaner, 
opens a second proxy and checks that its keytab is registered with 
`DeleteOnExitHook`. It fails if the cleaner does not set the flag or the 
recorder does not read it. 
`testKeytabFilesAreDeletedOnExitWhenTheCleanerCannotBeAdded` makes 
`addShutdownHook()` throw while another proxy is open, then opens two more, and 
checks that all three keytabs are registered. It fails if the proxy skips the 
handover.
   



##########
catalogs/hadoop-common/src/main/java/org/apache/gravitino/catalog/hadoop/fs/HDFSFileSystemProxy.java:
##########
@@ -182,6 +245,69 @@ private synchronized void close() {
       }
       kerberosClient = null;
     }
+    if (keytabFile != null) {
+      try {
+        Files.deleteIfExists(keytabFile.toPath());
+      } catch (IOException | RuntimeException e) {
+        LOG.warn("Failed to delete the keytab file {}", keytabFile, e);
+      }
+      LIVE_KEYTAB_FILES.remove(keytabFile);

Review Comment:
   Done. `close()` now removes the file from `LIVE_KEYTAB_FILES` only after 
`deleteIfExists()` succeeds, so the shutdown hook still sees it. On the server 
the hook alone is not enough, because it is removed together with the catalog 
class loader, so a failed delete also calls `File.deleteOnExit()`. 
`testKeytabFileThatFailsToDeleteOnCloseIsDeletedOnExit` makes the directory 
read-only while the proxy closes, then checks that the file stays in the set 
and is registered for deletion at exit.
   



##########
catalogs/hadoop-common/src/main/java/org/apache/gravitino/catalog/hadoop/fs/HDFSFileSystemProxy.java:
##########
@@ -182,6 +245,69 @@ private synchronized void close() {
       }
       kerberosClient = null;
     }
+    if (keytabFile != null) {
+      try {
+        Files.deleteIfExists(keytabFile.toPath());
+      } catch (IOException | RuntimeException e) {
+        LOG.warn("Failed to delete the keytab file {}", keytabFile, e);
+      }
+      LIVE_KEYTAB_FILES.remove(keytabFile);

Review Comment:
   Fixed together with mchades's comment on the same line. 
`LIVE_KEYTAB_FILES.remove(keytabFile)` now runs only after `deleteIfExists()` 
succeeds, and a failed delete also registers `File.deleteOnExit()`, because on 
the server the hook goes away with the catalog class loader. 
`testKeytabFileThatFailsToDeleteOnCloseIsDeletedOnExit` fails the delete with a 
read-only directory, then checks that the file is still tracked and registered 
with `DeleteOnExitHook`. The file is only deleted at JVM exit, so the test 
checks the registration rather than the deletion; 
`testKeytabFilesOfOpenProxiesAreDeletedOnExit` covers the hook's handover.
   



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