Search in sources :

Example 16 with Cancellable

use of org.apache.twill.common.Cancellable in project cdap by caskdata.

the class NettyRouter method startServer.

private Cancellable startServer(final ServerBootstrap serverBootstrap, final ChannelGroup channelGroup) throws Exception {
    // Start listening on ports.
    Map<Integer, String> serviceMap = new HashMap<>();
    for (Map.Entry<String, Integer> forward : serviceToPortMap.entrySet()) {
        int port = forward.getValue();
        String service = forward.getKey();
        String boundService = serviceLookup.getService(port);
        if (boundService != null) {
            LOG.warn("Port {} is already configured to service {}, ignoring forward for service {}", port, boundService, service);
            continue;
        }
        InetSocketAddress bindAddress = new InetSocketAddress(hostname, port);
        LOG.info("Starting Netty Router for service {} on address {}...", service, bindAddress);
        try {
            Channel channel = serverBootstrap.bind(bindAddress).sync().channel();
            channelGroup.add(channel);
            InetSocketAddress boundAddress = (InetSocketAddress) channel.localAddress();
            serviceMap.put(boundAddress.getPort(), service);
            // Update service map
            serviceLookup.updateServiceMap(serviceMap);
            LOG.info("Started Netty Router for service {} on address {}.", service, boundAddress);
        } catch (Exception e) {
            if ((Throwables.getRootCause(e) instanceof BindException)) {
                throw new ServiceBindException("Router", hostname.getCanonicalHostName(), port, e);
            }
            throw e;
        }
    }
    return new Cancellable() {

        @Override
        public void cancel() {
            List<Future<?>> futures = new ArrayList<>();
            futures.add(channelGroup.close());
            futures.add(serverBootstrap.config().group().shutdownGracefully(0, 5, TimeUnit.SECONDS));
            futures.add(serverBootstrap.config().childGroup().shutdownGracefully(0, 5, TimeUnit.SECONDS));
            for (Future<?> future : futures) {
                future.awaitUninterruptibly();
            }
        }
    };
}
Also used : ServiceBindException(co.cask.cdap.common.ServiceBindException) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) Cancellable(org.apache.twill.common.Cancellable) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) BindException(java.net.BindException) ServiceBindException(co.cask.cdap.common.ServiceBindException) BindException(java.net.BindException) ServiceBindException(co.cask.cdap.common.ServiceBindException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Future(io.netty.util.concurrent.Future) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with Cancellable

use of org.apache.twill.common.Cancellable in project cdap by caskdata.

the class LocalStreamService method createSizeAggregator.

/**
 * Create a new aggregator for the {@code streamId}, and add it to the existing map of {@link Cancellable}
 * {@code aggregators}. This method does not cancel previously existing aggregator associated to the
 * {@code streamId}.
 *
 * @param streamId stream name to create a new aggregator for
 * @param baseCount stream size from which to start aggregating
 * @param threshold notification threshold after which to publish a notification - in MB
 * @return the created {@link StreamSizeAggregator}
 */
private StreamSizeAggregator createSizeAggregator(StreamId streamId, long baseCount, int threshold) {
    // Handle threshold changes
    final Cancellable thresholdSubscription = getStreamCoordinatorClient().addListener(streamId, new StreamPropertyListener() {

        @Override
        public void thresholdChanged(StreamId streamId, int threshold) {
            StreamSizeAggregator aggregator = aggregators.get(streamId);
            while (aggregator == null) {
                Thread.yield();
                aggregator = aggregators.get(streamId);
            }
            aggregator.setStreamThresholdMB(threshold);
        }
    });
    StreamSizeAggregator newAggregator = new StreamSizeAggregator(streamId, baseCount, threshold, thresholdSubscription);
    aggregators.put(streamId, newAggregator);
    return newAggregator;
}
Also used : StreamPropertyListener(co.cask.cdap.data.stream.StreamPropertyListener) StreamId(co.cask.cdap.proto.id.StreamId) Cancellable(org.apache.twill.common.Cancellable)

Example 18 with Cancellable

use of org.apache.twill.common.Cancellable in project cdap by caskdata.

the class DistributedStreamService method createSizeAggregator.

/**
 * Create a new aggregator for the {@code streamId}, and add it to the existing map of {@link Cancellable}
 * {@code aggregators}. This method does not cancel previously existing aggregator associated to the
 * {@code streamId}.
 *
 * @param streamId stream Id to create a new aggregator for
 * @param baseCount stream size from which to start aggregating
 * @param threshold notification threshold after which to publish a notification - in MB
 * @return the created {@link StreamSizeAggregator}
 */
private StreamSizeAggregator createSizeAggregator(StreamId streamId, long baseCount, int threshold) {
    LOG.debug("Creating size aggregator for stream {} with baseCount {} and threshold {}", streamId, baseCount, threshold);
    // Handle threshold changes
    final Cancellable thresholdSubscription = getStreamCoordinatorClient().addListener(streamId, new StreamPropertyListener() {

        @Override
        public void thresholdChanged(StreamId streamId, int threshold) {
            StreamSizeAggregator aggregator = aggregators.get(streamId);
            while (aggregator == null) {
                Thread.yield();
                aggregator = aggregators.get(streamId);
            }
            aggregator.setStreamThresholdMB(threshold);
        }
    });
    StreamSizeAggregator newAggregator = new StreamSizeAggregator(streamId, baseCount, threshold, thresholdSubscription);
    newAggregator.init();
    aggregators.put(streamId, newAggregator);
    return newAggregator;
}
Also used : StreamPropertyListener(co.cask.cdap.data.stream.StreamPropertyListener) StreamId(co.cask.cdap.proto.id.StreamId) Cancellable(org.apache.twill.common.Cancellable)

Example 19 with Cancellable

use of org.apache.twill.common.Cancellable in project cdap by caskdata.

the class InMemoryElectionTest method testElection.

@Test(timeout = 5000)
public void testElection() throws ExecutionException, InterruptedException, BrokenBarrierException {
    final InMemoryElectionRegistry electionRegistry = new InMemoryElectionRegistry();
    ExecutorService executor = Executors.newCachedThreadPool();
    // Create 5 participants to join leader election process simultaneously
    int participantCount = 5;
    final CyclicBarrier barrier = new CyclicBarrier(participantCount + 1);
    final Semaphore leaderSem = new Semaphore(0);
    final Semaphore followerSem = new Semaphore(0);
    final CountDownLatch[] stopLatch = new CountDownLatch[participantCount];
    try {
        final AtomicInteger currentLeader = new AtomicInteger(-1);
        for (int i = 0; i < participantCount; i++) {
            stopLatch[i] = new CountDownLatch(1);
            final int idx = i;
            executor.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        barrier.await();
                        Cancellable cancel = electionRegistry.join("test", new ElectionHandler() {

                            @Override
                            public void leader() {
                                currentLeader.set(idx);
                                leaderSem.release();
                            }

                            @Override
                            public void follower() {
                                followerSem.release();
                            }
                        });
                        stopLatch[idx].await(10, TimeUnit.SECONDS);
                        cancel.cancel();
                    } catch (Exception e) {
                        LOG.error(e.getMessage(), e);
                    }
                }
            });
        }
        // Sync the joining
        barrier.await();
        // There should be 1 leader and 4 followers
        leaderSem.tryAcquire(10, TimeUnit.SECONDS);
        followerSem.tryAcquire(participantCount - 1, 10, TimeUnit.SECONDS);
        // Continuously stopping leader until there is one left.
        for (int i = 0; i < participantCount - 1; i++) {
            stopLatch[currentLeader.get()].countDown();
            // Each time when the leader is unregistered from the leader election, a new leader would rise and
            // the old leader would become a follower.
            leaderSem.tryAcquire(10, TimeUnit.SECONDS);
            followerSem.tryAcquire(10, TimeUnit.SECONDS);
        }
        // Withdraw the last leader, it'd become follower as well.
        stopLatch[currentLeader.get()].countDown();
        followerSem.tryAcquire(10, TimeUnit.SECONDS);
    } finally {
        executor.shutdown();
        executor.awaitTermination(5L, TimeUnit.SECONDS);
    }
}
Also used : Cancellable(org.apache.twill.common.Cancellable) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ExecutionException(java.util.concurrent.ExecutionException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) ElectionHandler(org.apache.twill.api.ElectionHandler) Test(org.junit.Test)

