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 ;
9393import java .util .HashSet ;
9494import java .util .List ;
9595import java .util .Map ;
96+ import java .util .function .ToIntFunction ;
9697import java .util .stream .IntStream ;
9798
9899import static java .util .Collections .emptySet ;
@@ -477,54 +478,56 @@ public void testSerialization() throws IOException {
477478 }
478479
479480 public void testChunking () {
480- assertChunkCount (
481- createNodeStats (),
482- randomFrom (ToXContent .EMPTY_PARAMS , new ToXContent .MapParams (Map .of ("level" , "node" ))),
483- nodeStats -> expectedChunks (nodeStats , NodeStatsLevel .NODE )
484- );
485- assertChunkCount (
486- createNodeStats (),
487- new ToXContent .MapParams (Map .of ("level" , "indices" )),
488- nodeStats -> expectedChunks (nodeStats , NodeStatsLevel .INDICES )
489- );
490- assertChunkCount (
491- createNodeStats (),
492- new ToXContent .MapParams (Map .of ("level" , "shards" )),
493- nodeStats -> expectedChunks (nodeStats , NodeStatsLevel .SHARDS )
494- );
481+ assertChunkCount (createNodeStats (), ToXContent .EMPTY_PARAMS , nodeStats -> expectedChunks (nodeStats , ToXContent .EMPTY_PARAMS ));
482+ for (NodeStatsLevel l : NodeStatsLevel .values ()) {
483+ ToXContent .Params p = new ToXContent .MapParams (Map .of ("level" , l .getLevel ()));
484+ assertChunkCount (createNodeStats (), p , nodeStats -> expectedChunks (nodeStats , p ));
485+ }
495486 }
496487
497- private static int expectedChunks (NodeStats nodeStats , NodeStatsLevel level ) {
498- return 7 // number of static chunks, see NodeStats#toXContentChunked
499- + expectedChunks (nodeStats .getHttp ()) //
500- + expectedChunks (nodeStats .getIndices (), level ) //
501- + expectedChunks (nodeStats .getTransport ()) //
502- + expectedChunks (nodeStats .getIngestStats ()) //
503- + expectedChunks (nodeStats .getThreadPool ()) //
504- + expectedChunks (nodeStats .getScriptStats ()) //
505- + expectedChunks (nodeStats .getScriptCacheStats ());
488+ private static int expectedChunks (NodeStats nodeStats , ToXContent .Params params ) {
489+ return 3 // number of static chunks, see NodeStats#toXContentChunked
490+ + assertExpectedChunks (nodeStats .getIndices (), i -> expectedChunks (i , NodeStatsLevel .of (params , NodeStatsLevel .NODE )), params )
491+ + assertExpectedChunks (nodeStats .getThreadPool (), NodeStatsTests ::expectedChunks , params ) // <br/>
492+ + chunkIfPresent (nodeStats .getFs ()) // <br/>
493+ + assertExpectedChunks (nodeStats .getTransport (), NodeStatsTests ::expectedChunks , params ) // <br/>
494+ + assertExpectedChunks (nodeStats .getHttp (), NodeStatsTests ::expectedChunks , params ) // <br/>
495+ + chunkIfPresent (nodeStats .getBreaker ()) // <br/>
496+ + assertExpectedChunks (nodeStats .getScriptStats (), NodeStatsTests ::expectedChunks , params ) // <br/>
497+ + chunkIfPresent (nodeStats .getDiscoveryStats ()) // <br/>
498+ + assertExpectedChunks (nodeStats .getIngestStats (), NodeStatsTests ::expectedChunks , params ) // <br/>
499+ + chunkIfPresent (nodeStats .getAdaptiveSelectionStats ()) // <br/>
500+ + assertExpectedChunks (nodeStats .getScriptCacheStats (), NodeStatsTests ::expectedChunks , params );
506501 }
507502
508- private static int expectedChunks (ScriptCacheStats scriptCacheStats ) {
509- if (scriptCacheStats == null ) return 0 ;
503+ private static int chunkIfPresent (ToXContent xcontent ) {
504+ return xcontent == null ? 0 : 1 ;
505+ }
510506
511- var chunks = 4 ;
512- if (scriptCacheStats .general () != null ) {
513- chunks += 3 ;
514- } else {
515- chunks += 2 ;
516- chunks += scriptCacheStats .context ().size () * 6 ;
507+ private static <T extends ChunkedToXContent > int assertExpectedChunks (T obj , ToIntFunction <T > getChunks , ToXContent .Params params ) {
508+ if (obj == null ) return 0 ;
509+ int chunks = getChunks .applyAsInt (obj );
510+ assertChunkCount (obj , params , t -> chunks );
511+ return chunks ;
512+ }
513+
514+ private static int expectedChunks (ScriptCacheStats scriptCacheStats ) {
515+ var chunks = 3 ; // start, end, SUM
516+ if (scriptCacheStats .general () == null ) {
517+ chunks += 2 + scriptCacheStats .context ().size () * 4 ;
517518 }
518519
519520 return chunks ;
520521 }
521522
522523 private static int expectedChunks (ScriptStats scriptStats ) {
523- return scriptStats == null ? 0 : 8 + scriptStats .contextStats ().size ();
524+ return 7 + (scriptStats .compilationsHistory () != null && scriptStats .compilationsHistory ().areTimingsEmpty () == false ? 1 : 0 )
525+ + (scriptStats .cacheEvictionsHistory () != null && scriptStats .cacheEvictionsHistory ().areTimingsEmpty () == false ? 1 : 0 )
526+ + scriptStats .contextStats ().size ();
524527 }
525528
526529 private static int expectedChunks (ThreadPoolStats threadPool ) {
527- return threadPool == null ? 0 : 2 + threadPool .stats ().stream ().mapToInt (s -> {
530+ return 2 + threadPool .stats ().stream ().mapToInt (s -> {
528531 var chunks = 0 ;
529532 chunks += s .threads () == -1 ? 0 : 1 ;
530533 chunks += s .queue () == -1 ? 0 : 1 ;
@@ -536,25 +539,23 @@ private static int expectedChunks(ThreadPoolStats threadPool) {
536539 }).sum ();
537540 }
538541
539- private static int expectedChunks (@ Nullable IngestStats ingestStats ) {
540- return ingestStats == null
541- ? 0
542- : 2 + ingestStats .pipelineStats ()
543- .stream ()
544- .mapToInt (pipelineStats -> 2 + ingestStats .processorStats ().getOrDefault (pipelineStats .pipelineId (), List .of ()).size ())
545- .sum ();
542+ private static int expectedChunks (IngestStats ingestStats ) {
543+ return 2 + ingestStats .pipelineStats ()
544+ .stream ()
545+ .mapToInt (pipelineStats -> 2 + ingestStats .processorStats ().getOrDefault (pipelineStats .pipelineId (), List .of ()).size ())
546+ .sum ();
546547 }
547548
548- private static int expectedChunks (@ Nullable HttpStats httpStats ) {
549- return httpStats == null ? 0 : 3 + httpStats .getClientStats ().size () + httpStats .httpRouteStats ().size ();
549+ private static int expectedChunks (HttpStats httpStats ) {
550+ return 3 + httpStats .getClientStats ().size () + httpStats .httpRouteStats ().size ();
550551 }
551552
552- private static int expectedChunks (@ Nullable TransportStats transportStats ) {
553- return transportStats == null ? 0 : 3 ; // only one transport action
553+ private static int expectedChunks (TransportStats transportStats ) {
554+ return 3 ; // only one transport action
554555 }
555556
556- private static int expectedChunks (@ Nullable NodeIndicesStats nodeIndicesStats , NodeStatsLevel level ) {
557- return nodeIndicesStats == null ? 0 : switch (level ) {
557+ private static int expectedChunks (NodeIndicesStats nodeIndicesStats , NodeStatsLevel level ) {
558+ return switch (level ) {
558559 case NODE -> 2 ;
559560 case INDICES -> 5 ; // only one index
560561 case SHARDS -> 9 ; // only one shard
0 commit comments