66 */
77package org .elasticsearch .xpack .monitoring ;
88
9+ import org .elasticsearch .action .support .IndicesOptions ;
10+ import org .elasticsearch .cluster .metadata .IndexNameExpressionResolver ;
911import org .elasticsearch .common .settings .Settings ;
12+ import org .elasticsearch .index .Index ;
1013import org .elasticsearch .index .query .QueryBuilders ;
1114import org .elasticsearch .search .aggregations .Aggregation ;
1215import org .elasticsearch .search .aggregations .AggregationBuilders ;
2225import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertResponse ;
2326import static org .hamcrest .Matchers .equalTo ;
2427import static org .hamcrest .Matchers .greaterThanOrEqualTo ;
25- import static org .hamcrest .Matchers .instanceOf ;
2628
2729@ ClusterScope (scope = Scope .TEST , numDataNodes = 0 , numClientNodes = 0 )
2830public class MultiNodesStatsTests extends MonitoringIntegTestCase {
@@ -65,10 +67,7 @@ public void testMultipleNodes() throws Exception {
6567 nodes += n ;
6668
6769 final int nbNodes = nodes ;
68- assertBusy (() -> {
69- assertThat (cluster ().size (), equalTo (nbNodes ));
70- assertNoTimeout (clusterAdmin ().prepareHealth (TEST_REQUEST_TIMEOUT ).setWaitForNodes (Integer .toString (nbNodes )).get ());
71- });
70+ assertNoTimeout (safeGet (clusterAdmin ().prepareHealth (TEST_REQUEST_TIMEOUT ).setWaitForNodes (Integer .toString (nbNodes )).execute ()));
7271
7372 enableMonitoringCollection ();
7473 waitForMonitoringIndices ();
@@ -83,11 +82,11 @@ public void testMultipleNodes() throws Exception {
8382 .addAggregation (AggregationBuilders .terms ("nodes_ids" ).field ("node_stats.node_id" )),
8483 response -> {
8584 for (Aggregation aggregation : response .getAggregations ()) {
86- assertThat ( aggregation , instanceOf (StringTerms .class ) );
87- assertThat ((( StringTerms ) aggregation ) .getBuckets ().size (), equalTo (nbNodes ));
85+ final var stringTerms = asInstanceOf (StringTerms .class , aggregation );
86+ assertThat (stringTerms .getBuckets ().size (), equalTo (nbNodes ));
8887
8988 for (String nodeName : internalCluster ().getNodeNames ()) {
90- StringTerms .Bucket bucket = (( StringTerms ) aggregation ) .getBucketByKey (getNodeId (nodeName ));
89+ StringTerms .Bucket bucket = stringTerms .getBucketByKey (getNodeId (nodeName ));
9190 // At least 1 doc must exist per node, but it can be more than 1
9291 // because the first node may have already collected many node stats documents
9392 // whereas the last node just started to collect node stats.
@@ -98,4 +97,24 @@ public void testMultipleNodes() throws Exception {
9897 );
9998 });
10099 }
100+
101+ private void waitForMonitoringIndices () throws Exception {
102+ final var indexNameExpressionResolver = internalCluster ().getCurrentMasterNodeInstance (IndexNameExpressionResolver .class );
103+ final var indicesOptions = IndicesOptions .builder ()
104+ .wildcardOptions (IndicesOptions .WildcardOptions .builder ().allowEmptyExpressions (true ))
105+ .build ();
106+ awaitClusterState (cs -> {
107+ final var indices = indexNameExpressionResolver .concreteIndices (cs , indicesOptions , ALL_MONITORING_INDICES );
108+ if (indices .length == 0 ) {
109+ return false ;
110+ }
111+ for (Index index : indices ) {
112+ final var indexRoutingTable = cs .routingTable ().index (index );
113+ if (indexRoutingTable .allPrimaryShardsActive () == false ) {
114+ return false ;
115+ }
116+ }
117+ return true ;
118+ });
119+ }
101120}
0 commit comments