Search in sources :

Example 1 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent in project hadoop by apache.

the class TestCuratorService method testBackgroundDelete.

@Test
public void testBackgroundDelete() throws Throwable {
    mkPath("/rm", CreateMode.PERSISTENT);
    mkPath("/rm/child", CreateMode.PERSISTENT);
    CuratorEventCatcher events = new CuratorEventCatcher();
    curatorService.zkDelete("/rm", true, events);
    CuratorEvent taken = events.take();
    LOG.info("took {}", taken);
    assertEquals(1, events.getCount());
}
Also used : CuratorEvent(org.apache.curator.framework.api.CuratorEvent) Test(org.junit.Test) AbstractZKRegistryTest(org.apache.hadoop.registry.AbstractZKRegistryTest)

Example 2 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent in project druid by druid-io.

the class AnnouncerTest method testSanity.

@Test(timeout = 60_000L)
public void testSanity() throws Exception {
    curator.start();
    curator.blockUntilConnected();
    Announcer announcer = new Announcer(curator, exec);
    final byte[] billy = "billy".getBytes();
    final String testPath1 = "/test1";
    final String testPath2 = "/somewhere/test2";
    announcer.announce(testPath1, billy);
    Assert.assertNull("/test1 does not exists", curator.checkExists().forPath(testPath1));
    Assert.assertNull("/somewhere/test2 does not exists", curator.checkExists().forPath(testPath2));
    announcer.start();
    try {
        Assert.assertArrayEquals("/test1 has data", billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertNull("/somewhere/test2 still does not exist", curator.checkExists().forPath(testPath2));
        announcer.announce(testPath2, billy);
        Assert.assertArrayEquals("/test1 still has data", billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertArrayEquals("/somewhere/test2 has data", billy, curator.getData().decompressed().forPath(testPath2));
        final CountDownLatch latch = new CountDownLatch(1);
        curator.getCuratorListenable().addListener(new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.CREATE && event.getPath().equals(testPath1)) {
                    latch.countDown();
                }
            }
        });
        curator.inTransaction().delete().forPath(testPath1).and().commit();
        Assert.assertTrue("Wait for /test1 to be created", timing.forWaiting().awaitLatch(latch));
        Assert.assertArrayEquals("expect /test1 data is restored", billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertArrayEquals("expect /somewhere/test2 is still there", billy, curator.getData().decompressed().forPath(testPath2));
        announcer.unannounce(testPath1);
        Assert.assertNull("expect /test1 unannounced", curator.checkExists().forPath(testPath1));
        Assert.assertArrayEquals("expect /somewhere/test2 is still still there", billy, curator.getData().decompressed().forPath(testPath2));
    } finally {
        announcer.stop();
    }
    Assert.assertNull("expect /test1 remains unannounced", curator.checkExists().forPath(testPath1));
    Assert.assertNull("expect /somewhere/test2 unannounced", curator.checkExists().forPath(testPath2));
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent in project druid by druid-io.

the class AnnouncerTest method awaitAnnounce.

private void awaitAnnounce(final Announcer announcer, final String path, final byte[] bytes, boolean removeParentsIfCreated) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    curator.getCuratorListenable().addListener(new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
            if (event.getType() == CuratorEventType.CREATE && event.getPath().equals(path)) {
                latch.countDown();
            }
        }
    });
    announcer.announce(path, bytes, removeParentsIfCreated);
    latch.await();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent in project druid by druid-io.

the class CuratorInventoryManagerTest method testSanity.

