Skip to content

Commit

Permalink
Remove engine factory provider
Browse files Browse the repository at this point in the history
This commit removes a level of indirection by removing the engine
factory provider abstraction.
  • Loading branch information
jasontedor committed Oct 1, 2017
1 parent ce7e52e commit 6e2f3ec
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 51 deletions.
10 changes: 5 additions & 5 deletions core/src/main/java/org/elasticsearch/indices/IndicesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public class IndicesService extends AbstractLifecycleComponent
private final IndicesRequestCache indicesRequestCache;
private final IndicesQueryCache indicesQueryCache;
private final MetaStateService metaStateService;
private final Collection<Tuple<EnginePlugin, EnginePlugin.EngineFactoryProvider>> engineFactoryProviders;
private final Collection<EnginePlugin> enginePlugins;

@Override
protected void doStart() {
Expand All @@ -190,7 +190,7 @@ public IndicesService(Settings settings, PluginsService pluginsService, NodeEnvi
MapperRegistry mapperRegistry, NamedWriteableRegistry namedWriteableRegistry, ThreadPool threadPool,
IndexScopedSettings indexScopedSettings, CircuitBreakerService circuitBreakerService, BigArrays bigArrays,
ScriptService scriptService, Client client, MetaStateService metaStateService,
Collection<Tuple<EnginePlugin, EnginePlugin.EngineFactoryProvider>> engineFactoryProviders) {
Collection<EnginePlugin> enginePlugins) {
super(settings);
this.threadPool = threadPool;
this.pluginsService = pluginsService;
Expand Down Expand Up @@ -221,7 +221,7 @@ public void onRemoval(ShardId shardId, String fieldName, boolean wasEvicted, lon
this.cleanInterval = INDICES_CACHE_CLEAN_INTERVAL_SETTING.get(settings);
this.cacheCleaner = new CacheCleaner(indicesFieldDataCache, indicesRequestCache, logger, threadPool, this.cleanInterval);
this.metaStateService = metaStateService;
this.engineFactoryProviders = engineFactoryProviders;
this.enginePlugins = enginePlugins;
}

@Override
Expand Down Expand Up @@ -474,9 +474,9 @@ private synchronized IndexService createIndexService(final String reason,

private EngineFactory getEngineFactory(final IndexSettings idxSettings) {
final List<Tuple<EnginePlugin, Optional<EngineFactory>>> engineFactories =
engineFactoryProviders
enginePlugins
.stream()
.map(p -> Tuple.tuple(p.v1(), p.v2().apply(idxSettings)))
.map(p -> Tuple.tuple(p, p.getMaybeEngineFactory(idxSettings)))
.filter(t -> Objects.requireNonNull(t.v2()).isPresent())
.collect(Collectors.toList());
if (engineFactories.isEmpty()) {
Expand Down
10 changes: 2 additions & 8 deletions core/src/main/java/org/elasticsearch/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import org.elasticsearch.cluster.routing.allocation.DiskThresholdMonitor;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.StopWatch;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.component.Lifecycle;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Binder;
Expand Down Expand Up @@ -389,18 +388,13 @@ protected Node(final Environment environment, Collection<Class<? extends Plugin>
final MetaStateService metaStateService = new MetaStateService(settings, nodeEnvironment, xContentRegistry);

// collect engine factory providers per plugin
final Collection<Tuple<EnginePlugin, EnginePlugin.EngineFactoryProvider>> engineFactoryProviders =
pluginsService
.filterPlugins(EnginePlugin.class)
.stream()
.map(p -> Tuple.tuple(p, p.getEngineFactoryProvider()))
.collect(Collectors.toList());
final Collection<EnginePlugin> enginePlugins = pluginsService.filterPlugins(EnginePlugin.class);

final IndicesService indicesService =
new IndicesService(settings, pluginsService, nodeEnvironment, xContentRegistry, analysisModule.getAnalysisRegistry(),
clusterModule.getIndexNameExpressionResolver(), indicesModule.getMapperRegistry(), namedWriteableRegistry,
threadPool, settingsModule.getIndexScopedSettings(), circuitBreakerService, bigArrays,
scriptModule.getScriptService(), client, metaStateService, engineFactoryProviders);
scriptModule.getScriptService(), client, metaStateService, enginePlugins);

Collection<Object> pluginComponents = pluginsService.filterPlugins(Plugin.class).stream()
.flatMap(p -> p.createComponents(client, clusterService, threadPool, resourceWatcherService,
Expand Down
27 changes: 9 additions & 18 deletions core/src/main/java/org/elasticsearch/plugins/EnginePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,20 @@
import org.elasticsearch.index.engine.EngineFactory;

import java.util.Optional;
import java.util.function.Function;

/**
* A plugin that provides alternative engine implementations.
*/
public interface EnginePlugin {

/**
* An engine factory provider. When an index is created this method is invoked for each engine plugin. Engine plugins can inspect the
* index settings to determine whether or not to provide an engine factory for the given index. A plugin that is not overriding the
* default engine should return {@link Optional#empty()}. If multiple plugins return an engine factory for a given index the index will
* not be created and an {@link IllegalStateException} will be thrown during index creation.
*/
@FunctionalInterface
interface EngineFactoryProvider extends Function<IndexSettings, Optional<EngineFactory>> {

}

/**
* The engine factory provider for this engine plugin.
*
* @return the engine factory provider
*/
EngineFactoryProvider getEngineFactoryProvider();
/**
* When an index is created this method is invoked for each engine plugin. Engine plugins can inspect the index settings to determine
* whether or not to provide an engine factory for the given index. A plugin that is not overriding the default engine should return
* {@link Optional#empty()}. If multiple plugins return an engine factory for a given index the index will not be created and an
* {@link IllegalStateException} will be thrown during index creation.
*
* @return an optional engine factory
*/
Optional<EngineFactory> getMaybeEngineFactory(final IndexSettings indexSettings);

}
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,12 @@ public List<Setting<?>> getSettings() {
}

@Override
public EngineFactoryProvider getEngineFactoryProvider() {
return indexSettings -> {
if (FOO_INDEX_SETTING.get(indexSettings.getSettings())) {
return Optional.of(new FooEngineFactory());
} else {
return Optional.empty();
}
};
public Optional<EngineFactory> getMaybeEngineFactory(final IndexSettings indexSettings) {
if (FOO_INDEX_SETTING.get(indexSettings.getSettings())) {
return Optional.of(new FooEngineFactory());
} else {
return Optional.empty();
}
}

}
Expand All @@ -158,14 +156,12 @@ public List<Setting<?>> getSettings() {
}

@Override
public EngineFactoryProvider getEngineFactoryProvider() {
return indexSettings -> {
if (BAR_INDEX_SETTING.get(indexSettings.getSettings())) {
return Optional.of(new BarEngineFactory());
} else {
return Optional.empty();
}
};
public Optional<EngineFactory> getMaybeEngineFactory(final IndexSettings indexSettings) {
if (BAR_INDEX_SETTING.get(indexSettings.getSettings())) {
return Optional.of(new BarEngineFactory());
} else {
return Optional.empty();
}
}

}
Expand Down Expand Up @@ -508,7 +504,7 @@ public void testStatsByShardDoesNotDieFromExpectedExceptions() {
assertThat("unexpected shard stats", indexStats.get(index), equalTo(shardStats));
}

public void testEngineFactoryProvider() throws IOException {
public void testGetEngineFactory() throws IOException {
final IndicesService indicesService = getIndicesService();

final Boolean[] values = new Boolean[] { true, false, null };
Expand Down Expand Up @@ -536,7 +532,7 @@ public void testEngineFactoryProvider() throws IOException {
}
}

public void testConflictingEngineFactoryProviders() throws IOException {
public void testConflictingEngineFactories() throws IOException {
final String indexName = "foobar";
final Index index = new Index(indexName, UUIDs.randomBase64UUID());
final Settings settings = Settings.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.lucene.index.AssertingDirectoryReader;
import org.apache.lucene.index.FilterDirectoryReader;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.index.engine.EngineFactory;
import org.elasticsearch.plugins.EnginePlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.engine.MockEngineFactory;
Expand All @@ -43,8 +44,8 @@ public List<Setting<?>> getSettings() {
}

@Override
public EngineFactoryProvider getEngineFactoryProvider() {
return indexSettings -> Optional.of(new MockEngineFactory(getReaderWrapperClass()));
public Optional<EngineFactory> getMaybeEngineFactory(final IndexSettings indexSettings) {
return Optional.of(new MockEngineFactory(getReaderWrapperClass()));
}

protected Class<? extends FilterDirectoryReader> getReaderWrapperClass() {
Expand Down

0 comments on commit 6e2f3ec

Please sign in to comment.