Search in sources :

Example 6 with QuorumCnxManager

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

Example 7 with QuorumCnxManager

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

the class CnxManagerTest method testCnxManagerNPE.

/**
     * Tests a bug in QuorumCnxManager that causes a NPE when a 3.4.6
     * observer connects to a 3.5.0 server. 
     * {@link https://issues.apache.org/jira/browse/ZOOKEEPER-1789}
     * 
     * @throws Exception
     */
@Test
public void testCnxManagerNPE() throws Exception {
    // the connecting peer (id = 2) is a 3.4.6 observer
    peers.get(2L).type = LearnerType.OBSERVER;
    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");
    }
    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);
    /*
         * Write id (3.4.6 protocol). This previously caused a NPE in
         * QuorumCnxManager.
         */
    byte[] msgBytes = new byte[8];
    ByteBuffer msgBuffer = ByteBuffer.wrap(msgBytes);
    msgBuffer.putLong(2L);
    msgBuffer.position(0);
    sc.write(msgBuffer);
    msgBuffer = ByteBuffer.wrap(new byte[8]);
    // write length of message
    msgBuffer.putInt(4);
    // write message
    msgBuffer.putInt(5);
    msgBuffer.position(0);
    sc.write(msgBuffer);
    Message m = cnxManager.pollRecvQueue(1000, TimeUnit.MILLISECONDS);
    Assert.assertNotNull(m);
    peer.shutdown();
    cnxManager.halt();
    Assert.assertFalse(cnxManager.listener.isAlive());
}
Also used : SocketChannel(java.nio.channels.SocketChannel) Message(org.apache.zookeeper.server.quorum.QuorumCnxManager.Message) InitialMessage(org.apache.zookeeper.server.quorum.QuorumCnxManager.InitialMessage) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ByteBuffer(java.nio.ByteBuffer) QuorumCnxManager(org.apache.zookeeper.server.quorum.QuorumCnxManager) Test(org.junit.Test)

Example 8 with QuorumCnxManager

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

the class FLELostMessageTest method mockServer.

void mockServer() throws InterruptedException, IOException {
    QuorumPeer peer = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 1000, 2, 2);
    cnxManager = new QuorumCnxManager(peer);
    cnxManager.listener.start();
    cnxManager.toSend(1l, FLETestUtils.createMsg(ServerState.LOOKING.ordinal(), 0, 0, 0));
    cnxManager.recvQueue.take();
    cnxManager.toSend(1L, FLETestUtils.createMsg(ServerState.FOLLOWING.ordinal(), 1, 0, 0));
}
Also used : QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) QuorumCnxManager(org.apache.zookeeper.server.quorum.QuorumCnxManager)

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