Search in sources :

Example 1 with QuorumCnxManager

use of org.apache.zookeeper.server.quorum.QuorumCnxManager in project zookeeper by apache.

the class FLEBackwardElectionRoundTest method testBackwardElectionRound.

/**
     * This test is checking the following case. A server S is
     * currently LOOKING and it receives notifications from
     * a quorum indicating they are following S. The election
     * round E of S is higher than the election round E' in the
     * notification messages, so S becomes the leader and sets
     * its epoch back to E'. In the meanwhile, one or more
     * followers turn to LOOKING and elect S in election round E.
     * Having leader and followers with different election rounds
     * might prevent other servers from electing a leader because
     * they can't get a consistent set of notifications from a
     * quorum.
     *
     * {@link https://issues.apache.org/jira/browse/ZOOKEEPER-1514}
     *
     *
     * @throws Exception
     */
@Test
public void testBackwardElectionRound() throws Exception {
    LOG.info("TestLE: {}, {}", getTestName(), count);
    for (int i = 0; i < count; i++) {
        int clientport = PortAssignment.unique();
        peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress(clientport), new InetSocketAddress(PortAssignment.unique())));
        tmpdir[i] = ClientBase.createTmpDir();
        port[i] = clientport;
    }
    ByteBuffer initialMsg = FLETestUtils.createMsg(ServerState.FOLLOWING.ordinal(), 0, 0, 1);
    /*
         * Start server 0
         */
    QuorumPeer peer = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 1000, 2, 2);
    peer.startLeaderElection();
    FLETestUtils.LEThread thread = new FLETestUtils.LEThread(peer, 0);
    thread.start();
    /*
         * Start mock server 1
         */
    QuorumPeer mockPeer = new QuorumPeer(peers, tmpdir[1], tmpdir[1], port[1], 3, 1, 1000, 2, 2);
    cnxManagers[0] = new QuorumCnxManager(mockPeer);
    cnxManagers[0].listener.start();
    cnxManagers[0].toSend(0l, initialMsg);
    /*
         * Start mock server 2
         */
    mockPeer = new QuorumPeer(peers, tmpdir[2], tmpdir[2], port[2], 3, 2, 1000, 2, 2);
    cnxManagers[1] = new QuorumCnxManager(mockPeer);
    cnxManagers[1].listener.start();
    cnxManagers[1].toSend(0l, initialMsg);
    /*
         * Run another instance of leader election.
         */
    thread.join(5000);
    thread = new FLETestUtils.LEThread(peer, 0);
    thread.start();
    /*
         * Send the same messages, this time should not make 0 the leader.
         */
    cnxManagers[0].toSend(0l, initialMsg);
    cnxManagers[1].toSend(0l, initialMsg);
    thread.join(5000);
    if (!thread.isAlive()) {
        Assert.fail("Should not have joined");
    }
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ByteBuffer(java.nio.ByteBuffer) QuorumCnxManager(org.apache.zookeeper.server.quorum.QuorumCnxManager) Test(org.junit.Test)

Example 2 with QuorumCnxManager

use of org.apache.zookeeper.server.quorum.QuorumCnxManager in project zookeeper by apache.

the class CnxManagerTest method testSocketTimeout.

/*
     * Test if a receiveConnection is able to timeout on socket errors
     */
@Test
public void testSocketTimeout() throws Exception {
    QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 2000, 2, 2);
    QuorumCnxManager cnxManager = new QuorumCnxManager(peer);
    QuorumCnxManager.Listener listener = cnxManager.listener;
    if (listener != null) {
        listener.start();
    } else {
        LOG.error("Null listener when initializing cnx manager");
    }
    int port = peers.get(peer.getId()).electionAddr.getPort();
    LOG.info("Election port: " + port);
    Thread.sleep(1000);
    Socket sock = new Socket();
    sock.connect(peers.get(1L).electionAddr, 5000);
    long begin = Time.currentElapsedTime();
    // Read without sending data. Verify timeout.
    cnxManager.receiveConnection(sock);
    long end = Time.currentElapsedTime();
    if ((end - begin) > ((peer.getSyncLimit() * peer.getTickTime()) + 500))
        Assert.fail("Waited more than necessary");
    cnxManager.halt();
    Assert.assertFalse(cnxManager.listener.isAlive());
}
Also used : QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) QuorumCnxManager(org.apache.zookeeper.server.quorum.QuorumCnxManager) Socket(java.net.Socket) Test(org.junit.Test)

Example 3 with QuorumCnxManager

use of org.apache.zookeeper.server.quorum.QuorumCnxManager in project zookeeper by apache.

the class CnxManagerTest method testCnxManager.

