Skip to content

Commit e2bfb35

Browse files
fred84jasontedor
authored andcommitted
Shrunk indices should ignore templates
A shrunk index should ignore anything from templates and instead take its mappings, aliases, and settings from the original index, plus any new settings and aliases passed in with the shrink request. This commit causes this to be the case. Relates #25380
1 parent b7bc790 commit e2bfb35

File tree

3 files changed

+130
-46
lines changed

3 files changed

+130
-46
lines changed

core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -264,58 +264,64 @@ public ClusterState execute(ClusterState currentState) throws Exception {
264264
customs.put(entry.getKey(), entry.getValue());
265265
}
266266

267-
// apply templates, merging the mappings into the request mapping if exists
268-
for (IndexTemplateMetaData template : templates) {
269-
templateNames.add(template.getName());
270-
for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
271-
String mappingString = cursor.value.string();
272-
if (mappings.containsKey(cursor.key)) {
273-
XContentHelper.mergeDefaults(mappings.get(cursor.key),
267+
final Index shrinkFromIndex = request.shrinkFrom();
268+
269+
if (shrinkFromIndex == null) {
270+
// apply templates, merging the mappings into the request mapping if exists
271+
for (IndexTemplateMetaData template : templates) {
272+
templateNames.add(template.getName());
273+
for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
274+
String mappingString = cursor.value.string();
275+
if (mappings.containsKey(cursor.key)) {
276+
XContentHelper.mergeDefaults(mappings.get(cursor.key),
274277
MapperService.parseMapping(xContentRegistry, mappingString));
275-
} else {
276-
mappings.put(cursor.key,
277-
MapperService.parseMapping(xContentRegistry, mappingString));
278-
}
279-
}
280-
// handle custom
281-
for (ObjectObjectCursor<String, Custom> cursor : template.customs()) {
282-
String type = cursor.key;
283-
IndexMetaData.Custom custom = cursor.value;
284-
IndexMetaData.Custom existing = customs.get(type);
285-
if (existing == null) {
286-
customs.put(type, custom);
287-
} else {
288-
IndexMetaData.Custom merged = existing.mergeWith(custom);
289-
customs.put(type, merged);
290-
}
291-
}
292-
//handle aliases
293-
for (ObjectObjectCursor<String, AliasMetaData> cursor : template.aliases()) {
294-
AliasMetaData aliasMetaData = cursor.value;
295-
//if an alias with same name came with the create index request itself,
296-
// ignore this one taken from the index template
297-
if (request.aliases().contains(new Alias(aliasMetaData.alias()))) {
298-
continue;
278+
} else {
279+
mappings.put(cursor.key,
280+
MapperService.parseMapping(xContentRegistry, mappingString));
281+
}
299282
}
300-
//if an alias with same name was already processed, ignore this one
301-
if (templatesAliases.containsKey(cursor.key)) {
302-
continue;
283+
// handle custom
284+
for (ObjectObjectCursor<String, Custom> cursor : template.customs()) {
285+
String type = cursor.key;
286+
IndexMetaData.Custom custom = cursor.value;
287+
IndexMetaData.Custom existing = customs.get(type);
288+
if (existing == null) {
289+
customs.put(type, custom);
290+
} else {
291+
IndexMetaData.Custom merged = existing.mergeWith(custom);
292+
customs.put(type, merged);
293+
}
303294
}
304-
305-
//Allow templatesAliases to be templated by replacing a token with the name of the index that we are applying it to
306-
if (aliasMetaData.alias().contains("{index}")) {
307-
String templatedAlias = aliasMetaData.alias().replace("{index}", request.index());
308-
aliasMetaData = AliasMetaData.newAliasMetaData(aliasMetaData, templatedAlias);
295+
//handle aliases
296+
for (ObjectObjectCursor<String, AliasMetaData> cursor : template.aliases()) {
297+
AliasMetaData aliasMetaData = cursor.value;
298+
//if an alias with same name came with the create index request itself,
299+
// ignore this one taken from the index template
300+
if (request.aliases().contains(new Alias(aliasMetaData.alias()))) {
301+
continue;
302+
}
303+
//if an alias with same name was already processed, ignore this one
304+
if (templatesAliases.containsKey(cursor.key)) {
305+
continue;
306+
}
307+
308+
//Allow templatesAliases to be templated by replacing a token with the name of the index that we are applying it to
309+
if (aliasMetaData.alias().contains("{index}")) {
310+
String templatedAlias = aliasMetaData.alias().replace("{index}", request.index());
311+
aliasMetaData = AliasMetaData.newAliasMetaData(aliasMetaData, templatedAlias);
312+
}
313+
314+
aliasValidator.validateAliasMetaData(aliasMetaData, request.index(), currentState.metaData());
315+
templatesAliases.put(aliasMetaData.alias(), aliasMetaData);
309316
}
310-
311-
aliasValidator.validateAliasMetaData(aliasMetaData, request.index(), currentState.metaData());
312-
templatesAliases.put(aliasMetaData.alias(), aliasMetaData);
313317
}
314318
}
315319
Settings.Builder indexSettingsBuilder = Settings.builder();
316-
// apply templates, here, in reverse order, since first ones are better matching
317-
for (int i = templates.size() - 1; i >= 0; i--) {
318-
indexSettingsBuilder.put(templates.get(i).settings());
320+
if (shrinkFromIndex == null) {
321+
// apply templates, here, in reverse order, since first ones are better matching
322+
for (int i = templates.size() - 1; i >= 0; i--) {
323+
indexSettingsBuilder.put(templates.get(i).settings());
324+
}
319325
}
320326
// now, put the request settings, so they override templates
321327
indexSettingsBuilder.put(request.settings());
@@ -340,7 +346,6 @@ public ClusterState execute(ClusterState currentState) throws Exception {
340346
}
341347
indexSettingsBuilder.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, request.getProvidedName());
342348
indexSettingsBuilder.put(SETTING_INDEX_UUID, UUIDs.randomBase64UUID());
343-
final Index shrinkFromIndex = request.shrinkFrom();
344349
final IndexMetaData.Builder tmpImdBuilder = IndexMetaData.builder(request.index());
345350

346351
final int routingNumShards;

core/src/test/java/org/elasticsearch/routing/PartitionedRoutingIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void testShrinking() throws Exception {
6767
client().admin().indices().prepareCreate(index)
6868
.setSettings(Settings.builder()
6969
.put("index.number_of_shards", currentShards)
70+
.put("index.number_of_replicas", numberOfReplicas())
7071
.put("index.routing_partition_size", partitionSize))
7172
.addMapping("type", "{\"type\":{\"_routing\":{\"required\":true}}}", XContentType.JSON)
7273
.execute().actionGet();
@@ -107,6 +108,7 @@ public void testShrinking() throws Exception {
107108
client().admin().indices().prepareShrinkIndex(previousIndex, index)
108109
.setSettings(Settings.builder()
109110
.put("index.number_of_shards", currentShards)
111+
.put("index.number_of_replicas", numberOfReplicas())
110112
.build()).get();
111113
ensureGreen();
112114
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
"Shrink index ignores target template mapping":
3+
- skip:
4+
version: " - 5.99.99"
5+
reason: bug fixed in 6.0
6+
7+
- do:
8+
cluster.state: {}
9+
# Get master node id
10+
11+
- set: { master_node: master }
12+
13+
# create index
14+
- do:
15+
indices.create:
16+
index: source
17+
wait_for_active_shards: 1
18+
body:
19+
settings:
20+
# ensure everything is allocated on a single node
21+
index.routing.allocation.include._id: $master
22+
number_of_replicas: 0
23+
mappings:
24+
test:
25+
properties:
26+
count:
27+
type: text
28+
29+
# index document
30+
- do:
31+
index:
32+
index: source
33+
type: test
34+
id: "1"
35+
body: { "count": "1" }
36+
37+
# create template matching shrink tagret
38+
- do:
39+
indices.put_template:
40+
name: tpl1
41+
body:
42+
index_patterns: targ*
43+
mappings:
44+
test:
45+
properties:
46+
count:
47+
type: integer
48+
49+
# make it read-only
50+
- do:
51+
indices.put_settings:
52+
index: source
53+
body:
54+
index.blocks.write: true
55+
index.number_of_replicas: 0
56+
57+
- do:
58+
cluster.health:
59+
wait_for_status: green
60+
index: source
61+
62+
# now we do the actual shrink
63+
- do:
64+
indices.shrink:
65+
index: "source"
66+
target: "target"
67+
wait_for_active_shards: 1
68+
master_timeout: 10s
69+
body:
70+
settings:
71+
index.number_of_replicas: 0
72+
73+
- do:
74+
cluster.health:
75+
wait_for_status: green
76+
77+

0 commit comments

Comments
 (0)