Examples of CountdownWatcher


Examples of org.apache.zookeeper.test.ClientBase.CountdownWatcher

    {
        final Semaphore sem = new Semaphore(0);

        QuorumUtil qu = new QuorumUtil(1);
        qu.startAll();
        CountdownWatcher watcher1 = new CountdownWatcher();
        CountdownWatcher watcher2 = new CountdownWatcher();
        CountdownWatcher watcher3 = new CountdownWatcher();

        int index = 1;
        while(qu.getPeer(index).peer.leader == null) {
            index++;
        }
View Full Code Here

Examples of org.apache.zookeeper.test.ClientBase.CountdownWatcher

    }

    private static TestableZooKeeper createTestableClient(String hp)
        throws IOException, TimeoutException, InterruptedException
    {
        CountdownWatcher watcher = new CountdownWatcher();
        return createTestableClient(watcher, hp);
    }
View Full Code Here

Examples of org.apache.zookeeper.test.ClientBase.CountdownWatcher

        int testPeerIdx = onLeader ? leaderIdx : followerIdx;
        int verifyPeerIdx = onLeader ? followerIdx : leaderIdx;

        String hostPorts[] = qb.hostPort.split(",");

        CountdownWatcher watcher = new CountdownWatcher();
        DisconnectableZooKeeper client = new DisconnectableZooKeeper(
                hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher);
        watcher.waitForConnected(CONNECTION_TIMEOUT);

        long localSessionId1 = client.getSessionId();

        // Cut the connection, so the server will create closeSession as part
        // of expiring the session.
        client.dontReconnect();
        client.disconnect();
        watcher.reset();

        // We don't validate right away, will do another session create first

        ZooKeeper zk = qb.createClient(watcher, hostPorts[testPeerIdx],
                CONNECTION_TIMEOUT);
        watcher.waitForConnected(CONNECTION_TIMEOUT);

        long localSessionId2 = zk.getSessionId();

        // Send closeSession request.
        zk.close();
        watcher.reset();

        // This should be enough time for the first session to expire and for
        // the closeSession request to propagate to other machines (if there is a bug)
        // Since it is time sensitive, we have false negative when test
        // machine is under load
View Full Code Here

