Search in sources :

Example 16 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.

the class ReconfigTest method createHandles.

public static ZooKeeper[] createHandles(QuorumUtil qu) throws IOException {
    // create an extra handle, so we can index the handles from 1 to qu.ALL
    // using the server id.
    ZooKeeper[] zkArr = new ZooKeeper[qu.ALL + 1];
    // not used.
    zkArr[0] = null;
    for (int i = 1; i <= qu.ALL; i++) {
        // server ids are 1, 2 and 3
        zkArr[i] = new ZooKeeper("127.0.0.1:" + qu.getPeer(i).peer.getClientPort(), ClientBase.CONNECTION_TIMEOUT, new Watcher() {

            public void process(WatchedEvent event) {
            }
        });
    }
    return zkArr;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher)

Example 17 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.

the class QuorumTest method testLeaderShutdown.

@Test
public void testLeaderShutdown() throws IOException, InterruptedException, KeeperException {
    ZooKeeper zk = new DisconnectableZooKeeper(qb.hostPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {

        public void process(WatchedEvent event) {
        }
    });
    zk.create("/blah", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/blah/blah", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Leader leader = qb.s1.leader;
    if (leader == null)
        leader = qb.s2.leader;
    if (leader == null)
        leader = qb.s3.leader;
    if (leader == null)
        leader = qb.s4.leader;
    if (leader == null)
        leader = qb.s5.leader;
    Assert.assertNotNull(leader);
    for (int i = 0; i < 5000; i++) {
        zk.setData("/blah/blah", new byte[0], -1, new AsyncCallback.StatCallback() {

            public void processResult(int rc, String path, Object ctx, Stat stat) {
                counter++;
                if (rc != 0) {
                    errors++;
                }
            }
        }, null);
    }
    for (LearnerHandler f : leader.getForwardingFollowers()) {
        f.getSocket().shutdownInput();
    }
    for (int i = 0; i < 5000; i++) {
        zk.setData("/blah/blah", new byte[0], -1, new AsyncCallback.StatCallback() {

            public void processResult(int rc, String path, Object ctx, Stat stat) {
                counter++;
                if (rc != 0) {
                    errors++;
                }
            }
        }, null);
    }
    // check if all the followers are alive
    Assert.assertTrue(qb.s1.isAlive());
    Assert.assertTrue(qb.s2.isAlive());
    Assert.assertTrue(qb.s3.isAlive());
    Assert.assertTrue(qb.s4.isAlive());
    Assert.assertTrue(qb.s5.isAlive());
    zk.close();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) LearnerHandler(org.apache.zookeeper.server.quorum.LearnerHandler) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) Leader(org.apache.zookeeper.server.quorum.Leader) AsyncCallback(org.apache.zookeeper.AsyncCallback) Watcher(org.apache.zookeeper.Watcher) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) Test(org.junit.Test)

Example 18 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.

the class QuorumZxidSyncTest method testLateLogs.

/**
     * find out what happens when the latest state is in the snapshots not
     * the logs.
     */
@Test
public void testLateLogs() throws Exception {
    // crank up the epoch numbers
    ClientBase.waitForServerUp(qb.hostPort, 10000);
    ClientBase.waitForServerUp(qb.hostPort, 10000);
    ZooKeeper zk = new ZooKeeper(qb.hostPort, 10000, new Watcher() {

        public void process(WatchedEvent event) {
        }
    });
    zk.create("/0", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.close();
    qb.shutdownServers();
    qb.startServers();
    ClientBase.waitForServerUp(qb.hostPort, 10000);
    qb.shutdownServers();
    qb.startServers();
    ClientBase.waitForServerUp(qb.hostPort, 10000);
    zk = new ZooKeeper(qb.hostPort, 10000, new Watcher() {

        public void process(WatchedEvent event) {
        }
    });
    zk.create("/1", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.close();
    qb.shutdownServers();
    qb.startServers();
    ClientBase.waitForServerUp(qb.hostPort, 10000);
    qb.shutdownServers();
    deleteLogs(qb.s1dir);
    deleteLogs(qb.s2dir);
    deleteLogs(qb.s3dir);
    deleteLogs(qb.s4dir);
    deleteLogs(qb.s5dir);
    qb.startServers();
    ClientBase.waitForServerUp(qb.hostPort, 10000);
    zk = new ZooKeeper(qb.hostPort, 10000, new Watcher() {

        public void process(WatchedEvent event) {
        }
    });
    zk.create("/2", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.close();
    qb.shutdownServers();
    qb.startServers();
    ClientBase.waitForServerUp(qb.hostPort, 10000);
    zk = new ZooKeeper(qb.hostPort, 10000, new Watcher() {

        public void process(WatchedEvent event) {
        }
    });
    boolean saw2 = false;
    for (String child : zk.getChildren("/", false)) {
        if (child.equals("2")) {
            saw2 = true;
        }
    }
    zk.close();
    Assert.assertTrue("Didn't see /2 (went back in time)", saw2);
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) Test(org.junit.Test)

Example 19 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.

the class WatchedEventTest method testCreatingWatchedEvent.

@Test
public void testCreatingWatchedEvent() {
    // EventWatch is a simple, immutable type, so all we need to do
    // is make sure we can create all possible combinations of values.
    EnumSet<EventType> allTypes = EnumSet.allOf(EventType.class);
    EnumSet<KeeperState> allStates = EnumSet.allOf(KeeperState.class);
    WatchedEvent we;
    for (EventType et : allTypes) {
        for (KeeperState ks : allStates) {
            we = new WatchedEvent(et, ks, "blah");
            Assert.assertEquals(et, we.getType());
            Assert.assertEquals(ks, we.getState());
            Assert.assertEquals("blah", we.getPath());
        }
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) EventType(org.apache.zookeeper.Watcher.Event.EventType) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) Test(org.junit.Test)

Example 20 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.

the class WatchedEventTest method testCreatingWatchedEventFromInvalidWrapper.

@Test
public void testCreatingWatchedEventFromInvalidWrapper() {
    try {
        WatcherEvent wep = new WatcherEvent(-2342, -252352, "foo");
        new WatchedEvent(wep);
        Assert.fail("Was able to create WatchedEvent from bad wrapper");
    } catch (RuntimeException re) {
    // we're good
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) WatcherEvent(org.apache.zookeeper.proto.WatcherEvent) Test(org.junit.Test)

Aggregations

WatchedEvent (org.apache.zookeeper.WatchedEvent)97 Watcher (org.apache.zookeeper.Watcher)61 ZooKeeper (org.apache.zookeeper.ZooKeeper)37 KeeperException (org.apache.zookeeper.KeeperException)36 Test (org.junit.Test)32 Stat (org.apache.zookeeper.data.Stat)28 CountDownLatch (java.util.concurrent.CountDownLatch)24 Test (org.testng.annotations.Test)14 IOException (java.io.IOException)12 AsyncCallback (org.apache.zookeeper.AsyncCallback)11 None (com.linkedin.common.util.None)7 List (java.util.List)6 KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)5 TimeoutException (java.util.concurrent.TimeoutException)4 SolrZkClient (org.apache.solr.common.cloud.SolrZkClient)4 WatcherEvent (org.apache.zookeeper.proto.WatcherEvent)4 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)4 ZooKeeperx (com.alibaba.otter.shared.common.utils.zookeeper.ZooKeeperx)3 FutureCallback (com.linkedin.common.callback.FutureCallback)3 ArrayList (java.util.ArrayList)3