Skip to content

Commit f91c8d4

Browse files
committed
Remove (again) test uses of onModule (#21414)
This change was reverted after it caused random test failures. This was due to a copy/paste error in the original PR which caused the mock version of ClusterInfoService to be used whenever the mock *ZenPing* was used, and the real ClusterInfoService to be used when MockZenPing was not used.
1 parent c7ccde3 commit f91c8d4

File tree

12 files changed

+120
-86
lines changed

12 files changed

+120
-86
lines changed

core/src/main/java/org/elasticsearch/cluster/ClusterModule.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ public class ClusterModule extends AbstractModule {
8686
final Collection<AllocationDecider> allocationDeciders;
8787
final ShardsAllocator shardsAllocator;
8888

89-
// pkg private so tests can mock
90-
Class<? extends ClusterInfoService> clusterInfoServiceImpl = InternalClusterInfoService.class;
91-
9289
public ClusterModule(Settings settings, ClusterService clusterService, List<ClusterPlugin> clusterPlugins) {
9390
this.settings = settings;
9491
this.allocationDeciders = createAllocationDeciders(settings, clusterService.getClusterSettings(), clusterPlugins);
@@ -159,7 +156,6 @@ private static ShardsAllocator createShardsAllocator(Settings settings, ClusterS
159156

160157
@Override
161158
protected void configure() {
162-
bind(ClusterInfoService.class).to(clusterInfoServiceImpl).asEagerSingleton();
163159
bind(GatewayAllocator.class).asEagerSingleton();
164160
bind(AllocationService.class).asEagerSingleton();
165161
bind(ClusterService.class).toInstance(clusterService);

core/src/main/java/org/elasticsearch/cluster/InternalClusterInfoService.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@
1919

2020
package org.elasticsearch.cluster;
2121

22+
import java.util.List;
23+
import java.util.concurrent.CopyOnWriteArrayList;
24+
import java.util.concurrent.CountDownLatch;
25+
import java.util.concurrent.TimeUnit;
26+
2227
import org.apache.logging.log4j.Logger;
2328
import org.elasticsearch.action.ActionListener;
2429
import org.elasticsearch.action.LatchedActionListener;
2530
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
2631
import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequest;
2732
import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse;
28-
import org.elasticsearch.action.admin.cluster.node.stats.TransportNodesStatsAction;
2933
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest;
3034
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
3135
import org.elasticsearch.action.admin.indices.stats.ShardStats;
32-
import org.elasticsearch.action.admin.indices.stats.TransportIndicesStatsAction;
36+
import org.elasticsearch.client.node.NodeClient;
3337
import org.elasticsearch.cluster.block.ClusterBlockException;
3438
import org.elasticsearch.cluster.metadata.IndexMetaData;
3539
import org.elasticsearch.cluster.metadata.MetaData;
@@ -39,7 +43,6 @@
3943
import org.elasticsearch.cluster.service.ClusterService;
4044
import org.elasticsearch.common.collect.ImmutableOpenMap;
4145
import org.elasticsearch.common.component.AbstractComponent;
42-
import org.elasticsearch.common.inject.Inject;
4346
import org.elasticsearch.common.settings.ClusterSettings;
4447
import org.elasticsearch.common.settings.Setting;
4548
import org.elasticsearch.common.settings.Setting.Property;
@@ -50,11 +53,6 @@
5053
import org.elasticsearch.threadpool.ThreadPool;
5154
import org.elasticsearch.transport.ReceiveTimeoutTransportException;
5255

53-
import java.util.List;
54-
import java.util.concurrent.CopyOnWriteArrayList;
55-
import java.util.concurrent.CountDownLatch;
56-
import java.util.concurrent.TimeUnit;
57-
5856
/**
5957
* InternalClusterInfoService provides the ClusterInfoService interface,
6058
* routinely updated on a timer. The timer can be dynamically changed by
@@ -84,29 +82,24 @@ public class InternalClusterInfoService extends AbstractComponent implements Clu
8482
private volatile boolean isMaster = false;
8583
private volatile boolean enabled;
8684
private volatile TimeValue fetchTimeout;
87-
private final TransportNodesStatsAction transportNodesStatsAction;
88-
private final TransportIndicesStatsAction transportIndicesStatsAction;
8985
private final ClusterService clusterService;
9086
private final ThreadPool threadPool;
87+
private final NodeClient client;
9188
private final List<Listener> listeners = new CopyOnWriteArrayList<>();
9289

93-
@Inject
94-
public InternalClusterInfoService(Settings settings, ClusterSettings clusterSettings,
95-
TransportNodesStatsAction transportNodesStatsAction,
96-
TransportIndicesStatsAction transportIndicesStatsAction, ClusterService clusterService,
97-
ThreadPool threadPool) {
90+
public InternalClusterInfoService(Settings settings, ClusterService clusterService, ThreadPool threadPool, NodeClient client) {
9891
super(settings);
9992
this.leastAvailableSpaceUsages = ImmutableOpenMap.of();
10093
this.mostAvailableSpaceUsages = ImmutableOpenMap.of();
10194
this.shardRoutingToDataPath = ImmutableOpenMap.of();
10295
this.shardSizes = ImmutableOpenMap.of();
103-
this.transportNodesStatsAction = transportNodesStatsAction;
104-
this.transportIndicesStatsAction = transportIndicesStatsAction;
10596
this.clusterService = clusterService;
10697
this.threadPool = threadPool;
98+
this.client = client;
10799
this.updateFrequency = INTERNAL_CLUSTER_INFO_UPDATE_INTERVAL_SETTING.get(settings);
108100
this.fetchTimeout = INTERNAL_CLUSTER_INFO_TIMEOUT_SETTING.get(settings);
109101
this.enabled = DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING.get(settings);
102+
ClusterSettings clusterSettings = clusterService.getClusterSettings();
110103
clusterSettings.addSettingsUpdateConsumer(INTERNAL_CLUSTER_INFO_TIMEOUT_SETTING, this::setFetchTimeout);
111104
clusterSettings.addSettingsUpdateConsumer(INTERNAL_CLUSTER_INFO_UPDATE_INTERVAL_SETTING, this::setUpdateFrequency);
112105
clusterSettings.addSettingsUpdateConsumer(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING, this::setEnabled);
@@ -259,8 +252,7 @@ protected CountDownLatch updateNodeStats(final ActionListener<NodesStatsResponse
259252
nodesStatsRequest.clear();
260253
nodesStatsRequest.fs(true);
261254
nodesStatsRequest.timeout(fetchTimeout);
262-
263-
transportNodesStatsAction.execute(nodesStatsRequest, new LatchedActionListener<>(listener, latch));
255+
client.admin().cluster().nodesStats(nodesStatsRequest, new LatchedActionListener<>(listener, latch));
264256
return latch;
265257
}
266258

@@ -274,7 +266,7 @@ protected CountDownLatch updateIndicesStats(final ActionListener<IndicesStatsRes
274266
indicesStatsRequest.clear();
275267
indicesStatsRequest.store(true);
276268

277-
transportIndicesStatsAction.execute(indicesStatsRequest, new LatchedActionListener<>(listener, latch));
269+
client.admin().indices().stats(indicesStatsRequest, new LatchedActionListener<>(listener, latch));
278270
return latch;
279271
}
280272

core/src/main/java/org/elasticsearch/node/Node.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
import org.elasticsearch.action.update.UpdateHelper;
3636
import org.elasticsearch.client.Client;
3737
import org.elasticsearch.client.node.NodeClient;
38+
import org.elasticsearch.cluster.ClusterInfoService;
3839
import org.elasticsearch.cluster.ClusterModule;
3940
import org.elasticsearch.cluster.ClusterState;
4041
import org.elasticsearch.cluster.ClusterStateObserver;
42+
import org.elasticsearch.cluster.InternalClusterInfoService;
4143
import org.elasticsearch.cluster.MasterNodeChangePredicate;
4244
import org.elasticsearch.cluster.NodeConnectionsService;
4345
import org.elasticsearch.cluster.action.index.MappingUpdatedAction;
@@ -322,6 +324,7 @@ protected Node(final Environment environment, Collection<Class<? extends Plugin>
322324
for (final ExecutorBuilder<?> builder : threadPool.builders()) {
323325
additionalSettings.addAll(builder.getRegisteredSettings());
324326
}
327+
client = new NodeClient(settings, threadPool);
325328
final ResourceWatcherService resourceWatcherService = new ResourceWatcherService(settings, threadPool);
326329
final ScriptModule scriptModule = ScriptModule.create(settings, this.environment, resourceWatcherService,
327330
pluginsService.filterPlugins(ScriptPlugin.class));
@@ -342,6 +345,7 @@ protected Node(final Environment environment, Collection<Class<? extends Plugin>
342345
resourcesToClose.add(tribeService);
343346
final IngestService ingestService = new IngestService(settings, threadPool, this.environment,
344347
scriptModule.getScriptService(), analysisModule.getAnalysisRegistry(), pluginsService.filterPlugins(IngestPlugin.class));
348+
final ClusterInfoService clusterInfoService = newClusterInfoService(settings, clusterService, threadPool, client);
345349

346350
ModulesBuilder modules = new ModulesBuilder();
347351
// plugin modules must be added here, before others or we can get crazy injection errors...
@@ -377,7 +381,6 @@ protected Node(final Environment environment, Collection<Class<? extends Plugin>
377381
.flatMap(Function.identity()).collect(Collectors.toList());
378382
final NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(namedWriteables);
379383
final MetaStateService metaStateService = new MetaStateService(settings, nodeEnvironment);
380-
client = new NodeClient(settings, threadPool);
381384
final IndicesService indicesService = new IndicesService(settings, pluginsService, nodeEnvironment,
382385
settingsModule.getClusterSettings(), analysisModule.getAnalysisRegistry(), searchModule.getQueryParserRegistry(),
383386
clusterModule.getIndexNameExpressionResolver(), indicesModule.getMapperRegistry(), namedWriteableRegistry,
@@ -447,6 +450,7 @@ protected Node(final Environment environment, Collection<Class<? extends Plugin>
447450
b.bind(UpdateHelper.class).toInstance(new UpdateHelper(settings, scriptModule.getScriptService()));
448451
b.bind(MetaDataIndexUpgradeService.class).toInstance(new MetaDataIndexUpgradeService(settings,
449452
indicesModule.getMapperRegistry(), settingsModule.getIndexScopedSettings()));
453+
b.bind(ClusterInfoService.class).toInstance(clusterInfoService);
450454
b.bind(Discovery.class).toInstance(discoveryModule.getDiscovery());
451455
b.bind(ZenPing.class).toInstance(discoveryModule.getZenPing());
452456
{
@@ -916,4 +920,10 @@ protected ZenPing newZenPing(Settings settings, ThreadPool threadPool, Transport
916920
protected Node newTribeClientNode(Settings settings, Collection<Class<? extends Plugin>> classpathPlugins) {
917921
return new Node(new Environment(settings), classpathPlugins);
918922
}
923+
924+
/** Constructs a ClusterInfoService which may be mocked for tests. */
925+
protected ClusterInfoService newClusterInfoService(Settings settings, ClusterService clusterService,
926+
ThreadPool threadPool, NodeClient client) {
927+
return new InternalClusterInfoService(settings, clusterService, threadPool, client);
928+
}
919929
}

core/src/main/java/org/elasticsearch/plugins/Plugin.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.elasticsearch.action.ActionModule;
2727
import org.elasticsearch.client.Client;
28+
import org.elasticsearch.cluster.ClusterModule;
2829
import org.elasticsearch.cluster.metadata.MetaData;
2930
import org.elasticsearch.cluster.service.ClusterService;
3031
import org.elasticsearch.common.component.LifecycleComponent;
@@ -38,6 +39,7 @@
3839
import org.elasticsearch.discovery.DiscoveryModule;
3940
import org.elasticsearch.index.IndexModule;
4041
import org.elasticsearch.indices.analysis.AnalysisModule;
42+
import org.elasticsearch.repositories.RepositoriesModule;
4143
import org.elasticsearch.script.ScriptModule;
4244
import org.elasticsearch.script.ScriptService;
4345
import org.elasticsearch.search.SearchModule;
@@ -224,6 +226,24 @@ public final void onModule(SearchModule module) {}
224226
@Deprecated
225227
public final void onModule(NetworkModule module) {}
226228

229+
/**
230+
* Old-style snapshot/restore extension point. {@code @Deprecated} and {@code final} to act as a signpost for plugin authors upgrading
231+
* from 2.x.
232+
*
233+
* @deprecated implement {@link RepositoryPlugin} instead
234+
*/
235+
@Deprecated
236+
public final void onModule(RepositoriesModule module) {}
237+
238+
/**
239+
* Old-style cluster extension point. {@code @Deprecated} and {@code final} to act as a signpost for plugin authors upgrading
240+
* from 2.x.
241+
*
242+
* @deprecated implement {@link ClusterPlugin} instead
243+
*/
244+
@Deprecated
245+
public final void onModule(ClusterModule module) {}
246+
227247
/**
228248
* Old-style discovery extension point. {@code @Deprecated} and {@code final} to act as a signpost for plugin authors upgrading
229249
* from 2.x.

core/src/test/java/org/elasticsearch/indices/memory/breaker/RandomExceptionCircuitBreakerIT.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.indices.memory.breaker;
2121

2222
import org.apache.lucene.index.DirectoryReader;
23+
import org.apache.lucene.index.FilterDirectoryReader;
2324
import org.apache.lucene.index.LeafReader;
2425
import org.elasticsearch.ElasticsearchException;
2526
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
@@ -39,16 +40,20 @@
3940
import org.elasticsearch.indices.IndicesService;
4041
import org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache;
4142
import org.elasticsearch.plugins.Plugin;
43+
import org.elasticsearch.search.basic.SearchWithRandomExceptionsIT;
4244
import org.elasticsearch.search.sort.SortOrder;
4345
import org.elasticsearch.test.ESIntegTestCase;
4446
import org.elasticsearch.test.engine.MockEngineSupport;
4547
import org.elasticsearch.test.engine.ThrowingLeafReaderWrapper;
4648

4749
import java.io.IOException;
50+
import java.util.ArrayList;
4851
import java.util.Arrays;
4952
import java.util.Collection;
53+
import java.util.HashSet;
5054
import java.util.List;
5155
import java.util.Random;
56+
import java.util.Set;
5257
import java.util.concurrent.ExecutionException;
5358

5459
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAllSuccessful;
@@ -60,7 +65,14 @@
6065
public class RandomExceptionCircuitBreakerIT extends ESIntegTestCase {
6166
@Override
6267
protected Collection<Class<? extends Plugin>> nodePlugins() {
63-
return Arrays.asList(RandomExceptionDirectoryReaderWrapper.TestPlugin.class, MockEngineFactoryPlugin.class);
68+
return Arrays.asList(RandomExceptionDirectoryReaderWrapper.TestPlugin.class);
69+
}
70+
71+
@Override
72+
protected Collection<Class<? extends Plugin>> getMockPlugins() {
73+
Set<Class<? extends Plugin>> mocks = new HashSet<>(super.getMockPlugins());
74+
mocks.remove(MockEngineFactoryPlugin.class);
75+
return mocks;
6476
}
6577

6678
public void testBreakerWithRandomExceptions() throws IOException, InterruptedException, ExecutionException {
@@ -200,14 +212,19 @@ public static class RandomExceptionDirectoryReaderWrapper extends MockEngineSupp
200212
Setting.doubleSetting(EXCEPTION_TOP_LEVEL_RATIO_KEY, 0.1d, 0.0d, Property.IndexScope);
201213
public static final Setting<Double> EXCEPTION_LOW_LEVEL_RATIO_SETTING =
202214
Setting.doubleSetting(EXCEPTION_LOW_LEVEL_RATIO_KEY, 0.1d, 0.0d, Property.IndexScope);
203-
public static class TestPlugin extends Plugin {
215+
public static class TestPlugin extends MockEngineFactoryPlugin {
204216
@Override
205217
public List<Setting<?>> getSettings() {
206-
return Arrays.asList(EXCEPTION_TOP_LEVEL_RATIO_SETTING, EXCEPTION_LOW_LEVEL_RATIO_SETTING);
218+
List<Setting<?>> settings = new ArrayList<>();
219+
settings.addAll(super.getSettings());
220+
settings.add(EXCEPTION_TOP_LEVEL_RATIO_SETTING);
221+
settings.add(EXCEPTION_LOW_LEVEL_RATIO_SETTING);
222+
return settings;
207223
}
208224

209-
public void onModule(MockEngineFactoryPlugin.MockEngineReaderModule module) {
210-
module.setReaderClass(RandomExceptionDirectoryReaderWrapper.class);
225+
@Override
226+
protected Class<? extends FilterDirectoryReader> getReaderWrapperClass() {
227+
return RandomExceptionDirectoryReaderWrapper.class;
211228
}
212229
}
213230

core/src/test/java/org/elasticsearch/search/basic/SearchWithRandomExceptionsIT.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@
4444
import org.elasticsearch.test.engine.ThrowingLeafReaderWrapper;
4545

4646
import java.io.IOException;
47+
import java.util.ArrayList;
4748
import java.util.Arrays;
4849
import java.util.Collection;
50+
import java.util.HashSet;
4951
import java.util.List;
5052
import java.util.Random;
53+
import java.util.Set;
5154
import java.util.concurrent.ExecutionException;
5255

5356
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
@@ -56,7 +59,14 @@ public class SearchWithRandomExceptionsIT extends ESIntegTestCase {
5659

5760
@Override
5861
protected Collection<Class<? extends Plugin>> nodePlugins() {
59-
return Arrays.asList(RandomExceptionDirectoryReaderWrapper.TestPlugin.class, MockEngineFactoryPlugin.class);
62+
return Arrays.asList(RandomExceptionDirectoryReaderWrapper.TestPlugin.class);
63+
}
64+
65+
@Override
66+
protected Collection<Class<? extends Plugin>> getMockPlugins() {
67+
Set<Class<? extends Plugin>> mocks = new HashSet<>(super.getMockPlugins());
68+
mocks.remove(MockEngineFactoryPlugin.class);
69+
return mocks;
6070
}
6171

6272
public void testRandomExceptions() throws IOException, InterruptedException, ExecutionException {
@@ -153,17 +163,22 @@ public void testRandomExceptions() throws IOException, InterruptedException, Exe
153163

154164
public static class RandomExceptionDirectoryReaderWrapper extends MockEngineSupport.DirectoryReaderWrapper {
155165

156-
public static class TestPlugin extends Plugin {
166+
public static class TestPlugin extends MockEngineFactoryPlugin {
157167
public static final Setting<Double> EXCEPTION_TOP_LEVEL_RATIO_SETTING =
158168
Setting.doubleSetting(EXCEPTION_TOP_LEVEL_RATIO_KEY, 0.1d, 0.0d, Property.IndexScope);
159169
public static final Setting<Double> EXCEPTION_LOW_LEVEL_RATIO_SETTING =
160170
Setting.doubleSetting(EXCEPTION_LOW_LEVEL_RATIO_KEY, 0.1d, 0.0d, Property.IndexScope);
161171
@Override
162172
public List<Setting<?>> getSettings() {
163-
return Arrays.asList(EXCEPTION_TOP_LEVEL_RATIO_SETTING, EXCEPTION_LOW_LEVEL_RATIO_SETTING);
173+
List<Setting<?>> settings = new ArrayList<>();
174+
settings.addAll(super.getSettings());
175+
settings.add(EXCEPTION_TOP_LEVEL_RATIO_SETTING);
176+
settings.add(EXCEPTION_LOW_LEVEL_RATIO_SETTING);
177+
return settings;
164178
}
165-
public void onModule(MockEngineFactoryPlugin.MockEngineReaderModule module) {
166-
module.setReaderClass(RandomExceptionDirectoryReaderWrapper.class);
179+
@Override
180+
protected Class<? extends FilterDirectoryReader> getReaderWrapperClass() {
181+
return RandomExceptionDirectoryReaderWrapper.class;
167182
}
168183
}
169184

docs/reference/modules/scripting/native.asciidoc

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,11 @@ If you squashed the whole thing into one class it'd look like:
2020

2121
[source,java]
2222
--------------------------------------------------
23-
public class MyNativeScriptPlugin extends Plugin {
24-
@Override
25-
public String name() {
26-
return "my-native-script";
27-
}
23+
public class MyNativeScriptPlugin extends Plugin implements ScriptPlugin {
24+
2825
@Override
29-
public String description() {
30-
return "my native script that does something great";
31-
}
32-
public void onModule(ScriptModule scriptModule) {
33-
scriptModule.registerScript("my_script", MyNativeScriptFactory.class);
26+
public List<NativeScriptFactory> getNativeScripts() {
27+
return Collections.singletonList(new MyNativeScriptFactory());
3428
}
3529
3630
public static class MyNativeScriptFactory implements NativeScriptFactory {

plugins/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/IngestGeoIpPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class IngestGeoIpPlugin extends Plugin implements IngestPlugin, Closeable
4646
@Override
4747
public Map<String, Processor.Factory> getProcessors(Processor.Parameters parameters) {
4848
if (databaseReaders != null) {
49-
throw new IllegalStateException("called onModule twice for geoip plugin!!");
49+
throw new IllegalStateException("getProcessors called twice for geoip plugin!!");
5050
}
5151
Path geoIpConfigDirectory = parameters.env.configFile().resolve("ingest-geoip");
5252
try {

plugins/jvm-example/src/main/java/org/elasticsearch/plugin/example/JvmExamplePlugin.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ public Settings additionalSettings() {
6060
return Settings.EMPTY;
6161
}
6262

63-
public void onModule(RepositoriesModule repositoriesModule) {
64-
}
65-
6663
/**
6764
* Module declaring some example configuration and a _cat action that uses
6865
* it.

0 commit comments

Comments
 (0)