From 00c123b59f8ba11eb260e6b70acf7be80bccc949 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Thu, 18 Aug 2016 22:41:25 -0700 Subject: [PATCH] Plugins: Remove IndexTemplateFilter How index templates match is currently controlled by the IndexTemplateFilter interface. It is pluggable, to add additional filter implementations to the default glob matcher. This change removes the IndexTemplateFilter interface completely. This is a very esoteric extension point, and not worth maintaining. Instead, any improvements should be made to all of our glob matching. --- .../elasticsearch/cluster/ClusterModule.java | 6 -- .../cluster/metadata/IndexTemplateFilter.java | 19 ----- .../metadata/MetaDataCreateIndexService.java | 30 +------ .../MetaDataIndexTemplateServiceTests.java | 2 - .../cluster/ClusterModuleTests.java | 24 ------ .../MetaDataCreateIndexServiceTests.java | 1 - .../indices/cluster/ClusterStateChanges.java | 2 +- .../template/IndexTemplateFilteringIT.java | 83 ------------------- 8 files changed, 5 insertions(+), 162 deletions(-) delete mode 100644 core/src/test/java/org/elasticsearch/indices/template/IndexTemplateFilteringIT.java diff --git a/core/src/main/java/org/elasticsearch/cluster/ClusterModule.java b/core/src/main/java/org/elasticsearch/cluster/ClusterModule.java index 34cf7df6a2e..c23dfb96a94 100644 --- a/core/src/main/java/org/elasticsearch/cluster/ClusterModule.java +++ b/core/src/main/java/org/elasticsearch/cluster/ClusterModule.java @@ -83,7 +83,6 @@ public class ClusterModule extends AbstractModule { private final Settings settings; private final ExtensionPoint.SelectedType shardsAllocators = new ExtensionPoint.SelectedType<>("shards_allocator", ShardsAllocator.class); - private final ExtensionPoint.ClassSet indexTemplateFilters = new ExtensionPoint.ClassSet<>("index_template_filter", IndexTemplateFilter.class); private final ClusterService clusterService; private final IndexNameExpressionResolver indexNameExpressionResolver; // pkg private for tests @@ -105,10 +104,6 @@ public class ClusterModule extends AbstractModule { shardsAllocators.registerExtension(name, clazz); } - public void registerIndexTemplateFilter(Class indexTemplateFilter) { - indexTemplateFilters.registerExtension(indexTemplateFilter); - } - public IndexNameExpressionResolver getIndexNameExpressionResolver() { return indexNameExpressionResolver; } @@ -156,7 +151,6 @@ public class ClusterModule extends AbstractModule { final ESLogger logger = Loggers.getLogger(getClass(), settings); logger.warn("{} allocator has been removed in 2.0 using {} instead", ClusterModule.EVEN_SHARD_COUNT_ALLOCATOR, ClusterModule.BALANCED_ALLOCATOR); } - indexTemplateFilters.bind(binder()); bind(ClusterInfoService.class).to(clusterInfoServiceImpl).asEagerSingleton(); bind(GatewayAllocator.class).asEagerSingleton(); diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateFilter.java b/core/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateFilter.java index 0ec3c5f863b..6ed9664c92b 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateFilter.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateFilter.java @@ -31,23 +31,4 @@ public interface IndexTemplateFilter { * {@code false} otherwise. */ boolean apply(CreateIndexClusterStateUpdateRequest request, IndexTemplateMetaData template); - - class Compound implements IndexTemplateFilter { - - private IndexTemplateFilter[] filters; - - Compound(IndexTemplateFilter... filters) { - this.filters = filters; - } - - @Override - public boolean apply(CreateIndexClusterStateUpdateRequest request, IndexTemplateMetaData template) { - for (IndexTemplateFilter filter : filters) { - if (!filter.apply(request, template)) { - return false; - } - } - return true; - } - } } diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java index dfb5474ca59..041850a0b27 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java @@ -102,13 +102,11 @@ import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_VERSION_C public class MetaDataCreateIndexService extends AbstractComponent { public static final int MAX_INDEX_NAME_BYTES = 255; - private static final DefaultIndexTemplateFilter DEFAULT_INDEX_TEMPLATE_FILTER = new DefaultIndexTemplateFilter(); private final ClusterService clusterService; private final IndicesService indicesService; private final AllocationService allocationService; private final AliasValidator aliasValidator; - private final IndexTemplateFilter indexTemplateFilter; private final Environment env; private final NodeServicesProvider nodeServicesProvider; private final IndexScopedSettings indexScopedSettings; @@ -117,8 +115,7 @@ public class MetaDataCreateIndexService extends AbstractComponent { @Inject public MetaDataCreateIndexService(Settings settings, ClusterService clusterService, IndicesService indicesService, AllocationService allocationService, - AliasValidator aliasValidator, - Set indexTemplateFilters, Environment env, + AliasValidator aliasValidator, Environment env, NodeServicesProvider nodeServicesProvider, IndexScopedSettings indexScopedSettings, ThreadPool threadPool) { super(settings); @@ -129,18 +126,6 @@ public class MetaDataCreateIndexService extends AbstractComponent { this.env = env; this.nodeServicesProvider = nodeServicesProvider; this.indexScopedSettings = indexScopedSettings; - - if (indexTemplateFilters.isEmpty()) { - this.indexTemplateFilter = DEFAULT_INDEX_TEMPLATE_FILTER; - } else { - IndexTemplateFilter[] templateFilters = new IndexTemplateFilter[indexTemplateFilters.size() + 1]; - templateFilters[0] = DEFAULT_INDEX_TEMPLATE_FILTER; - int i = 1; - for (IndexTemplateFilter indexTemplateFilter : indexTemplateFilters) { - templateFilters[i++] = indexTemplateFilter; - } - this.indexTemplateFilter = new IndexTemplateFilter.Compound(templateFilters); - } this.activeShardsObserver = new ActiveShardsObserver(settings, clusterService, threadPool); } @@ -242,7 +227,7 @@ public class MetaDataCreateIndexService extends AbstractComponent { // we only find a template when its an API call (a new index) // find templates, highest order are better matching - List templates = findTemplates(request, currentState, indexTemplateFilter); + List templates = findTemplates(request, currentState); Map customs = new HashMap<>(); @@ -470,11 +455,11 @@ public class MetaDataCreateIndexService extends AbstractComponent { }); } - private List findTemplates(CreateIndexClusterStateUpdateRequest request, ClusterState state, IndexTemplateFilter indexTemplateFilter) throws IOException { + private List findTemplates(CreateIndexClusterStateUpdateRequest request, ClusterState state) throws IOException { List templates = new ArrayList<>(); for (ObjectCursor cursor : state.metaData().templates().values()) { IndexTemplateMetaData template = cursor.value; - if (indexTemplateFilter.apply(request, template)) { + if (Regex.simpleMatch(template.template(), request.index())) { templates.add(template); } } @@ -525,13 +510,6 @@ public class MetaDataCreateIndexService extends AbstractComponent { return validationErrors; } - private static class DefaultIndexTemplateFilter implements IndexTemplateFilter { - @Override - public boolean apply(CreateIndexClusterStateUpdateRequest request, IndexTemplateMetaData template) { - return Regex.simpleMatch(template.template(), request.index()); - } - } - /** * Validates the settings and mappings for shrinking an index. * @return the list of nodes at least one instance of the source index shards are allocated diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/template/put/MetaDataIndexTemplateServiceTests.java b/core/src/test/java/org/elasticsearch/action/admin/indices/template/put/MetaDataIndexTemplateServiceTests.java index 8493c58729d..d895ef9cbf5 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/indices/template/put/MetaDataIndexTemplateServiceTests.java +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/template/put/MetaDataIndexTemplateServiceTests.java @@ -158,7 +158,6 @@ public class MetaDataIndexTemplateServiceTests extends ESSingleNodeTestCase { null, null, null, - new HashSet<>(), null, null, null, null); MetaDataIndexTemplateService service = new MetaDataIndexTemplateService(Settings.EMPTY, null, createIndexService, new AliasValidator(Settings.EMPTY), null, null); @@ -188,7 +187,6 @@ public class MetaDataIndexTemplateServiceTests extends ESSingleNodeTestCase { indicesService, null, null, - new HashSet<>(), null, nodeServicesProvider, null, diff --git a/core/src/test/java/org/elasticsearch/cluster/ClusterModuleTests.java b/core/src/test/java/org/elasticsearch/cluster/ClusterModuleTests.java index 6f2ede1c49f..865004ff68a 100644 --- a/core/src/test/java/org/elasticsearch/cluster/ClusterModuleTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/ClusterModuleTests.java @@ -65,13 +65,6 @@ public class ClusterModuleTests extends ModuleTestCase { } } - static class FakeIndexTemplateFilter implements IndexTemplateFilter { - @Override - public boolean apply(CreateIndexClusterStateUpdateRequest request, IndexTemplateMetaData template) { - return false; - } - } - public void testRegisterClusterDynamicSettingDuplicate() { try { new SettingsModule(Settings.EMPTY, EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING); @@ -154,21 +147,4 @@ public class ClusterModuleTests extends ModuleTestCase { ClusterModule module = new ClusterModule(settings, clusterService, Collections.emptyList()); assertBinding(module, ShardsAllocator.class, BalancedShardsAllocator.class); } - - public void testRegisterIndexTemplateFilterDuplicate() { - ClusterModule module = new ClusterModule(Settings.EMPTY, clusterService, Collections.emptyList()); - try { - module.registerIndexTemplateFilter(FakeIndexTemplateFilter.class); - module.registerIndexTemplateFilter(FakeIndexTemplateFilter.class); - } catch (IllegalArgumentException e) { - assertEquals(e.getMessage(), - "Can't register the same [index_template_filter] more than once for [" + FakeIndexTemplateFilter.class.getName() + "]"); - } - } - - public void testRegisterIndexTemplateFilter() { - ClusterModule module = new ClusterModule(Settings.EMPTY, clusterService, Collections.emptyList()); - module.registerIndexTemplateFilter(FakeIndexTemplateFilter.class); - assertSetMultiBinding(module, IndexTemplateFilter.class, FakeIndexTemplateFilter.class); - } } diff --git a/core/src/test/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexServiceTests.java b/core/src/test/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexServiceTests.java index adf34859d4e..de82ce8d073 100644 --- a/core/src/test/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexServiceTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexServiceTests.java @@ -215,7 +215,6 @@ public class MetaDataCreateIndexServiceTests extends ESTestCase { null, null, null, - new HashSet<>(), null, null, null, diff --git a/core/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java b/core/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java index c3429edc39b..a5e4b2ab7e1 100644 --- a/core/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java +++ b/core/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java @@ -165,7 +165,7 @@ public class ClusterStateChanges extends AbstractComponent { MetaDataUpdateSettingsService metaDataUpdateSettingsService = new MetaDataUpdateSettingsService(settings, clusterService, allocationService, IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, indicesService, nodeServicesProvider); MetaDataCreateIndexService createIndexService = new MetaDataCreateIndexService(settings, clusterService, indicesService, - allocationService, new AliasValidator(settings), Collections.emptySet(), environment, + allocationService, new AliasValidator(settings), environment, nodeServicesProvider, IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, threadPool); transportCloseIndexAction = new TransportCloseIndexAction(settings, transportService, clusterService, threadPool, diff --git a/core/src/test/java/org/elasticsearch/indices/template/IndexTemplateFilteringIT.java b/core/src/test/java/org/elasticsearch/indices/template/IndexTemplateFilteringIT.java deleted file mode 100644 index 356538d62cb..00000000000 --- a/core/src/test/java/org/elasticsearch/indices/template/IndexTemplateFilteringIT.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.indices.template; - -import org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest; -import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse; -import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.cluster.metadata.IndexTemplateFilter; -import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; -import org.elasticsearch.cluster.metadata.MappingMetaData; -import org.elasticsearch.common.collect.ImmutableOpenMap; -import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.test.ESIntegTestCase; -import org.elasticsearch.test.ESIntegTestCase.ClusterScope; -import org.elasticsearch.test.ESIntegTestCase.Scope; - -import java.util.Arrays; -import java.util.Collection; - -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.core.IsNull.notNullValue; - -@ClusterScope(scope = Scope.SUITE) -public class IndexTemplateFilteringIT extends ESIntegTestCase { - @Override - protected Collection> nodePlugins() { - return Arrays.asList(TestPlugin.class); - } - - public void testTemplateFiltering() throws Exception { - client().admin().indices().preparePutTemplate("template1") - .setTemplate("test*") - .addMapping("type1", "field1", "type=text").get(); - - client().admin().indices().preparePutTemplate("template2") - .setTemplate("test*") - .addMapping("type2", "field2", "type=text").get(); - - client().admin().indices().preparePutTemplate("template3") - .setTemplate("no_match") - .addMapping("type3", "field3", "type=text").get(); - - assertAcked(prepareCreate("test")); - - GetMappingsResponse response = client().admin().indices().prepareGetMappings("test").get(); - assertThat(response, notNullValue()); - ImmutableOpenMap metadata = response.getMappings().get("test"); - assertThat(metadata.size(), is(1)); - assertThat(metadata.get("type2"), notNullValue()); - } - - public static class TestFilter implements IndexTemplateFilter { - @Override - public boolean apply(CreateIndexClusterStateUpdateRequest request, IndexTemplateMetaData template) { - //make sure that no_match template is filtered out before the custom filters as it doesn't match the index name - return (template.name().equals("template2") || template.name().equals("no_match")); - } - } - - public static class TestPlugin extends Plugin { - public void onModule(ClusterModule module) { - module.registerIndexTemplateFilter(TestFilter.class); - } - } -}