Search in sources :

Example 11 with RunId

use of com.continuuity.weave.api.RunId 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

RunId (com.continuuity.weave.api.RunId)11 ZKClientService (com.continuuity.weave.zookeeper.ZKClientService)6 Service (com.google.common.util.concurrent.Service)5 InMemoryZKServer (com.continuuity.weave.internal.zookeeper.InMemoryZKServer)4 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)4 Test (org.junit.Test)4 WeaveController (com.continuuity.weave.api.WeaveController)3 ServiceListenerAdapter (com.continuuity.weave.common.ServiceListenerAdapter)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 WeaveSpecification (com.continuuity.weave.api.WeaveSpecification)2 WeaveContainerController (com.continuuity.weave.internal.WeaveContainerController)2 JsonObject (com.google.gson.JsonObject)2 File (java.io.File)2 Configuration (org.apache.hadoop.conf.Configuration)2 HdfsConfiguration (org.apache.hadoop.hdfs.HdfsConfiguration)2 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)2 LocalFile (com.continuuity.weave.api.LocalFile)1 SecureStore (com.continuuity.weave.api.SecureStore)1 WeaveRunResources (com.continuuity.weave.api.WeaveRunResources)1 WeaveRunnableSpecification (com.continuuity.weave.api.WeaveRunnableSpecification)1