Skip to content

Commit

Permalink
Universal cluster bootstrap method for tests with autoMinMasterNodes=…
Browse files Browse the repository at this point in the history
…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!
  • Loading branch information
andrershov authored Feb 1, 2019
1 parent 0e6a7c2 commit bfd618c
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.cluster.coordination.ClusterBootstrapService;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.routing.UnassignedInfo;
import org.elasticsearch.common.Priority;
Expand All @@ -41,10 +40,8 @@
import org.hamcrest.Matchers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

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

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

@Override
protected List<Settings> addExtraClusterBootstrapSettings(List<Settings> allNodesSettings) {
final Settings firstNodeSettings = allNodesSettings.get(0);
final List<Settings> otherNodesSettings = allNodesSettings.subList(1, allNodesSettings.size());
final List<String> masterNodeNames = allNodesSettings.stream()
.filter(org.elasticsearch.node.Node.NODE_MASTER_SETTING::get)
.map(org.elasticsearch.node.Node.NODE_NAME_SETTING::get)
.collect(Collectors.toList());
final List<Settings> updatedSettings = new ArrayList<>();

updatedSettings.add(Settings.builder().put(firstNodeSettings)
.putList(ClusterBootstrapService.INITIAL_MASTER_NODES_SETTING.getKey(), masterNodeNames)
.build());
updatedSettings.addAll(otherNodesSettings);

return updatedSettings;
}

@Override
protected boolean addMockHttpTransport() {
return false; // enable http
}

