Skip to content

Replace assertBusy of indexExists #126501

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
merged 8 commits into from
Apr 10, 2025
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 @@ -748,7 +748,7 @@ public void testDeletionOfFailingToRecoverIndexShouldStopRestore() throws Except

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

logger.info("--> delete index");
cluster().wipeIndices("test-idx");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@
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.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
Expand Down Expand Up @@ -1731,6 +1733,31 @@ public static boolean indexExists(String index, Client client) {
return getIndexResponse.getIndices().length > 0;
}

public static void awaitIndexExists(String index) throws Exception {
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(
client.admin()
.cluster()
.prepareHealth(SAFE_AWAIT_TIMEOUT, index)
.setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED)
.execute()
);
}

/**
* Syntactic sugar for enabling allocation for <code>indices</code>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void testAutoFollow() throws Exception {
createLeaderIndex("metrics-201901", leaderIndexSettings);

createLeaderIndex("logs-201901", leaderIndexSettings);
assertLongBusy(() -> { assertTrue(ESIntegTestCase.indexExists("copy-logs-201901", followerClient())); });
ESIntegTestCase.awaitIndexExists("copy-logs-201901", followerClient());
createLeaderIndex("transactions-201901", leaderIndexSettings);
assertLongBusy(() -> {
AutoFollowStats autoFollowStats = getAutoFollowStats();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void onFailure(Exception e) {
// the shrink index generated in the first attempt must've been deleted!
assertBusy(() -> assertFalse(indexExists(firstAttemptShrinkIndexName[0])));

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

// at this point, the second shrink attempt was executed and the manged index is looping into the `shrunk-shards-allocated` step as
// waiting for the huge numbers of replicas for the shrunk index to allocate. this will never happen, so let's unblock this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void testSingleNodeCluster() throws Exception {
ClusterState clusterState = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
RoutingNode routingNodeEntry1 = clusterState.getRoutingNodes().node(node1);
assertThat(routingNodeEntry1.numberOfShardsWithState(STARTED), equalTo(1));
assertBusy(() -> { assertTrue(indexExists("test")); });
awaitIndexExists("test");
IndexLifecycleService indexLifecycleService = internalCluster().getInstance(IndexLifecycleService.class, server_1);
assertThat(indexLifecycleService.getScheduler().jobCount(), equalTo(1));
assertNotNull(indexLifecycleService.getScheduledJob());
Expand Down Expand Up @@ -420,7 +420,7 @@ public void testMasterDedicatedDataDedicated() throws Exception {
RoutingNode routingNodeEntry1 = clusterState.getRoutingNodes().node(node2);
assertThat(routingNodeEntry1.numberOfShardsWithState(STARTED), equalTo(1));

assertBusy(() -> assertTrue(indexExists("test")));
awaitIndexExists("test");
assertBusy(() -> {
LifecycleExecutionState lifecycleState = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT)
.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ protected static void assertThatAuditMessagesMatch(String configId, String... ex
// Make sure we wrote to the audit
// Since calls to write the AbstractAuditor are sent and forgot (async) we could have returned from the start,
// finished the job (as this is a very short analytics job), all without the audit being fully written.
assertBusy(() -> assertTrue(indexExists(NotificationsIndex.NOTIFICATIONS_INDEX)));
awaitIndexExists(NotificationsIndex.NOTIFICATIONS_INDEX);

@SuppressWarnings("unchecked")
Matcher<String>[] itemMatchers = Arrays.stream(expectedAuditMessagePrefixes).map(Matchers::startsWith).toArray(Matcher[]::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.elasticsearch.analysis.common.CommonAnalysisPlugin;
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.CountDown;
Expand All @@ -33,7 +32,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

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

protected void awaitIndexExists(final String index) throws Exception {
assertBusy(() -> assertIndicesExists(index), 30, TimeUnit.SECONDS);
}

private void assertIndicesExists(String... indices) {
logger.trace("checking if index exists [{}]", Strings.arrayToCommaDelimitedString(indices));
for (String index : indices) {
assertThat(indexExists(index), is(true));
}
}

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