Search in sources :

Example 31 with CuratorFramework

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

the class CuratorStateManagerTest method testCreateNode.

/**
   * test createNode method
   * @throws Exception
   */
@Test
public void testCreateNode() throws Exception {
    CuratorStateManager spyStateManager = spy(new CuratorStateManager());
    CuratorFramework mockClient = mock(CuratorFramework.class);
    CreateBuilder mockCreateBuilder = mock(CreateBuilder.class);
    // Mockito doesn't support mock type-parametrized class, thus suppress the warning
    @SuppressWarnings("rawtypes") ACLBackgroundPathAndBytesable mockPath = spy(ACLBackgroundPathAndBytesable.class);
    final byte[] data = new byte[10];
    doReturn(mockClient).when(spyStateManager).getCuratorClient();
    doReturn(true).when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
    doReturn(mockCreateBuilder).when(mockClient).create();
    doReturn(mockPath).when(mockCreateBuilder).withMode(any(CreateMode.class));
    spyStateManager.initialize(config);
    // Verify the node is created successfully
    ListenableFuture<Boolean> result = spyStateManager.createNode(PATH, data, false);
    verify(mockCreateBuilder).withMode(any(CreateMode.class));
    verify(mockPath).forPath(PATH, data);
    assertTrue(result.get());
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CreateMode(org.apache.zookeeper.CreateMode) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) ACLBackgroundPathAndBytesable(org.apache.curator.framework.api.ACLBackgroundPathAndBytesable) TimeUnit(java.util.concurrent.TimeUnit) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) Test(org.junit.Test)

Example 32 with CuratorFramework

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

the class CuratorStateManagerTest method testDeleteSchedulerLocation.

/**
   * Test deleteSchedulerLocation method
   * @throws Exception
   */
@Test
public void testDeleteSchedulerLocation() throws Exception {
    CuratorStateManager spyStateManager = spy(new CuratorStateManager());
    CuratorFramework mockClient = mock(CuratorFramework.class);
    doReturn(mockClient).when(spyStateManager).getCuratorClient();
    doReturn(true).when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
    spyStateManager.initialize(config);
    final SettableFuture<Boolean> fakeResult = SettableFuture.create();
    fakeResult.set(false);
    doReturn(fakeResult).when(spyStateManager).deleteNode(anyString(), anyBoolean());
    ListenableFuture<Boolean> result = spyStateManager.deleteSchedulerLocation(TOPOLOGY_NAME);
    assertTrue(result.get());
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TimeUnit(java.util.concurrent.TimeUnit) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) Test(org.junit.Test)

Example 33 with CuratorFramework

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

the class CuratorStateManager method getNodeData.

@Override
protected <M extends Message> ListenableFuture<M> getNodeData(WatchCallback watcher, String path, final Message.Builder builder) {
    final SettableFuture<M> future = SettableFuture.create();
    Watcher wc = ZkWatcherCallback.makeZkWatcher(watcher);
    BackgroundCallback cb = new BackgroundCallback() {

        @Override
        // we don't know what M is until runtime
        @SuppressWarnings("unchecked")
        public void processResult(CuratorFramework aClient, CuratorEvent event) throws Exception {
            byte[] data;
            if (event != null & (data = event.getData()) != null) {
                builder.mergeFrom(data);
                future.set((M) builder.build());
            } else {
                future.setException(new RuntimeException("Failed to fetch data from path: " + event.getPath()));
            }
        }
    };
    try {
        client.getData().usingWatcher(wc).inBackground(cb).forPath(path);
    // Suppress it since forPath() throws Exception
    // SUPPRESS CHECKSTYLE IllegalCatch
    } catch (Exception e) {
        future.setException(new RuntimeException("Could not getNodeData", e));
    }
    return future;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Watcher(org.apache.zookeeper.Watcher) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) KeeperException(org.apache.zookeeper.KeeperException) ExecutionException(java.util.concurrent.ExecutionException)

Example 34 with CuratorFramework

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

the class MasterEndpointFailoverTest method beforeRun.

@Before
public void beforeRun() throws Exception {
    System.out.println("Starting ZK server!");
    serverFactoryBean.setPort(9004);
    serverFactoryBean.afterPropertiesSet();
    // Create the zkClientBean
    zkClientBean.setConnectString("localhost:9004");
    CuratorFramework client = zkClientBean.getObject();
    // Need to bind the zookeeper client with the name "curator"
    SimpleRegistry registry = new SimpleRegistry();
    registry.put("curator", client);
    producerContext = new DefaultCamelContext(registry);
    // Add the vm:start endpoint to avoid the NPE before starting the consumerContext1
    producerContext.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").to("vm:start");
        }
    });
    template = producerContext.createProducerTemplate();
    consumerContext1 = new DefaultCamelContext(registry);
    consumerContext1.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("zookeeper-master:MasterEndpointFailoverTest:vm:start").to("log:result1").to("mock:result1");
        }
    });
    consumerContext2 = new DefaultCamelContext(registry);
    consumerContext2.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("zookeeper-master:MasterEndpointFailoverTest:vm:start").to("log:result2").to("mock:result2");
        }
    });
    // Need to start at less one consumerContext to enable the vm queue for producerContext
    ServiceHelper.startServices(consumerContext1);
    ServiceHelper.startServices(producerContext);
    result1Endpoint = consumerContext1.getEndpoint("mock:result1", MockEndpoint.class);
    result2Endpoint = consumerContext2.getEndpoint("mock:result2", MockEndpoint.class);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SimpleRegistry(org.apache.camel.impl.SimpleRegistry) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Before(org.junit.Before)

Example 35 with CuratorFramework

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

the class GroupTest method testJoinBeforeConnect.

@Test
public void testJoinBeforeConnect() throws Exception {
    int port = findFreePort();
    CuratorFramework curator = CuratorFrameworkFactory.builder().connectString("localhost:" + port).retryPolicy(new RetryNTimes(10, 100)).build();
    curator.start();
    Group<NodeState> group = new ZooKeeperGroup<NodeState>(curator, "/singletons/test" + System.currentTimeMillis(), NodeState.class);
    group.add(listener);
    group.start();
    GroupCondition groupCondition = new GroupCondition();
    group.add(groupCondition);
    assertFalse(group.isConnected());
    assertFalse(group.isMaster());
    group.update(new NodeState("foo"));
    NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);
    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    assertTrue(groupCondition.waitForConnected(5, TimeUnit.SECONDS));
    assertTrue(groupCondition.waitForMaster(5, TimeUnit.SECONDS));
    group.close();
    curator.close();
    cnxnFactory.shutdown();
    cnxnFactory.join();
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) ZooKeeperGroup(org.apache.camel.component.zookeepermaster.group.internal.ZooKeeperGroup) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) 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