Search in sources :

Example 26 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project hadoop by apache.

the class TestChildReaper method testMultiPath.

@Test
public void testMultiPath() throws Exception {
    Timing timing = new Timing();
    ChildReaper reaper = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        for (int i = 0; i < 10; ++i) {
            client.create().creatingParentsIfNeeded().forPath("/test1/" + Integer.toString(i));
            client.create().creatingParentsIfNeeded().forPath("/test2/" + Integer.toString(i));
            client.create().creatingParentsIfNeeded().forPath("/test3/" + Integer.toString(i));
        }
        reaper = new ChildReaper(client, "/test2", Reaper.Mode.REAP_UNTIL_DELETE, 1);
        reaper.start();
        reaper.addPath("/test1");
        timing.forWaiting().sleepABit();
        Stat stat = client.checkExists().forPath("/test1");
        Assert.assertEquals(stat.getNumChildren(), 0);
        stat = client.checkExists().forPath("/test2");
        Assert.assertEquals(stat.getNumChildren(), 0);
        stat = client.checkExists().forPath("/test3");
        Assert.assertEquals(stat.getNumChildren(), 10);
    } finally {
        CloseableUtils.closeQuietly(reaper);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Stat(org.apache.zookeeper.data.Stat) Timing(org.apache.curator.test.Timing) Test(org.junit.Test)

Example 27 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project hadoop by apache.

the class TestChildReaper method testSomeNodes.

@Test
public void testSomeNodes() throws Exception {
    Timing timing = new Timing();
    ChildReaper reaper = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        Random r = new Random();
        int nonEmptyNodes = 0;
        for (int i = 0; i < 10; ++i) {
            client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i));
            if (r.nextBoolean()) {
                client.create().forPath("/test/" + Integer.toString(i) + "/foo");
                ++nonEmptyNodes;
            }
        }
        reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1);
        reaper.start();
        timing.forWaiting().sleepABit();
        Stat stat = client.checkExists().forPath("/test");
        Assert.assertEquals(stat.getNumChildren(), nonEmptyNodes);
    } finally {
        CloseableUtils.closeQuietly(reaper);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Stat(org.apache.zookeeper.data.Stat) Random(java.util.Random) Timing(org.apache.curator.test.Timing) Test(org.junit.Test)

Example 28 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project hadoop by apache.

the class TestChildReaper method testSimple.

@Test
public void testSimple() throws Exception {
    Timing timing = new Timing();
    ChildReaper reaper = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        for (int i = 0; i < 10; ++i) {
            client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i));
        }
        reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1);
        reaper.start();
        timing.forWaiting().sleepABit();
        Stat stat = client.checkExists().forPath("/test");
        Assert.assertEquals(stat.getNumChildren(), 0);
    } finally {
        CloseableUtils.closeQuietly(reaper);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Stat(org.apache.zookeeper.data.Stat) Timing(org.apache.curator.test.Timing) Test(org.junit.Test)

Example 29 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project alluxio by Alluxio.

the class LeaderSelectorClient method getNewCuratorClient.

/**
   * Returns a new client for the zookeeper connection. The client is already started before
   * returning.
   *
   * @return a new {@link CuratorFramework} client to use for leader selection
   */
private CuratorFramework getNewCuratorClient() {
    CuratorFramework client = CuratorFrameworkFactory.newClient(mZookeeperAddress, new ExponentialBackoffRetry(Constants.SECOND_MS, 3));
    client.start();
    // Sometimes, if the master crashes and restarts too quickly (faster than the zookeeper
    // timeout), zookeeper thinks the new client is still an old one. In order to ensure a clean
    // state, explicitly close the "old" client recreate a new one.
    client.close();
    client = CuratorFrameworkFactory.newClient(mZookeeperAddress, new ExponentialBackoffRetry(Constants.SECOND_MS, 3));
    client.start();
    return client;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry)

Example 30 with CuratorFramework

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

the class CuratorStateManagerTest method testDeleteNode.

/**
   * Test deleteNode method
   * @throws Exception
   */
@Test
public void testDeleteNode() throws Exception {
    CuratorStateManager spyStateManager = spy(new CuratorStateManager());
    CuratorFramework mockClient = mock(CuratorFramework.class);
    DeleteBuilder mockDeleteBuilder = mock(DeleteBuilder.class);
    // Mockito doesn't support mock type-parametrized class, thus suppress the warning
    @SuppressWarnings("rawtypes") BackgroundPathable mockBackPathable = mock(BackgroundPathable.class);
    doReturn(mockClient).when(spyStateManager).getCuratorClient();
    doReturn(true).when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
    doReturn(mockDeleteBuilder).when(mockClient).delete();
    doReturn(mockBackPathable).when(mockDeleteBuilder).withVersion(-1);
    spyStateManager.initialize(config);
    ListenableFuture<Boolean> result = spyStateManager.deleteExecutionState(PATH);
    // Verify the node is deleted correctly
    verify(mockDeleteBuilder).withVersion(-1);
    assertTrue(result.get());
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TimeUnit(java.util.concurrent.TimeUnit) BackgroundPathable(org.apache.curator.framework.api.BackgroundPathable) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) DeleteBuilder(org.apache.curator.framework.api.DeleteBuilder) Test(org.junit.Test)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)863 Test (org.testng.annotations.Test)290 RetryOneTime (org.apache.curator.retry.RetryOneTime)268 Test (org.junit.Test)183 Timing (org.apache.curator.test.Timing)147 CountDownLatch (java.util.concurrent.CountDownLatch)121 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)101 KeeperException (org.apache.zookeeper.KeeperException)91 IOException (java.io.IOException)76 ConnectionState (org.apache.curator.framework.state.ConnectionState)70 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)56 ExecutorService (java.util.concurrent.ExecutorService)53 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)50 RetryNTimes (org.apache.curator.retry.RetryNTimes)49 ArrayList (java.util.ArrayList)46 RetryPolicy (org.apache.curator.RetryPolicy)36 Stat (org.apache.zookeeper.data.Stat)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)35 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)33