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
This commit is contained in:
Sergey Galkin 2017-07-13 01:27:38 +03:00 committed by Jason Tedor
parent b7bc790428
commit e2bfb35f4a
3 changed files with 129 additions and 45 deletions

View File

@ -264,58 +264,64 @@ public class MetaDataCreateIndexService extends AbstractComponent {
customs.put(entry.getKey(), entry.getValue()); customs.put(entry.getKey(), entry.getValue());
} }
// apply templates, merging the mappings into the request mapping if exists final Index shrinkFromIndex = request.shrinkFrom();
for (IndexTemplateMetaData template : templates) {
templateNames.add(template.getName()); if (shrinkFromIndex == null) {
for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) { // apply templates, merging the mappings into the request mapping if exists
String mappingString = cursor.value.string(); for (IndexTemplateMetaData template : templates) {
if (mappings.containsKey(cursor.key)) { templateNames.add(template.getName());
XContentHelper.mergeDefaults(mappings.get(cursor.key), for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
String mappingString = cursor.value.string();
if (mappings.containsKey(cursor.key)) {
XContentHelper.mergeDefaults(mappings.get(cursor.key),
MapperService.parseMapping(xContentRegistry, mappingString)); MapperService.parseMapping(xContentRegistry, mappingString));
} else { } else {
mappings.put(cursor.key, mappings.put(cursor.key,
MapperService.parseMapping(xContentRegistry, mappingString)); MapperService.parseMapping(xContentRegistry, mappingString));
}
} }
} // handle custom
// handle custom for (ObjectObjectCursor<String, Custom> cursor : template.customs()) {
for (ObjectObjectCursor<String, Custom> cursor : template.customs()) { String type = cursor.key;
String type = cursor.key; IndexMetaData.Custom custom = cursor.value;
IndexMetaData.Custom custom = cursor.value; IndexMetaData.Custom existing = customs.get(type);
IndexMetaData.Custom existing = customs.get(type); if (existing == null) {
if (existing == null) { customs.put(type, custom);
customs.put(type, custom); } else {
} else { IndexMetaData.Custom merged = existing.mergeWith(custom);
IndexMetaData.Custom merged = existing.mergeWith(custom); customs.put(type, merged);
customs.put(type, merged); }
}
}
//handle aliases
for (ObjectObjectCursor<String, AliasMetaData> cursor : template.aliases()) {
AliasMetaData aliasMetaData = cursor.value;
//if an alias with same name came with the create index request itself,
// ignore this one taken from the index template
if (request.aliases().contains(new Alias(aliasMetaData.alias()))) {
continue;
}
//if an alias with same name was already processed, ignore this one
if (templatesAliases.containsKey(cursor.key)) {
continue;
} }
//handle aliases
for (ObjectObjectCursor<String, AliasMetaData> cursor : template.aliases()) {
AliasMetaData aliasMetaData = cursor.value;
//if an alias with same name came with the create index request itself,
// ignore this one taken from the index template
if (request.aliases().contains(new Alias(aliasMetaData.alias()))) {
continue;
}
//if an alias with same name was already processed, ignore this one
if (templatesAliases.containsKey(cursor.key)) {
continue;
}
//Allow templatesAliases to be templated by replacing a token with the name of the index that we are applying it to //Allow templatesAliases to be templated by replacing a token with the name of the index that we are applying it to
if (aliasMetaData.alias().contains("{index}")) { if (aliasMetaData.alias().contains("{index}")) {
String templatedAlias = aliasMetaData.alias().replace("{index}", request.index()); String templatedAlias = aliasMetaData.alias().replace("{index}", request.index());
aliasMetaData = AliasMetaData.newAliasMetaData(aliasMetaData, templatedAlias); aliasMetaData = AliasMetaData.newAliasMetaData(aliasMetaData, templatedAlias);
} }
aliasValidator.validateAliasMetaData(aliasMetaData, request.index(), currentState.metaData()); aliasValidator.validateAliasMetaData(aliasMetaData, request.index(), currentState.metaData());
templatesAliases.put(aliasMetaData.alias(), aliasMetaData); templatesAliases.put(aliasMetaData.alias(), aliasMetaData);
}
} }
} }
Settings.Builder indexSettingsBuilder = Settings.builder(); Settings.Builder indexSettingsBuilder = Settings.builder();
// apply templates, here, in reverse order, since first ones are better matching if (shrinkFromIndex == null) {
for (int i = templates.size() - 1; i >= 0; i--) { // apply templates, here, in reverse order, since first ones are better matching
indexSettingsBuilder.put(templates.get(i).settings()); for (int i = templates.size() - 1; i >= 0; i--) {
indexSettingsBuilder.put(templates.get(i).settings());
}
} }
// now, put the request settings, so they override templates // now, put the request settings, so they override templates
indexSettingsBuilder.put(request.settings()); indexSettingsBuilder.put(request.settings());
@ -340,7 +346,6 @@ public class MetaDataCreateIndexService extends AbstractComponent {
} }
indexSettingsBuilder.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, request.getProvidedName()); indexSettingsBuilder.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, request.getProvidedName());
indexSettingsBuilder.put(SETTING_INDEX_UUID, UUIDs.randomBase64UUID()); indexSettingsBuilder.put(SETTING_INDEX_UUID, UUIDs.randomBase64UUID());
final Index shrinkFromIndex = request.shrinkFrom();
final IndexMetaData.Builder tmpImdBuilder = IndexMetaData.builder(request.index()); final IndexMetaData.Builder tmpImdBuilder = IndexMetaData.builder(request.index());
final int routingNumShards; final int routingNumShards;

