Downgrade template update error to a warning for v1 templates (#55611)

For 7.x, we already implemented the `?prefer_v2_templates` flag and made V2 templates opt-in, so we
can relax the error when updating V1 templates to just a warning. This will still be a hard error
for 8.0+

Relates to #53101
This commit is contained in:
Lee Hinman 2020-04-28 09:16:08 -06:00 committed by GitHub
parent 51a94102e8
commit 3b211c1212
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 46 deletions

View File

@ -575,38 +575,21 @@ public class MetadataIndexTemplateService {
if (request.create && isUpdate) {
throw new IllegalArgumentException("index_template [" + request.name + "] already exists");
}
boolean isUpdateAndPatternsAreUnchanged = isUpdate &&
currentState.metadata().templates().get(request.name).patterns().equals(request.indexPatterns);
Map<String, List<String>> overlaps = findConflictingV2Templates(currentState, request.name, request.indexPatterns);
if (overlaps.size() > 0) {
// Be less strict (just a warning) if we're updating an existing template or this is a match-all template
if (isUpdateAndPatternsAreUnchanged || request.indexPatterns.stream().anyMatch(Regex::isMatchAllPattern)) {
String warning = String.format(Locale.ROOT, "template [%s] has index patterns %s matching patterns" +
" from existing index templates [%s] with patterns (%s); this template [%s] may be ignored in favor of " +
"an index template at index creation time",
request.name,
request.indexPatterns,
Strings.collectionToCommaDelimitedString(overlaps.keySet()),
overlaps.entrySet().stream()
.map(e -> e.getKey() + " => " + e.getValue())
.collect(Collectors.joining(",")),
request.name);
logger.warn(warning);
deprecationLogger.deprecatedAndMaybeLog("index_template_pattern_overlap", warning);
} else {
// Otherwise, this is a hard error, the user should use V2 index templates instead
String error = String.format(Locale.ROOT, "template [%s] has index patterns %s matching patterns" +
" from existing index templates [%s] with patterns (%s), use index templates (/_index_template) instead",
request.name,
request.indexPatterns,
Strings.collectionToCommaDelimitedString(overlaps.keySet()),
overlaps.entrySet().stream()
.map(e -> e.getKey() + " => " + e.getValue())
.collect(Collectors.joining(",")));
logger.error(error);
throw new IllegalArgumentException(error);
}
String warning = String.format(Locale.ROOT, "template [%s] has index patterns %s matching patterns" +
" from existing index templates [%s] with patterns (%s); this template [%s] may be ignored in favor of " +
"an index template at index creation time",
request.name,
request.indexPatterns,
Strings.collectionToCommaDelimitedString(overlaps.keySet()),
overlaps.entrySet().stream()
.map(e -> e.getKey() + " => " + e.getValue())
.collect(Collectors.joining(",")),
request.name);
logger.warn(warning);
deprecationLogger.deprecatedAndMaybeLog("index_template_pattern_overlap", warning);
}
templateBuilder.order(request.order);

View File

@ -484,24 +484,20 @@ public class MetadataIndexTemplateServiceTests extends ESSingleNodeTestCase {
}
/**
* Test that if we have a pre-existing v2 template and put a v1 template that would match the same indices, we generate a hard error
* Test that if we have a pre-existing v2 template and put a v1 template that would match the same indices, we generate a warning
*/
public void testPuttingV1NonStarTemplateGeneratesError() throws Exception {
public void testPuttingV1NonStarTemplateGeneratesWarning() throws Exception {
final MetadataIndexTemplateService metadataIndexTemplateService = getMetadataIndexTemplateService();
IndexTemplateV2 v2Template = new IndexTemplateV2(Arrays.asList("foo-bar-*", "eggplant"), null, null, null, null, null);
ClusterState state = metadataIndexTemplateService.addIndexTemplateV2(ClusterState.EMPTY_STATE, false, "v2-template", v2Template);
MetadataIndexTemplateService.PutRequest req = new MetadataIndexTemplateService.PutRequest("cause", "v1-template");
req.patterns(Arrays.asList("egg*", "baz"));
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> MetadataIndexTemplateService.innerPutTemplate(state, req, IndexTemplateMetadata.builder("v1-template")));
MetadataIndexTemplateService.innerPutTemplate(state, req, IndexTemplateMetadata.builder("v1-template"));
assertThat(e.getMessage(),
equalTo("template [v1-template] has index patterns [egg*, baz] matching patterns from existing index " +
"templates [v2-template] with patterns (v2-template => [foo-bar-*, eggplant]), use index templates " +
"(/_index_template) instead"));
assertNull(state.metadata().templates().get("v1-template"));
assertWarnings("template [v1-template] has index patterns [egg*, baz] matching patterns " +
"from existing index templates [v2-template] with patterns (v2-template => [foo-bar-*, eggplant]);" +
" this template [v1-template] may be ignored in favor of an index template at index creation time");
}
/**
@ -547,9 +543,9 @@ public class MetadataIndexTemplateServiceTests extends ESSingleNodeTestCase {
/**
* Test that if we have a pre-existing v1 and v2 template, and we update the existing v1
* template *AND* change the index patterns that an error is generated
* template *AND* change the index patterns that a warning is generated
*/
public void testUpdatingV1NonStarWithChangedPatternsTemplateGeneratesError() throws Exception {
public void testUpdatingV1NonStarWithChangedPatternsTemplateGeneratesWarning() throws Exception {
final MetadataIndexTemplateService metadataIndexTemplateService = getMetadataIndexTemplateService();
IndexTemplateMetadata v1Template = IndexTemplateMetadata.builder("v1-template")
.patterns(Arrays.asList("fo*", "baz"))
@ -576,12 +572,11 @@ public class MetadataIndexTemplateServiceTests extends ESSingleNodeTestCase {
MetadataIndexTemplateService.PutRequest req = new MetadataIndexTemplateService.PutRequest("cause", "v1-template");
req.patterns(Arrays.asList("egg*", "baz"));
final ClusterState finalState = state;
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> MetadataIndexTemplateService.innerPutTemplate(finalState, req, IndexTemplateMetadata.builder("v1-template")));
MetadataIndexTemplateService.innerPutTemplate(finalState, req, IndexTemplateMetadata.builder("v1-template"));
assertThat(e.getMessage(), equalTo("template [v1-template] has index patterns [egg*, baz] matching patterns " +
"from existing index templates [v2-template] with patterns (v2-template => [foo-bar-*, eggplant]), use index " +
"templates (/_index_template) instead"));
assertWarnings("template [v1-template] has index patterns [egg*, baz] matching patterns " +
"from existing index templates [v2-template] with patterns (v2-template => [foo-bar-*, eggplant]); " +
"this template [v1-template] may be ignored in favor of an index template at index creation time");
}
public void testPuttingOverlappingV2Template() throws Exception {