Merge pull request #20072 from rjernst/remove_index_template_filter

Plugins: Remove IndexTemplateFilter
This commit is contained in:
Ryan Ernst 2016-08-19 21:13:20 -07:00 committed by GitHub
commit 6ed83ce36a
8 changed files with 5 additions and 163 deletions

View File

@ -83,7 +83,6 @@ public class ClusterModule extends AbstractModule {
new Setting<>("cluster.routing.allocation.type", BALANCED_ALLOCATOR, Function.identity(), Property.NodeScope); new Setting<>("cluster.routing.allocation.type", BALANCED_ALLOCATOR, Function.identity(), Property.NodeScope);
private final Settings settings; private final Settings settings;
private final ExtensionPoint.ClassSet<IndexTemplateFilter> indexTemplateFilters = new ExtensionPoint.ClassSet<>("index_template_filter", IndexTemplateFilter.class);
private final ClusterService clusterService; private final ClusterService clusterService;
private final IndexNameExpressionResolver indexNameExpressionResolver; private final IndexNameExpressionResolver indexNameExpressionResolver;
// pkg private for tests // pkg private for tests
@ -101,10 +100,6 @@ public class ClusterModule extends AbstractModule {
indexNameExpressionResolver = new IndexNameExpressionResolver(settings); indexNameExpressionResolver = new IndexNameExpressionResolver(settings);
} }
public void registerIndexTemplateFilter(Class<? extends IndexTemplateFilter> indexTemplateFilter) {
indexTemplateFilters.registerExtension(indexTemplateFilter);
}
public IndexNameExpressionResolver getIndexNameExpressionResolver() { public IndexNameExpressionResolver getIndexNameExpressionResolver() {
return indexNameExpressionResolver; return indexNameExpressionResolver;
} }
@ -167,8 +162,6 @@ public class ClusterModule extends AbstractModule {
@Override @Override
protected void configure() { protected void configure() {
indexTemplateFilters.bind(binder());
bind(ClusterInfoService.class).to(clusterInfoServiceImpl).asEagerSingleton(); bind(ClusterInfoService.class).to(clusterInfoServiceImpl).asEagerSingleton();
bind(GatewayAllocator.class).asEagerSingleton(); bind(GatewayAllocator.class).asEagerSingleton();
bind(AllocationService.class).asEagerSingleton(); bind(AllocationService.class).asEagerSingleton();

View File

@ -31,23 +31,4 @@ public interface IndexTemplateFilter {
* {@code false} otherwise. * {@code false} otherwise.
*/ */
boolean apply(CreateIndexClusterStateUpdateRequest request, IndexTemplateMetaData template); 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;
}
}
} }

View File

