3030import org .elasticsearch .common .io .stream .StreamInput ;
3131import org .elasticsearch .common .network .HandlingTimeTracker ;
3232import org .elasticsearch .common .util .Maps ;
33- import org .elasticsearch .core . Nullable ;
33+ import org .elasticsearch .common . xcontent . ChunkedToXContent ;
3434import org .elasticsearch .core .Tuple ;
3535import org .elasticsearch .discovery .DiscoveryStats ;
3636import org .elasticsearch .http .HttpStats ;
9292import java .util .HashSet ;
9393import java .util .List ;
9494import java .util .Map ;
95+ import java .util .function .ToIntFunction ;
9596import java .util .stream .IntStream ;
9697
9798import static java .util .Collections .emptySet ;
@@ -476,54 +477,54 @@ public void testSerialization() throws IOException {
476477 }
477478
478479 public void testChunking () {
479- assertChunkCount (
480- createNodeStats (),
481- randomFrom (ToXContent .EMPTY_PARAMS , new ToXContent .MapParams (Map .of ("level" , "node" ))),
482- nodeStats -> expectedChunks (nodeStats , NodeStatsLevel .NODE )
483- );
484- assertChunkCount (
485- createNodeStats (),
486- new ToXContent .MapParams (Map .of ("level" , "indices" )),
487- nodeStats -> expectedChunks (nodeStats , NodeStatsLevel .INDICES )
488- );
489- assertChunkCount (
490- createNodeStats (),
491- new ToXContent .MapParams (Map .of ("level" , "shards" )),
492- nodeStats -> expectedChunks (nodeStats , NodeStatsLevel .SHARDS )
493- );
480+ assertChunkCount (createNodeStats (), ToXContent .EMPTY_PARAMS , nodeStats -> expectedChunks (nodeStats , ToXContent .EMPTY_PARAMS ));
481+ for (NodeStatsLevel l : NodeStatsLevel .values ()) {
482+ ToXContent .Params p = new ToXContent .MapParams (Map .of ("level" , l .getLevel ()));
483+ assertChunkCount (createNodeStats (), p , nodeStats -> expectedChunks (nodeStats , p ));
484+ }
494485 }
495486
496- private static int expectedChunks (NodeStats nodeStats , NodeStatsLevel level ) {
497- return 7 // number of static chunks, see NodeStats#toXContentChunked
498- + expectedChunks (nodeStats .getHttp ()) //
499- + expectedChunks (nodeStats .getIndices (), level ) //
500- + expectedChunks (nodeStats .getTransport ()) //
501- + expectedChunks (nodeStats .getIngestStats ()) //
502- + expectedChunks (nodeStats .getThreadPool ()) //
503- + expectedChunks (nodeStats .getScriptStats ()) //
504- + expectedChunks (nodeStats .getScriptCacheStats ());
487+ private static int expectedChunks (NodeStats nodeStats , ToXContent .Params params ) {
488+ return 3 // number of static chunks, see NodeStats#toXContentChunked
489+ + assertExpectedChunks (nodeStats .getIndices (), i -> expectedChunks (i , NodeStatsLevel .of (params , NodeStatsLevel .NODE )), params )
490+ + assertExpectedChunks (nodeStats .getThreadPool (), NodeStatsTests ::expectedChunks , params ) // <br/>
491+ + chunkIfPresent (nodeStats .getFs ()) // <br/>
492+ + assertExpectedChunks (nodeStats .getTransport (), NodeStatsTests ::expectedChunks , params ) // <br/>
493+ + assertExpectedChunks (nodeStats .getHttp (), NodeStatsTests ::expectedChunks , params ) // <br/>
494+ + chunkIfPresent (nodeStats .getBreaker ()) // <br/>
495+ + assertExpectedChunks (nodeStats .getScriptStats (), NodeStatsTests ::expectedChunks , params ) // <br/>
496+ + chunkIfPresent (nodeStats .getDiscoveryStats ()) // <br/>
497+ + assertExpectedChunks (nodeStats .getIngestStats (), NodeStatsTests ::expectedChunks , params ) // <br/>
498+ + chunkIfPresent (nodeStats .getAdaptiveSelectionStats ()) // <br/>
499+ + assertExpectedChunks (nodeStats .getScriptCacheStats (), NodeStatsTests ::expectedChunks , params );
505500 }
506501
507- private static int expectedChunks (ScriptCacheStats scriptCacheStats ) {
508- if (scriptCacheStats == null ) return 0 ;
502+ private static int chunkIfPresent (ToXContent xcontent ) {
503+ return xcontent == null ? 0 : 1 ;
504+ }
509505
510- var chunks = 4 ;
511- if (scriptCacheStats .general () != null ) {
512- chunks += 3 ;
513- } else {
514- chunks += 2 ;
515- chunks += scriptCacheStats .context ().size () * 6 ;
506+ private static <T extends ChunkedToXContent > int assertExpectedChunks (T obj , ToIntFunction <T > getChunks , ToXContent .Params params ) {
507+ if (obj == null ) return 0 ;
508+ int chunks = getChunks .applyAsInt (obj );
509+ assertChunkCount (obj , params , t -> chunks );
510+ return chunks ;
511+ }
512+
513+ private static int expectedChunks (ScriptCacheStats scriptCacheStats ) {
514+ var chunks = 3 ; // start, end, SUM
515+ if (scriptCacheStats .general () == null ) {
516+ chunks += 2 + scriptCacheStats .context ().size () * 4 ;
516517 }
517518
518519 return chunks ;
519520 }
520521
521522 private static int expectedChunks (ScriptStats scriptStats ) {
522- return scriptStats == null ? 0 : 8 + scriptStats .contextStats ().size ();
523+ return 8 + scriptStats .contextStats ().size ();
523524 }
524525
525526 private static int expectedChunks (ThreadPoolStats threadPool ) {
526- return threadPool == null ? 0 : 2 + threadPool .stats ().stream ().mapToInt (s -> {
527+ return 2 + threadPool .stats ().stream ().mapToInt (s -> {
527528 var chunks = 0 ;
528529 chunks += s .threads () == -1 ? 0 : 1 ;
529530 chunks += s .queue () == -1 ? 0 : 1 ;
@@ -535,25 +536,23 @@ private static int expectedChunks(ThreadPoolStats threadPool) {
535536 }).sum ();
536537 }
537538
538- private static int expectedChunks (@ Nullable IngestStats ingestStats ) {
539- return ingestStats == null
540- ? 0
541- : 2 + ingestStats .pipelineStats ()
542- .stream ()
543- .mapToInt (pipelineStats -> 2 + ingestStats .processorStats ().getOrDefault (pipelineStats .pipelineId (), List .of ()).size ())
544- .sum ();
539+ private static int expectedChunks (IngestStats ingestStats ) {
540+ return 2 + ingestStats .pipelineStats ()
541+ .stream ()
542+ .mapToInt (pipelineStats -> 2 + ingestStats .processorStats ().getOrDefault (pipelineStats .pipelineId (), List .of ()).size ())
543+ .sum ();
545544 }
546545
547- private static int expectedChunks (@ Nullable HttpStats httpStats ) {
548- return httpStats == null ? 0 : 3 + httpStats .getClientStats ().size () + httpStats .httpRouteStats ().size ();
546+ private static int expectedChunks (HttpStats httpStats ) {
547+ return 3 + httpStats .getClientStats ().size () + httpStats .httpRouteStats ().size ();
549548 }
550549
551- private static int expectedChunks (@ Nullable TransportStats transportStats ) {
552- return transportStats == null ? 0 : 3 ; // only one transport action
550+ private static int expectedChunks (TransportStats transportStats ) {
551+ return 3 ; // only one transport action
553552 }
554553
555- private static int expectedChunks (@ Nullable NodeIndicesStats nodeIndicesStats , NodeStatsLevel level ) {
556- return nodeIndicesStats == null ? 0 : switch (level ) {
554+ private static int expectedChunks (NodeIndicesStats nodeIndicesStats , NodeStatsLevel level ) {
555+ return switch (level ) {
557556 case NODE -> 2 ;
558557 case INDICES -> 5 ; // only one index
559558 case SHARDS -> 9 ; // only one shard
0 commit comments