@@ -69,13 +69,11 @@ public class IndicesQueryCache implements QueryCache, Closeable {
6969 private final Map <ShardId , Stats > shardStats = new ConcurrentHashMap <>();
7070 private volatile long sharedRamBytesUsed ;
7171
72- // Package-private for IndicesService efficient stats collection
7372 public long getCacheSizeForShard (ShardId shardId ) {
7473 Stats stats = shardStats .get (shardId );
7574 return stats != null ? stats .cacheSize : 0L ;
7675 }
7776
78- // Package-private for IndicesService efficient stats collection
7977 public long getSharedRamBytesUsed () {
8078 return sharedRamBytesUsed ;
8179 }
@@ -102,24 +100,11 @@ private static QueryCacheStats toQueryCacheStatsSafe(@Nullable Stats stats) {
102100 return stats == null ? new QueryCacheStats () : stats .toQueryCacheStats ();
103101 }
104102
105- /** Get usage statistics for the given shard. */
106- public QueryCacheStats getStats (ShardId shard , long precomputedSharedRamBytesUsed ) {
107- final QueryCacheStats queryCacheStats = toQueryCacheStatsSafe (shardStats .get (shard ));
108- queryCacheStats .addRamBytesUsed (precomputedSharedRamBytesUsed );
109- return queryCacheStats ;
110- }
111-
112- @ Override
113- public Weight doCache (Weight weight , QueryCachingPolicy policy ) {
114- while (weight instanceof CachingWeightWrapper ) {
115- weight = ((CachingWeightWrapper ) weight ).in ;
116- }
117- final Weight in = cache .doCache (weight , policy );
118- // We wrap the weight to track the readers it sees and map them with
119- // the shards they belong to
120- return new CachingWeightWrapper (in );
121- }
122-
103+ /**
104+ * This computes the total cache size in bytes, and the total shard count in the cache for all shards.
105+ * @param indicesService
106+ * @return A CacheTotals object containing the computed total cache size and shard count
107+ */
123108 public static CacheTotals getCacheTotalsForAllShards (IndicesService indicesService ) {
124109 IndicesQueryCache queryCache = indicesService .getIndicesQueryCache ();
125110 boolean hasQueryCache = queryCache != null ;
@@ -129,17 +114,22 @@ public static CacheTotals getCacheTotalsForAllShards(IndicesService indicesServi
129114 for (final IndexShard indexShard : indexService ) {
130115 long cacheSize = hasQueryCache ? queryCache .getCacheSizeForShard (indexShard .shardId ()) : 0L ;
131116 shardCount ++;
132- if (cacheSize > 0L ) {
133- totalSize += cacheSize ;
134- }
117+ assert cacheSize >= 0 : "Unexpected cache size of " + cacheSize + " for shard " + indexShard .shardId ();
118+ totalSize += cacheSize ;
135119 }
136120 }
137- long sharedRamBytesUsed = hasQueryCache ? queryCache .getSharedRamBytesUsed () : 0L ;
138- return new CacheTotals (totalSize , shardCount , sharedRamBytesUsed );
121+ return new CacheTotals (totalSize , shardCount );
139122 }
140123
124+ /**
125+ * This method computes the shared RAM size in bytes for the given indexShard.
126+ * @param queryCache
127+ * @param indexShard The shard to compute the shared RAM size for
128+ * @param cacheTotals Shard totals computed in getCacheTotalsForAllShards()
129+ * @return
130+ */
141131 public static long getSharedRamSizeForShard (IndicesQueryCache queryCache , IndexShard indexShard , CacheTotals cacheTotals ) {
142- long sharedRamBytesUsed = cacheTotals . sharedRamBytesUsed () ;
132+ long sharedRamBytesUsed = queryCache != null ? queryCache . getSharedRamBytesUsed () : 0L ;
143133 if (sharedRamBytesUsed == 0L ) {
144134 return 0L ;
145135 }
@@ -152,7 +142,7 @@ public static long getSharedRamSizeForShard(IndicesQueryCache queryCache, IndexS
152142 }
153143
154144 long totalSize = cacheTotals .totalSize ();
155- long cacheSize = queryCache != null ? queryCache .getCacheSizeForShard (indexShard .shardId ()) : 0L ;
145+ long cacheSize = queryCache .getCacheSizeForShard (indexShard .shardId ());
156146 final long additionalRamBytesUsed ;
157147 if (totalSize == 0 ) {
158148 // all shards have zero cache footprint, so we apportion the size of the shared bytes equally across all shards
@@ -165,7 +155,25 @@ public static long getSharedRamSizeForShard(IndicesQueryCache queryCache, IndexS
165155 return additionalRamBytesUsed ;
166156 }
167157
168- public record CacheTotals (long totalSize , int shardCount , long sharedRamBytesUsed ) {}
158+ public record CacheTotals (long totalSize , int shardCount ) {}
159+
160+ /** Get usage statistics for the given shard. */
161+ public QueryCacheStats getStats (ShardId shard , long precomputedSharedRamBytesUsed ) {
162+ final QueryCacheStats queryCacheStats = toQueryCacheStatsSafe (shardStats .get (shard ));
163+ queryCacheStats .addRamBytesUsed (precomputedSharedRamBytesUsed );
164+ return queryCacheStats ;
165+ }
166+
167+ @ Override
168+ public Weight doCache (Weight weight , QueryCachingPolicy policy ) {
169+ while (weight instanceof CachingWeightWrapper ) {
170+ weight = ((CachingWeightWrapper ) weight ).in ;
171+ }
172+ final Weight in = cache .doCache (weight , policy );
173+ // We wrap the weight to track the readers it sees and map them with
174+ // the shards they belong to
175+ return new CachingWeightWrapper (in );
176+ }
169177
170178 private class CachingWeightWrapper extends Weight {
171179
0 commit comments