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


##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/FilesetCatalogOperations.java:
##########
@@ -1481,17 +1572,179 @@ FileSystem getFileSystemWithCache(Path path, 
Map<String, String> conf) {
     FileSystemProvider provider = fileSystemProvidersMap.get(scheme);
     String authority =
         provider != null ? provider.getFullAuthority(path, conf) : 
path.toUri().getAuthority();
+    // Only an HDFS file system logs in with the settings of the 
configuration, see
+    // createFileSystem.
+    String fsScheme = scheme != null ? scheme : 
defaultFileSystemProvider.scheme();
+    if (!SCHEME_HDFS.equals(fsScheme)) {
+      return fileSystemCache.get(
+          new FileSystemCacheKey(scheme, authority, Collections.emptyMap()),
+          cacheKey -> newFileSystem(path, conf, cacheKey, null));
+    }
+
+    Map<String, String> hdfsConf = Collections.unmodifiableMap(new 
HashMap<>(conf));
+    // Resolving the settings and creating the file system share one 
connection timeout.
+    long deadlineNanos =
+        System.nanoTime() + 
TimeUnit.SECONDS.toNanos(connectionTimeoutSeconds(hdfsConf));
+    FileSystem fs = getHdfsFileSystemWithCache(path, hdfsConf, scheme, 
authority, deadlineNanos);
+    if (fs == null) {
+      // The config.resources files changed after their login settings were 
cached.
+      sharedHdfsLoginSettingsCache.invalidate(withoutIdentifier(hdfsConf));
+      hdfsLoginSettingsCache.invalidate(hdfsConf);
+      fs = getHdfsFileSystemWithCache(path, hdfsConf, scheme, authority, 
deadlineNanos);
+    }
+    if (fs == null) {
+      throw new GravitinoRuntimeException(
+          "The login settings in config.resources changed while getting 
FileSystem for fileset:"
+              + " path: %s",
+          path);
+    }
+    return fs;
+  }
+
+  // Returns null if the config.resources files no longer resolve to the 
cached login settings: a
+  // file system is cached only under the settings of the configuration it is 
created with.
+  @Nullable
+  private FileSystem getHdfsFileSystemWithCache(
+      Path path,
+      Map<String, String> conf,
+      @Nullable String scheme,
+      @Nullable String authority,
+      long deadlineNanos) {
+    Map<String, String> settings = hdfsLoginSettings(path, conf, 
deadlineNanos);
+    // A null result caches nothing; the cache statistics count it as a failed 
load.
     return fileSystemCache.get(
-        new FileSystemCacheKey(scheme, authority),
-        cacheKey -> {
-          try {
-            // Create the file system for the user it is cached under.
-            return getFileSystem(path, conf, cacheKey.currentUser);
-          } catch (IOException e) {
-            throw new GravitinoRuntimeException(
-                e, "Failed to get FileSystem for fileset: path: %s", path);
-          }
-        });
+        new FileSystemCacheKey(scheme, authority, settings),
+        cacheKey ->
+            newFileSystem(
+                path, conf, cacheKey, new 
ExpectedLogin(cacheKey.loginSettings, deadlineNanos)));
+  }
+
+  private Map<String, String> hdfsLoginSettings(
+      Path path, Map<String, String> conf, long deadlineNanos) {
+    // Settings resolved for the shared entry that expand variables, cached 
for this configuration
+    // after the shared cache's load returns.
+    AtomicReference<Map<String, String>> resolved = new AtomicReference<>();
+    Optional<Map<String, String>> shared =
+        sharedHdfsLoginSettingsCache.get(

Review Comment:
   **[P2] Apply each caller's deadline when waiting for a cache load**
   
   The loader's worker has a timeout, but another caller can block inside this 
synchronous `Cache.get()` before it reaches a timed wait. In a concurrent test, 
a 1-second request whose settings-cache bucket was occupied timed out only 
after 1.36 seconds. A 1-second request waiting on the same `FileSystem` cache 
key also returned successfully after 1.26 seconds. Could each caller wait for 
the in-flight result using its own remaining deadline?



##########
catalogs/hadoop-common/src/main/java/org/apache/gravitino/catalog/hadoop/fs/HDFSFileSystemProxy.java:
##########
@@ -182,6 +341,107 @@ 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 and keytab URI. A name is 
only reused for the
+  // same keytab URI, as the JDK may return the keys it cached for the path 
when the file it reads
+  // there has the same modification time.
+  private synchronized File reserveKeytabFile(String identifier, String 
keytabUri) {
+    String prefix =
+        identifier
+            + "-"
+            + JVM_KEYTAB_TOKEN
+            + "-"
+            + 
UUID.nameUUIDFromBytes(keytabUri.getBytes(StandardCharsets.UTF_8));

Review Comment:
   **[P2] Use a fresh keytab path when the credential changes**
   
   A keytab URI can stay the same across rotations. In a Java 17 probe, 
replacing a `file://` keytab with KVNO 2 at the same URI and mtime still 
returned KVNO 1 on this reused staging path. The previous revision's unique 
path read KVNO 2. The JDK also fell back to the old key when the replacement 
keytab was invalid, even with a new mtime. Can we make sure a changed keytab 
gets a fresh path or is reparsed by the JDK?



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