Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<CacheKey> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void testCacheSynchronization() throws Exception {
logger.trace("--> evicting random cache files");
final Map<CacheFile, Integer> evictions = new HashMap<>();
for (CacheKey evictedCacheKey : randomSubsetOf(Sets.union(previous.keySet(), updates.keySet()))) {
cacheService.removeFromCache(evictedCacheKey::equals);
cacheService.removeFromCache(evictedCacheKey);
Tuple<CacheFile, Integer> evicted = previous.remove(evictedCacheKey);
if (evicted != null) {
evictions.put(evicted.v1(), evicted.v2());
Expand Down