From c6a730d384bc1febacad0cb33ae153ff5109d900 Mon Sep 17 00:00:00 2001 From: Eric Hartmann Date: Tue, 22 Aug 2017 16:52:56 +0200 Subject: [PATCH] SONAR-6966 Implement node name for Hazelcast --- .../cluster/ClusterProperties.java | 7 ++++++ .../application/cluster/HazelcastCluster.java | 3 +++ .../process/cluster/ClusterObjectKeys.java | 1 - .../org/sonar/process/es/EsSettingsTest.java | 24 ++++++++++++++++--- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterProperties.java b/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterProperties.java index 6d0dd9fdc38..7dbac3b68cd 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterProperties.java +++ b/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterProperties.java @@ -26,6 +26,7 @@ import java.net.SocketException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; +import java.util.UUID; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; @@ -46,6 +47,7 @@ public final class ClusterProperties { private final List hosts; private final List networkInterfaces; private final NodeType nodeType; + private final String nodeName; ClusterProperties(AppSettings appSettings) { port = appSettings.getProps().valueAsInt(ProcessProperties.CLUSTER_NODE_PORT); @@ -56,6 +58,7 @@ public final class ClusterProperties { appSettings.getProps().value(ProcessProperties.CLUSTER_HOSTS, "") ); nodeType = NodeType.parse(appSettings.getProps().value(ProcessProperties.CLUSTER_NODE_TYPE)); + nodeName = appSettings.getProps().value(ProcessProperties.CLUSTER_NODE_NAME, "sonarqube-" + UUID.randomUUID().toString()); } int getPort() { @@ -74,6 +77,10 @@ public final class ClusterProperties { return networkInterfaces; } + public String getNodeName() { + return nodeName; + } + void validate() { // Test validity of port checkArgument( diff --git a/server/sonar-main/src/main/java/org/sonar/application/cluster/HazelcastCluster.java b/server/sonar-main/src/main/java/org/sonar/application/cluster/HazelcastCluster.java index b17332218be..9cee5a621bb 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/cluster/HazelcastCluster.java +++ b/server/sonar-main/src/main/java/org/sonar/application/cluster/HazelcastCluster.java @@ -60,6 +60,7 @@ import static org.sonar.process.cluster.ClusterObjectKeys.CLUSTER_NAME; import static org.sonar.process.cluster.ClusterObjectKeys.HOSTNAME; import static org.sonar.process.cluster.ClusterObjectKeys.IP_ADDRESSES; import static org.sonar.process.cluster.ClusterObjectKeys.LEADER; +import static org.sonar.process.cluster.ClusterObjectKeys.NODE_NAME; import static org.sonar.process.cluster.ClusterObjectKeys.NODE_TYPE; import static org.sonar.process.cluster.ClusterObjectKeys.OPERATIONAL_PROCESSES; import static org.sonar.process.cluster.ClusterObjectKeys.SONARQUBE_VERSION; @@ -243,6 +244,8 @@ public class HazelcastCluster implements AutoCloseable { .setProperty("hazelcast.logging.type", "slf4j"); // Trying to resolve the hostname + hzConfig.getMemberAttributeConfig() + .setStringAttribute(NODE_NAME, clusterProperties.getNodeName()); hzConfig.getMemberAttributeConfig() .setStringAttribute(HOSTNAME, getHostname()); hzConfig.getMemberAttributeConfig() diff --git a/server/sonar-process/src/main/java/org/sonar/process/cluster/ClusterObjectKeys.java b/server/sonar-process/src/main/java/org/sonar/process/cluster/ClusterObjectKeys.java index 0227fcb7c77..4fd578d0429 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/cluster/ClusterObjectKeys.java +++ b/server/sonar-process/src/main/java/org/sonar/process/cluster/ClusterObjectKeys.java @@ -49,7 +49,6 @@ public final class ClusterObjectKeys { * The key of the node name attribute of a member */ public static final String NODE_NAME = "NODE_NAME"; - /** * The role of the sonar-application inside the SonarQube cluster * {@link org.sonar.process.NodeType} diff --git a/server/sonar-process/src/test/java/org/sonar/process/es/EsSettingsTest.java b/server/sonar-process/src/test/java/org/sonar/process/es/EsSettingsTest.java index edad4dadc24..80c5128c1aa 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/es/EsSettingsTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/es/EsSettingsTest.java @@ -47,7 +47,7 @@ public class EsSettingsTest { public ExpectedException expectedException = ExpectedException.none(); @Test - public void test_default_settings() throws Exception { + public void test_default_settings_for_standalone_mode() throws Exception { File homeDir = temp.newFolder(); Props props = new Props(new Properties()); props.set(ProcessProperties.SEARCH_PORT, "1234"); @@ -55,7 +55,6 @@ public class EsSettingsTest { props.set(ProcessProperties.PATH_HOME, homeDir.getAbsolutePath()); props.set(ProcessProperties.PATH_TEMP, temp.newFolder().getAbsolutePath()); props.set(ProcessProperties.CLUSTER_NAME, "sonarqube"); - props.set(ProcessProperties.CLUSTER_NODE_NAME, "node-1"); EsSettings esSettings = new EsSettings(props, new EsFileSystem(props)); @@ -65,7 +64,7 @@ public class EsSettingsTest { // no cluster, but cluster and node names are set though assertThat(generated.get("cluster.name")).isEqualTo("sonarqube"); - assertThat(generated.get("node.name")).isEqualTo("sonar-1"); + assertThat(generated.get("node.name")).isEqualTo("sonarqube"); assertThat(generated.get("path.data")).isNotNull(); assertThat(generated.get("path.logs")).isNotNull(); @@ -82,6 +81,25 @@ public class EsSettingsTest { assertThat(generated.get("action.auto_create_index")).isEqualTo("false"); } + @Test + public void test_default_settings_for_cluster_mode() throws Exception { + File homeDir = temp.newFolder(); + Props props = new Props(new Properties()); + props.set(ProcessProperties.SEARCH_PORT, "1234"); + props.set(ProcessProperties.SEARCH_HOST, "127.0.0.1"); + props.set(ProcessProperties.PATH_HOME, homeDir.getAbsolutePath()); + props.set(ProcessProperties.PATH_TEMP, temp.newFolder().getAbsolutePath()); + props.set(ProcessProperties.CLUSTER_NAME, "sonarqube-1"); + props.set(ProcessProperties.CLUSTER_ENABLED, "true"); + props.set(ProcessProperties.CLUSTER_NODE_NAME, "node-1"); + + EsSettings esSettings = new EsSettings(props, new EsFileSystem(props)); + + Map generated = esSettings.build(); + assertThat(generated.get("cluster.name")).isEqualTo("sonarqube-1"); + assertThat(generated.get("node.name")).isEqualTo("node-1"); + } + @Test public void test_node_name_default_for_cluster_mode() throws Exception { File homeDir = temp.newFolder(); -- 2.39.5