Search in sources :

Example 21 with Cancellable

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();
}
Also used : DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) Cancellable(org.apache.twill.common.Cancellable) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) InMemoryDiscoveryService(org.apache.twill.discovery.InMemoryDiscoveryService) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) DiscoveryService(org.apache.twill.discovery.DiscoveryService) Service(com.google.common.util.concurrent.Service) NettyHttpService(co.cask.http.NettyHttpService) ServiceDiscovered(org.apache.twill.discovery.ServiceDiscovered) CountDownLatch(java.util.concurrent.CountDownLatch) Before(org.junit.Before)

Example 22 with Cancellable

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());
}
Also used : Cancellable(org.apache.twill.common.Cancellable) GenericLoggingContext(co.cask.cdap.logging.context.GenericLoggingContext) Test(org.junit.Test)

Example 23 with Cancellable

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));
}
Also used : Cancellable(org.apache.twill.common.Cancellable) KafkaConsumer(org.apache.twill.kafka.client.KafkaConsumer) FetchedMessage(org.apache.twill.kafka.client.FetchedMessage) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 24 with Cancellable

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);
    }
}
Also used : Cancellable(org.apache.twill.common.Cancellable) CountDownLatch(java.util.concurrent.CountDownLatch) NotificationFeedNotFoundException(co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException) DatasetId(co.cask.cdap.proto.id.DatasetId) NotificationContext(co.cask.cdap.notifications.service.NotificationContext) Type(java.lang.reflect.Type) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) TxRunnable(co.cask.cdap.api.TxRunnable) KeyValueTable(co.cask.cdap.api.dataset.lib.KeyValueTable) TransactionContext(org.apache.tephra.TransactionContext) DatasetContext(co.cask.cdap.api.data.DatasetContext) Test(org.junit.Test)

Example 25 with Cancellable

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));
    }
}
Also used : Cancellable(org.apache.twill.common.Cancellable) SynchronousQueue(java.util.concurrent.SynchronousQueue)

Aggregations

Cancellable (org.apache.twill.common.Cancellable)27 CountDownLatch (java.util.concurrent.CountDownLatch)7 Test (org.junit.Test)6 InetSocketAddress (java.net.InetSocketAddress)5 Discoverable (org.apache.twill.discovery.Discoverable)5 HttpContentProducer (co.cask.cdap.api.service.http.HttpContentProducer)3 ResolvingDiscoverable (co.cask.cdap.common.discovery.ResolvingDiscoverable)3 CombineClassLoader (co.cask.cdap.common.lang.CombineClassLoader)3 NotificationFeedNotFoundException (co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException)3 ArrayList (java.util.ArrayList)3 ZKClientService (org.apache.twill.zookeeper.ZKClientService)3 MetricsContext (co.cask.cdap.api.metrics.MetricsContext)2 HttpServiceContext (co.cask.cdap.api.service.http.HttpServiceContext)2 SparkRunnerClassLoader (co.cask.cdap.app.runtime.spark.classloader.SparkRunnerClassLoader)2 FilterClassLoader (co.cask.cdap.common.lang.FilterClassLoader)2 StreamPropertyListener (co.cask.cdap.data.stream.StreamPropertyListener)2 NotificationContext (co.cask.cdap.notifications.service.NotificationContext)2 StreamId (co.cask.cdap.proto.id.StreamId)2 BodyProducer (co.cask.http.BodyProducer)2 ImmutableSet (com.google.common.collect.ImmutableSet)2