Search in sources :

Example 86 with IpAddress

use of org.jgroups.stack.IpAddress in project openshift-ping by jboss-openshift.

the class OpenshiftPing method sendMcastDiscoveryRequest.

@Override
protected void sendMcastDiscoveryRequest(Message msg) {
    final List<InetSocketAddress> hosts = readAll();
    final PhysicalAddress physical_addr = (PhysicalAddress) down(new Event(Event.GET_PHYSICAL_ADDRESS, local_addr));
    if (!(physical_addr instanceof IpAddress)) {
        log.error("Unable to send PING requests: physical_addr is not an IpAddress.");
        return;
    }
    // XXX: is it better to force this to be defined?
    // assume symmetry
    final int port = ((IpAddress) physical_addr).getPort();
    for (InetSocketAddress host : hosts) {
        // JGroups messages cannot be reused - https://github.com/belaban/workshop/blob/master/slides/admin.adoc#problem-9-reusing-a-message-the-sebastian-problem
        Message msgToHost = msg.copy();
        msgToHost.dest(new IpAddress(host.getAddress(), port));
        sendDown(down_prot, msgToHost);
    }
}
Also used : Message(org.jgroups.Message) InetSocketAddress(java.net.InetSocketAddress) Event(org.jgroups.Event) PhysicalAddress(org.jgroups.PhysicalAddress) IpAddress(org.jgroups.stack.IpAddress)

Example 87 with IpAddress

use of org.jgroups.stack.IpAddress in project jgroups-kubernetes by jgroups-extras.

the class KUBE_PING method findMembers.

