Skip to content

Commit 62f9b3e

Browse files
martijnvgdakrone
andauthored
Support specifying multiple templates names in delete component template 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]>
1 parent 43c437c commit 62f9b3e

File tree

11 files changed

+296
-87
lines changed

11 files changed

+296
-87
lines changed

docs/reference/indices/delete-component-template.asciidoc

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ PUT _component_template/template_1
2626
DELETE _component_template/template_1
2727
--------------------------------------------------
2828

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

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

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

4952
[[delete-component-template-api-path-params]]
5053
==== {api-path-parms-title}

docs/reference/indices/simulate-template.asciidoc

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ PUT _index_template/template_1
4747
[source,console]
4848
--------------------------------------------------
4949
DELETE _index_template/*
50-
DELETE _component_template/*
5150
--------------------------------------------------
5251
// TEARDOWN
5352
////

rest-api-spec/src/main/resources/rest-api-spec/test/cluster.component_template/10_basic.yml

+68
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,71 @@
4545
name: test
4646

4747
- is_false: test
48+
49+
---
50+
"Delete multiple templates":
51+
- skip:
52+
version: " - 7.99.99"
53+
reason: "not yet backported"
54+
55+
- do:
56+
cluster.put_component_template:
57+
name: foo
58+
body:
59+
template:
60+
settings:
61+
number_of_shards: 1
62+
number_of_replicas: 0
63+
64+
- do:
65+
cluster.put_component_template:
66+
name: bar
67+
body:
68+
template:
69+
settings:
70+
number_of_shards: 1
71+
number_of_replicas: 0
72+
73+
- do:
74+
cluster.put_component_template:
75+
name: baz
76+
body:
77+
template:
78+
settings:
79+
number_of_shards: 1
80+
number_of_replicas: 0
81+
82+
- do:
83+
cluster.get_component_template:
84+
name: 'bar'
85+
- match: {component_templates.0.name: bar}
86+
87+
- do:
88+
cluster.get_component_template:
89+
name: 'baz'
90+
- match: {component_templates.0.name: baz}
91+
92+
- do:
93+
cluster.get_component_template:
94+
name: 'foo'
95+
- match: {component_templates.0.name: foo}
96+
97+
- do:
98+
cluster.delete_component_template:
99+
name: foo,bar
100+
101+
- do:
102+
catch: missing
103+
cluster.get_component_template:
104+
name: foo
105+
106+
- do:
107+
catch: missing
108+
cluster.get_component_template:
109+
name: bar
110+
111+
- do:
112+
cluster.get_component_template:
113+
name: baz
114+
115+
- match: {component_templates.0.name: baz}

rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_index_template/10_basic.yml

+78
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,81 @@
119119
indices.put_index_template:
120120
name: test
121121
body: {}
122+
123+
---
124+
"Delete multiple templates":
125+
- skip:
126+
version: " - 7.99.99"
127+
reason: "not yet backported"
128+
features: allowed_warnings
129+
130+
- do:
131+
allowed_warnings:
132+
- "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"
133+
indices.put_index_template:
134+
name: foo
135+
body:
136+
index_patterns: foo-*
137+
template:
138+
settings:
139+
number_of_shards: 1
140+
number_of_replicas: 0
141+
142+
- do:
143+
allowed_warnings:
144+
- "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"
145+
indices.put_index_template:
146+
name: bar
147+
body:
148+
index_patterns: bar-*
149+
template:
150+
settings:
151+
number_of_shards: 1
152+
number_of_replicas: 0
153+
154+
- do:
155+
allowed_warnings:
156+
- "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"
157+
indices.put_index_template:
158+
name: baz
159+
body:
160+
index_patterns: baz-*
161+
template:
162+
settings:
163+
number_of_shards: 1
164+
number_of_replicas: 0
165+
166+
- do:
167+
indices.get_index_template:
168+
name: 'bar'
169+
- match: {index_templates.0.name: "bar"}
170+
171+
- do:
172+
indices.get_index_template:
173+
name: 'baz'
174+
- match: {index_templates.0.name: "baz"}
175+
176+
- do:
177+
indices.get_index_template:
178+
name: 'foo'
179+
- match: {index_templates.0.name: "foo"}
180+
181+
- do:
182+
indices.delete_index_template:
183+
name: foo,bar
184+
185+
- do:
186+
catch: missing
187+
indices.get_index_template:
188+
name: foo
189+
190+
- do:
191+
catch: missing
192+
indices.get_index_template:
193+
name: bar
194+
195+
- do:
196+
indices.get_index_template:
197+
name: baz
198+
199+
- match: {index_templates.0.name: baz}

server/src/main/java/org/elasticsearch/action/admin/indices/template/delete/DeleteComponentTemplateAction.java

+22-20
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88

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

11+
import org.elasticsearch.Version;
1112
import org.elasticsearch.action.ActionRequestValidationException;
1213
import org.elasticsearch.action.ActionType;
1314
import org.elasticsearch.action.support.master.AcknowledgedResponse;
1415
import org.elasticsearch.action.support.master.MasterNodeRequest;
16+
import org.elasticsearch.common.Strings;
1517
import org.elasticsearch.common.io.stream.StreamInput;
1618
import org.elasticsearch.common.io.stream.StreamOutput;
1719

1820
import java.io.IOException;
21+
import java.util.Arrays;
22+
import java.util.Objects;
1923

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

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

3135
public static class Request extends MasterNodeRequest<Request> {
3236

33-
private String name;
37+
private final String[] names;
3438

3539
public Request(StreamInput in) throws IOException {
3640
super(in);
37-
name = in.readString();
41+
if (in.getVersion().onOrAfter(Version.V_7_13_0)) {
42+
names = in.readStringArray();
43+
} else {
44+
names = new String[] {in.readString()};
45+
}
3846
}
3947

40-
public Request() { }
41-
4248
/**
4349
* Constructs a new delete index request for the specified name.
4450
*/
45-
public Request(String name) {
46-
this.name = name;
47-
}
48-
49-
/**
50-
* Set the index template name to delete.
51-
*/
52-
public Request name(String name) {
53-
this.name = name;
54-
return this;
51+
public Request(String... names) {
52+
this.names = Objects.requireNonNull(names, "component templates to delete must not be null");
5553
}
5654

5755
@Override
5856
public ActionRequestValidationException validate() {
5957
ActionRequestValidationException validationException = null;
60-
if (name == null) {
61-
validationException = addValidationError("name is missing", validationException);
58+
if (Arrays.stream(names).anyMatch(Strings::hasLength) == false) {
59+
validationException = addValidationError("no component template names specified", validationException);
6260
}
6361
return validationException;
6462
}
6563

6664
/**
67-
* The index template name to delete.
65+
* The index template names to delete.
6866
*/
69-
public String name() {
70-
return name;
67+
public String[] names() {
68+
return names;
7169
}
7270

7371
@Override
7472
public void writeTo(StreamOutput out) throws IOException {
7573
super.writeTo(out);
76-
out.writeString(name);
74+
if (out.getVersion().onOrAfter(Version.V_7_13_0)) {
75+
out.writeStringArray(names);
76+
} else {
77+
out.writeString(names[0]);
78+
}
7779
}
7880
}
7981
}

server/src/main/java/org/elasticsearch/action/admin/indices/template/delete/DeleteComposableIndexTemplateAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ public Request(String... names) {
5656
public ActionRequestValidationException validate() {
5757
ActionRequestValidationException validationException = null;
5858
if (Arrays.stream(names).anyMatch(Strings::hasLength) == false) {
59-
validationException = addValidationError("name is missing", validationException);
59+
validationException = addValidationError("no template names specified", validationException);
6060
}
6161
return validationException;
6262
}
6363

6464
/**
65-
* The index template name to delete.
65+
* The index template names to delete.
6666
*/
6767
public String[] names() {
6868
return names;

server/src/main/java/org/elasticsearch/action/admin/indices/template/delete/TransportDeleteComponentTemplateAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ protected ClusterBlockException checkBlock(DeleteComponentTemplateAction.Request
4343
@Override
4444
protected void masterOperation(final DeleteComponentTemplateAction.Request request, final ClusterState state,
4545
final ActionListener<AcknowledgedResponse> listener) {
46-
indexTemplateService.removeComponentTemplate(request.name(), request.masterNodeTimeout(), listener);
46+
indexTemplateService.removeComponentTemplate(request.names(), request.masterNodeTimeout(), state, listener);
4747
}
4848
}

0 commit comments

Comments
 (0)