Skip to content

Commit 57a24ad

Browse files
committed
Use file-based discovery not MockUncasedHostsProvider
Today we use a special unicast hosts provider, the `MockUncasedHostsProvider`, in many integration tests, to deal with the dynamic nature of the allocation of ports to nodes. However elastic#33241 allows us to use file-based discovery to achieve the same goal, so the special test-only `MockUncasedHostsProvider` is no longer required. This change removes `MockUncasedHostProvider` and replaces it with file-based discovery in tests based on `EsIntegTestCase`.
1 parent 902d20c commit 57a24ad

File tree

11 files changed

+220
-171
lines changed

11 files changed

+220
-171
lines changed

server/src/main/java/org/elasticsearch/discovery/DiscoveryModule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public DiscoveryModule(Settings settings, ThreadPool threadPool, TransportServic
131131
if (discoverySupplier == null) {
132132
throw new IllegalArgumentException("Unknown discovery type [" + discoveryType + "]");
133133
}
134-
Loggers.getLogger(getClass(), settings).info("using discovery type [{}]", discoveryType);
134+
Loggers.getLogger(getClass(), settings).info("using discovery type [{}] and host providers {}", discoveryType, hostsProviderNames);
135135
discovery = Objects.requireNonNull(discoverySupplier.get());
136136
}
137137

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