Example 20 with Cancellable

use of org.apache.twill.common.Cancellable in project cdap by caskdata.

the class KafkaTester method getPublishedMessages.

public <T> Map<Integer, T> getPublishedMessages(String topic, Set<Integer> partitions, int expectedNumMsgs, final Function<FetchedMessage, T> converter) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(expectedNumMsgs);
    final CountDownLatch stopLatch = new CountDownLatch(1);
    final Map<Integer, T> actual = new HashMap<>();
    KafkaConsumer.Preparer preparer = kafkaClient.getConsumer().prepare();
    for (int partition : partitions) {
        preparer.addFromBeginning(topic, partition);
    }
    Cancellable cancellable = preparer.consume(new KafkaConsumer.MessageCallback() {

        @Override
        public long onReceived(Iterator<FetchedMessage> messages) {
            long offset = 0L;
            while (messages.hasNext()) {
                FetchedMessage message = messages.next();
                actual.put(message.getTopicPartition().getPartition(), converter.apply(message));
                latch.countDown();
                offset = message.getNextOffset();
            }
            return offset;
        }

        @Override
        public void finished() {
            stopLatch.countDown();
        }
    });
    Assert.assertTrue(String.format("Expected %d messages but found %d messages", expectedNumMsgs, actual.size()), latch.await(15, TimeUnit.SECONDS));
    cancellable.cancel();
    Assert.assertTrue(stopLatch.await(15, TimeUnit.SECONDS));
    return actual;
}
Also used : HashMap(java.util.HashMap) Cancellable(org.apache.twill.common.Cancellable) KafkaConsumer(org.apache.twill.kafka.client.KafkaConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) FetchedMessage(org.apache.twill.kafka.client.FetchedMessage)

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