Skip to content

Commit 0bd7fa4

Browse files
authored
[8.16] Fix NodeStatsTests chunking (#115929) (#115956)
* Fix NodeStatsTests chunking (#115929) Rewrite the test to make it a bit clearer * ScriptStats hasn't been converted
1 parent 992a973 commit 0bd7fa4

File tree

2 files changed

+47
-51
lines changed

2 files changed

+47
-51
lines changed

muted-tests.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,6 @@ tests:
182182
- class: org.elasticsearch.xpack.sql.qa.security.JdbcSqlSpecIT
183183
method: test {case-functions.testSelectInsertWithLcaseAndLengthWithOrderBy}
184184
issue: https://github.com/elastic/elasticsearch/issues/112642
185-
- class: org.elasticsearch.action.admin.cluster.node.stats.NodeStatsTests
186-
method: testChunking
187-
issue: https://github.com/elastic/elasticsearch/issues/113139
188185
- class: org.elasticsearch.xpack.inference.rest.ServerSentEventsRestActionListenerTests
189186
method: testResponse
190187
issue: https://github.com/elastic/elasticsearch/issues/113148

server/src/test/java/org/elasticsearch/action/admin/cluster/node/stats/NodeStatsTests.java

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.elasticsearch.common.io.stream.StreamInput;
3131
import org.elasticsearch.common.network.HandlingTimeTracker;
3232
import org.elasticsearch.common.util.Maps;
33-
import org.elasticsearch.core.Nullable;
33+
import org.elasticsearch.common.xcontent.ChunkedToXContent;
3434
import org.elasticsearch.core.Tuple;
3535
import org.elasticsearch.discovery.DiscoveryStats;
3636
import org.elasticsearch.http.HttpStats;
@@ -92,6 +92,7 @@
9292
import java.util.HashSet;
9393
import java.util.List;
9494
import java.util.Map;
95+
import java.util.function.ToIntFunction;
9596
import java.util.stream.IntStream;
9697

9798
import 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

Comments
 (0)