Search in sources :

Example 36 with PathChildrenCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project pravega by pravega.

the class ZooKeeperBucketManager method startBucketOwnershipListener.

@Override
public void startBucketOwnershipListener() {
    PathChildrenCache pathChildrenCache = bucketOwnershipCacheMap.computeIfAbsent(getServiceType(), x -> bucketStore.getServiceOwnershipPathChildrenCache(getServiceType()));
    PathChildrenCacheListener bucketListener = (client, event) -> {
        switch(event.getType()) {
            case CHILD_ADDED:
                // no action required
                break;
            case CHILD_REMOVED:
                int bucketId = Integer.parseInt(ZKPaths.getNodeFromPath(event.getData().getPath()));
                RetryHelper.withIndefiniteRetriesAsync(() -> tryTakeOwnership(bucketId), e -> log.warn("{}: exception while attempting to take ownership for bucket {}: {}", getServiceType(), bucketId, e.getMessage()), getExecutor());
                break;
            case CONNECTION_LOST:
                log.warn("{}: Received connectivity error", getServiceType());
                break;
            default:
                log.warn("Received unknown event {} on bucket root {} ", event.getType(), getServiceType());
        }
    };
    pathChildrenCache.getListenable().addListener(bucketListener);
    log.info("bucket ownership listener registered on bucket root {}", getServiceType());
    try {
        pathChildrenCache.start(PathChildrenCache.StartMode.NORMAL);
    } catch (Exception e) {
        log.error("Starting ownership listener for service {} threw exception", getServiceType(), e);
        throw Exceptions.sneakyThrow(e);
    }
}
Also used : Executor(java.util.concurrent.Executor) Exceptions(io.pravega.common.Exceptions) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) Function(java.util.function.Function) ConcurrentMap(java.util.concurrent.ConcurrentMap) BucketStore(io.pravega.controller.store.stream.BucketStore) Slf4j(lombok.extern.slf4j.Slf4j) ZKPaths(org.apache.curator.utils.ZKPaths) ZookeeperBucketStore(io.pravega.controller.store.stream.ZookeeperBucketStore) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) Preconditions(com.google.common.base.Preconditions) RetryHelper(io.pravega.controller.util.RetryHelper) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) IOException(java.io.IOException)

Example 37 with PathChildrenCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project BRFS by zhangnianli.

the class ResourceDuplicateNodeSelector method setClient.

public ResourceDuplicateNodeSelector setClient(CuratorFramework client, String zkPath) {
    this.client = client;
    this.pathCache = new PathChildrenCache(client, zkPath, true);
    return this;
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache)

Example 38 with PathChildrenCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project BRFS by zhangnianli.

the class FileNodeDistributor method serviceRemoved.

@Override
public void serviceRemoved(Service service) {
    LOG.info("Service removed#######{}", service.getServiceId());
    serviceTimeTable.remove(service.getServiceGroup(), service.getServiceId());
    PathChildrenCache childWatcher = childWatchers.get(service.getServiceId());
    CloseUtils.closeQuietly(childWatcher);
    // 删除服务对应的文件槽
    try {
        client.delete().quietly().deletingChildrenIfNeeded().forPath(ZkFileCoordinatorPaths.buildServiceSinkPath(service));
    } catch (Exception e) {
        LOG.warn("Can not delete the sink of crushed service[{}]", service.getServiceId(), e);
    }
    // 把崩溃的Service持有的文件节点放入列表
    executor.submit(new Runnable() {

        @Override
        public void run() {
            for (FileNode node : fileStorer.listFileNodes(new ServiceFileNodeFilter(service))) {
                wildFileNodes.add(node);
            }
        }
    });
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) FileNode(com.bonree.brfs.duplication.filenode.FileNode)

Example 39 with PathChildrenCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project twister2 by DSC-SPIDAL.

the class ZKMasterController method initRestarting.

/**
 * initialize JM when it is coming from failure
 * @throws Exception
 */
