Skip to content

Commit 16070a3

Browse files
authored
Fix tests in TimeSeriesDataStreamsIT (#126851)
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
1 parent 0d01f88 commit 16070a3

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

muted-tests.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,6 @@ tests:
387387
- class: org.elasticsearch.xpack.esql.action.EsqlActionIT
388388
method: testQueryOnEmptyDataIndex
389389
issue: https://github.com/elastic/elasticsearch/issues/126580
390-
- class: org.elasticsearch.xpack.ilm.TimeSeriesDataStreamsIT
391-
method: testShrinkActionInPolicyWithoutHotPhase
392-
issue: https://github.com/elastic/elasticsearch/issues/126746
393390
- class: org.elasticsearch.xpack.test.rest.XPackRestIT
394391
method: test {p0=transform/transforms_start_stop/Test start/stop/start continuous transform}
395392
issue: https://github.com/elastic/elasticsearch/issues/126755

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,12 +2044,34 @@ protected Map<String, Object> getIndexMappingAsMap(String index) throws IOExcept
20442044
}
20452045

20462046
protected static boolean indexExists(String index) throws IOException {
2047-
return indexExists(client(), index);
2047+
// We use the /_cluster/health/{index} API to ensure the index exists on the master node - which means all nodes see the index.
2048+
Request request = new Request("GET", "/_cluster/health/" + index);
2049+
request.addParameter("timeout", "0");
2050+
request.addParameter("level", "indices");
2051+
try {
2052+
final var response = client().performRequest(request);
2053+
@SuppressWarnings("unchecked")
2054+
final var indices = (Map<String, Object>) entityAsMap(response).get("indices");
2055+
return indices.containsKey(index);
2056+
} catch (ResponseException e) {
2057+
if (e.getResponse().getStatusLine().getStatusCode() == HttpStatus.SC_REQUEST_TIMEOUT) {
2058+
return false;
2059+
}
2060+
throw e;
2061+
}
20482062
}
20492063

2050-
protected static boolean indexExists(RestClient client, String index) throws IOException {
2051-
Response response = client.performRequest(new Request("HEAD", "/" + index));
2052-
return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode();
2064+
protected static void awaitIndexExists(String index) throws IOException {
2065+
awaitIndexExists(index, SAFE_AWAIT_TIMEOUT);
2066+
}
2067+
2068+
protected static void awaitIndexExists(String index, TimeValue timeout) throws IOException {
2069+
// We use the /_cluster/health/{index} API to ensure the index exists on the master node - which means all nodes see the index.
2070+
ensureHealth(client(), index, request -> request.addParameter("timeout", timeout.toString()));
2071+
}
2072+
2073+
protected static void awaitIndexDoesNotExist(String index, TimeValue timeout) throws Exception {
2074+
assertBusy(() -> assertFalse(indexExists(index)), timeout.millis(), TimeUnit.MILLISECONDS);
20532075
}
20542076

20552077
/**

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.cluster.metadata.IndexMetadata;
1515
import org.elasticsearch.cluster.metadata.Template;
1616
import org.elasticsearch.common.xcontent.XContentHelper;
17+
import org.elasticsearch.core.TimeValue;
1718
import org.elasticsearch.index.engine.EngineConfig;
1819
import org.elasticsearch.test.rest.ESRestTestCase;
1920
import org.elasticsearch.xcontent.XContentType;
@@ -153,7 +154,7 @@ public void testShrinkActionInPolicyWithoutHotPhase() throws Exception {
153154
);
154155

155156
String shrunkenIndex = waitAndGetShrinkIndexName(client(), backingIndexName);
156-
assertBusy(() -> assertTrue(indexExists(shrunkenIndex)), 30, TimeUnit.SECONDS);
157+
awaitIndexExists(shrunkenIndex, TimeValue.timeValueSeconds(30));
157158
assertBusy(() -> assertThat(getStepKeyForIndex(client(), shrunkenIndex), equalTo(PhaseCompleteStep.finalStep("warm").getKey())));
158159
assertBusy(() -> assertThat("the original index must've been deleted", indexExists(backingIndexName), is(false)));
159160
}
@@ -182,8 +183,8 @@ public void testSearchableSnapshotAction() throws Exception {
182183
// Manual rollover the original index such that it's not the write index in the data stream anymore
183184
rolloverMaxOneDocCondition(client(), dataStream);
184185

185-
assertBusy(() -> assertThat(indexExists(restoredIndexName), is(true)));
186-
assertBusy(() -> assertFalse(indexExists(backingIndexName)), 60, TimeUnit.SECONDS);
186+
awaitIndexExists(restoredIndexName);
187+
awaitIndexDoesNotExist(backingIndexName, TimeValue.timeValueSeconds(60));
187188
assertBusy(
188189
() -> assertThat(explainIndex(client(), restoredIndexName).get("step"), is(PhaseCompleteStep.NAME)),
189190
30,
@@ -211,15 +212,13 @@ public void testReadOnlyAction() throws Exception {
211212
// Manual rollover the original index such that it's not the write index in the data stream anymore
212213
rolloverMaxOneDocCondition(client(), dataStream);
213214

214-
assertBusy(
215-
() -> assertThat(explainIndex(client(), backingIndexName).get("step"), is(PhaseCompleteStep.NAME)),
216-
30,
217-
TimeUnit.SECONDS
218-
);
219-
assertThat(
220-
getOnlyIndexSettings(client(), backingIndexName).get(IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.getKey()),
221-
equalTo("true")
222-
);
215+
assertBusy(() -> {
216+
assertThat(explainIndex(client(), backingIndexName).get("step"), is(PhaseCompleteStep.NAME));
217+
assertThat(
218+
getOnlyIndexSettings(client(), backingIndexName).get(IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.getKey()),
219+
equalTo("true")
220+
);
221+
}, 30, TimeUnit.SECONDS);
223222
}
224223

225224
public void testFreezeAction() throws Exception {
@@ -243,10 +242,10 @@ public void testFreezeAction() throws Exception {
243242
containsString("The freeze action in ILM is deprecated and will be removed in a future version")
244243
);
245244
}
245+
Map<String, Object> settings = getOnlyIndexSettings(client(), backingIndexName);
246+
assertNull(settings.get("index.frozen"));
246247
}, 30, TimeUnit.SECONDS);
247248

248-
Map<String, Object> settings = getOnlyIndexSettings(client(), backingIndexName);
249-
assertNull(settings.get("index.frozen"));
250249
}
251250

252251
public void checkForceMergeAction(String codec) throws Exception {

0 commit comments

Comments
 (0)