Search in sources :

Example 36 with PathChildrenCache

use of 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 37 with PathChildrenCache

use of 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 38 with PathChildrenCache

use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project coprhd-controller by CoprHD.

the class DistributedDataManagerImpl method ensureCacheStarted.

/**
 * Use the double-check algorithm to initialize the child path cache for use in limit checking
 */
private void ensureCacheStarted() {
    if (_basePathCache == null) {
        synchronized (this) {
            if (_basePathCache == null) {
                try {
                    _basePathCache = new PathChildrenCache(_zkClient, _basePath, false);
                    _basePathCache.start();
                } catch (Exception ex) {
                    _basePathCache = null;
                    _log.error(String.format("%s: error initializing cache; will re-attempt", _basePath), ex);
                }
            }
        }
    }
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) DataManagerFullException(com.emc.storageos.coordinator.client.service.DataManagerFullException) CoordinatorException(com.emc.storageos.coordinator.exceptions.CoordinatorException)

Example 39 with PathChildrenCache

use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project hive by apache.

the class LlapZookeeperRegistryImpl method checkPathChildrenCache.

private synchronized void checkPathChildrenCache(long clusterReadyTimeoutMs) throws IOException {
    Preconditions.checkArgument(zooKeeperClient != null && zooKeeperClient.getState() == CuratorFrameworkState.STARTED, "client is not started");
    // lazily create PathChildrenCache
    if (instancesCache != null)
        return;
    ExecutorService tp = Executors.newFixedThreadPool(1, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("StateChangeNotificationHandler").build());
    long startTimeNs = System.nanoTime(), deltaNs = clusterReadyTimeoutMs * 1000000L;
    long sleepTimeMs = Math.min(16, clusterReadyTimeoutMs);
    while (true) {
        PathChildrenCache instancesCache = new PathChildrenCache(zooKeeperClient, workersPath, true);
        instancesCache.getListenable().addListener(new InstanceStateChangeListener(), tp);
        try {
            instancesCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
            this.instancesCache = instancesCache;
            break;
        } catch (InvalidACLException e) {
            // PathChildrenCache tried to mkdir when the znode wasn't there, and failed.
            CloseableUtils.closeQuietly(instancesCache);
            long elapsedNs = System.nanoTime() - startTimeNs;
            if (deltaNs == 0 || deltaNs <= elapsedNs) {
                LOG.error("Unable to start curator PathChildrenCache", e);
                throw new IOException(e);
            }
            LOG.warn("The cluster is not started yet (InvalidACL); will retry");
            try {
                Thread.sleep(Math.min(sleepTimeMs, (deltaNs - elapsedNs) / 1000000L));
            } catch (InterruptedException e1) {
                LOG.error("Interrupted while retrying the PathChildrenCache startup");
                throw new IOException(e1);
            }
            sleepTimeMs = sleepTimeMs << 1;
        } catch (Exception e) {
            CloseableUtils.closeQuietly(instancesCache);
            LOG.error("Unable to start curator PathChildrenCache", e);
            throw new IOException(e);
        }
    }
}
Also used : ServiceInstanceStateChangeListener(org.apache.hadoop.hive.llap.registry.ServiceInstanceStateChangeListener) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) IOException(java.io.IOException) InvalidACLException(org.apache.zookeeper.KeeperException.InvalidACLException) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvalidACLException(org.apache.zookeeper.KeeperException.InvalidACLException)

Example 40 with PathChildrenCache

use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project druid by druid-io.

the class ZkCoordinator method start.

@LifecycleStart
public void start() throws IOException {
    synchronized (lock) {
        if (started) {
            return;
        }
        log.info("Starting zkCoordinator for server[%s]", me.getName());
        final String loadQueueLocation = ZKPaths.makePath(zkPaths.getLoadQueuePath(), me.getName());
        final String servedSegmentsLocation = ZKPaths.makePath(zkPaths.getServedSegmentsPath(), me.getName());
        final String liveSegmentsLocation = ZKPaths.makePath(zkPaths.getLiveSegmentsPath(), me.getName());
        loadQueueCache = new PathChildrenCache(curator, loadQueueLocation, true, true, Execs.multiThreaded(config.getNumLoadingThreads(), "ZkCoordinator-%s"));
        try {
            curator.newNamespaceAwareEnsurePath(loadQueueLocation).ensure(curator.getZookeeperClient());
            curator.newNamespaceAwareEnsurePath(servedSegmentsLocation).ensure(curator.getZookeeperClient());
            curator.newNamespaceAwareEnsurePath(liveSegmentsLocation).ensure(curator.getZookeeperClient());
            loadLocalCache();
            loadQueueCache.getListenable().addListener(new PathChildrenCacheListener() {

                @Override
                public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                    final ChildData child = event.getData();
                    switch(event.getType()) {
                        case CHILD_ADDED:
                            final String path = child.getPath();
                            final DataSegmentChangeRequest request = jsonMapper.readValue(child.getData(), DataSegmentChangeRequest.class);
                            log.info("New request[%s] with zNode[%s].", request.asString(), path);
                            try {
                                request.go(getDataSegmentChangeHandler(), new DataSegmentChangeCallback() {

                                    boolean hasRun = false;

                                    @Override
                                    public void execute() {
                                        try {
                                            if (!hasRun) {
                                                curator.delete().guaranteed().forPath(path);
                                                log.info("Completed request [%s]", request.asString());
                                                hasRun = true;
                                            }
                                        } catch (Exception e) {
                                            try {
                                                curator.delete().guaranteed().forPath(path);
                                            } catch (Exception e1) {
                                                log.error(e1, "Failed to delete zNode[%s], but ignoring exception.", path);
                                            }
                                            log.error(e, "Exception while removing zNode[%s]", path);
                                            throw Throwables.propagate(e);
                                        }
                                    }
                                });
                            } catch (Exception e) {
                                try {
                                    curator.delete().guaranteed().forPath(path);
                                } catch (Exception e1) {
                                    log.error(e1, "Failed to delete zNode[%s], but ignoring exception.", path);
                                }
                                log.makeAlert(e, "Segment load/unload: uncaught exception.").addData("node", path).addData("nodeProperties", request).emit();
                            }
                            break;
                        case CHILD_REMOVED:
                            log.info("zNode[%s] was removed", event.getData().getPath());
                            break;
                        default:
                            log.info("Ignoring event[%s]", event);
                    }
                }
            });
            loadQueueCache.start();
        } catch (Exception e) {
            Throwables.propagateIfPossible(e, IOException.class);
            throw Throwables.propagate(e);
        }
        started = true;
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ChildData(org.apache.curator.framework.recipes.cache.ChildData) IOException(java.io.IOException) SegmentLoadingException(io.druid.segment.loading.SegmentLoadingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) LifecycleStart(io.druid.java.util.common.lifecycle.LifecycleStart)

Aggregations

PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)68 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)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 ChildData (org.apache.curator.framework.recipes.cache.ChildData)7 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