[7.x] Make noop template updates be cluster state noops (#57851) (#57880)

Backports the following commits to 7.x:

    Make noop template updates be cluster state noops (#57851)
This commit is contained in:
Lee Hinman 2020-06-09 09:26:06 -06:00 committed by GitHub
parent b501b282f8
commit cb2ce3736a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 7 deletions

View File

@ -180,7 +180,8 @@ public class MetadataIndexTemplateService {
// Package visible for testing
ClusterState addComponentTemplate(final ClusterState currentState, final boolean create,
final String name, final ComponentTemplate template) throws Exception {
if (create && currentState.metadata().componentTemplates().containsKey(name)) {
final ComponentTemplate existing = currentState.metadata().componentTemplates().get(name);
if (create && existing != null) {
throw new IllegalArgumentException("component template [" + name + "] already exists");
}
@ -195,8 +196,6 @@ public class MetadataIndexTemplateService {
.build();
}
validateTemplate(finalSettings, stringMappings, indicesService, xContentRegistry);
// Collect all the composable (index) templates that use this component template, we'll use
// this for validating that they're still going to be valid after this component template
// has been updated
@ -240,8 +239,15 @@ public class MetadataIndexTemplateService {
final Template finalTemplate = new Template(finalSettings,
stringMappings == null ? null : new CompressedXContent(stringMappings), template.template().aliases());
final ComponentTemplate finalComponentTemplate = new ComponentTemplate(finalTemplate, template.version(), template.metadata());
if (finalComponentTemplate.equals(existing)) {
return currentState;
}
validateTemplate(finalSettings, stringMappings, indicesService, xContentRegistry);
validate(name, finalComponentTemplate);
// Validate all composable index templates that use this component template
if (templatesUsingComponent.size() > 0) {
ClusterState tempStateWithComponentTemplateAdded = ClusterState.builder(currentState)
.metadata(Metadata.builder(currentState.metadata()).put(name, finalComponentTemplate))
@ -267,7 +273,7 @@ public class MetadataIndexTemplateService {
}
}
logger.info("adding component template [{}]", name);
logger.info("{} component template [{}]", existing == null ? "adding" : "updating", name);
return ClusterState.builder(currentState)
.metadata(Metadata.builder(currentState.metadata()).put(name, finalComponentTemplate))
.build();
@ -406,7 +412,8 @@ public class MetadataIndexTemplateService {
public ClusterState addIndexTemplateV2(final ClusterState currentState, final boolean create,
final String name, final ComposableIndexTemplate template) throws Exception {
if (create && currentState.metadata().templatesV2().containsKey(name)) {
final ComposableIndexTemplate existing = currentState.metadata().templatesV2().get(name);
if (create && existing != null) {
throw new IllegalArgumentException("index template [" + name + "] already exists");
}
@ -473,6 +480,10 @@ public class MetadataIndexTemplateService {
template.priority(), template.version(), template.metadata(), template.getDataStreamTemplate());
}
if (finalIndexTemplate.equals(existing)) {
return currentState;
}
validate(name, finalIndexTemplate);
// Finally, right before adding the template, we need to ensure that the composite settings,
@ -485,7 +496,7 @@ public class MetadataIndexTemplateService {
(finalIndexTemplate.composedOf().size() > 0 ? "with component templates " + finalIndexTemplate.composedOf() + " " : "") +
"is invalid", e);
}
logger.info("adding index template [{}]", name);
logger.info("{} index template [{}]", existing == null ? "adding" : "updating", name);
return ClusterState.builder(currentState)
.metadata(Metadata.builder(currentState.metadata()).put(name, finalIndexTemplate))
.build();

View File

@ -286,7 +286,7 @@ public class MetadataIndexTemplateServiceTests extends ESSingleNodeTestCase {
template = new Template(Settings.builder().build(), new CompressedXContent("{\"invalid\"}"),
ComponentTemplateTests.randomAliases());
ComponentTemplate componentTemplate2 = new ComponentTemplate(template, 1L, new HashMap<>());
expectThrows(MapperParsingException.class,
expectThrows(Exception.class,
() -> metadataIndexTemplateService.addComponentTemplate(throwState, true, "foo2", componentTemplate2));
template = new Template(Settings.builder().build(), new CompressedXContent("{\"invalid\":\"invalid\"}"),
@ -1052,6 +1052,28 @@ public class MetadataIndexTemplateServiceTests extends ESSingleNodeTestCase {
containsString("mapper [field2] cannot be changed from type [text] to [ObjectMapper]")));
}
public void testPutExistingComponentTemplateIsNoop() throws Exception {
MetadataIndexTemplateService metadataIndexTemplateService = getMetadataIndexTemplateService();
ClusterState state = ClusterState.EMPTY_STATE;
ComponentTemplate componentTemplate = ComponentTemplateTests.randomInstance();
state = metadataIndexTemplateService.addComponentTemplate(state, false, "foo", componentTemplate);
assertNotNull(state.metadata().componentTemplates().get("foo"));
assertThat(metadataIndexTemplateService.addComponentTemplate(state, false, "foo", componentTemplate), equalTo(state));
}
public void testPutExistingComposableTemplateIsNoop() throws Exception {
ClusterState state = ClusterState.EMPTY_STATE;
final MetadataIndexTemplateService metadataIndexTemplateService = getMetadataIndexTemplateService();
ComposableIndexTemplate template = ComposableIndexTemplateTests.randomInstance();
state = metadataIndexTemplateService.addIndexTemplateV2(state, false, "foo", template);
assertNotNull(state.metadata().templatesV2().get("foo"));
assertThat(metadataIndexTemplateService.addIndexTemplateV2(state, false, "foo", template), equalTo(state));
}
private static List<Throwable> putTemplate(NamedXContentRegistry xContentRegistry, PutRequest request) {
MetadataCreateIndexService createIndexService = new MetadataCreateIndexService(
Settings.EMPTY,