Skip to content

Commit c772d36

Browse files
committed
Create cache files with CREATE_NEW & SPARSE options (elastic#79371)
* Create cache files with CREATE_NEW & SPARSE options * assert + doc * rename
1 parent ffc61a2 commit c772d36

File tree

2 files changed

+20
-9
lines changed
  • x-pack/plugin/searchable-snapshots/src
    • main/java/org/elasticsearch/xpack/searchablesnapshots/cache/common
    • test/java/org/elasticsearch/xpack/searchablesnapshots/cache/common

2 files changed

+20
-9
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ public interface ModificationListener {
5353
void onCacheFileDelete(CacheFile cacheFile);
5454
}
5555

56-
private static final StandardOpenOption[] OPEN_OPTIONS = new StandardOpenOption[] {
56+
private static final StandardOpenOption[] CREATE_OPTIONS = new StandardOpenOption[] {
5757
StandardOpenOption.READ,
5858
StandardOpenOption.WRITE,
59-
StandardOpenOption.CREATE,
59+
StandardOpenOption.CREATE_NEW,
6060
StandardOpenOption.SPARSE };
6161

62+
private static final StandardOpenOption[] OPEN_OPTIONS = new StandardOpenOption[] { StandardOpenOption.READ, StandardOpenOption.WRITE };
63+
6264
/**
6365
* Reference counter that counts the number of eviction listeners referencing this cache file plus the number of open file channels
6466
* for it. Once this instance has been evicted, all listeners notified and all {@link FileChannelReference} for it released,
@@ -100,8 +102,8 @@ private final class FileChannelReference extends AbstractRefCounted {
100102

101103
private final FileChannel fileChannel;
102104

103-
FileChannelReference() throws IOException {
104-
this.fileChannel = FileChannel.open(file, OPEN_OPTIONS);
105+
FileChannelReference(StandardOpenOption[] options) throws IOException {
106+
this.fileChannel = FileChannel.open(file, options);
105107
refCounter.incRef();
106108
}
107109

@@ -124,19 +126,26 @@ protected void closeInternal() {
124126
@Nullable
125127
private volatile FileChannelReference channelRef;
126128

129+
/**
130+
* {@code true} if the physical cache file exists on disk
131+
*/
132+
private volatile boolean fileExists;
133+
127134
public CacheFile(CacheKey cacheKey, long length, Path file, ModificationListener listener) {
128-
this(cacheKey, new SparseFileTracker(file.toString(), length), file, listener);
135+
this(cacheKey, new SparseFileTracker(file.toString(), length), file, listener, false);
129136
}
130137

131138
public CacheFile(CacheKey cacheKey, long length, Path file, SortedSet<ByteRange> ranges, ModificationListener listener) {
132-
this(cacheKey, new SparseFileTracker(file.toString(), length, ranges), file, listener);
139+
this(cacheKey, new SparseFileTracker(file.toString(), length, ranges), file, listener, true);
133140
}
134141

135-
private CacheFile(CacheKey cacheKey, SparseFileTracker tracker, Path file, ModificationListener listener) {
142+
private CacheFile(CacheKey cacheKey, SparseFileTracker tracker, Path file, ModificationListener listener, boolean fileExists) {
136143
this.cacheKey = Objects.requireNonNull(cacheKey);
137144
this.tracker = Objects.requireNonNull(tracker);
138145
this.file = Objects.requireNonNull(file);
139146
this.listener = Objects.requireNonNull(listener);
147+
assert fileExists == Files.exists(file) : file + " exists? " + fileExists;
148+
this.fileExists = fileExists;
140149
assert invariant();
141150
}
142151

@@ -182,7 +191,8 @@ public void acquire(final EvictionListener listener) throws IOException {
182191
ensureOpen();
183192
if (listeners.isEmpty()) {
184193
assert channelRef == null;
185-
channelRef = new FileChannelReference();
194+
channelRef = new FileChannelReference(fileExists ? OPEN_OPTIONS : CREATE_OPTIONS);
195+
fileExists = true;
186196
}
187197
final boolean added = listeners.add(listener);
188198
assert added : "listener already exists " + listener;

x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/cache/common/CacheFileTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ public void onCacheFileDelete(CacheFile cacheFile) {}
5858
private static final CacheKey CACHE_KEY = new CacheKey("_snap_uuid", "_snap_index", new ShardId("_name", "_uuid", 0), "_filename");
5959

6060
public void testGetCacheKey() throws Exception {
61+
final Path file = createTempDir().resolve("file.new");
6162
final CacheKey cacheKey = new CacheKey(
6263
UUIDs.randomBase64UUID(random()),
6364
randomAlphaOfLength(5).toLowerCase(Locale.ROOT),
6465
new ShardId(randomAlphaOfLength(5).toLowerCase(Locale.ROOT), UUIDs.randomBase64UUID(random()), randomInt(5)),
6566
randomAlphaOfLength(105).toLowerCase(Locale.ROOT)
6667
);
6768

68-
final CacheFile cacheFile = new CacheFile(cacheKey, randomLongBetween(1, 100), createTempFile(), NOOP);
69+
final CacheFile cacheFile = new CacheFile(cacheKey, randomLongBetween(1, 100), file, NOOP);
6970
assertThat(cacheFile.getCacheKey(), sameInstance(cacheKey));
7071
}
7172

0 commit comments

Comments
 (0)