Search in sources :

Example 1 with DistributedAtomicInteger

use of org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger in project paascloud-master by paascloud.

the class ZookeeperRegistryCenter method increment.

/**
 * Increment.
 *
 * @param path        the path
 * @param retryNTimes the retry n times
 */
@Override
public void increment(String path, RetryNTimes retryNTimes) {
    try {
        distributedAtomicInteger = new DistributedAtomicInteger(client, path, retryNTimes);
        distributedAtomicInteger.increment();
    } catch (Exception e) {
        log.error("increment={}", e.getMessage(), e);
    }
}
Also used : DistributedAtomicInteger(org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger) KeeperException(org.apache.zookeeper.KeeperException)

Example 2 with DistributedAtomicInteger

use of org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger in project twister2 by DSC-SPIDAL.

the class ZKController method initialize.

/**
 * connect to the server
 * get a workerID for this worker
 * create an ephemeral znode for this client
 * @return
 */
public boolean initialize() {
    String zkServerAddress = ZKContext.zooKeeperServerIP(config);
    String zkServerPort = ZKContext.zooKeeperServerPort(config);
    zkAddress = zkServerAddress + ":" + zkServerPort;
    try {
        client = CuratorFrameworkFactory.newClient(zkAddress, new ExponentialBackoffRetry(1000, 3));
        client.start();
        dai = new DistributedAtomicInteger(client, daiPath, new ExponentialBackoffRetry(1000, 3));
        // if the job node does not exists, it means that this is not rejoining
        if (client.checkExists().forPath(jobPath) == null) {
            int workerID = createWorkerID();
            workerNetworkInfo = new WorkerNetworkInfo(hostAndPort, workerID);
            createWorkerZnode();
            appendWorkerInfo();
        // if the parent exists, check whether this worker joined the job before
        // whether it is coming from a failure
        } else {
            byte[] parentData = client.getData().forPath(jobPath);
            String parentStr = new String(parentData);
            if (parentStr.indexOf(hostAndPort) < 0) {
                int workerID = createWorkerID();
                workerNetworkInfo = new WorkerNetworkInfo(hostAndPort, workerID);
                createWorkerZnode();
                appendWorkerInfo();
            // if this worker is coming from a failure, get the ID from the parent content
            } else {
                int workerID = getWorkerIDFromParentData(parentStr);
                workerNetworkInfo = new WorkerNetworkInfo(hostAndPort, workerID);
                createWorkerZnode();
            }
        }
        // We childrenCache children data for parent path.
        // So we will listen for all workers in the job
        childrenCache = new PathChildrenCache(client, jobPath, true);
        childrenCache.start();
        LOG.log(Level.INFO, "This worker: " + workerNetworkInfo + " initialized successfully.");
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
Also used : DistributedAtomicInteger(org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache)

Aggregations

DistributedAtomicInteger (org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger)2 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)1 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)1 KeeperException (org.apache.zookeeper.KeeperException)1