Skip to content

Commit 1d77b9d

Browse files
committed
moving code into reusable methods
1 parent 34659a8 commit 1d77b9d

File tree

4 files changed

+50
-84
lines changed

4 files changed

+50
-84
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/stats/TransportClusterStatsAction.java

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import org.elasticsearch.index.seqno.RetentionLeaseStats;
4949
import org.elasticsearch.index.seqno.SeqNoStats;
5050
import org.elasticsearch.index.shard.IndexShard;
51-
import org.elasticsearch.index.shard.ShardId;
5251
import org.elasticsearch.indices.IndicesQueryCache;
5352
import org.elasticsearch.indices.IndicesService;
5453
import org.elasticsearch.injection.guice.Inject;
@@ -261,37 +260,12 @@ protected ClusterStatsNodeResponse nodeOperation(ClusterStatsNodeRequest nodeReq
261260
);
262261

263262
IndicesQueryCache queryCache = indicesService.getIndicesQueryCache();
264-
boolean hasQueryCache = queryCache != null;
265-
// First pass: gather all shards, cache sizes, and compute totals
266-
long totalSize = 0L;
267-
int shardCount = 0;
268-
boolean anyNonZero = false;
269-
// First pass: compute totals only
270-
for (final IndexService indexService : indicesService) {
271-
for (final IndexShard indexShard : indexService) {
272-
long cacheSize = hasQueryCache ? queryCache.getCacheSizeForShard(indexShard.shardId()) : 0L;
273-
shardCount++;
274-
if (cacheSize > 0L) {
275-
anyNonZero = true;
276-
totalSize += cacheSize;
277-
}
278-
}
279-
}
280-
long sharedRamBytesUsed = hasQueryCache ? queryCache.getSharedRamBytesUsed() : 0L;
263+
IndicesQueryCache.CacheTotals cacheTotals = IndicesQueryCache.getCacheTotalsForAllShards(indicesService);
281264

