Skip to content

Commit

Permalink
Support specifying multiple templates names in delete component templ…
Browse files Browse the repository at this point in the history
…ate api (#70379)

Backporting #70314 to 7.x branch.

Add support to delete component templates api to specify multiple template
names separated by a comma.

Change the cleanup template logic for rest tests to remove all component templates via a single delete component template request. This to optimize the cleanup logic. After each rest test we delete all templates. So deleting templates this via a single api call (and thus single cluster state update) saves a lot of time considering the number of rest tests.

Older versions don't support component / composable index templates
and/or data streams. Yet the test base class tries to remove objects
after each test, which adds a significant number of lines to the
log files (which slows the tests down). The ESRestTestCase will
now check whether all nodes have a specific version and then decide
whether data streams and component / composable index templates will
be deleted.

Also ensured that the logstash-index-template and security-index-template
aren't deleted between tests, these templates are builtin templates that
ES will install if missing. So if tests remove these templates between tests
then ES will add these template back almost immediately. These causes
many log lines and a lot of cluster state updates, which slow tests down.

Relates to #69973

Co-authored-by: Lee Hinman <[email protected]>

Co-authored-by: Lee Hinman <[email protected]>
  • Loading branch information
martijnvg and dakrone authored Mar 16, 2021
1 parent 43c437c commit 62f9b3e
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 87 deletions.
7 changes: 5 additions & 2 deletions docs/reference/indices/delete-component-template.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ PUT _component_template/template_1
DELETE _component_template/template_1
--------------------------------------------------

The provided <component-template> may contain multiple template names separated by a comma.
If multiple template names are specified then there is no wildcard support and the
provided names should match completely with existing component templates.

[[delete-component-template-api-request]]
==== {api-request-title}
Expand All @@ -43,8 +46,8 @@ privilege>> to use this API.
==== {api-description-title}

Use the delete component template API to delete one or more component templates
Component templates are building blocks for constructing <<index-templates,index templates>>
that specify index mappings, settings, and aliases.
Component templates are building blocks for constructing <<index-templates,index templates>>
that specify index mappings, settings, and aliases.

[[delete-component-template-api-path-params]]
==== {api-path-parms-title}
Expand Down
1 change: 0 additions & 1 deletion docs/reference/indices/simulate-template.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ PUT _index_template/template_1
[source,console]
--------------------------------------------------
DELETE _index_template/*
DELETE _component_template/*
--------------------------------------------------
// TEARDOWN
////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,71 @@
name: test

- is_false: test

---
"Delete multiple templates":
- skip:
version: " - 7.99.99"
reason: "not yet backported"

- do:
cluster.put_component_template:
name: foo
body:
template:
settings:
number_of_shards: 1
number_of_replicas: 0

- do:
cluster.put_component_template:
name: bar
body:
template:
settings:
number_of_shards: 1
number_of_replicas: 0

- do:
cluster.put_component_template:
name: baz
body:
template:
settings:
number_of_shards: 1
number_of_replicas: 0

- do:
cluster.get_component_template:
name: 'bar'
- match: {component_templates.0.name: bar}

- do:
cluster.get_component_template:
name: 'baz'
- match: {component_templates.0.name: baz}

- do:
cluster.get_component_template:
name: 'foo'
- match: {component_templates.0.name: foo}

- do:
cluster.delete_component_template:
name: foo,bar

- do:
catch: missing
cluster.get_component_template:
name: foo

- do:
catch: missing
cluster.get_component_template:
name: bar

- do:
cluster.get_component_template:
name: baz

- match: {component_templates.0.name: baz}
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,81 @@
indices.put_index_template:
name: test
body: {}

---
"Delete multiple templates":
- skip:
version: " - 7.99.99"
reason: "not yet backported"
features: allowed_warnings

- do:
allowed_warnings:
- "index template [foo] has index patterns [foo-*] matching patterns from existing older templates [global] with patterns (global => [*]); this template [foo] will take precedence during new index creation"
indices.put_index_template:
name: foo
body:
index_patterns: foo-*
template:
settings:
number_of_shards: 1
number_of_replicas: 0

- do:
allowed_warnings:
- "index template [bar] has index patterns [bar-*] matching patterns from existing older templates [global] with patterns (global => [*]); this template [bar] will take precedence during new index creation"
indices.put_index_template:
name: bar
body:
index_patterns: bar-*
template:
settings:
number_of_shards: 1
number_of_replicas: 0

- do:
allowed_warnings:
- "index template [baz] has index patterns [baz-*] matching patterns from existing older templates [global] with patterns (global => [*]); this template [baz] will take precedence during new index creation"
indices.put_index_template:
name: baz
body:
index_patterns: baz-*
template:
settings:
number_of_shards: 1
number_of_replicas: 0

- do:
indices.get_index_template:
name: 'bar'
- match: {index_templates.0.name: "bar"}

- do:
indices.get_index_template:
name: 'baz'
- match: {index_templates.0.name: "baz"}

- do:
indices.get_index_template:
name: 'foo'
- match: {index_templates.0.name: "foo"}

- do:
indices.delete_index_template:
name: foo,bar

- do:
catch: missing
indices.get_index_template:
name: foo

- do:
catch: missing
indices.get_index_template:
name: bar

- do:
indices.get_index_template:
name: baz

- match: {index_templates.0.name: baz}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@

package org.elasticsearch.action.admin.indices.template.delete;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.support.master.MasterNodeRequest;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;

import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;

import static org.elasticsearch.action.ValidateActions.addValidationError;

Expand All @@ -30,50 +34,48 @@ private DeleteComponentTemplateAction() {

public static class Request extends MasterNodeRequest<Request> {

private String name;
private final String[] names;

public Request(StreamInput in) throws IOException {
super(in);
name = in.readString();
if (in.getVersion().onOrAfter(Version.V_7_13_0)) {
names = in.readStringArray();
} else {
names = new String[] {in.readString()};
}
}

public Request() { }

/**
* Constructs a new delete index request for the specified name.
*/
public Request(String name) {
this.name = name;
}

/**
* Set the index template name to delete.
*/
public Request name(String name) {
this.name = name;
return this;
public Request(String... names) {
this.names = Objects.requireNonNull(names, "component templates to delete must not be null");
}

@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
if (name == null) {
validationException = addValidationError("name is missing", validationException);
if (Arrays.stream(names).anyMatch(Strings::hasLength) == false) {
validationException = addValidationError("no component template names specified", validationException);
}
return validationException;
}

/**
* The index template name to delete.
* The index template names to delete.
*/
public String name() {
return name;
public String[] names() {
return names;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeString(name);
if (out.getVersion().onOrAfter(Version.V_7_13_0)) {
out.writeStringArray(names);
} else {
out.writeString(names[0]);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public Request(String... names) {
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
if (Arrays.stream(names).anyMatch(Strings::hasLength) == false) {
validationException = addValidationError("name is missing", validationException);
validationException = addValidationError("no template names specified", validationException);
}
return validationException;
}

/**
* The index template name to delete.
* The index template names to delete.
*/
public String[] names() {
return names;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ protected ClusterBlockException checkBlock(DeleteComponentTemplateAction.Request
@Override
protected void masterOperation(final DeleteComponentTemplateAction.Request request, final ClusterState state,
final ActionListener<AcknowledgedResponse> listener) {
indexTemplateService.removeComponentTemplate(request.name(), request.masterNodeTimeout(), listener);
indexTemplateService.removeComponentTemplate(request.names(), request.masterNodeTimeout(), state, listener);
}
}
Loading

0 comments on commit 62f9b3e

Please sign in to comment.