Search in sources :

Example 1 with StateSnapshotContextSynchronousImpl

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

the class AbstractStreamOperatorTest method testSnapshotMethod.

/**
	 * Checks that the state snapshot context is closed after a successful snapshot operation.
	 */
@Test
public void testSnapshotMethod() throws Exception {
    final long checkpointId = 42L;
    final long timestamp = 1L;
    final CloseableRegistry closeableRegistry = new CloseableRegistry();
    StateSnapshotContextSynchronousImpl context = mock(StateSnapshotContextSynchronousImpl.class);
    whenNew(StateSnapshotContextSynchronousImpl.class).withAnyArguments().thenReturn(context);
    StreamTask<Void, AbstractStreamOperator<Void>> containingTask = mock(StreamTask.class);
    when(containingTask.getCancelables()).thenReturn(closeableRegistry);
    AbstractStreamOperator<Void> operator = mock(AbstractStreamOperator.class);
    when(operator.snapshotState(anyLong(), anyLong(), any(CheckpointOptions.class))).thenCallRealMethod();
    doReturn(containingTask).when(operator).getContainingTask();
    operator.snapshotState(checkpointId, timestamp, CheckpointOptions.forFullCheckpoint());
    verify(context).close();
}
Also used : StateSnapshotContextSynchronousImpl(org.apache.flink.runtime.state.StateSnapshotContextSynchronousImpl) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with StateSnapshotContextSynchronousImpl

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

the class AbstractStreamOperatorTest method testFailingBackendSnapshotMethod.

/**
	 * Tests that a failing snapshot method call to the keyed state backend will trigger the closing
	 * of the StateSnapshotContextSynchronousImpl and the cancellation of the
	 * OperatorSnapshotResult. The latter is supposed to also cancel all assigned futures.
	 */
@Test
public void testFailingBackendSnapshotMethod() throws Exception {
    final long checkpointId = 42L;
    final long timestamp = 1L;
    final Exception failingException = new Exception("Test exception");
    final CloseableRegistry closeableRegistry = new CloseableRegistry();
    RunnableFuture<KeyGroupsStateHandle> futureKeyGroupStateHandle = mock(RunnableFuture.class);
    RunnableFuture<OperatorStateHandle> futureOperatorStateHandle = mock(RunnableFuture.class);
    StateSnapshotContextSynchronousImpl context = mock(StateSnapshotContextSynchronousImpl.class);
    when(context.getKeyedStateStreamFuture()).thenReturn(futureKeyGroupStateHandle);
    when(context.getOperatorStateStreamFuture()).thenReturn(futureOperatorStateHandle);
    OperatorSnapshotResult operatorSnapshotResult = spy(new OperatorSnapshotResult());
    whenNew(StateSnapshotContextSynchronousImpl.class).withAnyArguments().thenReturn(context);
    whenNew(OperatorSnapshotResult.class).withAnyArguments().thenReturn(operatorSnapshotResult);
    CheckpointStreamFactory streamFactory = mock(CheckpointStreamFactory.class);
    StreamTask<Void, AbstractStreamOperator<Void>> containingTask = mock(StreamTask.class);
    when(containingTask.getCancelables()).thenReturn(closeableRegistry);
    AbstractStreamOperator<Void> operator = mock(AbstractStreamOperator.class);
    when(operator.snapshotState(anyLong(), anyLong(), any(CheckpointOptions.class))).thenCallRealMethod();
    // The amount of mocking in this test makes it necessary to make the
    // getCheckpointStreamFactory method visible for the test and to
    // overwrite its behaviour.
    when(operator.getCheckpointStreamFactory(any(CheckpointOptions.class))).thenReturn(streamFactory);
    doReturn(containingTask).when(operator).getContainingTask();
    RunnableFuture<OperatorStateHandle> futureManagedOperatorStateHandle = mock(RunnableFuture.class);
    OperatorStateBackend operatorStateBackend = mock(OperatorStateBackend.class);
    when(operatorStateBackend.snapshot(eq(checkpointId), eq(timestamp), eq(streamFactory), any(CheckpointOptions.class))).thenReturn(futureManagedOperatorStateHandle);
    AbstractKeyedStateBackend<?> keyedStateBackend = mock(AbstractKeyedStateBackend.class);
    when(keyedStateBackend.snapshot(eq(checkpointId), eq(timestamp), eq(streamFactory), eq(CheckpointOptions.forFullCheckpoint()))).thenThrow(failingException);
    Whitebox.setInternalState(operator, "operatorStateBackend", operatorStateBackend);
    Whitebox.setInternalState(operator, "keyedStateBackend", keyedStateBackend);
    Whitebox.setInternalState(operator, "checkpointStreamFactory", streamFactory);
    try {
        operator.snapshotState(checkpointId, timestamp, CheckpointOptions.forFullCheckpoint());
        fail("Exception expected.");
    } catch (Exception e) {
        assertEquals(failingException, e.getCause());
    }
    // verify that the context has been closed, the operator snapshot result has been cancelled
    // and that all futures have been cancelled.
    verify(context).close();
    verify(operatorSnapshotResult).cancel();
    verify(futureKeyGroupStateHandle).cancel(anyBoolean());
    verify(futureOperatorStateHandle).cancel(anyBoolean());
    verify(futureKeyGroupStateHandle).cancel(anyBoolean());
}
Also used : CheckpointStreamFactory(org.apache.flink.runtime.state.CheckpointStreamFactory) StateSnapshotContextSynchronousImpl(org.apache.flink.runtime.state.StateSnapshotContextSynchronousImpl) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) IOException(java.io.IOException) KeyGroupsStateHandle(org.apache.flink.runtime.state.KeyGroupsStateHandle) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) OperatorStateBackend(org.apache.flink.runtime.state.OperatorStateBackend) OperatorStateHandle(org.apache.flink.runtime.state.OperatorStateHandle) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with StateSnapshotContextSynchronousImpl

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

