diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-09-05 10:38:52 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-09-13 15:50:53 +0200 |
commit | 562cc41a1db2c52337f2dc3ccf6e11387a68bc4f (patch) | |
tree | 2bad1a08dc03b16275d5ce0d442d59b14d613027 | |
parent | 4b0d1ba16eccdc9e79154f31549d437ad1be6e0b (diff) | |
download | sonarqube-562cc41a1db2c52337f2dc3ccf6e11387a68bc4f.tar.gz sonarqube-562cc41a1db2c52337f2dc3ccf6e11387a68bc4f.zip |
SONAR-9741 add HazelcastClient#getClusterTime
5 files changed, 40 insertions, 1 deletions
diff --git a/server/sonar-cluster/src/main/java/org/sonar/cluster/localclient/HazelcastClient.java b/server/sonar-cluster/src/main/java/org/sonar/cluster/localclient/HazelcastClient.java index 03689945860..0351deb2a69 100644 --- a/server/sonar-cluster/src/main/java/org/sonar/cluster/localclient/HazelcastClient.java +++ b/server/sonar-cluster/src/main/java/org/sonar/cluster/localclient/HazelcastClient.java @@ -67,4 +67,9 @@ public interface HazelcastClient { * Gets lock among the cluster, identified by name */ Lock getLock(String name); + + /** + * Retrieves the cluster time which is (alsmost) identical on all members of the cluster. + */ + long getClusterTime(); } diff --git a/server/sonar-cluster/src/main/java/org/sonar/cluster/localclient/HazelcastLocalClient.java b/server/sonar-cluster/src/main/java/org/sonar/cluster/localclient/HazelcastLocalClient.java index 4e31de83ca8..c6e137e0374 100644 --- a/server/sonar-cluster/src/main/java/org/sonar/cluster/localclient/HazelcastLocalClient.java +++ b/server/sonar-cluster/src/main/java/org/sonar/cluster/localclient/HazelcastLocalClient.java @@ -109,6 +109,11 @@ public class HazelcastLocalClient implements Startable, HazelcastClient { } @Override + public long getClusterTime() { + return hzInstance.getCluster().getClusterTime(); + } + + @Override public void start() { this.hzInstance = com.hazelcast.client.HazelcastClient.newHazelcastClient(hzConfig); } diff --git a/server/sonar-cluster/src/test/java/org/sonar/cluster/localclient/HazelcastLocalClientTest.java b/server/sonar-cluster/src/test/java/org/sonar/cluster/localclient/HazelcastLocalClientTest.java index 084941a7d0c..63727d487ef 100644 --- a/server/sonar-cluster/src/test/java/org/sonar/cluster/localclient/HazelcastLocalClientTest.java +++ b/server/sonar-cluster/src/test/java/org/sonar/cluster/localclient/HazelcastLocalClientTest.java @@ -56,6 +56,7 @@ import org.sonar.api.config.internal.MapSettings; import org.sonar.cluster.internal.HazelcastTestHelper; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.within; import static org.assertj.core.data.MapEntry.entry; import static org.sonar.cluster.ClusterProperties.CLUSTER_ENABLED; import static org.sonar.cluster.ClusterProperties.CLUSTER_LOCALENDPOINT; @@ -259,6 +260,19 @@ public class HazelcastLocalClientTest { e -> Assertions.assertThat(e.getLoggerName()).startsWith("com.hazelcast")); } + @Test + public void getClusterTime_returns_time_of_cluster() { + try { + hzClient.start(); + + HazelcastClientInstanceImpl realClient = ((HazelcastClientProxy) hzClient.hzInstance).client; + assertThat(hzClient.getClusterTime()) + .isCloseTo(realClient.getCluster().getClusterTime(), within(1000L)); + } finally { + hzClient.stop(); + } + } + private class ClientListenerImpl implements ClientListener { CountDownLatch counter = new CountDownLatch(1); 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 737953f49ec..98f279e6e1e 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 @@ -330,6 +330,11 @@ public class HazelcastCluster implements AutoCloseable { public Lock getLock(String s) { return hzInstance.getLock(s); } + + @Override + public long getClusterTime() { + return hzInstance.getCluster().getClusterTime(); + } } private class OperationalProcessListener implements EntryListener<ClusterProcess, Boolean> { diff --git a/server/sonar-main/src/test/java/org/sonar/application/cluster/HazelcastClusterTest.java b/server/sonar-main/src/test/java/org/sonar/application/cluster/HazelcastClusterTest.java index 15ecf7a1df8..73dfb54f633 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/cluster/HazelcastClusterTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/cluster/HazelcastClusterTest.java @@ -50,6 +50,7 @@ import org.sonar.process.ProcessId; import static java.lang.String.format; import static junit.framework.TestCase.fail; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.within; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; @@ -286,13 +287,21 @@ public class HazelcastClusterTest { } @Test + public void getClusterTime_returns_time_of_cluster() { + ClusterProperties clusterProperties = new ClusterProperties(newApplicationSettings()); + try (HazelcastCluster hzCluster = HazelcastCluster.create(clusterProperties)) { + assertThat(hzCluster.getHazelcastClient().getClusterTime()) + .isCloseTo(hzCluster.hzInstance.getCluster().getClusterTime(), within(1000L)); + } + } + + @Test public void removing_the_last_application_node_must_clear_web_leader() throws InterruptedException { try (ClusterAppStateImpl appStateCluster = new ClusterAppStateImpl(newSearchSettings())) { TestAppSettings appSettings = newApplicationSettings(); appSettings.set(CLUSTER_HOSTS, appStateCluster.getHazelcastCluster().getLocalEndPoint()); appSettings.set(CLUSTER_NODE_PORT, "9004"); ClusterProperties clusterProperties = new ClusterProperties(appSettings); - // Simulate a connection from an application node HazelcastCluster appNode = HazelcastCluster.create(clusterProperties); appNode.tryToLockWebLeader(); @@ -320,6 +329,7 @@ public class HazelcastClusterTest { assertThat(appStateCluster.isOperational(ProcessId.COMPUTE_ENGINE, false)).isFalse(); assertThat(appStateCluster.getHazelcastCluster().getSonarQubeVersion()).isNull(); } + } @Test |