Skip to content

Commit ac66441

Browse files
[7.6] Make TimeSeriesLifecycleActionsIT.testWaitForSnapshot and testWaitForSnapshotSlmExecutedBefore wait for snaphost (#51892) (#52421)
* Make TimeSeriesLifecycleActionsIT.testWaitForSnapshot and testWaitForSnapshotSlmExecutedBefore wait for snaphost (#51892) * waitForSnapshot tests rework * Refactor assertBusy Co-authored-by: Elastic Machine <[email protected]> * fixed test failure Co-authored-by: Elastic Machine <[email protected]>
1 parent 5bf32f5 commit ac66441

File tree

1 file changed

+67
-33
lines changed

1 file changed

+67
-33
lines changed

x-pack/plugin/ilm/qa/multi-node/src/test/java/org/elasticsearch/xpack/ilm/TimeSeriesLifecycleActionsIT.java

+67-33
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.client.ResponseException;
1616
import org.elasticsearch.client.RestClient;
1717
import org.elasticsearch.cluster.metadata.IndexMetaData;
18+
import org.elasticsearch.common.CheckedRunnable;
1819
import org.elasticsearch.common.Nullable;
1920
import org.elasticsearch.common.Strings;
2021
import org.elasticsearch.common.settings.Settings;
@@ -326,63 +327,78 @@ public void testAllocateActionOnlyReplicas() throws Exception {
326327
});
327328
}
328329

329-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/50781")
330330
public void testWaitForSnapshot() throws Exception {
331331
createIndexWithSettings(index, Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
332332
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0));
333-
String smlPolicy = randomAlphaOfLengthBetween(4, 10);
334-
createNewSingletonPolicy("delete", new WaitForSnapshotAction(smlPolicy));
333+
String slmPolicy = randomAlphaOfLengthBetween(4, 10);
334+
createNewSingletonPolicy("delete", new WaitForSnapshotAction(slmPolicy));
335335
updatePolicy(index, policy);
336-
assertBusy(() -> assertThat(getStepKeyForIndex(index).getAction(), equalTo("wait_for_snapshot")));
337-
assertBusy(() -> assertThat(getFailedStepForIndex(index), equalTo("wait-for-snapshot")));
336+
assertBusy( () -> {
337+
Map<String, Object> indexILMState = explainIndex(index);
338+
assertThat(indexILMState.get("action"), is("wait_for_snapshot"));
339+
assertThat(indexILMState.get("failed_step"), is("wait-for-snapshot"));
340+
}, slmPolicy);
338341

339342
String repo = createSnapshotRepo();
340-
createSlmPolicy(smlPolicy, repo);
343+
createSlmPolicy(slmPolicy, repo);
341344

342-
assertBusy(() -> assertThat(getStepKeyForIndex(index).getAction(), equalTo("wait_for_snapshot")));
345+
assertBusy( () -> {
346+
Map<String, Object> indexILMState = explainIndex(index);
347+
//wait for step to notice that the slm policy is created and to get out of error
348+
assertThat(indexILMState.get("failed_step"), nullValue());
349+
assertThat(indexILMState.get("action"), is("wait_for_snapshot"));
350+
assertThat(indexILMState.get("step"), is("wait-for-snapshot"));
351+
}, slmPolicy);
343352

344-
Request request = new Request("PUT", "/_slm/policy/" + smlPolicy + "/_execute");
353+
Request request = new Request("PUT", "/_slm/policy/" + slmPolicy + "/_execute");
345354
assertOK(client().performRequest(request));
346355

347-
348-
assertBusy(() -> assertThat(getStepKeyForIndex(index).getAction(), equalTo("completed")), 2, TimeUnit.MINUTES);
349-
350-
request = new Request("DELETE", "/_slm/policy/" + smlPolicy);
351-
assertOK(client().performRequest(request));
352-
353-
request = new Request("DELETE", "/_snapshot/" + repo);
354-
assertOK(client().performRequest(request));
356+
assertBusy(() -> assertThat(getStepKeyForIndex(index).getAction(), equalTo("completed")), slmPolicy);
355357
}
356358

357359
public void testWaitForSnapshotSlmExecutedBefore() throws Exception {
358360
createIndexWithSettings(index, Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
359361
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0));
360-
String smlPolicy = randomAlphaOfLengthBetween(4, 10);
361-
createNewSingletonPolicy("delete", new WaitForSnapshotAction(smlPolicy));
362+
String slmPolicy = randomAlphaOfLengthBetween(4, 10);
363+
createNewSingletonPolicy("delete", new WaitForSnapshotAction(slmPolicy));
362364