@Test
public void testSanity() throws Exception {
    final MapStrategy strategy = new MapStrategy();
    CuratorInventoryManager<Map<String, Integer>, Integer> manager = new CuratorInventoryManager<Map<String, Integer>, Integer>(curator, new StringInventoryManagerConfig("/container", "/inventory"), exec, strategy);
    curator.start();
    curator.blockUntilConnected();
    manager.start();
    Assert.assertTrue(Iterables.isEmpty(manager.getInventory()));
    CountDownLatch containerLatch = new CountDownLatch(1);
    strategy.setNewContainerLatch(containerLatch);
    curator.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/container/billy", new byte[] {});
    Assert.assertTrue(timing.awaitLatch(containerLatch));
    strategy.setNewContainerLatch(null);
    final Iterable<Map<String, Integer>> inventory = manager.getInventory();
    Assert.assertTrue(Iterables.getOnlyElement(inventory).isEmpty());
    CountDownLatch inventoryLatch = new CountDownLatch(2);
    strategy.setNewInventoryLatch(inventoryLatch);
    curator.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/inventory/billy/1", Ints.toByteArray(100));
    curator.create().withMode(CreateMode.EPHEMERAL).forPath("/inventory/billy/bob", Ints.toByteArray(2287));
    Assert.assertTrue(timing.awaitLatch(inventoryLatch));
    strategy.setNewInventoryLatch(null);
    verifyInventory(manager);
    CountDownLatch deleteLatch = new CountDownLatch(1);
    strategy.setDeadInventoryLatch(deleteLatch);
    curator.delete().forPath("/inventory/billy/1");
    Assert.assertTrue(timing.awaitLatch(deleteLatch));
    strategy.setDeadInventoryLatch(null);
    Assert.assertEquals(1, manager.getInventoryValue("billy").size());
    Assert.assertEquals(2287, manager.getInventoryValue("billy").get("bob").intValue());
    inventoryLatch = new CountDownLatch(1);
    strategy.setNewInventoryLatch(inventoryLatch);
    curator.create().withMode(CreateMode.EPHEMERAL).forPath("/inventory/billy/1", Ints.toByteArray(100));
    Assert.assertTrue(timing.awaitLatch(inventoryLatch));
    strategy.setNewInventoryLatch(null);
    verifyInventory(manager);
    final CountDownLatch latch = new CountDownLatch(1);
    curator.getCuratorListenable().addListener(new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
            if (event.getType() == CuratorEventType.WATCHED && event.getWatchedEvent().getState() == Watcher.Event.KeeperState.Disconnected) {
                latch.countDown();
            }
        }
    });
    server.stop();
    Assert.assertTrue(timing.awaitLatch(latch));
    verifyInventory(manager);
    // Wait a bit
    Thread.sleep(50);
    verifyInventory(manager);
}
Also used : CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) Map(java.util.Map) Test(org.junit.Test)

Example 5 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent in project heron by twitter.

the class CuratorStateManager method getNodeData.

@Override
protected <M extends Message> ListenableFuture<M> getNodeData(WatchCallback watcher, String path, final Message.Builder builder) {
    final SettableFuture<M> future = SettableFuture.create();
    Watcher wc = ZkWatcherCallback.makeZkWatcher(watcher);
    BackgroundCallback cb = new BackgroundCallback() {

        @Override
        // we don't know what M is until runtime
        @SuppressWarnings("unchecked")
        public void processResult(CuratorFramework aClient, CuratorEvent event) throws Exception {
            byte[] data;
            if (event != null & (data = event.getData()) != null) {
                builder.mergeFrom(data);
                future.set((M) builder.build());
            } else {
                future.setException(new RuntimeException("Failed to fetch data from path: " + event.getPath()));
            }
        }
    };
    try {
        client.getData().usingWatcher(wc).inBackground(cb).forPath(path);
    // Suppress it since forPath() throws Exception
    // SUPPRESS CHECKSTYLE IllegalCatch
    } catch (Exception e) {
        future.setException(new RuntimeException("Could not getNodeData", e));
    }
    return future;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Watcher(org.apache.zookeeper.Watcher) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) KeeperException(org.apache.zookeeper.KeeperException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

CuratorEvent (org.apache.curator.framework.api.CuratorEvent)58 CuratorFramework (org.apache.curator.framework.CuratorFramework)55 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)34 CountDownLatch (java.util.concurrent.CountDownLatch)32 CuratorListener (org.apache.curator.framework.api.CuratorListener)23 Test (org.testng.annotations.Test)21 RetryOneTime (org.apache.curator.retry.RetryOneTime)18 KeeperException (org.apache.zookeeper.KeeperException)13 Test (org.junit.Test)11 Watcher (org.apache.zookeeper.Watcher)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Timing (org.apache.curator.test.Timing)4 WatchedEvent (org.apache.zookeeper.WatchedEvent)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Message (com.google.protobuf.Message)3 IOException (java.io.IOException)3 Map (java.util.Map)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeUnit (java.util.concurrent.TimeUnit)3 BackgroundPathable (org.apache.curator.framework.api.BackgroundPathable)3