282265
List<ShardStats> shardsStats = new ArrayList<>();
283266
for (IndexService indexService : indicesService) {
284267
for (IndexShard indexShard : indexService) {
285-
ShardId shardId = indexShard.shardId();
286-
long cacheSize = hasQueryCache ? queryCache.getCacheSizeForShard(shardId) : 0L;
287-
long sharedRam = 0L;
288-
if (sharedRamBytesUsed != 0L) {
289-
if (anyNonZero == false) {
290-
sharedRam = Math.round((double) sharedRamBytesUsed / shardCount);
291-
} else if (totalSize != 0) {
292-
sharedRam = Math.round((double) sharedRamBytesUsed * cacheSize / totalSize);
293-
}
294-
}
268+
long sharedRam = IndicesQueryCache.getSharedRamSize(queryCache, indexShard, cacheTotals);
295269
cancellableTask.ensureNotCancelled();
296270
if (indexShard.routingEntry() != null && indexShard.routingEntry().active()) {
297271
// only report on fully started shards

server/src/main/java/org/elasticsearch/action/admin/indices/stats/TransportIndicesStatsAction.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -115,37 +115,13 @@ protected void shardOperation(IndicesStatsRequest request, ShardRouting shardRou
115115
ActionListener.completeWith(listener, () -> {
116116
assert task instanceof CancellableTask;
117117
IndicesQueryCache queryCache = indicesService.getIndicesQueryCache();
118-
boolean hasQueryCache = queryCache != null;
119-
120-
// First pass: gather all shards, cache sizes, and compute totals
121-
long totalSize = 0L;
122-
int shardCount = 0;
123-
boolean anyNonZero = false;
124-
// First pass: compute totals only
125-
for (final IndexService indexService : indicesService) {
126-
for (final IndexShard indexShard : indexService) {
127-
long cacheSize = hasQueryCache ? queryCache.getCacheSizeForShard(indexShard.shardId()) : 0L;
128-
shardCount++;
129-
if (cacheSize > 0L) {
130-
anyNonZero = true;
131-
totalSize += cacheSize;
132-
}
133-
}
134-
}
135-
long sharedRamBytesUsed = hasQueryCache ? queryCache.getSharedRamBytesUsed() : 0L;
118+
119+
IndicesQueryCache.CacheTotals cacheTotals = IndicesQueryCache.getCacheTotalsForAllShards(indicesService);
136120

137121
IndexService indexService = indicesService.indexServiceSafe(shardRouting.shardId().getIndex());
138122
IndexShard indexShard = indexService.getShard(shardRouting.shardId().id());
139123
ShardId shardId = indexShard.shardId();
140-
long cacheSize = hasQueryCache ? queryCache.getCacheSizeForShard(shardId) : 0L;
141-
long sharedRam = 0L;
142-
if (sharedRamBytesUsed != 0L) {
143-
if (anyNonZero == false) {
144-
sharedRam = Math.round((double) sharedRamBytesUsed / shardCount);
145-
} else if (totalSize != 0) {
146-
sharedRam = Math.round((double) sharedRamBytesUsed * cacheSize / totalSize);
147-
}
148-
}
124+
long sharedRam = IndicesQueryCache.getSharedRamSize(queryCache, indexShard, cacheTotals);
149125

150126
CommonStats commonStats = CommonStats.getShardLevelStats(
151127
indicesService.getIndicesQueryCache(),

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import org.elasticsearch.common.unit.ByteSizeValue;
2727
import org.elasticsearch.core.Nullable;
2828
import org.elasticsearch.core.Predicates;
29+
import org.elasticsearch.index.IndexService;
2930
import org.elasticsearch.index.cache.query.QueryCacheStats;
31+
import org.elasticsearch.index.shard.IndexShard;
3032
import org.elasticsearch.index.shard.ShardId;
3133

3234
import java.io.Closeable;
@@ -118,6 +120,46 @@ public Weight doCache(Weight weight, QueryCachingPolicy policy) {
118120
return new CachingWeightWrapper(in);
119121
}
120122

123+
public static CacheTotals getCacheTotalsForAllShards(IndicesService indicesService) {
124+
IndicesQueryCache queryCache = indicesService.getIndicesQueryCache();
125+
boolean hasQueryCache = queryCache != null;
126+
long totalSize = 0L;
127+
int shardCount = 0;
128+
boolean anyNonZero = false;
129+
for (final IndexService indexService : indicesService) {
130+
for (final IndexShard indexShard : indexService) {
131+
long cacheSize = hasQueryCache ? queryCache.getCacheSizeForShard(indexShard.shardId()) : 0L;
132+
shardCount++;
133+
if (cacheSize > 0L) {
134+
anyNonZero = true;
135+
totalSize += cacheSize;
136+
}
137+
}
138+
}
139+
long sharedRamBytesUsed = hasQueryCache ? queryCache.getSharedRamBytesUsed() : 0L;
140+
return new CacheTotals(totalSize, shardCount, anyNonZero, sharedRamBytesUsed);
141+
}
142+
143+
public static long getSharedRamSize(IndicesQueryCache queryCache, IndexShard indexShard, CacheTotals cacheTotals) {
144+
long sharedRamBytesUsed = cacheTotals.sharedRamBytesUsed();
145+
long totalSize = cacheTotals.totalSize();
146+
boolean anyNonZero = cacheTotals.anyNonZero();
147+
int shardCount = cacheTotals.shardCount();
148+
ShardId shardId = indexShard.shardId();
149+
long cacheSize = queryCache != null ? queryCache.getCacheSizeForShard(shardId) : 0L;
150+
long sharedRam = 0L;
151+
if (sharedRamBytesUsed != 0L) {
152+
if (anyNonZero == false) {
153+
sharedRam = Math.round((double) sharedRamBytesUsed / shardCount);
154+
} else if (totalSize != 0) {
155+
sharedRam = Math.round((double) sharedRamBytesUsed * cacheSize / totalSize);
156+
}
157+
}
158+
return sharedRam;
159+
}
160+
161+
public record CacheTotals(long totalSize, int shardCount, boolean anyNonZero, long sharedRamBytesUsed) {}
162+
121163
private class CachingWeightWrapper extends Weight {
122164

123165
private final Weight in;

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

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -520,37 +520,11 @@ static Map<Index, CommonStats> statsByIndex(final IndicesService indicesService,
520520

521521
static Map<Index, List<IndexShardStats>> statsByShard(final IndicesService indicesService, final CommonStatsFlags flags) {
522522
IndicesQueryCache queryCache = indicesService.getIndicesQueryCache();
523-
boolean hasQueryCache = queryCache != null;
524-
// First pass: gather all shards, cache sizes, and compute totals
525-
long totalSize = 0L;
526-
int shardCount = 0;
527-
boolean anyNonZero = false;
528-
// First pass: compute totals only
529-
for (final IndexService indexService : indicesService) {
530-
for (final IndexShard indexShard : indexService) {
531-
long cacheSize = hasQueryCache ? queryCache.getCacheSizeForShard(indexShard.shardId()) : 0L;
532-
shardCount++;
533-
if (cacheSize > 0L) {
534-
anyNonZero = true;
535-
totalSize += cacheSize;
536-
}
537-
}
538-
}
539-
long sharedRamBytesUsed = hasQueryCache ? queryCache.getSharedRamBytesUsed() : 0L;
523+
IndicesQueryCache.CacheTotals cacheTotals = IndicesQueryCache.getCacheTotalsForAllShards(indicesService);
540524
final Map<Index, List<IndexShardStats>> statsByShard = new HashMap<>();
541-
// Second pass: build stats, compute shared RAM on the fly
542525
for (final IndexService indexService : indicesService) {
543526
for (final IndexShard indexShard : indexService) {
544-
ShardId shardId = indexShard.shardId();
545-
long cacheSize = hasQueryCache ? queryCache.getCacheSizeForShard(shardId) : 0L;
546-
long sharedRam = 0L;
547-
if (sharedRamBytesUsed != 0L) {
548-
if (anyNonZero == false) {
549-
sharedRam = Math.round((double) sharedRamBytesUsed / shardCount);
550-
} else if (totalSize != 0) {
551-
sharedRam = Math.round((double) sharedRamBytesUsed * cacheSize / totalSize);
552-
}
553-
}
527+
long sharedRam = IndicesQueryCache.getSharedRamSize(queryCache, indexShard, cacheTotals);
554528
try {
555529
final IndexShardStats indexShardStats = indicesService.indexShardStats(indicesService, indexShard, flags, sharedRam);
556530
if (indexShardStats == null) {
@@ -562,7 +536,7 @@ static Map<Index, List<IndexShardStats>> statsByShard(final IndicesService indic
562536
statsByShard.get(indexService.index()).add(indexShardStats);
563537
}
564538
} catch (IllegalIndexShardStateException | AlreadyClosedException e) {
565-
logger.trace(() -> format("%s ignoring shard stats", shardId), e);
539+
logger.trace(() -> format("%s ignoring shard stats", indexShard.shardId()), e);
566540
}
567541
}
568542
}

0 commit comments

Comments
 (0)