363365
String repo = createSnapshotRepo();
364-
createSlmPolicy(smlPolicy, repo);
366+
createSlmPolicy(slmPolicy, repo);
365367

366-
Request request = new Request("PUT", "/_slm/policy/" + smlPolicy + "/_execute");
368+
Request request = new Request("PUT", "/_slm/policy/" + slmPolicy + "/_execute");
367369
assertOK(client().performRequest(request));
368370

371+
//wait for slm to finish execution
372+
assertBusy(() -> {
373+
Response response = client().performRequest(new Request("GET", "/_slm/policy/" + slmPolicy));
374+
try (InputStream is = response.getEntity().getContent()) {
375+
Map<String, Object> responseMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true);
376+
assertEquals(1, ((Map<?, ?>) ((Map<?, ?>) responseMap.get(slmPolicy)).get("stats")).get("snapshots_taken"));
377+
}
378+
}, slmPolicy);
379+
369380
updatePolicy(index, policy);
370-
assertBusy(() -> assertThat(getStepKeyForIndex(index).getAction(), equalTo("wait_for_snapshot")));
371-
assertBusy(() -> assertThat(getStepKeyForIndex(index).getName(), equalTo("wait-for-snapshot")));
372381

373-
request = new Request("PUT", "/_slm/policy/" + smlPolicy + "/_execute");
374-
assertOK(client().performRequest(request));
382+
assertBusy( () -> {
383+
Map<String, Object> indexILMState = explainIndex(index);
384+
assertThat(indexILMState.get("failed_step"), nullValue());
385+
assertThat(indexILMState.get("action"), is("wait_for_snapshot"));
386+
assertThat(indexILMState.get("step"), is("wait-for-snapshot"));
387+
}, slmPolicy);
375388

376-
request = new Request("PUT", "/_slm/policy/" + smlPolicy + "/_execute");
389+
request = new Request("PUT", "/_slm/policy/" + slmPolicy + "/_execute");
377390
assertOK(client().performRequest(request));
378391

379-
assertBusy(() -> assertThat(getStepKeyForIndex(index).getAction(), equalTo("completed")), 2, TimeUnit.MINUTES);
380-
381-
request = new Request("DELETE", "/_slm/policy/" + smlPolicy);
382-
assertOK(client().performRequest(request));
392+
//wait for slm to finish execution
393+
assertBusy(() -> {
394+
Response response = client().performRequest(new Request("GET", "/_slm/policy/" + slmPolicy));
395+
try (InputStream is = response.getEntity().getContent()) {
396+
Map<String, Object> responseMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true);
397+
assertEquals(2, ((Map<?, ?>) ((Map<?, ?>) responseMap.get(slmPolicy)).get("stats")).get("snapshots_taken"));
398+
}
399+
}, slmPolicy);
383400

384-
request = new Request("DELETE", "/_snapshot/" + repo);
385-
assertOK(client().performRequest(request));
401+
assertBusy(() -> assertThat(getStepKeyForIndex(index).getAction(), equalTo("completed")), slmPolicy);
386402
}
387403

388404
public void testDelete() throws Exception {
@@ -1677,11 +1693,29 @@ private String createSnapshotRepo() throws IOException {
16771693
.field("type", "fs")
16781694
.startObject("settings")
16791695
.field("compress", randomBoolean())
1680-
.field("location", System.getProperty("tests.path.repo"))
1681-
.field("max_snapshot_bytes_per_sec", "256b")
1696+
//random location to avoid clash with other snapshots
1697+
.field("location", System.getProperty("tests.path.repo")+ "/" + randomAlphaOfLengthBetween(4, 10))
1698+
.field("max_snapshot_bytes_per_sec", "100m")
16821699
.endObject()
16831700
.endObject()));
16841701
assertOK(client().performRequest(request));
16851702
return repo;
16861703
}
1704+
1705+
//adds debug information for waitForSnapshot tests
1706+
private void assertBusy(CheckedRunnable<Exception> runnable, String slmPolicy) throws Exception {
1707+
assertBusy(() -> {
1708+
try {
1709+
runnable.run();
1710+
} catch (AssertionError e) {
1711+
Map<String, Object> slm;
1712+
try (InputStream is = client().performRequest(new Request("GET", "/_slm/policy/" + slmPolicy)).getEntity().getContent()) {
1713+
slm = XContentHelper.convertToMap(XContentType.JSON.xContent(), is, false);
1714+
} catch (Exception ignored) {
1715+
slm = new HashMap<>();
1716+
}
1717+
throw new AssertionError("Index:" + explainIndex(index) + "\nSLM:" + slm, e);
1718+
}
1719+
});
1720+
}
16871721
}

0 commit comments

Comments
 (0)