Skip to content

Commit 4df868f

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 f5a0c22 commit 4df868f

File tree

3 files changed

+151
-55
lines changed

3 files changed

+151
-55
lines changed

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

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

266-
// apply templates, merging the mappings into the request mapping if exists
267-
for (IndexTemplateMetaData template : templates) {
268-
templateNames.add(template.getName());
269-
for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
270-
String mappingString = cursor.value.string();
271-
if (mappings.containsKey(cursor.key)) {
272-
XContentHelper.mergeDefaults(mappings.get(cursor.key),
266+
final Index shrinkFromIndex = request.shrinkFrom();
267+
268+
if (shrinkFromIndex == null) {
269+
// apply templates, merging the mappings into the request mapping if exists
270+
for (IndexTemplateMetaData template : templates) {
271+
templateNames.add(template.getName());
272+
for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
273+
String mappingString = cursor.value.string();
274+
if (mappings.containsKey(cursor.key)) {
275+
XContentHelper.mergeDefaults(mappings.get(cursor.key),
273276
MapperService.parseMapping(xContentRegistry, mappingString));
274-
} else {
275-
mappings.put(cursor.key,
276-
MapperService.parseMapping(xContentRegistry, mappingString));
277-
}
278-
}
279-
// handle custom
280-
for (ObjectObjectCursor<String, Custom> cursor : template.customs()) {
281-
String type = cursor.key;
282-
IndexMetaData.Custom custom = cursor.value;
283-
IndexMetaData.Custom existing = customs.get(type);
284-
if (existing == null) {
285-
customs.put(type, custom);
286-
} else {
287-
IndexMetaData.Custom merged = existing.mergeWith(custom);
288-
customs.put(type, merged);
289-
}
290-
}
291-
//handle aliases
292-
for (ObjectObjectCursor<String, AliasMetaData> cursor : template.aliases()) {
293-
AliasMetaData aliasMetaData = cursor.value;
294-
//if an alias with same name came with the create index request itself,
295-
// ignore this one taken from the index template
296-
if (request.aliases().contains(new Alias(aliasMetaData.alias()))) {
297-
continue;
277+
} else {
278+
mappings.put(cursor.key,
279+
MapperService.parseMapping(xContentRegistry, mappingString));
280+
}
298281
}
299-
//if an alias with same name was already processed, ignore this one
300-
if (templatesAliases.containsKey(cursor.key)) {
301-
continue;
282+
// handle custom
283+
for (ObjectObjectCursor<String, Custom> cursor : template.customs()) {
284+
String type = cursor.key;
285+
IndexMetaData.Custom custom = cursor.value;
286+
IndexMetaData.Custom existing = customs.get(type);
287+
if (existing == null) {
288+
customs.put(type, custom);
289+
} else {
290+
IndexMetaData.Custom merged = existing.mergeWith(custom);
291+
customs.put(type, merged);
292+
}
302293
}
303-
304-
//Allow templatesAliases to be templated by replacing a token with the name of the index that we are applying it to
305-
if (aliasMetaData.alias().contains("{index}")) {
306-
String templatedAlias = aliasMetaData.alias().replace("{index}", request.index());
307-
aliasMetaData = AliasMetaData.newAliasMetaData(aliasMetaData, templatedAlias);
294+
//handle aliases
295+
for (ObjectObjectCursor<String, AliasMetaData> cursor : template.aliases()) {
296+
AliasMetaData aliasMetaData = cursor.value;
297+
//if an alias with same name came with the create index request itself,
298+
// ignore this one taken from the index template
299+
if (request.aliases().contains(new Alias(aliasMetaData.alias()))) {
300+
continue;
301+
}
302+
//if an alias with same name was already processed, ignore this one
303+
if (templatesAliases.containsKey(cursor.key)) {
304+
continue;
305+
}
306+
307+
//Allow templatesAliases to be templated by replacing a token with the name of the index that we are applying it to
308+
if (aliasMetaData.alias().contains("{index}")) {
309+
String templatedAlias = aliasMetaData.alias().replace("{index}", request.index());
310+
aliasMetaData = AliasMetaData.newAliasMetaData(aliasMetaData, templatedAlias);
311+
}
312+
313+
aliasValidator.validateAliasMetaData(aliasMetaData, request.index(), currentState.metaData());
314+
templatesAliases.put(aliasMetaData.alias(), aliasMetaData);
308315
}
309-
310-
aliasValidator.validateAliasMetaData(aliasMetaData, request.index(), currentState.metaData());
311-
templatesAliases.put(aliasMetaData.alias(), aliasMetaData);
312316
}
313317
}
314318
Settings.Builder indexSettingsBuilder = Settings.builder();
315-
// apply templates, here, in reverse order, since first ones are better matching
316-
for (int i = templates.size() - 1; i >= 0; i--) {
317-
indexSettingsBuilder.put(templates.get(i).settings());
319+
if (shrinkFromIndex == null) {
320+
// apply templates, here, in reverse order, since first ones are better matching
321+
for (int i = templates.size() - 1; i >= 0; i--) {
322+
indexSettingsBuilder.put(templates.get(i).settings());
323+
}
318324
}
319325
// now, put the request settings, so they override templates
320326
indexSettingsBuilder.put(request.settings());
@@ -339,20 +345,26 @@ public ClusterState execute(ClusterState currentState) throws Exception {
339345
}
340346
indexSettingsBuilder.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, request.getProvidedName());
341347
indexSettingsBuilder.put(SETTING_INDEX_UUID, UUIDs.randomBase64UUID());
342-
final Index shrinkFromIndex = request.shrinkFrom();
343-
int routingNumShards = IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.get(indexSettingsBuilder.build());;
344-
if (shrinkFromIndex != null) {
345-
prepareShrinkIndexSettings(currentState, mappings.keySet(), indexSettingsBuilder, shrinkFromIndex,
346-
request.index());
347-
IndexMetaData sourceMetaData = currentState.metaData().getIndexSafe(shrinkFromIndex);
348+
final IndexMetaData.Builder tmpImdBuilder = IndexMetaData.builder(request.index());
349+
350+
final int routingNumShards;
351+
if (shrinkFromIndex == null) {
352+
routingNumShards = IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.get(indexSettingsBuilder.build());
353+
} else {
354+
final IndexMetaData sourceMetaData = currentState.metaData().getIndexSafe(shrinkFromIndex);
348355
routingNumShards = sourceMetaData.getRoutingNumShards();
349356
}
357+
tmpImdBuilder.setRoutingNumShards(routingNumShards);
358+
359+
if (shrinkFromIndex != null) {
360+
prepareShrinkIndexSettings(
361+
currentState, mappings.keySet(), indexSettingsBuilder, shrinkFromIndex, request.index());
362+
}
363+
final Settings actualIndexSettings = indexSettingsBuilder.build();
364+
tmpImdBuilder.settings(actualIndexSettings);
350365

351-
Settings actualIndexSettings = indexSettingsBuilder.build();
352-
IndexMetaData.Builder tmpImdBuilder = IndexMetaData.builder(request.index())
353-
.setRoutingNumShards(routingNumShards);
354366
// Set up everything, now locally create the index to see that things are ok, and apply
355-
final IndexMetaData tmpImd = tmpImdBuilder.settings(actualIndexSettings).build();
367+
final IndexMetaData tmpImd = tmpImdBuilder.build();
356368
ActiveShardCount waitForActiveShards = request.waitForActiveShards();
357369
if (waitForActiveShards == ActiveShardCount.DEFAULT) {
358370
waitForActiveShards = tmpImd.getWaitForActiveShards();
@@ -399,6 +411,11 @@ public ClusterState execute(ClusterState currentState) throws Exception {
399411
final IndexMetaData.Builder indexMetaDataBuilder = IndexMetaData.builder(request.index())
400412
.settings(actualIndexSettings)
401413
.setRoutingNumShards(routingNumShards);
414+
415+
for (int shardId = 0; shardId < tmpImd.getNumberOfShards(); shardId++) {
416+
indexMetaDataBuilder.primaryTerm(shardId, tmpImd.primaryTerm(shardId));
417+
}
418+
402419
for (MappingMetaData mappingMd : mappingsMetaData.values()) {
403420
indexMetaDataBuilder.putMapping(mappingMd);
404421
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public void testShrinking() throws Exception {
6666
client().admin().indices().prepareCreate(index)
6767
.setSettings(Settings.builder()
6868
.put("index.number_of_shards", currentShards)
69+
.put("index.number_of_replicas", numberOfReplicas())
6970
.put("index.routing_partition_size", partitionSize))
7071
.addMapping("type", "{\"type\":{\"_routing\":{\"required\":true}}}")
7172
.execute().actionGet();
@@ -106,6 +107,7 @@ public void testShrinking() throws Exception {
106107
client().admin().indices().prepareShrinkIndex(previousIndex, index)
107108
.setSettings(Settings.builder()
108109
.put("index.number_of_shards", currentShards)
110+
.put("index.number_of_replicas", numberOfReplicas())
109111
.build()).get();
110112
ensureGreen();
111113
}
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.5.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)