3838import org .elasticsearch .common .unit .ByteSizeValue ;
3939import org .elasticsearch .common .util .concurrent .EsExecutors ;
4040import org .elasticsearch .core .TimeValue ;
41+ import org .elasticsearch .index .shard .IndexingStats ;
4142import org .elasticsearch .index .shard .ShardId ;
4243import org .elasticsearch .index .store .StoreStats ;
4344import org .elasticsearch .threadpool .ThreadPool ;
@@ -215,7 +216,7 @@ void execute() {
215216 logger .trace ("starting async refresh" );
216217
217218 try (var ignoredRefs = fetchRefs ) {
218- maybeFetchIndicesStats (diskThresholdEnabled );
219+ maybeFetchIndicesStats (diskThresholdEnabled || writeLoadConstraintEnabled == WriteLoadDeciderStatus . ENABLED );
219220 maybeFetchNodeStats (diskThresholdEnabled || estimatedHeapThresholdEnabled );
220221 maybeFetchNodesEstimatedHeapUsage (estimatedHeapThresholdEnabled );
221222 maybeFetchNodesUsageStatsForThreadPools (writeLoadConstraintEnabled );
@@ -301,7 +302,14 @@ public void onFailure(Exception e) {
301302 private void fetchIndicesStats () {
302303 final IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest ();
303304 indicesStatsRequest .clear ();
304- indicesStatsRequest .store (true );
305+ if (diskThresholdEnabled ) {
306+ // This returns the shard sizes on disk
307+ indicesStatsRequest .store (true );
308+ }
309+ if (writeLoadConstraintEnabled == WriteLoadDeciderStatus .ENABLED ) {
310+ // This returns the shard write-loads
311+ indicesStatsRequest .indexing (true );
312+ }
305313 indicesStatsRequest .indicesOptions (IndicesOptions .STRICT_EXPAND_OPEN_CLOSED_HIDDEN );
306314 indicesStatsRequest .timeout (fetchTimeout );
307315 client .admin ()
@@ -350,13 +358,15 @@ public void onResponse(IndicesStatsResponse indicesStatsResponse) {
350358 }
351359
352360 final ShardStats [] stats = indicesStatsResponse .getShards ();
361+ final Map <ShardId , Double > shardWriteLoadByIdentifierBuilder = new HashMap <>();
353362 final Map <String , Long > shardSizeByIdentifierBuilder = new HashMap <>();
354363 final Map <ShardId , Long > shardDataSetSizeBuilder = new HashMap <>();
355364 final Map <ClusterInfo .NodeAndShard , String > dataPath = new HashMap <>();
356365 final Map <ClusterInfo .NodeAndPath , ClusterInfo .ReservedSpace .Builder > reservedSpaceBuilders =
357366 new HashMap <>();
358367 buildShardLevelInfo (
359368 adjustShardStats (stats ),
369+ shardWriteLoadByIdentifierBuilder ,
360370 shardSizeByIdentifierBuilder ,
361371 shardDataSetSizeBuilder ,
362372 dataPath ,
@@ -370,7 +380,8 @@ public void onResponse(IndicesStatsResponse indicesStatsResponse) {
370380 Map .copyOf (shardSizeByIdentifierBuilder ),
371381 Map .copyOf (shardDataSetSizeBuilder ),
372382 Map .copyOf (dataPath ),
373- Map .copyOf (reservedSpace )
383+ Map .copyOf (reservedSpace ),
384+ Map .copyOf (shardWriteLoadByIdentifierBuilder )
374385 );
375386 }
376387
@@ -527,8 +538,6 @@ public ClusterInfo getClusterInfo() {
527538 estimatedHeapUsages .put (nodeId , new EstimatedHeapUsage (nodeId , maxHeapSize .getBytes (), estimatedHeapUsage ));
528539 }
529540 });
530- final Map <String , NodeUsageStatsForThreadPools > nodeThreadPoolUsageStats = new HashMap <>();
531- nodeThreadPoolUsageStatsPerNode .forEach ((nodeId , nodeWriteLoad ) -> { nodeThreadPoolUsageStats .put (nodeId , nodeWriteLoad ); });
532541 return new ClusterInfo (
533542 leastAvailableSpaceUsages ,
534543 mostAvailableSpaceUsages ,
@@ -537,7 +546,8 @@ public ClusterInfo getClusterInfo() {
537546 indicesStatsSummary .dataPath ,
538547 indicesStatsSummary .reservedSpace ,
539548 estimatedHeapUsages ,
540- nodeThreadPoolUsageStats
549+ nodeThreadPoolUsageStatsPerNode ,
550+ indicesStatsSummary .shardWriteLoads ()
541551 );
542552 }
543553
@@ -567,6 +577,7 @@ public void addListener(Consumer<ClusterInfo> clusterInfoConsumer) {
567577
568578 static void buildShardLevelInfo (
569579 ShardStats [] stats ,
580+ Map <ShardId , Double > shardWriteLoads ,
570581 Map <String , Long > shardSizes ,
571582 Map <ShardId , Long > shardDataSetSizeBuilder ,
572583 Map <ClusterInfo .NodeAndShard , String > dataPathByShard ,
@@ -577,25 +588,31 @@ static void buildShardLevelInfo(
577588 dataPathByShard .put (ClusterInfo .NodeAndShard .from (shardRouting ), s .getDataPath ());
578589
579590 final StoreStats storeStats = s .getStats ().getStore ();
580- if (storeStats == null ) {
581- continue ;
582- }
583- final long size = storeStats .sizeInBytes ();
584- final long dataSetSize = storeStats .totalDataSetSizeInBytes ();
585- final long reserved = storeStats .reservedSizeInBytes ();
586-
587- final String shardIdentifier = ClusterInfo .shardIdentifierFromRouting (shardRouting );
588- logger .trace ("shard: {} size: {} reserved: {}" , shardIdentifier , size , reserved );
589- shardSizes .put (shardIdentifier , size );
590- if (dataSetSize > shardDataSetSizeBuilder .getOrDefault (shardRouting .shardId (), -1L )) {
591- shardDataSetSizeBuilder .put (shardRouting .shardId (), dataSetSize );
591+ if (storeStats != null ) {
592+ final long size = storeStats .sizeInBytes ();
593+ final long dataSetSize = storeStats .totalDataSetSizeInBytes ();
594+ final long reserved = storeStats .reservedSizeInBytes ();
595+
596+ final String shardIdentifier = ClusterInfo .shardIdentifierFromRouting (shardRouting );
597+ logger .trace ("shard: {} size: {} reserved: {}" , shardIdentifier , size , reserved );
598+ shardSizes .put (shardIdentifier , size );
599+ if (dataSetSize > shardDataSetSizeBuilder .getOrDefault (shardRouting .shardId (), -1L )) {
600+ shardDataSetSizeBuilder .put (shardRouting .shardId (), dataSetSize );
601+ }
602+ if (reserved != StoreStats .UNKNOWN_RESERVED_BYTES ) {
603+ final ClusterInfo .ReservedSpace .Builder reservedSpaceBuilder = reservedSpaceByShard .computeIfAbsent (
604+ new ClusterInfo .NodeAndPath (shardRouting .currentNodeId (), s .getDataPath ()),
605+ t -> new ClusterInfo .ReservedSpace .Builder ()
606+ );
607+ reservedSpaceBuilder .add (shardRouting .shardId (), reserved );
608+ }
592609 }
593- if ( reserved != StoreStats . UNKNOWN_RESERVED_BYTES ) {
594- final ClusterInfo . ReservedSpace . Builder reservedSpaceBuilder = reservedSpaceByShard . computeIfAbsent (
595- new ClusterInfo . NodeAndPath ( shardRouting . currentNodeId (), s . getDataPath ()),
596- t -> new ClusterInfo . ReservedSpace . Builder ()
597- );
598- reservedSpaceBuilder . add ( shardRouting . shardId (), reserved );
610+ final IndexingStats indexingStats = s . getStats (). getIndexing ();
611+ if ( indexingStats != null ) {
612+ final double shardWriteLoad = indexingStats . getTotal (). getPeakWriteLoad ();
613+ if ( shardWriteLoad > shardWriteLoads . getOrDefault ( shardRouting . shardId (), - 1.0 )) {
614+ shardWriteLoads . put ( shardRouting . shardId (), shardWriteLoad );
615+ }
599616 }
600617 }
601618 }
@@ -623,9 +640,10 @@ private record IndicesStatsSummary(
623640 Map <String , Long > shardSizes ,
624641 Map <ShardId , Long > shardDataSetSizes ,
625642 Map <ClusterInfo .NodeAndShard , String > dataPath ,
626- Map <ClusterInfo .NodeAndPath , ClusterInfo .ReservedSpace > reservedSpace
643+ Map <ClusterInfo .NodeAndPath , ClusterInfo .ReservedSpace > reservedSpace ,
644+ Map <ShardId , Double > shardWriteLoads
627645 ) {
628- static final IndicesStatsSummary EMPTY = new IndicesStatsSummary (Map .of (), Map .of (), Map .of (), Map .of ());
646+ static final IndicesStatsSummary EMPTY = new IndicesStatsSummary (Map .of (), Map .of (), Map .of (), Map .of (), Map . of () );
629647 }
630648
631649}
0 commit comments