@ -102,13 +102,11 @@ import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_VERSION_C
public class MetaDataCreateIndexService extends AbstractComponent { public class MetaDataCreateIndexService extends AbstractComponent {
public static final int MAX_INDEX_NAME_BYTES = 255; 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 ClusterService clusterService;
private final IndicesService indicesService; private final IndicesService indicesService;
private final AllocationService allocationService; private final AllocationService allocationService;
private final AliasValidator aliasValidator; private final AliasValidator aliasValidator;
private final IndexTemplateFilter indexTemplateFilter;
private final Environment env; private final Environment env;
private final NodeServicesProvider nodeServicesProvider; private final NodeServicesProvider nodeServicesProvider;
private final IndexScopedSettings indexScopedSettings; private final IndexScopedSettings indexScopedSettings;
@ -117,8 +115,7 @@ public class MetaDataCreateIndexService extends AbstractComponent {
@Inject @Inject
public MetaDataCreateIndexService(Settings settings, ClusterService clusterService, public MetaDataCreateIndexService(Settings settings, ClusterService clusterService,
IndicesService indicesService, AllocationService allocationService, IndicesService indicesService, AllocationService allocationService,
AliasValidator aliasValidator, AliasValidator aliasValidator, Environment env,
Set<IndexTemplateFilter> indexTemplateFilters, Environment env,
NodeServicesProvider nodeServicesProvider, IndexScopedSettings indexScopedSettings, NodeServicesProvider nodeServicesProvider, IndexScopedSettings indexScopedSettings,
ThreadPool threadPool) { ThreadPool threadPool) {
super(settings); super(settings);
@ -129,18 +126,6 @@ public class MetaDataCreateIndexService extends AbstractComponent {
this.env = env; this.env = env;
this.nodeServicesProvider = nodeServicesProvider; this.nodeServicesProvider = nodeServicesProvider;
this.indexScopedSettings = indexScopedSettings; 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); 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) // we only find a template when its an API call (a new index)
// find templates, highest order are better matching // find templates, highest order are better matching
List<IndexTemplateMetaData> templates = findTemplates(request, currentState, indexTemplateFilter); List<IndexTemplateMetaData> templates = findTemplates(request, currentState);
Map<String, Custom> customs = new HashMap<>(); Map<String, Custom> customs = new HashMap<>();
@ -470,11 +455,11 @@ public class MetaDataCreateIndexService extends AbstractComponent {
}); });
} }
private List<IndexTemplateMetaData> findTemplates(CreateIndexClusterStateUpdateRequest request, ClusterState state, IndexTemplateFilter indexTemplateFilter) throws IOException { private List<IndexTemplateMetaData> findTemplates(CreateIndexClusterStateUpdateRequest request, ClusterState state) throws IOException {
List<IndexTemplateMetaData> templates = new ArrayList<>(); List<IndexTemplateMetaData> templates = new ArrayList<>();
for (ObjectCursor<IndexTemplateMetaData> cursor : state.metaData().templates().values()) { for (ObjectCursor<IndexTemplateMetaData> cursor : state.metaData().templates().values()) {
IndexTemplateMetaData template = cursor.value; IndexTemplateMetaData template = cursor.value;
if (indexTemplateFilter.apply(request, template)) { if (Regex.simpleMatch(template.template(), request.index())) {
templates.add(template); templates.add(template);
} }
} }
@ -525,13 +510,6 @@ public class MetaDataCreateIndexService extends AbstractComponent {
return validationErrors; 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. * 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 * @return the list of nodes at least one instance of the source index shards are allocated

View File

@ -158,7 +158,6 @@ public class MetaDataIndexTemplateServiceTests extends ESSingleNodeTestCase {
null, null,
null, null,
null, null,
new HashSet<>(),
null, null,
null, null, null); null, null, null);
MetaDataIndexTemplateService service = new MetaDataIndexTemplateService(Settings.EMPTY, null, createIndexService, new AliasValidator(Settings.EMPTY), 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, indicesService,
null, null,
null, null,
new HashSet<>(),
null, null,
nodeServicesProvider, nodeServicesProvider,
null, null,

View File

@ -67,13 +67,6 @@ public class ClusterModuleTests extends ModuleTestCase {
} }
} }
static class FakeIndexTemplateFilter implements IndexTemplateFilter {
@Override
public boolean apply(CreateIndexClusterStateUpdateRequest request, IndexTemplateMetaData template) {
return false;
}
}
public void testRegisterClusterDynamicSettingDuplicate() { public void testRegisterClusterDynamicSettingDuplicate() {
try { try {
new SettingsModule(Settings.EMPTY, EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING); new SettingsModule(Settings.EMPTY, EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING);
@ -163,21 +156,4 @@ public class ClusterModuleTests extends ModuleTestCase {
NullPointerException e = expectThrows(NullPointerException.class, () -> NullPointerException e = expectThrows(NullPointerException.class, () ->
newClusterModuleWithShardsAllocator(settings, "bad", () -> null)); newClusterModuleWithShardsAllocator(settings, "bad", () -> null));
} }
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);
}
} }

View File

@ -215,7 +215,6 @@ public class MetaDataCreateIndexServiceTests extends ESTestCase {
null, null,
null, null,
null, null,
new HashSet<>(),
null, null,
null, null,
null, null,

View File

@ -165,7 +165,7 @@ public class ClusterStateChanges extends AbstractComponent {
MetaDataUpdateSettingsService metaDataUpdateSettingsService = new MetaDataUpdateSettingsService(settings, clusterService, MetaDataUpdateSettingsService metaDataUpdateSettingsService = new MetaDataUpdateSettingsService(settings, clusterService,
allocationService, IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, indicesService, nodeServicesProvider); allocationService, IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, indicesService, nodeServicesProvider);
MetaDataCreateIndexService createIndexService = new MetaDataCreateIndexService(settings, clusterService, indicesService, 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); nodeServicesProvider, IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, threadPool);
transportCloseIndexAction = new TransportCloseIndexAction(settings, transportService, clusterService, threadPool, transportCloseIndexAction = new TransportCloseIndexAction(settings, transportService, clusterService, threadPool,

View File

@ -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<Class<? extends Plugin>> 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<String, MappingMetaData> 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);
}
}
}