Skip to content

Commit d467c50

Browse files
Make TimeSeriesLifecycleActionsIT.testWaitForSnapshot and testWaitForSnapshotSlmExecutedBefore wait for snaphost (#51892) (#52419)
* waitForSnapshot tests rework * Refactor assertBusy Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
1 parent d17ecb5 commit d467c50

File tree

1 file changed

+67
-34
lines changed

1 file changed

+67
-34
lines changed

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

Lines changed: 67 additions & 34 deletions
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;
@@ -329,64 +330,78 @@ public void testAllocateActionOnlyReplicas() throws Exception {
329330
});
330331
}
331332

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

342345
String repo = createSnapshotRepo();
343-
createSlmPolicy(smlPolicy, repo);
346+
createSlmPolicy(slmPolicy, repo);
344347

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

347-
Request request = new Request("PUT", "/_slm/policy/" + smlPolicy + "/_execute");
356+
Request request = new Request("PUT", "/_slm/policy/" + slmPolicy + "/_execute");
348357
assertOK(client().performRequest(request));
349358

350-
351-
assertBusy(() -> assertThat(getStepKeyForIndex(index).getAction(), equalTo("completed")), 2, TimeUnit.MINUTES);
352-
353-
request = new Request("DELETE", "/_slm/policy/" + smlPolicy);
354-
assertOK(client().performRequest(request));
355-
356-
request = new Request("DELETE", "/_snapshot/" + repo);
357-
assertOK(client().performRequest(request));
359+
assertBusy(() -> assertThat(getStepKeyForIndex(index).getAction(), equalTo("complete")), slmPolicy);
358360
}
359361

360-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/50781")
361362
public void testWaitForSnapshotSlmExecutedBefore() throws Exception {
362363
createIndexWithSettings(index, Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
363364
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0));
364-
String smlPolicy = randomAlphaOfLengthBetween(4, 10);
365-
createNewSingletonPolicy("delete", new WaitForSnapshotAction(smlPolicy));
365+
String slmPolicy = randomAlphaOfLengthBetween(4, 10);
366+
createNewSingletonPolicy("delete", new WaitForSnapshotAction(slmPolicy));
366367

367368
String repo = createSnapshotRepo();
368-
createSlmPolicy(smlPolicy, repo);
369+
createSlmPolicy(slmPolicy, repo);
369370

370-
Request request = new Request("PUT", "/_slm/policy/" + smlPolicy + "/_execute");
371+
Request request = new Request("PUT", "/_slm/policy/" + slmPolicy + "/_execute");
371372
assertOK(client().performRequest(request));
372373

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

377-
request = new Request("PUT", "/_slm/policy/" + smlPolicy + "/_execute");
378-
assertOK(client().performRequest(request));
385+
assertBusy( () -> {
386+
Map<String, Object> indexILMState = explainIndex(index);
387+
assertThat(indexILMState.get("failed_step"), nullValue());
388+
assertThat(indexILMState.get("action"), is("wait_for_snapshot"));
389+
assertThat(indexILMState.get("step"), is("wait-for-snapshot"));
390+
}, slmPolicy);
379391

380-
request = new Request("PUT", "/_slm/policy/" + smlPolicy + "/_execute");
392+
request = new Request("PUT", "/_slm/policy/" + slmPolicy + "/_execute");
381393
assertOK(client().performRequest(request));
382394

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

388-
request = new Request("DELETE", "/_snapshot/" + repo);
389-
assertOK(client().performRequest(request));
404+
assertBusy(() -> assertThat(getStepKeyForIndex(index).getAction(), equalTo("complete")), slmPolicy);
390405
}
391406

392407
public void testDelete() throws Exception {
@@ -1811,11 +1826,29 @@ private String createSnapshotRepo() throws IOException {
18111826
.field("type", "fs")
18121827
.startObject("settings")
18131828
.field("compress", randomBoolean())
1814-
.field("location", System.getProperty("tests.path.repo"))
1815-
.field("max_snapshot_bytes_per_sec", "256b")
1829+
//random location to avoid clash with other snapshots
1830+
.field("location", System.getProperty("tests.path.repo")+ "/" + randomAlphaOfLengthBetween(4, 10))
1831+
.field("max_snapshot_bytes_per_sec", "100m")
18161832
.endObject()
18171833
.endObject()));
18181834
assertOK(client().performRequest(request));
18191835
return repo;
18201836
}
1837+
1838+
//adds debug information for waitForSnapshot tests
1839+
private void assertBusy(CheckedRunnable<Exception> runnable, String slmPolicy) throws Exception {
1840+
assertBusy(() -> {
1841+
try {
1842+
runnable.run();
1843+
} catch (AssertionError e) {
1844+
Map<String, Object> slm;
1845+
try (InputStream is = client().performRequest(new Request("GET", "/_slm/policy/" + slmPolicy)).getEntity().getContent()) {
1846+
slm = XContentHelper.convertToMap(XContentType.JSON.xContent(), is, false);
1847+
} catch (Exception ignored) {
1848+
slm = new HashMap<>();
1849+
}
1850+
throw new AssertionError("Index:" + explainIndex(index) + "\nSLM:" + slm, e);
1851+
}
1852+
});
1853+
}
18211854
}

0 commit comments

Comments
 (0)