]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9002 Add a log when a node is waiting for initialization
authorEric Hartmann <hartmann.eric@gmail.Com>
Fri, 24 Mar 2017 09:23:29 +0000 (10:23 +0100)
committerGitHub <noreply@github.com>
Fri, 24 Mar 2017 09:23:29 +0000 (10:23 +0100)
server/sonar-process-monitor/src/main/java/org/sonar/application/AppState.java
server/sonar-process-monitor/src/main/java/org/sonar/application/AppStateImpl.java
server/sonar-process-monitor/src/main/java/org/sonar/application/SchedulerImpl.java
server/sonar-process-monitor/src/main/java/org/sonar/application/cluster/AppStateClusterImpl.java
server/sonar-process-monitor/src/main/java/org/sonar/application/cluster/HazelcastCluster.java
server/sonar-process-monitor/src/test/java/org/sonar/application/TestAppState.java
server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastClusterTest.java

index 212fa7b33f7c1eb5ede71f7ca1433a7684342118..cace6dfd2cf7db3282c3acb0a90f938195774738 100644 (file)
@@ -51,6 +51,8 @@ public interface AppState extends AutoCloseable {
 
   void registerSonarQubeVersion(String sonarqubeVersion);
 
+  String getLeaderHostName();
+
   @Override
   void close();
 }
index 3e748b70f37f5afe983da4f4bb7f1ccb87b863fe..5c9727457713d020b566ae19ae851c06c8ad211d 100644 (file)
@@ -25,6 +25,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicBoolean;
 import javax.annotation.Nonnull;
+import org.sonar.process.NetworkUtils;
 import org.sonar.process.ProcessId;
 
 public class AppStateImpl implements AppState {
@@ -65,6 +66,11 @@ public class AppStateImpl implements AppState {
     // Nothing to do on non clustered version
   }
 
+  @Override
+  public String getLeaderHostName() {
+    return NetworkUtils.getHostName();
+  }
+
   @Override
   public void close() {
     // nothing to do
index 8359d66e08dfb39b0c3b043b3902e6a84ccdc378..3a836f47a946bfe810b4b95585f4f7c89887b8f7 100644 (file)
@@ -117,6 +117,8 @@ public class SchedulerImpl implements Scheduler, ProcessEventListener, ProcessLi
       tryToStartProcess(process, () -> javaCommandFactory.createWebCommand(false));
     } else if (appState.tryToLockWebLeader()) {
       tryToStartProcess(process, () -> javaCommandFactory.createWebCommand(true));
+    } else {
+      LOG.info("Waiting for initialization from " + appState.getLeaderHostName());
     }
   }
 
index cae4d8805720bbf44aa526e97c8bc5305e5488b5..4b08de70890f383dcad1049d4659b2f867942e29 100644 (file)
@@ -90,6 +90,11 @@ public class AppStateClusterImpl implements AppState {
     hazelcastCluster.registerSonarQubeVersion(sonarqubeVersion);
   }
 
+  @Override
+  public String getLeaderHostName() {
+    return hazelcastCluster.getLeaderHostName();
+  }
+
   HazelcastCluster getHazelcastCluster() {
     return hazelcastCluster;
   }
index 39b4b18b98f50a95edc5514a1ab761a4cd3af7f9..3d11c63909591ab9643d4c07876b70fc321abe58 100644 (file)
@@ -30,10 +30,12 @@ import com.hazelcast.core.HazelcastInstance;
 import com.hazelcast.core.IAtomicReference;
 import com.hazelcast.core.ILock;
 import com.hazelcast.core.MapEvent;
+import com.hazelcast.core.Member;
 import com.hazelcast.core.ReplicatedMap;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import org.sonar.application.AppStateListener;
 import org.sonar.process.ProcessId;
 
@@ -196,6 +198,17 @@ public class HazelcastCluster implements AutoCloseable {
     return new HazelcastCluster(hzConfig);
   }
 
+  String getLeaderHostName() {
+    String leaderId = (String) hzInstance.getAtomicReference(LEADER).get();
+    if (leaderId != null) {
+      Optional<Member> leader = hzInstance.getCluster().getMembers().stream().filter(m -> m.getUuid().equals(leaderId)).findFirst();
+      if (leader.isPresent()) {
+        return leader.get().getStringAttribute(HOSTNAME);
+      }
+    }
+    return "No leader";
+  }
+
   private class OperationalProcessListener implements EntryListener<ClusterProcess, Boolean> {
     @Override
     public void entryAdded(EntryEvent<ClusterProcess, Boolean> event) {
index 40d4196cdd8b83df71c0117cbb18fc564d88d707..41efc5b03ba08e3e05313108c101753bda5d031d 100644 (file)
@@ -25,6 +25,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicBoolean;
 import javax.annotation.Nonnull;
+import org.sonar.process.NetworkUtils;
 import org.sonar.process.ProcessId;
 
 public class TestAppState implements AppState {
@@ -75,6 +76,11 @@ public class TestAppState implements AppState {
     // nothing to do
   }
 
+  @Override
+  public String getLeaderHostName() {
+    return NetworkUtils.getHostName();
+  }
+
   @Override
   public void close() {
     // nothing to do
index a265ff1ea1d6ca7b50e6f2d10010a17c736c6aff..fa73e73111e3a2e875a4b054a35288be787eed4d 100644 (file)
@@ -32,6 +32,7 @@ import org.junit.rules.TestRule;
 import org.junit.rules.Timeout;
 import org.sonar.application.AppStateListener;
 import org.sonar.application.config.TestAppSettings;
+import org.sonar.process.NetworkUtils;
 import org.sonar.process.ProcessId;
 import org.sonar.process.ProcessProperties;
 
@@ -73,6 +74,23 @@ public class HazelcastClusterTest {
     }
   }
 
+  @Test
+  public void when_no_leader_getLeaderHostName_must_return_NO_LEADER() {
+    ClusterProperties clusterProperties = new ClusterProperties(newClusterSettings());
+    try (HazelcastCluster hzCluster = HazelcastCluster.create(clusterProperties)) {
+      assertThat(hzCluster.getLeaderHostName()).isEqualTo("No leader");
+    }
+  }
+
+  @Test
+  public void when_no_leader_getLeaderHostName_must_return_the_hostname() {
+    ClusterProperties clusterProperties = new ClusterProperties(newClusterSettings());
+    try (HazelcastCluster hzCluster = HazelcastCluster.create(clusterProperties)) {
+      assertThat(hzCluster.tryToLockWebLeader()).isTrue();
+      assertThat(hzCluster.getLeaderHostName()).isEqualTo(NetworkUtils.getHostName());
+    }
+  }
+
   @Test
   public void members_must_be_empty_when_there_is_no_other_node() {
     ClusterProperties clusterProperties = new ClusterProperties(newClusterSettings());