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