Search in sources :

Example 36 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 37 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 38 with RetryNTimes

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

the class TestLeaderLatch method testNoServerAtStart.

@Test
public void testNoServerAtStart() {
    CloseableUtils.closeQuietly(server);
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryNTimes(5, 1000));
    client.start();
    final LeaderLatch leader = new LeaderLatch(client, PATH_NAME);
    final CountDownLatch leaderCounter = new CountDownLatch(1);
    final AtomicInteger leaderCount = new AtomicInteger(0);
    final AtomicInteger notLeaderCount = new AtomicInteger(0);
    leader.addListener(new LeaderLatchListener() {

        @Override
        public void isLeader() {
            leaderCounter.countDown();
            leaderCount.incrementAndGet();
        }

        @Override
        public void notLeader() {
            notLeaderCount.incrementAndGet();
        }
    });
    try {
        leader.start();
        timing.sleepABit();
        // Start the new server
        server = new TestingServer(server.getPort(), server.getTempDirectory());
        Assert.assertTrue(timing.awaitLatch(leaderCounter), "Not elected leader");
        Assert.assertEquals(leaderCount.get(), 1, "Elected too many times");
        Assert.assertEquals(notLeaderCount.get(), 0, "Unelected too many times");
    } catch (Exception e) {
        Assert.fail("Unexpected exception", e);
    } finally {
        CloseableUtils.closeQuietly(leader);
        CloseableUtils.closeQuietly(client);
        CloseableUtils.closeQuietly(server);
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) TestingServer(org.apache.curator.test.TestingServer) CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Timing(org.apache.curator.test.Timing) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 39 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)

Example 40 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project drill by axbaretto.

the class TestEphemeralStore method setUp.

@Before
public void setUp() throws Exception {
    ZookeeperTestUtil.setZookeeperSaslTestConfigProps();
    server = new TestingServer();
    final RetryPolicy policy = new RetryNTimes(2, 1000);
    curator = CuratorFrameworkFactory.newClient(server.getConnectString(), policy);
    config = Mockito.mock(TransientStoreConfig.class);
    Mockito.when(config.getName()).thenReturn(root);
    Mockito.when(config.getSerializer()).thenReturn(new InstanceSerializer<String>() {

        @Override
        public byte[] serialize(final String instance) throws IOException {
            if (instance == null) {
                return null;
            }
            return instance.getBytes();
        }

        @Override
        public String deserialize(final byte[] raw) throws IOException {
            if (raw == null) {
                return null;
            }
            return new String(raw);
        }
    });
    store = new ZkEphemeralStore<>(config, curator);
    server.start();
    curator.start();
    store.start();
}
Also used : TestingServer(org.apache.curator.test.TestingServer) RetryNTimes(org.apache.curator.retry.RetryNTimes) TransientStoreConfig(org.apache.drill.exec.coord.store.TransientStoreConfig) IOException(java.io.IOException) RetryPolicy(org.apache.curator.RetryPolicy) Before(org.junit.Before)

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