]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6966 Implement node name for Hazelcast
authorEric Hartmann <hartmann.eric@gmail.com>
Tue, 22 Aug 2017 14:52:56 +0000 (16:52 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 5 Sep 2017 12:24:13 +0000 (14:24 +0200)
server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterProperties.java
server/sonar-main/src/main/java/org/sonar/application/cluster/HazelcastCluster.java
server/sonar-process/src/main/java/org/sonar/process/cluster/ClusterObjectKeys.java
server/sonar-process/src/test/java/org/sonar/process/es/EsSettingsTest.java

index 6d0dd9fdc38862e010208907de75169f19fca258..7dbac3b68cd6db37d3f81418c79dd227533e52ed 100644 (file)
@@ -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<String> hosts;
   private final List<String> 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(
index b17332218be25ca4dc021e7925c9fb53b7f6c90c..9cee5a621bbfb273e9558d895735f88a27248dd0 100644 (file)
@@ -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()
index 0227fcb7c773309e745d299213352b170e542656..4fd578d04290dc2c3b4ef0b4c5d91186df5c60f5 100644 (file)
@@ -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}
index edad4dadc244c3cbb8b6f77186b3eb2ef260c829..80c5128c1aa57616391628a736958dbe90bc776d 100644 (file)
@@ -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<String, String> 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();