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);
}
};
}
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);
}
};
}
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));
}
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));
}
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());
}
Aggregations