Search in sources :

Example 1 with CheckpointedStateScope

use of org.apache.flink.runtime.state.CheckpointedStateScope in project flink by apache.

the class RocksDBStateUploaderTest method testMultiThreadUploadThreadPoolExceptionRethrow.

/**
 * Test that the exception arose in the thread pool will rethrow to the main thread.
 */
@Test
public void testMultiThreadUploadThreadPoolExceptionRethrow() throws IOException {
    SpecifiedException expectedException = new SpecifiedException("throw exception while multi thread upload states.");
    CheckpointStateOutputStream outputStream = createFailingCheckpointStateOutputStream(expectedException);
    CheckpointStreamFactory checkpointStreamFactory = new CheckpointStreamFactory() {

        @Override
        public CheckpointStateOutputStream createCheckpointStateOutputStream(CheckpointedStateScope scope) throws IOException {
            return outputStream;
        }

        @Override
        public boolean canFastDuplicate(StreamStateHandle stateHandle, CheckpointedStateScope scope) throws IOException {
            return false;
        }

        @Override
        public List<StreamStateHandle> duplicate(List<StreamStateHandle> stateHandles, CheckpointedStateScope scope) throws IOException {
            return null;
        }
    };
    File file = temporaryFolder.newFile(String.valueOf(UUID.randomUUID()));
    generateRandomFileContent(file.getPath(), 20);
    Map<StateHandleID, Path> filePaths = new HashMap<>(1);
    filePaths.put(new StateHandleID("mockHandleID"), file.toPath());
    try (RocksDBStateUploader rocksDBStateUploader = new RocksDBStateUploader(5)) {
        rocksDBStateUploader.uploadFilesToCheckpointFs(filePaths, checkpointStreamFactory, CheckpointedStateScope.SHARED, new CloseableRegistry());
        fail();
    } catch (Exception e) {
        assertEquals(expectedException, e);
    }
}
Also used : Path(java.nio.file.Path) FsCheckpointStreamFactory(org.apache.flink.runtime.state.filesystem.FsCheckpointStreamFactory) CheckpointStreamFactory(org.apache.flink.runtime.state.CheckpointStreamFactory) HashMap(java.util.HashMap) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) IOException(java.io.IOException) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) ByteStreamStateHandle(org.apache.flink.runtime.state.memory.ByteStreamStateHandle) StateHandleID(org.apache.flink.runtime.state.StateHandleID) CheckpointStateOutputStream(org.apache.flink.runtime.state.CheckpointStateOutputStream) List(java.util.List) CheckpointedStateScope(org.apache.flink.runtime.state.CheckpointedStateScope) File(java.io.File) Test(org.junit.Test)

Example 2 with CheckpointedStateScope

use of org.apache.flink.runtime.state.CheckpointedStateScope in project flink by apache.

the class RocksDBAsyncSnapshotTest method testCancelFullyAsyncCheckpoints.

/**
 * This tests ensures that canceling of asynchronous snapshots works as expected and does not
 * block.
 */
