Skip to content

Commit 4ed68f8

Browse files
Fix Shared Snapshot Cache not Created in Data Path (#70362) (#70369)
We have to use the node environment instead of the plain environment here to get the actual data paths.
1 parent 40d53d1 commit 4ed68f8

File tree

6 files changed

+35
-42
lines changed

6 files changed

+35
-42
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public Collection<Object> createComponents(
331331
if (DiscoveryNode.isDataNode(settings)) {
332332
final CacheService cacheService = new CacheService(settings, clusterService, threadPool, new PersistentCache(nodeEnvironment));
333333
this.cacheService.set(cacheService);
334-
final FrozenCacheService frozenCacheService = new FrozenCacheService(environment, threadPool);
334+
final FrozenCacheService frozenCacheService = new FrozenCacheService(nodeEnvironment, settings, threadPool);
335335
this.frozenCacheService.set(frozenCacheService);
336336
components.add(cacheService);
337337
final BlobStoreCacheService blobStoreCacheService = new BlobStoreCacheService(

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
3131
import org.elasticsearch.common.util.concurrent.KeyedLock;
3232
import org.elasticsearch.core.internal.io.IOUtils;
33-
import org.elasticsearch.env.Environment;
33+
import org.elasticsearch.env.NodeEnvironment;
3434
import org.elasticsearch.index.shard.ShardId;
3535
import org.elasticsearch.index.store.cache.CacheKey;
3636
import org.elasticsearch.index.store.cache.SparseFileTracker;
@@ -175,9 +175,8 @@ public Iterator<Setting<?>> settings() {
175175
private final CacheDecayTask decayTask;
176176

177177
@SuppressWarnings({ "unchecked", "rawtypes" })
178-
public FrozenCacheService(Environment environment, ThreadPool threadPool) {
178+
public FrozenCacheService(NodeEnvironment environment, Settings settings, ThreadPool threadPool) {
179179
this.currentTimeSupplier = threadPool::relativeTimeInMillis;
180-
final Settings settings = environment.settings();
181180
long cacheSize = SNAPSHOT_CACHE_SIZE_SETTING.get(settings).getBytes();
182181
if (cacheSize == Long.MAX_VALUE) {
183182
cacheSize = 0L;

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
1717
import org.elasticsearch.core.internal.io.IOUtils;
1818
import org.elasticsearch.env.Environment;
19+
import org.elasticsearch.env.NodeEnvironment;
1920
import org.elasticsearch.xpack.searchablesnapshots.preallocate.Preallocate;
2021

2122
import java.io.IOException;
@@ -46,7 +47,7 @@ public class SharedBytes extends AbstractRefCounted {
4647

4748
private final Path path;
4849

49-
SharedBytes(int numRegions, long regionSize, Environment environment) throws IOException {
50+
SharedBytes(int numRegions, long regionSize, NodeEnvironment environment) throws IOException {
5051
super("shared-bytes");
5152
this.numRegions = numRegions;
5253
this.regionSize = regionSize;
@@ -81,7 +82,7 @@ public class SharedBytes extends AbstractRefCounted {
8182
}
8283
} else {
8384
this.fileChannel = null;
84-
for (Path path : environment.dataFiles()) {
85+
for (Path path : environment.nodeDataPaths()) {
8586
Files.deleteIfExists(path.resolve(CACHE_FILE_NAME));
8687
}
8788
}
@@ -94,9 +95,9 @@ public class SharedBytes extends AbstractRefCounted {
9495
* @return path for the cache file or {@code null} if none could be found
9596
*/
9697
@Nullable
97-
public static Path findCacheSnapshotCacheFilePath(Environment environment, long fileSize) throws IOException {
98+
public static Path findCacheSnapshotCacheFilePath(NodeEnvironment environment, long fileSize) throws IOException {
9899
Path cacheFile = null;
99-
for (Path path : environment.dataFiles()) {
100+
for (Path path : environment.nodeDataPaths()) {
100101
Files.createDirectories(path);
101102
// TODO: be resilient to this check failing and try next path?
102103
long usableSpace = Environment.getUsableSpace(path);

x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/index/store/cache/FrozenIndexInputTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.common.settings.Settings;
1515
import org.elasticsearch.common.unit.ByteSizeValue;
1616
import org.elasticsearch.env.Environment;
17+
import org.elasticsearch.env.NodeEnvironment;
1718
import org.elasticsearch.env.TestEnvironment;
1819
import org.elasticsearch.index.Index;
1920
import org.elasticsearch.index.shard.ShardId;
@@ -95,9 +96,9 @@ public void testRandomReads() throws IOException {
9596
for (Path path : environment.dataFiles()) {
9697
Files.createDirectories(path);
9798
}
98-
9999
try (
100-
FrozenCacheService cacheService = new FrozenCacheService(environment, threadPool);
100+
NodeEnvironment nodeEnvironment = new NodeEnvironment(settings, environment);
101+
FrozenCacheService cacheService = new FrozenCacheService(nodeEnvironment, settings, threadPool);
101102
TestSearchableSnapshotDirectory directory = new TestSearchableSnapshotDirectory(cacheService, tempDir, fileInfo, fileData)
102103
) {
103104
directory.loadSnapshot(createRecoveryState(true), ActionListener.wrap(() -> {}));

x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/AbstractSearchableSnapshotsTestCase.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public void setUpTest() throws Exception {
103103
threadPool = new TestThreadPool(getTestName(), SearchableSnapshots.executorBuilders());
104104
clusterService = ClusterServiceUtils.createClusterService(threadPool, node, CLUSTER_SETTINGS);
105105
nodeEnvironment = newNodeEnvironment();
106-
environment = newEnvironment();
107106
}
108107

109108
@After
@@ -146,7 +145,7 @@ protected CacheService randomCacheService() {
146145
* @return a new {@link FrozenCacheService} instance configured with default settings
147146
*/
148147
protected FrozenCacheService defaultFrozenCacheService() {
149-
return new FrozenCacheService(environment, threadPool);
148+
return new FrozenCacheService(nodeEnvironment, Settings.EMPTY, threadPool);
150149
}
151150

152151
protected FrozenCacheService randomFrozenCacheService() {
@@ -163,7 +162,7 @@ protected FrozenCacheService randomFrozenCacheService() {
163162
if (randomBoolean()) {
164163
cacheSettings.put(FrozenCacheService.FROZEN_CACHE_RECOVERY_RANGE_SIZE_SETTING.getKey(), randomCacheRangeSize());
165164
}
166-
return new FrozenCacheService(newEnvironment(cacheSettings.build()), threadPool);
165+
return new FrozenCacheService(nodeEnvironment, cacheSettings.build(), threadPool);
167166
}
168167

169168
/**
@@ -183,12 +182,11 @@ protected CacheService createCacheService(final ByteSizeValue cacheSize, final B
183182

184183
protected FrozenCacheService createFrozenCacheService(final ByteSizeValue cacheSize, final ByteSizeValue cacheRangeSize) {
185184
return new FrozenCacheService(
186-
newEnvironment(
187-
Settings.builder()
188-
.put(FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), cacheSize)
189-
.put(FrozenCacheService.SHARED_CACHE_RANGE_SIZE_SETTING.getKey(), cacheRangeSize)
190-
.build()
191-
),
185+
nodeEnvironment,
186+
Settings.builder()
187+
.put(FrozenCacheService.SNAPSHOT_CACHE_SIZE_SETTING.getKey(), cacheSize)
188+
.put(FrozenCacheService.SHARED_CACHE_RANGE_SIZE_SETTING.getKey(), cacheRangeSize)
189+
.build(),
192190
threadPool
193191
);
194192
}

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

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.elasticsearch.cluster.coordination.DeterministicTaskQueue;
1111
import org.elasticsearch.cluster.node.DiscoveryNode;
1212
import org.elasticsearch.common.settings.Settings;
13-
import org.elasticsearch.env.Environment;
13+
import org.elasticsearch.env.NodeEnvironment;
1414
import org.elasticsearch.env.TestEnvironment;
1515
import org.elasticsearch.index.shard.ShardId;
1616
import org.elasticsearch.index.store.cache.CacheKey;
@@ -21,8 +21,6 @@
2121
import org.elasticsearch.xpack.searchablesnapshots.cache.FrozenCacheService.CacheFileRegion;
2222

2323
import java.io.IOException;
24-
import java.nio.file.Files;
25-
import java.nio.file.Path;
2624
import java.util.Arrays;
2725
import java.util.HashSet;
2826

@@ -38,11 +36,10 @@ public void testBasicEviction() throws IOException {
3836
.put("path.home", createTempDir())
3937
.build();
4038
final DeterministicTaskQueue taskQueue = new DeterministicTaskQueue(settings, random());
41-
final Environment environment = TestEnvironment.newEnvironment(settings);
42-
for (Path path : environment.dataFiles()) {
43-
Files.createDirectories(path);
44-
}
45-
try (FrozenCacheService cacheService = new FrozenCacheService(environment, taskQueue.getThreadPool())) {
39+
try (
40+
NodeEnvironment environment = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
41+
FrozenCacheService cacheService = new FrozenCacheService(environment, settings, taskQueue.getThreadPool())
42+
) {
4643
final CacheKey cacheKey = generateCacheKey();
4744
assertEquals(5, cacheService.freeRegionCount());
4845
final CacheFileRegion region0 = cacheService.get(cacheKey, 250, 0);
@@ -84,11 +81,10 @@ public void testAutoEviction() throws IOException {
8481
.put("path.home", createTempDir())
8582
.build();
8683
final DeterministicTaskQueue taskQueue = new DeterministicTaskQueue(settings, random());
87-
final Environment environment = TestEnvironment.newEnvironment(settings);
88-
for (Path path : environment.dataFiles()) {
89-
Files.createDirectories(path);
90-
}
91-
try (FrozenCacheService cacheService = new FrozenCacheService(environment, taskQueue.getThreadPool())) {
84+
try (
85+
NodeEnvironment environment = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
86+
FrozenCacheService cacheService = new FrozenCacheService(environment, settings, taskQueue.getThreadPool())
87+
) {
9288
final CacheKey cacheKey = generateCacheKey();
9389
assertEquals(2, cacheService.freeRegionCount());
9490
final CacheFileRegion region0 = cacheService.get(cacheKey, 250, 0);
@@ -121,11 +117,10 @@ public void testForceEviction() throws IOException {
121117
.put("path.home", createTempDir())
122118
.build();
123119
final DeterministicTaskQueue taskQueue = new DeterministicTaskQueue(settings, random());
124-
final Environment environment = TestEnvironment.newEnvironment(settings);
125-
for (Path path : environment.dataFiles()) {
126-
Files.createDirectories(path);
127-
}
128-
try (FrozenCacheService cacheService = new FrozenCacheService(environment, taskQueue.getThreadPool())) {
120+
try (
121+
NodeEnvironment environment = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
122+
FrozenCacheService cacheService = new FrozenCacheService(environment, settings, taskQueue.getThreadPool())
123+
) {
129124
final CacheKey cacheKey1 = generateCacheKey();
130125
final CacheKey cacheKey2 = generateCacheKey();
131126
assertEquals(5, cacheService.freeRegionCount());
@@ -150,11 +145,10 @@ public void testDecay() throws IOException {
150145
.put("path.home", createTempDir())
151146
.build();
152147
final DeterministicTaskQueue taskQueue = new DeterministicTaskQueue(settings, random());
153-
final Environment environment = TestEnvironment.newEnvironment(settings);
154-
for (Path path : environment.dataFiles()) {
155-
Files.createDirectories(path);
156-
}
157-
try (FrozenCacheService cacheService = new FrozenCacheService(environment, taskQueue.getThreadPool())) {
148+
try (
149+
NodeEnvironment environment = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
150+
FrozenCacheService cacheService = new FrozenCacheService(environment, settings, taskQueue.getThreadPool())
151+
) {
158152
final CacheKey cacheKey1 = generateCacheKey();
159153
final CacheKey cacheKey2 = generateCacheKey();
160154
assertEquals(5, cacheService.freeRegionCount());

0 commit comments

Comments
 (0)