void registerSonarQubeVersion(String sonarqubeVersion);
+ String getLeaderHostName();
+
@Override
void close();
}
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 {
// Nothing to do on non clustered version
}
+ @Override
+ public String getLeaderHostName() {
+ return NetworkUtils.getHostName();
+ }
+
@Override
public void close() {
// nothing to do
tryToStartProcess(process, () -> javaCommandFactory.createWebCommand(false));
} else if (appState.tryToLockWebLeader()) {
tryToStartProcess(process, () -> javaCommandFactory.createWebCommand(true));
+ } else {
+ LOG.info("Waiting for initialization from " + appState.getLeaderHostName());
}
}
hazelcastCluster.registerSonarQubeVersion(sonarqubeVersion);
}
+ @Override
+ public String getLeaderHostName() {
+ return hazelcastCluster.getLeaderHostName();
+ }
+
HazelcastCluster getHazelcastCluster() {
return hazelcastCluster;
}
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;
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) {
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 {
// nothing to do
}
+ @Override
+ public String getLeaderHostName() {
+ return NetworkUtils.getHostName();
+ }
+
@Override
public void close() {
// nothing to do
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;
}
}
+ @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());