Search in sources :

Example 36 with CheckpointMetaData

use of org.apache.flink.runtime.checkpoint.CheckpointMetaData in project flink by apache.

the class StreamTaskTest method testSyncSavepointWithEndInput.

/**
 * Test for SyncSavepoint and EndInput interactions. Targets following scenarios scenarios:
 *
 * <ol>
 *   <li>Thread1: notify sync savepoint
 *   <li>Thread2: endInput
 *   <li>Thread1: confirm/abort/abortAsync
 *   <li>assert inputEnded: confirmed - no, abort/abortAsync - yes
 * </ol>
 */
private void testSyncSavepointWithEndInput(BiConsumerWithException<StreamTask<?, ?>, Long, IOException> savepointResult, SnapshotType checkpointType, boolean expectEndInput) throws Exception {
    StreamTaskMailboxTestHarness<String> harness = new StreamTaskMailboxTestHarnessBuilder<>(OneInputStreamTask::new, STRING_TYPE_INFO).addInput(STRING_TYPE_INFO).setupOutputForSingletonOperatorChain(new TestBoundedOneInputStreamOperator()).build();
    final long checkpointId = 1L;
    CountDownLatch savepointTriggeredLatch = new CountDownLatch(1);
    CountDownLatch inputEndedLatch = new CountDownLatch(1);
    MailboxExecutor executor = harness.streamTask.getMailboxExecutorFactory().createExecutor(MAX_PRIORITY);
    executor.execute(() -> {
        try {
            harness.streamTask.triggerCheckpointOnBarrier(new CheckpointMetaData(checkpointId, checkpointId), new CheckpointOptions(checkpointType, getDefault()), new CheckpointMetricsBuilder());
        } catch (IOException e) {
            fail(e.getMessage());
        }
    }, "triggerCheckpointOnBarrier");
    new Thread(() -> {
        try {
            savepointTriggeredLatch.await();
            harness.endInput(expectEndInput);
            inputEndedLatch.countDown();
        } catch (InterruptedException e) {
            fail(e.getMessage());
        }
    }).start();
    // this mails should be executed from the one above (from triggerCheckpointOnBarrier)
    executor.execute(savepointTriggeredLatch::countDown, "savepointTriggeredLatch");
    executor.execute(() -> {
        inputEndedLatch.await();
        savepointResult.accept(harness.streamTask, checkpointId);
    }, "savepointResult");
    harness.processAll();
    Assert.assertEquals(expectEndInput, TestBoundedOneInputStreamOperator.isInputEnded());
}
Also used : CheckpointMetricsBuilder(org.apache.flink.runtime.checkpoint.CheckpointMetricsBuilder) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) CheckpointMetaData(org.apache.flink.runtime.checkpoint.CheckpointMetaData) MailboxExecutor(org.apache.flink.api.common.operators.MailboxExecutor) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions)

Example 37 with CheckpointMetaData

use of org.apache.flink.runtime.checkpoint.CheckpointMetaData in project flink by apache.

the class StreamTaskTest method testEmptySubtaskStateLeadsToStatelessAcknowledgment.

/**
 * FLINK-5985
 *
 * <p>This test ensures that empty snapshots (no op/keyed stated whatsoever) will be reported as
 * stateless tasks. This happens by translating an empty {@link SubtaskState} into reporting
 * 'null' to #acknowledgeCheckpoint.
 */
