Search in sources :

Example 1 with NodeData

use of com.continuuity.weave.zookeeper.NodeData 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 NodeData

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

the class ControllerTest method getController.

private WeaveController getController(ZKClient zkClient, RunId runId) {
    WeaveController controller = new AbstractWeaveController(runId, zkClient, ImmutableList.<LogHandler>of()) {

        @Override
        public void kill() {
        // No-op
        }

        @Override
        protected void instanceNodeUpdated(NodeData nodeData) {
        // No-op
        }

        @Override
        protected void stateNodeUpdated(StateNode stateNode) {
        // No-op
        }

        @Override
        public ResourceReport getResourceReport() {
            return null;
        }
    };
    controller.startAndWait();
    return controller;
}
Also used : StateNode(com.continuuity.weave.internal.state.StateNode) WeaveController(com.continuuity.weave.api.WeaveController) NodeData(com.continuuity.weave.zookeeper.NodeData)

Aggregations

NodeData (com.continuuity.weave.zookeeper.NodeData)2 WeaveController (com.continuuity.weave.api.WeaveController)1 StateNode (com.continuuity.weave.internal.state.StateNode)1 NodeChildren (com.continuuity.weave.zookeeper.NodeChildren)1 ZKOperations (com.continuuity.weave.zookeeper.ZKOperations)1 CacheLoader (com.google.common.cache.CacheLoader)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 List (java.util.List)1