From 1b80a582f2d21e0d666fb2a59faf010c50e93da0 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 8 Mar 2021 14:33:09 +0100 Subject: [PATCH] Improve template cleanup in ESRestTestCase (#70066) Backport #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 #69973 --- .../test/rest/ESRestTestCase.java | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java index 7df7777f6172a..eba68300730fb 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java @@ -100,7 +100,6 @@ import static org.elasticsearch.rest.action.admin.indices.RestCloseIndexAction.WAIT_FOR_ACTIVE_SHARDS_DEFAULT_DEPRECATION_MESSAGE; 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; @@ -603,31 +602,31 @@ 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"); + getTemplatesRequest.setOptions(allowTypesRemovalWarnings()); + Map composableIndexTemplates = XContentHelper.convertToMap(JsonXContent.jsonXContent, + EntityUtils.toString(adminClient().performRequest(getTemplatesRequest).getEntity()), false); + List 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"); + compReq.setOptions(allowTypesRemovalWarnings()); String componentTemplates = EntityUtils.toString(adminClient().performRequest(compReq).getEntity()); Map cTemplates = XContentHelper.convertToMap(JsonXContent.jsonXContent, componentTemplates, false); @SuppressWarnings("unchecked") @@ -641,13 +640,27 @@ 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"); + getLegacyTemplatesRequest.setOptions(allowTypesRemovalWarnings()); + Map 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/*"));