Skip to content

Commit a4a8f08

Browse files
committed
Add rolling upgrade tests for component and composable templates (elastic#58867)
This adds rolling upgrade tests that component and composable templates can be read from older versions of the cluster. Relates to elastic#58643
1 parent bd9b3b6 commit a4a8f08

File tree

4 files changed

+155
-23
lines changed

4 files changed

+155
-23
lines changed

qa/rolling-upgrade/src/test/resources/rest-api-spec/test/mixed_cluster/10_basic.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,29 @@
8080
index: queries
8181
include_type_name: false
8282
- match: { queries.mappings.properties.id.type: "keyword" }
83+
84+
---
85+
"Component and composable templates can be retrieved":
86+
- do:
87+
cluster.get_component_template:
88+
name: my-ct
89+
90+
- match: {component_templates.0.name: my-ct}
91+
- match: {component_templates.0.component_template.version: 2}
92+
- match: {component_templates.0.component_template._meta: {foo: bar, baz: {eggplant: true}}}
93+
- match: {component_templates.0.component_template.template.settings: {index: {number_of_shards: '1', number_of_replicas: '0'}}}
94+
- is_true: component_templates.0.component_template.template.mappings
95+
- match: {component_templates.0.component_template.template.aliases: {aliasname: {}}}
96+
97+
- do:
98+
indices.get_index_template:
99+
name: my-it
100+
101+
- match: {index_templates.0.index_template.index_patterns: ["test-*"]}
102+
- match: {index_templates.0.index_template.template.settings.index: {number_of_shards: '1', number_of_replicas: '0'}}
103+
- is_true: index_templates.0.index_template.template.mappings
104+
- length: {index_templates.0.index_template.template.aliases: 3}
105+
- is_true: index_templates.0.index_template.template.aliases.test_alias
106+
- match: {index_templates.0.index_template.template.aliases.test_blias.index_routing: "b" }
107+
- match: {index_templates.0.index_template.template.aliases.test_blias.search_routing: "b" }
108+
- match: {index_templates.0.index_template.template.aliases.test_clias.filter.term.user: "kimchy" }

qa/rolling-upgrade/src/test/resources/rest-api-spec/test/old_cluster/10_basic.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,49 @@
205205
wait_for_completion: true
206206
task_id: $task
207207

208+
---
209+
"Component and composable template validation":
210+
- do:
211+
cluster.put_component_template:
212+
name: my-ct
213+
body:
214+
template:
215+
settings:
216+
number_of_shards: 1
217+
number_of_replicas: 0
218+
mappings:
219+
dynamic: false
220+
aliases:
221+
aliasname: {}
222+
version: 2
223+
_meta:
224+
foo: bar
225+
baz:
226+
eggplant: true
227+
228+
- do:
229+
indices.put_index_template:
230+
name: my-it
231+
body:
232+
index_patterns: [test-*]
233+
composed_of: ["my-ct"]
234+
template:
235+
settings:
236+
number_of_shards: 1
237+
number_of_replicas: 0
238+
mappings:
239+
properties:
240+
field:
241+
type: text
242+
aliases:
243+
test_alias: {}
244+
test_blias: { routing: b }
245+
test_clias: { filter: { term: { user: kimchy }}}
246+
247+
- do:
248+
cluster.get_component_template:
249+
name: my-ct
250+
251+
- do:
252+
indices.get_index_template:
253+
name: my-it

qa/rolling-upgrade/src/test/resources/rest-api-spec/test/upgraded_cluster/10_basic.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,56 @@
126126
task_id: $task_id
127127
- match: { task.headers.X-Opaque-Id: "Reindexing Again" }
128128

129+
---
130+
"Component and composable templates can be retrieved and updated":
131+
- do:
132+
cluster.get_component_template:
133+
name: my-ct
134+
135+
- match: {component_templates.0.name: my-ct}
136+
- match: {component_templates.0.component_template.version: 2}
137+
- match: {component_templates.0.component_template._meta: {foo: bar, baz: {eggplant: true}}}
138+
- match: {component_templates.0.component_template.template.settings: {index: {number_of_shards: '1', number_of_replicas: '0'}}}
139+
- match: {component_templates.0.component_template.template.mappings: {dynamic: false}}
140+
- match: {component_templates.0.component_template.template.aliases: {aliasname: {}}}
141+
142+
- do:
143+
indices.get_index_template:
144+
name: my-it
145+
146+
- match: {index_templates.0.index_template.index_patterns: ["test-*"]}
147+
- match: {index_templates.0.index_template.template.settings.index: {number_of_shards: '1', number_of_replicas: '0'}}
148+
- is_true: index_templates.0.index_template.template.mappings
149+
- length: {index_templates.0.index_template.template.aliases: 3}
150+
- is_true: index_templates.0.index_template.template.aliases.test_alias
151+
- match: {index_templates.0.index_template.template.aliases.test_blias.index_routing: "b" }
152+
- match: {index_templates.0.index_template.template.aliases.test_blias.search_routing: "b" }
153+
- match: {index_templates.0.index_template.template.aliases.test_clias.filter.term.user: "kimchy" }
154+
- do:
155+
cluster.put_component_template:
156+
name: my-ct
157+
body:
158+
template:
159+
settings:
160+
number_of_shards: 1
161+
number_of_replicas: 0
162+
mappings:
163+
dynamic: true
164+
aliases:
165+
aliasname: {}
166+
version: 2
167+
_meta:
168+
foo: bar
169+
baz:
170+
eggplant: true
171+
172+
- do:
173+
cluster.get_component_template:
174+
name: my-ct
129175

176+
- match: {component_templates.0.name: my-ct}
177+
- match: {component_templates.0.component_template.version: 2}
178+
- match: {component_templates.0.component_template._meta: {foo: bar, baz: {eggplant: true}}}
179+
- match: {component_templates.0.component_template.template.settings: {index: {number_of_shards: '1', number_of_replicas: '0'}}}
180+
- is_true: component_templates.0.component_template.template.mappings
181+
- match: {component_templates.0.component_template.template.aliases: {aliasname: {}}}

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ ClusterState addComponentTemplate(final ClusterState currentState, final boolean
188188
}
189189

190190
CompressedXContent mappings = template.template().mappings();
191-
String stringMappings = mappings == null ? null : mappings.string();
191+
String stringMappings = wrapMappingsIfNecessary(mappings == null ? null : mappings.string(), xContentRegistry);
192192

193193
// We may need to normalize index settings, so do that also
194194
Settings finalSettings = template.template().settings();
@@ -227,17 +227,6 @@ ClusterState addComponentTemplate(final ClusterState currentState, final boolean
227227
}
228228
}
229229