@Test
public void testEmptySubtaskStateLeadsToStatelessAcknowledgment() throws Exception {
    // latch blocks until the async checkpoint thread acknowledges
    final OneShotLatch checkpointCompletedLatch = new OneShotLatch();
    final List<SubtaskState> checkpointResult = new ArrayList<>(1);
    CheckpointResponder checkpointResponder = mock(CheckpointResponder.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            SubtaskState subtaskState = invocation.getArgument(4);
            checkpointResult.add(subtaskState);
            checkpointCompletedLatch.trigger();
            return null;
        }
    }).when(checkpointResponder).acknowledgeCheckpoint(any(JobID.class), any(ExecutionAttemptID.class), anyLong(), any(CheckpointMetrics.class), nullable(TaskStateSnapshot.class));
    TaskStateManager taskStateManager = new TaskStateManagerImpl(new JobID(1L, 2L), new ExecutionAttemptID(), mock(TaskLocalStateStoreImpl.class), new InMemoryStateChangelogStorage(), null, checkpointResponder);
    // mock the operator with empty snapshot result (all state handles are null)
    OneInputStreamOperator<String, String> statelessOperator = streamOperatorWithSnapshot(new OperatorSnapshotFutures());
    try (MockEnvironment mockEnvironment = new MockEnvironmentBuilder().setTaskStateManager(taskStateManager).build()) {
        RunningTask<MockStreamTask> task = runTask(() -> createMockStreamTask(mockEnvironment, operatorChain(statelessOperator)));
        waitTaskIsRunning(task.streamTask, task.invocationFuture);
        task.streamTask.triggerCheckpointAsync(new CheckpointMetaData(42L, 1L), CheckpointOptions.forCheckpointWithDefaultLocation());
        checkpointCompletedLatch.await(30, TimeUnit.SECONDS);
        // ensure that 'null' was acknowledged as subtask state
        Assert.assertNull(checkpointResult.get(0));
        task.streamTask.cancel();
        task.waitForTaskCompletion(true);
    }
}
Also used : OperatorSnapshotFutures(org.apache.flink.streaming.api.operators.OperatorSnapshotFutures) TaskStateManagerImpl(org.apache.flink.runtime.state.TaskStateManagerImpl) MockEnvironmentBuilder(org.apache.flink.runtime.operators.testutils.MockEnvironmentBuilder) ArrayList(java.util.ArrayList) CheckpointMetrics(org.apache.flink.runtime.checkpoint.CheckpointMetrics) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TaskStateSnapshot(org.apache.flink.runtime.checkpoint.TaskStateSnapshot) InMemoryStateChangelogStorage(org.apache.flink.runtime.state.changelog.inmemory.InMemoryStateChangelogStorage) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) TaskLocalStateStoreImpl(org.apache.flink.runtime.state.TaskLocalStateStoreImpl) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) ExecutionAttemptID(org.apache.flink.runtime.executiongraph.ExecutionAttemptID) CheckpointResponder(org.apache.flink.runtime.taskmanager.CheckpointResponder) TaskStateManager(org.apache.flink.runtime.state.TaskStateManager) CheckpointMetaData(org.apache.flink.runtime.checkpoint.CheckpointMetaData) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) SubtaskState(org.apache.flink.runtime.checkpoint.SubtaskState) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 38 with CheckpointMetaData

use of org.apache.flink.runtime.checkpoint.CheckpointMetaData in project flink by apache.

the class StreamTaskTest method testCheckpointFailueOnClosedOperator.

/**
 * Tests exeptions is thrown by triggering checkpoint if operators are closed. This was
 * initially implemented for FLINK-16383. However after FLINK-2491 operators lifecycle has
 * changed and now we: (1) redefined close() to dispose(). After closing operators, there should
 * be no opportunity to invoke anything on the task. close() mentioned in FLINK-16383 is now
 * more like finish(). (2) We support triggering and performing checkpoints if operators are
 * finished.
 */
