Skip to content

Commit 2c5c07d

Browse files
authored
Enhancements to IndicesQueryCache. (#39099) (#40446)
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 #37117
1 parent b4b69d2 commit 2c5c07d

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

server/src/main/java/org/elasticsearch/indices/IndicesQueryCache.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import java.io.Closeable;
4444
import java.io.IOException;
45+
import java.util.Collections;
4546
import java.util.HashMap;
4647
import java.util.IdentityHashMap;
4748
import java.util.Map;
@@ -71,7 +72,7 @@ public class IndicesQueryCache implements QueryCache, Closeable {
7172
// This is a hack for the fact that the close listener for the
7273
// ShardCoreKeyMap will be called before onDocIdSetEviction
7374
// See onDocIdSetEviction for more info
74-
private final Map<Object, StatsAndCount> stats2 = new IdentityHashMap<>();
75+
private final Map<Object, StatsAndCount> stats2 = Collections.synchronizedMap(new IdentityHashMap<>());
7576

7677
public IndicesQueryCache(Settings settings) {
7778
final ByteSizeValue size = INDICES_CACHE_QUERY_SIZE_SETTING.get(settings);
@@ -189,20 +190,35 @@ public void close() {
189190
assert shardKeyMap.size() == 0 : shardKeyMap.size();
190191
assert shardStats.isEmpty() : shardStats.keySet();
191192
assert stats2.isEmpty() : stats2;
193+
194+
// This cache stores two things: filters, and doc id sets. At this time
195+
// we only know that there are no more doc id sets, but we still track
196+
// recently used queries, which we want to reclaim.
192197
cache.clear();
193198
}
194199

195200
private static class Stats implements Cloneable {
196201

202+
final ShardId shardId;
197203
volatile long ramBytesUsed;
198204
volatile long hitCount;
199205
volatile long missCount;
200206
volatile long cacheCount;
201207
volatile long cacheSize;
202208

209+
Stats(ShardId shardId) {
210+
this.shardId = shardId;
211+
}
212+
203213
QueryCacheStats toQueryCacheStats() {
204214
return new QueryCacheStats(ramBytesUsed, hitCount, missCount, cacheCount, cacheSize);
205215
}
216+
217+
@Override
218+
public String toString() {
219+
return "{shardId=" + shardId + ", ramBytedUsed=" + ramBytesUsed + ", hitCount=" + hitCount + ", missCount=" + missCount +
220+
", cacheCount=" + cacheCount + ", cacheSize=" + cacheSize + "}";
221+
}
206222
}
207223

208224
private static class StatsAndCount {
@@ -213,6 +229,11 @@ private static class StatsAndCount {
213229
this.stats = stats;
214230
this.count = 0;
215231
}
232+
233+
@Override
234+
public String toString() {
235+
return "{stats=" + stats + " ,count=" + count + "}";
236+
}
216237
}
217238

218239
private boolean empty(Stats stats) {
@@ -249,7 +270,7 @@ private Stats getOrCreateStats(Object coreKey) {
249270
final ShardId shardId = shardKeyMap.getShardId(coreKey);
250271
Stats stats = shardStats.get(shardId);
251272
if (stats == null) {
252-
stats = new Stats();
273+
stats = new Stats(shardId);
253274
shardStats.put(shardId, stats);
254275
}
255276
return stats;
@@ -265,6 +286,7 @@ protected void onClear() {
265286
stats.cacheSize = 0;
266287
stats.ramBytesUsed = 0;
267288
}
289+
stats2.clear();
268290
sharedRamBytesUsed = 0;
269291
}
270292

0 commit comments

Comments
 (0)