From 1cb30c3a493c2cebf49a2479cdda3f470c0bb179 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Wed, 7 Oct 2015 20:34:44 +0200 Subject: [PATCH] Fold IndexAliasesService into IndexService The IndexAliasesService abstraction only adds unnecessary code and classes and can be removed. This commit folds the rather simple methods in this class into IndexService where the IndexAliasesService was obtained from in the past. --- .../metadata/MetaDataCreateIndexService.java | 4 +- .../metadata/MetaDataIndexAliasesService.java | 2 +- .../metadata/MetaDataMappingService.java | 4 +- .../org/elasticsearch/index/IndexModule.java | 11 +- .../org/elasticsearch/index/IndexService.java | 95 +++++++++++-- .../index/IndexServicesProvider.java | 9 +- .../index/aliases/IndexAliasesService.java | 127 ------------------ .../elasticsearch/index/shard/IndexShard.java | 1 - .../elasticsearch/indices/IndicesService.java | 14 +- .../cluster/IndicesClusterStateService.java | 6 +- .../percolator/PercolatorService.java | 2 +- .../search/internal/DefaultSearchContext.java | 2 +- .../common/inject/ModuleTestCase.java | 6 +- .../elasticsearch/index/IndexModuleTests.java | 10 +- .../index/IndexServiceTests.java | 107 ++++++++++++++- .../aliases/IndexAliasesServiceTests.java | 121 ----------------- .../index/shard/IndexShardTests.java | 2 +- .../indices/IndicesLifecycleListenerIT.java | 2 +- 18 files changed, 224 insertions(+), 301 deletions(-) delete mode 100644 core/src/main/java/org/elasticsearch/index/aliases/IndexAliasesService.java delete mode 100644 core/src/test/java/org/elasticsearch/index/aliases/IndexAliasesServiceTests.java 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 049eadcb281..64492d8a92c 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java @@ -360,9 +360,9 @@ public class MetaDataCreateIndexService extends AbstractComponent { Settings actualIndexSettings = indexSettingsBuilder.build(); // Set up everything, now locally create the index to see that things are ok, and apply - + final IndexMetaData tmpImd = IndexMetaData.builder(request.index()).settings(actualIndexSettings).build(); // create the index here (on the master) to validate it can be created, as well as adding the mapping - indicesService.createIndex(request.index(), actualIndexSettings, clusterService.localNode().id()); + indicesService.createIndex(tmpImd); indexCreated = true; // now add the mappings IndexService indexService = indicesService.indexServiceSafe(request.index()); diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexAliasesService.java b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexAliasesService.java index bbe88f4d8c4..82d7f5511eb 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexAliasesService.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexAliasesService.java @@ -98,7 +98,7 @@ public class MetaDataIndexAliasesService extends AbstractComponent { if (indexService == null) { // temporarily create the index and add mappings so we can parse the filter try { - indexService = indicesService.createIndex(indexMetaData.index(), indexMetaData.settings(), clusterService.localNode().id()); + indexService = indicesService.createIndex(indexMetaData); if (indexMetaData.mappings().containsKey(MapperService.DEFAULT_MAPPING)) { indexService.mapperService().merge(MapperService.DEFAULT_MAPPING, indexMetaData.mappings().get(MapperService.DEFAULT_MAPPING).source(), false, false); } diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java index c9662e7266a..36d727a1134 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java @@ -172,7 +172,7 @@ public class MetaDataMappingService extends AbstractComponent { IndexService indexService = indicesService.indexService(index); if (indexService == null) { // we need to create the index here, and add the current mapping to it, so we can merge - indexService = indicesService.createIndex(indexMetaData.index(), indexMetaData.settings(), currentState.nodes().localNode().id()); + indexService = indicesService.createIndex(indexMetaData); removeIndex = true; Set typesToIntroduce = new HashSet<>(); for (MappingTask task : tasks) { @@ -350,7 +350,7 @@ public class MetaDataMappingService extends AbstractComponent { continue; } final IndexMetaData indexMetaData = currentState.metaData().index(index); - IndexService indexService = indicesService.createIndex(indexMetaData.index(), indexMetaData.settings(), clusterService.localNode().id()); + IndexService indexService = indicesService.createIndex(indexMetaData); indicesToClose.add(indexMetaData.index()); // make sure to add custom default mapping if exists if (indexMetaData.mappings().containsKey(MapperService.DEFAULT_MAPPING)) { diff --git a/core/src/main/java/org/elasticsearch/index/IndexModule.java b/core/src/main/java/org/elasticsearch/index/IndexModule.java index 192984815d2..dc637cfd5e9 100644 --- a/core/src/main/java/org/elasticsearch/index/IndexModule.java +++ b/core/src/main/java/org/elasticsearch/index/IndexModule.java @@ -19,9 +19,9 @@ package org.elasticsearch.index; +import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.util.Providers; -import org.elasticsearch.index.aliases.IndexAliasesService; import org.elasticsearch.index.engine.EngineFactory; import org.elasticsearch.index.engine.InternalEngineFactory; import org.elasticsearch.index.fielddata.IndexFieldDataService; @@ -33,10 +33,15 @@ import org.elasticsearch.index.shard.IndexSearcherWrapper; */ public class IndexModule extends AbstractModule { + private final IndexMetaData indexMetaData; // pkg private so tests can mock Class engineFactoryImpl = InternalEngineFactory.class; Class indexSearcherWrapper = null; - + + public IndexModule(IndexMetaData indexMetaData) { + this.indexMetaData = indexMetaData; + } + @Override protected void configure() { bind(EngineFactory.class).to(engineFactoryImpl).asEagerSingleton(); @@ -45,10 +50,10 @@ public class IndexModule extends AbstractModule { } else { bind(IndexSearcherWrapper.class).to(indexSearcherWrapper).asEagerSingleton(); } + bind(IndexMetaData.class).toInstance(indexMetaData); bind(IndexService.class).asEagerSingleton(); bind(IndexServicesProvider.class).asEagerSingleton(); bind(MapperService.class).asEagerSingleton(); - bind(IndexAliasesService.class).asEagerSingleton(); bind(IndexFieldDataService.class).asEagerSingleton(); } diff --git a/core/src/main/java/org/elasticsearch/index/IndexService.java b/core/src/main/java/org/elasticsearch/index/IndexService.java index 602ccc3bbce..9c932e2468a 100644 --- a/core/src/main/java/org/elasticsearch/index/IndexService.java +++ b/core/src/main/java/org/elasticsearch/index/IndexService.java @@ -19,17 +19,23 @@ package org.elasticsearch.index; +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.Query; import org.apache.lucene.util.Accountable; import org.apache.lucene.util.IOUtils; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.env.ShardLock; -import org.elasticsearch.index.aliases.IndexAliasesService; import org.elasticsearch.index.analysis.AnalysisService; import org.elasticsearch.index.cache.IndexCache; import org.elasticsearch.index.cache.bitset.BitsetFilterCache; @@ -39,6 +45,7 @@ import org.elasticsearch.index.fielddata.IndexFieldDataService; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.IndexQueryParserService; +import org.elasticsearch.index.query.ParsedQuery; import org.elasticsearch.index.settings.IndexSettings; import org.elasticsearch.index.settings.IndexSettingsService; import org.elasticsearch.index.shard.IndexShard; @@ -49,8 +56,10 @@ import org.elasticsearch.index.shard.ShardPath; import org.elasticsearch.index.similarity.SimilarityService; import org.elasticsearch.index.store.IndexStore; import org.elasticsearch.index.store.Store; +import org.elasticsearch.indices.AliasFilterParsingException; import org.elasticsearch.indices.IndicesService; import org.elasticsearch.indices.InternalIndicesLifecycle; +import org.elasticsearch.indices.InvalidAliasNameException; import java.io.Closeable; import java.io.IOException; @@ -71,7 +80,6 @@ import static org.elasticsearch.common.collect.MapBuilder.newMapBuilder; */ public class IndexService extends AbstractIndexComponent implements IndexComponent, Iterable { - private final Settings indexSettings; private final InternalIndicesLifecycle indicesLifecycle; private final AnalysisService analysisService; private final IndexFieldDataService indexFieldData; @@ -84,9 +92,10 @@ public class IndexService extends AbstractIndexComponent implements IndexCompone private volatile Map shards = emptyMap(); private final AtomicBoolean closed = new AtomicBoolean(false); private final AtomicBoolean deleted = new AtomicBoolean(false); + private volatile IndexMetaData indexMetaData; @Inject - public IndexService(Index index, @IndexSettings Settings indexSettings, NodeEnvironment nodeEnv, + public IndexService(Index index, IndexMetaData indexMetaData, NodeEnvironment nodeEnv, AnalysisService analysisService, IndexSettingsService settingsService, IndexFieldDataService indexFieldData, @@ -94,8 +103,8 @@ public class IndexService extends AbstractIndexComponent implements IndexCompone IndicesService indicesServices, IndexServicesProvider indexServicesProvider, IndexStore indexStore) { - super(index, indexSettings); - this.indexSettings = indexSettings; + super(index, settingsService.indexSettings()); + assert indexMetaData != null; this.analysisService = analysisService; this.indexFieldData = indexFieldData; this.settingsService = settingsService; @@ -105,6 +114,7 @@ public class IndexService extends AbstractIndexComponent implements IndexCompone this.nodeEnv = nodeEnv; this.indexServicesProvider = indexServicesProvider; this.indexStore = indexStore; + this.indexMetaData = indexMetaData; indexFieldData.setListener(new FieldDataCacheListener(this)); bitSetFilterCache.setListener(new BitsetCacheListener(this)); } @@ -181,10 +191,6 @@ public class IndexService extends AbstractIndexComponent implements IndexCompone return indexServicesProvider.getSimilarityService(); } - public IndexAliasesService aliasesService() { - return indexServicesProvider.getIndexAliasesService(); - } - public synchronized void close(final String reason, boolean delete) { if (closed.compareAndSet(false, true)) { deleted.compareAndSet(false, delete); @@ -229,9 +235,7 @@ public class IndexService extends AbstractIndexComponent implements IndexCompone if (closed.get()) { throw new IllegalStateException("Can't create shard [" + index.name() + "][" + sShardId + "], closed"); } - if (indexSettings.get("index.translog.type") != null) { // TODO remove? - throw new IllegalStateException("a custom translog type is no longer supported. got [" + indexSettings.get("index.translog.type") + "]"); - } + final Settings indexSettings = settingsService.getSettings(); final ShardId shardId = new ShardId(index, sShardId); ShardLock lock = null; boolean success = false; @@ -327,6 +331,7 @@ public class IndexService extends AbstractIndexComponent implements IndexCompone private void closeShard(String reason, ShardId sId, IndexShard indexShard, Store store) { final int shardId = sId.id(); + final Settings indexSettings = settingsService.getSettings(); try { try { indicesLifecycle.beforeIndexShardClosed(sId, indexShard, indexSettings); @@ -358,6 +363,7 @@ public class IndexService extends AbstractIndexComponent implements IndexCompone private void onShardClose(ShardLock lock, boolean ownsShard) { if (deleted.get()) { // we remove that shards content if this index has been deleted + final Settings indexSettings = settingsService.getSettings(); try { if (ownsShard) { try { @@ -406,7 +412,7 @@ public class IndexService extends AbstractIndexComponent implements IndexCompone } public Settings getIndexSettings() { - return indexSettings; + return settingsService.getSettings(); } private static final class BitsetCacheListener implements BitsetFilterCache.Listener { @@ -466,4 +472,67 @@ public class IndexService extends AbstractIndexComponent implements IndexCompone } } } + /** + * Returns the filter associated with listed filtering aliases. + *

+ * The list of filtering aliases should be obtained by calling MetaData.filteringAliases. + * Returns null if no filtering is required.

+ */ + public Query aliasFilter(String... aliasNames) { + if (aliasNames == null || aliasNames.length == 0) { + return null; + } + final IndexQueryParserService indexQueryParser = queryParserService(); + final ImmutableOpenMap aliases = this.indexMetaData.getAliases(); + if (aliasNames.length == 1) { + AliasMetaData alias = aliases.get(aliasNames[0]); + if (alias == null) { + // This shouldn't happen unless alias disappeared after filteringAliases was called. + throw new InvalidAliasNameException(index, aliasNames[0], "Unknown alias name was passed to alias Filter"); + } + return parse(alias, indexQueryParser); + } else { + // we need to bench here a bit, to see maybe it makes sense to use OrFilter + BooleanQuery.Builder combined = new BooleanQuery.Builder(); + for (String aliasName : aliasNames) { + AliasMetaData alias = aliases.get(aliasName); + if (alias == null) { + // This shouldn't happen unless alias disappeared after filteringAliases was called. + throw new InvalidAliasNameException(indexQueryParser.index(), aliasNames[0], "Unknown alias name was passed to alias Filter"); + } + Query parsedFilter = parse(alias, indexQueryParser); + if (parsedFilter != null) { + combined.add(parsedFilter, BooleanClause.Occur.SHOULD); + } else { + // The filter might be null only if filter was removed after filteringAliases was called + return null; + } + } + return combined.build(); + } + } + + private Query parse(AliasMetaData alias, IndexQueryParserService indexQueryParser) { + if (alias.filter() == null) { + return null; + } + try { + byte[] filterSource = alias.filter().uncompressed(); + try (XContentParser parser = XContentFactory.xContent(filterSource).createParser(filterSource)) { + ParsedQuery parsedFilter = indexQueryParser.parseInnerFilter(parser); + return parsedFilter == null ? null : parsedFilter.query(); + } + } catch (IOException ex) { + throw new AliasFilterParsingException(indexQueryParser.index(), alias.getAlias(), "Invalid alias filter", ex); + } + } + + public IndexMetaData getMetaData() { + return indexMetaData; + } + + public void updateMetaData(IndexMetaData metadata) { + this.indexMetaData = metadata; + } + } diff --git a/core/src/main/java/org/elasticsearch/index/IndexServicesProvider.java b/core/src/main/java/org/elasticsearch/index/IndexServicesProvider.java index 0d5c3cb12c1..53b9f061abf 100644 --- a/core/src/main/java/org/elasticsearch/index/IndexServicesProvider.java +++ b/core/src/main/java/org/elasticsearch/index/IndexServicesProvider.java @@ -22,7 +22,6 @@ package org.elasticsearch.index; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.index.aliases.IndexAliasesService; import org.elasticsearch.index.cache.IndexCache; import org.elasticsearch.index.codec.CodecService; import org.elasticsearch.index.engine.EngineFactory; @@ -50,7 +49,6 @@ public final class IndexServicesProvider { private final MapperService mapperService; private final IndexQueryParserService queryParserService; private final IndexCache indexCache; - private final IndexAliasesService indexAliasesService; private final IndicesQueryCache indicesQueryCache; private final CodecService codecService; private final TermVectorsService termVectorsService; @@ -63,13 +61,12 @@ public final class IndexServicesProvider { private final IndexingMemoryController indexingMemoryController; @Inject - public IndexServicesProvider(IndicesLifecycle indicesLifecycle, ThreadPool threadPool, MapperService mapperService, IndexQueryParserService queryParserService, IndexCache indexCache, IndexAliasesService indexAliasesService, IndicesQueryCache indicesQueryCache, CodecService codecService, TermVectorsService termVectorsService, IndexFieldDataService indexFieldDataService, @Nullable IndicesWarmer warmer, SimilarityService similarityService, EngineFactory factory, BigArrays bigArrays, @Nullable IndexSearcherWrapper indexSearcherWrapper, IndexingMemoryController indexingMemoryController) { + public IndexServicesProvider(IndicesLifecycle indicesLifecycle, ThreadPool threadPool, MapperService mapperService, IndexQueryParserService queryParserService, IndexCache indexCache, IndicesQueryCache indicesQueryCache, CodecService codecService, TermVectorsService termVectorsService, IndexFieldDataService indexFieldDataService, @Nullable IndicesWarmer warmer, SimilarityService similarityService, EngineFactory factory, BigArrays bigArrays, @Nullable IndexSearcherWrapper indexSearcherWrapper, IndexingMemoryController indexingMemoryController) { this.indicesLifecycle = indicesLifecycle; this.threadPool = threadPool; this.mapperService = mapperService; this.queryParserService = queryParserService; this.indexCache = indexCache; - this.indexAliasesService = indexAliasesService; this.indicesQueryCache = indicesQueryCache; this.codecService = codecService; this.termVectorsService = termVectorsService; @@ -102,10 +99,6 @@ public final class IndexServicesProvider { return indexCache; } - public IndexAliasesService getIndexAliasesService() { - return indexAliasesService; - } - public IndicesQueryCache getIndicesQueryCache() { return indicesQueryCache; } diff --git a/core/src/main/java/org/elasticsearch/index/aliases/IndexAliasesService.java b/core/src/main/java/org/elasticsearch/index/aliases/IndexAliasesService.java deleted file mode 100644 index 2b519c2b843..00000000000 --- a/core/src/main/java/org/elasticsearch/index/aliases/IndexAliasesService.java +++ /dev/null @@ -1,127 +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.index.aliases; - -import org.apache.lucene.search.BooleanClause; -import org.apache.lucene.search.BooleanQuery; -import org.apache.lucene.search.Query; -import org.elasticsearch.cluster.metadata.AliasMetaData; -import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.collect.ImmutableOpenMap; -import org.elasticsearch.common.compress.CompressedXContent; -import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.AbstractIndexComponent; -import org.elasticsearch.index.Index; -import org.elasticsearch.index.query.IndexQueryParserService; -import org.elasticsearch.index.query.ParsedQuery; -import org.elasticsearch.index.settings.IndexSettings; -import org.elasticsearch.indices.AliasFilterParsingException; -import org.elasticsearch.indices.InvalidAliasNameException; - -import java.io.IOException; - -/** - * - */ -public class IndexAliasesService extends AbstractIndexComponent { - - private final IndexQueryParserService indexQueryParser; - private volatile ImmutableOpenMap aliases = ImmutableOpenMap.of(); - - @Inject - public IndexAliasesService(Index index, @IndexSettings Settings indexSettings, IndexQueryParserService indexQueryParser) { - super(index, indexSettings); - this.indexQueryParser = indexQueryParser; - } - - /** - * Returns the filter associated with listed filtering aliases. - *

- * The list of filtering aliases should be obtained by calling MetaData.filteringAliases. - * Returns null if no filtering is required.

- */ - public Query aliasFilter(String... aliasNames) { - if (aliasNames == null || aliasNames.length == 0) { - return null; - } - if (aliasNames.length == 1) { - AliasMetaData alias = this.aliases.get(aliasNames[0]); - if (alias == null) { - // This shouldn't happen unless alias disappeared after filteringAliases was called. - throw new InvalidAliasNameException(index, aliasNames[0], "Unknown alias name was passed to alias Filter"); - } - return parse(alias); - } else { - // we need to bench here a bit, to see maybe it makes sense to use OrFilter - BooleanQuery.Builder combined = new BooleanQuery.Builder(); - for (String aliasName : aliasNames) { - AliasMetaData alias = this.aliases.get(aliasName); - if (alias == null) { - // This shouldn't happen unless alias disappeared after filteringAliases was called. - throw new InvalidAliasNameException(index, aliasNames[0], "Unknown alias name was passed to alias Filter"); - } - Query parsedFilter = parse(alias); - if (parsedFilter != null) { - combined.add(parsedFilter, BooleanClause.Occur.SHOULD); - } else { - // The filter might be null only if filter was removed after filteringAliases was called - return null; - } - } - return combined.build(); - } - } - - public void setAliases(ImmutableOpenMap aliases) { - this.aliases = aliases; - } - - Query parse(AliasMetaData alias) { - if (alias.filter() == null) { - return null; - } - try { - byte[] filterSource = alias.filter().uncompressed(); - try (XContentParser parser = XContentFactory.xContent(filterSource).createParser(filterSource)) { - ParsedQuery parsedFilter = indexQueryParser.parseInnerFilter(parser); - return parsedFilter == null ? null : parsedFilter.query(); - } - } catch (IOException ex) { - throw new AliasFilterParsingException(index, alias.getAlias(), "Invalid alias filter", ex); - } - } - - // Used by tests: - void add(String alias, @Nullable CompressedXContent filter) { - AliasMetaData aliasMetaData = AliasMetaData.builder(alias).filter(filter).build(); - aliases = ImmutableOpenMap.builder(aliases).fPut(alias, aliasMetaData).build(); - } - - boolean hasAlias(String alias) { - return aliases.containsKey(alias); - } - - void remove(String alias) { - aliases = ImmutableOpenMap.builder(aliases).fRemove(alias).build(); - } -} diff --git a/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java b/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java index 86e53b41c9b..2ff8c37db3b 100644 --- a/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java +++ b/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java @@ -55,7 +55,6 @@ import org.elasticsearch.common.util.concurrent.FutureUtils; import org.elasticsearch.gateway.MetaDataStateFormat; import org.elasticsearch.index.IndexServicesProvider; import org.elasticsearch.index.VersionType; -import org.elasticsearch.index.aliases.IndexAliasesService; import org.elasticsearch.index.cache.IndexCache; import org.elasticsearch.index.cache.IndexCacheModule; import org.elasticsearch.index.cache.bitset.ShardBitsetFilterCache; diff --git a/core/src/main/java/org/elasticsearch/indices/IndicesService.java b/core/src/main/java/org/elasticsearch/indices/IndicesService.java index 95525767001..dedfb4b1ff9 100644 --- a/core/src/main/java/org/elasticsearch/indices/IndicesService.java +++ b/core/src/main/java/org/elasticsearch/indices/IndicesService.java @@ -305,11 +305,12 @@ public class IndicesService extends AbstractLifecycleComponent i return indexService; } - public synchronized IndexService createIndex(String sIndexName, @IndexSettings Settings settings, String localNodeId) { + public synchronized IndexService createIndex(IndexMetaData indexMetaData) { if (!lifecycle.started()) { - throw new IllegalStateException("Can't create an index [" + sIndexName + "], node is closed"); + throw new IllegalStateException("Can't create an index [" + indexMetaData.getIndex() + "], node is closed"); } - Index index = new Index(sIndexName); + final Settings settings = indexMetaData.getSettings(); + Index index = new Index(indexMetaData.getIndex()); if (indices.containsKey(index.name())) { throw new IndexAlreadyExistsException(index); } @@ -317,14 +318,14 @@ public class IndicesService extends AbstractLifecycleComponent i indicesLifecycle.beforeIndexCreated(index, settings); logger.debug("creating Index [{}], shards [{}]/[{}{}]", - sIndexName, + indexMetaData.getIndex(), settings.get(SETTING_NUMBER_OF_SHARDS), settings.get(SETTING_NUMBER_OF_REPLICAS), IndexMetaData.isIndexUsingShadowReplicas(settings) ? "s" : ""); Settings indexSettings = settingsBuilder() .put(this.settings) - .put(settings) + .put(indexMetaData.getSettings()) .build(); ModulesBuilder modules = new ModulesBuilder(); @@ -338,7 +339,7 @@ public class IndicesService extends AbstractLifecycleComponent i modules.add(new AnalysisModule(indexSettings, indicesAnalysisService)); modules.add(new SimilarityModule(index, indexSettings)); modules.add(new IndexCacheModule(indexSettings)); - modules.add(new IndexModule()); + modules.add(new IndexModule(indexMetaData)); pluginsService.processModules(modules); Injector indexInjector; @@ -355,7 +356,6 @@ public class IndicesService extends AbstractLifecycleComponent i indicesLifecycle.afterIndexCreated(indexService); indices = newMapBuilder(indices).put(index.name(), new IndexServiceInjectorPair(indexService, indexInjector)).immutableMap(); - return indexService; } diff --git a/core/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java b/core/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java index 6bce5bc4be1..1da88f7f488 100644 --- a/core/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java +++ b/core/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java @@ -44,7 +44,6 @@ import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.IndexShardAlreadyExistsException; -import org.elasticsearch.index.aliases.IndexAliasesService; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MapperService; @@ -300,7 +299,7 @@ public class IndicesClusterStateService extends AbstractLifecycleComponent x == null); module.indexSearcherWrapper = Wrapper.class; assertBinding(module, IndexSearcherWrapper.class, Wrapper.class); } public void testEngineFactoryBound() { - IndexModule module = new IndexModule(); + IndexModule module = new IndexModule(IndexMetaData.PROTO); assertBinding(module, EngineFactory.class, InternalEngineFactory.class); module.engineFactoryImpl = MockEngineFactory.class; assertBinding(module, EngineFactory.class, MockEngineFactory.class); } public void testOtherServiceBound() { - IndexModule module = new IndexModule(); + final IndexMetaData meta = IndexMetaData.builder(IndexMetaData.PROTO).index("foo").build(); + IndexModule module = new IndexModule(meta); assertBinding(module, IndexService.class, IndexService.class); assertBinding(module, IndexServicesProvider.class, IndexServicesProvider.class); + assertInstanceBinding(module, IndexMetaData.class, (x) -> x == meta); } public static final class Wrapper implements IndexSearcherWrapper { diff --git a/core/src/test/java/org/elasticsearch/index/IndexServiceTests.java b/core/src/test/java/org/elasticsearch/index/IndexServiceTests.java index 7d66382440a..412141cdc8d 100644 --- a/core/src/test/java/org/elasticsearch/index/IndexServiceTests.java +++ b/core/src/test/java/org/elasticsearch/index/IndexServiceTests.java @@ -19,14 +19,27 @@ package org.elasticsearch.index; +import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.indices.InvalidAliasNameException; +import org.elasticsearch.test.ESSingleNodeTestCase; import org.junit.Test; +import java.io.IOException; + +import static org.elasticsearch.index.query.QueryBuilders.termQuery; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.nullValue; + /** Unit test(s) for IndexService */ -public class IndexServiceTests extends ESTestCase { +public class IndexServiceTests extends ESSingleNodeTestCase { @Test public void testDetermineShadowEngineShouldBeUsed() { @@ -46,4 +59,92 @@ public class IndexServiceTests extends ESTestCase { assertFalse("no shadow replicas for primary shard with shadow settings", IndexService.useShadowEngine(true, shadowSettings)); assertTrue("shadow replicas for replica shards with shadow settings",IndexService.useShadowEngine(false, shadowSettings)); } + + public IndexService newIndexService() { + Settings settings = Settings.builder().put("name", "indexServiceTests").build(); + return createIndex("test", settings); + } + + + public static CompressedXContent filter(QueryBuilder filterBuilder) throws IOException { + XContentBuilder builder = XContentFactory.jsonBuilder(); + filterBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); + builder.close(); + return new CompressedXContent(builder.string()); + } + + @Test + public void testFilteringAliases() throws Exception { + + IndexService indexService = newIndexService(); + add(indexService, "cats", filter(termQuery("animal", "cat"))); + add(indexService, "dogs", filter(termQuery("animal", "dog"))); + add(indexService, "all", null); + + assertThat(indexService.getMetaData().getAliases().containsKey("cats"), equalTo(true)); + assertThat(indexService.getMetaData().getAliases().containsKey("dogs"), equalTo(true)); + assertThat(indexService.getMetaData().getAliases().containsKey("turtles"), equalTo(false)); + + assertThat(indexService.aliasFilter("cats").toString(), equalTo("animal:cat")); + assertThat(indexService.aliasFilter("cats", "dogs").toString(), equalTo("animal:cat animal:dog")); + + // Non-filtering alias should turn off all filters because filters are ORed + assertThat(indexService.aliasFilter("all"), nullValue()); + assertThat(indexService.aliasFilter("cats", "all"), nullValue()); + assertThat(indexService.aliasFilter("all", "cats"), nullValue()); + + add(indexService, "cats", filter(termQuery("animal", "feline"))); + add(indexService, "dogs", filter(termQuery("animal", "canine"))); + assertThat(indexService.aliasFilter("dogs", "cats").toString(), equalTo("animal:canine animal:feline")); + } + + @Test + public void testAliasFilters() throws Exception { + IndexService indexService = newIndexService(); + add(indexService, "cats", filter(termQuery("animal", "cat"))); + add(indexService, "dogs", filter(termQuery("animal", "dog"))); + + assertThat(indexService.aliasFilter(), nullValue()); + assertThat(indexService.aliasFilter("dogs").toString(), equalTo("animal:dog")); + assertThat(indexService.aliasFilter("dogs", "cats").toString(), equalTo("animal:dog animal:cat")); + + add(indexService, "cats", filter(termQuery("animal", "feline"))); + add(indexService, "dogs", filter(termQuery("animal", "canine"))); + + assertThat(indexService.aliasFilter("dogs", "cats").toString(), equalTo("animal:canine animal:feline")); + } + + @Test(expected = InvalidAliasNameException.class) + public void testRemovedAliasFilter() throws Exception { + IndexService indexService = newIndexService(); + + add(indexService, "cats", filter(termQuery("animal", "cat"))); + remove(indexService, "cats"); + indexService.aliasFilter("cats"); + } + + + @Test + public void testUnknownAliasFilter() throws Exception { + IndexService indexService = newIndexService(); + add(indexService, "cats", filter(termQuery("animal", "cat"))); + add(indexService, "dogs", filter(termQuery("animal", "dog"))); + + try { + indexService.aliasFilter("unknown"); + fail(); + } catch (InvalidAliasNameException e) { + // all is well + } + } + + private void remove(IndexService service, String alias) { + IndexMetaData build = IndexMetaData.builder(service.getMetaData()).removeAlias(alias).build(); + service.updateMetaData(build); + } + + private void add(IndexService service, String alias, @Nullable CompressedXContent filter) { + IndexMetaData build = IndexMetaData.builder(service.getMetaData()).putAlias(AliasMetaData.builder(alias).filter(filter).build()).build(); + service.updateMetaData(build); + } } diff --git a/core/src/test/java/org/elasticsearch/index/aliases/IndexAliasesServiceTests.java b/core/src/test/java/org/elasticsearch/index/aliases/IndexAliasesServiceTests.java deleted file mode 100644 index cb6a2425061..00000000000 --- a/core/src/test/java/org/elasticsearch/index/aliases/IndexAliasesServiceTests.java +++ /dev/null @@ -1,121 +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.index.aliases; - -import org.elasticsearch.common.compress.CompressedXContent; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.index.IndexService; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.indices.InvalidAliasNameException; -import org.elasticsearch.test.ESSingleNodeTestCase; -import org.junit.Test; - -import java.io.IOException; - -import static org.elasticsearch.index.query.QueryBuilders.termQuery; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.nullValue; - -/** - * - */ -public class IndexAliasesServiceTests extends ESSingleNodeTestCase { - - public IndexAliasesService newIndexAliasesService() { - Settings settings = Settings.builder().put("name", "IndexAliasesServiceTests").build(); - IndexService indexService = createIndex("test", settings); - return indexService.aliasesService(); - } - - public static CompressedXContent filter(QueryBuilder filterBuilder) throws IOException { - XContentBuilder builder = XContentFactory.jsonBuilder(); - filterBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); - builder.close(); - return new CompressedXContent(builder.string()); - } - - @Test - public void testFilteringAliases() throws Exception { - IndexAliasesService indexAliasesService = newIndexAliasesService(); - indexAliasesService.add("cats", filter(termQuery("animal", "cat"))); - indexAliasesService.add("dogs", filter(termQuery("animal", "dog"))); - indexAliasesService.add("all", null); - - assertThat(indexAliasesService.hasAlias("cats"), equalTo(true)); - assertThat(indexAliasesService.hasAlias("dogs"), equalTo(true)); - assertThat(indexAliasesService.hasAlias("turtles"), equalTo(false)); - - assertThat(indexAliasesService.aliasFilter("cats").toString(), equalTo("animal:cat")); - assertThat(indexAliasesService.aliasFilter("cats", "dogs").toString(), equalTo("animal:cat animal:dog")); - - // Non-filtering alias should turn off all filters because filters are ORed - assertThat(indexAliasesService.aliasFilter("all"), nullValue()); - assertThat(indexAliasesService.aliasFilter("cats", "all"), nullValue()); - assertThat(indexAliasesService.aliasFilter("all", "cats"), nullValue()); - - indexAliasesService.add("cats", filter(termQuery("animal", "feline"))); - indexAliasesService.add("dogs", filter(termQuery("animal", "canine"))); - assertThat(indexAliasesService.aliasFilter("dogs", "cats").toString(), equalTo("animal:canine animal:feline")); - } - - @Test - public void testAliasFilters() throws Exception { - IndexAliasesService indexAliasesService = newIndexAliasesService(); - indexAliasesService.add("cats", filter(termQuery("animal", "cat"))); - indexAliasesService.add("dogs", filter(termQuery("animal", "dog"))); - - assertThat(indexAliasesService.aliasFilter(), nullValue()); - assertThat(indexAliasesService.aliasFilter("dogs").toString(), equalTo("animal:dog")); - assertThat(indexAliasesService.aliasFilter("dogs", "cats").toString(), equalTo("animal:dog animal:cat")); - - indexAliasesService.add("cats", filter(termQuery("animal", "feline"))); - indexAliasesService.add("dogs", filter(termQuery("animal", "canine"))); - - assertThat(indexAliasesService.aliasFilter("dogs", "cats").toString(), equalTo("animal:canine animal:feline")); - } - - @Test(expected = InvalidAliasNameException.class) - public void testRemovedAliasFilter() throws Exception { - IndexAliasesService indexAliasesService = newIndexAliasesService(); - indexAliasesService.add("cats", filter(termQuery("animal", "cat"))); - indexAliasesService.remove("cats"); - indexAliasesService.aliasFilter("cats"); - } - - - @Test - public void testUnknownAliasFilter() throws Exception { - IndexAliasesService indexAliasesService = newIndexAliasesService(); - indexAliasesService.add("cats", filter(termQuery("animal", "cat"))); - indexAliasesService.add("dogs", filter(termQuery("animal", "dog"))); - - try { - indexAliasesService.aliasFilter("unknown"); - fail(); - } catch (InvalidAliasNameException e) { - // all is well - } - } - - -} diff --git a/core/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java b/core/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java index 7d4b10a41ca..b891219d636 100644 --- a/core/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java +++ b/core/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java @@ -926,7 +926,7 @@ public class IndexShardTests extends ESSingleNodeTestCase { } }; - IndexServicesProvider newProvider = new IndexServicesProvider(indexServices.getIndicesLifecycle(), indexServices.getThreadPool(), indexServices.getMapperService(), indexServices.getQueryParserService(), indexServices.getIndexCache(), indexServices.getIndexAliasesService(), indexServices.getIndicesQueryCache(), indexServices.getCodecService(), indexServices.getTermVectorsService(), indexServices.getIndexFieldDataService(), indexServices.getWarmer(), indexServices.getSimilarityService(), indexServices.getFactory(), indexServices.getBigArrays(), wrapper, indexServices.getIndexingMemoryController()); + IndexServicesProvider newProvider = new IndexServicesProvider(indexServices.getIndicesLifecycle(), indexServices.getThreadPool(), indexServices.getMapperService(), indexServices.getQueryParserService(), indexServices.getIndexCache(), indexServices.getIndicesQueryCache(), indexServices.getCodecService(), indexServices.getTermVectorsService(), indexServices.getIndexFieldDataService(), indexServices.getWarmer(), indexServices.getSimilarityService(), indexServices.getFactory(), indexServices.getBigArrays(), wrapper, indexServices.getIndexingMemoryController()); IndexShard newShard = new IndexShard(shard.shardId(), shard.indexSettings, shard.shardPath(), shard.store(), newProvider); ShardRoutingHelper.reinit(routing); diff --git a/core/src/test/java/org/elasticsearch/indices/IndicesLifecycleListenerIT.java b/core/src/test/java/org/elasticsearch/indices/IndicesLifecycleListenerIT.java index 1526df3a60a..bf3b84e4d46 100644 --- a/core/src/test/java/org/elasticsearch/indices/IndicesLifecycleListenerIT.java +++ b/core/src/test/java/org/elasticsearch/indices/IndicesLifecycleListenerIT.java @@ -198,7 +198,7 @@ public class IndicesLifecycleListenerIT extends ESIntegTestCase { assertAcked(client().admin().indices().prepareClose("test")); assertThat(stateChangeListenerNode1.afterCloseSettings.getAsInt(SETTING_NUMBER_OF_SHARDS, -1), equalTo(6)); - assertThat(stateChangeListenerNode1.afterCloseSettings.getAsInt(SETTING_NUMBER_OF_REPLICAS, -1), equalTo(0)); + assertThat(stateChangeListenerNode1.afterCloseSettings.getAsInt(SETTING_NUMBER_OF_REPLICAS, -1), equalTo(1)); assertShardStatesMatch(stateChangeListenerNode1, 6, CLOSED); assertShardStatesMatch(stateChangeListenerNode2, 6, CLOSED);