Make isHidden a primitive when finding v2 templates (#55408) (#55433)

isHidden was a `Boolean` in order to treat a special case identified
with V1 templates where if the create index request didn't specify if
the index should be hidden or not (ie. isHidden was `null`) but the
index matched a template that specified the `index.hidden` setting we
needed to remove the global templates from the templates we'll apply to
the new index (note: this is important with V1 templates as inheritance
is supported).

With V2 templates we match only one template with an index so the
equivalent check did not need to exist (we added a sanity check in
https://github.com/elastic/elasticsearch/pull/55015 where we make sure
we don't apply an invalid global template - one that specifes the
`index.hidden` setting, but this is a check we make irrespective of the
user specifying or not if the index should be hidden)

This commit makes `isHidden` when matching V2 templates a boolean
primitive, eliminating the need for the `null` state to exist. Note that
some methods which use the matching V2 templates still work with a
`Boolean` object `isHidden` attribute as they are also matching the V1
templates. These methods will pass in `false` instead of `null` when
finding the V2 templates.

(cherry picked from commit c5b923afec911c6ae8fc5179e65ae6bf55dcc5f1)
Signed-off-by: Andrei Dan <andrei.dan@elastic.co>
This commit is contained in:
Andrei Dan 2020-04-20 09:41:32 +01:00 committed by GitHub
parent 0458770556
commit e98b68b5f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 11 additions and 12 deletions

View File

@ -177,7 +177,7 @@ public class MetadataRolloverService {
} }
} }
final String matchedV2Template = findV2Template(metadata, rolloverIndexName, isHidden); final String matchedV2Template = findV2Template(metadata, rolloverIndexName, isHidden == null ? false : isHidden);
if (matchedV2Template != null) { if (matchedV2Template != null) {
List<Map<String, AliasMetadata>> aliases = MetadataIndexTemplateService.resolveAliases(metadata, matchedV2Template); List<Map<String, AliasMetadata>> aliases = MetadataIndexTemplateService.resolveAliases(metadata, matchedV2Template);
for (Map<String, AliasMetadata> aliasConfig : aliases) { for (Map<String, AliasMetadata> aliasConfig : aliases) {

View File

@ -313,7 +313,7 @@ public class TransportBulkAction extends HandledTransportAction<BulkRequest, Bul
// the index does not exist yet (and this is a valid request), so match index // the index does not exist yet (and this is a valid request), so match index
// templates to look for pipelines in either a matching V2 template (which takes // templates to look for pipelines in either a matching V2 template (which takes
// precedence), or if a V2 template does not match, any V1 templates // precedence), or if a V2 template does not match, any V1 templates
String v2Template = MetadataIndexTemplateService.findV2Template(metadata, indexRequest.index(), null); String v2Template = MetadataIndexTemplateService.findV2Template(metadata, indexRequest.index(), false);
if (v2Template != null) { if (v2Template != null) {
Settings settings = MetadataIndexTemplateService.resolveSettings(metadata, v2Template); Settings settings = MetadataIndexTemplateService.resolveSettings(metadata, v2Template);
if (defaultPipeline == null && IndexSettings.DEFAULT_PIPELINE.exists(settings)) { if (defaultPipeline == null && IndexSettings.DEFAULT_PIPELINE.exists(settings)) {

View File

@ -324,7 +324,7 @@ public class MetadataCreateIndexService {
// Check to see if a v2 template matched // Check to see if a v2 template matched
final String v2Template = MetadataIndexTemplateService.findV2Template(currentState.metadata(), final String v2Template = MetadataIndexTemplateService.findV2Template(currentState.metadata(),
request.index(), isHiddenFromRequest); request.index(), isHiddenFromRequest == null ? false : isHiddenFromRequest);
if (v2Template != null) { if (v2Template != null) {
// If a v2 template was found, it takes precedence over all v1 templates, so create // If a v2 template was found, it takes precedence over all v1 templates, so create

View File

@ -664,19 +664,18 @@ public class MetadataIndexTemplateService {
* the event that no templates are matched, {@code null} is returned. * the event that no templates are matched, {@code null} is returned.
*/ */
@Nullable @Nullable
public static String findV2Template(Metadata metadata, String indexName, @Nullable Boolean isHidden) { public static String findV2Template(Metadata metadata, String indexName, boolean isHidden) {
final Predicate<String> patternMatchPredicate = pattern -> Regex.simpleMatch(pattern, indexName); final Predicate<String> patternMatchPredicate = pattern -> Regex.simpleMatch(pattern, indexName);
final Map<IndexTemplateV2, String> matchedTemplates = new HashMap<>(); final Map<IndexTemplateV2, String> matchedTemplates = new HashMap<>();
for (Map.Entry<String, IndexTemplateV2> entry : metadata.templatesV2().entrySet()) { for (Map.Entry<String, IndexTemplateV2> entry : metadata.templatesV2().entrySet()) {
final String name = entry.getKey(); final String name = entry.getKey();
final IndexTemplateV2 template = entry.getValue(); final IndexTemplateV2 template = entry.getValue();
if (isHidden == null || isHidden == Boolean.FALSE) { if (isHidden == false) {
final boolean matched = template.indexPatterns().stream().anyMatch(patternMatchPredicate); final boolean matched = template.indexPatterns().stream().anyMatch(patternMatchPredicate);
if (matched) { if (matched) {
matchedTemplates.put(template, name); matchedTemplates.put(template, name);
} }
} else { } else {
assert isHidden == Boolean.TRUE;
final boolean isNotMatchAllTemplate = template.indexPatterns().stream().noneMatch(Regex::isMatchAllPattern); final boolean isNotMatchAllTemplate = template.indexPatterns().stream().noneMatch(Regex::isMatchAllPattern);
if (isNotMatchAllTemplate) { if (isNotMatchAllTemplate) {
if (template.indexPatterns().stream().anyMatch(patternMatchPredicate)) { if (template.indexPatterns().stream().anyMatch(patternMatchPredicate)) {

View File

@ -324,7 +324,7 @@ public class MetadataRolloverServiceTests extends ESTestCase {
MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, Boolean.TRUE); MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, Boolean.TRUE);
// not hidden will throw // not hidden will throw
final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () ->
MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, randomFrom(Boolean.FALSE, null))); MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, false));
assertThat(ex.getMessage(), containsString("index template [test-template]")); assertThat(ex.getMessage(), containsString("index template [test-template]"));
} }
@ -344,7 +344,7 @@ public class MetadataRolloverServiceTests extends ESTestCase {
MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, Boolean.TRUE); MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, Boolean.TRUE);
// not hidden will throw // not hidden will throw
final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () ->
MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, randomFrom(Boolean.FALSE, null))); MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, false));
assertThat(ex.getMessage(), containsString("index template [test-template]")); assertThat(ex.getMessage(), containsString("index template [test-template]"));
} }
@ -367,7 +367,7 @@ public class MetadataRolloverServiceTests extends ESTestCase {
MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, Boolean.TRUE); MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, Boolean.TRUE);
// not hidden will throw // not hidden will throw
final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () ->
MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, randomFrom(Boolean.FALSE, null))); MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, false));
assertThat(ex.getMessage(), containsString("index template [test-template]")); assertThat(ex.getMessage(), containsString("index template [test-template]"));
} }

View File

@ -490,7 +490,7 @@ public class MetadataIndexTemplateServiceTests extends ESSingleNodeTestCase {
public void testFindV2Templates() throws Exception { public void testFindV2Templates() throws Exception {
final MetadataIndexTemplateService service = getMetadataIndexTemplateService(); final MetadataIndexTemplateService service = getMetadataIndexTemplateService();
ClusterState state = ClusterState.EMPTY_STATE; ClusterState state = ClusterState.EMPTY_STATE;
assertNull(MetadataIndexTemplateService.findV2Template(state.metadata(), "index", randomBoolean() ? null : randomBoolean())); assertNull(MetadataIndexTemplateService.findV2Template(state.metadata(), "index", randomBoolean()));
ComponentTemplate ct = ComponentTemplateTests.randomInstance(); ComponentTemplate ct = ComponentTemplateTests.randomInstance();
state = service.addComponentTemplate(state, true, "ct", ct); state = service.addComponentTemplate(state, true, "ct", ct);
@ -499,7 +499,7 @@ public class MetadataIndexTemplateServiceTests extends ESSingleNodeTestCase {
IndexTemplateV2 it2 = new IndexTemplateV2(Collections.singletonList("in*"), null, Collections.singletonList("ct"), 10L, 2L, null); IndexTemplateV2 it2 = new IndexTemplateV2(Collections.singletonList("in*"), null, Collections.singletonList("ct"), 10L, 2L, null);
state = service.addIndexTemplateV2(state, true, "my-template2", it2); state = service.addIndexTemplateV2(state, true, "my-template2", it2);
String result = MetadataIndexTemplateService.findV2Template(state.metadata(), "index", randomBoolean() ? null : randomBoolean()); String result = MetadataIndexTemplateService.findV2Template(state.metadata(), "index", randomBoolean());
assertThat(result, equalTo("my-template2")); assertThat(result, equalTo("my-template2"));
} }
@ -531,7 +531,7 @@ public class MetadataIndexTemplateServiceTests extends ESSingleNodeTestCase {
new IndexTemplateV2Metadata(org.elasticsearch.common.collect.Map.of("invalid_global_template", invalidGlobalTemplate))) new IndexTemplateV2Metadata(org.elasticsearch.common.collect.Map.of("invalid_global_template", invalidGlobalTemplate)))
.build(); .build();
MetadataIndexTemplateService.findV2Template(invalidGlobalTemplateMetadata, "index-name", null); MetadataIndexTemplateService.findV2Template(invalidGlobalTemplateMetadata, "index-name", false);
fail("expecting an exception as the matching global template is invalid"); fail("expecting an exception as the matching global template is invalid");
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
assertThat(e.getMessage(), is("global index template [invalid_global_template], composed of component templates [ct] " + assertThat(e.getMessage(), is("global index template [invalid_global_template], composed of component templates [ct] " +