Skip to content

Commit c48e670

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

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
@@ -323,7 +323,7 @@ public Collection<Object> createComponents(
323323
if (DiscoveryNode.isDataNode(settings)) {
324324
final CacheService cacheService = new CacheService(settings, clusterService, threadPool, new PersistentCache(nodeEnvironment));
325325
this.cacheService.set(cacheService);
326-
final FrozenCacheService frozenCacheService = new FrozenCacheService(environment, threadPool);
326+
final FrozenCacheService frozenCacheService = new FrozenCacheService(nodeEnvironment, settings, threadPool);
327327
this.frozenCacheService.set(frozenCacheService);
328328
components.add(cacheService);
329329
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;
@@ -174,9 +174,8 @@ public Iterator<Setting<?>> settings() {
174174
private final CacheDecayTask decayTask;
175175

176176
@SuppressWarnings({ "unchecked", "rawtypes" })
177-
public FrozenCacheService(Environment environment, ThreadPool threadPool) {
177+
public FrozenCacheService(NodeEnvironment environment, Settings settings, ThreadPool threadPool) {
178178
this.currentTimeSupplier = threadPool::relativeTimeInMillis;
179-
final Settings settings = environment.settings();
180179
final long cacheSize = SNAPSHOT_CACHE_SIZE_SETTING.get(settings).getBytes();
181180
final long regionSize = SNAPSHOT_CACHE_REGION_SIZE_SETTING.get(settings).getBytes();
182181
final int numRegions = Math.toIntExact(cacheSize / regionSize);

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.Set;
2725

2826
import static org.elasticsearch.node.Node.NODE_NAME_SETTING;
@@ -37,11 +35,10 @@ public void testBasicEviction() throws IOException {
3735
.put("path.home", createTempDir())
3836
.build();
3937
final DeterministicTaskQueue taskQueue = new DeterministicTaskQueue(settings, random());
40-
final Environment environment = TestEnvironment.newEnvironment(settings);
41-
for (Path path : environment.dataFiles()) {
42-
Files.createDirectories(path);
43-
}
44-
try (FrozenCacheService cacheService = new FrozenCacheService(environment, taskQueue.getThreadPool())) {
38+
try (
39+
NodeEnvironment environment = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
40+
FrozenCacheService cacheService = new FrozenCacheService(environment, settings, taskQueue.getThreadPool())
41+
) {
4542
final CacheKey cacheKey = generateCacheKey();
4643
assertEquals(5, cacheService.freeRegionCount());
4744
final CacheFileRegion region0 = cacheService.get(cacheKey, 250, 0);
@@ -83,11 +80,10 @@ public void testAutoEviction() throws IOException {
8380
.put("path.home", createTempDir())
8481
.build();
8582
final DeterministicTaskQueue taskQueue = new DeterministicTaskQueue(settings, random());
86-
final Environment environment = TestEnvironment.newEnvironment(settings);
87-
for (Path path : environment.dataFiles()) {
88-
Files.createDirectories(path);
89-
}
90-
try (FrozenCacheService cacheService = new FrozenCacheService(environment, taskQueue.getThreadPool())) {
83+
try (
84+
NodeEnvironment environment = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
85+
FrozenCacheService cacheService = new FrozenCacheService(environment, settings, taskQueue.getThreadPool())
86+
) {
9187
final CacheKey cacheKey = generateCacheKey();
9288
assertEquals(2, cacheService.freeRegionCount());
9389
final CacheFileRegion region0 = cacheService.get(cacheKey, 250, 0);
@@ -120,11 +116,10 @@ public void testForceEviction() throws IOException {
120116
.put("path.home", createTempDir())
121117
.build();
122118
final DeterministicTaskQueue taskQueue = new DeterministicTaskQueue(settings, random());
123-
final Environment environment = TestEnvironment.newEnvironment(settings);
124-
for (Path path : environment.dataFiles()) {
125-
Files.createDirectories(path);
126-
}
127-
try (FrozenCacheService cacheService = new FrozenCacheService(environment, taskQueue.getThreadPool())) {
119+
try (
120+
NodeEnvironment environment = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
121+
FrozenCacheService cacheService = new FrozenCacheService(environment, settings, taskQueue.getThreadPool())
122+
) {
128123
final CacheKey cacheKey1 = generateCacheKey();
129124
final CacheKey cacheKey2 = generateCacheKey();
130125
assertEquals(5, cacheService.freeRegionCount());
@@ -149,11 +144,10 @@ public void testDecay() throws IOException {
149144
.put("path.home", createTempDir())
150145
.build();
151146
final DeterministicTaskQueue taskQueue = new DeterministicTaskQueue(settings, random());
152-
final Environment environment = TestEnvironment.newEnvironment(settings);
153-
for (Path path : environment.dataFiles()) {
154-
Files.createDirectories(path);
155-
}
156-
try (FrozenCacheService cacheService = new FrozenCacheService(environment, taskQueue.getThreadPool())) {
147+
try (
148+
NodeEnvironment environment = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
149+
FrozenCacheService cacheService = new FrozenCacheService(environment, settings, taskQueue.getThreadPool())
150+
) {
157151
final CacheKey cacheKey1 = generateCacheKey();
158152
final CacheKey cacheKey2 = generateCacheKey();
159153
assertEquals(5, cacheService.freeRegionCount());

0 commit comments

Comments
 (0)