Skip to content

Commit 5a3fd8e

Browse files
authored
Use file-based discovery not MockUncasedHostsProvider (#33554)
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 #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 94f6d45 commit 5a3fd8e

File tree

10 files changed

+178
-164
lines changed

10 files changed

+178
-164
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/test/java/org/elasticsearch/cluster/ClusterInfoServiceIT.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import java.util.Collection;
5454
import java.util.List;
5555
import java.util.Set;
56-
import java.util.concurrent.ExecutionException;
5756
import java.util.concurrent.atomic.AtomicBoolean;
5857

5958
import static java.util.Collections.emptySet;
@@ -113,6 +112,7 @@ public void blockActions(String... actions) {
113112
@Override
114113
protected Settings nodeSettings(int nodeOrdinal) {
115114
return Settings.builder()
115+
.put(super.nodeSettings(nodeOrdinal))
116116
// manual collection or upon cluster forming.
117117
.put(NodeEnvironment.MAX_LOCAL_STORAGE_NODES_SETTING.getKey(), 2)
118118
.put(InternalClusterInfoService.INTERNAL_CLUSTER_INFO_TIMEOUT_SETTING.getKey(), "1s")
@@ -121,8 +121,7 @@ protected Settings nodeSettings(int nodeOrdinal) {
121121

122122
@Override
123123
protected Collection<Class<? extends Plugin>> nodePlugins() {
124-
return Arrays.asList(TestPlugin.class,
125-
MockTransportService.TestPlugin.class);
124+
return Arrays.asList(TestPlugin.class, MockTransportService.TestPlugin.class);
126125
}
127126

128127
public void testClusterInfoServiceCollectsInformation() throws Exception {
@@ -172,7 +171,7 @@ public void testClusterInfoServiceCollectsInformation() throws Exception {
172171
}
173172
}
174173

175-
public void testClusterInfoServiceInformationClearOnError() throws InterruptedException, ExecutionException {
174+
public void testClusterInfoServiceInformationClearOnError() {
176175
internalCluster().startNodes(2,
177176
// manually control publishing
178177
Settings.builder().put(InternalClusterInfoService.INTERNAL_CLUSTER_INFO_UPDATE_INTERVAL_SETTING.getKey(), "60m").build());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.test.ESIntegTestCase;
26+
27+
import static org.elasticsearch.discovery.DiscoveryModule.DISCOVERY_HOSTS_PROVIDER_SETTING;
28+
import static org.elasticsearch.discovery.zen.SettingsBasedHostsProvider.DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING;
29+
import static org.elasticsearch.discovery.zen.SettingsBasedHostsProvider.LIMIT_LOCAL_PORTS_COUNT;
30+
import static org.elasticsearch.transport.TcpTransport.PORT;
31+
32+
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0)
33+
public class SettingsBasedHostProviderIT extends ESIntegTestCase {
34+
35+
@Override
36+
protected Settings nodeSettings(int nodeOrdinal) {
37+
Settings.Builder builder = Settings.builder().put(super.nodeSettings(nodeOrdinal));
38+
39+
// super.nodeSettings enables file-based discovery, but here we disable it again so we can test the static list:
40+
if (randomBoolean()) {
41+
builder.putList(DISCOVERY_HOSTS_PROVIDER_SETTING.getKey());
42+
} else {
43+
builder.remove(DISCOVERY_HOSTS_PROVIDER_SETTING.getKey());
44+
}
45+
46+
// super.nodeSettings sets this to an empty list, which disables any search for other nodes, but here we want this to happen:
47+
builder.remove(DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING.getKey());
48+
49+
return builder.build();
50+
}
51+
52+
public void testClusterFormsWithSingleSeedHostInSettings() {
53+
final String seedNodeName = internalCluster().startNode();
54+
final NodesInfoResponse nodesInfoResponse
55+
= client(seedNodeName).admin().cluster().nodesInfo(new NodesInfoRequest("_local")).actionGet();
56+
final String seedNodeAddress = nodesInfoResponse.getNodes().get(0).getTransport().getAddress().publishAddress().toString();
57+
logger.info("--> using seed node address {}", seedNodeAddress);
58+
59+
int extraNodes = randomIntBetween(1, 5);
60+
internalCluster().startNodes(extraNodes,
61+
Settings.builder().putList(DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING.getKey(), seedNodeAddress).build());
62+
63+
ensureStableCluster(extraNodes + 1);
64+
}
65+
66+
public void testClusterFormsByScanningPorts() {
67+
// This test will fail if all 4 ports just less than the one used by the first node are already bound by something else. It's hard
68+
// to know how often this might happen in reality, so let's try it and see.
69+
70+
final String seedNodeName = internalCluster().startNode();
71+
final NodesInfoResponse nodesInfoResponse
72+
= client(seedNodeName).admin().cluster().nodesInfo(new NodesInfoRequest("_local")).actionGet();
73+
final int seedNodePort = nodesInfoResponse.getNodes().get(0).getTransport().getAddress().publishAddress().getPort();
74+
final int minPort = randomIntBetween(seedNodePort - LIMIT_LOCAL_PORTS_COUNT + 1, seedNodePort - 1);
75+
final String portSpec = minPort + "-" + seedNodePort;
76+
77+
logger.info("--> using port specification [{}]", portSpec);
78+
internalCluster().startNode(Settings.builder().put(PORT.getKey(), portSpec));
79+
ensureStableCluster(2);
80+
}
81+
}

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/test/ESIntegTestCase.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@
206206
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
207207
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
208208
import static org.elasticsearch.common.util.CollectionUtils.eagerPartition;
209+
import static org.elasticsearch.discovery.DiscoveryModule.DISCOVERY_HOSTS_PROVIDER_SETTING;
210+
import static org.elasticsearch.discovery.zen.SettingsBasedHostsProvider.DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING;
209211
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
210212
import static org.elasticsearch.test.XContentTestUtils.convertToMap;
211213
import static org.elasticsearch.test.XContentTestUtils.differenceBetweenMapsIgnoringArrayOrder;
@@ -1808,7 +1810,9 @@ protected Settings nodeSettings(int nodeOrdinal) {
18081810
// wait short time for other active shards before actually deleting, default 30s not needed in tests
18091811
.put(IndicesStore.INDICES_STORE_DELETE_SHARD_TIMEOUT.getKey(), new TimeValue(1, TimeUnit.SECONDS))
18101812
// randomly enable low-level search cancellation to make sure it does not alter results
1811-
.put(SearchService.LOW_LEVEL_CANCELLATION_SETTING.getKey(), randomBoolean());
1813+
.put(SearchService.LOW_LEVEL_CANCELLATION_SETTING.getKey(), randomBoolean())
1814+
.putList(DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING.getKey()) // empty list disables a port scan for other nodes
1815+
.putList(DISCOVERY_HOSTS_PROVIDER_SETTING.getKey(), "file");
18121816
if (rarely()) {
18131817
// Sometimes adjust the minimum search thread pool size, causing
18141818
// QueueResizingEsThreadPoolExecutor to be used instead of a regular
@@ -1921,7 +1925,7 @@ protected NodeConfigurationSource getNodeConfigSource() {
19211925
networkSettings.put(NetworkModule.TRANSPORT_TYPE_KEY, getTestTransportType());
19221926
}
19231927

1924-
NodeConfigurationSource nodeConfigurationSource = new NodeConfigurationSource() {
1928+
return new NodeConfigurationSource() {
19251929
@Override
19261930
public Settings nodeSettings(int nodeOrdinal) {
19271931
return Settings.builder()
@@ -1955,7 +1959,6 @@ public Collection<Class<? extends Plugin>> transportClientPlugins() {
19551959
return Collections.unmodifiableCollection(plugins);
19561960
}
19571961
};
1958-
return nodeConfigurationSource;
19591962
}
19601963

19611964
/**
@@ -2029,7 +2032,7 @@ protected Collection<Class<? extends Plugin>> getMockPlugins() {
20292032
public static final class TestSeedPlugin extends Plugin {
20302033
@Override
20312034
public List<Setting<?>> getSettings() {
2032-
return Arrays.asList(INDEX_TEST_SEED_SETTING);
2035+
return Collections.singletonList(INDEX_TEST_SEED_SETTING);
20332036
}
20342037
}
20352038

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)