Example usage for java.util.concurrent RunnableFuture get

List of usage examples for java.util.concurrent RunnableFuture get

Introduction

In this page you can find the example usage for java.util.concurrent RunnableFuture get.

Prototype

V get() throws InterruptedException, ExecutionException;

Source Link

Document

Waits if necessary for the computation to complete, and then retrieves its result.

Usage

From source file:co.pugo.convert.ConvertServlet.java

/**
 * download imageData and encode it base64
 * @param imageLinks set of image links extracted with extractImageLinks()
 * @return map, key = imageLink, value = base64 encoded image
 *//*from   w w w  .  j  av  a 2s .c  om*/
private HashMap<String, String> downloadImageData(Set<String> imageLinks) {
    HashMap<String, String> imageData = new HashMap<>();
    ExecutorService service = Executors.newCachedThreadPool();
    for (final String imageLink : imageLinks) {
        RunnableFuture<byte[]> future = new FutureTask<>(new Callable<byte[]>() {
            @Override
            public byte[] call() {
                try {
                    URL srcUrl = new URL(imageLink);
                    URLConnection urlConnection = srcUrl.openConnection();
                    return IOUtils.toByteArray(urlConnection.getInputStream());
                } catch (IOException e) {
                    LOG.severe(e.getMessage());
                    return null;
                }
            }
        });
        service.execute(future);
        try {
            imageData.put(imageLink, Base64.encodeBase64String(future.get()));
        } catch (InterruptedException | ExecutionException e) {
            LOG.severe(e.getMessage());
        }
    }
    service.shutdown();
    try {
        service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    } catch (InterruptedException e) {
        LOG.severe(e.getMessage());
    }
    return imageData;
}

From source file:org.apache.flink.contrib.streaming.state.RocksDBStateBackendTest.java

@Test
public void testRunningSnapshotAfterBackendClosed() throws Exception {
    setupRocksKeyedStateBackend();//from   w w  w.  j  a  v  a2 s . c  om
    RunnableFuture<KeyGroupsStateHandle> snapshot = keyedStateBackend.snapshot(0L, 0L, testStreamFactory,
            CheckpointOptions.forFullCheckpoint());

    RocksDB spyDB = keyedStateBackend.db;

    verify(spyDB, times(1)).getSnapshot();
    verify(spyDB, times(0)).releaseSnapshot(any(Snapshot.class));

    this.keyedStateBackend.dispose();
    verify(spyDB, times(1)).close();
    assertEquals(null, keyedStateBackend.db);

    //Ensure every RocksObjects not closed yet
    for (RocksObject rocksCloseable : allCreatedCloseables) {
        verify(rocksCloseable, times(0)).close();
    }

    Thread asyncSnapshotThread = new Thread(snapshot);
    asyncSnapshotThread.start();
    try {
        snapshot.get();
        fail();
    } catch (Exception ignored) {

    }

    asyncSnapshotThread.join();

    //Ensure every RocksObject was closed exactly once
    for (RocksObject rocksCloseable : allCreatedCloseables) {
        verify(rocksCloseable, times(1)).close();
    }

}

From source file:org.apache.flink.contrib.streaming.state.RocksDBStateBackendTest.java

@Test
public void testDismissingSnapshotNotRunnable() throws Exception {
    setupRocksKeyedStateBackend();//w w  w .  j  ava  2s  .com
    RunnableFuture<KeyGroupsStateHandle> snapshot = keyedStateBackend.snapshot(0L, 0L, testStreamFactory,
            CheckpointOptions.forFullCheckpoint());
    snapshot.cancel(true);
    Thread asyncSnapshotThread = new Thread(snapshot);
    asyncSnapshotThread.start();
    try {
        snapshot.get();
        fail();
    } catch (Exception ignored) {

    }
    asyncSnapshotThread.join();
    verifyRocksObjectsReleased();
}

From source file:org.apache.flink.contrib.streaming.state.RocksDBStateBackendTest.java

@Test
public void testCompletingSnapshot() throws Exception {
    setupRocksKeyedStateBackend();//from   w ww  .  ja  va2s .  co  m
    RunnableFuture<KeyGroupsStateHandle> snapshot = keyedStateBackend.snapshot(0L, 0L, testStreamFactory,
            CheckpointOptions.forFullCheckpoint());
    Thread asyncSnapshotThread = new Thread(snapshot);
    asyncSnapshotThread.start();
    waiter.await(); // wait for snapshot to run
    waiter.reset();
    runStateUpdates();
    blocker.trigger(); // allow checkpointing to start writing
    waiter.await(); // wait for snapshot stream writing to run
    KeyGroupsStateHandle keyGroupsStateHandle = snapshot.get();
    assertNotNull(keyGroupsStateHandle);
    assertTrue(keyGroupsStateHandle.getStateSize() > 0);
    assertEquals(2, keyGroupsStateHandle.getNumberOfKeyGroups());
    assertTrue(testStreamFactory.getLastCreatedStream().isClosed());
    asyncSnapshotThread.join();
    verifyRocksObjectsReleased();
}

