Search in sources :

Example 1 with FindCoordinatorRequest

use of org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorRequest in project geode by apache.

the class GMSJoinLeave method findCoordinatorFromView.

@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "WA_NOT_IN_LOOP")
boolean findCoordinatorFromView() {
    ArrayList<FindCoordinatorResponse> result;
    SearchState state = searchState;
    NetView v = state.view;
    List<InternalDistributedMember> recipients = new ArrayList<>(v.getMembers());
    if (recipients.size() > MAX_DISCOVERY_NODES && MAX_DISCOVERY_NODES > 0) {
        recipients = recipients.subList(0, MAX_DISCOVERY_NODES);
    }
    if (state.registrants != null) {
        recipients.addAll(state.registrants);
    }
    recipients.remove(localAddress);
    // FindCoordinatorRequest req = new FindCoordinatorRequest(localAddress, state.alreadyTried,
    // state.viewId, services.getMessenger().getPublickey(
    // localAddress), services.getMessenger().getRequestId());
    // req.setRecipients(v.getMembers());
    boolean testing = unitTesting.contains("findCoordinatorFromView");
    synchronized (state.responses) {
        if (!testing) {
            state.responses.clear();
        }
        String dhalgo = services.getConfig().getDistributionConfig().getSecurityUDPDHAlgo();
        if (!dhalgo.isEmpty()) {
            // Usually this happens when locator re-joins the cluster and it has saved view.
            for (InternalDistributedMember mbr : v.getMembers()) {
                Set<InternalDistributedMember> r = new HashSet<>();
                r.add(mbr);
                FindCoordinatorRequest req = new FindCoordinatorRequest(localAddress, state.alreadyTried, state.viewId, services.getMessenger().getPublicKey(localAddress), services.getMessenger().getRequestId(), dhalgo);
                req.setRecipients(r);
                services.getMessenger().send(req, v);
            }
        } else {
            FindCoordinatorRequest req = new FindCoordinatorRequest(localAddress, state.alreadyTried, state.viewId, services.getMessenger().getPublicKey(localAddress), services.getMessenger().getRequestId(), dhalgo);
            req.setRecipients(v.getMembers());
            services.getMessenger().send(req, v);
        }
        try {
            if (!testing) {
                state.responses.wait(DISCOVERY_TIMEOUT);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return false;
        }
        result = new ArrayList<>(state.responses);
        state.responses.clear();
    }
    InternalDistributedMember coord = null;
    if (localAddress.getNetMember().preferredForCoordinator()) {
        // it's possible that all other potential coordinators are gone
        // and this new member must become the coordinator
        coord = localAddress;
    }
    boolean coordIsNoob = true;
    for (FindCoordinatorResponse resp : result) {
        InternalDistributedMember mbr = resp.getCoordinator();
        if (!state.alreadyTried.contains(mbr)) {
            boolean mbrIsNoob = (mbr.getVmViewId() < 0);
            if (mbrIsNoob) {
                // member has not yet joined
                if (coordIsNoob && (coord == null || coord.compareTo(mbr, false) > 0)) {
                    coord = mbr;
                }
            } else {
                // member has already joined
                if (coordIsNoob || mbr.getVmViewId() > coord.getVmViewId()) {
                    coord = mbr;
                    coordIsNoob = false;
                }
            }
        }
    }
    state.possibleCoordinator = coord;
    return coord != null;
}
Also used : FindCoordinatorResponse(org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorResponse) NetView(org.apache.geode.distributed.internal.membership.NetView) ArrayList(java.util.ArrayList) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) FindCoordinatorRequest(org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorRequest) HashSet(java.util.HashSet)

Example 2 with FindCoordinatorRequest

use of org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorRequest in project geode by apache.

the class GMSJoinLeave method findCoordinator.

/**
   * This contacts the locators to find out who the current coordinator is. All locators are
   * contacted. If they don't agree then we choose the oldest coordinator and return it.
   */
