Search in sources :

Example 6 with Callable

use of java.util.concurrent.Callable in project flink by apache.

the class FlinkFuture method thenCombineAsync.

@Override
public <U, R> Future<R> thenCombineAsync(final Future<U> other, final BiFunction<? super T, ? super U, ? extends R> biFunction, final Executor executor) {
    Preconditions.checkNotNull(other);
    Preconditions.checkNotNull(biFunction);
    Preconditions.checkNotNull(executor);
    final ExecutionContext executionContext = createExecutionContext(executor);
    final scala.concurrent.Future<U> thatScalaFuture;
    if (other instanceof FlinkFuture) {
        thatScalaFuture = ((FlinkFuture<U>) other).scalaFuture;
    } else {
        thatScalaFuture = Futures.future(new Callable<U>() {

            @Override
            public U call() throws Exception {
                try {
                    return other.get();
                } catch (ExecutionException e) {
                    // unwrap the execution exception if the cause is an Exception
                    if (e.getCause() instanceof Exception) {
                        throw (Exception) e.getCause();
                    } else {
                        // it's an error or a throwable which we have to wrap for the moment
                        throw new FlinkFuture.ThrowableWrapperException(e.getCause());
                    }
                }
            }
        }, executionContext);
    }
    scala.concurrent.Future<R> result = scalaFuture.zip(thatScalaFuture).map(new Mapper<Tuple2<T, U>, R>() {

        @Override
        public R apply(Tuple2<T, U> tuple2) {
            return biFunction.apply(tuple2._1, tuple2._2);
        }
    }, executionContext);
    return new FlinkFuture<>(result);
}
Also used : Callable(java.util.concurrent.Callable) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) ExecutionContext(scala.concurrent.ExecutionContext) Tuple2(scala.Tuple2) ExecutionException(java.util.concurrent.ExecutionException)

Example 7 with Callable

use of java.util.concurrent.Callable in project flink by apache.

the class RemoteInputChannelTest method testConcurrentOnBufferAndRelease.

@Test
public void testConcurrentOnBufferAndRelease() throws Exception {
    // Config
    // Repeatedly spawn two tasks: one to queue buffers and the other to release the channel
    // concurrently. We do this repeatedly to provoke races.
    final int numberOfRepetitions = 8192;
    // Setup
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    try {
        // Test
        final SingleInputGate inputGate = mock(SingleInputGate.class);
        for (int i = 0; i < numberOfRepetitions; i++) {
            final RemoteInputChannel inputChannel = createRemoteInputChannel(inputGate);
            final Callable<Void> enqueueTask = new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    while (true) {
                        for (int j = 0; j < 128; j++) {
                            inputChannel.onBuffer(TestBufferFactory.getMockBuffer(), j);
                        }
                        if (inputChannel.isReleased()) {
                            return null;
                        }
                    }
                }
            };
            final Callable<Void> releaseTask = new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    inputChannel.releaseAllResources();
                    return null;
                }
            };
            // Submit tasks and wait to finish
            List<Future<Void>> results = Lists.newArrayListWithCapacity(2);
            results.add(executor.submit(enqueueTask));
            results.add(executor.submit(releaseTask));
            for (Future<Void> result : results) {
                result.get();
            }
            assertEquals("Resource leak during concurrent release and enqueue.", 0, inputChannel.getNumberOfQueuedBuffers());
        }
    } finally {
        executor.shutdown();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 8 with Callable

use of java.util.concurrent.Callable in project flink by apache.

the class AsynchronousBufferFileWriterTest method testConcurrentSubscribeAndHandleRequest.

@Test
public void testConcurrentSubscribeAndHandleRequest() throws Exception {
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final TestNotificationListener listener = new TestNotificationListener();
    final Callable<Boolean> subscriber = new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return writer.registerAllRequestsProcessedListener(listener);
        }
    };
    final Callable<Void> requestHandler = new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            handleRequest();
            return null;
        }
    };
    try {
        // Repeat this to provoke races
        for (int i = 0; i < 50000; i++) {
            listener.reset();
            addRequest();
            Future<Void> handleRequestFuture = executor.submit(requestHandler);
            Future<Boolean> subscribeFuture = executor.submit(subscriber);
            handleRequestFuture.get();
            try {
                if (subscribeFuture.get()) {
                    assertEquals("Race: Successfully subscribed, but was never notified.", 1, listener.getNumberOfNotifications());
                } else {
                    assertEquals("Race: Never subscribed successfully, but was notified.", 0, listener.getNumberOfNotifications());
                }
            } catch (Throwable t) {
                System.out.println(i);
                Assert.fail(t.getMessage());
            }
        }
    } finally {
        executor.shutdownNow();
    }
}
Also used : TestNotificationListener(org.apache.flink.runtime.io.network.util.TestNotificationListener) ExecutorService(java.util.concurrent.ExecutorService) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 9 with Callable

use of java.util.concurrent.Callable in project flink by apache.

the class AsynchronousFileIOChannelTest method testClosedButAddRequestAndRegisterListenerRace.

