Search in sources :

Example 1 with CuratorListener

use of org.apache.curator.framework.api.CuratorListener 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 2 with CuratorListener

use of org.apache.curator.framework.api.CuratorListener 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 3 with CuratorListener

use of org.apache.curator.framework.api.CuratorListener 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 4 with CuratorListener

use of org.apache.curator.framework.api.CuratorListener in project BRFS by zhangnianli.

the class CuratorZookeeperClientTest method testCuratorListener.

// public void testGetClientInstance() throws Exception {
// CuratorZookeeperClient client = CuratorZookeeperClient.getClientInstance(zkUrl, retry, sessionTimeoutMs, connectionTimeoutMs, isWaitConnection);
// assertNotNull(client);
// client.close();
// }
// 
// public void testCurd() throws Exception {
// boolean flag = false;
// CuratorZookeeperClient client = CuratorZookeeperClient.getClientInstance(zkUrl);
// 
// client.createEphemeral("/brfs/wz/test/createEphemeral", true);
// flag = client.checkExists("/brfs/wz/test/createEphemeral");
// assertEquals(flag, true);
// 
// client.createPersistent("/brfs/wz/test/createPersistent", true);
// flag = client.checkExists("/brfs/wz/test/createPersistent");
// assertEquals(flag, true);
// 
// client.guaranteedDelete("/brfs/wz/test/createPersistent", false);
// flag = client.checkExists("/brfs/wz/test/createPersistent");
// assertEquals(flag, false);
// 
// client.setData("/brfs/wz/test/createEphemeral", "createEphemeral".getBytes());
// 
// assertEquals("createEphemeral", new String(client.getData("/brfs/wz/test/createEphemeral")));
// 
// client.close();
// }
// 
// public void testWatcher() throws Exception {
// 
// ExecutorService serverThreads = Executors.newFixedThreadPool(10);
// final CuratorZookeeperClient client = CuratorZookeeperClient.getClientInstance(zkUrl);
// if (!client.checkExists("/brfs/wz/servers")) {
// client.createPersistent("/brfs/wz/servers", true);
// }
// MyWatcher watcher = new MyWatcher(client);
// System.out.println(client.watchedGetChildren("/brfs/wz/servers", watcher));
// 
// for (int i = 0; i < 10; i++) {
// final int count = i;
// serverThreads.execute(new Runnable() {
// //
// @Override
// public void run() {
// synchronized (client) {
// client.createEphemeral("/brfs/wz/servers/server" + count, true);
// }
// }
// });
// }
// serverThreads.shutdown();
// serverThreads.awaitTermination(1, TimeUnit.DAYS);
// client.close();
// }
// 
// public class MyWatcher implements Watcher {
// 
// private final CuratorZookeeperClient client;
// 
// public MyWatcher(CuratorZookeeperClient client) {
// 
// this.client = client;
// }
// 
// @Override
// public void process(WatchedEvent event) {
// if (event.getType() == EventType.NodeChildrenChanged) {
// List<String> tmps = client.watchedGetChildren(event.getPath(), this);
// System.out.println(1111);
// System.out.println(tmps);
// }
// }
// 
// }
public void testCuratorListener() throws Exception {
    final CuratorClient client = CuratorClient.getClientInstance(zkUrl);
    CuratorFramework curatorClient = client.getInnerClient();
    // curatorClient.getChildren().inBackground(new BackgroundCallback() {
    // 
    // @Override
    // public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
    // System.out.println("aaaaa" + event.getChildren());
    // }
    // }).forPath("/brfs/wz");
    curatorClient.getCuratorListenable().addListener(new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
            System.out.println("CuratorListener1--" + event.getPath() + "--" + event.getWatchedEvent());
        }
    });
    curatorClient.getCuratorListenable().addListener(new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
            System.out.println("CuratorListener2--" + event.getPath() + "--" + event.getWatchedEvent());
        }
    });
    curatorClient.setData().inBackground().forPath("/yupeng/yupeng/yupeng", "aaa".getBytes());
    Thread.sleep(2000);
    client.close();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent)

Example 5 with CuratorListener

use of org.apache.curator.framework.api.CuratorListener in project xian by happyyangyuan.

the class TestFramework method testBackgroundDelete.

@Test
public void testBackgroundDelete() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    try {
        client.getCuratorListenable().addListener(new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.DELETE) {
                    Assert.assertEquals(event.getPath(), "/head");
                    ((CountDownLatch) event.getContext()).countDown();
                }
            }
        });
        client.create().forPath("/head");
        Assert.assertNotNull(client.checkExists().forPath("/head"));
        CountDownLatch latch = new CountDownLatch(1);
        client.delete().inBackground(latch).forPath("/head");
        Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
        Assert.assertNull(client.checkExists().forPath("/head"));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)23 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)23 CuratorListener (org.apache.curator.framework.api.CuratorListener)23 CountDownLatch (java.util.concurrent.CountDownLatch)16 RetryOneTime (org.apache.curator.retry.RetryOneTime)9 KeeperException (org.apache.zookeeper.KeeperException)9 Test (org.testng.annotations.Test)9 Test (org.junit.Test)6 WatchedEvent (org.apache.zookeeper.WatchedEvent)3 IOException (java.io.IOException)2 Map (java.util.Map)2 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)2 BindException (java.net.BindException)1 UnknownHostException (java.net.UnknownHostException)1 TreeMap (java.util.TreeMap)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)1 UnhandledErrorListener (org.apache.curator.framework.api.UnhandledErrorListener)1 CuratorOp (org.apache.curator.framework.api.transaction.CuratorOp)1 CuratorTransactionResult (org.apache.curator.framework.api.transaction.CuratorTransactionResult)1