42
42
43
43
import java .io .Closeable ;
44
44
import java .io .IOException ;
45
+ import java .util .Collections ;
45
46
import java .util .HashMap ;
46
47
import java .util .IdentityHashMap ;
47
48
import java .util .Map ;
@@ -71,7 +72,7 @@ public class IndicesQueryCache implements QueryCache, Closeable {
71
72
// This is a hack for the fact that the close listener for the
72
73
// ShardCoreKeyMap will be called before onDocIdSetEviction
73
74
// 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 <>() );
75
76
76
77
public IndicesQueryCache (Settings settings ) {
77
78
final ByteSizeValue size = INDICES_CACHE_QUERY_SIZE_SETTING .get (settings );
@@ -189,20 +190,35 @@ public void close() {
189
190
assert shardKeyMap .size () == 0 : shardKeyMap .size ();
190
191
assert shardStats .isEmpty () : shardStats .keySet ();
191
192
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.
192
197
cache .clear ();
193
198
}
194
199
195
200
private static class Stats implements Cloneable {
196
201
202
+ final ShardId shardId ;
197
203
volatile long ramBytesUsed ;
198
204
volatile long hitCount ;
199
205
volatile long missCount ;
200
206
volatile long cacheCount ;
201
207
volatile long cacheSize ;
202
208
209
+ Stats (ShardId shardId ) {
210
+ this .shardId = shardId ;
211
+ }
212
+
203
213
QueryCacheStats toQueryCacheStats () {
204
214
return new QueryCacheStats (ramBytesUsed , hitCount , missCount , cacheCount , cacheSize );
205
215
}
216
+
217
+ @ Override
218
+ public String toString () {
219
+ return "{shardId=" + shardId + ", ramBytedUsed=" + ramBytesUsed + ", hitCount=" + hitCount + ", missCount=" + missCount +
220
+ ", cacheCount=" + cacheCount + ", cacheSize=" + cacheSize + "}" ;
221
+ }
206
222
}
207
223
208
224
private static class StatsAndCount {
@@ -213,6 +229,11 @@ private static class StatsAndCount {
213
229
this .stats = stats ;
214
230
this .count = 0 ;
215
231
}
232
+
233
+ @ Override
234
+ public String toString () {
235
+ return "{stats=" + stats + " ,count=" + count + "}" ;
236
+ }
216
237
}
217
238
218
239
private boolean empty (Stats stats ) {
@@ -249,7 +270,7 @@ private Stats getOrCreateStats(Object coreKey) {
249
270
final ShardId shardId = shardKeyMap .getShardId (coreKey );
250
271
Stats stats = shardStats .get (shardId );
251
272
if (stats == null ) {
252
- stats = new Stats ();
273
+ stats = new Stats (shardId );
253
274
shardStats .put (shardId , stats );
254
275
}
255
276
return stats ;
@@ -265,6 +286,7 @@ protected void onClear() {
265
286
stats .cacheSize = 0 ;
266
287
stats .ramBytesUsed = 0 ;
267
288
}
289
+ stats2 .clear ();
268
290
sharedRamBytesUsed = 0 ;
269
291
}
270
292
0 commit comments