From b3968feb20e92cdc5c3252bf922293877e8b65df Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 21 Sep 2017 15:45:30 +0200 Subject: [PATCH] SONAR-9802 fix support of overridden cluster name --- .../org/sonar/application/AppStateFactory.java | 1 - .../cluster/ClusterAppStateImplTest.java | 1 - .../cluster/hz/HazelcastMemberBuilder.java | 12 +++++------- .../cluster/hz/HazelcastMemberBuilderTest.java | 1 - .../cluster/hz/HazelcastMemberImplTest.java | 1 - .../cluster/StartableHazelcastMember.java | 1 - .../sonarqube/tests/cluster/ClusterTest.java | 18 ++++++++++++++++++ 7 files changed, 23 insertions(+), 12 deletions(-) diff --git a/server/sonar-main/src/main/java/org/sonar/application/AppStateFactory.java b/server/sonar-main/src/main/java/org/sonar/application/AppStateFactory.java index 0498f2e4100..75f1577c432 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/AppStateFactory.java +++ b/server/sonar-main/src/main/java/org/sonar/application/AppStateFactory.java @@ -49,7 +49,6 @@ public class AppStateFactory { private static HazelcastMember createHzMember(Props props) { HazelcastMemberBuilder builder = new HazelcastMemberBuilder() - .setClusterName("sonarqube") .setNetworkInterface(props.nonNullValue(ProcessProperties.CLUSTER_NODE_HOST)) .setMembers(asList(props.nonNullValue(ProcessProperties.CLUSTER_HOSTS).split(","))) .setNodeType(NodeType.parse(props.nonNullValue(ProcessProperties.CLUSTER_NODE_TYPE))) diff --git a/server/sonar-main/src/test/java/org/sonar/application/cluster/ClusterAppStateImplTest.java b/server/sonar-main/src/test/java/org/sonar/application/cluster/ClusterAppStateImplTest.java index ba85353df39..7af7c97e83f 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/cluster/ClusterAppStateImplTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/cluster/ClusterAppStateImplTest.java @@ -141,7 +141,6 @@ public class ClusterAppStateImplTest { return new HazelcastMemberBuilder() .setNodeType(NodeType.APPLICATION) .setProcessId(ProcessId.COMPUTE_ENGINE) - .setClusterName("foo") .setNodeName("bar") .setPort(NetworkUtilsImpl.INSTANCE.getNextAvailablePort(loopback)) .setNetworkInterface(loopback.getHostAddress()) diff --git a/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/HazelcastMemberBuilder.java b/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/HazelcastMemberBuilder.java index 55a5dd0a4ed..73ba63abfa4 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/HazelcastMemberBuilder.java +++ b/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/HazelcastMemberBuilder.java @@ -41,7 +41,6 @@ import static java.util.Objects.requireNonNull; public class HazelcastMemberBuilder { - private String clusterName; private String nodeName; private int port; private NodeType nodeType; @@ -49,11 +48,6 @@ public class HazelcastMemberBuilder { private String networkInterface; private List members = new ArrayList<>(); - public HazelcastMemberBuilder setClusterName(String s) { - this.clusterName = s; - return this; - } - public HazelcastMemberBuilder setNodeName(String s) { this.nodeName = s; return this; @@ -97,7 +91,11 @@ public class HazelcastMemberBuilder { public HazelcastMember build() { Config config = new Config(); - config.getGroupConfig().setName(requireNonNull(clusterName, "Cluster name is missing")); + // do not use the value defined by property sonar.cluster.name. + // Hazelcast does not fail when joining a cluster with different name. + // Apparently this behavior exists since Hazelcast 3.8.2 (see note + // at http://docs.hazelcast.org/docs/3.8.6/manual/html-single/index.html#creating-cluster-groups) + config.getGroupConfig().setName("SonarQube"); // Configure network NetworkConfig netConfig = config.getNetworkConfig(); diff --git a/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberBuilderTest.java b/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberBuilderTest.java index 6a4973b8d13..b01223d7616 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberBuilderTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberBuilderTest.java @@ -47,7 +47,6 @@ public class HazelcastMemberBuilderTest { HazelcastMember member = underTest .setNodeType(NodeType.APPLICATION) .setProcessId(ProcessId.COMPUTE_ENGINE) - .setClusterName("foo") .setNodeName("bar") .setPort(NetworkUtilsImpl.INSTANCE.getNextAvailablePort(loopback)) .setNetworkInterface(loopback.getHostAddress()) diff --git a/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberImplTest.java b/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberImplTest.java index 4d83a82f233..3bf649baecd 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberImplTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberImplTest.java @@ -114,7 +114,6 @@ public class HazelcastMemberImplTest { return new HazelcastMemberBuilder() .setNodeType(NodeType.APPLICATION) .setProcessId(ProcessId.COMPUTE_ENGINE) - .setClusterName("foo") .setNodeName("name" + port) .setPort(port) .setNetworkInterface(loopback.getHostAddress()) diff --git a/server/sonar-server/src/main/java/org/sonar/server/cluster/StartableHazelcastMember.java b/server/sonar-server/src/main/java/org/sonar/server/cluster/StartableHazelcastMember.java index 1e430e17d29..7bf6c82d487 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/cluster/StartableHazelcastMember.java +++ b/server/sonar-server/src/main/java/org/sonar/server/cluster/StartableHazelcastMember.java @@ -140,7 +140,6 @@ public class StartableHazelcastMember implements HazelcastMember, Startable { throw new IllegalStateException(format("Can not resolve address %s", networkAddress), e); } this.member = new HazelcastMemberBuilder() - .setClusterName(config.get(ProcessProperties.CLUSTER_NAME).orElseThrow(() -> new IllegalStateException("Missing cluster name"))) .setNodeName(config.get(ProcessProperties.CLUSTER_NODE_NAME).orElseThrow(() -> new IllegalStateException("Missing node name"))) .setNodeType(NodeType.parse(config.get(CLUSTER_NODE_TYPE).orElseThrow(() -> new IllegalStateException("Missing node type")))) .setPort(freePort) diff --git a/tests/src/test/java/org/sonarqube/tests/cluster/ClusterTest.java b/tests/src/test/java/org/sonarqube/tests/cluster/ClusterTest.java index 169e9a2caad..b7221c8bb03 100644 --- a/tests/src/test/java/org/sonarqube/tests/cluster/ClusterTest.java +++ b/tests/src/test/java/org/sonarqube/tests/cluster/ClusterTest.java @@ -232,6 +232,24 @@ public class ClusterTest { } } + @Test + public void cluster_name_can_be_overridden() throws Exception { + try (Cluster cluster = new Cluster("foo")) { + NodeConfig searchConfig1 = newSearchConfig("Search 1"); + NodeConfig searchConfig2 = newSearchConfig("Search 2"); + NodeConfig appConfig = newApplicationConfig("App 1"); + NodeConfig.interconnectBus(searchConfig1, searchConfig2, appConfig); + NodeConfig.interconnectSearch(searchConfig1, searchConfig2, appConfig); + + cluster.startNode(searchConfig1, nothing()); + cluster.startNode(searchConfig2, nothing()); + cluster.startNode(appConfig, nothing()); + + Node appNode = cluster.getAppNode(0); + appNode.waitForStatusUp(); + } + } + @Test public void node_fails_to_join_cluster_if_different_cluster_name() throws Exception { try (Cluster cluster = new Cluster("foo")) { -- 2.39.5