Skip to content

Commit a098151

Browse files
original-brownbearkcm
authored andcommitted
Disc: Move AbstractDisruptionTC to filebased D. (#34461)
* Discovery: Move AbstractDisruptionTestCase to file-based discovery. * Relates #33675 * Simplify away ClusterDiscoveryConfiguration
1 parent 6320608 commit a098151

File tree

11 files changed

+61
-244
lines changed

11 files changed

+61
-244
lines changed

server/src/test/java/org/elasticsearch/discovery/AbstractDisruptionTestCase.java

+25-29
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.discovery;
2121

22+
import java.nio.file.Path;
2223
import org.elasticsearch.cluster.ClusterState;
2324
import org.elasticsearch.cluster.block.ClusterBlock;
2425
import org.elasticsearch.cluster.block.ClusterBlockLevel;
@@ -33,7 +34,8 @@
3334
import org.elasticsearch.env.NodeEnvironment;
3435
import org.elasticsearch.plugins.Plugin;
3536
import org.elasticsearch.test.ESIntegTestCase;
36-
import org.elasticsearch.test.discovery.ClusterDiscoveryConfiguration;
37+
import org.elasticsearch.test.InternalTestCluster;
38+
import org.elasticsearch.test.NodeConfigurationSource;
3739
import org.elasticsearch.test.discovery.TestZenDiscovery;
3840
import org.elasticsearch.test.disruption.NetworkDisruption;
3941
import org.elasticsearch.test.disruption.NetworkDisruption.Bridge;
@@ -52,17 +54,17 @@
5254
import java.util.HashSet;
5355
import java.util.List;
5456
import java.util.Set;
55-
import java.util.concurrent.ExecutionException;
5657
import java.util.concurrent.TimeUnit;
5758

59+
import static org.elasticsearch.discovery.DiscoveryModule.DISCOVERY_HOSTS_PROVIDER_SETTING;
5860
import static org.hamcrest.Matchers.equalTo;
5961
import static org.hamcrest.Matchers.not;
6062

6163
public abstract class AbstractDisruptionTestCase extends ESIntegTestCase {
6264

6365
static final TimeValue DISRUPTION_HEALING_OVERHEAD = TimeValue.timeValueSeconds(40); // we use 30s as timeout in many places.
6466

65-
private ClusterDiscoveryConfiguration discoveryConfig;
67+
private NodeConfigurationSource discoveryConfig;
6668

6769
@Override
6870
protected Settings nodeSettings(int nodeOrdinal) {
@@ -116,18 +118,14 @@ protected void beforeIndexDeletion() throws Exception {
116118
}
117119
}
118120

119-
List<String> startCluster(int numberOfNodes) throws ExecutionException, InterruptedException {
121+
List<String> startCluster(int numberOfNodes) {
120122
return startCluster(numberOfNodes, -1);
121123
}
122124

123-
List<String> startCluster(int numberOfNodes, int minimumMasterNode) throws ExecutionException, InterruptedException {
124-
return startCluster(numberOfNodes, minimumMasterNode, null);
125-
}
126-
127-
List<String> startCluster(int numberOfNodes, int minimumMasterNode, @Nullable int[] unicastHostsOrdinals) throws
128-
ExecutionException, InterruptedException {
129-
configureCluster(numberOfNodes, unicastHostsOrdinals, minimumMasterNode);
130-
List<String> nodes = internalCluster().startNodes(numberOfNodes);
125+
List<String> startCluster(int numberOfNodes, int minimumMasterNode) {
126+
configureCluster(numberOfNodes, minimumMasterNode);
127+
InternalTestCluster internalCluster = internalCluster();
128+
List<String> nodes = internalCluster.startNodes(numberOfNodes);
131129
ensureStableCluster(numberOfNodes);
132130

133131
// TODO: this is a temporary solution so that nodes will not base their reaction to a partition based on previous successful results
@@ -154,20 +152,11 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
154152
return Arrays.asList(MockTransportService.TestPlugin.class);
155153
}
156154

157-
void configureCluster(
158-
int numberOfNodes,
159-
@Nullable int[] unicastHostsOrdinals,
160-
int minimumMasterNode
161-
) throws ExecutionException, InterruptedException {
162-
configureCluster(DEFAULT_SETTINGS, numberOfNodes, unicastHostsOrdinals, minimumMasterNode);
155+
void configureCluster(int numberOfNodes, int minimumMasterNode) {
156+
configureCluster(DEFAULT_SETTINGS, numberOfNodes, minimumMasterNode);
163157
}
164158

165-
void configureCluster(
166-
Settings settings,
167-
int numberOfNodes,
168-
@Nullable int[] unicastHostsOrdinals,
169-
int minimumMasterNode
170-
) throws ExecutionException, InterruptedException {
159+
void configureCluster(Settings settings, int numberOfNodes, int minimumMasterNode) {
171160
if (minimumMasterNode < 0) {
172161
minimumMasterNode = numberOfNodes / 2 + 1;
173162
}
@@ -177,14 +166,21 @@ void configureCluster(
177166
.put(settings)
178167
.put(NodeEnvironment.MAX_LOCAL_STORAGE_NODES_SETTING.getKey(), numberOfNodes)
179168
.put(ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), minimumMasterNode)
169+
.putList(DISCOVERY_HOSTS_PROVIDER_SETTING.getKey(), "file")
180170
.build();
181171

182172
if (discoveryConfig == null) {
183-
if (unicastHostsOrdinals == null) {
184-
discoveryConfig = new ClusterDiscoveryConfiguration.UnicastZen(numberOfNodes, nodeSettings);
185-
} else {
186-
discoveryConfig = new ClusterDiscoveryConfiguration.UnicastZen(numberOfNodes, nodeSettings, unicastHostsOrdinals);
187-
}
173+
discoveryConfig = new NodeConfigurationSource() {
174+
@Override
175+
public Settings nodeSettings(final int nodeOrdinal) {
176+
return nodeSettings;
177+
}
178+
179+
@Override
180+
public Path nodeConfigPath(final int nodeOrdinal) {
181+
return null;
182+
}
183+
};
188184
}
189185
}
190186

server/src/test/java/org/elasticsearch/discovery/ClusterDisruptionIT.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ public void onFailure(Exception e) {
363363
*/
364364
public void testSearchWithRelocationAndSlowClusterStateProcessing() throws Exception {
365365
// don't use DEFAULT settings (which can cause node disconnects on a slow CI machine)
366-
configureCluster(Settings.EMPTY, 3, null, 1);
366+
configureCluster(Settings.EMPTY, 3, 1);
367367
internalCluster().startMasterOnlyNode();
368368
final String node_1 = internalCluster().startDataOnlyNode();
369369

@@ -390,7 +390,7 @@ public void testSearchWithRelocationAndSlowClusterStateProcessing() throws Excep
390390

391391
public void testIndexImportedFromDataOnlyNodesIfMasterLostDataFolder() throws Exception {
392392
// test for https://github.com/elastic/elasticsearch/issues/8823
393-
configureCluster(2, null, 1);
393+
configureCluster(2, 1);
394394
String masterNode = internalCluster().startMasterOnlyNode(Settings.EMPTY);
395395
internalCluster().startDataOnlyNode(Settings.EMPTY);
396396

@@ -421,7 +421,7 @@ public void testIndicesDeleted() throws Exception {
421421
.put(DiscoverySettings.COMMIT_TIMEOUT_SETTING.getKey(), "30s") // wait till cluster state is committed
422422
.build();
423423
final String idxName = "test";
424-
configureCluster(settings, 3, null, 2);
424+
configureCluster(settings, 3, 2);
425425
final List<String> allMasterEligibleNodes = internalCluster().startMasterOnlyNodes(2);
426426
final String dataNode = internalCluster().startDataOnlyNode();
427427
ensureStableCluster(3);

server/src/test/java/org/elasticsearch/discovery/DiscoveryDisruptionIT.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
public class DiscoveryDisruptionIT extends AbstractDisruptionTestCase {
6060

6161
public void testIsolatedUnicastNodes() throws Exception {
62-
List<String> nodes = startCluster(4, -1, new int[]{0});
62+
internalCluster().setHostsListContainsOnlyFirstNode(true);
63+
List<String> nodes = startCluster(4, -1);
6364
// Figure out what is the elected master node
6465
final String unicastTarget = nodes.get(0);
6566

@@ -98,7 +99,8 @@ public void testIsolatedUnicastNodes() throws Exception {
9899
* The rejoining node should take this master node and connect.
99100
*/
100101
public void testUnicastSinglePingResponseContainsMaster() throws Exception {
101-
List<String> nodes = startCluster(4, -1, new int[]{0});
102+
internalCluster().setHostsListContainsOnlyFirstNode(true);
103+
List<String> nodes = startCluster(4, -1);
102104
// Figure out what is the elected master node
103105
final String masterNode = internalCluster().getMasterName();
104106
logger.info("---> legit elected master node={}", masterNode);
@@ -194,7 +196,7 @@ public void testClusterJoinDespiteOfPublishingIssues() throws Exception {
194196
}
195197

196198
public void testClusterFormingWithASlowNode() throws Exception {
197-
configureCluster(3, null, 2);
199+
configureCluster(3, 2);
198200

199201
SlowClusterStateProcessing disruption = new SlowClusterStateProcessing(random(), 0, 0, 1000, 2000);
200202

@@ -210,7 +212,7 @@ public void testClusterFormingWithASlowNode() throws Exception {
210212
}
211213

212214
public void testElectMasterWithLatestVersion() throws Exception {
213-
configureCluster(3, null, 2);
215+
configureCluster(3, 2);
214216
final Set<String> nodes = new HashSet<>(internalCluster().startNodes(3));
215217
ensureStableCluster(3);
216218
ServiceDisruptionScheme isolateAllNodes =

server/src/test/java/org/elasticsearch/discovery/SnapshotDisruptionIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void testDisruptionOnSnapshotInitialization() throws Exception {
5959
.put(DiscoverySettings.COMMIT_TIMEOUT_SETTING.getKey(), "30s") // wait till cluster state is committed
6060
.build();
6161
final String idxName = "test";
62-
configureCluster(settings, 4, null, 2);
62+
configureCluster(settings, 4, 2);
6363
final List<String> allMasterEligibleNodes = internalCluster().startMasterOnlyNodes(3);
6464
final String dataNode = internalCluster().startDataOnlyNode();
6565
ensureStableCluster(4);

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ public final class InternalTestCluster extends TestCluster {
231231
private ServiceDisruptionScheme activeDisruptionScheme;
232232
private Function<Client, Client> clientWrapper;
233233

234+
// If set to true only the first node in the cluster will be made a unicast node
235+
private boolean hostsListContainsOnlyFirstNode;
236+
234237
public InternalTestCluster(
235238
final long clusterSeed,
236239
final Path baseDir,
@@ -1605,12 +1608,17 @@ private synchronized void startAndPublishNodesAndClients(List<NodeAndClient> nod
16051608

16061609
private final Object discoveryFileMutex = new Object();
16071610

1608-
private void rebuildUnicastHostFiles(Collection<NodeAndClient> newNodes) {
1611+
private void rebuildUnicastHostFiles(List<NodeAndClient> newNodes) {
16091612
// cannot be a synchronized method since it's called on other threads from within synchronized startAndPublishNodesAndClients()
16101613
synchronized (discoveryFileMutex) {
16111614
try {
1612-
List<String> discoveryFileContents = Stream.concat(nodes.values().stream(), newNodes.stream())
1613-
.map(nac -> nac.node.injector().getInstance(TransportService.class)).filter(Objects::nonNull)
1615+
Stream<NodeAndClient> unicastHosts = Stream.concat(nodes.values().stream(), newNodes.stream());
1616+
if (hostsListContainsOnlyFirstNode) {
1617+
unicastHosts = unicastHosts.limit(1L);
1618+
}
1619+
List<String> discoveryFileContents = unicastHosts.map(
1620+
nac -> nac.node.injector().getInstance(TransportService.class)
1621+
).filter(Objects::nonNull)
16141622
.map(TransportService::getLocalNode).filter(Objects::nonNull).filter(DiscoveryNode::isMasterNode)
16151623
.map(n -> n.getAddress().toString())
16161624
.distinct().collect(Collectors.toList());
@@ -2038,6 +2046,9 @@ public synchronized int numMasterNodes() {
20382046
return filterNodes(nodes, NodeAndClient::isMasterEligible).size();
20392047
}
20402048

2049+
public void setHostsListContainsOnlyFirstNode(boolean hostsListContainsOnlyFirstNode) {
2050+
this.hostsListContainsOnlyFirstNode = hostsListContainsOnlyFirstNode;
2051+
}
20412052

20422053
public void setDisruptionScheme(ServiceDisruptionScheme scheme) {
20432054
assert activeDisruptionScheme == null :

0 commit comments

Comments
 (0)