@Test
public void testClosedButAddRequestAndRegisterListenerRace() throws Exception {
    // -- Config ----------------------------------------------------------
    final int numberOfRuns = 1024;
    // -- Setup -----------------------------------------------------------
    final IOManagerAsync ioManager = new IOManagerAsync();
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final RequestQueue<WriteRequest> requestQueue = new RequestQueue<WriteRequest>();
    @SuppressWarnings("unchecked") final RequestDoneCallback<Buffer> ioChannelCallback = mock(RequestDoneCallback.class);
    final TestNotificationListener listener = new TestNotificationListener();
    // -- The Test --------------------------------------------------------
    try {
        // Repeatedly close the channel and add a request.
        for (int i = 0; i < numberOfRuns; i++) {
            final TestAsyncFileIOChannel ioChannel = new TestAsyncFileIOChannel(ioManager.createChannel(), requestQueue, ioChannelCallback, true);
            final CountDownLatch sync = new CountDownLatch(2);
            final WriteRequest request = mock(WriteRequest.class);
            ioChannel.close();
            // Add request task
            Callable<Void> addRequestTask = new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    try {
                        ioChannel.addRequest(request);
                    } catch (Throwable expected) {
                    } finally {
                        sync.countDown();
                    }
                    return null;
                }
            };
            // Listener
            Callable<Void> registerListenerTask = new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    try {
                        while (true) {
                            int current = listener.getNumberOfNotifications();
                            if (ioChannel.registerAllRequestsProcessedListener(listener)) {
                                listener.waitForNotification(current);
                            } else if (ioChannel.isClosed()) {
                                break;
                            }
                        }
                    } finally {
                        sync.countDown();
                    }
                    return null;
                }
            };
            executor.submit(addRequestTask);
            executor.submit(registerListenerTask);
            if (!sync.await(2, TimeUnit.MINUTES)) {
                fail("Test failed due to a timeout. This indicates a deadlock due to the way" + "that listeners are registered/notified in the asynchronous file I/O" + "channel.");
            }
        }
    } finally {
        ioManager.shutdown();
        executor.shutdown();
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) TestNotificationListener(org.apache.flink.runtime.io.network.util.TestNotificationListener) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 10 with Callable

use of java.util.concurrent.Callable in project hadoop by apache.

the class FCStatisticsBaseTest method testStatisticsThreadLocalDataCleanUp.

@Test(timeout = 70000)
public void testStatisticsThreadLocalDataCleanUp() throws Exception {
    final Statistics stats = new Statistics("test");
    // create a small thread pool to test the statistics
    final int size = 2;
    ExecutorService es = Executors.newFixedThreadPool(size);
    List<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>(size);
    for (int i = 0; i < size; i++) {
        tasks.add(new Callable<Boolean>() {

            public Boolean call() {
                // this populates the data set in statistics
                stats.incrementReadOps(1);
                return true;
            }
        });
    }
    // run the threads
    es.invokeAll(tasks);
    // assert that the data size is exactly the number of threads
    final AtomicInteger allDataSize = new AtomicInteger(0);
    allDataSize.set(stats.getAllThreadLocalDataSize());
    Assert.assertEquals(size, allDataSize.get());
    Assert.assertEquals(size, stats.getReadOps());
    // force the GC to collect the threads by shutting down the thread pool
    es.shutdownNow();
    es.awaitTermination(1, TimeUnit.MINUTES);
    es = null;
    // force GC to garbage collect threads
    System.gc();
    // wait for up to 60 seconds
    GenericTestUtils.waitFor(new Supplier<Boolean>() {

        @Override
        public Boolean get() {
            int size = stats.getAllThreadLocalDataSize();
            allDataSize.set(size);
            if (size == 0) {
                return true;
            }
            LOG.warn("not all references have been cleaned up; still " + allDataSize.get() + " references left");
            LOG.warn("triggering another GC");
            System.gc();
            return false;
        }
    }, 500, 60 * 1000);
    Assert.assertEquals(0, allDataSize.get());
    Assert.assertEquals(size, stats.getReadOps());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Statistics(org.apache.hadoop.fs.FileSystem.Statistics) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Aggregations

Callable (java.util.concurrent.Callable)850 Test (org.junit.Test)254 ArrayList (java.util.ArrayList)245 ExecutorService (java.util.concurrent.ExecutorService)243 Future (java.util.concurrent.Future)203 IOException (java.io.IOException)118 ExecutionException (java.util.concurrent.ExecutionException)103 CountDownLatch (java.util.concurrent.CountDownLatch)77 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)77 Ignite (org.apache.ignite.Ignite)60 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)58 File (java.io.File)52 HashMap (java.util.HashMap)44 List (java.util.List)44 Map (java.util.Map)33 LinkedList (java.util.LinkedList)32 HashSet (java.util.HashSet)29 IgniteException (org.apache.ignite.IgniteException)27 Message (org.graylog2.plugin.Message)26 Result (org.graylog2.plugin.inputs.Extractor.Result)26