View File

@ -67,6 +67,7 @@ public class PartitionedRoutingIT extends ESIntegTestCase {
client().admin().indices().prepareCreate(index) client().admin().indices().prepareCreate(index)
.setSettings(Settings.builder() .setSettings(Settings.builder()
.put("index.number_of_shards", currentShards) .put("index.number_of_shards", currentShards)
.put("index.number_of_replicas", numberOfReplicas())
.put("index.routing_partition_size", partitionSize)) .put("index.routing_partition_size", partitionSize))
.addMapping("type", "{\"type\":{\"_routing\":{\"required\":true}}}", XContentType.JSON) .addMapping("type", "{\"type\":{\"_routing\":{\"required\":true}}}", XContentType.JSON)
.execute().actionGet(); .execute().actionGet();
@ -107,6 +108,7 @@ public class PartitionedRoutingIT extends ESIntegTestCase {
client().admin().indices().prepareShrinkIndex(previousIndex, index) client().admin().indices().prepareShrinkIndex(previousIndex, index)
.setSettings(Settings.builder() .setSettings(Settings.builder()
.put("index.number_of_shards", currentShards) .put("index.number_of_shards", currentShards)
.put("index.number_of_replicas", numberOfReplicas())
.build()).get(); .build()).get();
ensureGreen(); ensureGreen();
} }

View File

@ -0,0 +1,77 @@
---
"Shrink index ignores target template mapping":
- skip:
version: " - 5.99.99"
reason: bug fixed in 6.0
- do:
cluster.state: {}
# Get master node id
- set: { master_node: master }
# create index
- do:
indices.create:
index: source
wait_for_active_shards: 1
body:
settings:
# ensure everything is allocated on a single node
index.routing.allocation.include._id: $master
number_of_replicas: 0
mappings:
test:
properties:
count:
type: text
# index document
- do:
index:
index: source
type: test
id: "1"
body: { "count": "1" }
# create template matching shrink tagret
- do:
indices.put_template:
name: tpl1
body:
index_patterns: targ*
mappings:
test:
properties:
count:
type: integer
# make it read-only
- do:
indices.put_settings:
index: source
body:
index.blocks.write: true
index.number_of_replicas: 0
- do:
cluster.health:
wait_for_status: green
index: source
# now we do the actual shrink
- do:
indices.shrink:
index: "source"
target: "target"
wait_for_active_shards: 1
master_timeout: 10s
body:
settings:
index.number_of_replicas: 0
- do:
cluster.health:
wait_for_status: green