PyRSA commented on code in PR #11037:
URL: https://github.com/apache/paimon/pull/11037#discussion_r4533099521


##########
paimon-filesystems/paimon-s3-impl/src/main/java/org/apache/paimon/s3/S3FileIO.java:
##########
@@ -115,34 +125,162 @@ private Options mirrorCertainHadoopConfig(Options 
hadoopConfig) {
         return hadoopConfig;
     }
 
+    /**
+     * Overrides parent to refresh {@link #fsMap} when a cached {@link 
RecoverableS3AFileSystem} has
+     * been invalidated. Avoids introducing a generic health hook on {@link 
HadoopCompliantFileIO}.
+     */
+    @Override
+    protected FileSystem getFileSystem(org.apache.hadoop.fs.Path path) throws 
IOException {
+        if (fsMap == null) {
+            synchronized (this) {
+                if (fsMap == null) {
+                    fsMap = new ConcurrentHashMap<>();
+                }
+            }
+        }
+
+        final Map<Pair<String, String>, FileSystem> map = fsMap;
+        URI uri = path.toUri();
+        Pair<String, String> key = Pair.of(uri.getScheme(), 
uri.getAuthority());
+
+        // Create outside fsMap's ConcurrentHashMap compute lock.
+        while (true) {
+            FileSystem current = map.get(key);
+            if (isUsableFileSystem(current)) {
+                return current;
+            }
+
+            FileSystem created = createFileSystem(path);
+            if (!isUsableFileSystem(created)) {
+                continue;
+            }
+
+            if (current == null) {
+                FileSystem existing = map.putIfAbsent(key, created);
+                if (existing == null) {
+                    if (isUsableFileSystem(created)) {
+                        return created;
+                    }
+                    map.remove(key, created);
+                    continue;
+                }
+                if (isUsableFileSystem(existing)) {
+                    return existing;
+                }
+                current = existing;
+            }
+
+            if (isUsableFileSystem(current)) {
+                return current;
+            }
+            if (!map.replace(key, current, created)) {
+                continue;
+            }
+            if (isUsableFileSystem(created)) {
+                return created;
+            }
+            map.remove(key, created);
+        }
+    }
+
     @Override
-    protected FileSystem createFileSystem(org.apache.hadoop.fs.Path path) {
+    protected FileSystem createFileSystem(org.apache.hadoop.fs.Path path) 
throws IOException {
         final String scheme = path.toUri().getScheme();
         final String authority = path.toUri().getAuthority();
-        return CACHE.computeIfAbsent(
-                new CacheKey(hadoopOptions, scheme, authority),
-                key -> {
-                    Configuration hadoopConf = new Configuration();
-                    key.options.toMap().forEach(hadoopConf::set);
-                    URI fsUri = path.toUri();
-                    if (scheme == null && authority == null) {
-                        fsUri = FileSystem.getDefaultUri(hadoopConf);
-                    } else if (scheme != null && authority == null) {
-                        URI defaultUri = FileSystem.getDefaultUri(hadoopConf);
-                        if (scheme.equals(defaultUri.getScheme())
-                                && defaultUri.getAuthority() != null) {
-                            fsUri = defaultUri;
-                        }
-                    }
+        final CacheKey cacheKey = new CacheKey(hadoopOptions, scheme, 
authority);
 
-                    S3AFileSystem fs = new S3AFileSystem();
-                    try {
-                        fs.initialize(fsUri, hadoopConf);
-                    } catch (IOException e) {
-                        throw new UncheckedIOException(e);
-                    }
-                    return fs;
-                });
+        S3AFileSystem cached = CACHE.get(cacheKey);
+        if (isUsableFileSystem(cached)) {
+            return cached;
+        }
+
+        Object lock = getCacheLock(cacheKey);
+        synchronized (lock) {
+            cached = CACHE.get(cacheKey);
+            if (isUsableFileSystem(cached)) {
+                return cached;
+            }
+
+            // Only this confirmed mapping may be removed if initialize fails.
+            final S3AFileSystem expectedOld = cached;
+
+            Configuration hadoopConf = new Configuration();
+            cacheKey.options.toMap().forEach(hadoopConf::set);
+            URI fsUri = path.toUri();
+            if (scheme == null && authority == null) {
+                fsUri = FileSystem.getDefaultUri(hadoopConf);
+            } else if (scheme != null && authority == null) {
+                URI defaultUri = FileSystem.getDefaultUri(hadoopConf);
+                if (scheme.equals(defaultUri.getScheme()) && 
defaultUri.getAuthority() != null) {
+                    fsUri = defaultUri;
+                }
+            }
+
+            RecoverableS3AFileSystem fs = newRecoverableFileSystem();
+            fs.bind(() -> CACHE.remove(cacheKey, fs));
+            try {
+                fs.initialize(fsUri, hadoopConf);
+                if (!fs.isUsable()) {
+                    throw new IOException("S3A filesystem became unusable 
during initialize");
+                }
+            } catch (IOException | RuntimeException | Error e) {
+                discardFailedCreate(cacheKey, expectedOld, fs);
+                throw e;
+            }
+
+            CACHE.put(cacheKey, fs);
+            if (!fs.isUsable()) {
+                CACHE.remove(cacheKey, fs);
+                try {
+                    fs.close();

Review Comment:
   Fixed. After publishing the filesystem to the shared cache, the 
post-publication health check now evicts the mapping without closing the 
filesystem. This avoids closing an instance that may already have been borrowed 
by another caller. Unpublished instances are still closed when initialization 
fails.
   
   Added a latch-based regression test to reproduce the race and verify that 
the borrowed filesystem is not closed.



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