Skip to content

Enhancements to IndicesQueryCache. (#39099) #40446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2019
Merged
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 @@ -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