Skip to content

Reduce assertBusy usage in testMultipleNodes #126582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,14 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoTimeout;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;

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

public static void awaitIndexExists(String index) throws Exception {
public static void awaitIndexExists(String index) {
awaitIndexExists(index, client());
}

public static void awaitIndexExists(String index, Client client) throws Exception {
if (Regex.isSimpleMatchPattern(index) || Metadata.ALL.equals(index)) {
assertBusy(() -> {
final var response = clusterHealthWithIndex(index, client);
assertThat(response.getIndices(), not(anEmptyMap()));
});
} else {
clusterHealthWithIndex(index, client);
}
}

private static ClusterHealthResponse clusterHealthWithIndex(String index, Client client) {
return safeGet(
public static void awaitIndexExists(String index, Client client) {
assertThat("wildcards not supported", index, allOf(not(Metadata.ALL), not(containsString("*"))));
safeGet(
client.admin()
.cluster()
.prepareHealth(SAFE_AWAIT_TIMEOUT, index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
*/
package org.elasticsearch.xpack.monitoring;

import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
Expand All @@ -22,7 +25,6 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.instanceOf;

@ClusterScope(scope = Scope.TEST, numDataNodes = 0, numClientNodes = 0)
public class MultiNodesStatsTests extends MonitoringIntegTestCase {
Expand Down Expand Up @@ -65,10 +67,7 @@ public void testMultipleNodes() throws Exception {
nodes += n;

final int nbNodes = nodes;
assertBusy(() -> {
assertThat(cluster().size(), equalTo(nbNodes));
assertNoTimeout(clusterAdmin().prepareHealth(TEST_REQUEST_TIMEOUT).setWaitForNodes(Integer.toString(nbNodes)).get());
});
assertNoTimeout(safeGet(clusterAdmin().prepareHealth(TEST_REQUEST_TIMEOUT).setWaitForNodes(Integer.toString(nbNodes)).execute()));

enableMonitoringCollection();
waitForMonitoringIndices();
Expand All @@ -83,11 +82,11 @@ public void testMultipleNodes() throws Exception {
.addAggregation(AggregationBuilders.terms("nodes_ids").field("node_stats.node_id")),
response -> {
for (Aggregation aggregation : response.getAggregations()) {
assertThat(aggregation, instanceOf(StringTerms.class));
assertThat(((StringTerms) aggregation).getBuckets().size(), equalTo(nbNodes));
final var stringTerms = asInstanceOf(StringTerms.class, aggregation);
assertThat(stringTerms.getBuckets().size(), equalTo(nbNodes));

for (String nodeName : internalCluster().getNodeNames()) {
StringTerms.Bucket bucket = ((StringTerms) aggregation).getBucketByKey(getNodeId(nodeName));
StringTerms.Bucket bucket = stringTerms.getBucketByKey(getNodeId(nodeName));
// At least 1 doc must exist per node, but it can be more than 1
// because the first node may have already collected many node stats documents
// whereas the last node just started to collect node stats.
Expand All @@ -98,4 +97,24 @@ public void testMultipleNodes() throws Exception {
);
});
}

private void waitForMonitoringIndices() throws Exception {
final var indexNameExpressionResolver = internalCluster().getCurrentMasterNodeInstance(IndexNameExpressionResolver.class);
final var indicesOptions = IndicesOptions.builder()
.wildcardOptions(IndicesOptions.WildcardOptions.builder().allowEmptyExpressions(true))
.build();
awaitClusterState(cs -> {
final var indices = indexNameExpressionResolver.concreteIndices(cs, indicesOptions, ".monitoring-es-*");
if (indices.length == 0) {
return false;
}
for (Index index : indices) {
final var indexRoutingTable = cs.routingTable().index(index);
if (indexRoutingTable.allPrimaryShardsActive() == false) {
return false;
}
}
return true;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ protected void deleteMonitoringIndices() {
assertAcked(client().admin().indices().prepareDelete(ALL_MONITORING_INDICES));
}

protected void ensureMonitoringIndicesYellow() {
ensureYellowAndNoInitializingShards(".monitoring-es-*");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ALL_MONITORING_INDICES is "monitoring-* and this is .monitoring-es-*. I doubt the difference would actual matter. But probably still better to be safe and use .monitoring-es-* in awaitClusterState?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah well spotted :) Yeah it doesn't matter I think, we're only monitoring ES itself in this test, but I fixed that up.

}

protected List<Tuple<String, String>> monitoringWatches() {
final ClusterService clusterService = clusterService();

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

protected void waitForMonitoringIndices() throws Exception {
awaitIndexExists(ALL_MONITORING_INDICES);
assertBusy(this::ensureMonitoringIndicesYellow);
}

protected void enableMonitoringCollection() {
updateClusterSettings(Settings.builder().put(MonitoringService.ENABLED.getKey(), true));
}
Expand Down