Skip to content

Commit 6019a7f

Browse files
committed
Reduce assertBusy usage in testMultipleNodes
Relates #126501
1 parent 62636f9 commit 6019a7f

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,14 @@
226226
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
227227
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
228228
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoTimeout;
229-
import static org.hamcrest.CoreMatchers.not;
230-
import static org.hamcrest.Matchers.anEmptyMap;
229+
import static org.hamcrest.Matchers.allOf;
230+
import static org.hamcrest.Matchers.containsString;
231231
import static org.hamcrest.Matchers.empty;
232232
import static org.hamcrest.Matchers.equalTo;
233233
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
234234
import static org.hamcrest.Matchers.is;
235235
import static org.hamcrest.Matchers.lessThanOrEqualTo;
236+
import static org.hamcrest.Matchers.not;
236237
import static org.hamcrest.Matchers.startsWith;
237238

238239
/**
@@ -1733,23 +1734,13 @@ public static boolean indexExists(String index, Client client) {
17331734
return getIndexResponse.getIndices().length > 0;
17341735
}
17351736

1736-
public static void awaitIndexExists(String index) throws Exception {
1737+
public static void awaitIndexExists(String index) {
17371738
awaitIndexExists(index, client());
17381739
}
17391740

1740-
public static void awaitIndexExists(String index, Client client) throws Exception {
1741-
if (Regex.isSimpleMatchPattern(index) || Metadata.ALL.equals(index)) {
1742-
assertBusy(() -> {
1743-
final var response = clusterHealthWithIndex(index, client);
1744-
assertThat(response.getIndices(), not(anEmptyMap()));
1745-
});
1746-
} else {
1747-
clusterHealthWithIndex(index, client);
1748-
}
1749-
}
1750-
1751-
private static ClusterHealthResponse clusterHealthWithIndex(String index, Client client) {
1752-
return safeGet(
1741+
public static void awaitIndexExists(String index, Client client) {
1742+
assertThat("wildcards not supported", index, allOf(not(Metadata.ALL), not(containsString("*"))));
1743+
safeGet(
17531744
client.admin()
17541745
.cluster()
17551746
.prepareHealth(SAFE_AWAIT_TIMEOUT, index)

x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.cluster.metadata.Metadata;
2323
import org.elasticsearch.cluster.metadata.MetadataIndexTemplateService;
2424
import org.elasticsearch.cluster.metadata.Template;
25+
import org.elasticsearch.cluster.service.ClusterService;
2526
import org.elasticsearch.common.Strings;
2627
import org.elasticsearch.common.regex.Regex;
2728
import org.elasticsearch.common.settings.Settings;

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
*/
77
package org.elasticsearch.xpack.monitoring;
88

9+
import org.elasticsearch.action.support.IndicesOptions;
10+
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
911
import org.elasticsearch.common.settings.Settings;
12+
import org.elasticsearch.index.Index;
1013
import org.elasticsearch.index.query.QueryBuilders;
1114
import org.elasticsearch.search.aggregations.Aggregation;
1215
import org.elasticsearch.search.aggregations.AggregationBuilders;
@@ -22,7 +25,6 @@
2225
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse;
2326
import static org.hamcrest.Matchers.equalTo;
2427
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
25-
import static org.hamcrest.Matchers.instanceOf;
2628

2729
@ClusterScope(scope = Scope.TEST, numDataNodes = 0, numClientNodes = 0)
2830
public 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
}

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@ protected void deleteMonitoringIndices() {
124124
assertAcked(client().admin().indices().prepareDelete(ALL_MONITORING_INDICES));
125125
}
126126

127-
protected void ensureMonitoringIndicesYellow() {
128-
ensureYellowAndNoInitializingShards(".monitoring-es-*");
129-
}
130-
131127
protected List<Tuple<String, String>> monitoringWatches() {
132128
final ClusterService clusterService = clusterService();
133129

@@ -150,11 +146,6 @@ protected void assertTemplateInstalled(String name) {
150146
assertTrue("failed to find a template matching [" + name + "]", found);
151147
}
152148

153-
protected void waitForMonitoringIndices() throws Exception {
154-
awaitIndexExists(ALL_MONITORING_INDICES);
155-
assertBusy(this::ensureMonitoringIndicesYellow);
156-
}
157-
158149
protected void enableMonitoringCollection() {
159150
updateClusterSettings(Settings.builder().put(MonitoringService.ENABLED.getKey(), true));
160151
}

0 commit comments

Comments
 (0)