Skip to content

Commit 62636f9

Browse files
authored
Replace assertBusy of indexExists (#126501)
Relates: #126437 (review)
1 parent 4ea4a29 commit 62636f9

File tree

7 files changed

+33
-19
lines changed

7 files changed

+33
-19
lines changed

server/src/internalClusterTest/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ public void testDeletionOfFailingToRecoverIndexShouldStopRestore() throws Except
748748

749749
logger.info("--> wait for the index to appear");
750750
// that would mean that recovery process started and failing
751-
safeGet(clusterAdmin().prepareHealth(SAFE_AWAIT_TIMEOUT, "test-idx").execute());
751+
awaitIndexExists("test-idx");
752752

753753
logger.info("--> delete index");
754754
cluster().wipeIndices("test-idx");

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@
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;
229231
import static org.hamcrest.Matchers.empty;
230232
import static org.hamcrest.Matchers.equalTo;
231233
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
@@ -1731,6 +1733,31 @@ public static boolean indexExists(String index, Client client) {
17311733
return getIndexResponse.getIndices().length > 0;
17321734
}
17331735

1736+
public static void awaitIndexExists(String index) throws Exception {
1737+
awaitIndexExists(index, client());
1738+
}
1739+
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(
1753+
client.admin()
1754+
.cluster()
1755+
.prepareHealth(SAFE_AWAIT_TIMEOUT, index)
1756+
.setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED)
1757+
.execute()
1758+
);
1759+
}
1760+
17341761
/**
17351762
* Syntactic sugar for enabling allocation for <code>indices</code>
17361763
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void testAutoFollow() throws Exception {
118118
createLeaderIndex("metrics-201901", leaderIndexSettings);
119119

120120
createLeaderIndex("logs-201901", leaderIndexSettings);
121-
assertLongBusy(() -> { assertTrue(ESIntegTestCase.indexExists("copy-logs-201901", followerClient())); });
121+
ESIntegTestCase.awaitIndexExists("copy-logs-201901", followerClient());
122122
createLeaderIndex("transactions-201901", leaderIndexSettings);
123123
assertLongBusy(() -> {
124124
AutoFollowStats autoFollowStats = getAutoFollowStats();

x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ClusterStateWaitThresholdBreachTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void onFailure(Exception e) {
173173
// the shrink index generated in the first attempt must've been deleted!
174174
assertBusy(() -> assertFalse(indexExists(firstAttemptShrinkIndexName[0])));
175175

176-
assertBusy(() -> assertTrue(indexExists(secondCycleShrinkIndexName[0])), 30, TimeUnit.SECONDS);
176+
awaitIndexExists(secondCycleShrinkIndexName[0]);
177177

178178
// at this point, the second shrink attempt was executed and the manged index is looping into the `shrunk-shards-allocated` step as
179179
// waiting for the huge numbers of replicas for the shrunk index to allocate. this will never happen, so let's unblock this

x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleInitialisationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public void testSingleNodeCluster() throws Exception {
179179
ClusterState clusterState = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
180180
RoutingNode routingNodeEntry1 = clusterState.getRoutingNodes().node(node1);
181181
assertThat(routingNodeEntry1.numberOfShardsWithState(STARTED), equalTo(1));
182-
assertBusy(() -> { assertTrue(indexExists("test")); });
182+
awaitIndexExists("test");
183183
IndexLifecycleService indexLifecycleService = internalCluster().getInstance(IndexLifecycleService.class, server_1);
184184
assertThat(indexLifecycleService.getScheduler().jobCount(), equalTo(1));
185185
assertNotNull(indexLifecycleService.getScheduledJob());
@@ -420,7 +420,7 @@ public void testMasterDedicatedDataDedicated() throws Exception {
420420
RoutingNode routingNodeEntry1 = clusterState.getRoutingNodes().node(node2);
421421
assertThat(routingNodeEntry1.numberOfShardsWithState(STARTED), equalTo(1));
422422

423-
assertBusy(() -> assertTrue(indexExists("test")));
423+
awaitIndexExists("test");
424424
assertBusy(() -> {
425425
LifecycleExecutionState lifecycleState = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT)
426426
.get()

x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeDataFrameAnalyticsIntegTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ protected static void assertThatAuditMessagesMatch(String configId, String... ex
367367
// Make sure we wrote to the audit
368368
// Since calls to write the AbstractAuditor are sent and forgot (async) we could have returned from the start,
369369
// finished the job (as this is a very short analytics job), all without the audit being fully written.
370-
assertBusy(() -> assertTrue(indexExists(NotificationsIndex.NOTIFICATIONS_INDEX)));
370+
awaitIndexExists(NotificationsIndex.NOTIFICATIONS_INDEX);
371371

372372
@SuppressWarnings("unchecked")
373373
Matcher<String>[] itemMatchers = Arrays.stream(expectedAuditMessagePrefixes).map(Matchers::startsWith).toArray(Matcher[]::new);

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.elasticsearch.analysis.common.CommonAnalysisPlugin;
1010
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
1111
import org.elasticsearch.cluster.service.ClusterService;
12-
import org.elasticsearch.common.Strings;
1312
import org.elasticsearch.common.regex.Regex;
1413
import org.elasticsearch.common.settings.Settings;
1514
import org.elasticsearch.common.util.concurrent.CountDown;
@@ -33,7 +32,6 @@
3332
import java.util.HashSet;
3433
import java.util.List;
3534
import java.util.Set;
36-
import java.util.concurrent.TimeUnit;
3735
import java.util.stream.Collectors;
3836

3937
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
@@ -157,17 +155,6 @@ protected void waitForMonitoringIndices() throws Exception {
157155
assertBusy(this::ensureMonitoringIndicesYellow);
158156
}
159157

160-
protected void awaitIndexExists(final String index) throws Exception {
161-
assertBusy(() -> assertIndicesExists(index), 30, TimeUnit.SECONDS);
162-
}
163-
164-
private void assertIndicesExists(String... indices) {
165-
logger.trace("checking if index exists [{}]", Strings.arrayToCommaDelimitedString(indices));
166-
for (String index : indices) {
167-
assertThat(indexExists(index), is(true));
168-
}
169-
}
170-
171158
protected void enableMonitoringCollection() {
172159
updateClusterSettings(Settings.builder().put(MonitoringService.ENABLED.getKey(), true));
173160
}

0 commit comments

Comments
 (0)