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);
}
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();
}
}
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();
}
}
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();
}
}
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());
}
Aggregations