private void initRestarting() throws Exception {
    LOG.info("Job Master restarting .... ");
    // build the cache
    // we will not get events for the past worker joins/fails
    ephemChildrenCache = new PathChildrenCache(client, ephemDir, true);
    addEphemChildrenCacheListener(ephemChildrenCache);
    ephemChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
    List<ChildData> joinedWorkerZnodes = ephemChildrenCache.getCurrentData();
    LOG.info("Initially existing workers: " + joinedWorkerZnodes.size());
    // We listen for status updates for persistent path
    persChildrenCache = new PathChildrenCache(client, persDir, true);
    addPersChildrenCacheListener(persChildrenCache);
    persChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
    // get all joined workers and provide them to workerMonitor
    List<WorkerWithState> joinedWorkers = new LinkedList<>();
    for (ChildData child : joinedWorkerZnodes) {
        String fullPath = child.getPath();
        int workerID = ZKUtils.getWorkerIDFromEphemPath(fullPath);
        WorkerWithState workerWithState = getWorkerWithState(workerID);
        if (workerWithState != null) {
            joinedWorkers.add(workerWithState);
        } else {
            LOG.severe("worker[" + fullPath + "] added, but its data can not be retrieved.");
        }
    }
    // publish jm restarted event
    jmRestarted();
    // if all workers joined and allJoined event has not been published, publish it
    boolean allJoined = workerMonitor.addJoinedWorkers(joinedWorkers);
    if (allJoined && !allJoinedPublished()) {
        LOG.info("Publishing AllJoined event when restarting, since it is not previously published.");
        allJoined();
    }
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ChildData(org.apache.curator.framework.recipes.cache.ChildData) WorkerWithState(edu.iu.dsc.tws.common.zk.WorkerWithState) LinkedList(java.util.LinkedList)

Example 40 with PathChildrenCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project twister2 by DSC-SPIDAL.

the class ZKMasterController method initialize.

/**
 * initialize ZKMasterController,
 * create znode children caches for job master to watch proper events
 */
public void initialize(JobMasterState initialState) throws Twister2Exception {
    if (!(initialState == JobMasterState.JM_STARTED || initialState == JobMasterState.JM_RESTARTED)) {
        throw new Twister2Exception("initialState has to be either JobMasterState.JM_STARTED or " + "JobMasterState.JM_RESTARTED. Supplied value: " + initialState);
    }
    try {
        String zkServerAddresses = ZKContext.serverAddresses(config);
        int sessionTimeoutMs = FaultToleranceContext.sessionTimeout(config);
        client = ZKUtils.connectToServer(zkServerAddresses, sessionTimeoutMs);
        // with scaling up/down, it may have been changed
        if (initialState == JobMasterState.JM_RESTARTED) {
            initRestarting();
        } else {
            // We listen for join/remove events for ephemeral children
            ephemChildrenCache = new PathChildrenCache(client, ephemDir, true);
            addEphemChildrenCacheListener(ephemChildrenCache);
            ephemChildrenCache.start();
            // We listen for status updates for persistent path
            persChildrenCache = new PathChildrenCache(client, persDir, true);
            addPersChildrenCacheListener(persChildrenCache);
            persChildrenCache.start();
        }
        // TODO: we nay need to create ephemeral job master znode so that
        // workers can know when jm fails
        // createJobMasterZnode(initialState);
        LOG.info("Job Master: " + jmAddress + " initialized successfully.");
    } catch (Twister2Exception e) {
        throw e;
    } catch (Exception e) {
        throw new Twister2Exception("Exception when initializing ZKMasterController.", e);
    }
}
Also used : Twister2Exception(edu.iu.dsc.tws.api.exceptions.Twister2Exception) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) Twister2RuntimeException(edu.iu.dsc.tws.api.exceptions.Twister2RuntimeException) Twister2Exception(edu.iu.dsc.tws.api.exceptions.Twister2Exception)

Aggregations

PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)73 IOException (java.io.IOException)25 CuratorFramework (org.apache.curator.framework.CuratorFramework)21 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)20 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)14 KeeperException (org.apache.zookeeper.KeeperException)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 ChildData (org.apache.curator.framework.recipes.cache.ChildData)8 CountDownLatch (java.util.concurrent.CountDownLatch)6 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)6 Before (org.junit.Before)5 Test (org.junit.Test)5 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 ExecutorService (java.util.concurrent.ExecutorService)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 Slf4j (lombok.extern.slf4j.Slf4j)4 ZKPaths (org.apache.curator.utils.ZKPaths)4 Preconditions (com.google.common.base.Preconditions)3 StreamImpl (io.pravega.client.stream.impl.StreamImpl)3 CompletableFuture (java.util.concurrent.CompletableFuture)3