From source file:org.apache.flink.contrib.streaming.state.RocksDBStateBackendTest.java

@Test
public void testCancelRunningSnapshot() throws Exception {
    setupRocksKeyedStateBackend();/*from w ww. j a va  2 s .co  m*/
    RunnableFuture<KeyGroupsStateHandle> snapshot = keyedStateBackend.snapshot(0L, 0L, testStreamFactory,
            CheckpointOptions.forFullCheckpoint());
    Thread asyncSnapshotThread = new Thread(snapshot);
    asyncSnapshotThread.start();
    waiter.await(); // wait for snapshot to run
    waiter.reset();
    runStateUpdates();
    blocker.trigger(); // allow checkpointing to start writing
    snapshot.cancel(true);
    assertTrue(testStreamFactory.getLastCreatedStream().isClosed());
    waiter.await(); // wait for snapshot stream writing to run
    try {
        snapshot.get();
        fail();
    } catch (Exception ignored) {
    }

    verifyRocksObjectsReleased();
    asyncSnapshotThread.join();
}

From source file:org.apache.flink.runtime.state.StateBackendTestBase.java

private KeyGroupsStateHandle runSnapshot(RunnableFuture<KeyGroupsStateHandle> snapshotRunnableFuture)
        throws Exception {
    if (!snapshotRunnableFuture.isDone()) {
        Thread runner = new Thread(snapshotRunnableFuture);
        runner.start();/*  w  ww .  j  a va 2 s .co  m*/
    }
    return snapshotRunnableFuture.get();
}

From source file:org.apache.flink.runtime.state.StateSnapshotCompressionTest.java

private void snapshotRestoreRoundtrip(boolean useCompression) throws Exception {

    ExecutionConfig executionConfig = new ExecutionConfig();
    executionConfig.setUseSnapshotCompression(useCompression);

    KeyedStateHandle stateHandle = null;

    ValueStateDescriptor<String> stateDescriptor = new ValueStateDescriptor<>("test", String.class);
    stateDescriptor.initializeSerializerUnlessSet(executionConfig);

    AbstractKeyedStateBackend<String> stateBackend = new HeapKeyedStateBackend<>(
            mock(TaskKvStateRegistry.class), StringSerializer.INSTANCE,
            StateSnapshotCompressionTest.class.getClassLoader(), 16, new KeyGroupRange(0, 15), true,
            executionConfig, TestLocalRecoveryConfig.disabled(), mock(HeapPriorityQueueSetFactory.class),
            TtlTimeProvider.DEFAULT);//from  w  ww  .  ja  v  a  2 s. co m

    try {

        InternalValueState<String, VoidNamespace, String> state = stateBackend
                .createInternalState(new VoidNamespaceSerializer(), stateDescriptor);

        stateBackend.setCurrentKey("A");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        state.update("42");
        stateBackend.setCurrentKey("B");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        state.update("43");
        stateBackend.setCurrentKey("C");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        state.update("44");
        stateBackend.setCurrentKey("D");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        state.update("45");
        CheckpointStreamFactory streamFactory = new MemCheckpointStreamFactory(4 * 1024 * 1024);
        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot = stateBackend.snapshot(0L, 0L, streamFactory,
                CheckpointOptions.forCheckpointWithDefaultLocation());
        snapshot.run();
        SnapshotResult<KeyedStateHandle> snapshotResult = snapshot.get();
        stateHandle = snapshotResult.getJobManagerOwnedSnapshot();

    } finally {
        IOUtils.closeQuietly(stateBackend);
        stateBackend.dispose();
    }

    executionConfig = new ExecutionConfig();

    stateBackend = new HeapKeyedStateBackend<>(mock(TaskKvStateRegistry.class), StringSerializer.INSTANCE,
            StateSnapshotCompressionTest.class.getClassLoader(), 16, new KeyGroupRange(0, 15), true,
            executionConfig, TestLocalRecoveryConfig.disabled(), mock(HeapPriorityQueueSetFactory.class),
            TtlTimeProvider.DEFAULT);
    try {

        stateBackend.restore(StateObjectCollection.singleton(stateHandle));

        InternalValueState<String, VoidNamespace, String> state = stateBackend
                .createInternalState(new VoidNamespaceSerializer(), stateDescriptor);

        stateBackend.setCurrentKey("A");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        Assert.assertEquals("42", state.value());
        stateBackend.setCurrentKey("B");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        Assert.assertEquals("43", state.value());
        stateBackend.setCurrentKey("C");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        Assert.assertEquals("44", state.value());
        stateBackend.setCurrentKey("D");
        state.setCurrentNamespace(VoidNamespace.INSTANCE);
        Assert.assertEquals("45", state.value());

    } finally {
        IOUtils.closeQuietly(stateBackend);
        stateBackend.dispose();
    }
}