diff --git a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/SearchableSnapshotDirectory.java b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/SearchableSnapshotDirectory.java index acf506cec7bee..06f7a89e228ef 100644 --- a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/SearchableSnapshotDirectory.java +++ b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/SearchableSnapshotDirectory.java @@ -337,7 +337,9 @@ public final void close() { } public void clearCache() { - cacheService.removeFromCache(cacheKey -> cacheKey.belongsTo(snapshotId, indexId, shardId)); + for (BlobStoreIndexShardSnapshot.FileInfo file : files()) { + cacheService.removeFromCache(createCacheKey(file.physicalName())); + } } protected IndexInputStats createIndexInputStats(final long fileLength) { diff --git a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/cache/CacheKey.java b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/cache/CacheKey.java index ba4b4c315c2ef..5433fa06b6d54 100644 --- a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/cache/CacheKey.java +++ b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/cache/CacheKey.java @@ -65,10 +65,4 @@ public int hashCode() { public String toString() { return "[" + "snapshotId=" + snapshotId + ", indexId=" + indexId + ", shardId=" + shardId + ", fileName='" + fileName + "']"; } - - public boolean belongsTo(SnapshotId snapshotId, IndexId indexId, ShardId shardId) { - return Objects.equals(this.snapshotId, snapshotId) - && Objects.equals(this.indexId, indexId) - && Objects.equals(this.shardId, shardId); - } } diff --git a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/CacheService.java b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/CacheService.java index c35a7bfcfff63..b3bdaafd96cc0 100644 --- a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/CacheService.java +++ b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/CacheService.java @@ -40,7 +40,6 @@ import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.locks.ReentrantLock; -import java.util.function.Predicate; import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotsConstants.toIntBytes; @@ -270,17 +269,12 @@ void put( } /** - * Invalidate cache entries with keys matching the given predicate + * Evicts the cache file associated with the specified cache key. * - * @param predicate the predicate to evaluate + * @param cacheKey the {@link CacheKey} whose associated {@link CacheFile} must be evicted from cache */ - public void removeFromCache(final Predicate predicate) { - for (CacheKey cacheKey : cache.keys()) { - if (predicate.test(cacheKey)) { - cache.invalidate(cacheKey); - } - } - cache.refresh(); + public void removeFromCache(final CacheKey cacheKey) { + cache.invalidate(cacheKey); } void setCacheSyncInterval(TimeValue interval) { diff --git a/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/index/store/cache/CacheKeyTests.java b/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/index/store/cache/CacheKeyTests.java index af18dda3d441d..6c136f0abf9be 100644 --- a/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/index/store/cache/CacheKeyTests.java +++ b/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/index/store/cache/CacheKeyTests.java @@ -12,45 +12,12 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; -import static org.hamcrest.Matchers.equalTo; - public class CacheKeyTests extends ESTestCase { public void testEqualsAndHashCode() { EqualsHashCodeTestUtils.checkEqualsAndHashCode(createInstance(), this::copy, this::mutate); } - public void testBelongsTo() { - final CacheKey cacheKey = createInstance(); - - SnapshotId snapshotId = cacheKey.getSnapshotId(); - IndexId indexId = cacheKey.getIndexId(); - ShardId shardId = cacheKey.getShardId(); - - final boolean belongsTo; - switch (randomInt(2)) { - case 0: - snapshotId = randomValueOtherThan(cacheKey.getSnapshotId(), this::randomSnapshotId); - belongsTo = false; - break; - case 1: - indexId = randomValueOtherThan(cacheKey.getIndexId(), this::randomIndexId); - belongsTo = false; - break; - case 2: - shardId = randomValueOtherThan(cacheKey.getShardId(), this::randomShardId); - belongsTo = false; - break; - case 3: - belongsTo = true; - break; - default: - throw new AssertionError("Unsupported value"); - } - - assertThat(cacheKey.belongsTo(snapshotId, indexId, shardId), equalTo(belongsTo)); - } - private SnapshotId randomSnapshotId() { return new SnapshotId(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10)); } diff --git a/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/cache/CacheServiceTests.java b/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/cache/CacheServiceTests.java index f95b58b880971..c0e6b66a12120 100644 --- a/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/cache/CacheServiceTests.java +++ b/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/cache/CacheServiceTests.java @@ -121,7 +121,7 @@ public void testCacheSynchronization() throws Exception { logger.trace("--> evicting random cache files"); final Map evictions = new HashMap<>(); for (CacheKey evictedCacheKey : randomSubsetOf(Sets.union(previous.keySet(), updates.keySet()))) { - cacheService.removeFromCache(evictedCacheKey::equals); + cacheService.removeFromCache(evictedCacheKey); Tuple evicted = previous.remove(evictedCacheKey); if (evicted != null) { evictions.put(evicted.v1(), evicted.v2());