private boolean findCoordinator() {
    SearchState state = searchState;
    assert this.localAddress != null;
    // the coordinator
    if (!state.hasContactedAJoinedLocator && state.view != null) {
        return findCoordinatorFromView();
    }
    String dhalgo = services.getConfig().getDistributionConfig().getSecurityUDPDHAlgo();
    FindCoordinatorRequest request = new FindCoordinatorRequest(this.localAddress, state.alreadyTried, state.viewId, services.getMessenger().getPublicKey(localAddress), services.getMessenger().getRequestId(), dhalgo);
    Set<InternalDistributedMember> possibleCoordinators = new HashSet<InternalDistributedMember>();
    Set<InternalDistributedMember> coordinatorsWithView = new HashSet<InternalDistributedMember>();
    long giveUpTime = System.currentTimeMillis() + ((long) services.getConfig().getLocatorWaitTime() * 1000L);
    int connectTimeout = (int) services.getConfig().getMemberTimeout() * 2;
    boolean anyResponses = false;
    logger.debug("sending {} to {}", request, locators);
    state.hasContactedAJoinedLocator = false;
    state.locatorsContacted = 0;
    do {
        for (InetSocketAddress addr : locators) {
            try {
                Object o = tcpClientWrapper.sendCoordinatorFindRequest(addr, request, connectTimeout);
                FindCoordinatorResponse response = (o instanceof FindCoordinatorResponse) ? (FindCoordinatorResponse) o : null;
                if (response != null) {
                    if (response.getRejectionMessage() != null) {
                        throw new GemFireConfigException(response.getRejectionMessage());
                    }
                    setCoordinatorPublicKey(response);
                    state.locatorsContacted++;
                    if (!state.hasContactedAJoinedLocator && response.getSenderId() != null && response.getSenderId().getVmViewId() >= 0) {
                        logger.debug("Locator's address indicates it is part of a distributed system " + "so I will not become membership coordinator on this attempt to join");
                        state.hasContactedAJoinedLocator = true;
                    }
                    if (response.getCoordinator() != null) {
                        anyResponses = true;
                        NetView v = response.getView();
                        int viewId = v == null ? -1 : v.getViewId();
                        if (viewId > state.viewId) {
                            state.viewId = viewId;
                            state.view = v;
                            state.registrants.clear();
                            if (response.getRegistrants() != null) {
                                state.registrants.addAll(response.getRegistrants());
                            }
                        }
                        if (viewId > -1) {
                            coordinatorsWithView.add(response.getCoordinator());
                        }
                        possibleCoordinators.add(response.getCoordinator());
                    }
                }
            } catch (IOException | ClassNotFoundException problem) {
            }
        }
    } while (!anyResponses && System.currentTimeMillis() < giveUpTime);
    if (possibleCoordinators.isEmpty()) {
        return false;
    }
    if (coordinatorsWithView.size() > 0) {
        // lets check current coordinators in view only
        possibleCoordinators = coordinatorsWithView;
    }
    Iterator<InternalDistributedMember> it = possibleCoordinators.iterator();
    if (possibleCoordinators.size() == 1) {
        state.possibleCoordinator = it.next();
    } else {
        InternalDistributedMember oldest = it.next();
        while (it.hasNext()) {
            InternalDistributedMember candidate = it.next();
            if (oldest.compareTo(candidate) > 0) {
                oldest = candidate;
            }
        }
        state.possibleCoordinator = oldest;
    }
    InternalDistributedMember coord = null;
    boolean coordIsNoob = true;
    for (; it.hasNext(); ) {
        InternalDistributedMember mbr = it.next();
        if (!state.alreadyTried.contains(mbr)) {
            boolean mbrIsNoob = (mbr.getVmViewId() < 0);
            if (mbrIsNoob) {
                // member has not yet joined
                if (coordIsNoob && (coord == null || coord.compareTo(mbr) > 0)) {
                    coord = mbr;
                }
            } else {
                // member has already joined
                if (coordIsNoob || mbr.getVmViewId() > coord.getVmViewId()) {
                    coord = mbr;
                    coordIsNoob = false;
                }
            }
        }
    }
    return true;
}
Also used : FindCoordinatorResponse(org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorResponse) InetSocketAddress(java.net.InetSocketAddress) NetView(org.apache.geode.distributed.internal.membership.NetView) IOException(java.io.IOException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) GemFireConfigException(org.apache.geode.GemFireConfigException) FindCoordinatorRequest(org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorRequest) HashSet(java.util.HashSet)

