Search in sources :

Example 1 with NodeChildren

use of com.continuuity.weave.zookeeper.NodeChildren in project weave by continuuity.

the class ZKDiscoveryService method createServiceLoader.

/**
   * Creates a CacheLoader for creating live Iterable for watching instances changes for a given service.
   */
private CacheLoader<String, ServiceDiscovered> createServiceLoader() {
    return new CacheLoader<String, ServiceDiscovered>() {

        @Override
        public ServiceDiscovered load(String service) throws Exception {
            final DefaultServiceDiscovered serviceDiscovered = new DefaultServiceDiscovered(service);
            final String serviceBase = "/" + service;
            // Watch for children changes in /service
            ZKOperations.watchChildren(zkClient, serviceBase, new ZKOperations.ChildrenCallback() {

                @Override
                public void updated(NodeChildren nodeChildren) {
                    // Fetch data of all children nodes in parallel.
                    List<String> children = nodeChildren.getChildren();
                    List<OperationFuture<NodeData>> dataFutures = Lists.newArrayListWithCapacity(children.size());
                    for (String child : children) {
                        dataFutures.add(zkClient.getData(serviceBase + "/" + child));
                    }
                    // Update the service map when all fetching are done.
                    final ListenableFuture<List<NodeData>> fetchFuture = Futures.successfulAsList(dataFutures);
                    fetchFuture.addListener(new Runnable() {

                        @Override
                        public void run() {
                            ImmutableSet.Builder<Discoverable> builder = ImmutableSet.builder();
                            for (NodeData nodeData : Futures.getUnchecked(fetchFuture)) {
                                // For successful fetch, decode the content.
                                if (nodeData != null) {
                                    Discoverable discoverable = DiscoverableAdapter.decode(nodeData.getData());
                                    if (discoverable != null) {
                                        builder.add(discoverable);
                                    }
                                }
                            }
                            serviceDiscovered.setDiscoverables(builder.build());
                        }
                    }, Threads.SAME_THREAD_EXECUTOR);
                }
            });
            return serviceDiscovered;
        }
    };
}
Also used : NodeData(com.continuuity.weave.zookeeper.NodeData) ImmutableSet(com.google.common.collect.ImmutableSet) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) CacheLoader(com.google.common.cache.CacheLoader) List(java.util.List) ZKOperations(com.continuuity.weave.zookeeper.ZKOperations) NodeChildren(com.continuuity.weave.zookeeper.NodeChildren)

Example 2 with NodeChildren

use of com.continuuity.weave.zookeeper.NodeChildren in project weave by continuuity.

the class YarnWeaveRunnerService method watchLiveApps.

private Cancellable watchLiveApps() {
    final Map<String, Cancellable> watched = Maps.newConcurrentMap();
    final AtomicBoolean cancelled = new AtomicBoolean(false);
    // Watch child changes in the root, which gives all application names.
    final Cancellable cancellable = ZKOperations.watchChildren(zkClientService, "/", new ZKOperations.ChildrenCallback() {

        @Override
        public void updated(NodeChildren nodeChildren) {
            if (cancelled.get()) {
                return;
            }
            Set<String> apps = ImmutableSet.copyOf(nodeChildren.getChildren());
            // For each for the application name, watch for ephemeral nodes under /instances.
            for (final String appName : apps) {
                if (watched.containsKey(appName)) {
                    continue;
                }
                final String instancePath = String.format("/%s/instances", appName);
                watched.put(appName, ZKOperations.watchChildren(zkClientService, instancePath, new ZKOperations.ChildrenCallback() {

                    @Override
                    public void updated(NodeChildren nodeChildren) {
                        if (cancelled.get()) {
                            return;
                        }
                        if (nodeChildren.getChildren().isEmpty()) {
                            // No more child, means no live instances
                            Cancellable removed = watched.remove(appName);
                            if (removed != null) {
                                removed.cancel();
                            }
                            return;
                        }
                        synchronized (YarnWeaveRunnerService.this) {
                            // fetch the application Id and construct WeaveController.
                            for (final RunId runId : Iterables.transform(nodeChildren.getChildren(), STRING_TO_RUN_ID)) {
                                if (controllers.contains(appName, runId)) {
                                    continue;
                                }
                                updateController(appName, runId, cancelled);
                            }
                        }
                    }
                }));
            }
            // in the state listener attached to the weave controller.
            for (String removeApp : Sets.difference(watched.keySet(), apps)) {
                watched.remove(removeApp).cancel();
            }
        }
    });
    return new Cancellable() {

        @Override
        public void cancel() {
            cancelled.set(true);
            cancellable.cancel();
            for (Cancellable c : watched.values()) {
                c.cancel();
            }
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Cancellable(com.continuuity.weave.common.Cancellable) ZKOperations(com.continuuity.weave.zookeeper.ZKOperations) RunId(com.continuuity.weave.api.RunId) NodeChildren(com.continuuity.weave.zookeeper.NodeChildren)

Aggregations

NodeChildren (com.continuuity.weave.zookeeper.NodeChildren)2 ZKOperations (com.continuuity.weave.zookeeper.ZKOperations)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 RunId (com.continuuity.weave.api.RunId)1 Cancellable (com.continuuity.weave.common.Cancellable)1 NodeData (com.continuuity.weave.zookeeper.NodeData)1 CacheLoader (com.google.common.cache.CacheLoader)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 List (java.util.List)1 Set (java.util.Set)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1