Search in sources :

Example 1 with Discovery

use of org.jgroups.protocols.Discovery in project JGroups by belaban.

the class DiscoveryTest method testLeak.

protected static void testLeak(JChannel discovery_initiator) throws Exception {
    ProtocolStack stack = discovery_initiator.getProtocolStack();
    Discovery ping = stack.findProtocol(Discovery.class);
    ping.discoveryRspExpiryTime(1000);
    Field ping_rsps_field = Util.getField(Discovery.class, "ping_responses");
    Map<Long, Responses> ping_rsps = (Map<Long, Responses>) Util.getField(ping_rsps_field, ping);
    for (int i = 1; i <= 10; i++) {
        ping.down(new Event(Event.FIND_INITIAL_MBRS, 1000L));
    }
    for (int i = 0; i < 10; i++) {
        Util.sleep(500);
        ping.weedOutCompletedDiscoveryResponses();
        if (ping_rsps.isEmpty())
            break;
        System.out.printf("responses map has %d elements:\n%s\n", ping_rsps.size(), print(ping_rsps));
    }
    assert ping_rsps.isEmpty() : String.format("responses map has %d elements:\n%s\n", ping_rsps.size(), print(ping_rsps));
}
Also used : Field(java.lang.reflect.Field) ProtocolStack(org.jgroups.stack.ProtocolStack) Discovery(org.jgroups.protocols.Discovery) Event(org.jgroups.Event) Map(java.util.Map) Responses(org.jgroups.util.Responses)

Example 2 with Discovery

use of org.jgroups.protocols.Discovery 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

Discovery (org.jgroups.protocols.Discovery)2 Responses (org.jgroups.util.Responses)2 Field (java.lang.reflect.Field)1 java.util (java.util)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 org.jgroups (org.jgroups)1 Event (org.jgroups.Event)1 MBean (org.jgroups.annotations.MBean)1 ManagedOperation (org.jgroups.annotations.ManagedOperation)1 Property (org.jgroups.annotations.Property)1 ClassConfigurator (org.jgroups.conf.ClassConfigurator)1 PingData (org.jgroups.protocols.PingData)1 PingHeader (org.jgroups.protocols.PingHeader)1 Utils.readFileToString (org.jgroups.protocols.kubernetes.Utils.readFileToString)1 CertificateStreamProvider (org.jgroups.protocols.kubernetes.stream.CertificateStreamProvider)1 StreamProvider (org.jgroups.protocols.kubernetes.stream.StreamProvider)1 TokenStreamProvider (org.jgroups.protocols.kubernetes.stream.TokenStreamProvider)1 IpAddress (org.jgroups.stack.IpAddress)1 ProtocolStack (org.jgroups.stack.ProtocolStack)1