@Test
public void testCancelFullyAsyncCheckpoints() throws Exception {
    final OneInputStreamTaskTestHarness<String, String> testHarness = new OneInputStreamTaskTestHarness<>(OneInputStreamTask::new, BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO);
    testHarness.setupOutputForSingletonOperatorChain();
    testHarness.configureForKeyedStream(value -> value, BasicTypeInfo.STRING_TYPE_INFO);
    StreamConfig streamConfig = testHarness.getStreamConfig();
    File dbDir = temporaryFolder.newFolder();
    final EmbeddedRocksDBStateBackend.PriorityQueueStateType timerServicePriorityQueueType = RocksDBOptions.TIMER_SERVICE_FACTORY.defaultValue();
    final int skipStreams;
    if (timerServicePriorityQueueType == EmbeddedRocksDBStateBackend.PriorityQueueStateType.HEAP) {
        // we skip the first created stream, because it is used to checkpoint the timer service,
        // which is
        // currently not asynchronous.
        skipStreams = 1;
    } else if (timerServicePriorityQueueType == EmbeddedRocksDBStateBackend.PriorityQueueStateType.ROCKSDB) {
        skipStreams = 0;
    } else {
        throw new AssertionError(String.format("Unknown timer service priority queue type %s.", timerServicePriorityQueueType));
    }
    // this is the proper instance that we need to call.
    BlockerCheckpointStreamFactory blockerCheckpointStreamFactory = new BlockerCheckpointStreamFactory(4 * 1024 * 1024) {

        int count = skipStreams;

        @Override
        public CheckpointStateOutputStream createCheckpointStateOutputStream(CheckpointedStateScope scope) throws IOException {
            if (count > 0) {
                --count;
                return new BlockingCheckpointOutputStream(new MemCheckpointStreamFactory.MemoryCheckpointOutputStream(maxSize), null, null, Integer.MAX_VALUE);
            } else {
                return super.createCheckpointStateOutputStream(scope);
            }
        }
    };
    // to avoid serialization of the above factory instance, we need to pass it in
    // through a static variable
    StateBackend stateBackend = new BackendForTestStream(new StaticForwardFactory(blockerCheckpointStreamFactory));
    RocksDBStateBackend backend = new RocksDBStateBackend(stateBackend);
    backend.setDbStoragePath(dbDir.getAbsolutePath());
    streamConfig.setStateBackend(backend);
    streamConfig.setStreamOperator(new AsyncCheckpointOperator());
    streamConfig.setOperatorID(new OperatorID());
    TestTaskStateManager taskStateManagerTestMock = new TestTaskStateManager();
    StreamMockEnvironment mockEnv = new StreamMockEnvironment(testHarness.jobConfig, testHarness.taskConfig, testHarness.memorySize, new MockInputSplitProvider(), testHarness.bufferSize, taskStateManagerTestMock);
    blockerCheckpointStreamFactory.setBlockerLatch(new OneShotLatch());
    blockerCheckpointStreamFactory.setWaiterLatch(new OneShotLatch());
    testHarness.invoke(mockEnv);
    testHarness.waitForTaskRunning();
    final OneInputStreamTask<String, String> task = testHarness.getTask();
    task.triggerCheckpointAsync(new CheckpointMetaData(42, 17), CheckpointOptions.forCheckpointWithDefaultLocation()).get();
    testHarness.processElement(new StreamRecord<>("Wohoo", 0));
    blockerCheckpointStreamFactory.getWaiterLatch().await();
    task.cancel();
    blockerCheckpointStreamFactory.getBlockerLatch().trigger();
    testHarness.endInput();
    ExecutorService threadPool = task.getAsyncOperationsThreadPool();
    threadPool.shutdown();
    Assert.assertTrue(threadPool.awaitTermination(60_000, TimeUnit.MILLISECONDS));
    Set<BlockingCheckpointOutputStream> createdStreams = blockerCheckpointStreamFactory.getAllCreatedStreams();
    for (BlockingCheckpointOutputStream stream : createdStreams) {
        Assert.assertTrue("Not all of the " + createdStreams.size() + " created streams have been closed.", stream.isClosed());
    }
    try {
        testHarness.waitForTaskCompletion();
        fail("Operation completed. Cancel failed.");
    } catch (Exception expected) {
        Throwable cause = expected.getCause();
        if (!(cause instanceof CancelTaskException)) {
            fail("Unexpected exception: " + expected);
        }
    }
}
Also used : OneInputStreamTask(org.apache.flink.streaming.runtime.tasks.OneInputStreamTask) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) StateBackend(org.apache.flink.runtime.state.StateBackend) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) AbstractKeyedStateBackend(org.apache.flink.runtime.state.AbstractKeyedStateBackend) MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) BlockerCheckpointStreamFactory(org.apache.flink.runtime.util.BlockerCheckpointStreamFactory) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) StreamMockEnvironment(org.apache.flink.streaming.runtime.tasks.StreamMockEnvironment) MockInputSplitProvider(org.apache.flink.runtime.operators.testutils.MockInputSplitProvider) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) BlockingCheckpointOutputStream(org.apache.flink.runtime.util.BlockingCheckpointOutputStream) CheckpointMetaData(org.apache.flink.runtime.checkpoint.CheckpointMetaData) CheckpointException(org.apache.flink.runtime.checkpoint.CheckpointException) CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) BackendForTestStream(org.apache.flink.runtime.state.testutils.BackendForTestStream) TestTaskStateManager(org.apache.flink.runtime.state.TestTaskStateManager) CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) OneInputStreamTaskTestHarness(org.apache.flink.streaming.runtime.tasks.OneInputStreamTaskTestHarness) ExecutorService(java.util.concurrent.ExecutorService) CheckpointedStateScope(org.apache.flink.runtime.state.CheckpointedStateScope) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 CheckpointedStateScope (org.apache.flink.runtime.state.CheckpointedStateScope)2 Test (org.junit.Test)2 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)1 OneShotLatch (org.apache.flink.core.testutils.OneShotLatch)1 CheckpointException (org.apache.flink.runtime.checkpoint.CheckpointException)1 CheckpointMetaData (org.apache.flink.runtime.checkpoint.CheckpointMetaData)1 CancelTaskException (org.apache.flink.runtime.execution.CancelTaskException)1 OperatorID (org.apache.flink.runtime.jobgraph.OperatorID)1 MockInputSplitProvider (org.apache.flink.runtime.operators.testutils.MockInputSplitProvider)1 AbstractKeyedStateBackend (org.apache.flink.runtime.state.AbstractKeyedStateBackend)1 CheckpointStateOutputStream (org.apache.flink.runtime.state.CheckpointStateOutputStream)1 CheckpointStreamFactory (org.apache.flink.runtime.state.CheckpointStreamFactory)1 StateBackend (org.apache.flink.runtime.state.StateBackend)1