Search in sources :

Example 1 with QuorumPeer

use of org.apache.zookeeper.server.quorum.QuorumPeer 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)

Example 2 with QuorumPeer

use of org.apache.zookeeper.server.quorum.QuorumPeer 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 = peer.createCnxnManager();
    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 QuorumPeer

use of org.apache.zookeeper.server.quorum.QuorumPeer 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 = peer.createCnxnManager();
    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 QuorumPeer

use of org.apache.zookeeper.server.quorum.QuorumPeer 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 = peer.createCnxnManager();
    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 QuorumPeer

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

the class CnxManagerTest method testCnxManagerSpinLock.

/**
 * Tests a bug in QuorumCnxManager that causes a spin lock
 * when a negative value is sent. This test checks if the
 * connection is being closed upon a message with negative
 * length.
 *
 * @throws Exception
 */
@Test
public void testCnxManagerSpinLock() throws Exception {
    QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2);
    QuorumCnxManager cnxManager = peer.createCnxnManager();
    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);
    SocketChannel sc = SocketChannel.open();
    sc.socket().connect(peers.get(1L).electionAddr, 5000);
    InetSocketAddress otherAddr = peers.get(new Long(2)).electionAddr;
    DataOutputStream dout = new DataOutputStream(sc.socket().getOutputStream());
    dout.writeLong(QuorumCnxManager.PROTOCOL_VERSION);
    dout.writeLong(new Long(2));
    String addr = otherAddr.getHostString() + ":" + otherAddr.getPort();
    byte[] addr_bytes = addr.getBytes();
    dout.writeInt(addr_bytes.length);
    dout.write(addr_bytes);
    dout.flush();
    ByteBuffer msgBuffer = ByteBuffer.wrap(new byte[4]);
    msgBuffer.putInt(-20);
    msgBuffer.position(0);
    sc.write(msgBuffer);
    Thread.sleep(1000);
    try {
        /*
             * Write a number of times until it
             * detects that the socket is broken.
             */
        for (int i = 0; i < 100; i++) {
            msgBuffer.position(0);
            sc.write(msgBuffer);
        }
        Assert.fail("Socket has not been closed");
    } catch (Exception e) {
        LOG.info("Socket has been closed as expected");
    }
    peer.shutdown();
    cnxManager.halt();
    Assert.assertFalse(cnxManager.listener.isAlive());
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) DataOutputStream(java.io.DataOutputStream) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ByteBuffer(java.nio.ByteBuffer) QuorumCnxManager(org.apache.zookeeper.server.quorum.QuorumCnxManager) Test(org.junit.Test)

Aggregations

QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)50 InetSocketAddress (java.net.InetSocketAddress)19 Test (org.junit.jupiter.api.Test)18 QuorumServer (org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)17 IOException (java.io.IOException)9 ZooKeeper (org.apache.zookeeper.ZooKeeper)9 Test (org.junit.Test)9 File (java.io.File)8 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)6 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)5 QuorumCnxManager (org.apache.zookeeper.server.quorum.QuorumCnxManager)5 ByteBuffer (java.nio.ByteBuffer)4 LinkedHashSet (java.util.LinkedHashSet)3 Stat (org.apache.zookeeper.data.Stat)3 CreateRequest (org.apache.zookeeper.proto.CreateRequest)3 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ServerSocket (java.net.ServerSocket)2