Example 3 with FindCoordinatorRequest

use of org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorRequest in project geode by apache.

the class GMSJoinLeaveJUnitTest method testCoordinatorFindRequestFailure.

@Test
public void testCoordinatorFindRequestFailure() throws Exception {
    try {
        initMocks(false);
        HashSet<InternalDistributedMember> registrants = new HashSet<>();
        registrants.add(mockMembers[0]);
        FindCoordinatorResponse fcr = new FindCoordinatorResponse(mockMembers[0], mockMembers[0], false, null, registrants, false, true, null);
        NetView view = createView();
        JoinResponseMessage jrm = new JoinResponseMessage(mockMembers[0], view, 0);
        gmsJoinLeave.setJoinResponseMessage(jrm);
        TcpClientWrapper tcpClientWrapper = mock(TcpClientWrapper.class);
        gmsJoinLeave.setTcpClientWrapper(tcpClientWrapper);
        FindCoordinatorRequest fcreq = new FindCoordinatorRequest(gmsJoinLeaveMemberId, new HashSet<>(), -1, null, 0, "");
        int connectTimeout = (int) services.getConfig().getMemberTimeout() * 2;
        // passing wrong port here, so ot will fail
        when(tcpClientWrapper.sendCoordinatorFindRequest(new InetSocketAddress("localhost", 12346), fcreq, connectTimeout)).thenReturn(fcr);
        assertFalse("Should not be able to join ", gmsJoinLeave.join());
    } finally {
    }
}
Also used : TcpClientWrapper(org.apache.geode.distributed.internal.membership.gms.membership.GMSJoinLeave.TcpClientWrapper) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) FindCoordinatorResponse(org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorResponse) FindCoordinatorRequest(org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorRequest) InetSocketAddress(java.net.InetSocketAddress) NetView(org.apache.geode.distributed.internal.membership.NetView) JoinResponseMessage(org.apache.geode.distributed.internal.membership.gms.messages.JoinResponseMessage) HashSet(java.util.HashSet) Test(org.junit.Test) MembershipTest(org.apache.geode.test.junit.categories.MembershipTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 4 with FindCoordinatorRequest

use of org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorRequest in project geode by apache.

the class JGroupsMessengerJUnitTest method testEncryptedFindCoordinatorRequest.

@Test
public void testEncryptedFindCoordinatorRequest() throws Exception {
    InternalDistributedMember otherMbr = new InternalDistributedMember("localhost", 8888);
    Properties p = new Properties();
    p.put(ConfigurationProperties.SECURITY_UDP_DHALGO, "AES:128");
    initMocks(false, p);
    NetView v = createView(otherMbr);
    when(joinLeave.getMemberID(messenger.getMemberID().getNetMember())).thenReturn(messenger.getMemberID());
    GMSEncrypt otherMbrEncrptor = new GMSEncrypt(services);
    messenger.setPublicKey(otherMbrEncrptor.getPublicKeyBytes(), otherMbr);
    messenger.initClusterKey();
    FindCoordinatorRequest gfmsg = new FindCoordinatorRequest(messenger.getMemberID(), new ArrayList<InternalDistributedMember>(2), 1, messenger.getPublicKey(messenger.getMemberID()), 1, "");
    Set<InternalDistributedMember> recipients = new HashSet<>();
    recipients.add(otherMbr);
    gfmsg.setRecipients(recipients);
    short version = Version.CURRENT_ORDINAL;
    HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
    messenger.writeEncryptedMessage(gfmsg, version, out);
    byte[] requestBytes = out.toByteArray();
    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(requestBytes));
    DistributionMessage distributionMessage = messenger.readEncryptedMessage(dis, version, otherMbrEncrptor);
    assertEquals(gfmsg, distributionMessage);
}
Also used : NetView(org.apache.geode.distributed.internal.membership.NetView) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) DataInputStream(java.io.DataInputStream) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) ByteArrayInputStream(java.io.ByteArrayInputStream) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) FindCoordinatorRequest(org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorRequest) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) HashSet(java.util.HashSet) Test(org.junit.Test) MembershipTest(org.apache.geode.test.junit.categories.MembershipTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 5 with FindCoordinatorRequest

use of org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorRequest in project geode by apache.

the class TcpServerBackwardCompatDUnitTest method testGossipVersionBackwardCompatibility.

/**
   * This test starts two locators with current GOSSIPVERSION and then shuts down one of them and
   * restart it with new GOSSIPVERSION and verifies that it has recoverd the system View. Then we
   * upgrade next locator.
   */
@Test
public void testGossipVersionBackwardCompatibility() {
    Host host = Host.getHost(0);
    final VM locator0 = host.getVM(0);
    final VM locator1 = host.getVM(1);
    final VM locatorRestart0 = host.getVM(2);
    final VM member = host.getVM(3);
    int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(2);
    // Create properties for locator0
    final int port0 = ports[0];
    // new File("");
    final File logFile0 = null;
    // Create properties for locator1
    final int port1 = ports[1];
    // new File("");
    final File logFile1 = null;
    final String locators = host.getHostName() + "[" + port0 + "]," + host.getHostName() + "[" + port1 + "]";
    final Properties props = new Properties();
    props.setProperty(LOCATORS, locators);
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(ENABLE_CLUSTER_CONFIGURATION, "false");
    // props.setProperty(LOG_LEVEL, "finest");
    // Start locator0 with props.
    // props.setProperty(DistributionConfig.START_LOCATOR_NAME, host.getHostName() + "["+port0+"]");
    locator0.invoke(new CacheSerializableRunnable("Starting first locator on port " + port0) {

        @Override
        public void run2() throws CacheException {
            try {
                TcpServer.getGossipVersionMapForTestOnly().put(TcpServer.TESTVERSION - 100, Version.CURRENT_ORDINAL);
                Locator.startLocatorAndDS(port0, logFile0, props);
            } catch (IOException e) {
                org.apache.geode.test.dunit.Assert.fail("Locator1 start failed with Gossip Version: " + TcpServer.GOSSIPVERSION + "!", e);
            }
        }
    });
    // Start a new member to add it to discovery set of locator0.
    member.invoke(new CacheSerializableRunnable("Start a member") {

        @Override
        public void run2() throws CacheException {
            disconnectFromDS();
            TcpServer.getGossipVersionMapForTestOnly().put(TcpServer.TESTVERSION - 100, Version.CURRENT_ORDINAL);
            InternalDistributedSystem.connect(props);
        }
    });
    // Start locator1 with props.
    // props.setProperty(DistributionConfig.START_LOCATOR_NAME, host.getHostName() + "["+port1+"]");
    locator1.invoke(new CacheSerializableRunnable("Starting second locator on port " + port1) {

        @Override
        public void run2() throws CacheException {
            try {
                TcpServer.TESTVERSION -= 100;
                TcpServer.OLDTESTVERSION -= 100;
                TcpServer.getGossipVersionMapForTestOnly().put(TcpServer.TESTVERSION, Version.CURRENT_ORDINAL);
                TcpServer.getGossipVersionMapForTestOnly().put(TcpServer.OLDTESTVERSION, Version.GFE_57.ordinal());
                // assertIndexDetailsEquals("Gossip Version and Test version are not same",
                // TcpServer.GOSSIPVERSION, TcpServer.TESTVERSION);
                // assertIndexDetailsEquals("Previous Gossip Version and Test version are not same",
                // TcpServer.OLDGOSSIPVERSION, TcpServer.OLDTESTVERSION);
                Locator.startLocatorAndDS(port1, logFile1, props);
                // Start a gossip client to connect to first locator "locator0".
                FindCoordinatorRequest req = new FindCoordinatorRequest(new InternalDistributedMember(SocketCreator.getLocalHost(), 1234));
                FindCoordinatorResponse response = null;
                response = (FindCoordinatorResponse) new TcpClient().requestToServer(SocketCreator.getLocalHost(), port1, req, 5000);
                assertNotNull(response);
            } catch (Exception e) {
                org.apache.geode.test.dunit.Assert.fail("Locator1 start failed with Gossip Version: " + TcpServer.GOSSIPVERSION + "!", e);
            }
        }
    });
    // Stop first locator currently running in locator0 VM.
    locator0.invoke(new CacheSerializableRunnable("Stopping first locator") {

        @Override
        public void run2() throws CacheException {
            Locator.getLocator().stop();
            disconnectFromDS();
        }
    });
    // Restart first locator in new VM.
    // props.setProperty(DistributionConfig.START_LOCATOR_NAME, host.getHostName() + "["+port0+"]");
    locatorRestart0.invoke(new CacheSerializableRunnable("Restarting first locator on port " + port0) {

        @Override
        public void run2() throws CacheException {
            try {
                TcpServer.TESTVERSION -= 100;
                TcpServer.OLDTESTVERSION -= 100;
                TcpServer.getGossipVersionMapForTestOnly().put(TcpServer.TESTVERSION, Version.CURRENT_ORDINAL);
                TcpServer.getGossipVersionMapForTestOnly().put(TcpServer.OLDTESTVERSION, Version.GFE_57.ordinal());
                // assertIndexDetailsEquals("Gossip Version and Test version are not same",
                // TcpServer.GOSSIPVERSION, TcpServer.TESTVERSION);
                // assertIndexDetailsEquals("Previous Gossip Version and Test version are not same",
                // TcpServer.OLDGOSSIPVERSION, TcpServer.OLDTESTVERSION);
                Locator.startLocatorAndDS(port0, logFile0, props);
                // Start a gossip client to connect to first locator "locator0".
                FindCoordinatorRequest req = new FindCoordinatorRequest(new InternalDistributedMember(SocketCreator.getLocalHost(), 1234));
                FindCoordinatorResponse response = null;
                response = (FindCoordinatorResponse) new TcpClient().requestToServer(SocketCreator.getLocalHost(), port0, req, 5000);
                assertNotNull(response);
            } catch (Exception e) {
                org.apache.geode.test.dunit.Assert.fail("Locator0 start failed with Gossip Version: " + TcpServer.GOSSIPVERSION + "!", e);
            }
        }
    });
}
Also used : FindCoordinatorResponse(org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorResponse) CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) FindCoordinatorRequest(org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorRequest) VM(org.apache.geode.test.dunit.VM) File(java.io.File) Test(org.junit.Test) MembershipTest(org.apache.geode.test.junit.categories.MembershipTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)6 FindCoordinatorRequest (org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorRequest)6 HashSet (java.util.HashSet)5 NetView (org.apache.geode.distributed.internal.membership.NetView)5 FindCoordinatorResponse (org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorResponse)5 MembershipTest (org.apache.geode.test.junit.categories.MembershipTest)4 Test (org.junit.Test)4 InetSocketAddress (java.net.InetSocketAddress)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3 IOException (java.io.IOException)2 Properties (java.util.Properties)2 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)2 TcpClientWrapper (org.apache.geode.distributed.internal.membership.gms.membership.GMSJoinLeave.TcpClientWrapper)2 JoinResponseMessage (org.apache.geode.distributed.internal.membership.gms.messages.JoinResponseMessage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 GemFireConfigException (org.apache.geode.GemFireConfigException)1 CacheException (org.apache.geode.cache.CacheException)1