@Test
public void testCheckpointFailueOnClosedOperator() throws Throwable {
    ClosingOperator<Integer> operator = new ClosingOperator<>();
    StreamTaskMailboxTestHarnessBuilder<Integer> builder = new StreamTaskMailboxTestHarnessBuilder<>(OneInputStreamTask::new, BasicTypeInfo.INT_TYPE_INFO).addInput(BasicTypeInfo.INT_TYPE_INFO);
    try (StreamTaskMailboxTestHarness<Integer> harness = builder.setupOutputForSingletonOperatorChain(operator).build()) {
        // keeps the mailbox from suspending
        harness.setAutoProcess(false);
        harness.processElement(new StreamRecord<>(1));
        harness.streamTask.operatorChain.finishOperators(harness.streamTask.getActionExecutor(), StopMode.DRAIN);
        harness.streamTask.operatorChain.closeAllOperators();
        assertTrue(ClosingOperator.closed.get());
        harness.streamTask.triggerCheckpointOnBarrier(new CheckpointMetaData(1, 0), CheckpointOptions.forCheckpointWithDefaultLocation(), new CheckpointMetricsBuilder());
    } catch (Exception ex) {
        ExceptionUtils.assertThrowableWithMessage(ex, "OperatorChain and Task should never be closed at this point");
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CheckpointMetricsBuilder(org.apache.flink.runtime.checkpoint.CheckpointMetricsBuilder) CheckpointMetaData(org.apache.flink.runtime.checkpoint.CheckpointMetaData) CheckpointException(org.apache.flink.runtime.checkpoint.CheckpointException) FunctionWithException(org.apache.flink.util.function.FunctionWithException) AsynchronousException(org.apache.flink.runtime.taskmanager.AsynchronousException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) RunnableWithException(org.apache.flink.util.function.RunnableWithException) TimeoutException(java.util.concurrent.TimeoutException) ExpectedTestException(org.apache.flink.runtime.operators.testutils.ExpectedTestException) SupplierWithException(org.apache.flink.util.function.SupplierWithException) BiConsumerWithException(org.apache.flink.util.function.BiConsumerWithException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Example 39 with CheckpointMetaData

use of org.apache.flink.runtime.checkpoint.CheckpointMetaData in project flink by apache.

the class SubtaskCheckpointCoordinatorTest method testBroadcastCancelCheckpointMarkerOnAbortingFromCoordinator.

@Test
public void testBroadcastCancelCheckpointMarkerOnAbortingFromCoordinator() throws Exception {
    OneInputStreamTaskTestHarness<String, String> testHarness = new OneInputStreamTaskTestHarness<>(OneInputStreamTask::new, 1, 1, BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO);
    testHarness.setupOutputForSingletonOperatorChain();
    StreamConfig streamConfig = testHarness.getStreamConfig();
    streamConfig.setStreamOperator(new MapOperator());
    testHarness.invoke();
    testHarness.waitForTaskRunning();
    MockEnvironment mockEnvironment = MockEnvironment.builder().build();
    try (SubtaskCheckpointCoordinator subtaskCheckpointCoordinator = new MockSubtaskCheckpointCoordinatorBuilder().setEnvironment(mockEnvironment).build()) {
        ArrayList<Object> recordOrEvents = new ArrayList<>();
        StreamElementSerializer<String> stringStreamElementSerializer = new StreamElementSerializer<>(StringSerializer.INSTANCE);
        ResultPartitionWriter resultPartitionWriter = new RecordOrEventCollectingResultPartitionWriter<>(recordOrEvents, stringStreamElementSerializer);
        mockEnvironment.addOutputs(Collections.singletonList(resultPartitionWriter));
        OneInputStreamTask<String, String> task = testHarness.getTask();
        OperatorChain<String, OneInputStreamOperator<String, String>> operatorChain = new RegularOperatorChain<>(task, StreamTask.createRecordWriterDelegate(streamConfig, mockEnvironment));
        long checkpointId = 42L;
        // notify checkpoint aborted before execution.
        subtaskCheckpointCoordinator.notifyCheckpointAborted(checkpointId, operatorChain, () -> true);
        subtaskCheckpointCoordinator.checkpointState(new CheckpointMetaData(checkpointId, System.currentTimeMillis()), CheckpointOptions.forCheckpointWithDefaultLocation(), new CheckpointMetricsBuilder(), operatorChain, false, () -> false);
        assertEquals(1, recordOrEvents.size());
        Object recordOrEvent = recordOrEvents.get(0);
        // ensure CancelCheckpointMarker is broadcast downstream.
        assertTrue(recordOrEvent instanceof CancelCheckpointMarker);
        assertEquals(checkpointId, ((CancelCheckpointMarker) recordOrEvent).getCheckpointId());
        testHarness.endInput();
        testHarness.waitForTaskCompletion();
    }
}
Also used : CheckpointMetricsBuilder(org.apache.flink.runtime.checkpoint.CheckpointMetricsBuilder) RecordOrEventCollectingResultPartitionWriter(org.apache.flink.runtime.io.network.api.writer.RecordOrEventCollectingResultPartitionWriter) ResultPartitionWriter(org.apache.flink.runtime.io.network.api.writer.ResultPartitionWriter) ArrayList(java.util.ArrayList) CancelCheckpointMarker(org.apache.flink.runtime.io.network.api.CancelCheckpointMarker) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) CheckpointMetaData(org.apache.flink.runtime.checkpoint.CheckpointMetaData) OneInputStreamOperator(org.apache.flink.streaming.api.operators.OneInputStreamOperator) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) StreamElementSerializer(org.apache.flink.streaming.runtime.streamrecord.StreamElementSerializer) RecordOrEventCollectingResultPartitionWriter(org.apache.flink.runtime.io.network.api.writer.RecordOrEventCollectingResultPartitionWriter) Test(org.junit.Test)

Example 40 with CheckpointMetaData

use of org.apache.flink.runtime.checkpoint.CheckpointMetaData in project flink by apache.

the class SubtaskCheckpointCoordinatorTest method testNotifyCheckpointSubsumed.

@Test
public void testNotifyCheckpointSubsumed() throws Exception {
    TestTaskStateManager stateManager = new TestTaskStateManager();
    MockEnvironment mockEnvironment = MockEnvironment.builder().setTaskStateManager(stateManager).build();
    try (SubtaskCheckpointCoordinatorImpl subtaskCheckpointCoordinator = (SubtaskCheckpointCoordinatorImpl) new MockSubtaskCheckpointCoordinatorBuilder().setEnvironment(mockEnvironment).setUnalignedCheckpointEnabled(true).build()) {
        StreamMap<String, String> streamMap = new StreamMap<>((MapFunction<String, String>) value -> value);
        streamMap.setProcessingTimeService(new TestProcessingTimeService());
        final OperatorChain<String, AbstractStreamOperator<String>> operatorChain = operatorChain(streamMap);
        StreamTaskStateInitializerImpl stateInitializer = new StreamTaskStateInitializerImpl(mockEnvironment, new TestStateBackend());
        operatorChain.initializeStateAndOpenOperators(stateInitializer);
        long checkpointId = 42L;
        subtaskCheckpointCoordinator.getChannelStateWriter().start(checkpointId, CheckpointOptions.forCheckpointWithDefaultLocation());
        subtaskCheckpointCoordinator.checkpointState(new CheckpointMetaData(checkpointId, System.currentTimeMillis()), CheckpointOptions.forCheckpointWithDefaultLocation(), new CheckpointMetricsBuilder(), operatorChain, false, () -> false);
        long notifySubsumeCheckpointId = checkpointId + 1L;
        // notify checkpoint aborted before execution.
        subtaskCheckpointCoordinator.notifyCheckpointSubsumed(notifySubsumeCheckpointId, operatorChain, () -> true);
        assertEquals(notifySubsumeCheckpointId, ((TestStateBackend.TestKeyedStateBackend<?>) streamMap.getKeyedStateBackend()).getSubsumeCheckpointId());
    }
}
Also used : StreamTaskStateInitializerImpl(org.apache.flink.streaming.api.operators.StreamTaskStateInitializerImpl) CheckpointMetricsBuilder(org.apache.flink.runtime.checkpoint.CheckpointMetricsBuilder) BufferBuilderTestUtils(org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils) SavepointType(org.apache.flink.runtime.checkpoint.SavepointType) ExceptionUtils(org.apache.flink.util.ExceptionUtils) StringSerializer(org.apache.flink.api.common.typeutils.base.StringSerializer) MoreExecutors.newDirectExecutorService(org.apache.flink.shaded.guava30.com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService) StreamElementSerializer(org.apache.flink.streaming.runtime.streamrecord.StreamElementSerializer) MapFunction(org.apache.flink.api.common.functions.MapFunction) BasicTypeInfo(org.apache.flink.api.common.typeinfo.BasicTypeInfo) TestTaskStateManager(org.apache.flink.runtime.state.TestTaskStateManager) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) CheckpointException(org.apache.flink.runtime.checkpoint.CheckpointException) Assert.fail(org.junit.Assert.fail) WatermarkStatus(org.apache.flink.streaming.runtime.watermarkstatus.WatermarkStatus) CheckpointType(org.apache.flink.runtime.checkpoint.CheckpointType) AbstractEvent(org.apache.flink.runtime.event.AbstractEvent) KeyedStateHandle(org.apache.flink.runtime.state.KeyedStateHandle) MockStreamTaskBuilder(org.apache.flink.streaming.util.MockStreamTaskBuilder) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) SnapshotType(org.apache.flink.runtime.checkpoint.SnapshotType) TestCheckpointStorageWorkerView(org.apache.flink.runtime.state.TestCheckpointStorageWorkerView) Assert.assertFalse(org.junit.Assert.assertFalse) NonRecordWriter(org.apache.flink.runtime.io.network.api.writer.NonRecordWriter) CancelCheckpointMarker(org.apache.flink.runtime.io.network.api.CancelCheckpointMarker) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) DummyEnvironment(org.apache.flink.runtime.operators.testutils.DummyEnvironment) OneInputStreamOperator(org.apache.flink.streaming.api.operators.OneInputStreamOperator) RecordOrEventCollectingResultPartitionWriter(org.apache.flink.runtime.io.network.api.writer.RecordOrEventCollectingResultPartitionWriter) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) CHECKPOINT(org.apache.flink.runtime.checkpoint.CheckpointType.CHECKPOINT) SavepointFormatType(org.apache.flink.core.execution.SavepointFormatType) CheckpointMetaData(org.apache.flink.runtime.checkpoint.CheckpointMetaData) Watermark(org.apache.flink.streaming.api.watermark.Watermark) ResultPartitionWriter(org.apache.flink.runtime.io.network.api.writer.ResultPartitionWriter) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) OperatorSnapshotFutures(org.apache.flink.streaming.api.operators.OperatorSnapshotFutures) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) StreamTaskStateInitializer(org.apache.flink.streaming.api.operators.StreamTaskStateInitializer) OperatorMetricGroup(org.apache.flink.metrics.groups.OperatorMetricGroup) NoOpStreamTask(org.apache.flink.streaming.runtime.tasks.StreamTaskTest.NoOpStreamTask) ChannelStateWriter(org.apache.flink.runtime.checkpoint.channel.ChannelStateWriter) StreamMap(org.apache.flink.streaming.api.operators.StreamMap) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) CheckpointStorageLocationReference(org.apache.flink.runtime.state.CheckpointStorageLocationReference) RunnableFuture(java.util.concurrent.RunnableFuture) SnapshotResult(org.apache.flink.runtime.state.SnapshotResult) ChannelStateWriterImpl(org.apache.flink.runtime.checkpoint.channel.ChannelStateWriterImpl) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) AbstractStreamOperator(org.apache.flink.streaming.api.operators.AbstractStreamOperator) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) DoneFuture(org.apache.flink.runtime.state.DoneFuture) LatencyMarker(org.apache.flink.streaming.runtime.streamrecord.LatencyMarker) CheckpointStreamFactory(org.apache.flink.runtime.state.CheckpointStreamFactory) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ResultSubpartitionInfo(org.apache.flink.runtime.checkpoint.channel.ResultSubpartitionInfo) CheckpointMetricsBuilder(org.apache.flink.runtime.checkpoint.CheckpointMetricsBuilder) CheckpointMetaData(org.apache.flink.runtime.checkpoint.CheckpointMetaData) AbstractStreamOperator(org.apache.flink.streaming.api.operators.AbstractStreamOperator) TestTaskStateManager(org.apache.flink.runtime.state.TestTaskStateManager) StreamTaskStateInitializerImpl(org.apache.flink.streaming.api.operators.StreamTaskStateInitializerImpl) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) StreamMap(org.apache.flink.streaming.api.operators.StreamMap) Test(org.junit.Test)

