|
167 | 167 | import java.util.ArrayList; |
168 | 168 | import java.util.Arrays; |
169 | 169 | import java.util.Collection; |
170 | | -import java.util.Collections; |
171 | 170 | import java.util.EnumMap; |
172 | 171 | import java.util.HashMap; |
173 | 172 | import java.util.Iterator; |
@@ -521,65 +520,55 @@ static Map<Index, CommonStats> statsByIndex(final IndicesService indicesService, |
521 | 520 |
|
522 | 521 | static Map<Index, List<IndexShardStats>> statsByShard(final IndicesService indicesService, final CommonStatsFlags flags) { |
523 | 522 | IndicesQueryCache queryCache = indicesService.getIndicesQueryCache(); |
| 523 | + boolean hasQueryCache = queryCache != null; |
524 | 524 | // First pass: gather all shards, cache sizes, and compute totals |
525 | | - class ShardCacheInfo { |
526 | | - final IndexService indexService; |
527 | | - final IndexShard indexShard; |
528 | | - final org.elasticsearch.index.shard.ShardId shardId; |
529 | | - final long cacheSize; |
530 | | - |
531 | | - ShardCacheInfo(IndexService is, IndexShard shard, org.elasticsearch.index.shard.ShardId id, long size) { |
532 | | - this.indexService = is; |
533 | | - this.indexShard = shard; |
534 | | - this.shardId = id; |
535 | | - this.cacheSize = size; |
536 | | - } |
537 | | - } |
538 | | - List<ShardCacheInfo> shardInfos = new ArrayList<>(); |
539 | 525 | long totalSize = 0L; |
540 | 526 | int shardCount = 0; |
541 | 527 | boolean anyNonZero = false; |
| 528 | + // First pass: compute totals only |
542 | 529 | for (final IndexService indexService : indicesService) { |
543 | 530 | for (final IndexShard indexShard : indexService) { |
544 | | - org.elasticsearch.index.shard.ShardId shardId = indexShard.shardId(); |
545 | | - long cacheSize = queryCache.getCacheSizeForShard(shardId); |
546 | | - shardInfos.add(new ShardCacheInfo(indexService, indexShard, shardId, cacheSize)); |
| 531 | + long cacheSize = hasQueryCache ? queryCache.getCacheSizeForShard(indexShard.shardId()) : 0L; |
547 | 532 | shardCount++; |
548 | 533 | if (cacheSize > 0L) { |
549 | 534 | anyNonZero = true; |
550 | 535 | totalSize += cacheSize; |
551 | 536 | } |
552 | 537 | } |
553 | 538 | } |
554 | | - long sharedRamBytesUsed = queryCache.getSharedRamBytesUsed(); |
| 539 | + long sharedRamBytesUsed = hasQueryCache ? queryCache.getSharedRamBytesUsed() : 0L; |
555 | 540 | final Map<Index, List<IndexShardStats>> statsByShard = new HashMap<>(); |
556 | 541 | // Second pass: build stats, compute shared RAM on the fly |
557 | | - for (ShardCacheInfo info : shardInfos) { |
558 | | - long sharedRam = 0L; |
559 | | - if (sharedRamBytesUsed != 0L) { |
560 | | - if (anyNonZero == false) { |
561 | | - sharedRam = Math.round((double) sharedRamBytesUsed / shardCount); |
562 | | - } else if (totalSize != 0) { |
563 | | - sharedRam = Math.round((double) sharedRamBytesUsed * info.cacheSize / totalSize); |
564 | | - } |
565 | | - } |
566 | | - try { |
567 | | - final IndexShardStats indexShardStats = indicesService.indexShardStats( |
568 | | - indicesService, |
569 | | - info.indexShard, |
570 | | - flags, |
571 | | - Collections.singletonMap(info.shardId, sharedRam) |
572 | | - ); |
573 | | - if (indexShardStats == null) { |
574 | | - continue; |
| 542 | + for (final IndexService indexService : indicesService) { |
| 543 | + for (final IndexShard indexShard : indexService) { |
| 544 | + org.elasticsearch.index.shard.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 | + } |
575 | 553 | } |
576 | | - if (statsByShard.containsKey(info.indexService.index()) == false) { |
577 | | - statsByShard.put(info.indexService.index(), arrayAsArrayList(indexShardStats)); |
578 | | - } else { |
579 | | - statsByShard.get(info.indexService.index()).add(indexShardStats); |
| 554 | + try { |
| 555 | + final IndexShardStats indexShardStats = indicesService.indexShardStats( |
| 556 | + indicesService, |
| 557 | + indexShard, |
| 558 | + flags, |
| 559 | + java.util.Collections.singletonMap(shardId, sharedRam) |
| 560 | + ); |
| 561 | + if (indexShardStats == null) { |
| 562 | + continue; |
| 563 | + } |
| 564 | + if (statsByShard.containsKey(indexService.index()) == false) { |
| 565 | + statsByShard.put(indexService.index(), arrayAsArrayList(indexShardStats)); |
| 566 | + } else { |
| 567 | + statsByShard.get(indexService.index()).add(indexShardStats); |
| 568 | + } |
| 569 | + } catch (IllegalIndexShardStateException | AlreadyClosedException e) { |
| 570 | + logger.trace(() -> format("%s ignoring shard stats", shardId), e); |
580 | 571 | } |
581 | | - } catch (IllegalIndexShardStateException | AlreadyClosedException e) { |
582 | | - logger.trace(() -> format("%s ignoring shard stats", info.shardId), e); |
583 | 572 | } |
584 | 573 | } |
585 | 574 | return statsByShard; |
|
0 commit comments