From 6019a7ff53385c858a88cbeb4b487f27eb228c1d Mon Sep 17 00:00:00 2001 From: David Turner Date: Thu, 10 Apr 2025 08:02:10 +0100 Subject: [PATCH 1/3] Reduce `assertBusy` usage in `testMultipleNodes` Relates #126501 --- .../elasticsearch/test/ESIntegTestCase.java | 23 ++++-------- .../elasticsearch/xpack/ccr/AutoFollowIT.java | 1 + .../monitoring/MultiNodesStatsTests.java | 35 ++++++++++++++----- .../test/MonitoringIntegTestCase.java | 9 ----- 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java index 5a9e6b8b95281..233c3fd9eabd8 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java @@ -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; /** @@ -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) diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java index 6cbe5128929c6..ef301a7a412c2 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java @@ -22,6 +22,7 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.metadata.MetadataIndexTemplateService; import org.elasticsearch.cluster.metadata.Template; +import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.Strings; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java index fbe2e957fc73b..751ce217b7777 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java @@ -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; @@ -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 { @@ -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(); @@ -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. @@ -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, ALL_MONITORING_INDICES); + 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; + }); + } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java index 6c301032c1b94..eee7671c118b0 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java @@ -124,10 +124,6 @@ protected void deleteMonitoringIndices() { assertAcked(client().admin().indices().prepareDelete(ALL_MONITORING_INDICES)); } - protected void ensureMonitoringIndicesYellow() { - ensureYellowAndNoInitializingShards(".monitoring-es-*"); - } - protected List> monitoringWatches() { final ClusterService clusterService = clusterService(); @@ -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)); } From c21a50e4b21236e44fa94fe98000d28426e9a4b7 Mon Sep 17 00:00:00 2001 From: elasticsearchmachine Date: Thu, 10 Apr 2025 07:15:15 +0000 Subject: [PATCH 2/3] [CI] Auto commit changes from spotless --- .../java/org/elasticsearch/xpack/ccr/AutoFollowIT.java | 1 - 1 file changed, 1 deletion(-) diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java index ef301a7a412c2..6cbe5128929c6 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java @@ -22,7 +22,6 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.metadata.MetadataIndexTemplateService; import org.elasticsearch.cluster.metadata.Template; -import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.Strings; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; From 0fdd0ef25ca9e6cc6bf8903a1a89f2665fce5580 Mon Sep 17 00:00:00 2001 From: David Turner Date: Thu, 10 Apr 2025 08:23:01 +0100 Subject: [PATCH 3/3] Wait for ES monitoring indices specifically --- .../elasticsearch/xpack/monitoring/MultiNodesStatsTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java index 751ce217b7777..2d51303b1d939 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java @@ -104,7 +104,7 @@ private void waitForMonitoringIndices() throws Exception { .wildcardOptions(IndicesOptions.WildcardOptions.builder().allowEmptyExpressions(true)) .build(); awaitClusterState(cs -> { - final var indices = indexNameExpressionResolver.concreteIndices(cs, indicesOptions, ALL_MONITORING_INDICES); + final var indices = indexNameExpressionResolver.concreteIndices(cs, indicesOptions, ".monitoring-es-*"); if (indices.length == 0) { return false; }