diff options
author | Zipeng WU <zipeng.wu@sonarsource.com> | 2021-04-13 09:14:07 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-04-15 20:03:44 +0000 |
commit | d8130b676481993c2b258ea326a5778356d93085 (patch) | |
tree | fb6ee119dc0f4920a700a29e0603b7025235d7c8 /server/sonar-main/src | |
parent | 4e232156aab47f195c3558757df2b87a7153cfea (diff) | |
download | sonarqube-d8130b676481993c2b258ea326a5778356d93085.tar.gz sonarqube-d8130b676481993c2b258ea326a5778356d93085.zip |
SONAR-14625 Upgrade to Hazelcast 4.2
Diffstat (limited to 'server/sonar-main/src')
6 files changed, 58 insertions, 36 deletions
diff --git a/server/sonar-main/src/main/java/org/sonar/application/cluster/AppNodesClusterHostsConsistency.java b/server/sonar-main/src/main/java/org/sonar/application/cluster/AppNodesClusterHostsConsistency.java index 358facf4032..49e32144f66 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/cluster/AppNodesClusterHostsConsistency.java +++ b/server/sonar-main/src/main/java/org/sonar/application/cluster/AppNodesClusterHostsConsistency.java @@ -20,10 +20,10 @@ package org.sonar.application.cluster; import com.google.common.annotations.VisibleForTesting; +import com.hazelcast.cluster.Address; +import com.hazelcast.cluster.Member; +import com.hazelcast.cluster.MemberSelector; import com.hazelcast.cluster.memberselector.MemberSelectors; -import com.hazelcast.core.Member; -import com.hazelcast.core.MemberSelector; -import com.hazelcast.nio.Address; import java.util.Arrays; import java.util.Collections; import java.util.List; diff --git a/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterAppStateImpl.java b/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterAppStateImpl.java index 76f30de035c..b2f7ab578e6 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterAppStateImpl.java +++ b/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterAppStateImpl.java @@ -19,21 +19,21 @@ */ package org.sonar.application.cluster; +import com.hazelcast.cluster.Member; +import com.hazelcast.cluster.MembershipEvent; +import com.hazelcast.cluster.MembershipListener; import com.hazelcast.core.EntryEvent; import com.hazelcast.core.EntryListener; import com.hazelcast.core.HazelcastInstanceNotActiveException; -import com.hazelcast.core.IAtomicReference; -import com.hazelcast.core.MapEvent; -import com.hazelcast.core.Member; -import com.hazelcast.core.MemberAttributeEvent; -import com.hazelcast.core.MembershipEvent; -import com.hazelcast.core.MembershipListener; -import com.hazelcast.core.ReplicatedMap; +import com.hazelcast.cp.IAtomicReference; +import com.hazelcast.map.MapEvent; +import com.hazelcast.replicatedmap.ReplicatedMap; import java.util.ArrayList; import java.util.EnumMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.UUID; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,8 +63,8 @@ public class ClusterAppStateImpl implements ClusterAppState { private final List<AppStateListener> listeners = new ArrayList<>(); private final Map<ProcessId, Boolean> operationalLocalProcesses = new EnumMap<>(ProcessId.class); private final ReplicatedMap<ClusterProcess, Boolean> operationalProcesses; - private final String operationalProcessListenerUUID; - private final String nodeDisconnectedListenerUUID; + private final UUID operationalProcessListenerUUID; + private final UUID nodeDisconnectedListenerUUID; private final EsConnector esConnector; private HealthStateSharing healthStateSharing = null; @@ -120,7 +120,7 @@ public class ClusterAppStateImpl implements ClusterAppState { @Override public boolean tryToLockWebLeader() { - IAtomicReference<String> leader = hzMember.getAtomicReference(LEADER); + IAtomicReference<UUID> leader = hzMember.getAtomicReference(LEADER); return leader.compareAndSet(null, hzMember.getUuid()); } @@ -159,9 +159,9 @@ public class ClusterAppStateImpl implements ClusterAppState { @Override public Optional<String> getLeaderHostName() { - String leaderId = (String) hzMember.getAtomicReference(LEADER).get(); - if (leaderId != null) { - Optional<Member> leader = hzMember.getCluster().getMembers().stream().filter(m -> m.getUuid().equals(leaderId)).findFirst(); + UUID leaderUuid = (UUID) hzMember.getAtomicReference(LEADER).get(); + if (leaderUuid != null) { + Optional<Member> leader = hzMember.getCluster().getMembers().stream().filter(m -> m.getUuid().equals(leaderUuid)).findFirst(); if (leader.isPresent()) { return Optional.of(leader.get().getAddress().getHost()); } @@ -239,6 +239,11 @@ public class ClusterAppStateImpl implements ClusterAppState { public void mapEvicted(MapEvent event) { // Ignore it } + + @Override + public void entryExpired(EntryEvent<ClusterProcess, Boolean> event) { + // Ignore it + } } private class NodeDisconnectedListener implements MembershipListener { @@ -252,12 +257,7 @@ public class ClusterAppStateImpl implements ClusterAppState { removeOperationalProcess(membershipEvent.getMember().getUuid()); } - @Override - public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) { - // Nothing to do - } - - private void removeOperationalProcess(String uuid) { + private void removeOperationalProcess(UUID uuid) { for (ClusterProcess clusterProcess : operationalProcesses.keySet()) { if (clusterProcess.getNodeUuid().equals(uuid)) { LOGGER.debug("Set node process off for [{}:{}] : ", clusterProcess.getNodeUuid(), clusterProcess.getProcessId()); diff --git a/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterProcess.java b/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterProcess.java index cf7859c7b16..d48591bb056 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterProcess.java +++ b/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterProcess.java @@ -21,15 +21,16 @@ package org.sonar.application.cluster; import java.io.Serializable; import java.util.Objects; +import java.util.UUID; import org.sonar.process.ProcessId; import static java.util.Objects.requireNonNull; public class ClusterProcess implements Serializable { private final ProcessId processId; - private final String nodeUuid; + private final UUID nodeUuid; - public ClusterProcess(String nodeUuid, ProcessId processId) { + public ClusterProcess(UUID nodeUuid, ProcessId processId) { this.processId = requireNonNull(processId); this.nodeUuid = requireNonNull(nodeUuid); } @@ -38,7 +39,7 @@ public class ClusterProcess implements Serializable { return processId; } - public String getNodeUuid() { + public UUID getNodeUuid() { return nodeUuid; } diff --git a/server/sonar-main/src/test/java/org/sonar/application/cluster/AppNodesClusterHostsConsistencyTest.java b/server/sonar-main/src/test/java/org/sonar/application/cluster/AppNodesClusterHostsConsistencyTest.java index cdae656ebd7..0107499267a 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/cluster/AppNodesClusterHostsConsistencyTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/cluster/AppNodesClusterHostsConsistencyTest.java @@ -20,11 +20,11 @@ package org.sonar.application.cluster; import com.google.common.collect.ImmutableMap; -import com.hazelcast.core.Cluster; -import com.hazelcast.core.IAtomicReference; -import com.hazelcast.core.Member; -import com.hazelcast.core.MemberSelector; -import com.hazelcast.nio.Address; +import com.hazelcast.cluster.Address; +import com.hazelcast.cluster.Cluster; +import com.hazelcast.cluster.Member; +import com.hazelcast.cluster.MemberSelector; +import com.hazelcast.cp.IAtomicReference; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; @@ -33,6 +33,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.UUID; import java.util.concurrent.locks.Lock; import java.util.function.Consumer; import org.junit.After; @@ -164,12 +165,12 @@ public class AppNodesClusterHostsConsistencyTest { } @Override - public String getUuid() { + public UUID getUuid() { throw new IllegalStateException("not expected to be called"); } @Override - public Set<String> getMemberUuids() { + public Set<UUID> getMemberUuids() { throw new IllegalStateException("not expected to be called"); } 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 eef179ed076..1501f6f6243 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 @@ -20,6 +20,7 @@ package org.sonar.application.cluster; import java.net.InetAddress; +import java.util.Optional; import org.junit.Rule; import org.junit.Test; import org.junit.rules.DisableOnDebug; @@ -145,6 +146,23 @@ public class ClusterAppStateImplTest { } } + @Test + public void return_hostname_if_node_is_leader() { + try (ClusterAppStateImpl underTest = createClusterAppState()) { + underTest.tryToLockWebLeader(); + Optional<String> hostname = underTest.getLeaderHostName(); + assertThat(hostname).isNotEmpty(); + } + } + + @Test + public void return_null_if_node_is_not_leader() { + try (ClusterAppStateImpl underTest = createClusterAppState()) { + Optional<String> hostname = underTest.getLeaderHostName(); + assertThat(hostname).isEmpty(); + } + } + private ClusterAppStateImpl createClusterAppState() { return new ClusterAppStateImpl(new TestAppSettings(), newHzMember(), mock(EsConnector.class), mock(AppNodesClusterHostsConsistency.class)); } diff --git a/server/sonar-main/src/test/java/org/sonar/application/cluster/ClusterProcessTest.java b/server/sonar-main/src/test/java/org/sonar/application/cluster/ClusterProcessTest.java index 2b7586a2403..38d5a862161 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/cluster/ClusterProcessTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/cluster/ClusterProcessTest.java @@ -19,6 +19,7 @@ */ package org.sonar.application.cluster; +import java.util.UUID; import org.junit.Test; import org.sonar.process.ProcessId; @@ -27,13 +28,14 @@ import static org.assertj.core.api.Assertions.assertThat; public class ClusterProcessTest { @Test public void test_equality() { - ClusterProcess clusterProcess = new ClusterProcess("A", ProcessId.WEB_SERVER); + UUID nodeUuid = UUID.randomUUID(); + ClusterProcess clusterProcess = new ClusterProcess(nodeUuid, ProcessId.WEB_SERVER); assertThat(clusterProcess) .isNotEqualTo(null) .isEqualTo(clusterProcess) - .isNotEqualTo(new ClusterProcess("B", ProcessId.WEB_SERVER)) - .isNotEqualTo(new ClusterProcess("A", ProcessId.ELASTICSEARCH)) - .isEqualTo(new ClusterProcess("A", ProcessId.WEB_SERVER)); + .isNotEqualTo(new ClusterProcess(UUID.randomUUID(), ProcessId.WEB_SERVER)) + .isNotEqualTo(new ClusterProcess(nodeUuid, ProcessId.ELASTICSEARCH)) + .isEqualTo(new ClusterProcess(nodeUuid, ProcessId.WEB_SERVER)); } } |