public void findMembers(List<Address> members, boolean initial_discovery, Responses responses) {
    List<Pod> hosts = readAll();
    List<PhysicalAddress> cluster_members = new ArrayList<>(hosts != null ? hosts.size() : 16);
    PhysicalAddress physical_addr = null;
    PingData data = null;
    physical_addr = getCurrentPhysicalAddress(local_addr);
    // https://issues.jboss.org/browse/JGRP-1670
    data = new PingData(local_addr, false, NameCache.get(local_addr), physical_addr);
    if (members != null && members.size() <= max_members_in_discovery_request)
        data.mbrs(members);
    if (hosts != null) {
        if (log.isTraceEnabled())
            log.trace("%s: hosts fetched from Kubernetes: %s", local_addr, hosts);
        for (Pod host : hosts) {
            if (!host.isReady() && !useNotReadyAddresses)
                continue;
            for (int i = 0; i <= port_range; i++) {
                try {
                    IpAddress addr = new IpAddress(host.getIp(), tp_bind_port + i);
                    if (!cluster_members.contains(addr))
                        cluster_members.add(addr);
                } catch (Exception ex) {
                    log.warn("failed translating host %s into InetAddress: %s", host, ex);
                }
            }
        }
    }
    if (use_disk_cache) {
        // this only makes sense if we have PDC below us
        Collection<PhysicalAddress> list = (Collection<PhysicalAddress>) down_prot.down(new Event(Event.GET_PHYSICAL_ADDRESSES));
        if (list != null)
            list.stream().filter(phys_addr -> !cluster_members.contains(phys_addr)).forEach(cluster_members::add);
    }
    if (split_clusters_during_rolling_update) {
        if (physical_addr != null) {
            String senderIp = ((IpAddress) physical_addr).getIpAddress().getHostAddress();
            // Please note we search for sender parent group through all pods, ever not ready. It's because JGroup discovery is performed
            // before WildFly can respond to http readiness probe.
            hosts.stream().filter(p -> p.getPodGroup() == null).forEach(p -> log.warn("Pod %s doesn't have group assigned. Impossible to reliably determine pod group during Rolling Update."));
            String senderPodGroup = hosts.stream().filter(pod -> senderIp.contains(pod.getIp())).map(Pod::getPodGroup).findFirst().orElse(null);
            if (senderPodGroup != null) {
                Set<String> allowedAddresses = hosts.stream().filter(pod -> senderPodGroup.equals(pod.getPodGroup())).map(Pod::getIp).collect(Collectors.toSet());
                for (Iterator<PhysicalAddress> memberIterator = cluster_members.iterator(); memberIterator.hasNext(); ) {
                    IpAddress podAddress = (IpAddress) memberIterator.next();
                    if (!allowedAddresses.contains(podAddress.getIpAddress().getHostAddress())) {
                        log.trace("removing pod %s from cluster members list since its parent domain is different than senders (%s). Allowed hosts: %s", podAddress, senderPodGroup, allowedAddresses);
                        memberIterator.remove();
                    }
                }
            } else {
                log.warn("split_clusters_during_rolling_update is set to 'true' but can't obtain local node parent deployment. All nodes will be placed in the same cluster.");
            }
        } else {
            log.warn("split_clusters_during_rolling_update is set to 'true' but can't obtain local node IP address. All nodes will be placed in the same cluster.");
        }
    }
    if (log.isTraceEnabled())
        log.trace("%s: sending discovery requests to %s", local_addr, cluster_members);
    PingHeader hdr = new PingHeader(PingHeader.GET_MBRS_REQ).clusterName(cluster_name).initialDiscovery(initial_discovery);
    for (final PhysicalAddress addr : cluster_members) {
        if (// no need to send the request to myself
        addr.equals(physical_addr))
            continue;
        // the message needs to be DONT_BUNDLE, see explanation above
        final Message msg = new BytesMessage(addr).setFlag(Message.Flag.DONT_BUNDLE, Message.Flag.OOB).putHeader(this.id, hdr);
        if (data != null)
            msg.setArray(marshal(data));
        if (async_discovery_use_separate_thread_per_request)
            timer.execute(() -> sendDiscoveryRequest(msg), sends_can_block);
        else
            sendDiscoveryRequest(msg);
    }
}
Also used : Property(org.jgroups.annotations.Property) NameCache(org.jgroups.util.NameCache) PingData(org.jgroups.protocols.PingData) Discovery(org.jgroups.protocols.Discovery) java.util(java.util) CertificateStreamProvider(org.jgroups.protocols.kubernetes.stream.CertificateStreamProvider) StreamProvider(org.jgroups.protocols.kubernetes.stream.StreamProvider) Collectors(java.util.stream.Collectors) IpAddress(org.jgroups.stack.IpAddress) PingHeader(org.jgroups.protocols.PingHeader) ManagedOperation(org.jgroups.annotations.ManagedOperation) MBean(org.jgroups.annotations.MBean) Responses(org.jgroups.util.Responses) Utils.readFileToString(org.jgroups.protocols.kubernetes.Utils.readFileToString) org.jgroups(org.jgroups) ClassConfigurator(org.jgroups.conf.ClassConfigurator) TokenStreamProvider(org.jgroups.protocols.kubernetes.stream.TokenStreamProvider) Utils.readFileToString(org.jgroups.protocols.kubernetes.Utils.readFileToString) PingData(org.jgroups.protocols.PingData) IpAddress(org.jgroups.stack.IpAddress) PingHeader(org.jgroups.protocols.PingHeader)

Aggregations

IpAddress (org.jgroups.stack.IpAddress)87 InetSocketAddress (java.net.InetSocketAddress)15 Address (org.jgroups.Address)13 InetAddress (java.net.InetAddress)7 Test (org.testng.annotations.Test)7 Event (org.jgroups.Event)6 PhysicalAddress (org.jgroups.PhysicalAddress)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4 IOException (java.io.IOException)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 JChannel (org.jgroups.JChannel)3 ManagedOperation (org.jgroups.annotations.ManagedOperation)3 DefaultSocketFactory (org.jgroups.util.DefaultSocketFactory)3 Container (io.fabric8.kubernetes.api.model.Container)2 ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 ServerSocket (java.net.ServerSocket)2