Aggregations

CheckpointMetaData (org.apache.flink.runtime.checkpoint.CheckpointMetaData)47 Test (org.junit.Test)33 CheckpointMetricsBuilder (org.apache.flink.runtime.checkpoint.CheckpointMetricsBuilder)16 CheckpointOptions (org.apache.flink.runtime.checkpoint.CheckpointOptions)15 TaskStateSnapshot (org.apache.flink.runtime.checkpoint.TaskStateSnapshot)13 IOException (java.io.IOException)12 CheckpointMetrics (org.apache.flink.runtime.checkpoint.CheckpointMetrics)12 MockEnvironment (org.apache.flink.runtime.operators.testutils.MockEnvironment)11 StreamConfig (org.apache.flink.streaming.api.graph.StreamConfig)11 OperatorSnapshotFutures (org.apache.flink.streaming.api.operators.OperatorSnapshotFutures)11 JobID (org.apache.flink.api.common.JobID)10 CheckpointException (org.apache.flink.runtime.checkpoint.CheckpointException)10 ExecutionAttemptID (org.apache.flink.runtime.executiongraph.ExecutionAttemptID)10 ExecutionException (java.util.concurrent.ExecutionException)9 OperatorID (org.apache.flink.runtime.jobgraph.OperatorID)9 CancelTaskException (org.apache.flink.runtime.execution.CancelTaskException)8 TestTaskStateManager (org.apache.flink.runtime.state.TestTaskStateManager)8 CheckpointResponder (org.apache.flink.runtime.taskmanager.CheckpointResponder)7 FlinkRuntimeException (org.apache.flink.util.FlinkRuntimeException)7 CompletableFuture (java.util.concurrent.CompletableFuture)6