public void testRollingRestartOfTwoNodeCluster() throws Exception {
internalCluster().setBootstrapMasterNodeIndex(1);
final List<String> nodes = internalCluster().startNodes(2);
createIndex("test",
Settings.builder()
Expand Down Expand Up @@ -142,6 +122,7 @@ public Settings onNodeStopped(String nodeName) throws IOException {
}

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

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

public void testFailsOnUnknownNode() throws Exception {
internalCluster().setBootstrapMasterNodeIndex(2);
internalCluster().startNodes(3);
RestClient restClient = getRestClient();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

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

import org.elasticsearch.cluster.coordination.ClusterBootstrapService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.discovery.MasterNotDiscoveredException;
Expand All @@ -29,26 +28,14 @@
import org.elasticsearch.test.InternalTestCluster;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import static org.elasticsearch.node.Node.NODE_MASTER_SETTING;
import static org.elasticsearch.node.Node.NODE_NAME_SETTING;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;

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

@Override
protected List<Settings> addExtraClusterBootstrapSettings(List<Settings> allNodesSettings) {
final List<String> masterNodeNames
= allNodesSettings.stream().filter(NODE_MASTER_SETTING::get).map(NODE_NAME_SETTING::get).collect(Collectors.toList());
return allNodesSettings.stream().map(s -> Settings.builder().put(s)
.putList(ClusterBootstrapService.INITIAL_MASTER_NODES_SETTING.getKey(), masterNodeNames).build()).collect(Collectors.toList());
}

public void testIndexExistsWithBlocksInPlace() throws IOException {
internalCluster().setBootstrapMasterNodeIndex(0);
Settings settings = Settings.builder()
.put(GatewayService.RECOVER_AFTER_NODES_SETTING.getKey(), 99).build();
String node = internalCluster().startNode(settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.elasticsearch.action.admin.cluster.configuration.ClearVotingConfigExclusionsRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.coordination.ClusterBootstrapService;
import org.elasticsearch.cluster.coordination.FailedToCommitClusterStateException;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.node.DiscoveryNode;
Expand All @@ -35,7 +34,6 @@
import org.elasticsearch.discovery.DiscoverySettings;
import org.elasticsearch.discovery.zen.ZenDiscovery;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
Expand All @@ -46,12 +44,10 @@
import org.elasticsearch.test.junit.annotations.TestLogging;
import org.elasticsearch.test.transport.MockTransportService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -68,37 +64,15 @@
@TestLogging("_root:DEBUG,org.elasticsearch.cluster.service:TRACE,org.elasticsearch.discovery.zen:TRACE")
public class MinimumMasterNodesIT extends ESIntegTestCase {

private int bootstrapNodeId;

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
final HashSet<Class<? extends Plugin>> classes = new HashSet<>(super.nodePlugins());
classes.add(MockTransportService.TestPlugin.class);
return classes;
}

@Override
protected List<Settings> addExtraClusterBootstrapSettings(List<Settings> allNodesSettings) {
if (internalCluster().size() + allNodesSettings.size() == bootstrapNodeId) {
List<String> nodeNames = new ArrayList<>();
Collections.addAll(nodeNames, internalCluster().getNodeNames());
allNodesSettings.forEach(settings -> nodeNames.add(Node.NODE_NAME_SETTING.get(settings)));

List<Settings> otherNodesSettings = allNodesSettings.subList(0, allNodesSettings.size() - 1);
Settings lastNodeSettings = allNodesSettings.get(allNodesSettings.size()-1);
List<Settings> newSettings = new ArrayList<>();
newSettings.addAll(otherNodesSettings);
newSettings.add(Settings.builder().put(lastNodeSettings)
.putList(ClusterBootstrapService.INITIAL_MASTER_NODES_SETTING.getKey(), nodeNames)
.build());
return newSettings;
}
return allNodesSettings;
}

public void testTwoNodesNoMasterBlock() throws Exception {
//bootstrap cluster once second node is started
bootstrapNodeId = 2;
internalCluster().setBootstrapMasterNodeIndex(1);

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

public void testThreeNodesNoMasterBlock() throws Exception {
//bootstrap cluster once 3rd node is started
bootstrapNodeId = 3;
internalCluster().setBootstrapMasterNodeIndex(2);

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

public void testCannotCommitStateThreeNodes() throws Exception {
//bootstrap cluster once 3rd node is started
bootstrapNodeId = 3;
internalCluster().setBootstrapMasterNodeIndex(2);

Settings settings = Settings.builder()
.put(ZenDiscovery.PING_TIMEOUT_SETTING.getKey(), "200ms")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.admin.cluster.configuration.AddVotingConfigExclusionsAction;
import org.elasticsearch.action.admin.cluster.configuration.AddVotingConfigExclusionsRequest;
import org.elasticsearch.cluster.coordination.ClusterBootstrapService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.discovery.MasterNotDiscoveredException;
Expand All @@ -35,8 +34,6 @@
import org.elasticsearch.test.junit.annotations.TestLogging;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

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

@Override
protected List<Settings> addExtraClusterBootstrapSettings(List<Settings> allNodesSettings) {
// if it's the first master in the cluster bootstrap the cluster with this node name
Settings settings = allNodesSettings.get(0);
if (internalCluster().numMasterNodes() == 0 && settings.getAsBoolean(Node.NODE_MASTER_SETTING.getKey(), false)) {
return Collections.singletonList(Settings.builder()
.put(settings)
.put(ClusterBootstrapService.INITIAL_MASTER_NODES_SETTING.getKey(), settings.get(Node.NODE_NAME_SETTING.getKey()))
.build());
}
return allNodesSettings;
}

public void testSimpleOnlyMasterNodeElection() throws IOException {
internalCluster().setBootstrapMasterNodeIndex(0);
logger.info("--> start data node / non master node");
internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), true)
.put(Node.NODE_MASTER_SETTING.getKey(), false)
Expand Down Expand Up @@ -100,6 +85,7 @@ public void testSimpleOnlyMasterNodeElection() throws IOException {
}

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

public void testAliasFilterValidation() {
internalCluster().setBootstrapMasterNodeIndex(0);
logger.info("--> start master node / non data");
internalCluster().startNode(Settings.builder()
.put(Node.NODE_DATA_SETTING.getKey(), false).put(Node.NODE_MASTER_SETTING.getKey(), true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.InternalTestCluster;
import org.elasticsearch.test.junit.annotations.TestLogging;
import org.junit.Before;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

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

private int bootstrapNodeId;

@Before
public void resetBootstrapNodeId() {
bootstrapNodeId = -1;
}

/**
* Performs cluster bootstrap when node with id bootstrapNodeId is started.
* Any node of the batch could be selected as bootstrap target.
*/
@Override
protected List<Settings> addExtraClusterBootstrapSettings(List<Settings> allNodesSettings) {
if (internalCluster().size() + allNodesSettings.size() == bootstrapNodeId) {
List<String> nodeNames = new ArrayList<>();
Collections.addAll(nodeNames, internalCluster().getNodeNames());
allNodesSettings.forEach(settings -> nodeNames.add(Node.NODE_NAME_SETTING.get(settings)));

List<Settings> newSettings = new ArrayList<>();
int bootstrapIndex = randomInt(allNodesSettings.size() - 1);
for (int i = 0; i < allNodesSettings.size(); i++) {
Settings nodeSettings = allNodesSettings.get(i);
if (i == bootstrapIndex) {
newSettings.add(Settings.builder().put(nodeSettings)
.putList(ClusterBootstrapService.INITIAL_MASTER_NODES_SETTING.getKey(), nodeNames)
.build());
} else {
newSettings.add(nodeSettings);
}
}

return newSettings;
}
return allNodesSettings;
}

private MockTerminal executeCommand(Environment environment, boolean abort) throws Exception {
final UnsafeBootstrapMasterCommand command = new UnsafeBootstrapMasterCommand();
final MockTerminal terminal = new MockTerminal();
Expand Down Expand Up @@ -169,7 +130,7 @@ public void testNotBootstrappedCluster() throws Exception {
}

public void testNoManifestFile() throws IOException {
bootstrapNodeId = 1;
internalCluster().setBootstrapMasterNodeIndex(0);
internalCluster().startNode();
ensureStableCluster(1);
NodeEnvironment nodeEnvironment = internalCluster().getMasterNodeInstance(NodeEnvironment.class);
Expand All @@ -181,7 +142,7 @@ public void testNoManifestFile() throws IOException {
}

public void testNoMetaData() throws IOException {
bootstrapNodeId = 1;
internalCluster().setBootstrapMasterNodeIndex(0);
internalCluster().startNode();
ensureStableCluster(1);
NodeEnvironment nodeEnvironment = internalCluster().getMasterNodeInstance(NodeEnvironment.class);
Expand All @@ -194,7 +155,7 @@ public void testNoMetaData() throws IOException {
}

public void testAbortedByUser() throws IOException {
bootstrapNodeId = 1;
internalCluster().setBootstrapMasterNodeIndex(0);
internalCluster().startNode();
ensureStableCluster(1);
internalCluster().stopRandomDataNode();
Expand All @@ -204,7 +165,7 @@ public void testAbortedByUser() throws IOException {
}

public void test3MasterNodes2Failed() throws Exception {
bootstrapNodeId = 3;
internalCluster().setBootstrapMasterNodeIndex(2);
List<String> masterNodes = internalCluster().startMasterOnlyNodes(3, Settings.EMPTY);

String dataNode = internalCluster().startDataOnlyNode();
Expand Down
Loading

0 comments on commit bfd618c

Please sign in to comment.