Skip to content

Commit

Permalink
Enhancements to IndicesQueryCache. (elastic#39099)
Browse files Browse the repository at this point in the history
This commit adds the following:
 - made `IndicesQueryCache.stats2` a synchronized map. All writes to it are
   already protected by the lock of the Lucene cache, but the final read from
   an assertion in `IndicesQueryCache#close()` was not so this change should
   avoid any potential visibility issues.
 - human-readable `toString`s to make debugging easier.

Relates elastic#37117
  • Loading branch information
jpountz committed Mar 26, 2019
1 parent 6c337a5 commit fbe909b
Showing 1 changed file with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import java.io.Closeable;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
Expand Down Expand Up @@ -71,7 +72,7 @@ public class IndicesQueryCache implements QueryCache, Closeable {
// This is a hack for the fact that the close listener for the
// ShardCoreKeyMap will be called before onDocIdSetEviction
// See onDocIdSetEviction for more info
private final Map<Object, StatsAndCount> stats2 = new IdentityHashMap<>();
private final Map<Object, StatsAndCount> stats2 = Collections.synchronizedMap(new IdentityHashMap<>());

public IndicesQueryCache(Settings settings) {
final ByteSizeValue size = INDICES_CACHE_QUERY_SIZE_SETTING.get(settings);
Expand Down Expand Up @@ -189,20 +190,35 @@ public void close() {
assert shardKeyMap.size() == 0 : shardKeyMap.size();
assert shardStats.isEmpty() : shardStats.keySet();
assert stats2.isEmpty() : stats2;

// This cache stores two things: filters, and doc id sets. At this time
// we only know that there are no more doc id sets, but we still track
// recently used queries, which we want to reclaim.
cache.clear();
}

private static class Stats implements Cloneable {

final ShardId shardId;
volatile long ramBytesUsed;
volatile long hitCount;
volatile long missCount;
volatile long cacheCount;
volatile long cacheSize;

Stats(ShardId shardId) {
this.shardId = shardId;
}

QueryCacheStats toQueryCacheStats() {
return new QueryCacheStats(ramBytesUsed, hitCount, missCount, cacheCount, cacheSize);
}

@Override
public String toString() {
return "{shardId=" + shardId + ", ramBytedUsed=" + ramBytesUsed + ", hitCount=" + hitCount + ", missCount=" + missCount +
", cacheCount=" + cacheCount + ", cacheSize=" + cacheSize + "}";
}
}

private static class StatsAndCount {
Expand All @@ -213,6 +229,11 @@ private static class StatsAndCount {
this.stats = stats;
this.count = 0;
}

@Override
public String toString() {
return "{stats=" + stats + " ,count=" + count + "}";
}
}

private boolean empty(Stats stats) {
Expand Down Expand Up @@ -249,7 +270,7 @@ private Stats getOrCreateStats(Object coreKey) {
final ShardId shardId = shardKeyMap.getShardId(coreKey);
Stats stats = shardStats.get(shardId);
if (stats == null) {
stats = new Stats();
stats = new Stats(shardId);
shardStats.put(shardId, stats);
}
return stats;
Expand All @@ -265,6 +286,7 @@ protected void onClear() {
stats.cacheSize = 0;
stats.ramBytesUsed = 0;
}
stats2.clear();
sharedRamBytesUsed = 0;
}

Expand Down

0 comments on commit fbe909b

Please sign in to comment.