Examples of org.apache.zookeeper.test.ClientBase.CountdownWatcher

        Assert.assertFalse("No leader in quorum?", leaderIdx == -1);
        int followerIdx = (leaderIdx + 1) % 5;
        int otherFollowerIdx = (leaderIdx + 2) % 5;
        int testPeerIdx = testLeader ? leaderIdx : followerIdx;
        String hostPorts[] = qb.hostPort.split(",");
        CountdownWatcher watcher = new CountdownWatcher();
        DisconnectableZooKeeper zk = new DisconnectableZooKeeper(
                hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher);
        watcher.waitForConnected(CONNECTION_TIMEOUT);

        // Try creating some data.
        for (int i = 0; i < 5; i++) {
            zk.create(nodePrefix + i, new byte[0],
                      ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }

        long localSessionId = zk.getSessionId();
        byte[] localSessionPwd = zk.getSessionPasswd().clone();

        // Try connecting with the same session id on a different
        // server.  This should fail since it is a local sesion.
        try {
            watcher.reset();
            DisconnectableZooKeeper zknew = new DisconnectableZooKeeper(
                    hostPorts[otherFollowerIdx], CONNECTION_TIMEOUT, watcher,
                    localSessionId, localSessionPwd);

            zknew.create(nodePrefix + "5", new byte[0],
                         ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            Assert.fail("Connection on the same session ID should fail.");
        } catch (KeeperException.SessionExpiredException e) {
        } catch (KeeperException.ConnectionLossException e) {
        }

        // If we're testing a follower, also check the session id on the
        // leader. This should also fail
        if (!testLeader) {
            try {
                watcher.reset();
                DisconnectableZooKeeper zknew = new DisconnectableZooKeeper(
                        hostPorts[leaderIdx], CONNECTION_TIMEOUT,
                        watcher, localSessionId, localSessionPwd);

                zknew.create(nodePrefix + "5", new byte[0],
                             ZooDefs.Ids.OPEN_ACL_UNSAFE,
                             CreateMode.PERSISTENT);
                Assert.fail("Connection on the same session ID should fail.");
            } catch (KeeperException.SessionExpiredException e) {
            } catch (KeeperException.ConnectionLossException e) {
            }
        }

        // However, we should be able to disconnect and reconnect to the same
        // server with the same session id (as long as we do it quickly
        // before expiration).
        zk.disconnect();

        watcher.reset();
        zk = new DisconnectableZooKeeper(
                hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher,
                localSessionId, localSessionPwd);
        watcher.waitForConnected(CONNECTION_TIMEOUT);

        zk.create(nodePrefix + "6", new byte[0],
                  ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        // If we explicitly close the session, then the session id should no
        // longer be valid.
        zk.close();
        try {
            watcher.reset();
            zk = new DisconnectableZooKeeper(
                    hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher,
                    localSessionId, localSessionPwd);

            zk.create(nodePrefix + "7", new byte[0],
View Full Code Here

Examples of org.apache.zookeeper.test.ClientBase.CountdownWatcher

        int followerIdx = (leaderIdx + 1) % 5;
        int otherFollowerIdx = (leaderIdx + 2) % 5;
        int testPeerIdx = testLeader ? leaderIdx : followerIdx;
        String hostPorts[] = qb.hostPort.split(",");

        CountdownWatcher watcher = new CountdownWatcher();
        DisconnectableZooKeeper zk = new DisconnectableZooKeeper(
                hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher);
        watcher.waitForConnected(CONNECTION_TIMEOUT);

        // Create some ephemeral nodes.  This should force the session to
        // be propagated to the other servers in the ensemble.
        for (int i = 0; i < 5; i++) {
            zk.create(nodePrefix + i, new byte[0],
                      ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        }

        // We should be able to reconnect with the same session id on a
        // different server, since it has been propagated.
        long localSessionId = zk.getSessionId();
        byte[] localSessionPwd = zk.getSessionPasswd().clone();

        zk.disconnect();
        watcher.reset();
        zk = new DisconnectableZooKeeper(
                hostPorts[otherFollowerIdx], CONNECTION_TIMEOUT, watcher,
                localSessionId, localSessionPwd);
        watcher.waitForConnected(CONNECTION_TIMEOUT);

        // The created ephemeral nodes are still around.
        for (int i = 0; i < 5; i++) {
            Assert.assertNotNull(zk.exists(nodePrefix + i, null));
        }

        // When we explicitly close the session, we should not be able to
        // reconnect with the same session id
        zk.close();

        try {
            watcher.reset();
            zk = new DisconnectableZooKeeper(
                    hostPorts[otherFollowerIdx], CONNECTION_TIMEOUT, watcher,
                    localSessionId, localSessionPwd);
            zk.exists(nodePrefix + "0", null);
            Assert.fail("Reconnecting to a closed session ID should fail.");
        } catch (KeeperException.SessionExpiredException e) {
        }

        watcher.reset();
        // And the ephemeral nodes will be gone since the session died.
        zk = new DisconnectableZooKeeper(
                hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher);
        watcher.waitForConnected(CONNECTION_TIMEOUT);
        for (int i = 0; i < 5; i++) {
            Assert.assertNull(zk.exists(nodePrefix + i, null));
        }
    }
View Full Code Here

Examples of org.apache.zookeeper.test.ClientBase.CountdownWatcher

        Assert.assertFalse("No leader in quorum?", leaderIdx == -1);
        int followerIdx = (leaderIdx + 1) % 5;
        int testPeerIdx = testLeader ? leaderIdx : followerIdx;
        String hostPorts[] = qb.hostPort.split(",");

        CountdownWatcher watcher = new CountdownWatcher();
        ZooKeeper zk = qb.createClient(watcher, hostPorts[testPeerIdx],
                CONNECTION_TIMEOUT);
        watcher.waitForConnected(CONNECTION_TIMEOUT);

        final String firstPath = "/first";
        final String secondPath = "/ephemeral";

        // Just create some node so that we know the current zxid
View Full Code Here

Examples of org.apache.zookeeper.test.ClientBase.CountdownWatcher

    /**
     * Test write operations using multi request.
     */
    @Test(timeout = 90000)
    public void testMultiTransaction() throws Exception {
        CountdownWatcher watcher = new CountdownWatcher();
        ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
                watcher, true);
        watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected

        final String data = "Data to be read in RO mode";
        final String node1 = "/tnode1";
        final String node2 = "/tnode2";
        zk.create(node1, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);

        watcher.reset();
        qu.shutdown(2);
        watcher.waitForConnected(CONNECTION_TIMEOUT);
        Assert.assertEquals("Should be in r-o mode", States.CONNECTEDREADONLY,
                zk.getState());

        // read operation during r/o mode
        String remoteData = new String(zk.getData(node1, false, null));
View Full Code Here

Examples of org.apache.zookeeper.test.ClientBase.CountdownWatcher

     * Basic test of read-only client functionality. Tries to read and write
     * during read-only mode, then regains a quorum and tries to write again.
     */
    @Test(timeout = 90000)
    public void testReadOnlyClient() throws Exception {
        CountdownWatcher watcher = new CountdownWatcher();
        ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
                watcher, true);
        watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected

        final String data = "Data to be read in RO mode";
        final String node = "/tnode";
        zk.create(node, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);

        watcher.reset();
        qu.shutdown(2);
        zk.close();

        // Re-connect the client (in case we were connected to the shut down
        // server and the local session was not persisted).
        zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
                watcher, true);
        watcher.waitForConnected(CONNECTION_TIMEOUT);

        // read operation during r/o mode
        String remoteData = new String(zk.getData(node, false, null));
        Assert.assertEquals(data, remoteData);

        try {
            zk.setData(node, "no way".getBytes(), -1);
            Assert.fail("Write operation has succeeded during RO mode");
        } catch (NotReadOnlyException e) {
            // ok
        }

        watcher.reset();
        qu.start(2);
        Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(
                "127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
        zk.close();
        watcher.reset();

        // Re-connect the client (in case we were connected to the shut down
        // server and the local session was not persisted).
        zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
                watcher, true);
        watcher.waitForConnected(CONNECTION_TIMEOUT);
        zk.setData(node, "We're in the quorum now".getBytes(), -1);

        zk.close();
    }
View Full Code Here

Examples of org.apache.zookeeper.test.ClientBase.CountdownWatcher

     */
    @Test(timeout = 90000)
    public void testSessionEstablishment() throws Exception {
        qu.shutdown(2);

        CountdownWatcher watcher = new CountdownWatcher();
        ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
                watcher, true);
        watcher.waitForConnected(CONNECTION_TIMEOUT);
        Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk
                .getState());
        long fakeId = zk.getSessionId();

        watcher.reset();
        qu.start(2);
        Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(
                "127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
        watcher.waitForConnected(CONNECTION_TIMEOUT);
        zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
        Assert.assertFalse("fake session and real session have same id", zk
                .getSessionId() == fakeId);

View Full Code Here

Examples of org.apache.zookeeper.test.ClientBase.CountdownWatcher

        Logger zlogger = Logger.getLogger("org.apache.zookeeper");
        zlogger.addAppender(appender);

        try {
            qu.shutdown(2);
            CountdownWatcher watcher = new CountdownWatcher();
            ZooKeeper zk = new ZooKeeper(qu.getConnString(),
                    CONNECTION_TIMEOUT, watcher, true);
            watcher.waitForConnected(CONNECTION_TIMEOUT);

            // if we don't suspend a peer it will rejoin a quorum
            qu.getPeer(1).peer.suspend();

            // start two servers to form a quorum; client should detect this and
            // connect to one of them
            watcher.reset();
            qu.start(2);
            qu.start(3);
            ClientBase.waitForServerUp(qu.getConnString(), 2000);
            watcher.waitForConnected(CONNECTION_TIMEOUT);
            zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                    CreateMode.PERSISTENT);

            // resume poor fellow
            qu.getPeer(1).peer.resume();
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.