use of org.apache.flink.runtime.checkpoint.OperatorState in project flink by apache.
the class ListStateInputFormatTest method testReadListOperatorState.
@Test
public void testReadListOperatorState() throws Exception {
try (OneInputStreamOperatorTestHarness<Integer, Void> testHarness = getTestHarness()) {
testHarness.open();
testHarness.processElement(1, 0);
testHarness.processElement(2, 0);
testHarness.processElement(3, 0);
OperatorSubtaskState subtaskState = testHarness.snapshot(0, 0);
OperatorState state = new OperatorState(OperatorIDGenerator.fromUid("uid"), 1, 4);
state.putState(0, subtaskState);
OperatorStateInputSplit split = new OperatorStateInputSplit(subtaskState.getManagedOperatorState(), 0);
ListStateInputFormat<Integer> format = new ListStateInputFormat<>(state, new Configuration(), null, descriptor);
format.setRuntimeContext(new MockStreamingRuntimeContext(false, 1, 0));
format.open(split);
List<Integer> results = new ArrayList<>();
while (!format.reachedEnd()) {
results.add(format.nextRecord(0));
}
results.sort(Comparator.naturalOrder());
Assert.assertEquals("Failed to read correct list state from state backend", Arrays.asList(1, 2, 3), results);
}
}
use of org.apache.flink.runtime.checkpoint.OperatorState in project flink by apache.
the class KeyedStateInputFormatTest method testInvalidProcessReaderFunctionFails.
@Test(expected = IOException.class)
public void testInvalidProcessReaderFunctionFails() throws Exception {
OperatorID operatorID = OperatorIDGenerator.fromUid("uid");
OperatorSubtaskState state = createOperatorSubtaskState(new StreamFlatMap<>(new StatefulFunction()));
OperatorState operatorState = new OperatorState(operatorID, 1, 128);
operatorState.putState(0, state);
KeyedStateInputFormat<?, ?, ?> format = new KeyedStateInputFormat<>(operatorState, new MemoryStateBackend(), new Configuration(), new KeyedStateReaderOperator<>(new ReaderFunction(), Types.INT));
KeyGroupRangeInputSplit split = format.createInputSplits(1)[0];
KeyedStateReaderFunction<Integer, Integer> userFunction = new InvalidReaderFunction();
readInputSplit(split, userFunction);
Assert.fail("KeyedStateReaderFunction did not fail on invalid RuntimeContext use");
}
use of org.apache.flink.runtime.checkpoint.OperatorState in project flink by apache.
the class KeyedStateInputFormatTest method readInputSplit.
@Nonnull
private List<Integer> readInputSplit(KeyGroupRangeInputSplit split, KeyedStateReaderFunction<Integer, Integer> userFunction) throws IOException {
KeyedStateInputFormat<Integer, VoidNamespace, Integer> format = new KeyedStateInputFormat<>(new OperatorState(OperatorIDGenerator.fromUid("uid"), 1, 4), new MemoryStateBackend(), new Configuration(), new KeyedStateReaderOperator<>(userFunction, Types.INT));
List<Integer> data = new ArrayList<>();
format.setRuntimeContext(new MockStreamingRuntimeContext(false, 1, 0));
format.openInputFormat();
format.open(split);
while (!format.reachedEnd()) {
data.add(format.nextRecord(0));
}
format.close();
format.closeInputFormat();
data.sort(Comparator.comparingInt(id -> id));
return data;
}
use of org.apache.flink.runtime.checkpoint.OperatorState in project flink by apache.
the class UnionStateInputFormatTest method testReadUnionOperatorState.
@Test
public void testReadUnionOperatorState() throws Exception {
try (OneInputStreamOperatorTestHarness<Integer, Void> testHarness = getTestHarness()) {
testHarness.open();
testHarness.processElement(1, 0);
testHarness.processElement(2, 0);
testHarness.processElement(3, 0);
OperatorSubtaskState subtaskState = testHarness.snapshot(0, 0);
OperatorState state = new OperatorState(OperatorIDGenerator.fromUid("uid"), 1, 4);
state.putState(0, subtaskState);
OperatorStateInputSplit split = new OperatorStateInputSplit(subtaskState.getManagedOperatorState(), 0);
UnionStateInputFormat<Integer> format = new UnionStateInputFormat<>(state, new Configuration(), null, descriptor);
format.setRuntimeContext(new MockStreamingRuntimeContext(false, 1, 0));
format.open(split);
List<Integer> results = new ArrayList<>();
while (!format.reachedEnd()) {
results.add(format.nextRecord(0));
}
results.sort(Comparator.naturalOrder());
Assert.assertEquals("Failed to read correct list state from state backend", Arrays.asList(1, 2, 3), results);
}
}
use of org.apache.flink.runtime.checkpoint.OperatorState in project flink by apache.
the class WindowReaderTest method testPerPaneAndPerKeyState.
@Test
public void testPerPaneAndPerKeyState() throws Exception {
WindowOperator<Integer, Integer, ?, Void, ?> operator = getWindowOperator(stream -> stream.window(TumblingEventTimeWindows.of(Time.milliseconds(1))).trigger(new AlwaysFireTrigger<>()).process(new MultiFireWindow(), Types.INT));
OperatorState operatorState = getOperatorState(operator);
KeyedStateInputFormat<Integer, TimeWindow, Tuple2<Integer, Integer>> format = new KeyedStateInputFormat<>(operatorState, new MemoryStateBackend(), new Configuration(), WindowReaderOperator.process(new MultiFireReaderFunction(), Types.INT, new TimeWindow.Serializer(), Types.INT));
List<Tuple2<Integer, Integer>> list = readState(format);
Assert.assertEquals(Arrays.asList(Tuple2.of(2, 1), Tuple2.of(2, 1)), list);
}
Aggregations