Search in sources :

Example 51 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project cdap by caskdata.

the class PayloadTableTest method testConcurrentWrites.

@Test
public void testConcurrentWrites() throws Exception {
    // Create two threads, each of them writes to a different topic with two events in one store call.
    // The iterators in the two threads would alternate to produce payload. This is for testing CDAP-12013
    ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch storeCompletion = new CountDownLatch(2);
    for (int i = 0; i < 2; i++) {
        final TopicId topicId = NamespaceId.DEFAULT.topic("testConcurrentWrites" + i);
        TopicMetadata metadata = new TopicMetadata(topicId, DEFAULT_PROPERTY);
        try (MetadataTable metadataTable = getMetadataTable()) {
            metadataTable.createTopic(metadata);
        }
        final int threadId = i;
        executor.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                try (PayloadTable payloadTable = getPayloadTable()) {
                    payloadTable.store(new AbstractIterator<PayloadTable.Entry>() {

                        short messageCount = 0;

                        @Override
                        protected PayloadTable.Entry computeNext() {
                            if (messageCount >= 2) {
                                return endOfData();
                            }
                            try {
                                barrier.await();
                            } catch (Exception e) {
                                throw Throwables.propagate(e);
                            }
                            return new TestPayloadEntry(topicId, GENERATION, threadId, 0, messageCount, Bytes.toBytes("message " + threadId + " " + messageCount++));
                        }
                    });
                    storeCompletion.countDown();
                } catch (Exception e) {
                    LOG.error("Failed to store to MessageTable", e);
                }
                return null;
            }
        });
    }
    executor.shutdown();
    Assert.assertTrue(storeCompletion.await(5, TimeUnit.SECONDS));
    // Read from each topic. Each topic should have two messages
    for (int i = 0; i < 2; i++) {
        TopicId topicId = NamespaceId.DEFAULT.topic("testConcurrentWrites" + i);
        TopicMetadata metadata = new TopicMetadata(topicId, DEFAULT_PROPERTY);
        byte[] rawId = new byte[MessageId.RAW_ID_SIZE];
        MessageId.putRawId(0L, (short) 0, 0, (short) 0, rawId, 0);
        MessageId messageId = new MessageId(rawId);
        try (PayloadTable payloadTable = getPayloadTable();
            CloseableIterator<PayloadTable.Entry> iterator = payloadTable.fetch(metadata, i, messageId, true, 10)) {
            List<PayloadTable.Entry> entries = Lists.newArrayList(iterator);
            Assert.assertEquals(2, entries.size());
            int count = 0;
            for (PayloadTable.Entry entry : entries) {
                Assert.assertEquals("message " + i + " " + count++, Bytes.toString(entry.getPayload()));
            }
        }
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) TopicId(co.cask.cdap.proto.id.TopicId) AbstractIterator(com.google.common.collect.AbstractIterator) MessageId(co.cask.cdap.messaging.data.MessageId) Test(org.junit.Test)

Example 52 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project cdap by caskdata.

the class HBaseTestBase method parallelRun.

/**
 * Executes the given list of Runnable in parallel using a fixed thread pool executor. This method blocks
 * until all runnables finished.
 */
private void parallelRun(List<? extends Runnable> runnables) {
    ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(runnables.size()));
    try {
        List<ListenableFuture<?>> futures = new ArrayList<>(runnables.size());
        for (Runnable r : runnables) {
            futures.add(executor.submit(r));
        }
        Futures.getUnchecked(Futures.allAsList(futures));
    } finally {
        executor.shutdownNow();
        try {
            executor.awaitTermination(60, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            LOG.error("Interrupted", e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService)

Example 53 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project controller by opendaylight.

the class AbstractDataServiceTest method setUp.

@Before
public void setUp() {
    ListeningExecutorService executor = MoreExecutors.newDirectExecutorService();
    BindingBrokerTestFactory factory = new BindingBrokerTestFactory();
    factory.setExecutor(executor);
    factory.setStartWithParsedSchema(getStartWithSchema());
    testContext = factory.getTestContext();
    testContext.start();
}
Also used : BindingBrokerTestFactory(org.opendaylight.controller.sal.binding.test.util.BindingBrokerTestFactory) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Before(org.junit.Before)

Example 54 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project tutorials by eugenp.

the class MoreExecutorsUnitTest method whenExecutingRunnableInListeningExecutor_shouldLogThreadExecution.

@Test
public void whenExecutingRunnableInListeningExecutor_shouldLogThreadExecution() throws Exception {
    ConcurrentHashMap<String, Boolean> threadExecutions = new ConcurrentHashMap<>();
    Runnable logThreadRun = () -> threadExecutions.put(Thread.currentThread().getName(), true);
    ListeningExecutorService executor = MoreExecutors.newDirectExecutorService();
    executor.execute(logThreadRun);
    Assert.assertTrue(threadExecutions.get("main"));
}
Also used : ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Test(org.junit.Test)

Example 55 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project bgpcep by opendaylight.

the class MockedExecutorWrapper method getMockedExecutor.

ListeningExecutorService getMockedExecutor() {
    final ListeningExecutorService mockedExecutor = mock(ListeningExecutorService.class);
    final Answer<ListenableFuture<?>> submitAnswer = invocation -> {
        final Object task = invocation.getArguments()[0];
        this.submittedTasksToExecutor.add(task);
        Object result = null;
        if (task instanceof Runnable) {
            ((Runnable) task).run();
        } else if (task instanceof Callable) {
            result = ((Callable<?>) task).call();
        }
        final ListenableFuture<?> mockedFuture = mock(ListenableFuture.class);
        doReturn(result).when(mockedFuture).get();
        return mockedFuture;
    };
    doAnswer(submitAnswer).when(mockedExecutor).submit(any(Runnable.class));
    doAnswer(submitAnswer).when(mockedExecutor).submit(any(Callable.class));
    return mockedExecutor;
}
Also used : Matchers.any(org.mockito.Matchers.any) Answer(org.mockito.stubbing.Answer) List(java.util.List) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Callable(java.util.concurrent.Callable) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Callable(java.util.concurrent.Callable)

Aggregations

ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)201 Test (org.junit.Test)115 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)75 ArrayList (java.util.ArrayList)43 CountDownLatch (java.util.concurrent.CountDownLatch)29 ExecutorService (java.util.concurrent.ExecutorService)28 IOException (java.io.IOException)25 ExecutionException (java.util.concurrent.ExecutionException)25 Interval (org.joda.time.Interval)25 DateTime (org.joda.time.DateTime)23 List (java.util.List)21 Callable (java.util.concurrent.Callable)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 DruidServer (io.druid.client.DruidServer)18 DataSegment (io.druid.timeline.DataSegment)18 DruidServer (org.apache.druid.client.DruidServer)17 ImmutableMap (com.google.common.collect.ImmutableMap)16 File (java.io.File)16 Map (java.util.Map)16 ApplicationId (org.apache.hadoop.yarn.api.records.ApplicationId)15