Search in sources :

Example 16 with CuratorListener

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

the class AnnouncerTest method testSessionKilled.

@Test(timeout = 60_000L)
public void testSessionKilled() throws Exception {
    curator.start();
    curator.blockUntilConnected();
    Announcer announcer = new Announcer(curator, exec);
    try {
        curator.inTransaction().create().forPath("/somewhere").and().commit();
        announcer.start();
        final byte[] billy = "billy".getBytes();
        final String testPath1 = "/test1";
        final String testPath2 = "/somewhere/test2";
        final Set<String> paths = Sets.newHashSet(testPath1, testPath2);
        announcer.announce(testPath1, billy);
        announcer.announce(testPath2, billy);
        Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertArrayEquals(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) {
                    paths.remove(event.getPath());
                    if (paths.isEmpty()) {
                        latch.countDown();
                    }
                }
            }
        });
        KillSession.kill(curator.getZookeeperClient().getZooKeeper(), server.getConnectString());
        Assert.assertTrue(timing.forWaiting().awaitLatch(latch));
        Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath2));
        announcer.stop();
        while ((curator.checkExists().forPath(testPath1) != null) || (curator.checkExists().forPath(testPath2) != null)) {
            Thread.sleep(100);
        }
        Assert.assertNull(curator.checkExists().forPath(testPath1));
        Assert.assertNull(curator.checkExists().forPath(testPath2));
    } finally {
        announcer.stop();
    }
}
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 17 with CuratorListener

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

the class Zookeeper method mkClientImpl.

public CuratorFramework mkClientImpl(Map conf, List<String> servers, Object port, String root, final WatcherCallBack watcher, Map authConf) {
    CuratorFramework fk;
    if (authConf != null) {
        fk = Utils.newCurator(conf, servers, port, root, new ZookeeperAuthInfo(authConf));
    } else {
        fk = Utils.newCurator(conf, servers, port, root);
    }
    fk.getCuratorListenable().addListener(new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework _fk, CuratorEvent e) throws Exception {
            if (e.getType().equals(CuratorEventType.WATCHED)) {
                WatchedEvent event = e.getWatchedEvent();
                watcher.execute(event.getState(), event.getType(), event.getPath());
            }
        }
    });
    LOG.info("Staring ZK Curator");
    fk.start();
    return fk;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) ZookeeperAuthInfo(org.apache.storm.utils.ZookeeperAuthInfo) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) BindException(java.net.BindException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) AuthorizationException(org.apache.storm.generated.AuthorizationException) KeyNotFoundException(org.apache.storm.generated.KeyNotFoundException)

Example 18 with CuratorListener

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

the class TestFramework method testBackgroundCreate.

@Test
public void testBackgroundCreate() 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.CREATE) {
                    Assert.assertEquals(event.getPath(), "/test");
                    ((CountDownLatch) event.getContext()).countDown();
                }
            }
        });
        CountDownLatch latch = new CountDownLatch(1);
        client.create().inBackground(latch).forPath("/test", new byte[] { 1, 2, 3 });
        Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    } 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)

Example 19 with CuratorListener

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

the class TestBlockUntilConnected method testBlockUntilConnectedSessionExpired.

/**
 * Test that we got disconnected before calling blockUntilConnected and we reconnect we receive a session expired event.
 */
@Test
public void testBlockUntilConnectedSessionExpired() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).build();
    final CountDownLatch lostLatch = new CountDownLatch(1);
    client.getConnectionStateListenable().addListener(new ConnectionStateListener() {

        @Override
        public void stateChanged(CuratorFramework client, ConnectionState newState) {
            if (newState == ConnectionState.LOST) {
                lostLatch.countDown();
            }
        }
    });
    final CountDownLatch expiredLatch = new CountDownLatch(1);
    client.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.Expired) {
                expiredLatch.countDown();
            }
        }
    });
    ConnectionStateAccessor.setDebugWaitOnExpiredForClient(client);
    try {
        client.start();
        // Block until we're connected
        Assert.assertTrue(client.blockUntilConnected(5, TimeUnit.SECONDS), "Failed to connect");
        final long sessionTimeoutMs = client.getZookeeperClient().getConnectionTimeoutMs();
        // Kill the server
        CloseableUtils.closeQuietly(server);
        // Wait until we hit the lost state
        Assert.assertTrue(timing.awaitLatch(lostLatch), "Failed to reach LOST state");
        Thread.sleep(sessionTimeoutMs);
        server = new TestingServer(server.getPort(), server.getTempDirectory());
        // Wait until we get expired event
        Assert.assertTrue(timing.awaitLatch(expiredLatch), "Failed to get Expired event");
        final boolean blockUntilConnected5Seconds = client.blockUntilConnected(5, TimeUnit.SECONDS);
        Assert.assertTrue(client.getZookeeperClient().isConnected(), "ConnectionState.isConnected returned false");
        Assert.assertTrue(blockUntilConnected5Seconds, "BlockUntilConnected returned false");
    } catch (Exception e) {
        Assert.fail("Unexpected exception " + e);
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) RetryOneTime(org.apache.curator.retry.RetryOneTime) 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) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 20 with CuratorListener

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

the class CrudExamples method setDataAsync.

public static void setDataAsync(CuratorFramework client, String path, byte[] payload) throws Exception {
    // this is one method of getting event/async notifications
    CuratorListener listener = new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
        // examine event for details
        }
    };
    client.getCuratorListenable().addListener(listener);
    // set data for the given node asynchronously. The completion notification
    // is done via the CuratorListener.
    client.setData().inBackground().forPath(path, payload);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent)

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