Skip to content

Commit bfd618c

Browse files
authored
Universal cluster bootstrap method for tests with autoMinMasterNodes=false (#38038)
Currently, there are a few tests that use autoMinMasterNodes=false and hence override addExtraClusterBootstrapSettings, mostly this is 10-30 lines of codes that are copy-pasted from class to class. This PR introduces `InternalTestCluster.setBootstrapMasterNodeIndex` which is suitable for all classes and copy-paste could be removed. Removing code is always a good thing!
1 parent 0e6a7c2 commit bfd618c

File tree

10 files changed

+105
-213
lines changed

10 files changed

+105
-213
lines changed

modules/transport-netty4/src/test/java/org/elasticsearch/rest/discovery/Zen2RestApiIT.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.elasticsearch.client.Response;
2929
import org.elasticsearch.client.ResponseException;
3030
import org.elasticsearch.client.RestClient;
31-
import org.elasticsearch.cluster.coordination.ClusterBootstrapService;
3231
import org.elasticsearch.cluster.metadata.IndexMetaData;
3332
import org.elasticsearch.cluster.routing.UnassignedInfo;
3433
import org.elasticsearch.common.Priority;
@@ -41,10 +40,8 @@
4140
import org.hamcrest.Matchers;
4241

4342
import java.io.IOException;
44-
import java.util.ArrayList;
4543
import java.util.Collections;
4644
import java.util.List;
47-
import java.util.stream.Collectors;
4845

4946
import static org.hamcrest.core.Is.is;
5047

@@ -59,30 +56,13 @@ protected Settings nodeSettings(int nodeOrdinal) {
5956
return Settings.builder().put(super.nodeSettings(nodeOrdinal)).put(TestZenDiscovery.USE_ZEN2.getKey(), true).build();
6057
}
6158

62-
@Override
63-
protected List<Settings> addExtraClusterBootstrapSettings(List<Settings> allNodesSettings) {
64-
final Settings firstNodeSettings = allNodesSettings.get(0);
65-
final List<Settings> otherNodesSettings = allNodesSettings.subList(1, allNodesSettings.size());
66-
final List<String> masterNodeNames = allNodesSettings.stream()
67-
.filter(org.elasticsearch.node.Node.NODE_MASTER_SETTING::get)
68-
.map(org.elasticsearch.node.Node.NODE_NAME_SETTING::get)
69-
.collect(Collectors.toList());
70-
final List<Settings> updatedSettings = new ArrayList<>();
71-
72-
updatedSettings.add(Settings.builder().put(firstNodeSettings)
73-
.putList(ClusterBootstrapService.INITIAL_MASTER_NODES_SETTING.getKey(), masterNodeNames)
74-
.build());
75-
updatedSettings.addAll(otherNodesSettings);
76-
77-
return updatedSettings;
78-
}
79-
8059
@Override
8160
protected boolean addMockHttpTransport() {
8261
return false; // enable http
8362
}
8463

8564
public void testRollingRestartOfTwoNodeCluster() throws Exception {
65+
internalCluster().setBootstrapMasterNodeIndex(1);
8666
final List<String> nodes = internalCluster().startNodes(2);
8767
createIndex("test",
8868
Settings.builder()
@@ -142,6 +122,7 @@ public Settings onNodeStopped(String nodeName) throws IOException {
142122
}
143123

144124
public void testClearVotingTombstonesNotWaitingForRemoval() throws Exception {
125+
internalCluster().setBootstrapMasterNodeIndex(2);
145126
List<String> nodes = internalCluster().startNodes(3);
146127
RestClient restClient = getRestClient();
147128
Response response = restClient.performRequest(new Request("POST", "/_cluster/voting_config_exclusions/" + nodes.get(2)));
@@ -154,6 +135,7 @@ public void testClearVotingTombstonesNotWaitingForRemoval() throws Exception {
154135
}
155136

156137
public void testClearVotingTombstonesWaitingForRemoval() throws Exception {
138+
internalCluster().setBootstrapMasterNodeIndex(2);
157139
List<String> nodes = internalCluster().startNodes(3);
158140
RestClient restClient = getRestClient();
159141
String nodeToWithdraw = nodes.get(randomIntBetween(0, 2));
@@ -167,6 +149,7 @@ public void testClearVotingTombstonesWaitingForRemoval() throws Exception {
167149
}
168150

169151
public void testFailsOnUnknownNode() throws Exception {
152+
internalCluster().setBootstrapMasterNodeIndex(2);
170153
internalCluster().startNodes(3);
171154
RestClient restClient = getRestClient();
172155
try {

server/src/test/java/org/elasticsearch/action/admin/indices/exists/IndicesExistsIT.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.action.admin.indices.exists;
2121

22-
import org.elasticsearch.cluster.coordination.ClusterBootstrapService;
2322
import org.elasticsearch.common.settings.Settings;
2423
import org.elasticsearch.common.unit.TimeValue;
2524
import org.elasticsearch.discovery.MasterNotDiscoveredException;
@@ -29,26 +28,14 @@
2928
import org.elasticsearch.test.InternalTestCluster;
3029

3130
import java.io.IOException;
32-
import java.util.List;
33-
import java.util.stream.Collectors;
34-
35-
import static org.elasticsearch.node.Node.NODE_MASTER_SETTING;
36-
import static org.elasticsearch.node.Node.NODE_NAME_SETTING;
3731
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
3832

3933
@ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0, transportClientRatio = 0.0,
4034
autoMinMasterNodes = false)
4135
public class IndicesExistsIT extends ESIntegTestCase {
4236

43-
@Override
44-
protected List<Settings> addExtraClusterBootstrapSettings(List<Settings> allNodesSettings) {
45-
final List<String> masterNodeNames
46-
= allNodesSettings.stream().filter(NODE_MASTER_SETTING::get).map(NODE_NAME_SETTING::get).collect(Collectors.toList());
47-
return allNodesSettings.stream().map(s -> Settings.builder().put(s)
48-
.putList(ClusterBootstrapService.INITIAL_MASTER_NODES_SETTING.getKey(), masterNodeNames).build()).collect(Collectors.toList());
49-
}
50-
5137
public void testIndexExistsWithBlocksInPlace() throws IOException {
38+
internalCluster().setBootstrapMasterNodeIndex(0);
5239
Settings settings = Settings.builder()
5340
.put(GatewayService.RECOVER_AFTER_NODES_SETTING.getKey(), 99).build();
5441
String node = internalCluster().startNode(settings);

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

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.elasticsearch.action.admin.cluster.configuration.ClearVotingConfigExclusionsRequest;
2626
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
2727
import org.elasticsearch.client.Client;
28-
import org.elasticsearch.cluster.coordination.ClusterBootstrapService;
2928
import org.elasticsearch.cluster.coordination.FailedToCommitClusterStateException;
3029
import org.elasticsearch.cluster.metadata.MetaData;
3130
import org.elasticsearch.cluster.node.DiscoveryNode;
@@ -35,7 +34,6 @@
3534
import org.elasticsearch.discovery.DiscoverySettings;
3635
import org.elasticsearch.discovery.zen.ZenDiscovery;
3736
import org.elasticsearch.index.query.QueryBuilders;
38-
import org.elasticsearch.node.Node;
3937
import org.elasticsearch.plugins.Plugin;
4038
import org.elasticsearch.test.ESIntegTestCase;
4139
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
@@ -46,12 +44,10 @@
4644
import org.elasticsearch.test.junit.annotations.TestLogging;
4745
import org.elasticsearch.test.transport.MockTransportService;
4846

49-
import java.util.ArrayList;
5047
import java.util.Arrays;
5148
import java.util.Collection;
5249
import java.util.Collections;
5350
import java.util.HashSet;
54-
import java.util.List;
5551
import java.util.Set;
5652
import java.util.concurrent.CountDownLatch;
5753
import java.util.concurrent.atomic.AtomicReference;
@@ -68,37 +64,15 @@
6864
@TestLogging("_root:DEBUG,org.elasticsearch.cluster.service:TRACE,org.elasticsearch.discovery.zen:TRACE")
6965
public class MinimumMasterNodesIT extends ESIntegTestCase {
7066

71-
private int bootstrapNodeId;
72-
7367
@Override
7468
protected Collection<Class<? extends Plugin>> nodePlugins() {
7569
final HashSet<Class<? extends Plugin>> classes = new HashSet<>(super.nodePlugins());
7670
classes.add(MockTransportService.TestPlugin.class);
7771
return classes;
7872
}
7973

80-
@Override
81-
protected List<Settings> addExtraClusterBootstrapSettings(List<Settings> allNodesSettings) {
82-
if (internalCluster().size() + allNodesSettings.size() == bootstrapNodeId) {
83-
List<String> nodeNames = new ArrayList<>();
84-
Collections.addAll(nodeNames, internalCluster().getNodeNames());
85-
allNodesSettings.forEach(settings -> nodeNames.add(Node.NODE_NAME_SETTING.get(settings)));
86-
87-
List<Settings> otherNodesSettings = allNodesSettings.subList(0, allNodesSettings.size() - 1);
88-
Settings lastNodeSettings = allNodesSettings.get(allNodesSettings.size()-1);
89-
List<Settings> newSettings = new ArrayList<>();
90-
newSettings.addAll(otherNodesSettings);
91-
newSettings.add(Settings.builder().put(lastNodeSettings)
92-
.putList(ClusterBootstrapService.INITIAL_MASTER_NODES_SETTING.getKey(), nodeNames)
93-
.build());
94-
return newSettings;
95-
}
96-
return allNodesSettings;
97-
}
98-
9974
public void testTwoNodesNoMasterBlock() throws Exception {
100-
//bootstrap cluster once second node is started
101-
bootstrapNodeId = 2;
75+
internalCluster().setBootstrapMasterNodeIndex(1);
10276

10377
Settings settings = Settings.builder()
10478
.put(ZenDiscovery.PING_TIMEOUT_SETTING.getKey(), "200ms")
@@ -231,8 +205,7 @@ public void testTwoNodesNoMasterBlock() throws Exception {
231205
}
232206

233207
public void testThreeNodesNoMasterBlock() throws Exception {
234-
//bootstrap cluster once 3rd node is started
235-
bootstrapNodeId = 3;
208+
internalCluster().setBootstrapMasterNodeIndex(2);
236209

237210
Settings settings = Settings.builder()
238211
.put(ZenDiscovery.PING_TIMEOUT_SETTING.getKey(), "1s")
@@ -307,8 +280,7 @@ public void testThreeNodesNoMasterBlock() throws Exception {
307280
}
308281

309282
public void testCannotCommitStateThreeNodes() throws Exception {
310-
//bootstrap cluster once 3rd node is started
311-
bootstrapNodeId = 3;
283+
internalCluster().setBootstrapMasterNodeIndex(2);
312284

313285
Settings settings = Settings.builder()
314286
.put(ZenDiscovery.PING_TIMEOUT_SETTING.getKey(), "200ms")

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.apache.lucene.search.join.ScoreMode;
2323
import org.elasticsearch.action.admin.cluster.configuration.AddVotingConfigExclusionsAction;
2424
import org.elasticsearch.action.admin.cluster.configuration.AddVotingConfigExclusionsRequest;
25-
import org.elasticsearch.cluster.coordination.ClusterBootstrapService;
2625
import org.elasticsearch.common.settings.Settings;
2726
import org.elasticsearch.common.xcontent.XContentType;
2827
import org.elasticsearch.discovery.MasterNotDiscoveredException;
@@ -35,8 +34,6 @@
3534
import org.elasticsearch.test.junit.annotations.TestLogging;
3635

3736
import java.io.IOException;
38-
import java.util.Collections;
39-
import java.util.List;
4037

4138
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
4239
import static org.hamcrest.Matchers.equalTo;
@@ -46,20 +43,8 @@
4643
@TestLogging("_root:DEBUG,org.elasticsearch.action.admin.cluster.state:TRACE")
4744
public class SpecificMasterNodesIT extends ESIntegTestCase {
4845

49-
@Override
50-
protected List<Settings> addExtraClusterBootstrapSettings(List<Settings> allNodesSettings) {
51-
// if it's the first master in the cluster bootstrap the cluster with this node name
52-
Settings settings = allNodesSettings.get(0);
53-
if (internalCluster().numMasterNodes() == 0 && settings.getAsBoolean(Node.NODE_MASTER_SETTING.getKey(), false)) {
54-
return Collections.singletonList(Settings.builder()
55-
.put(settings)
56-
.put(ClusterBootstrapService.INITIAL_MASTER_NODES_SETTING.getKey(), settings.get(Node.NODE_NAME_SETTING.getKey()))
57-
.build());
58-
}
59-
return allNodesSettings;
60-
}
61-
6246
public void testSimpleOnlyMasterNodeElection() throws IOException {
47+
internalCluster().setBootstrapMasterNodeIndex(0);
6348
logger.info("--> start data node / non master node");
6449
internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), true)
6550
.put(Node.NODE_MASTER_SETTING.getKey(), false)
@@ -100,6 +85,7 @@ public void testSimpleOnlyMasterNodeElection() throws IOException {
10085
}
10186

10287
public void testElectOnlyBetweenMasterNodes() throws Exception {
88+
internalCluster().setBootstrapMasterNodeIndex(0);
10389
logger.info("--> start data node / non master node");
10490
internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), true)
10591
.put(Node.NODE_MASTER_SETTING.getKey(), false).put("discovery.initial_state_timeout", "1s"));
@@ -146,6 +132,7 @@ public void testElectOnlyBetweenMasterNodes() throws Exception {
146132
}
147133

148134
public void testAliasFilterValidation() {
135+
internalCluster().setBootstrapMasterNodeIndex(0);
149136
logger.info("--> start master node / non data");
150137
internalCluster().startNode(Settings.builder()
151138
.put(Node.NODE_DATA_SETTING.getKey(), false).put(Node.NODE_MASTER_SETTING.getKey(), true));

server/src/test/java/org/elasticsearch/cluster/coordination/UnsafeBootstrapMasterIT.java

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@
3636
import org.elasticsearch.test.ESIntegTestCase;
3737
import org.elasticsearch.test.InternalTestCluster;
3838
import org.elasticsearch.test.junit.annotations.TestLogging;
39-
import org.junit.Before;
4039

4140
import java.io.IOException;
42-
import java.util.ArrayList;
43-
import java.util.Collections;
4441
import java.util.List;
4542
import java.util.Locale;
4643

@@ -50,42 +47,6 @@
5047
@TestLogging("_root:DEBUG,org.elasticsearch.cluster.service:TRACE,org.elasticsearch.discovery.zen:TRACE")
5148
public class UnsafeBootstrapMasterIT extends ESIntegTestCase {
5249

53-
private int bootstrapNodeId;
54-
55-
@Before
56-
public void resetBootstrapNodeId() {
57-
bootstrapNodeId = -1;
58-
}
59-
60-
/**
61-
* Performs cluster bootstrap when node with id bootstrapNodeId is started.
62-
* Any node of the batch could be selected as bootstrap target.
63-
*/
64-
@Override
65-
protected List<Settings> addExtraClusterBootstrapSettings(List<Settings> allNodesSettings) {
66-
if (internalCluster().size() + allNodesSettings.size() == bootstrapNodeId) {
67-
List<String> nodeNames = new ArrayList<>();
68-
Collections.addAll(nodeNames, internalCluster().getNodeNames());
69-
allNodesSettings.forEach(settings -> nodeNames.add(Node.NODE_NAME_SETTING.get(settings)));
70-
71-
List<Settings> newSettings = new ArrayList<>();
72-
int bootstrapIndex = randomInt(allNodesSettings.size() - 1);
73-
for (int i = 0; i < allNodesSettings.size(); i++) {
74-
Settings nodeSettings = allNodesSettings.get(i);
75-
if (i == bootstrapIndex) {
76-
newSettings.add(Settings.builder().put(nodeSettings)
77-
.putList(ClusterBootstrapService.INITIAL_MASTER_NODES_SETTING.getKey(), nodeNames)
78-
.build());
79-
} else {
80-
newSettings.add(nodeSettings);
81-
}
82-
}
83-
84-
return newSettings;
85-
}
86-
return allNodesSettings;
87-
}
88-
8950
private MockTerminal executeCommand(Environment environment, boolean abort) throws Exception {
9051
final UnsafeBootstrapMasterCommand command = new UnsafeBootstrapMasterCommand();
9152
final MockTerminal terminal = new MockTerminal();
@@ -169,7 +130,7 @@ public void testNotBootstrappedCluster() throws Exception {
169130
}
170131

171132
public void testNoManifestFile() throws IOException {
172-
bootstrapNodeId = 1;
133+
internalCluster().setBootstrapMasterNodeIndex(0);
173134
internalCluster().startNode();
174135
ensureStableCluster(1);
175136
NodeEnvironment nodeEnvironment = internalCluster().getMasterNodeInstance(NodeEnvironment.class);
@@ -181,7 +142,7 @@ public void testNoManifestFile() throws IOException {
181142
}
182143

183144
public void testNoMetaData() throws IOException {
184-
bootstrapNodeId = 1;
145+
internalCluster().setBootstrapMasterNodeIndex(0);
185146
internalCluster().startNode();
186147
ensureStableCluster(1);
187148
NodeEnvironment nodeEnvironment = internalCluster().getMasterNodeInstance(NodeEnvironment.class);
@@ -194,7 +155,7 @@ public void testNoMetaData() throws IOException {
194155
}
195156

196157
public void testAbortedByUser() throws IOException {
197-
bootstrapNodeId = 1;
158+
internalCluster().setBootstrapMasterNodeIndex(0);
198159
internalCluster().startNode();
199160
ensureStableCluster(1);
200161
internalCluster().stopRandomDataNode();
@@ -204,7 +165,7 @@ public void testAbortedByUser() throws IOException {
204165
}
205166

206167
public void test3MasterNodes2Failed() throws Exception {
207-
bootstrapNodeId = 3;
168+
internalCluster().setBootstrapMasterNodeIndex(2);
208169
List<String> masterNodes = internalCluster().startMasterOnlyNodes(3, Settings.EMPTY);
209170

210171
String dataNode = internalCluster().startDataOnlyNode();

0 commit comments

Comments
 (0)