Skip to content

Commit f381bb9

Browse files
committed
reducing memory usage
1 parent 168c6c5 commit f381bb9

File tree

1 file changed

+32
-43
lines changed

1 file changed

+32
-43
lines changed

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

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@
167167
import java.util.ArrayList;
168168
import java.util.Arrays;
169169
import java.util.Collection;
170-
import java.util.Collections;
171170
import java.util.EnumMap;
172171
import java.util.HashMap;
173172
import java.util.Iterator;
@@ -521,65 +520,55 @@ static Map<Index, CommonStats> statsByIndex(final IndicesService indicesService,
521520

522521
static Map<Index, List<IndexShardStats>> statsByShard(final IndicesService indicesService, final CommonStatsFlags flags) {
523522
IndicesQueryCache queryCache = indicesService.getIndicesQueryCache();
523+
boolean hasQueryCache = queryCache != null;
524524
// 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<>();
539525
long totalSize = 0L;
540526
int shardCount = 0;
541527
boolean anyNonZero = false;
528+
// First pass: compute totals only
542529
for (final IndexService indexService : indicesService) {
543530
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;
547532
shardCount++;
548533
if (cacheSize > 0L) {
549534
anyNonZero = true;
550535
totalSize += cacheSize;
551536
}
552537
}
553538
}
554-
long sharedRamBytesUsed = queryCache.getSharedRamBytesUsed();
539+
long sharedRamBytesUsed = hasQueryCache ? queryCache.getSharedRamBytesUsed() : 0L;
555540
final Map<Index, List<IndexShardStats>> statsByShard = new HashMap<>();
556541
// 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+
}
575553
}
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);
580571
}
581-
} catch (IllegalIndexShardStateException | AlreadyClosedException e) {
582-
logger.trace(() -> format("%s ignoring shard stats", info.shardId), e);
583572
}
584573
}
585574
return statsByShard;

0 commit comments

Comments
 (0)