Skip to content

Commit 1b80a58

Browse files
authored
Improve template cleanup in ESRestTestCase (elastic#70066)
Backport elastic#70052 to the 7.x branch. Before this change upon wiping the cluster, we would get a list of all legacy and index component templates. For each template first attempt to delete it as legacy template if that returned a 404 then remove it as composable index template. In the worst case this means that we would make double the amount of delete requests for templates then is necessary. This change first gets all composable index templates (if exist and if the cluster supports it) and then deletes these composable index templates. After this separately get a list of all legacy templates and then delete those legacy templates. Relates to elastic#69973
1 parent 465a3ac commit 1b80a58

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

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

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
import static org.elasticsearch.rest.action.admin.indices.RestCloseIndexAction.WAIT_FOR_ACTIVE_SHARDS_DEFAULT_DEPRECATION_MESSAGE;
101101
import static org.hamcrest.Matchers.anEmptyMap;
102102
import static org.hamcrest.Matchers.anyOf;
103-
import static org.hamcrest.Matchers.containsString;
104103
import static org.hamcrest.Matchers.equalTo;
105104
import static org.hamcrest.Matchers.everyItem;
106105
import static org.hamcrest.Matchers.in;
@@ -603,31 +602,31 @@ private void wipeCluster() throws Exception {
603602
* slows down the test because xpack will just recreate
604603
* them.
605604
*/
606-
Request request = new Request("GET", "_cat/templates");
607-
request.addParameter("h", "name");
608-
String templates = EntityUtils.toString(adminClient().performRequest(request).getEntity());
609-
if (false == "".equals(templates)) {
610-
for (String template : templates.split("\n")) {
611-
if (isXPackTemplate(template)) continue;
612-
if ("".equals(template)) {
613-
throw new IllegalStateException("empty template in templates list:\n" + templates);
605+
try {
606+
Request getTemplatesRequest = new Request("GET", "_index_template");
607+
getTemplatesRequest.setOptions(allowTypesRemovalWarnings());
608+
Map<String, Object> composableIndexTemplates = XContentHelper.convertToMap(JsonXContent.jsonXContent,
609+
EntityUtils.toString(adminClient().performRequest(getTemplatesRequest).getEntity()), false);
610+
List<String> names = ((List<?>) composableIndexTemplates.get("index_templates")).stream()
611+
.map(ct -> (String) ((Map<?, ?>) ct).get("name"))
612+
.collect(Collectors.toList());
613+
for (String name : names) {
614+
if (isXPackTemplate(name)) {
615+
continue;
614616
}
615-
logger.info("Clearing template [{}]", template);
616617
try {
617-
adminClient().performRequest(new Request("DELETE", "_template/" + template));
618+
adminClient().performRequest(new Request("DELETE", "_index_template/" + name));
618619
} catch (ResponseException e) {
619-
// This is fine, it could be a V2 template
620-
assertThat(e.getMessage(), containsString("index_template [" + template + "] missing"));
621-
try {
622-
adminClient().performRequest(new Request("DELETE", "_index_template/" + template));
623-
} catch (ResponseException e2) {
624-
// We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
625-
}
620+
logger.debug(new ParameterizedMessage("unable to remove index template {}", name), e);
626621
}
627622
}
623+
} catch (Exception e) {
624+
logger.info("ignoring exception removing all composable index templates", e);
625+
// We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
628626
}
629627
try {
630628
Request compReq = new Request("GET", "_component_template");
629+
compReq.setOptions(allowTypesRemovalWarnings());
631630
String componentTemplates = EntityUtils.toString(adminClient().performRequest(compReq).getEntity());
632631
Map<String, Object> cTemplates = XContentHelper.convertToMap(JsonXContent.jsonXContent, componentTemplates, false);
633632
@SuppressWarnings("unchecked")
@@ -641,13 +640,27 @@ private void wipeCluster() throws Exception {
641640
}
642641
adminClient().performRequest(new Request("DELETE", "_component_template/" + componentTemplate));
643642
} catch (ResponseException e) {
644-
logger.debug(new ParameterizedMessage("unable to remove component template {}", componentTemplate), e);
643+
logger.debug(new ParameterizedMessage("unable to remove component template {}", componentTemplate), e);
645644
}
646645
}
647646
} catch (Exception e) {
648647
logger.info("ignoring exception removing all component templates", e);
649648
// We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
650649
}
650+
Request getLegacyTemplatesRequest = new Request("GET", "_template");
651+
getLegacyTemplatesRequest.setOptions(allowTypesRemovalWarnings());
652+
Map<String, Object> legacyTemplates = XContentHelper.convertToMap(JsonXContent.jsonXContent,
653+
EntityUtils.toString(adminClient().performRequest(getLegacyTemplatesRequest).getEntity()), false);
654+
for (String name : legacyTemplates.keySet()) {
655+
if (isXPackTemplate(name)) {
656+
continue;
657+
}
658+
try {
659+
adminClient().performRequest(new Request("DELETE", "_template/" + name));
660+
} catch (ResponseException e) {
661+
logger.debug(new ParameterizedMessage("unable to remove index template {}", name), e);
662+
}
663+
}
651664
} else {
652665
logger.debug("Clearing all templates");
653666
adminClient().performRequest(new Request("DELETE", "_template/*"));

0 commit comments

Comments
 (0)