Skip to content

Commit f2a4957

Browse files
authored
Remove file field from CacheFileReference (#51520)
`CacheFileReference#file` is a path to a file that doesn't exist, for use as a cache key, but whose parent directory is the location of the actual cache file. This commit replaces it with the path to the cache directory itself, and computes the cache key when it is needed.
1 parent 714c480 commit f2a4957

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/CacheDirectory.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,11 @@ private class CacheFileReference implements CacheFile.EvictionListener {
5959

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

65-
private CacheFileReference(String fileName, long fileLength, Path file) {
64+
private CacheFileReference(String fileName, long fileLength) {
6665
this.fileName = fileName;
6766
this.fileLength = fileLength;
68-
this.file = file;
6967
}
7068

7169
@Nullable
@@ -75,7 +73,7 @@ CacheFile get() throws Exception {
7573
return currentCacheFile;
7674
}
7775

78-
final CacheFile newCacheFile = cacheService.get(fileName, fileLength, file);
76+
final CacheFile newCacheFile = cacheService.get(fileName, fileLength, cacheDir);
7977
synchronized (this) {
8078
currentCacheFile = cacheFile.get();
8179
if (currentCacheFile != null) {
@@ -117,7 +115,7 @@ public String toString() {
117115
return "CacheFileReference{" +
118116
"fileName='" + fileName + '\'' +
119117
", fileLength=" + fileLength +
120-
", file=" + file +
118+
", cacheDir=" + cacheDir +
121119
", acquired=" + (cacheFile.get() != null) +
122120
'}';
123121
}
@@ -135,7 +133,7 @@ public class CacheBufferedIndexInput extends BufferedIndexInput {
135133
private boolean isClone;
136134

137135
CacheBufferedIndexInput(String fileName, long fileLength, IOContext ioContext) {
138-
this(new CacheFileReference(fileName, fileLength, cacheDir.resolve(fileName)), ioContext,
136+
this(new CacheFileReference(fileName, fileLength), ioContext,
139137
"CachedBufferedIndexInput(" + fileName + ")", 0L, fileLength, false);
140138
}
141139

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/CacheFile.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ protected void closeInternal() {
5555
private final Path file;
5656

5757
private volatile Set<EvictionListener> listeners;
58-
private volatile FileChannel channel;
5958
private volatile boolean evicted;
6059

60+
@Nullable // if evicted, or there are no listeners
61+
private volatile FileChannel channel;
62+
6163
CacheFile(String name, long length, Path file) {
6264
this.tracker = new SparseFileTracker(file.toString(), length);
6365
this.name = Objects.requireNonNull(name);

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/CacheService.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ private void ensureLifecycleStarted() {
6464
}
6565
}
6666

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

77-
return new CacheFile(name, length, path);
77+
return new CacheFile(fileName, length, path);
7878
});
7979
}
8080

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

9595
/**
9696
* Computes the cache key associated to the given Lucene cached file
97-
*
98-
* @param cacheFile the cached file
99-
* @return the cache key
10097
*/
101-
private static String toCacheKey(final Path cacheFile) { // TODO Fix this. Cache Key should be computed from snapshot id/index id/shard
102-
return cacheFile.toAbsolutePath().toString();
98+
private static String toCacheKey(final Path cacheDir, String fileName) {
99+
// TODO Fix this. Cache Key should be computed from snapshot id/index id/shard
100+
return cacheDir.resolve(fileName).toAbsolutePath().toString();
103101
}
104102
}

0 commit comments

Comments
 (0)