+6
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ public Node start() throws NodeValidationException {
695695
assert localNodeFactory.getNode() != null;
696696
assert transportService.getLocalNode().equals(localNodeFactory.getNode())
697697
: "transportService has a different local node than the factory provided";
698+
onTransportServiceStarted();
699+
698700
final MetaData onDiskMetadata;
699701
try {
700702
// we load the global state here (the persistent part of the cluster state stored on disk) to
@@ -771,6 +773,10 @@ public void onTimeout(TimeValue timeout) {
771773
return this;
772774
}
773775

776+
// For notifying tests that discovery can be configured
777+
protected void onTransportServiceStarted() {
778+
}
779+
774780
private Node stop() {
775781
if (!lifecycle.moveToStopped()) {
776782
return this;

server/src/test/java/org/elasticsearch/cluster/ClusterInfoServiceIT.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public void blockActions(String... actions) {
113113
@Override
114114
protected Settings nodeSettings(int nodeOrdinal) {
115115
return Settings.builder()
116+
.put(super.nodeSettings(nodeOrdinal))
116117
// manual collection or upon cluster forming.
117118
.put(NodeEnvironment.MAX_LOCAL_STORAGE_NODES_SETTING.getKey(), 2)
118119
.put(InternalClusterInfoService.INTERNAL_CLUSTER_INFO_TIMEOUT_SETTING.getKey(), "1s")
@@ -121,8 +122,7 @@ protected Settings nodeSettings(int nodeOrdinal) {
121122

122123
@Override
123124
protected Collection<Class<? extends Plugin>> nodePlugins() {
124-
return Arrays.asList(TestPlugin.class,
125-
MockTransportService.TestPlugin.class);
125+
return Arrays.asList(TestPlugin.class, MockTransportService.TestPlugin.class);
126126
}
127127

128128
public void testClusterInfoServiceCollectsInformation() throws Exception {
@@ -172,7 +172,7 @@ public void testClusterInfoServiceCollectsInformation() throws Exception {
172172
}
173173
}
174174

175-
public void testClusterInfoServiceInformationClearOnError() throws InterruptedException, ExecutionException {
175+
public void testClusterInfoServiceInformationClearOnError() {
176176
internalCluster().startNodes(2,
177177
// manually control publishing
178178
Settings.builder().put(InternalClusterInfoService.INTERNAL_CLUSTER_INFO_UPDATE_INTERVAL_SETTING.getKey(), "60m").build());
@@ -186,7 +186,6 @@ public void testClusterInfoServiceInformationClearOnError() throws InterruptedEx
186186
assertThat("some usages are populated", info.getNodeLeastAvailableDiskUsages().size(), Matchers.equalTo(2));
187187
assertThat("some shard sizes are populated", info.shardSizes.size(), greaterThan(0));
188188

189-
190189
MockTransportService mockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, internalTestCluster.getMasterName());
191190

192191
final AtomicBoolean timeout = new AtomicBoolean(false);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.discovery.zen;
21+
22+
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoRequest;
23+
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
24+
import org.elasticsearch.common.settings.Settings;
25+
import org.elasticsearch.common.settings.Settings.Builder;
26+
import org.elasticsearch.common.transport.TransportAddress;
27+
import org.elasticsearch.test.ESIntegTestCase;
28+
import org.junit.Before;
29+
30+
import java.util.function.Consumer;
31+
32+
import static org.elasticsearch.discovery.DiscoveryModule.DISCOVERY_HOSTS_PROVIDER_SETTING;
33+
import static org.elasticsearch.discovery.zen.SettingsBasedHostsProvider.DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING;
34+
import static org.elasticsearch.discovery.zen.SettingsBasedHostsProvider.LIMIT_LOCAL_PORTS_COUNT;
35+
import static org.elasticsearch.transport.TcpTransport.PORT;
36+
37+
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0)
38+
public class SettingsBasedHostProviderIT extends ESIntegTestCase {
39+
40+
private Consumer<Builder> configureDiscovery;
41+
42+
@Before
43+
public void setDefaultDiscoveryConfiguration() {
44+
configureDiscovery = b -> {
45+
};
46+
}
47+
48+
@Override
49+
protected Settings nodeSettings(int nodeOrdinal) {
50+
Settings.Builder builder = Settings.builder().put(super.nodeSettings(nodeOrdinal));
51+
52+
// super.nodeSettings enables file-based discovery, but here we disable it again so we can test the static list:
53+
if (randomBoolean()) {
54+
builder.putList(DISCOVERY_HOSTS_PROVIDER_SETTING.getKey());
55+
} else {
56+
builder.remove(DISCOVERY_HOSTS_PROVIDER_SETTING.getKey());
57+
}
58+
59+
builder.remove(DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING.getKey());
60+
61+
configureDiscovery.accept(builder);
62+
return builder.build();
63+
}
64+
65+
public void testClusterFormsWithSingleSeedHostInSettings() {
66+
final String seedNodeName = internalCluster().startNode();
67+
final NodesInfoResponse nodesInfoResponse
68+
= client(seedNodeName).admin().cluster().nodesInfo(new NodesInfoRequest("_local")).actionGet();
69+
final String seedNodeAddress = nodesInfoResponse.getNodes().get(0).getTransport().getAddress().publishAddress().toString();
70+
configureDiscovery = b -> b.putList(DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING.getKey(), seedNodeAddress);
71+
logger.info("--> using seed node address {}", seedNodeAddress);
72+
73+
int extraNodes = randomIntBetween(1, 5);
74+
internalCluster().startNodes(extraNodes);
75+
76+
ensureStableCluster(extraNodes + 1);
77+
}
78+
79+
public void testClusterFormsByScanningPorts() {
80+
final String seedNodeName = internalCluster().startNode();
81+
final NodesInfoResponse nodesInfoResponse
82+
= client(seedNodeName).admin().cluster().nodesInfo(new NodesInfoRequest("_local")).actionGet();
83+
final int seedNodePort = nodesInfoResponse.getNodes().get(0).getTransport().getAddress().publishAddress().getPort();
84+
final int minPort = randomIntBetween(seedNodePort - LIMIT_LOCAL_PORTS_COUNT + 1, seedNodePort - 1);
85+
final String portSpec = minPort + "-" + seedNodePort;
86+
87+
logger.info("--> using port specification [{}]", portSpec);
88+
89+
configureDiscovery = b -> b.put(PORT.getKey(), portSpec);
90+
91+
internalCluster().startNode();
92+
ensureStableCluster(2);
93+
}
94+
}

server/src/test/java/org/elasticsearch/search/SearchCancellationIT.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
6969
protected Settings nodeSettings(int nodeOrdinal) {
7070
boolean lowLevelCancellation = randomBoolean();
7171
logger.info("Using lowLevelCancellation: {}", lowLevelCancellation);
72-
return Settings.builder().put(SearchService.LOW_LEVEL_CANCELLATION_SETTING.getKey(), lowLevelCancellation).build();
72+
return Settings.builder()
73+
.put(super.nodeSettings(nodeOrdinal))
74+
.put(SearchService.LOW_LEVEL_CANCELLATION_SETTING.getKey(), lowLevelCancellation)
75+
.build();
7376
}
7477

7578
private void indexTestData() {

test/framework/src/main/java/org/elasticsearch/node/MockNode.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
public class MockNode extends Node {
6868

6969
private final Collection<Class<? extends Plugin>> classpathPlugins;
70+
private final Runnable onTransportServiceStarted;
7071

7172
public MockNode(final Settings settings, final Collection<Class<? extends Plugin>> classpathPlugins) {
7273
this(settings, classpathPlugins, true);
@@ -76,26 +77,30 @@ public MockNode(
7677
final Settings settings,
7778
final Collection<Class<? extends Plugin>> classpathPlugins,
7879
final boolean forbidPrivateIndexSettings) {
79-
this(settings, classpathPlugins, null, forbidPrivateIndexSettings);
80+
this(settings, classpathPlugins, null, forbidPrivateIndexSettings, () -> {});
8081
}
8182

8283
public MockNode(
8384
final Settings settings,
8485
final Collection<Class<? extends Plugin>> classpathPlugins,
8586
final Path configPath,
86-
final boolean forbidPrivateIndexSettings) {
87+
final boolean forbidPrivateIndexSettings,
88+
final Runnable onTransportServiceStarted) {
8789
this(
8890
InternalSettingsPreparer.prepareEnvironment(settings, Collections.emptyMap(), configPath),
8991
classpathPlugins,
90-
forbidPrivateIndexSettings);
92+
forbidPrivateIndexSettings,
93+
onTransportServiceStarted);
9194
}
9295

9396
private MockNode(
9497
final Environment environment,
9598
final Collection<Class<? extends Plugin>> classpathPlugins,
96-
final boolean forbidPrivateIndexSettings) {
99+
final boolean forbidPrivateIndexSettings,
100+
final Runnable onTransportServiceStarted) {
97101
super(environment, classpathPlugins, forbidPrivateIndexSettings);
98102
this.classpathPlugins = classpathPlugins;
103+
this.onTransportServiceStarted = onTransportServiceStarted;
99104
}
100105

101106
/**
@@ -179,4 +184,9 @@ protected HttpServerTransport newHttpTransport(NetworkModule networkModule) {
179184
protected void registerDerivedNodeNameWithLogger(String nodeName) {
180185
// Nothing to do because test uses the thread name
181186
}
187+
188+
@Override
189+
protected void onTransportServiceStarted() {
190+
onTransportServiceStarted.run();
191+
}
182192
}

test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@
204204
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
205205
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
206206
import static org.elasticsearch.common.util.CollectionUtils.eagerPartition;
207+
import static org.elasticsearch.discovery.DiscoveryModule.DISCOVERY_HOSTS_PROVIDER_SETTING;
208+
import static org.elasticsearch.discovery.zen.SettingsBasedHostsProvider.DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING;
207209
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
208210
import static org.elasticsearch.test.XContentTestUtils.convertToMap;
209211
import static org.elasticsearch.test.XContentTestUtils.differenceBetweenMapsIgnoringArrayOrder;
@@ -1806,7 +1808,9 @@ protected Settings nodeSettings(int nodeOrdinal) {
18061808
// wait short time for other active shards before actually deleting, default 30s not needed in tests
18071809
.put(IndicesStore.INDICES_STORE_DELETE_SHARD_TIMEOUT.getKey(), new TimeValue(1, TimeUnit.SECONDS))
18081810
// randomly enable low-level search cancellation to make sure it does not alter results
1809-
.put(SearchService.LOW_LEVEL_CANCELLATION_SETTING.getKey(), randomBoolean());
1811+
.put(SearchService.LOW_LEVEL_CANCELLATION_SETTING.getKey(), randomBoolean())
1812+
.putList(DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING.getKey()) // empty list disables a port scan for other nodes
1813+
.putList(DISCOVERY_HOSTS_PROVIDER_SETTING.getKey(), "file");
18101814
if (rarely()) {
18111815
// Sometimes adjust the minimum search thread pool size, causing
18121816
// QueueResizingEsThreadPoolExecutor to be used instead of a regular
@@ -1919,7 +1923,7 @@ protected NodeConfigurationSource getNodeConfigSource() {
19191923
networkSettings.put(NetworkModule.TRANSPORT_TYPE_KEY, getTestTransportType());
19201924
}
19211925

1922-
NodeConfigurationSource nodeConfigurationSource = new NodeConfigurationSource() {
1926+
return new NodeConfigurationSource() {
19231927
@Override
19241928
public Settings nodeSettings(int nodeOrdinal) {
19251929
return Settings.builder()
@@ -1953,7 +1957,6 @@ public Collection<Class<? extends Plugin>> transportClientPlugins() {
19531957
return Collections.unmodifiableCollection(plugins);
19541958
}
19551959
};
1956-
return nodeConfigurationSource;
19571960
}
19581961

19591962
/**
@@ -2027,7 +2030,7 @@ protected Collection<Class<? extends Plugin>> getMockPlugins() {
20272030
public static final class TestSeedPlugin extends Plugin {
20282031
@Override
20292032
public List<Setting<?>> getSettings() {
2030-
return Arrays.asList(INDEX_TEST_SEED_SETTING);
2033+
return Collections.singletonList(INDEX_TEST_SEED_SETTING);
20312034
}
20322035
}
20332036

test/framework/src/main/java/org/elasticsearch/test/ESSingleNodeTestCase.java

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import java.util.Collection;
6363
import java.util.Collections;
6464

65+
import static org.elasticsearch.discovery.zen.SettingsBasedHostsProvider.DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING;
6566
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
6667
import static org.hamcrest.Matchers.equalTo;
6768
import static org.hamcrest.Matchers.lessThanOrEqualTo;
@@ -197,6 +198,7 @@ private Node newNode() {
197198
// turning on the real memory circuit breaker leads to spurious test failures. As have no full control over heap usage, we
198199
// turn it off for these tests.
199200
.put(HierarchyCircuitBreakerService.USE_REAL_MEMORY_USAGE_SETTING.getKey(), false)
201+
.putList(DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING.getKey()) // empty list disables a port scan for other nodes
200202
.put(nodeSettings()) // allow test cases to provide their own settings or override these
201203
.build();
202204
Collection<Class<? extends Plugin>> plugins = getPlugins();

0 commit comments

Comments
 (0)