Search in sources :

Example 1 with KeyGroupRangeInputSplit

use of org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit in project flink by apache.

the class KeyedStateInputFormatTest method testReadTime.

@Test
public void testReadTime() throws Exception {
    OperatorID operatorID = OperatorIDGenerator.fromUid("uid");
    OperatorSubtaskState state = createOperatorSubtaskState(new KeyedProcessOperator<>(new StatefulFunctionWithTime()));
    OperatorState operatorState = new OperatorState(operatorID, 1, 128);
    operatorState.putState(0, state);
    KeyedStateInputFormat<?, ?, ?> format = new KeyedStateInputFormat<>(operatorState, new MemoryStateBackend(), new Configuration(), new KeyedStateReaderOperator<>(new TimerReaderFunction(), Types.INT));
    KeyGroupRangeInputSplit split = format.createInputSplits(1)[0];
    KeyedStateReaderFunction<Integer, Integer> userFunction = new TimerReaderFunction();
    List<Integer> data = readInputSplit(split, userFunction);
    Assert.assertEquals("Incorrect data read from input split", Arrays.asList(1, 1, 2, 2, 3, 3), data);
}
Also used : KeyGroupRangeInputSplit(org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit) Configuration(org.apache.flink.configuration.Configuration) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) OperatorState(org.apache.flink.runtime.checkpoint.OperatorState) Test(org.junit.Test)

Example 2 with KeyGroupRangeInputSplit

use of org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit in project flink by apache.

the class KeyedStateInputFormatTest method testReadState.

@Test
public void testReadState() 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 ReaderFunction();
    List<Integer> data = readInputSplit(split, userFunction);
    Assert.assertEquals("Incorrect data read from input split", Arrays.asList(1, 2, 3), data);
}
Also used : KeyGroupRangeInputSplit(org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit) Configuration(org.apache.flink.configuration.Configuration) KeyedStateReaderFunction(org.apache.flink.state.api.functions.KeyedStateReaderFunction) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) OperatorState(org.apache.flink.runtime.checkpoint.OperatorState) Test(org.junit.Test)

Example 3 with KeyGroupRangeInputSplit

use of org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit 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");
}
Also used : KeyGroupRangeInputSplit(org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit) Configuration(org.apache.flink.configuration.Configuration) KeyedStateReaderFunction(org.apache.flink.state.api.functions.KeyedStateReaderFunction) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) OperatorState(org.apache.flink.runtime.checkpoint.OperatorState) Test(org.junit.Test)

Example 4 with KeyGroupRangeInputSplit

use of org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit 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;
}
Also used : RichFlatMapFunction(org.apache.flink.api.common.functions.RichFlatMapFunction) VoidNamespace(org.apache.flink.runtime.state.VoidNamespace) Arrays(java.util.Arrays) OperatorState(org.apache.flink.runtime.checkpoint.OperatorState) KeyedOneInputStreamOperatorTestHarness(org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness) KeyedStateReaderOperator(org.apache.flink.state.api.input.operator.KeyedStateReaderOperator) KeyedProcessFunction(org.apache.flink.streaming.api.functions.KeyedProcessFunction) KeyedStateReaderFunction(org.apache.flink.state.api.functions.KeyedStateReaderFunction) ArrayList(java.util.ArrayList) MockStreamingRuntimeContext(org.apache.flink.streaming.util.MockStreamingRuntimeContext) KeyGroupRangeInputSplit(org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit) KeyedProcessOperator(org.apache.flink.streaming.api.operators.KeyedProcessOperator) Collector(org.apache.flink.util.Collector) Nonnull(javax.annotation.Nonnull) Types(org.apache.flink.api.common.typeinfo.Types) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) StreamFlatMap(org.apache.flink.streaming.api.operators.StreamFlatMap) Configuration(org.apache.flink.configuration.Configuration) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) List(java.util.List) ValueState(org.apache.flink.api.common.state.ValueState) VoidSerializer(org.apache.flink.api.common.typeutils.base.VoidSerializer) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) OneInputStreamOperator(org.apache.flink.streaming.api.operators.OneInputStreamOperator) Assert(org.junit.Assert) Comparator(java.util.Comparator) OperatorIDGenerator(org.apache.flink.state.api.runtime.OperatorIDGenerator) MockStreamingRuntimeContext(org.apache.flink.streaming.util.MockStreamingRuntimeContext) Configuration(org.apache.flink.configuration.Configuration) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) ArrayList(java.util.ArrayList) VoidNamespace(org.apache.flink.runtime.state.VoidNamespace) OperatorState(org.apache.flink.runtime.checkpoint.OperatorState) Nonnull(javax.annotation.Nonnull)

Example 5 with KeyGroupRangeInputSplit

use of org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit in project flink by apache.

the class KeyedStateInputFormatTest method testMaxParallelismRespected.

@Test
public void testMaxParallelismRespected() 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[] splits = format.createInputSplits(129);
    Assert.assertEquals("Failed to properly partition operator state into input splits", 128, splits.length);
}
Also used : KeyGroupRangeInputSplit(org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit) Configuration(org.apache.flink.configuration.Configuration) KeyedStateReaderFunction(org.apache.flink.state.api.functions.KeyedStateReaderFunction) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) OperatorState(org.apache.flink.runtime.checkpoint.OperatorState) Test(org.junit.Test)

Aggregations

KeyGroupRangeInputSplit (org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit)8 Configuration (org.apache.flink.configuration.Configuration)7 OperatorState (org.apache.flink.runtime.checkpoint.OperatorState)7 OperatorSubtaskState (org.apache.flink.runtime.checkpoint.OperatorSubtaskState)7 OperatorID (org.apache.flink.runtime.jobgraph.OperatorID)7 MemoryStateBackend (org.apache.flink.runtime.state.memory.MemoryStateBackend)7 Test (org.junit.Test)7 KeyedStateReaderFunction (org.apache.flink.state.api.functions.KeyedStateReaderFunction)6 ArrayList (java.util.ArrayList)2 Nonnull (javax.annotation.Nonnull)2 MockStreamingRuntimeContext (org.apache.flink.streaming.util.MockStreamingRuntimeContext)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Set (java.util.Set)1 RichFlatMapFunction (org.apache.flink.api.common.functions.RichFlatMapFunction)1 ValueState (org.apache.flink.api.common.state.ValueState)1 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)1 Types (org.apache.flink.api.common.typeinfo.Types)1