Skip to content

Possibly better cache strategy for FileSystem #617

@ceclin

Description

@ceclin

// We cache the FileSystem instances as their creation is expensive.
// The Guava Cache is thread-safe (see JavaDoc of LoadingCache) hence this
// cache can be safely shared in a static variable.
protected static final LoadingCache<Path, FileSystem> fileSystemCache =
CacheBuilder.newBuilder()
.removalListener(
(RemovalNotification<Path, FileSystem> removalNotification) -> {
try {
removalNotification.getValue().close();
} catch (IOException e) {
throw new RuntimeException(
"Could not close file system of " + removalNotification.getKey(), e);
}
})
.expireAfterAccess(1, TimeUnit.SECONDS)
.build(
CacheLoader.from(
path -> {
try {
return FileSystems.newFileSystem(
Objects.requireNonNull(path), (ClassLoader) null);
} catch (IOException e) {
throw new RuntimeException("Could not open file system of " + path, e);
}
}));

I think it is better to use reference-based eviction instead of timed eviction, that is CacheBuilder.weakValues().

Firstly, in this way, we can use cached FileSystem in:

// we don't use the filesystem cache here as it could close the filesystem after the timeout
// while we are still iterating
try (FileSystem fs = FileSystems.newFileSystem(path, (ClassLoader) null)) {
final Path archiveRoot = fs.getPath("/");
return walkDirectory(
archiveRoot, view.getProject().getIdentifierFactory(), new AsmJavaClassProvider(view));
} catch (IOException e) {
throw new RuntimeException(e);
}

Secondly, MultiReleaseJarAnalysisInputLocation.discoverInputLocations adds archiveRoot to inputLocations:

baseInputLocations.add(new PathBasedAnalysisInputLocation(archiveRoot, srcType));

This archiveRoot is a DirectoryBasedAnalysisInputLocation. It uses its path's FileSystem in getClassSources, getClassSource and some other methods. If the FileSystem is used after closed (1 second timeout), it will cause a ClosedFileSystemException.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomersimprovementnew feature, improve in readability, structure or performance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions