Search in sources :

Example 1 with Cancellable

use of com.continuuity.weave.common.Cancellable in project weave by continuuity.

the class ZKOperations method watchData.

/**
   * Watch for data changes of the given path. The callback will be triggered whenever changes has been
   * detected. Note that the callback won't see every single changes, as that's not the guarantee of ZooKeeper.
   * If the node doesn't exists, it will watch for its creation then starts watching for data changes.
   * When the node is deleted afterwards,
   *
   * @param zkClient The {@link ZKClient} for the operation
   * @param path Path to watch
   * @param callback Callback to be invoked when data changes is detected.
   * @return A {@link Cancellable} to cancel the watch.
   */
public static Cancellable watchData(final ZKClient zkClient, final String path, final DataCallback callback) {
    final AtomicBoolean cancelled = new AtomicBoolean(false);
    watchChanges(new Operation<NodeData>() {

        @Override
        public ZKClient getZKClient() {
            return zkClient;
        }

        @Override
        public OperationFuture<NodeData> exec(String path, Watcher watcher) {
            return zkClient.getData(path, watcher);
        }
    }, path, callback, cancelled);
    return new Cancellable() {

        @Override
        public void cancel() {
            cancelled.set(true);
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Cancellable(com.continuuity.weave.common.Cancellable) Watcher(org.apache.zookeeper.Watcher) SettableOperationFuture(com.continuuity.weave.internal.zookeeper.SettableOperationFuture)

Example 2 with Cancellable

use of com.continuuity.weave.common.Cancellable in project weave by continuuity.

the class ZKOperations method watchChildren.

public static Cancellable watchChildren(final ZKClient zkClient, String path, ChildrenCallback callback) {
    final AtomicBoolean cancelled = new AtomicBoolean(false);
    watchChanges(new Operation<NodeChildren>() {

        @Override
        public ZKClient getZKClient() {
            return zkClient;
        }

        @Override
        public OperationFuture<NodeChildren> exec(String path, Watcher watcher) {
            return zkClient.getChildren(path, watcher);
        }
    }, path, callback, cancelled);
    return new Cancellable() {

        @Override
        public void cancel() {
            cancelled.set(true);
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Cancellable(com.continuuity.weave.common.Cancellable) Watcher(org.apache.zookeeper.Watcher) SettableOperationFuture(com.continuuity.weave.internal.zookeeper.SettableOperationFuture)

Example 3 with Cancellable

use of com.continuuity.weave.common.Cancellable in project weave by continuuity.

the class DiscoveryServiceTestBase method testCancelChangeListener.

@Test
public void testCancelChangeListener() throws InterruptedException {
    Map.Entry<DiscoveryService, DiscoveryServiceClient> entry = create();
    DiscoveryService discoveryService = entry.getKey();
    DiscoveryServiceClient discoveryServiceClient = entry.getValue();
    String serviceName = "cancel_listener";
    ServiceDiscovered serviceDiscovered = discoveryServiceClient.discover(serviceName);
    // An executor that delay execute a Runnable. It's for testing race because listener cancel and discovery changes.
    Executor delayExecutor = new Executor() {

        @Override
        public void execute(final Runnable command) {
            Thread t = new Thread() {

                @Override
                public void run() {
                    try {
                        TimeUnit.SECONDS.sleep(2);
                        command.run();
                    } catch (InterruptedException e) {
                        throw Throwables.propagate(e);
                    }
                }
            };
            t.start();
        }
    };
    final BlockingQueue<List<Discoverable>> events = new ArrayBlockingQueue<List<Discoverable>>(10);
    Cancellable cancelWatch = serviceDiscovered.watchChanges(new ServiceDiscovered.ChangeListener() {

        @Override
        public void onChange(ServiceDiscovered serviceDiscovered) {
            events.add(ImmutableList.copyOf(serviceDiscovered));
        }
    }, delayExecutor);
    // Wait for the init event call
    Assert.assertNotNull(events.poll(3, TimeUnit.SECONDS));
    // Register a new service endpoint, wait a short while and then cancel the listener
    register(discoveryService, serviceName, "localhost", 1);
    TimeUnit.SECONDS.sleep(1);
    cancelWatch.cancel();
    // The change listener shouldn't get any event, since the invocation is delayed by the executor.
    Assert.assertNull(events.poll(3, TimeUnit.SECONDS));
}
Also used : Cancellable(com.continuuity.weave.common.Cancellable) Executor(java.util.concurrent.Executor) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Test(org.junit.Test)

Example 4 with Cancellable

use of com.continuuity.weave.common.Cancellable in project weave by continuuity.

the class DiscoveryServiceTestBase method simpleDiscoverable.

@Test
public void simpleDiscoverable() throws Exception {
    Map.Entry<DiscoveryService, DiscoveryServiceClient> entry = create();
    DiscoveryService discoveryService = entry.getKey();
    DiscoveryServiceClient discoveryServiceClient = entry.getValue();
    // Register one service running on one host:port
    Cancellable cancellable = register(discoveryService, "foo", "localhost", 8090);
    // Discover that registered host:port.
    ServiceDiscovered serviceDiscovered = discoveryServiceClient.discover("foo");
    Assert.assertTrue(waitTillExpected(1, serviceDiscovered));
    Discoverable discoverable = new Discoverable() {

        @Override
        public String getName() {
            return "foo";
        }

        @Override
        public InetSocketAddress getSocketAddress() {
            return new InetSocketAddress("localhost", 8090);
        }
    };
    // Check it exists.
    Assert.assertTrue(serviceDiscovered.contains(discoverable));
    // Remove the service
    cancellable.cancel();
    // There should be no service.
    Assert.assertTrue(waitTillExpected(0, serviceDiscovered));
    Assert.assertFalse(serviceDiscovered.contains(discoverable));
}
Also used : Cancellable(com.continuuity.weave.common.Cancellable) InetSocketAddress(java.net.InetSocketAddress) Map(java.util.Map) Test(org.junit.Test)

Example 5 with Cancellable

use of com.continuuity.weave.common.Cancellable in project weave by continuuity.

the class DiscoveryServiceTestBase method testChangeListener.

@Test
public void testChangeListener() throws InterruptedException {
    Map.Entry<DiscoveryService, DiscoveryServiceClient> entry = create();
    DiscoveryService discoveryService = entry.getKey();
    DiscoveryServiceClient discoveryServiceClient = entry.getValue();
    // Start discovery
    String serviceName = "listener_test";
    ServiceDiscovered serviceDiscovered = discoveryServiceClient.discover(serviceName);
    // Watch for changes.
    final BlockingQueue<List<Discoverable>> events = new ArrayBlockingQueue<List<Discoverable>>(10);
    serviceDiscovered.watchChanges(new ServiceDiscovered.ChangeListener() {

        @Override
        public void onChange(ServiceDiscovered serviceDiscovered) {
            events.add(ImmutableList.copyOf(serviceDiscovered));
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    // An empty list will be received first, as no endpoint has been registered.
    List<Discoverable> discoverables = events.poll(5, TimeUnit.SECONDS);
    Assert.assertNotNull(discoverables);
    Assert.assertTrue(discoverables.isEmpty());
    // Register a service
    Cancellable cancellable = register(discoveryService, serviceName, "localhost", 10000);
    discoverables = events.poll(5, TimeUnit.SECONDS);
    Assert.assertNotNull(discoverables);
    Assert.assertEquals(1, discoverables.size());
    // Register another service endpoint
    Cancellable cancellable2 = register(discoveryService, serviceName, "localhost", 10001);
    discoverables = events.poll(5, TimeUnit.SECONDS);
    Assert.assertNotNull(discoverables);
    Assert.assertEquals(2, discoverables.size());
    // Cancel both of them
    cancellable.cancel();
    cancellable2.cancel();
    // There could be more than one event triggered, but the last event should be an empty list.
    discoverables = events.poll(5, TimeUnit.SECONDS);
    Assert.assertNotNull(discoverables);
    if (!discoverables.isEmpty()) {
        discoverables = events.poll(5, TimeUnit.SECONDS);
    }
    Assert.assertTrue(discoverables.isEmpty());
}
Also used : Cancellable(com.continuuity.weave.common.Cancellable) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Cancellable (com.continuuity.weave.common.Cancellable)11 Map (java.util.Map)6 Test (org.junit.Test)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 SettableOperationFuture (com.continuuity.weave.internal.zookeeper.SettableOperationFuture)2 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)2 Watcher (org.apache.zookeeper.Watcher)2 RunId (com.continuuity.weave.api.RunId)1 ProcessController (com.continuuity.weave.internal.ProcessController)1 NodeChildren (com.continuuity.weave.zookeeper.NodeChildren)1 ZKClientService (com.continuuity.weave.zookeeper.ZKClientService)1 ZKOperations (com.continuuity.weave.zookeeper.ZKOperations)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 Set (java.util.Set)1 Executor (java.util.concurrent.Executor)1