Search in sources :

Example 66 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project xian by happyyangyuan.

the class TestLockCleanlinessWithFaults method testNodeDeleted.

@Test
public void testNodeDeleted() throws Exception {
    final String PATH = "/foo/bar";
    CuratorFramework client = null;
    try {
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryNTimes(0, 0));
        client.start();
        client.create().creatingParentsIfNeeded().forPath(PATH);
        Assert.assertEquals(client.checkExists().forPath(PATH).getNumChildren(), 0);
        LockInternals internals = new LockInternals(client, new StandardLockInternalsDriver(), PATH, "lock-", 1) {

            @Override
            List<String> getSortedChildren() throws Exception {
                throw new KeeperException.NoNodeException();
            }
        };
        try {
            internals.attemptLock(0, null, null);
            Assert.fail();
        } catch (KeeperException.NoNodeException dummy) {
        // expected
        }
        // make sure no nodes are left lying around
        Assert.assertEquals(client.checkExists().forPath(PATH).getNumChildren(), 0);
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 67 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project xian by happyyangyuan.

the class TestSharedCount method testDisconnectEventOnWatcherDoesNotRetry.

@Test
public void testDisconnectEventOnWatcherDoesNotRetry() throws Exception {
    final CountDownLatch gotSuspendEvent = new CountDownLatch(1);
    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryNTimes(10, 1000));
    curatorFramework.start();
    curatorFramework.blockUntilConnected();
    SharedCount sharedCount = new SharedCount(curatorFramework, "/count", 10);
    sharedCount.start();
    curatorFramework.getConnectionStateListenable().addListener(new ConnectionStateListener() {

        @Override
        public void stateChanged(CuratorFramework client, ConnectionState newState) {
            if (newState == ConnectionState.SUSPENDED) {
                gotSuspendEvent.countDown();
            }
        }
    });
    try {
        server.stop();
        // if watcher goes into 10second retry loop we won't get timely notification
        Assert.assertTrue(gotSuspendEvent.await(5, TimeUnit.SECONDS));
    } finally {
        CloseableUtils.closeQuietly(sharedCount);
        CloseableUtils.closeQuietly(curatorFramework);
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 68 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project xian by happyyangyuan.

the class TestLeaderSelectorEdges method createProtectedNodeInBackgroundTest.

/**
 * Create a protected node in background with a retry policy
 */
@Test
public void createProtectedNodeInBackgroundTest() throws Exception {
    final CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryNTimes(2, 1)).connectionTimeoutMs(100).sessionTimeoutMs(60000).build();
    final CountDownLatch latch = new CountDownLatch(1);
    client.start();
    try {
        client.create().forPath(ChaosMonkeyCnxnFactory.CHAOS_ZNODE);
        client.create().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).inBackground(new BackgroundCallback() {

            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                log.info("Receive event {}", event.toString());
                if (event.getResultCode() == KeeperException.Code.CONNECTIONLOSS.intValue()) {
                    latch.countDown();
                }
            }
        }).forPath(ChaosMonkeyCnxnFactory.CHAOS_ZNODE_PREFIX + "foo-");
        Assert.assertTrue(latch.await(30, TimeUnit.SECONDS), "Callback has not been called");
        // Wait for the znode to be deleted
        Thread.sleep(ChaosMonkeyCnxnFactory.LOCKOUT_DURATION_MS * 2);
        // Check that there is no znode
        final int children = client.getChildren().forPath(ChaosMonkeyCnxnFactory.CHAOS_ZNODE).size();
        Assert.assertEquals(children, 0, "Still " + children + " znodes under " + ChaosMonkeyCnxnFactory.CHAOS_ZNODE + " lock");
    } finally {
        client.close();
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Aggregations

RetryNTimes (org.apache.curator.retry.RetryNTimes)68 CuratorFramework (org.apache.curator.framework.CuratorFramework)51 Test (org.junit.Test)20 RetryPolicy (org.apache.curator.RetryPolicy)12 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)12 Test (org.testng.annotations.Test)11 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 ZooKeeperGroup (io.fabric8.groups.internal.ZooKeeperGroup)8 IOException (java.io.IOException)6 ZooKeeperGroup (org.apache.camel.component.zookeepermaster.group.internal.ZooKeeperGroup)6 Before (org.junit.Before)6 ArrayList (java.util.ArrayList)5 TestingServer (org.apache.curator.test.TestingServer)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ConnectionState (org.apache.curator.framework.state.ConnectionState)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)3 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)3