Search in sources :

Example 56 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class TestZK method testChildWatches.

@Test
public void testChildWatches() throws Exception {
    ZooKeeper zk = getClient(0);
    ZooKeeper zk2 = getClient(1);
    final Semaphore sem = new Semaphore(0);
    zk.exists("/foo", new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.NodeCreated) {
                sem.release();
                System.out.println(event);
            }
        }
    });
    zk2.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    sem.tryAcquire(5, TimeUnit.SECONDS);
    zk.create("/foo2", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk2.exists("/foo2", new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.NodeDeleted) {
                sem.release();
                System.out.println(event);
            }
        }
    });
    zk.delete("/foo2", -1);
    sem.acquire();
}
Also used : WatchedEvent(org.apache.zookeeper_voltpatches.WatchedEvent) ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) Watcher(org.apache.zookeeper_voltpatches.Watcher) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 57 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class ZKTestBase method getClient.

protected ZooKeeper getClient(int site) throws Exception {
    final Semaphore permit = new Semaphore(0);
    int clientPort = m_siteIdToZKPort.get(site);
    ZooKeeper keeper = new ZooKeeper("127.0.0.1:" + Integer.toString(clientPort), 4000, new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getState() == KeeperState.SyncConnected) {
                permit.release();
            }
            System.out.println(event);
        }
    }, Sets.<Long>newHashSet());
    m_clients.add(keeper);
    permit.acquire();
    return keeper;
}
Also used : WatchedEvent(org.apache.zookeeper_voltpatches.WatchedEvent) ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) Watcher(org.apache.zookeeper_voltpatches.Watcher) Semaphore(java.util.concurrent.Semaphore)

Example 58 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class TestZK method testBasic.

@Test
public void testBasic() throws Exception {
    ZooKeeper zk = getClient(0);
    zk.create("/foo", new byte[1], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    ZooKeeper zk2 = getClient(1);
    Stat stat = new Stat();
    assertEquals(1, zk2.getData("/foo", false, stat).length);
    zk2.setData("/foo", new byte[4], stat.getVersion());
    assertEquals(4, zk.getData("/foo", false, stat).length);
    zk.delete("/foo", -1);
    zk2.create("/bar", new byte[6], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
    zk.create("/bar", new byte[7], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
    ZooKeeper zk3 = getClient(2);
    List<String> children = zk3.getChildren("/", false);
    System.out.println(children);
    assertEquals(4, children.size());
    assertTrue(children.contains("zookeeper"));
    assertTrue(children.contains("bar0000000003"));
    assertTrue(children.contains("bar0000000004"));
    assertTrue(children.contains("core"));
    zk.close();
    zk2.close();
    m_clients.clear();
    m_clients.add(zk3);
    children = zk3.getChildren("/", false);
    System.out.println(children);
    assertEquals(2, children.size());
    assertTrue(children.contains("zookeeper"));
    assertTrue(children.contains("core"));
}
Also used : ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) Stat(org.apache.zookeeper_voltpatches.data.Stat) Test(org.junit.Test)

Example 59 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class TestZK method testNonLeaderFailure.

@Test
public void testNonLeaderFailure() throws Exception {
    ZooKeeper zk = getClient(0);
    ZooKeeper zk2 = getClient(1);
    ZooKeeper zk3 = getClient(2);
    zk.create("/election", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    final Semaphore sem3 = new Semaphore(0);
    LeaderNoticeHandler r3 = new LeaderNoticeHandler() {

        @Override
        public void becomeLeader() {
            sem3.release();
        }

        @Override
        public void noticedTopologyChange(boolean added, boolean removed) {
        }
    };
    LeaderElector elector1 = new LeaderElector(zk, "/election", "node", new byte[0], null);
    LeaderElector elector2 = new LeaderElector(zk2, "/election", "node", new byte[0], null);
    LeaderElector elector3 = new LeaderElector(zk3, "/election", "node", new byte[0], r3);
    elector1.start(true);
    elector2.start(true);
    elector3.start(true);
    assertTrue(elector1.isLeader());
    assertFalse(elector2.isLeader());
    assertFalse(elector3.isLeader());
    // 1 is still the leader
    elector2.shutdown();
    zk2.close();
    assertTrue(elector1.isLeader());
    assertFalse(elector3.isLeader());
    // 3 should become the leader now
    elector1.shutdown();
    zk.close();
    assertTrue(sem3.tryAcquire(5, TimeUnit.SECONDS));
    assertTrue(elector3.isLeader());
    assertEquals(0, sem3.availablePermits());
    elector3.shutdown();
    zk3.close();
}
Also used : ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 60 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class TestMapCache method testAddChildWithPutWithCallback.

@Test
public void testAddChildWithPutWithCallback() throws Exception {
    ZooKeeper zk = getClient(0);
    configure("/cache04", zk);
    TestCallback cb = new TestCallback();
    MapCache dut = new MapCache(zk, "/cache04", cb);
    dut.start(true);
    Map<String, JSONObject> cache = cb.m_cache;
    JSONObject dd = new JSONObject("{key:ddval}");
    dut.put("dd", dd);
    while (true) {
        cache = cb.m_cache;
        if (cache.size() == 3) {
            Thread.sleep(1);
        } else {
            break;
        }
    }
    assertEquals("Item added", 4, cache.size());
    assertEquals("aaval", cache.get("/cache04/aa").get("key"));
    assertEquals("bbval", cache.get("/cache04/bb").get("key"));
    assertEquals("ccval", cache.get("/cache04/cc").get("key"));
    assertEquals("ddval", cache.get("/cache04/dd").get("key"));
    // modify the new child and make sure it has a watch set.
    JSONObject dd2 = new JSONObject("{key:ddval2}");
    dut.put("dd", dd2);
    while (true) {
        cache = cb.m_cache;
        if (cache.get("/cache04/dd").get("key").equals("ddval2")) {
            break;
        }
    }
    assertEquals("Items accounted for.", 4, cache.size());
    assertEquals("ddval2", cache.get("/cache04/dd").get("key"));
    dut.shutdown();
    zk.close();
}
Also used : ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) JSONObject(org.json_voltpatches.JSONObject) Test(org.junit.Test)

Aggregations

ZooKeeper (org.apache.zookeeper_voltpatches.ZooKeeper)62 Test (org.junit.Test)38 KeeperException (org.apache.zookeeper_voltpatches.KeeperException)14 JSONObject (org.json_voltpatches.JSONObject)14 Semaphore (java.util.concurrent.Semaphore)11 Stat (org.apache.zookeeper_voltpatches.data.Stat)10 ExecutionException (java.util.concurrent.ExecutionException)8 HostMessenger (org.voltcore.messaging.HostMessenger)8 IOException (java.io.IOException)7 JSONException (org.json_voltpatches.JSONException)7 WatchedEvent (org.apache.zookeeper_voltpatches.WatchedEvent)5 Watcher (org.apache.zookeeper_voltpatches.Watcher)5 VoltTable (org.voltdb.VoltTable)5 SettingsException (org.voltdb.settings.SettingsException)5 List (java.util.List)4 Map (java.util.Map)4 HostAndPort (com.google_voltpatches.common.net.HostAndPort)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 SocketException (java.net.SocketException)3 ImmutableMap (com.google_voltpatches.common.collect.ImmutableMap)2