230-
// Mappings in component templates don't include _doc, so update the mappings to include this single type
231-
if (stringMappings != null) {
232-
Map<String, Object> parsedMappings = MapperService.parseMapping(xContentRegistry, stringMappings);
233-
if (parsedMappings.size() > 0) {
234-
stringMappings = Strings.toString(XContentFactory.jsonBuilder()
235-
.startObject()
236-
.field(MapperService.SINGLE_MAPPING_NAME, parsedMappings)
237-
.endObject());
238-
}
239-
}
240-
241230
final Template finalTemplate = new Template(finalSettings,
242231
stringMappings == null ? null : new CompressedXContent(stringMappings), template.template().aliases());
243232
final ComponentTemplate finalComponentTemplate = new ComponentTemplate(finalTemplate, template.version(), template.metadata());
@@ -281,6 +270,35 @@ ClusterState addComponentTemplate(final ClusterState currentState, final boolean
281270
.build();
282271
}
283272

273+
@Nullable
274+
private static String wrapMappingsIfNecessary(@Nullable String mappings, NamedXContentRegistry xContentRegistry) throws Exception {
275+
// Mappings in templates don't have to include _doc, so update
276+
// the mappings to include this single type if necessary
277+
278+
String stringMappings = mappings;
279+
if (stringMappings != null) {
280+
Map<String, Object> parsedMappings = MapperService.parseMapping(xContentRegistry, stringMappings);
281+
if (parsedMappings.size() > 0) {
282+
if (parsedMappings.size() == 1) {
283+
final String keyName = parsedMappings.keySet().iterator().next();
284+
// Check if it's already wrapped in `_doc`, only rewrap if needed
285+
if (MapperService.SINGLE_MAPPING_NAME.equals(keyName) == false) {
286+
stringMappings = Strings.toString(XContentFactory.jsonBuilder()
287+
.startObject()
288+
.field(MapperService.SINGLE_MAPPING_NAME, parsedMappings)
289+
.endObject());
290+
}
291+
} else {
292+
stringMappings = Strings.toString(XContentFactory.jsonBuilder()
293+
.startObject()
294+
.field(MapperService.SINGLE_MAPPING_NAME, parsedMappings)
295+
.endObject());
296+
}
297+
}
298+
}
299+
return stringMappings;
300+
}
301+
284302
/**
285303
* Remove the given component template from the cluster state. The component template name
286304
* supports simple regex wildcards for removing multiple component templates at a time.
@@ -464,18 +482,8 @@ public ClusterState addIndexTemplateV2(final ClusterState currentState, final bo
464482
// If an inner template was specified, its mappings may need to be
465483
// adjusted (to add _doc) and it should be validated
466484
CompressedXContent mappings = innerTemplate.mappings();
467-
String stringMappings = mappings == null ? null : mappings.string();
485+
String stringMappings = wrapMappingsIfNecessary(mappings == null ? null : mappings.string(), xContentRegistry);
468486

469-
// Mappings in index templates don't include _doc, so update the mappings to include this single type
470-
if (stringMappings != null) {
471-
Map<String, Object> parsedMappings = MapperService.parseMapping(xContentRegistry, stringMappings);
472-
if (parsedMappings.size() > 0) {
473-
stringMappings = Strings.toString(XContentFactory.jsonBuilder()
474-
.startObject()
475-
.field(MapperService.SINGLE_MAPPING_NAME, parsedMappings)
476-
.endObject());
477-
}
478-
}
479487
final Template finalTemplate = new Template(finalSettings,
480488
stringMappings == null ? null : new CompressedXContent(stringMappings), innerTemplate.aliases());
481489
finalIndexTemplate = new ComposableIndexTemplate(template.indexPatterns(), finalTemplate, template.composedOf(),

0 commit comments

Comments
 (0)