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.
This commit is contained in:
Simon Willnauer 2015-10-07 20:34:44 +02:00
parent 1205c66cfb
commit 1cb30c3a49
18 changed files with 224 additions and 301 deletions

View File

@ -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());

View File

@ -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);
}

View File

@ -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<String> 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)) {

View File

@ -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<? extends EngineFactory> engineFactoryImpl = InternalEngineFactory.class;
Class<? extends IndexSearcherWrapper> 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();
}

View File

@ -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<IndexShard> {
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<Integer, IndexShard> 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.
* <p>
* The list of filtering aliases should be obtained by calling MetaData.filteringAliases.
* Returns <tt>null</tt> if no filtering is required.</p>
*/
public Query aliasFilter(String... aliasNames) {
if (aliasNames == null || aliasNames.length == 0) {
return null;
}
final IndexQueryParserService indexQueryParser = queryParserService();
final ImmutableOpenMap<String, AliasMetaData> 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;
}
}

View File

@ -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;
}

View File

@ -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<String, AliasMetaData> 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.
* <p>
* The list of filtering aliases should be obtained by calling MetaData.filteringAliases.
* Returns <tt>null</tt> if no filtering is required.</p>
*/
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<String, AliasMetaData> 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();
}
}

View File

@ -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;

View File

@ -305,11 +305,12 @@ public class IndicesService extends AbstractLifecycleComponent<IndicesService> 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<IndicesService> 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<IndicesService> 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<IndicesService> i
indicesLifecycle.afterIndexCreated(indexService);
indices = newMapBuilder(indices).put(index.name(), new IndexServiceInjectorPair(indexService, indexInjector)).immutableMap();
return indexService;
}

View File

@ -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<Indic
logger.debug("[{}] creating index", indexMetaData.index());
}
try {
indicesService.createIndex(indexMetaData.index(), indexMetaData.settings(), event.state().nodes().localNode().id());
indicesService.createIndex(indexMetaData);
} catch (Throwable e) {
sendFailShard(shard, indexMetaData.getIndexUUID(), "failed to create index", e);
}
@ -458,8 +457,7 @@ public class IndicesClusterStateService extends AbstractLifecycleComponent<Indic
// we only create / update here
continue;
}
IndexAliasesService indexAliasesService = indexService.aliasesService();
indexAliasesService.setAliases(indexMetaData.getAliases());
indexService.updateMetaData(indexMetaData);
}
}
}

View File

@ -190,7 +190,7 @@ public class PercolatorService extends AbstractComponent {
indexShard.shardId().index().name(),
request.indices()
);
Query aliasFilter = percolateIndexService.aliasesService().aliasFilter(filteringAliases);
Query aliasFilter = percolateIndexService.aliasFilter(filteringAliases);
SearchShardTarget searchShardTarget = new SearchShardTarget(clusterService.localNode().id(), request.shardId().getIndex(), request.shardId().id());
final PercolateContext context = new PercolateContext(

View File

@ -193,7 +193,7 @@ public class DefaultSearchContext extends SearchContext {
}
// initialize the filtering alias based on the provided filters
aliasFilter = indexService.aliasesService().aliasFilter(request.filteringAliases());
aliasFilter = indexService.aliasFilter(request.filteringAliases());
if (query() == null) {
parsedQuery(ParsedQuery.parsedMatchAllQuery());

View File

@ -166,8 +166,10 @@ public abstract class ModuleTestCase extends ESTestCase {
}
} else if (element instanceof ProviderInstanceBinding) {
ProviderInstanceBinding binding = (ProviderInstanceBinding) element;
assertTrue(tester.test(to.cast(binding.getProviderInstance().get())));
return;
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
assertTrue(tester.test(to.cast(binding.getProviderInstance().get())));
return;
}
}
}
StringBuilder s = new StringBuilder();

View File

@ -20,7 +20,9 @@ package org.elasticsearch.index;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.search.IndexSearcher;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.inject.ModuleTestCase;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.engine.EngineConfig;
import org.elasticsearch.index.engine.EngineException;
import org.elasticsearch.index.engine.EngineFactory;
@ -31,23 +33,25 @@ import org.elasticsearch.test.engine.MockEngineFactory;
public class IndexModuleTests extends ModuleTestCase {
public void testWrapperIsBound() {
IndexModule module = new IndexModule();
IndexModule module = new IndexModule(IndexMetaData.PROTO);
assertInstanceBinding(module, IndexSearcherWrapper.class,(x) -> 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 {

View File

@ -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);
}
}

View File

@ -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
}
}
}

View File

@ -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);

View File

@ -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);