the class StateSnapshotContextSynchronousImplTest method setUp.

@Before
public void setUp() throws Exception {
    CloseableRegistry closableRegistry = new CloseableRegistry();
    CheckpointStreamFactory streamFactory = new MemCheckpointStreamFactory(1024);
    KeyGroupRange keyGroupRange = new KeyGroupRange(0, 2);
    this.snapshotContext = new StateSnapshotContextSynchronousImpl(42, 4711, streamFactory, keyGroupRange, closableRegistry);
}
Also used : MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) CheckpointStreamFactory(org.apache.flink.runtime.state.CheckpointStreamFactory) StateSnapshotContextSynchronousImpl(org.apache.flink.runtime.state.StateSnapshotContextSynchronousImpl) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) Before(org.junit.Before)

Example 4 with StateSnapshotContextSynchronousImpl

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

the class StateSnapshotContextSynchronousImplTest method testStreamClosingWhenClosing.

/**
	 * Tests that closing the StateSnapshotContextSynchronousImpl will also close the associated
	 * output streams.
	 */
@Test
public void testStreamClosingWhenClosing() throws Exception {
    long checkpointId = 42L;
    long checkpointTimestamp = 1L;
    CheckpointStreamFactory.CheckpointStateOutputStream outputStream1 = mock(CheckpointStreamFactory.CheckpointStateOutputStream.class);
    CheckpointStreamFactory.CheckpointStateOutputStream outputStream2 = mock(CheckpointStreamFactory.CheckpointStateOutputStream.class);
    CheckpointStreamFactory streamFactory = mock(CheckpointStreamFactory.class);
    when(streamFactory.createCheckpointStateOutputStream(eq(checkpointId), eq(checkpointTimestamp))).thenReturn(outputStream1, outputStream2);
    InsightCloseableRegistry closableRegistry = new InsightCloseableRegistry();
    KeyGroupRange keyGroupRange = new KeyGroupRange(0, 2);
    StateSnapshotContextSynchronousImpl context = new StateSnapshotContextSynchronousImpl(checkpointId, checkpointTimestamp, streamFactory, keyGroupRange, closableRegistry);
    // creating the output streams
    context.getRawKeyedOperatorStateOutput();
    context.getRawOperatorStateOutput();
    verify(streamFactory, times(2)).createCheckpointStateOutputStream(eq(checkpointId), eq(checkpointTimestamp));
    assertEquals(2, closableRegistry.size());
    assertTrue(closableRegistry.contains(outputStream1));
    assertTrue(closableRegistry.contains(outputStream2));
    context.close();
    verify(outputStream1).close();
    verify(outputStream2).close();
    assertEquals(0, closableRegistry.size());
}
Also used : MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) CheckpointStreamFactory(org.apache.flink.runtime.state.CheckpointStreamFactory) StateSnapshotContextSynchronousImpl(org.apache.flink.runtime.state.StateSnapshotContextSynchronousImpl) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) Test(org.junit.Test)

Example 5 with StateSnapshotContextSynchronousImpl

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

the class FlinkKafkaConsumerBaseTest method ignoreCheckpointWhenNotRunning.

/**
	 * Tests that no checkpoints happen when the fetcher is not running.
	 */
@Test
public void ignoreCheckpointWhenNotRunning() throws Exception {
    @SuppressWarnings("unchecked") final AbstractFetcher<String, ?> fetcher = mock(AbstractFetcher.class);
    FlinkKafkaConsumerBase<String> consumer = getConsumer(fetcher, new LinkedMap(), false);
    OperatorStateStore operatorStateStore = mock(OperatorStateStore.class);
    TestingListState<Tuple2<KafkaTopicPartition, Long>> listState = new TestingListState<>();
    when(operatorStateStore.getOperatorState(Matchers.any(ListStateDescriptor.class))).thenReturn(listState);
    consumer.snapshotState(new StateSnapshotContextSynchronousImpl(1, 1));
    assertFalse(listState.get().iterator().hasNext());
    consumer.notifyCheckpointComplete(66L);
}
Also used : OperatorStateStore(org.apache.flink.api.common.state.OperatorStateStore) Tuple2(org.apache.flink.api.java.tuple.Tuple2) StateSnapshotContextSynchronousImpl(org.apache.flink.runtime.state.StateSnapshotContextSynchronousImpl) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) LinkedMap(org.apache.commons.collections.map.LinkedMap) Test(org.junit.Test)

Aggregations

StateSnapshotContextSynchronousImpl (org.apache.flink.runtime.state.StateSnapshotContextSynchronousImpl)11 Test (org.junit.Test)9 LinkedMap (org.apache.commons.collections.map.LinkedMap)5 OperatorStateStore (org.apache.flink.api.common.state.OperatorStateStore)5 Serializable (java.io.Serializable)4 Configuration (org.apache.flink.configuration.Configuration)4 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)4 CheckpointStreamFactory (org.apache.flink.runtime.state.CheckpointStreamFactory)4 StateInitializationContext (org.apache.flink.runtime.state.StateInitializationContext)4 IOException (java.io.IOException)3 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)3 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)3 CheckpointOptions (org.apache.flink.runtime.checkpoint.CheckpointOptions)3 KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)3 KafkaTopicPartition (org.apache.flink.streaming.connectors.kafka.internals.KafkaTopicPartition)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 HashMap (java.util.HashMap)2 MemCheckpointStreamFactory (org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory)2 StreamingRuntimeContext (org.apache.flink.streaming.api.operators.StreamingRuntimeContext)2 ConcurrentModificationException (java.util.ConcurrentModificationException)1