diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-08-25 11:54:26 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-09-13 15:50:49 +0200 |
commit | 80269a5d22553335f22f3e8a8a5840724fb154f9 (patch) | |
tree | df2a88bb4ae61251040499ec77f2d217246512a1 /server/sonar-ce | |
parent | 904cd8b327790c31d1abe0bceb0007015e74dfdf (diff) | |
download | sonarqube-80269a5d22553335f22f3e8a8a5840724fb154f9.tar.gz sonarqube-80269a5d22553335f22f3e8a8a5840724fb154f9.zip |
SONAR-9741 move shared cluster related classes to sonar-cluster
Diffstat (limited to 'server/sonar-ce')
10 files changed, 22 insertions, 608 deletions
diff --git a/server/sonar-ce/pom.xml b/server/sonar-ce/pom.xml index 007270e7793..03472a98f01 100644 --- a/server/sonar-ce/pom.xml +++ b/server/sonar-ce/pom.xml @@ -12,11 +12,16 @@ <dependencies> <dependency> - <groupId>org.sonarsource.sonarqube</groupId> + <groupId>${project.groupId}</groupId> <artifactId>sonar-server</artifactId> <scope>provided</scope> </dependency> <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>sonar-cluster</artifactId> + <scope>provided</scope> + </dependency> + <dependency> <groupId>org.nanohttpd</groupId> <artifactId>nanohttpd</artifactId> </dependency> @@ -30,10 +35,6 @@ <artifactId>jsr305</artifactId> <scope>provided</scope> </dependency> - <dependency> - <groupId>com.hazelcast</groupId> - <artifactId>hazelcast-client</artifactId> - </dependency> <!-- unit tests --> <dependency> diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/CeDistributedInformationImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/CeDistributedInformationImpl.java index cfb52e7cc95..87436b26d1c 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/CeDistributedInformationImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/CeDistributedInformationImpl.java @@ -24,12 +24,12 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.locks.Lock; import org.picocontainer.Startable; -import org.sonar.ce.cluster.HazelcastClientWrapper; import org.sonar.ce.taskprocessor.CeWorkerFactory; -import org.sonar.process.cluster.ClusterObjectKeys; +import org.sonar.cluster.ClusterObjectKeys; +import org.sonar.cluster.localclient.HazelcastClientWrapper; +import static org.sonar.cluster.ClusterObjectKeys.WORKER_UUIDS; import static org.sonar.core.util.stream.MoreCollectors.toSet; -import static org.sonar.process.cluster.ClusterObjectKeys.WORKER_UUIDS; /** * Provide the set of worker's UUID in a clustered SonarQube instance diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/cluster/HazelcastClientWrapper.java b/server/sonar-ce/src/main/java/org/sonar/ce/cluster/HazelcastClientWrapper.java deleted file mode 100644 index a9ee247cd39..00000000000 --- a/server/sonar-ce/src/main/java/org/sonar/ce/cluster/HazelcastClientWrapper.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.ce.cluster; - -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.locks.Lock; - -/** - * The interface Hazelcast client wrapper. - */ -public interface HazelcastClientWrapper { - /** - * Gets the set shared by the cluster and identified by name - */ - <E> Set<E> getSet(String name); - - /** - * Gets the list shared by the cluster and identified by name - */ - <E> List<E> getList(String name); - - /** - * Gets the map shared by the cluster and identified by name - */ - <K, V> Map<K, V> getMap(String name); - - /** - * Gets the replicated map shared by the cluster and identified by name - */ - <K,V> Map<K,V> getReplicatedMap(String name); - - /** - * Retrieve the local UUID - */ - String getClientUUID(); - - /** - * Retrieve the Set of connected clients. - * The client is only CE for the time being - * - * @return the connected clients - */ - Set<String> getConnectedClients(); - - /** - * Gets lock among the cluster, identified by name - */ - Lock getLock(String name); -} diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/cluster/HazelcastClientWrapperImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/cluster/HazelcastClientWrapperImpl.java deleted file mode 100644 index a4b40580540..00000000000 --- a/server/sonar-ce/src/main/java/org/sonar/ce/cluster/HazelcastClientWrapperImpl.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.ce.cluster; - -import com.google.common.annotations.VisibleForTesting; -import com.hazelcast.client.HazelcastClient; -import com.hazelcast.client.config.ClientConfig; -import com.hazelcast.core.HazelcastInstance; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.locks.Lock; -import org.picocontainer.Startable; -import org.sonar.api.config.Configuration; -import org.sonar.process.ProcessProperties; - -import static com.google.common.base.Preconditions.checkState; -import static org.apache.commons.lang.StringUtils.isNotEmpty; -import static org.sonar.process.cluster.ClusterObjectKeys.CLIENT_UUIDS; - -/** - * This class will connect as a Hazelcast client to the local instance of Hazelcluster - */ -public class HazelcastClientWrapperImpl implements Startable, HazelcastClientWrapper { - - private static final String HAZELCAST_CLUSTER_NAME = "sonarqube"; - private final ClientConfig hzConfig; - - @VisibleForTesting - protected HazelcastInstance hzInstance; - - public HazelcastClientWrapperImpl(Configuration config) { - boolean clusterEnabled = config.getBoolean(ProcessProperties.CLUSTER_ENABLED).orElse(false); - String clusterLocalEndPoint = config.get(ProcessProperties.CLUSTER_LOCALENDPOINT).orElse(null); - - checkState(clusterEnabled, "Cluster is not enabled"); - checkState(isNotEmpty(clusterLocalEndPoint), "LocalEndPoint have not been set"); - - hzConfig = new ClientConfig(); - hzConfig.getGroupConfig().setName(HAZELCAST_CLUSTER_NAME); - hzConfig.getNetworkConfig().addAddress(clusterLocalEndPoint); - - // Tweak HazelCast configuration - hzConfig - // Increase the number of tries - .setProperty("hazelcast.tcp.join.port.try.count", "10") - // Don't phone home - .setProperty("hazelcast.phone.home.enabled", "false") - // Use slf4j for logging - .setProperty("hazelcast.logging.type", "slf4j"); - } - - @Override - public <E> Set<E> getSet(String name) { - return hzInstance.getSet(name); - } - - @Override - public <E> List<E> getList(String name) { - return hzInstance.getList(name); - } - - @Override - public <K, V> Map<K, V> getMap(String name) { - return hzInstance.getMap(name); - } - - @Override - public <K, V> Map<K, V> getReplicatedMap(String name) { - return hzInstance.getReplicatedMap(name); - } - - @Override - public String getClientUUID() { - return hzInstance.getLocalEndpoint().getUuid(); - } - - @Override - public Set<String> getConnectedClients() { - return hzInstance.getSet(CLIENT_UUIDS); - } - - @Override - public Lock getLock(String name) { - return hzInstance.getLock(name); - } - - @Override - public void start() { - this.hzInstance = HazelcastClient.newHazelcastClient(hzConfig); - } - - @Override - public void stop() { - // Shutdown Hazelcast properly - hzInstance.shutdown(); - } -} diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/cluster/package-info.java b/server/sonar-ce/src/main/java/org/sonar/ce/cluster/package-info.java deleted file mode 100644 index 42c5c45b237..00000000000 --- a/server/sonar-ce/src/main/java/org/sonar/ce/cluster/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.ce.cluster; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java index 0536be88443..d05761ecd7d 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java @@ -46,7 +46,6 @@ import org.sonar.ce.CeQueueModule; import org.sonar.ce.CeTaskCommonsModule; import org.sonar.ce.StandaloneCeDistributedInformation; import org.sonar.ce.cleaning.CeCleaningModule; -import org.sonar.ce.cluster.HazelcastClientWrapperImpl; import org.sonar.ce.db.ReadOnlyPropertiesDao; import org.sonar.ce.log.CeProcessLogging; import org.sonar.ce.platform.ComputeEngineExtensionInstaller; @@ -56,6 +55,7 @@ import org.sonar.ce.settings.ProjectConfigurationFactory; import org.sonar.ce.taskprocessor.CeProcessingScheduler; import org.sonar.ce.taskprocessor.CeTaskProcessorModule; import org.sonar.ce.user.CeUserSession; +import org.sonar.cluster.localclient.HazelcastClientWrapperImpl; import org.sonar.core.component.DefaultResourceTypes; import org.sonar.core.config.ConfigurationProvider; import org.sonar.core.config.CorePropertyDefinitions; diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/CeDistributedInformationImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/CeDistributedInformationImplTest.java index 6bb5387cf7d..74b34822f6a 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/CeDistributedInformationImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/CeDistributedInformationImplTest.java @@ -26,14 +26,14 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; import org.junit.Test; -import org.sonar.ce.cluster.HazelcastClientWrapperImpl; import org.sonar.ce.taskprocessor.CeWorkerFactory; +import org.sonar.cluster.localclient.HazelcastClientWrapperImpl; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.data.MapEntry.entry; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static org.sonar.process.cluster.ClusterObjectKeys.WORKER_UUIDS; +import static org.sonar.cluster.ClusterObjectKeys.WORKER_UUIDS; public class CeDistributedInformationImplTest { private String clientUUID1 = "1"; diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/cluster/HazelcastClientWrapperImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/cluster/HazelcastClientWrapperImplTest.java deleted file mode 100644 index 58be787c69f..00000000000 --- a/server/sonar-ce/src/test/java/org/sonar/ce/cluster/HazelcastClientWrapperImplTest.java +++ /dev/null @@ -1,287 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.ce.cluster; - -import ch.qos.logback.classic.LoggerContext; -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.AppenderBase; -import com.google.common.collect.ImmutableSet; -import com.hazelcast.client.impl.HazelcastClientInstanceImpl; -import com.hazelcast.client.impl.HazelcastClientProxy; -import com.hazelcast.core.Client; -import com.hazelcast.core.ClientListener; -import com.hazelcast.core.HazelcastInstance; -import java.net.InetAddress; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import org.apache.commons.lang.RandomStringUtils; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.DisableOnDebug; -import org.junit.rules.ExpectedException; -import org.junit.rules.TestRule; -import org.junit.rules.Timeout; -import org.slf4j.LoggerFactory; -import org.sonar.api.config.PropertyDefinitions; -import org.sonar.api.config.internal.MapSettings; -import org.sonar.process.NetworkUtils; -import org.sonar.process.ProcessProperties; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.data.MapEntry.entry; - -public class HazelcastClientWrapperImplTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Rule - public TestRule safeguardTimeout = new DisableOnDebug(Timeout.seconds(60)); - - private static HazelcastInstance hzCluster; - private static HazelcastClientWrapperImpl hzClient; - - @BeforeClass - public static void setupHazelcastClusterAndHazelcastClient() { - int port = NetworkUtils.getNextAvailablePort(InetAddress.getLoopbackAddress()); - hzCluster = HazelcastTestHelper.createHazelcastCluster(port); - - MapSettings settings = createClusterSettings("localhost:" + port); - hzClient = new HazelcastClientWrapperImpl(settings.asConfig()); - } - - @AfterClass - public static void stopHazelcastClusterAndHazelcastClient() { - try { - hzClient.stop(); - } catch (Exception e) { - // Ignore it - } - try { - hzCluster.shutdown(); - } catch (Exception e) { - // Ignore it - } - } - - @Test - public void start_throws_ISE_if_LOCALENDPOINT_is_incorrect() { - MapSettings settings = createClusterSettings("\u4563\u1432\u1564"); - HazelcastClientWrapperImpl hzClient = new HazelcastClientWrapperImpl(settings.asConfig()); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Unable to connect to any address in the config! The following addresses were tried:"); - - hzClient.start(); - } - - @Test - public void constructor_throws_ISE_if_LOCALENDPOINT_is_empty() { - MapSettings settings = createClusterSettings(""); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("LocalEndPoint have not been set"); - - new HazelcastClientWrapperImpl(settings.asConfig()); - } - - @Test - public void constructor_throws_ISE_if_CLUSTER_ENABLED_is_false() { - MapSettings settings = createClusterSettings("localhost:9003"); - settings.setProperty(ProcessProperties.CLUSTER_ENABLED, false); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Cluster is not enabled"); - - new HazelcastClientWrapperImpl(settings.asConfig()); - } - - @Test - public void constructor_throws_ISE_if_missing_CLUSTER_ENABLED() { - MapSettings settings = createClusterSettings("localhost:9003"); - settings.removeProperty(ProcessProperties.CLUSTER_ENABLED); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Cluster is not enabled"); - - new HazelcastClientWrapperImpl(settings.asConfig()); - } - - @Test - public void constructor_throws_ISE_if_missing_CLUSTER_LOCALENDPOINT() { - MapSettings settings = createClusterSettings("localhost:9003"); - settings.removeProperty(ProcessProperties.CLUSTER_LOCALENDPOINT); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("LocalEndPoint have not been set"); - - new HazelcastClientWrapperImpl(settings.asConfig()); - } - - @Test - public void client_must_connect_to_hazelcast() throws InterruptedException { - int port = NetworkUtils.getNextAvailablePort(InetAddress.getLoopbackAddress()); - // Launch a fake Hazelcast instance - HazelcastInstance hzInstance = HazelcastTestHelper.createHazelcastCluster(port); - MapSettings settings = createClusterSettings("localhost:" + port); - - HazelcastClientWrapperImpl hazelcastClientWrapperImpl = new HazelcastClientWrapperImpl(settings.asConfig()); - ClientListenerImpl clientListener = new ClientListenerImpl(); - hzInstance.getClientService().addClientListener(clientListener); - try { - hazelcastClientWrapperImpl.start(); - clientListener.counter.await(5, TimeUnit.SECONDS); - assertThat(hazelcastClientWrapperImpl.getConnectedClients()).hasSize(1); - assertThat(hazelcastClientWrapperImpl.getClientUUID()).isNotEmpty(); - } finally { - hazelcastClientWrapperImpl.stop(); - } - } - - @Test - public void client_must_be_able_to_set_ReplicatedMap_objects() throws InterruptedException { - hzClient.start(); - try { - - Set<String> setTest = new HashSet<>(); - setTest.addAll( - Arrays.asList(RandomStringUtils.randomAlphanumeric(10), RandomStringUtils.randomAlphanumeric(10))); - Map<String, Set<String>> replicatedMap = hzClient.getReplicatedMap("TEST1"); - replicatedMap.put("KEY1", ImmutableSet.copyOf(setTest)); - assertThat(hzCluster.getReplicatedMap("TEST1")) - .containsOnlyKeys("KEY1"); - assertThat(hzCluster.getReplicatedMap("TEST1").get("KEY1")) - .isEqualTo(setTest); - } finally { - hzClient.stop(); - } - } - - @Test - public void client_must_be_able_to_retrieve_Set_objects() { - hzClient.start(); - try { - - // Set - Set<String> setTest = new HashSet<>(); - setTest.addAll(Arrays.asList("8", "9")); - hzCluster.getSet("TEST1").addAll(setTest); - assertThat(hzClient.getSet("TEST1")).containsAll(setTest); - } finally { - hzClient.stop(); - } - } - - @Test - public void client_must_be_able_to_retrieve_List_objects() { - hzClient.start(); - try { - - // List - List<String> listTest = Arrays.asList("1", "2"); - hzCluster.getList("TEST2").addAll(listTest); - assertThat(hzClient.getList("TEST2")).containsAll(listTest); - } finally { - hzClient.stop(); - } - } - - @Test - public void client_must_be_able_to_retrieve_Map_objects() { - hzClient.start(); - try { - Map mapTest = new HashMap<>(); - mapTest.put("a", Arrays.asList("123", "456")); - hzCluster.getMap("TEST3").putAll(mapTest); - assertThat(hzClient.getMap("TEST3")).containsExactly( - entry("a", Arrays.asList("123", "456"))); - } finally { - hzClient.stop(); - } - } - - @Test - public void configuration_tweaks_of_hazelcast_must_be_present() { - hzClient.start(); - try { - HazelcastClientInstanceImpl realClient = ((HazelcastClientProxy) hzClient.hzInstance).client; - assertThat(realClient.getClientConfig().getProperty("hazelcast.tcp.join.port.try.count")).isEqualTo("10"); - assertThat(realClient.getClientConfig().getProperty("hazelcast.phone.home.enabled")).isEqualTo("false"); - assertThat(realClient.getClientConfig().getProperty("hazelcast.logging.type")).isEqualTo("slf4j"); - } finally { - hzClient.stop(); - } - } - - @Test - public void hazelcast_client_must_log_through_sl4fj() { - MemoryAppender<ILoggingEvent> memoryAppender = new MemoryAppender<>(); - LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); - lc.reset(); - memoryAppender.setContext(lc); - memoryAppender.start(); - lc.getLogger("com.hazelcast").addAppender(memoryAppender); - - hzClient.start(); - hzClient.stop(); - memoryAppender.stop(); - assertThat(memoryAppender.events).isNotEmpty(); - memoryAppender.events.stream().forEach( - e -> assertThat(e.getLoggerName()).startsWith("com.hazelcast")); - } - - private class ClientListenerImpl implements ClientListener { - CountDownLatch counter = new CountDownLatch(1); - - @Override - public void clientConnected(Client client) { - counter.countDown(); - } - - @Override - public void clientDisconnected(Client client) { - - } - } - - private static MapSettings createClusterSettings(String localEndPoint) { - return new MapSettings(new PropertyDefinitions()) - .setProperty(ProcessProperties.CLUSTER_LOCALENDPOINT, localEndPoint) - .setProperty(ProcessProperties.CLUSTER_ENABLED, "true"); - } - - private class MemoryAppender<E> extends AppenderBase<E> { - private final List<E> events = new ArrayList(); - - @Override - protected void append(E eventObject) { - events.add(eventObject); - } - } -} diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/cluster/HazelcastTestHelper.java b/server/sonar-ce/src/test/java/org/sonar/ce/cluster/HazelcastTestHelper.java deleted file mode 100644 index 5774dbce735..00000000000 --- a/server/sonar-ce/src/test/java/org/sonar/ce/cluster/HazelcastTestHelper.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.ce.cluster; - -import com.hazelcast.config.Config; -import com.hazelcast.config.JoinConfig; -import com.hazelcast.config.NetworkConfig; -import com.hazelcast.core.Client; -import com.hazelcast.core.ClientListener; -import com.hazelcast.core.Hazelcast; -import com.hazelcast.core.HazelcastInstance; -import java.net.InetAddress; - -import static org.sonar.process.NetworkUtils.getHostname; -import static org.sonar.process.cluster.ClusterObjectKeys.CLIENT_UUIDS; - -public class HazelcastTestHelper { - - public static HazelcastInstance createHazelcastCluster(int port) { - Config hzConfig = new Config(); - hzConfig.getGroupConfig().setName("sonarqube"); - - // Configure the network instance - NetworkConfig netConfig = hzConfig.getNetworkConfig(); - netConfig - .setPort(port) - .setReuseAddress(true); - - netConfig.getInterfaces() - .setEnabled(true) - .addInterface(InetAddress.getLoopbackAddress().getHostAddress()); - - // Only allowing TCP/IP configuration - JoinConfig joinConfig = netConfig.getJoin(); - joinConfig.getAwsConfig().setEnabled(false); - joinConfig.getMulticastConfig().setEnabled(false); - joinConfig.getTcpIpConfig().setEnabled(true); - - // Tweak HazelCast configuration - hzConfig - // Increase the number of tries - .setProperty("hazelcast.tcp.join.port.try.count", "10") - // Don't bind on all interfaces - .setProperty("hazelcast.socket.bind.any", "false") - // Don't phone home - .setProperty("hazelcast.phone.home.enabled", "false") - // Use slf4j for logging - .setProperty("hazelcast.logging.type", "slf4j"); - - // Trying to resolve the hostname - hzConfig.getMemberAttributeConfig().setStringAttribute("HOSTNAME", getHostname()); - - // We are not using the partition group of Hazelcast, so disabling it - hzConfig.getPartitionGroupConfig().setEnabled(false); - HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance(hzConfig); - hzInstance.getClientService().addClientListener(new ConnectedClientListener(hzInstance)); - return hzInstance; - } - - private static class ConnectedClientListener implements ClientListener { - private final HazelcastInstance hzInstance; - - private ConnectedClientListener(HazelcastInstance hzInstance) { - this.hzInstance = hzInstance; - } - - @Override - public void clientConnected(Client client) { - hzInstance.getSet(CLIENT_UUIDS).add(client.getUuid()); - } - - @Override - public void clientDisconnected(Client client) { - hzInstance.getSet(CLIENT_UUIDS).remove(client.getUuid()); - } - } -} diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java index 85608130f81..28b8e81f499 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java @@ -33,17 +33,17 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.picocontainer.ComponentAdapter; import org.picocontainer.MutablePicoContainer; +import org.sonar.NetworkUtils; import org.sonar.api.CoreProperties; import org.sonar.api.database.DatabaseProperties; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.System2; import org.sonar.ce.CeDistributedInformationImpl; import org.sonar.ce.StandaloneCeDistributedInformation; -import org.sonar.ce.cluster.HazelcastClientWrapperImpl; -import org.sonar.ce.cluster.HazelcastTestHelper; +import org.sonar.cluster.internal.HazelcastTestHelper; +import org.sonar.cluster.localclient.HazelcastClientWrapperImpl; import org.sonar.db.DbTester; import org.sonar.db.property.PropertyDto; -import org.sonar.process.NetworkUtils; import org.sonar.process.ProcessId; import org.sonar.process.ProcessProperties; import org.sonar.process.Props; @@ -51,6 +51,9 @@ import org.sonar.process.Props; import static java.lang.String.valueOf; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; +import static org.sonar.cluster.ClusterProperties.CLUSTER_ENABLED; +import static org.sonar.cluster.ClusterProperties.CLUSTER_LOCALENDPOINT; +import static org.sonar.cluster.ClusterProperties.CLUSTER_NODE_TYPE; import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_INDEX; import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH; import static org.sonar.process.ProcessProperties.PATH_DATA; @@ -82,12 +85,12 @@ public class ComputeEngineContainerImplTest { @Test public void real_start_with_cluster() throws IOException { int port = NetworkUtils.getNextAvailablePort(InetAddress.getLoopbackAddress()); - HazelcastInstance hzInstance = HazelcastTestHelper.createHazelcastCluster(port); + HazelcastInstance hzInstance = HazelcastTestHelper.createHazelcastCluster(NetworkUtils.getHostname(), port); Properties properties = getProperties(); - properties.setProperty(ProcessProperties.CLUSTER_NODE_TYPE, "application"); - properties.setProperty(ProcessProperties.CLUSTER_ENABLED, "true"); - properties.setProperty(ProcessProperties.CLUSTER_LOCALENDPOINT, String.format("%s:%d", hzInstance.getCluster().getLocalMember().getAddress().getHost(), port)); + properties.setProperty(CLUSTER_NODE_TYPE, "application"); + properties.setProperty(CLUSTER_ENABLED, "true"); + properties.setProperty(CLUSTER_LOCALENDPOINT, String.format("%s:%d", hzInstance.getCluster().getLocalMember().getAddress().getHost(), port)); // required persisted properties insertProperty(CoreProperties.SERVER_ID, "a_startup_id"); |