Search in sources :

Example 61 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project apex-malhar by apache.

the class ZKAssistedDiscovery method setup.

@Override
public void setup(com.datatorrent.api.Context context) {
    ObjectMapper om = new ObjectMapper();
    instanceSerializerFactory = new InstanceSerializerFactory(om.reader(), om.writer());
    curatorFramework = CuratorFrameworkFactory.builder().connectionTimeoutMs(connectionTimeoutMillis).retryPolicy(new RetryNTimes(connectionRetryCount, conntectionRetrySleepMillis)).connectString(connectionString).build();
    curatorFramework.start();
    discovery = getDiscovery(curatorFramework);
    try {
        discovery.start();
    } catch (Exception ex) {
        Throwables.propagate(ex);
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) IOException(java.io.IOException)

Example 62 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project paascloud-master by paascloud.

the class IncrementIdGenerator method nextId.

/**
 * Next id long.
 *
 * @return the long
 */
@Override
public Long nextId() {
    String app = this.registerDto.getApp();
    String host = this.registerDto.getHost();
    CoordinatorRegistryCenter regCenter = this.registerDto.getCoordinatorRegistryCenter();
    String path = GlobalConstant.ZK_REGISTRY_ID_ROOT_PATH + GlobalConstant.Symbol.SLASH + app + GlobalConstant.Symbol.SLASH + host;
    if (regCenter.isExisted(path)) {
        // 如果已经有该节点,表示已经为当前的host上部署的该app分配的编号(应对某个服务重启之后编号不变的问题),直接获取该id,而无需生成
        return Long.valueOf(regCenter.getDirectly(GlobalConstant.ZK_REGISTRY_ID_ROOT_PATH + GlobalConstant.Symbol.SLASH + app + GlobalConstant.Symbol.SLASH + host));
    } else {
        // 节点不存在,那么需要生成id,利用zk节点的版本号每写一次就自增的机制来实现
        regCenter.increment(GlobalConstant.ZK_REGISTRY_SEQ, new RetryNTimes(2000, 3));
        // 生成id
        Integer id = regCenter.getAtomicValue(GlobalConstant.ZK_REGISTRY_SEQ, new RetryNTimes(2000, 3)).postValue();
        // 将数据写入节点
        regCenter.persist(path);
        regCenter.persist(path, String.valueOf(id));
        return Long.valueOf(id);
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CoordinatorRegistryCenter(com.paascloud.core.registry.base.CoordinatorRegistryCenter)

Example 63 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project tutorials by eugenp.

the class BaseTest method newClient.

protected CuratorFramework newClient() {
    int sleepMsBetweenRetries = 100;
    int maxRetries = 3;
    RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
    return CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) RetryPolicy(org.apache.curator.RetryPolicy)

Example 64 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project tutorials by eugenp.

the class ConnectionManagementManualTest method givenRunningZookeeper_whenOpenConnection_thenClientIsOpened.

@Test
public void givenRunningZookeeper_whenOpenConnection_thenClientIsOpened() throws Exception {
    int sleepMsBetweenRetries = 100;
    int maxRetries = 3;
    RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
    try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
        client.start();
        assertThat(client.checkExists().forPath("/")).isNotNull();
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) RetryPolicy(org.apache.curator.RetryPolicy) Test(org.junit.Test)

Example 65 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project tutorials by eugenp.

the class ConnectionManagementManualTest method givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened.

@Test
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened() throws InterruptedException {
    int sleepMsBetweenRetries = 100;
    int maxRetries = 3;
    RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
    try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
        client.start();
        AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
        AtomicBoolean exists = new AtomicBoolean(false);
        async.checkExists().forPath("/").thenAccept(s -> exists.set(s != null));
        await().until(() -> assertThat(exists.get()).isTrue());
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) RetryPolicy(org.apache.curator.RetryPolicy) Test(org.junit.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