diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2021-07-23 15:29:17 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-07-29 20:04:50 +0000 |
commit | f827bdec3dd63f407ccba44fc5a8462528e0717f (patch) | |
tree | 53fd1ed5b53fd3e2c2d7a1c3c4cbe1e26893b160 /server/sonar-process | |
parent | 5dd693fb86fb9a0e6335de1c9dbabab7dbd89ce6 (diff) | |
download | sonarqube-f827bdec3dd63f407ccba44fc5a8462528e0717f.tar.gz sonarqube-f827bdec3dd63f407ccba44fc5a8462528e0717f.zip |
SONAR-15230 Hazelcast clean up
Diffstat (limited to 'server/sonar-process')
6 files changed, 23 insertions, 146 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/HazelcastMemberBuilder.java b/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/HazelcastMemberBuilder.java index 0e207d7050d..ae94f1f64dd 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/HazelcastMemberBuilder.java +++ b/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/HazelcastMemberBuilder.java @@ -24,29 +24,28 @@ import com.hazelcast.config.JoinConfig; import com.hazelcast.config.MemberAttributeConfig; import com.hazelcast.config.NetworkConfig; import com.hazelcast.core.Hazelcast; -import java.util.ArrayList; -import java.util.Collection; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.sonar.process.ProcessId; import org.sonar.process.cluster.hz.HazelcastMember.Attribute; +import static java.lang.String.format; import static java.util.Collections.singletonList; import static java.util.Objects.requireNonNull; +import static org.sonar.process.ProcessProperties.Property.CLUSTER_NODE_HZ_PORT; import static org.sonar.process.cluster.hz.JoinConfigurationType.KUBERNETES; -import static org.sonar.process.cluster.hz.JoinConfigurationType.TCP_IP; public class HazelcastMemberBuilder { private String nodeName; private int port; private ProcessId processId; private String networkInterface; - private final MembersResolver membersResolver; - private final List<String> members = new ArrayList<>(); + private String members; private final JoinConfigurationType type; public HazelcastMemberBuilder(JoinConfigurationType type) { this.type = type; - this.membersResolver = TCP_IP.equals(type) ? new TcpIpMembersResolver() : new NopMembersResolver(); } public HazelcastMemberBuilder setNodeName(String s) { @@ -75,8 +74,8 @@ public class HazelcastMemberBuilder { /** * Adds references to cluster members */ - public HazelcastMemberBuilder setMembers(Collection<String> members) { - this.members.addAll(members); + public HazelcastMemberBuilder setMembers(String members) { + this.members = members; return this; } @@ -102,14 +101,18 @@ public class HazelcastMemberBuilder { joinConfig.getAwsConfig().setEnabled(false); joinConfig.getMulticastConfig().setEnabled(false); - List<String> resolvedNodes = membersResolver.resolveMembers(this.members); if (KUBERNETES.equals(type)) { joinConfig.getKubernetesConfig().setEnabled(true) - .setProperty("service-dns", requireNonNull(resolvedNodes.get(0), "Service DNS is missing")) - .setProperty("service-port", "9003"); + .setProperty("service-dns", requireNonNull(members, "Service DNS is missing")) + .setProperty("service-port", CLUSTER_NODE_HZ_PORT.getDefaultValue()); } else { + List<String> addressesWithDefaultPorts = Stream.of(this.members.split(",")) + .filter(host -> !host.isBlank()) + .map(String::trim) + .map(HazelcastMemberBuilder::applyDefaultPortToHost) + .collect(Collectors.toList()); joinConfig.getTcpIpConfig().setEnabled(true); - joinConfig.getTcpIpConfig().setMembers(requireNonNull(resolvedNodes, "Members are missing")); + joinConfig.getTcpIpConfig().setMembers(requireNonNull(addressesWithDefaultPorts, "Members are missing")); } // We are not using the partition group of Hazelcast, so disabling it @@ -133,4 +136,8 @@ public class HazelcastMemberBuilder { return new HazelcastMemberImpl(Hazelcast.newHazelcastInstance(config)); } + private static String applyDefaultPortToHost(String host) { + return host.contains(":") ? host : format("%s:%s", host, CLUSTER_NODE_HZ_PORT.getDefaultValue()); + } + } diff --git a/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/MembersResolver.java b/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/MembersResolver.java deleted file mode 100644 index 3419c8b0bc2..00000000000 --- a/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/MembersResolver.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2021 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.process.cluster.hz; - -import java.util.List; - -interface MembersResolver { - List<String> resolveMembers(List<String> membersToResolve); -} diff --git a/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/NopMembersResolver.java b/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/NopMembersResolver.java deleted file mode 100644 index fbafb4f90f7..00000000000 --- a/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/NopMembersResolver.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2021 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.process.cluster.hz; - -import java.util.List; - -class NopMembersResolver implements MembersResolver { - @Override - public List<String> resolveMembers(List<String> membersToResolve) { - return membersToResolve; - } - -} diff --git a/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/TcpIpMembersResolver.java b/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/TcpIpMembersResolver.java deleted file mode 100644 index 89e7ef68fc0..00000000000 --- a/server/sonar-process/src/main/java/org/sonar/process/cluster/hz/TcpIpMembersResolver.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2021 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.process.cluster.hz; - -import com.hazelcast.internal.util.AddressUtil; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; - -import static java.lang.String.format; -import static org.sonar.process.ProcessProperties.Property.CLUSTER_NODE_HZ_PORT; - -class TcpIpMembersResolver implements MembersResolver { - private static final Logger LOG = Loggers.get(TcpIpMembersResolver.class); - - @Override - public List<String> resolveMembers(List<String> membersToResolve) { - return membersToResolve.stream().map(this::extractMembers).flatMap(Collection::stream).collect(Collectors.toList()); - } - - private List<String> extractMembers(String host) { - LOG.debug("Trying to add host: " + host); - String hostStripped = host.split(":")[0]; - if (AddressUtil.isIpAddress(hostStripped)) { - LOG.debug("Found ip based host config for host: " + host); - return Collections.singletonList(host.contains(":") ? host : format("%s:%s", host, CLUSTER_NODE_HZ_PORT.getDefaultValue())); - } else { - List<String> membersToAdd = new ArrayList<>(); - for (String memberIp : getAllByName(hostStripped)) { - String prefix = memberIp.split("/")[1]; - LOG.debug("Found IP for: " + hostStripped + " : " + prefix); - String memberPort = host.contains(":") ? host.split(":")[1] : CLUSTER_NODE_HZ_PORT.getDefaultValue(); - String member = prefix + ":" + memberPort; - membersToAdd.add(member); - } - return membersToAdd; - } - } - - private List<String> getAllByName(String hostname) { - LOG.debug("Trying to resolve Hostname: " + hostname); - try { - return Arrays.stream(InetAddress.getAllByName(hostname)).map(InetAddress::toString).collect(Collectors.toList()); - } catch (UnknownHostException e) { - LOG.error("Host could not be found: " + e.getMessage()); - } - return new ArrayList<>(); - } - -} diff --git a/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberBuilderTest.java b/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberBuilderTest.java index 054c05cda77..1586ef7e9e7 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberBuilderTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberBuilderTest.java @@ -43,7 +43,7 @@ public class HazelcastMemberBuilderTest { @Test public void build_tcp_ip_member_hostaddress() { HazelcastMember member = new HazelcastMemberBuilder(JoinConfigurationType.TCP_IP) - .setMembers(Collections.singletonList(loopback.getHostAddress())) + .setMembers(loopback.getHostAddress()) .setProcessId(ProcessId.COMPUTE_ENGINE) .setNodeName("bar") .setPort(NetworkUtilsImpl.INSTANCE.getNextLoopbackAvailablePort()) @@ -65,7 +65,7 @@ public class HazelcastMemberBuilderTest { @Test public void build_tcp_ip_member_hostname() { HazelcastMember member = new HazelcastMemberBuilder(JoinConfigurationType.TCP_IP) - .setMembers(Collections.singletonList(loopback.getHostName())) + .setMembers(loopback.getHostName()) .setProcessId(ProcessId.COMPUTE_ENGINE) .setNodeName("bar") .setPort(NetworkUtilsImpl.INSTANCE.getNextLoopbackAvailablePort()) @@ -87,7 +87,7 @@ public class HazelcastMemberBuilderTest { @Test public void build_kubernetes_member() { HazelcastMember member = new HazelcastMemberBuilder(JoinConfigurationType.KUBERNETES) - .setMembers(Collections.singletonList(loopback.getHostAddress())) + .setMembers(loopback.getHostAddress()) .setProcessId(ProcessId.COMPUTE_ENGINE) .setNodeName("bar") .setPort(NetworkUtilsImpl.INSTANCE.getNextLoopbackAvailablePort()) diff --git a/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberImplTest.java b/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberImplTest.java index 62202667554..eebdff9b9d3 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberImplTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/cluster/hz/HazelcastMemberImplTest.java @@ -115,7 +115,7 @@ public class HazelcastMemberImplTest { .setNodeName("name" + port) .setPort(port) .setNetworkInterface(loopback.getHostAddress()) - .setMembers(Arrays.stream(otherPorts).mapToObj(p -> loopback.getHostAddress() + ":" + p).collect(Collectors.toList())) + .setMembers(Arrays.stream(otherPorts).mapToObj(p -> loopback.getHostAddress() + ":" + p).collect(Collectors.joining(","))) .build(); } |