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


##########
catalogs/catalog-fileset/src/main/java/org/apache/gravitino/catalog/fileset/FilesetCatalogOperations.java:
##########
@@ -1481,17 +1691,188 @@ FileSystem getFileSystemWithCache(Path path, 
Map<String, String> conf) {
     FileSystemProvider provider = fileSystemProvidersMap.get(scheme);
     String authority =
         provider != null ? provider.getFullAuthority(path, conf) : 
path.toUri().getAuthority();
-    return fileSystemCache.get(
-        new FileSystemCacheKey(scheme, authority),
-        cacheKey -> {
-          try {
+    // Resolving the settings and creating the file system share one 
connection timeout.
+    long deadlineNanos =
+        System.nanoTime() + 
TimeUnit.SECONDS.toNanos(connectionTimeoutSeconds(conf));
+    // 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 cachedFileSystem(
+          path,
+          conf,
+          new FileSystemCacheKey(scheme, authority, Collections.emptyMap()),
+          null,
+          deadlineNanos);
+    }
+
+    Map<String, String> hdfsConf = Collections.unmodifiableMap(new 
HashMap<>(conf));

Review Comment:
   Done. The two settings caches are now keyed by a small `ConfKey` that wraps 
the caller's map; the shared cache's key leaves `gravitino.identifier` out of 
`equals` and `hashCode`. A lookup wraps the map as it is, so a hit copies 
nothing: it hashes the map and compares it with the cached key once per 
settings cache it reads, and reads the second only when the settings expand 
Hadoop variables. A copy is made only when a load starts, for the cache key and 
for the file system task.
   
   In a microbenchmark of a settings-cache hit, a 20-entry configuration took 
about 1.3 µs before and 0.33 µs after, and a 60-entry one about 2.8 µs before 
and 0.65 µs after. 
`testFileSystemCacheKeepsLoginSettingsWhenTheCallerChangesItsConfiguration` 
changes the caller's map after a lookup and checks that the next lookup still 
hits.
   



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

Review Comment:
   I'd like to keep these here, since each part answers a problem the cache-key 
change exposes, but I'm happy to split them if you still prefer that. A second 
user's proxy could already rewrite the shared `keytabs/gravitino-<id>`; with 
login settings in the key, so does a fileset's new proxy after its keytab URI 
changes, while the older cached file system still renews from that file. So 
each proxy has its own keytab file, and a shutdown hook hands the open ones to 
`File.deleteOnExit`, since calling it per proxy would keep every name ever used.
   
   `KeytabNames` and the content-based names answer mchades's two comments on 
name reuse. The `AsyncCache` rewrite answers his comment on lookups waiting 
past their deadline behind another lookup's settings resolution or file system 
creation, which this PR put under one timeout. Split out, this PR would ship 
the overwrite and those waits until the follow-ups land.
   



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