use of org.apache.twill.common.Cancellable in project cdap by caskdata.
the class NettyRouterTestBase method startUp.
@Before
public void startUp() throws Exception {
List<ListenableFuture<Service.State>> futures = new ArrayList<>();
futures.add(routerService.start());
for (ServerService server : allServers) {
futures.add(server.start());
}
Futures.allAsList(futures).get();
// Wait for both servers of defaultService to be registered
ServiceDiscovered discover = ((DiscoveryServiceClient) discoveryService).discover(APP_FABRIC_SERVICE);
final CountDownLatch latch = new CountDownLatch(1);
Cancellable cancellable = discover.watchChanges(new ServiceDiscovered.ChangeListener() {
@Override
public void onChange(ServiceDiscovered serviceDiscovered) {
if (Iterables.size(serviceDiscovered) == allServers.size()) {
latch.countDown();
}
}
}, Threads.SAME_THREAD_EXECUTOR);
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
cancellable.cancel();
}
use of org.apache.twill.common.Cancellable in project cdap by caskdata.
the class LoggingContextAccessorTest method testReset.
@Test
public void testReset() {
Cancellable cancellable = LoggingContextAccessor.setLoggingContext(new GenericLoggingContext(OLD_NS, OLD_APP, OLD_ENTITY));
Assert.assertEquals(MDC.get(NamespaceLoggingContext.TAG_NAMESPACE_ID), OLD_NS);
Assert.assertEquals(MDC.get(ApplicationLoggingContext.TAG_APPLICATION_ID), OLD_APP);
Assert.assertEquals(MDC.get(GenericLoggingContext.TAG_ENTITY_ID), OLD_ENTITY);
final Cancellable cancellable2 = LoggingContextAccessor.setLoggingContext(new GenericLoggingContext(NS, APP, ENTITY));
Assert.assertEquals(MDC.get(NamespaceLoggingContext.TAG_NAMESPACE_ID), NS);
Assert.assertEquals(MDC.get(ApplicationLoggingContext.TAG_APPLICATION_ID), APP);
Assert.assertEquals(MDC.get(GenericLoggingContext.TAG_ENTITY_ID), ENTITY);
// Verify a different thread cannot change context
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
cancellable2.cancel();
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
Assert.fail();
}
Assert.assertEquals(MDC.get(NamespaceLoggingContext.TAG_NAMESPACE_ID), NS);
Assert.assertEquals(MDC.get(ApplicationLoggingContext.TAG_APPLICATION_ID), APP);
Assert.assertEquals(MDC.get(GenericLoggingContext.TAG_ENTITY_ID), ENTITY);
// Reset in the same thread
cancellable2.cancel();
Assert.assertEquals(MDC.get(NamespaceLoggingContext.TAG_NAMESPACE_ID), OLD_NS);
Assert.assertEquals(MDC.get(ApplicationLoggingContext.TAG_APPLICATION_ID), OLD_APP);
Assert.assertEquals(MDC.get(GenericLoggingContext.TAG_ENTITY_ID), OLD_ENTITY);
// Check reset back to nothing
cancellable.cancel();
Assert.assertTrue(MDC.getCopyOfContextMap().isEmpty());
}
use of org.apache.twill.common.Cancellable in project cdap by caskdata.
the class KafkaOffsetResolverTest method waitForAllLogsPublished.
private void waitForAllLogsPublished(String topic, int logsNum) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(logsNum);
final CountDownLatch stopLatch = new CountDownLatch(1);
Cancellable cancel = KAFKA_TESTER.getKafkaClient().getConsumer().prepare().add(topic, 0, 0).consume(new KafkaConsumer.MessageCallback() {
@Override
public long onReceived(Iterator<FetchedMessage> messages) {
long nextOffset = -1L;
while (messages.hasNext()) {
FetchedMessage message = messages.next();
nextOffset = message.getNextOffset();
Assert.assertTrue(latch.getCount() > 0);
latch.countDown();
}
return nextOffset;
}
@Override
public void finished() {
stopLatch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
cancel.cancel();
Assert.assertTrue(stopLatch.await(1, TimeUnit.SECONDS));
}
use of org.apache.twill.common.Cancellable in project cdap by caskdata.
the class NotificationTest method useTransactionTest.
@Test
public void useTransactionTest() throws Exception {
// Performing admin operations to create dataset instance
// keyValueTable is a system dataset module
namespaceAdmin.create(new NamespaceMeta.Builder().setName(namespace).build());
DatasetId myTableInstance = namespace.dataset("myTable");
dsFramework.addInstance("keyValueTable", myTableInstance, DatasetProperties.EMPTY);
final CountDownLatch receivedLatch = new CountDownLatch(1);
Assert.assertTrue(feedManager.createFeed(FEED1_INFO));
try {
Cancellable cancellable = notificationService.subscribe(FEED1, new NotificationHandler<String>() {
private int received = 0;
@Override
public Type getNotificationType() {
return String.class;
}
@Override
public void received(final String notification, NotificationContext notificationContext) {
notificationContext.execute(new TxRunnable() {
@Override
public void run(DatasetContext context) throws Exception {
KeyValueTable table = context.getDataset("myTable");
table.write("foo", String.format("%s-%d", notification, received++));
receivedLatch.countDown();
}
}, TxRetryPolicy.maxRetries(5));
}
});
// Short delay for the subscriber to setup the subscription.
TimeUnit.MILLISECONDS.sleep(500);
try {
notificationService.publish(FEED1, "foobar");
// Waiting for the subscriber to receive that notification
Assert.assertTrue(receivedLatch.await(5, TimeUnit.SECONDS));
// Read the KeyValueTable for the value updated from the subscriber.
// Need to poll it couple times since after the received method returned,
// the tx may not yet committed when we try to read it here.
final KeyValueTable table = dsFramework.getDataset(myTableInstance, DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(table);
final TransactionContext txContext = new TransactionContext(txClient, table);
Tasks.waitFor(true, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
txContext.start();
try {
return "foobar-0".equals(Bytes.toString(table.read("foo")));
} finally {
txContext.finish();
}
}
}, 5, TimeUnit.SECONDS);
} finally {
cancellable.cancel();
}
} finally {
dsFramework.deleteInstance(myTableInstance);
feedManager.deleteFeed(FEED1);
namespaceAdmin.delete(namespace);
}
}
use of org.apache.twill.common.Cancellable in project cdap by caskdata.
the class AbstractProgramController method addListener.
@Override
public final Cancellable addListener(Listener listener, final Executor listenerExecutor) {
Preconditions.checkNotNull(listener, "Listener shouldn't be null.");
Preconditions.checkNotNull(listenerExecutor, "Executor shouldn't be null.");
final ListenerCaller caller = new ListenerCaller(listener, listenerExecutor);
final Cancellable cancellable = new Cancellable() {
@Override
public void cancel() {
// Simply remove the listener from the map through the executor and block on the completion
Futures.getUnchecked(executor.submit(new Runnable() {
@Override
public void run() {
listeners.remove(caller);
}
}));
}
};
try {
// Use a synchronous queue to communicate the Cancellable to return
final SynchronousQueue<Cancellable> result = new SynchronousQueue<>();
// Use the single thread executor to add the listener and call init
executor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
Cancellable existing = listeners.get(caller);
if (existing == null) {
listeners.put(caller, cancellable);
result.put(cancellable);
caller.init(getState(), getFailureCause());
} else {
result.put(existing);
}
return null;
}
});
return result.take();
} catch (Exception e) {
// there shouldn't be interrupted exception as well.
throw Throwables.propagate(Throwables.getRootCause(e));
}
}
Aggregations