Skip to content

Commit

Permalink
Improve template cleanup in ESRestTestCase (elastic#70052)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
martijnvg authored Mar 8, 2021
1 parent 60d53c0 commit 172ca20
Showing 1 changed file with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
import static java.util.Collections.unmodifiableList;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.in;
Expand Down Expand Up @@ -572,28 +571,26 @@ private void wipeCluster() throws Exception {
* slows down the test because xpack will just recreate
* them.
*/
Request request = new Request("GET", "_cat/templates");
request.addParameter("h", "name");
String templates = EntityUtils.toString(adminClient().performRequest(request).getEntity());
if (false == "".equals(templates)) {
for (String template : templates.split("\n")) {
if (isXPackTemplate(template)) continue;
if ("".equals(template)) {
throw new IllegalStateException("empty template in templates list:\n" + templates);
try {
Request getTemplatesRequest = new Request("GET", "_index_template");
Map<String, Object> composableIndexTemplates = XContentHelper.convertToMap(JsonXContent.jsonXContent,
EntityUtils.toString(adminClient().performRequest(getTemplatesRequest).getEntity()), false);
List<String> names = ((List<?>) composableIndexTemplates.get("index_templates")).stream()
.map(ct -> (String) ((Map<?, ?>) ct).get("name"))
.collect(Collectors.toList());
for (String name : names) {
if (isXPackTemplate(name)) {
continue;
}
logger.info("Clearing template [{}]", template);
try {
adminClient().performRequest(new Request("DELETE", "_template/" + template));
adminClient().performRequest(new Request("DELETE", "_index_template/" + name));
} catch (ResponseException e) {
// This is fine, it could be a V2 template
assertThat(e.getMessage(), containsString("index_template [" + template + "] missing"));
try {
adminClient().performRequest(new Request("DELETE", "_index_template/" + template));
} catch (ResponseException e2) {
// We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
}
logger.debug(new ParameterizedMessage("unable to remove index template {}", name), e);
}
}
} catch (Exception e) {
logger.info("ignoring exception removing all composable index templates", e);
// We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
}
try {
Request compReq = new Request("GET", "_component_template");
Expand All @@ -610,13 +607,26 @@ private void wipeCluster() throws Exception {
}
adminClient().performRequest(new Request("DELETE", "_component_template/" + componentTemplate));
} catch (ResponseException e) {
logger.debug(new ParameterizedMessage("unable to remove component template {}", componentTemplate), e);
logger.debug(new ParameterizedMessage("unable to remove component template {}", componentTemplate), e);
}
}
} catch (Exception e) {
logger.info("ignoring exception removing all component templates", e);
// We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
}
Request getLegacyTemplatesRequest = new Request("GET", "_template");
Map<String, Object> legacyTemplates = XContentHelper.convertToMap(JsonXContent.jsonXContent,
EntityUtils.toString(adminClient().performRequest(getLegacyTemplatesRequest).getEntity()), false);
for (String name : legacyTemplates.keySet()) {
if (isXPackTemplate(name)) {
continue;
}
try {
adminClient().performRequest(new Request("DELETE", "_template/" + name));
} catch (ResponseException e) {
logger.debug(new ParameterizedMessage("unable to remove index template {}", name), e);
}
}
} else {
logger.debug("Clearing all templates");
adminClient().performRequest(new Request("DELETE", "_template/*"));
Expand Down

0 comments on commit 172ca20

Please sign in to comment.