Skip to content

Commit 49f7318

Browse files
authored
Create cache files with CREATE_NEW & SPARSE options (#79371) (#79465)
Backport of #79371
1 parent c77b0ea commit 49f7318

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,
@@ -114,9 +116,9 @@ private final class FileChannelReference extends AbstractRefCounted {
114116

115117
private final FileChannel fileChannel;
116118

117-
FileChannelReference() throws IOException {
119+
FileChannelReference(StandardOpenOption[] options) throws IOException {
118120
super("FileChannel[" + file + "]");
119-
this.fileChannel = FileChannel.open(file, OPEN_OPTIONS);
121+
this.fileChannel = FileChannel.open(file, options);
120122
refCounter.incRef();
121123
}
122124

@@ -139,19 +141,26 @@ protected void closeInternal() {
139141
@Nullable
140142
private volatile FileChannelReference channelRef;
141143

144+
/**
145+
* {@code true} if the physical cache file exists on disk
146+
*/
147+
private volatile boolean fileExists;
148+
142149
public CacheFile(CacheKey cacheKey, long length, Path file, ModificationListener listener) {
143-
this(cacheKey, new SparseFileTracker(file.toString(), length), file, listener);
150+
this(cacheKey, new SparseFileTracker(file.toString(), length), file, listener, false);
144151
}
145152

146153
public CacheFile(CacheKey cacheKey, long length, Path file, SortedSet<ByteRange> ranges, ModificationListener listener) {
147-
this(cacheKey, new SparseFileTracker(file.toString(), length, ranges), file, listener);
154+
this(cacheKey, new SparseFileTracker(file.toString(), length, ranges), file, listener, true);
148155
}
149156

150-
private CacheFile(CacheKey cacheKey, SparseFileTracker tracker, Path file, ModificationListener listener) {
157+
private CacheFile(CacheKey cacheKey, SparseFileTracker tracker, Path file, ModificationListener listener, boolean fileExists) {
151158
this.cacheKey = Objects.requireNonNull(cacheKey);
152159
this.tracker = Objects.requireNonNull(tracker);
153160
this.file = Objects.requireNonNull(file);
154161
this.listener = Objects.requireNonNull(listener);
162+
assert fileExists == Files.exists(file) : file + " exists? " + fileExists;
163+
this.fileExists = fileExists;
155164
assert invariant();
156165
}
157166

@@ -197,7 +206,8 @@ public void acquire(final EvictionListener listener) throws IOException {
197206
ensureOpen();
198207
if (listeners.isEmpty()) {
199208
assert channelRef == null;
200-
channelRef = new FileChannelReference();
209+
channelRef = new FileChannelReference(fileExists ? OPEN_OPTIONS : CREATE_OPTIONS);
210+
fileExists = true;
201211
}
202212
final boolean added = listeners.add(listener);
203213
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)