@Test
public void testCnxManager() throws Exception {
    CnxManagerThread thread = new CnxManagerThread();
    thread.start();
    QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2);
    QuorumCnxManager cnxManager = new QuorumCnxManager(peer);
    QuorumCnxManager.Listener listener = cnxManager.listener;
    if (listener != null) {
        listener.start();
    } else {
        LOG.error("Null listener when initializing cnx manager");
    }
    cnxManager.toSend(0L, createMsg(ServerState.LOOKING.ordinal(), 1, -1, 1));
    Message m = null;
    int numRetries = 1;
    while ((m == null) && (numRetries++ <= THRESHOLD)) {
        m = cnxManager.pollRecvQueue(3000, TimeUnit.MILLISECONDS);
        if (m == null)
            cnxManager.connectAll();
    }
    Assert.assertTrue("Exceeded number of retries", numRetries <= THRESHOLD);
    thread.join(5000);
    if (thread.isAlive()) {
        Assert.fail("Thread didn't join");
    } else {
        if (thread.failed)
            Assert.fail("Did not receive expected message");
    }
    cnxManager.halt();
    Assert.assertFalse(cnxManager.listener.isAlive());
}
Also used : Message(org.apache.zookeeper.server.quorum.QuorumCnxManager.Message) InitialMessage(org.apache.zookeeper.server.quorum.QuorumCnxManager.InitialMessage) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) QuorumCnxManager(org.apache.zookeeper.server.quorum.QuorumCnxManager) Test(org.junit.Test)

Example 4 with QuorumCnxManager

use of org.apache.zookeeper.server.quorum.QuorumCnxManager in project zookeeper by apache.

the class CnxManagerTest method testCnxManagerTimeout.

@Test
public void testCnxManagerTimeout() throws Exception {
    Random rand = new Random();
    byte b = (byte) rand.nextInt();
    int deadPort = PortAssignment.unique();
    String deadAddress = "10.1.1." + b;
    LOG.info("This is the dead address I'm trying: " + deadAddress);
    peers.put(Long.valueOf(2), new QuorumServer(2, new InetSocketAddress(deadAddress, deadPort), new InetSocketAddress(deadAddress, PortAssignment.unique()), new InetSocketAddress(deadAddress, PortAssignment.unique())));
    peerTmpdir[2] = ClientBase.createTmpDir();
    QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2);
    QuorumCnxManager cnxManager = new QuorumCnxManager(peer);
    QuorumCnxManager.Listener listener = cnxManager.listener;
    if (listener != null) {
        listener.start();
    } else {
        LOG.error("Null listener when initializing cnx manager");
    }
    long begin = Time.currentElapsedTime();
    cnxManager.toSend(2L, createMsg(ServerState.LOOKING.ordinal(), 1, -1, 1));
    long end = Time.currentElapsedTime();
    if ((end - begin) > 6000)
        Assert.fail("Waited more than necessary");
    cnxManager.halt();
    Assert.assertFalse(cnxManager.listener.isAlive());
}
Also used : Random(java.util.Random) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) QuorumCnxManager(org.apache.zookeeper.server.quorum.QuorumCnxManager) Test(org.junit.Test)

Example 5 with QuorumCnxManager

use of org.apache.zookeeper.server.quorum.QuorumCnxManager in project zookeeper by apache.

the class CnxManagerTest method _verifyThreadCount.

public String _verifyThreadCount(ArrayList<QuorumPeer> peerList, long ecnt) {
    for (int myid = 0; myid < peerList.size(); myid++) {
        QuorumPeer peer = peerList.get(myid);
        QuorumCnxManager cnxManager = peer.getQuorumCnxManager();
        long cnt = cnxManager.getThreadCount();
        if (cnt != ecnt) {
            return new Date() + " Incorrect number of Worker threads for sid=" + myid + " expected " + ecnt + " found " + cnt;
        }
    }
    return null;
}
Also used : QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) QuorumCnxManager(org.apache.zookeeper.server.quorum.QuorumCnxManager) Date(java.util.Date)

Aggregations

QuorumCnxManager (org.apache.zookeeper.server.quorum.QuorumCnxManager)8 QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)8 Test (org.junit.Test)6 InetSocketAddress (java.net.InetSocketAddress)3 ByteBuffer (java.nio.ByteBuffer)3 SocketChannel (java.nio.channels.SocketChannel)2 InitialMessage (org.apache.zookeeper.server.quorum.QuorumCnxManager.InitialMessage)2 Message (org.apache.zookeeper.server.quorum.QuorumCnxManager.Message)2 QuorumServer (org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)2 DataOutputStream (java.io.DataOutputStream)1 Socket (java.net.Socket)1 Date (java.util.Date)1 Random (java.util.Random)1