import org.sonar.process.cluster.hz.HazelcastMember;
import static java.lang.String.format;
-import static org.sonar.process.cluster.hz.HazelcastMember.Attribute.HOSTNAME;
-import static org.sonar.process.cluster.hz.HazelcastMember.Attribute.IP_ADDRESSES;
import static org.sonar.process.cluster.hz.HazelcastMember.Attribute.NODE_TYPE;
import static org.sonar.process.cluster.hz.HazelcastObjects.CLUSTER_NAME;
import static org.sonar.process.cluster.hz.HazelcastObjects.LEADER;
if (leaderId != null) {
Optional<Member> leader = hzMember.getCluster().getMembers().stream().filter(m -> m.getUuid().equals(leaderId)).findFirst();
if (leader.isPresent()) {
- return Optional.of(
- format("%s (%s)", leader.get().getStringAttribute(HOSTNAME.getKey()), leader.get().getStringAttribute(IP_ADDRESSES.getKey())));
+ return Optional.of(leader.get().getAddress().getHost());
}
}
return Optional.empty();
*/
String getHostname();
- /**
- * Identifying the IPs addresses
- *
- * @return "ipv4_1, ipv4_2"
- */
- String getIPAddresses();
-
/**
* Converts a text representation of an IP address or host name to
* a {@link InetAddress}.
import com.google.common.annotations.VisibleForTesting;
import com.google.common.net.InetAddresses;
import java.io.IOException;
-import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
-import java.util.stream.Collectors;
-
-import static java.util.Collections.list;
-import static org.apache.commons.lang.StringUtils.isBlank;
public class NetworkUtilsImpl implements NetworkUtils {
throw new IllegalStateException("Fail to find an available port on " + address, e);
}
}
-
}
@Override
return hostname;
}
- @Override
- public String getIPAddresses() {
- String ips;
-
- try {
- ips = list(NetworkInterface.getNetworkInterfaces()).stream()
- .flatMap(netif -> list(netif.getInetAddresses()).stream()
- .filter(inetAddress ->
- // Removing IPv6 for the time being
- inetAddress instanceof Inet4Address &&
- // Removing loopback addresses, useless for identifying a server
- !inetAddress.isLoopbackAddress() &&
- // Removing interfaces without IPs
- !isBlank(inetAddress.getHostAddress()))
- .map(InetAddress::getHostAddress))
- .filter(p -> !isBlank(p))
- .collect(Collectors.joining(","));
- } catch (SocketException e) {
- ips = "unresolved IPs";
- }
-
- return ips;
- }
-
@Override
public InetAddress toInetAddress(String hostOrAddress) throws UnknownHostException {
if (InetAddresses.isInetAddress(hostOrAddress)) {
public interface HazelcastMember extends AutoCloseable {
enum Attribute {
- /**
- * The key of the hostname attribute of a member
- */
- HOSTNAME("HOSTNAME"),
- /**
- * The key of the ips list attribute of a member
- */
- IP_ADDRESSES("IP_ADDRESSES"),
/**
* The key of the node name attribute of a member
*/
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
-import org.sonar.process.NetworkUtilsImpl;
import org.sonar.process.ProcessId;
import org.sonar.process.ProcessProperties;
import org.sonar.process.cluster.NodeType;
.setProperty("hazelcast.logging.type", "slf4j");
MemberAttributeConfig attributes = config.getMemberAttributeConfig();
- attributes.setStringAttribute(Attribute.HOSTNAME.getKey(), NetworkUtilsImpl.INSTANCE.getHostname());
- attributes.setStringAttribute(Attribute.IP_ADDRESSES.getKey(), NetworkUtilsImpl.INSTANCE.getIPAddresses());
attributes.setStringAttribute(Attribute.NODE_NAME.getKey(), requireNonNull(nodeName, "Node name is missing"));
attributes.setStringAttribute(Attribute.NODE_TYPE.getKey(), requireNonNull(nodeType, "Node type is missing").getValue());
attributes.setStringAttribute(Attribute.PROCESS_KEY.getKey(), requireNonNull(processId, "Process key is missing").getKey());