Skip to content

Commit 573430f

Browse files
authored
Be more specific when clearing cache for a specified shard (#66011)
Today we evict all cache files that belong to a given searchable snapshot directory when this directory is closed or if the Clear Cache API is used. This eviction iterates over all cache entries and find the entries that matches a given shard. We can be more specific than that by recreating the cache keys to evict from the list of file names instead of iterating over entries. Backport of #66003
1 parent 1bb6740 commit 573430f

File tree

4 files changed

+7
-50
lines changed

4 files changed

+7
-50
lines changed

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/SearchableSnapshotDirectory.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,9 @@ public final void close() {
337337
}
338338

339339
public void clearCache() {
340-
cacheService.removeFromCache(cacheKey -> cacheKey.belongsTo(snapshotId, indexId, shardId));
340+
for (BlobStoreIndexShardSnapshot.FileInfo file : files()) {
341+
cacheService.removeFromCache(createCacheKey(file.physicalName()));
342+
}
341343
}
342344

343345
protected IndexInputStats createIndexInputStats(final long fileLength) {

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/cache/CacheKey.java

-6
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,4 @@ public int hashCode() {
6565
public String toString() {
6666
return "[" + "snapshotId=" + snapshotId + ", indexId=" + indexId + ", shardId=" + shardId + ", fileName='" + fileName + "']";
6767
}
68-
69-
public boolean belongsTo(SnapshotId snapshotId, IndexId indexId, ShardId shardId) {
70-
return Objects.equals(this.snapshotId, snapshotId)
71-
&& Objects.equals(this.indexId, indexId)
72-
&& Objects.equals(this.shardId, shardId);
73-
}
7468
}

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

+4-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.nio.file.Files;
2323
import java.nio.file.Path;
2424
import java.util.Objects;
25-
import java.util.function.Predicate;
2625

2726
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotsConstants.toIntBytes;
2827

@@ -129,16 +128,11 @@ public CacheFile get(final CacheKey cacheKey, final long fileLength, final Path
129128
}
130129

131130
/**
132-
* Invalidate cache entries with keys matching the given predicate
131+
* Evicts the cache file associated with the specified cache key.
133132
*
134-
* @param predicate the predicate to evaluate
133+
* @param cacheKey the {@link CacheKey} whose associated {@link CacheFile} must be evicted from cache
135134
*/
136-
public void removeFromCache(final Predicate<CacheKey> predicate) {
137-
for (CacheKey cacheKey : cache.keys()) {
138-
if (predicate.test(cacheKey)) {
139-
cache.invalidate(cacheKey);
140-
}
141-
}
142-
cache.refresh();
135+
public void removeFromCache(final CacheKey cacheKey) {
136+
cache.invalidate(cacheKey);
143137
}
144138
}

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

-33
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,12 @@
1212
import org.elasticsearch.test.ESTestCase;
1313
import org.elasticsearch.test.EqualsHashCodeTestUtils;
1414

15-
import static org.hamcrest.Matchers.equalTo;
16-
1715
public class CacheKeyTests extends ESTestCase {
1816

1917
public void testEqualsAndHashCode() {
2018
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createInstance(), this::copy, this::mutate);
2119
}
2220

23-
public void testBelongsTo() {
24-
final CacheKey cacheKey = createInstance();
25-
26-
SnapshotId snapshotId = cacheKey.getSnapshotId();
27-
IndexId indexId = cacheKey.getIndexId();
28-
ShardId shardId = cacheKey.getShardId();
29-
30-
final boolean belongsTo;
31-
switch (randomInt(2)) {
32-
case 0:
33-
snapshotId = randomValueOtherThan(cacheKey.getSnapshotId(), this::randomSnapshotId);
34-
belongsTo = false;
35-
break;
36-
case 1:
37-
indexId = randomValueOtherThan(cacheKey.getIndexId(), this::randomIndexId);
38-
belongsTo = false;
39-
break;
40-
case 2:
41-
shardId = randomValueOtherThan(cacheKey.getShardId(), this::randomShardId);
42-
belongsTo = false;
43-
break;
44-
case 3:
45-
belongsTo = true;
46-
break;
47-
default:
48-
throw new AssertionError("Unsupported value");
49-
}
50-
51-
assertThat(cacheKey.belongsTo(snapshotId, indexId, shardId), equalTo(belongsTo));
52-
}
53-
5421
private SnapshotId randomSnapshotId() {
5522
return new SnapshotId(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10));
5623
}

0 commit comments

Comments
 (0)