Search in sources :

Example 26 with QuorumServer

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

the class FLETest method testJoin.

/*
     * For ZOOKEEPER-975 verify that a peer joining an established cluster
     * does not go in LEADING state.
     */
@Test
public void testJoin() throws Exception {
    int sid;
    QuorumPeer peer;
    int waitTime = 10 * 1000;
    ArrayList<QuorumPeer> peerList = new ArrayList<QuorumPeer>();
    for (sid = 0; sid < 3; sid++) {
        port[sid] = PortAssignment.unique();
        peers.put(Long.valueOf(sid), new QuorumServer(sid, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port[sid])));
        tmpdir[sid] = ClientBase.createTmpDir();
    }
    // start 2 peers and verify if they form the cluster
    for (sid = 0; sid < 2; sid++) {
        peer = new QuorumPeer(peers, tmpdir[sid], tmpdir[sid], port[sid], 3, sid, 2000, 2, 2);
        LOG.info("Starting peer " + peer.getId());
        peer.start();
        peerList.add(sid, peer);
    }
    peer = peerList.get(0);
    VerifyState v1 = new VerifyState(peerList.get(0));
    v1.start();
    v1.join(waitTime);
    Assert.assertFalse("Unable to form cluster in " + waitTime + " ms", !v1.isSuccess());
    // Start 3rd peer and check if it goes in LEADING state
    peer = new QuorumPeer(peers, tmpdir[sid], tmpdir[sid], port[sid], 3, sid, 2000, 2, 2);
    LOG.info("Starting peer " + peer.getId());
    peer.start();
    peerList.add(sid, peer);
    v1 = new VerifyState(peer);
    v1.start();
    v1.join(waitTime);
    if (v1.isAlive()) {
        Assert.fail("Peer " + peer.getId() + " failed to join the cluster " + "within " + waitTime + " ms");
    } else if (!v1.isSuccess()) {
        Assert.fail("Incorrect LEADING state for peer " + peer.getId());
    }
    // cleanup
    for (int id = 0; id < 3; id++) {
        peer = peerList.get(id);
        if (peer != null) {
            peer.shutdown();
        }
    }
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 27 with QuorumServer

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

the class FLENewEpochTest method testLENewEpoch.

@Test
public void testLENewEpoch() throws Exception {
    LOG.info("TestLE: " + getTestName() + ", " + count);
    for (int i = 0; i < count; i++) {
        peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique())));
        tmpdir[i] = ClientBase.createTmpDir();
        port[i] = PortAssignment.unique();
    }
    for (int i = 1; i < count; i++) {
        QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 1000, 2, 2);
        peer.startLeaderElection();
        LEThread thread = new LEThread(peer, i);
        thread.start();
        threads.add(thread);
    }
    if (!start0.tryAcquire(4000, java.util.concurrent.TimeUnit.MILLISECONDS))
        Assert.fail("First leader election failed");
    QuorumPeer peer = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 1000, 2, 2);
    peer.startLeaderElection();
    LEThread thread = new LEThread(peer, 0);
    thread.start();
    threads.add(thread);
    LOG.info("Started threads " + getTestName());
    for (int i = 0; i < threads.size(); i++) {
        threads.get(i).join(10000);
        if (threads.get(i).isAlive()) {
            Assert.fail("Threads didn't join");
        }
    }
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) Test(org.junit.Test)

Example 28 with QuorumServer

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

the class FLERestartTest method testLERestart.

@Test
public void testLERestart() throws Exception {
    LOG.info("TestLE: " + getTestName() + ", " + count);
    for (int i = 0; i < count; i++) {
        peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique())));
        tmpdir[i] = ClientBase.createTmpDir();
        port[i] = PortAssignment.unique();
    }
    for (int i = 0; i < count; i++) {
        QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 1000, 2, 2);
        peer.startLeaderElection();
        FLERestartThread thread = new FLERestartThread(peer, i);
        thread.start();
        restartThreads.add(thread);
    }
    LOG.info("Started threads " + getTestName());
    for (int i = 0; i < restartThreads.size(); i++) {
        restartThreads.get(i).join(10000);
        if (restartThreads.get(i).isAlive()) {
            Assert.fail("Threads didn't join");
        }
    }
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) Test(org.junit.Test)

Example 29 with QuorumServer

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

the class LENonTerminateTest method testNonTermination.

