Skip to content

Commit 57e082e

Browse files
authored
Fix tests in TimeSeriesDataStreamsIT (#126851) (#127015)
These tests had the potential to fail when subsequent requests would hit different nodes with different versions of the cluster state. Only one of these tests failed already, but we fix the other ones proactively to avoid future failures. Fixes #126746 (cherry picked from commit 16070a3) # Conflicts: # muted-tests.yml # test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java # x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesDataStreamsIT.java
1 parent a136a0d commit 57e082e

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,8 +2052,34 @@ protected Map<String, Object> getIndexMappingAsMap(String index) throws IOExcept
20522052
}
20532053

20542054
protected static boolean indexExists(String index) throws IOException {
2055-
Response response = client().performRequest(new Request("HEAD", "/" + index));
2056-
return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode();
2055+
// We use the /_cluster/health/{index} API to ensure the index exists on the master node - which means all nodes see the index.
2056+
Request request = new Request("GET", "/_cluster/health/" + index);
2057+
request.addParameter("timeout", "0");
2058+
request.addParameter("level", "indices");
2059+
try {
2060+
final var response = client().performRequest(request);
2061+
@SuppressWarnings("unchecked")
2062+
final var indices = (Map<String, Object>) entityAsMap(response).get("indices");
2063+
return indices.containsKey(index);
2064+
} catch (ResponseException e) {
2065+
if (e.getResponse().getStatusLine().getStatusCode() == HttpStatus.SC_REQUEST_TIMEOUT) {
2066+
return false;
2067+
}
2068+
throw e;
2069+
}
2070+
}
2071+
2072+
protected static void awaitIndexExists(String index) throws IOException {
2073+
awaitIndexExists(index, SAFE_AWAIT_TIMEOUT);
2074+
}
2075+
2076+
protected static void awaitIndexExists(String index, TimeValue timeout) throws IOException {
2077+
// We use the /_cluster/health/{index} API to ensure the index exists on the master node - which means all nodes see the index.
2078+
ensureHealth(client(), index, request -> request.addParameter("timeout", timeout.toString()));
2079+
}
2080+
2081+
protected static void awaitIndexDoesNotExist(String index, TimeValue timeout) throws Exception {
2082+
assertBusy(() -> assertFalse(indexExists(index)), timeout.millis(), TimeUnit.MILLISECONDS);
20572083
}
20582084

20592085
/**

x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesDataStreamsIT.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.cluster.metadata.IndexMetadata;
1313
import org.elasticsearch.cluster.metadata.Template;
1414
import org.elasticsearch.common.xcontent.XContentHelper;
15+
import org.elasticsearch.core.TimeValue;
1516
import org.elasticsearch.index.engine.EngineConfig;
1617
import org.elasticsearch.test.rest.ESRestTestCase;
1718
import org.elasticsearch.xcontent.XContentType;
@@ -144,7 +145,7 @@ public void testShrinkActionInPolicyWithoutHotPhase() throws Exception {
144145
);
145146

146147
String shrunkenIndex = waitAndGetShrinkIndexName(client(), backingIndexName);
147-
assertBusy(() -> assertTrue(indexExists(shrunkenIndex)), 30, TimeUnit.SECONDS);
148+
awaitIndexExists(shrunkenIndex, TimeValue.timeValueSeconds(30));
148149
assertBusy(() -> assertThat(getStepKeyForIndex(client(), shrunkenIndex), equalTo(PhaseCompleteStep.finalStep("warm").getKey())));
149150
assertBusy(() -> assertThat("the original index must've been deleted", indexExists(backingIndexName), is(false)));
150151
}
@@ -173,8 +174,8 @@ public void testSearchableSnapshotAction() throws Exception {
173174
// Manual rollover the original index such that it's not the write index in the data stream anymore
174175
rolloverMaxOneDocCondition(client(), dataStream);
175176

176-
assertBusy(() -> assertThat(indexExists(restoredIndexName), is(true)));
177-
assertBusy(() -> assertFalse(indexExists(backingIndexName)), 60, TimeUnit.SECONDS);
177+
awaitIndexExists(restoredIndexName);
178+
awaitIndexDoesNotExist(backingIndexName, TimeValue.timeValueSeconds(60));
178179
assertBusy(
179180
() -> assertThat(explainIndex(client(), restoredIndexName).get("step"), is(PhaseCompleteStep.NAME)),
180181
30,
@@ -202,15 +203,13 @@ public void testReadOnlyAction() throws Exception {
202203
// Manual rollover the original index such that it's not the write index in the data stream anymore
203204
rolloverMaxOneDocCondition(client(), dataStream);
204205

205-
assertBusy(
206-
() -> assertThat(explainIndex(client(), backingIndexName).get("step"), is(PhaseCompleteStep.NAME)),
207-
30,
208-
TimeUnit.SECONDS
209-
);
210-
assertThat(
211-
getOnlyIndexSettings(client(), backingIndexName).get(IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.getKey()),
212-
equalTo("true")
213-
);
206+
assertBusy(() -> {
207+
assertThat(explainIndex(client(), backingIndexName).get("step"), is(PhaseCompleteStep.NAME));
208+
assertThat(
209+
getOnlyIndexSettings(client(), backingIndexName).get(IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.getKey()),
210+
equalTo("true")
211+
);
212+
}, 30, TimeUnit.SECONDS);
214213
}
215214

216215
public void testFreezeAction() throws Exception {
@@ -232,14 +231,12 @@ public void testFreezeAction() throws Exception {
232231
// Manual rollover the original index such that it's not the write index in the data stream anymore
233232
rolloverMaxOneDocCondition(client(), dataStream);
234233

235-
assertBusy(
236-
() -> assertThat(explainIndex(client(), backingIndexName).get("step"), is(PhaseCompleteStep.NAME)),
237-
30,
238-
TimeUnit.SECONDS
239-
);
234+
assertBusy(() -> {
235+
assertThat(explainIndex(client(), backingIndexName).get("step"), is(PhaseCompleteStep.NAME));
236+
Map<String, Object> settings = getOnlyIndexSettings(client(), backingIndexName);
237+
assertNull(settings.get("index.frozen"));
238+
}, 30, TimeUnit.SECONDS);
240239

241-
Map<String, Object> settings = getOnlyIndexSettings(client(), backingIndexName);
242-
assertNull(settings.get("index.frozen"));
243240
}
244241

245242
public void checkForceMergeAction(String codec) throws Exception {

0 commit comments

Comments
 (0)