Skip to content

Remove file field from CacheFileReference #51520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@ private class CacheFileReference implements CacheFile.EvictionListener {

private final String fileName;
private final long fileLength;
private final Path file;
private final AtomicReference<CacheFile> cacheFile = new AtomicReference<>(); // null if evicted or not yet acquired

private CacheFileReference(String fileName, long fileLength, Path file) {
private CacheFileReference(String fileName, long fileLength) {
this.fileName = fileName;
this.fileLength = fileLength;
this.file = file;
}

@Nullable
Expand All @@ -75,7 +73,7 @@ CacheFile get() throws Exception {
return currentCacheFile;
}

final CacheFile newCacheFile = cacheService.get(fileName, fileLength, file);
final CacheFile newCacheFile = cacheService.get(fileName, fileLength, cacheDir);
synchronized (this) {
currentCacheFile = cacheFile.get();
if (currentCacheFile != null) {
Expand Down Expand Up @@ -117,7 +115,7 @@ public String toString() {
return "CacheFileReference{" +
"fileName='" + fileName + '\'' +
", fileLength=" + fileLength +
", file=" + file +
", cacheDir=" + cacheDir +
", acquired=" + (cacheFile.get() != null) +
'}';
}
Expand All @@ -135,7 +133,7 @@ public class CacheBufferedIndexInput extends BufferedIndexInput {
private boolean isClone;

CacheBufferedIndexInput(String fileName, long fileLength, IOContext ioContext) {
this(new CacheFileReference(fileName, fileLength, cacheDir.resolve(fileName)), ioContext,
this(new CacheFileReference(fileName, fileLength), ioContext,
"CachedBufferedIndexInput(" + fileName + ")", 0L, fileLength, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ protected void closeInternal() {
private final Path file;

private volatile Set<EvictionListener> listeners;
private volatile FileChannel channel;
private volatile boolean evicted;

@Nullable // if evicted, or there are no listeners
private volatile FileChannel channel;

CacheFile(String name, long length, Path file) {
this.tracker = new SparseFileTracker(file.toString(), length);
this.name = Objects.requireNonNull(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ private void ensureLifecycleStarted() {
}
}

public CacheFile get(final String name, final long length, final Path file) throws Exception {
public CacheFile get(final String fileName, final long length, final Path cacheDir) throws Exception {
ensureLifecycleStarted();
return cache.computeIfAbsent(toCacheKey(file), key -> {
return cache.computeIfAbsent(toCacheKey(cacheDir, fileName), key -> {
ensureLifecycleStarted();
// generate a random UUID for the name of the cache file on disk
final String uuid = UUIDs.randomBase64UUID();
// resolve the cache file on disk w/ the expected cached file
final Path path = file.getParent().resolve(uuid);
final Path path = cacheDir.resolve(uuid);
assert Files.notExists(path) : "cache file already exists " + path;

return new CacheFile(name, length, path);
return new CacheFile(fileName, length, path);
});
}

Expand All @@ -94,11 +94,9 @@ public void removeFromCache(final Predicate<String> predicate) {

/**
* Computes the cache key associated to the given Lucene cached file
*
* @param cacheFile the cached file
* @return the cache key
*/
private static String toCacheKey(final Path cacheFile) { // TODO Fix this. Cache Key should be computed from snapshot id/index id/shard
return cacheFile.toAbsolutePath().toString();
private static String toCacheKey(final Path cacheDir, String fileName) {
// TODO Fix this. Cache Key should be computed from snapshot id/index id/shard
return cacheDir.resolve(fileName).toAbsolutePath().toString();
}
}