/**
     * This tests ZK-569.
     * With three peers A, B and C, the following could happen:
     * 1. Round 1, A,B and C all vote for themselves
     * 2. Round 2, C dies, A and B vote for C
     * 3. Because C has died, votes for it are ignored, but A and B never
     * reset their votes. Hence LE never terminates. ZK-569 fixes this by
     * resetting votes to themselves if the set of votes for live peers is null.
     */
@Test
public void testNonTermination() throws Exception {
    LOG.info("TestNonTermination: " + getTestName() + ", " + count);
    for (int i = 0; i < count; i++) {
        int clientport = PortAssignment.unique();
        peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", clientport), new InetSocketAddress("127.0.0.1", PortAssignment.unique())));
        tmpdir[i] = ClientBase.createTmpDir();
        port[i] = clientport;
    }
    /*
         * peer1 and peer2 are A and B in the above example.
         */
    QuorumPeer peer1 = new MockQuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 0, 0, 2, 2, 2);
    peer1.startLeaderElection();
    LEThread thread1 = new LEThread(peer1, 0);
    QuorumPeer peer2 = new MockQuorumPeer(peers, tmpdir[1], tmpdir[1], port[1], 0, 1, 2, 2, 2);
    peer2.startLeaderElection();
    LEThread thread2 = new LEThread(peer2, 1);
    /*
         * Start mock server.
         */
    Thread thread3 = new Thread() {

        public void run() {
            try {
                mockServer();
            } catch (Exception e) {
                LOG.error("exception", e);
                Assert.fail("Exception when running mocked server " + e);
            }
        }
    };
    thread3.start();
    Assert.assertTrue("mockServer did not start in 5s", mockLatch.await(5000, TimeUnit.MILLISECONDS));
    thread1.start();
    thread2.start();
    /*
         * Occasionally seen false negatives with a 5s timeout.
         */
    thread1.join(15000);
    thread2.join(15000);
    thread3.join(15000);
    if (thread1.isAlive() || thread2.isAlive() || thread3.isAlive()) {
        Assert.fail("Threads didn't join");
    }
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) SocketException(java.net.SocketException) IOException(java.io.IOException) FLELostMessageTest(org.apache.zookeeper.server.quorum.FLELostMessageTest) Test(org.junit.Test)

Example 30 with QuorumServer

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

the class LENonTerminateTest method mockServer.

/**
     * MockServer plays the role of peer C. Respond to two requests for votes
     * with vote for self and then Assert.fail.
     */
void mockServer() throws InterruptedException, IOException {
    byte[] b = new byte[36];
    ByteBuffer responseBuffer = ByteBuffer.wrap(b);
    DatagramPacket packet = new DatagramPacket(b, b.length);
    QuorumServer server = peers.get(Long.valueOf(2));
    DatagramSocket udpSocket = new DatagramSocket(server.addr.getPort());
    LOG.info("In MockServer");
    mockLatch.countDown();
    Vote current = new Vote(2, 1);
    for (int i = 0; i < 2; ++i) {
        udpSocket.receive(packet);
        responseBuffer.rewind();
        LOG.info("Received " + responseBuffer.getInt() + " " + responseBuffer.getLong() + " " + responseBuffer.getLong());
        LOG.info("From " + packet.getSocketAddress());
        responseBuffer.clear();
        // Skip the xid
        responseBuffer.getInt();
        responseBuffer.putLong(2);
        responseBuffer.putLong(current.getId());
        responseBuffer.putLong(current.getZxid());
        packet.setData(b);
        udpSocket.send(packet);
    }
}
Also used : Vote(org.apache.zookeeper.server.quorum.Vote) DatagramSocket(java.net.DatagramSocket) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) DatagramPacket(java.net.DatagramPacket) ByteBuffer(java.nio.ByteBuffer)

Aggregations

QuorumServer (org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)45 InetSocketAddress (java.net.InetSocketAddress)24 Test (org.junit.Test)20 QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)19 IOException (java.io.IOException)10 HashMap (java.util.HashMap)9 File (java.io.File)7 ArrayList (java.util.ArrayList)5 Before (org.junit.Before)4 ServerSocket (java.net.ServerSocket)3 Socket (java.net.Socket)3 ByteBuffer (java.nio.ByteBuffer)3 KeeperException (org.apache.zookeeper.KeeperException)3 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 Vote (org.apache.zookeeper.server.quorum.Vote)3 EOFException (java.io.EOFException)2 FileNotFoundException (java.io.FileNotFoundException)2 DatagramPacket (java.net.DatagramPacket)2 DatagramSocket (java.net.DatagramSocket)2 SocketException (java.net.SocketException)2