Search in sources :

Example 6 with KeyedStateStore

use of org.apache.flink.api.common.state.KeyedStateStore in project flink by apache.

the class WindowOperatorContractTest method testStateTypeIsConsistentFromWindowStateAndGlobalState.

@Test
public void testStateTypeIsConsistentFromWindowStateAndGlobalState() throws Exception {
    class NoOpAggregateFunction implements AggregateFunction<String, String, String> {

        @Override
        public String createAccumulator() {
            return null;
        }

        @Override
        public String add(String value, String accumulator) {
            return null;
        }

        @Override
        public String getResult(String accumulator) {
            return null;
        }

        @Override
        public String merge(String a, String b) {
            return null;
        }
    }
    WindowAssigner<Integer, TimeWindow> mockAssigner = mockTimeWindowAssigner();
    Trigger<Integer, TimeWindow> mockTrigger = mockTrigger();
    InternalWindowFunction<Iterable<Integer>, Void, Integer, TimeWindow> mockWindowFunction = mockWindowFunction();
    KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Void> testHarness = createWindowOperator(mockAssigner, mockTrigger, 20L, mockWindowFunction);
    testHarness.open();
    when(mockTrigger.onElement(anyInt(), anyLong(), anyTimeWindow(), anyTriggerContext())).thenReturn(TriggerResult.FIRE);
    when(mockAssigner.assignWindows(anyInt(), anyLong(), anyAssignerContext())).thenReturn(Arrays.asList(new TimeWindow(0, 20)));
    AtomicBoolean processWasInvoked = new AtomicBoolean(false);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            InternalWindowFunction.InternalWindowContext context = (InternalWindowFunction.InternalWindowContext) invocationOnMock.getArguments()[2];
            KeyedStateStore windowKeyedStateStore = context.windowState();
            KeyedStateStore globalKeyedStateStore = context.globalState();
            ListStateDescriptor<String> windowListStateDescriptor = new ListStateDescriptor<String>("windowListState", String.class);
            ListStateDescriptor<String> globalListStateDescriptor = new ListStateDescriptor<String>("globalListState", String.class);
            assertEquals(windowKeyedStateStore.getListState(windowListStateDescriptor).getClass(), globalKeyedStateStore.getListState(globalListStateDescriptor).getClass());
            ValueStateDescriptor<String> windowValueStateDescriptor = new ValueStateDescriptor<String>("windowValueState", String.class);
            ValueStateDescriptor<String> globalValueStateDescriptor = new ValueStateDescriptor<String>("globalValueState", String.class);
            assertEquals(windowKeyedStateStore.getState(windowValueStateDescriptor).getClass(), globalKeyedStateStore.getState(globalValueStateDescriptor).getClass());
            AggregatingStateDescriptor<String, String, String> windowAggStateDesc = new AggregatingStateDescriptor<String, String, String>("windowAgg", new NoOpAggregateFunction(), String.class);
            AggregatingStateDescriptor<String, String, String> globalAggStateDesc = new AggregatingStateDescriptor<String, String, String>("globalAgg", new NoOpAggregateFunction(), String.class);
            assertEquals(windowKeyedStateStore.getAggregatingState(windowAggStateDesc).getClass(), globalKeyedStateStore.getAggregatingState(globalAggStateDesc).getClass());
            ReducingStateDescriptor<String> windowReducingStateDesc = new ReducingStateDescriptor<String>("windowReducing", (a, b) -> a, String.class);
            ReducingStateDescriptor<String> globalReducingStateDesc = new ReducingStateDescriptor<String>("globalReducing", (a, b) -> a, String.class);
            assertEquals(windowKeyedStateStore.getReducingState(windowReducingStateDesc).getClass(), globalKeyedStateStore.getReducingState(globalReducingStateDesc).getClass());
            MapStateDescriptor<String, String> windowMapStateDescriptor = new MapStateDescriptor<String, String>("windowMapState", String.class, String.class);
            MapStateDescriptor<String, String> globalMapStateDescriptor = new MapStateDescriptor<String, String>("globalMapState", String.class, String.class);
            assertEquals(windowKeyedStateStore.getMapState(windowMapStateDescriptor).getClass(), globalKeyedStateStore.getMapState(globalMapStateDescriptor).getClass());
            processWasInvoked.set(true);
            return null;
        }
    }).when(mockWindowFunction).process(anyInt(), anyTimeWindow(), anyInternalWindowContext(), anyIntIterable(), WindowOperatorContractTest.<Void>anyCollector());
    testHarness.processElement(new StreamRecord<>(0, 0L));
    assertTrue(processWasInvoked.get());
}
Also used : Arrays(java.util.Arrays) ReducingStateDescriptor(org.apache.flink.api.common.state.ReducingStateDescriptor) Trigger(org.apache.flink.streaming.api.windowing.triggers.Trigger) KeyedOneInputStreamOperatorTestHarness(org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness) StringSerializer(org.apache.flink.api.common.typeutils.base.StringSerializer) OneInputStreamOperatorTestHarness(org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness) Assert.assertThat(org.junit.Assert.assertThat) AggregateFunction(org.apache.flink.api.common.functions.AggregateFunction) Matchers.eq(org.mockito.Matchers.eq) MockitoHamcrest.argThat(org.mockito.hamcrest.MockitoHamcrest.argThat) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TestLogger(org.apache.flink.util.TestLogger) KeyedStateStore(org.apache.flink.api.common.state.KeyedStateStore) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) Matchers.anyInt(org.mockito.Matchers.anyInt) GlobalWindow(org.apache.flink.streaming.api.windowing.windows.GlobalWindow) Collection(java.util.Collection) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) OutputTag(org.apache.flink.util.OutputTag) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) VerificationMode(org.mockito.verification.VerificationMode) AbstractStreamOperatorTestHarness(org.apache.flink.streaming.util.AbstractStreamOperatorTestHarness) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) InternalWindowFunction(org.apache.flink.streaming.runtime.operators.windowing.functions.InternalWindowFunction) Mockito.mock(org.mockito.Mockito.mock) Matchers(org.mockito.Matchers) Matchers.anyCollection(org.mockito.Matchers.anyCollection) Watermark(org.apache.flink.streaming.api.watermark.Watermark) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) TriggerResult(org.apache.flink.streaming.api.windowing.triggers.TriggerResult) Window(org.apache.flink.streaming.api.windowing.windows.Window) ArrayList(java.util.ArrayList) StreamRecordMatchers.streamRecord(org.apache.flink.streaming.util.StreamRecordMatchers.streamRecord) Answer(org.mockito.stubbing.Answer) WindowAssigner(org.apache.flink.streaming.api.windowing.assigners.WindowAssigner) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) Collector(org.apache.flink.util.Collector) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) Matchers.anyLong(org.mockito.Matchers.anyLong) ExpectedException(org.junit.rules.ExpectedException) AggregatingStateDescriptor(org.apache.flink.api.common.state.AggregatingStateDescriptor) MergingWindowAssigner(org.apache.flink.streaming.api.windowing.assigners.MergingWindowAssigner) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) AggregatingStateDescriptor(org.apache.flink.api.common.state.AggregatingStateDescriptor) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) ReducingStateDescriptor(org.apache.flink.api.common.state.ReducingStateDescriptor) InternalWindowFunction(org.apache.flink.streaming.runtime.operators.windowing.functions.InternalWindowFunction) TimeWindow(org.apache.flink.streaming.api.windowing.windows.TimeWindow) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KeyedStateStore(org.apache.flink.api.common.state.KeyedStateStore) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AggregateFunction(org.apache.flink.api.common.functions.AggregateFunction) Test(org.junit.Test)

Example 7 with KeyedStateStore

use of org.apache.flink.api.common.state.KeyedStateStore in project flink by apache.

the class StreamingRuntimeContext method getListState.

@Override
public <T> ListState<T> getListState(ListStateDescriptor<T> stateProperties) {
    KeyedStateStore keyedStateStore = checkPreconditionsAndGetKeyedStateStore(stateProperties);
    stateProperties.initializeSerializerUnlessSet(getExecutionConfig());
    return keyedStateStore.getListState(stateProperties);
}
Also used : KeyedStateStore(org.apache.flink.api.common.state.KeyedStateStore)

Example 8 with KeyedStateStore

use of org.apache.flink.api.common.state.KeyedStateStore in project flink by apache.

the class StreamingRuntimeContext method getMapState.

@Override
public <UK, UV> MapState<UK, UV> getMapState(MapStateDescriptor<UK, UV> stateProperties) {
    KeyedStateStore keyedStateStore = checkPreconditionsAndGetKeyedStateStore(stateProperties);
    stateProperties.initializeSerializerUnlessSet(getExecutionConfig());
    return keyedStateStore.getMapState(stateProperties);
}
Also used : KeyedStateStore(org.apache.flink.api.common.state.KeyedStateStore)

Example 9 with KeyedStateStore

use of org.apache.flink.api.common.state.KeyedStateStore in project flink by apache.

the class StreamingRuntimeContext method getReducingState.

@Override
public <T> ReducingState<T> getReducingState(ReducingStateDescriptor<T> stateProperties) {
    KeyedStateStore keyedStateStore = checkPreconditionsAndGetKeyedStateStore(stateProperties);
    stateProperties.initializeSerializerUnlessSet(getExecutionConfig());
    return keyedStateStore.getReducingState(stateProperties);
}
Also used : KeyedStateStore(org.apache.flink.api.common.state.KeyedStateStore)

Aggregations

KeyedStateStore (org.apache.flink.api.common.state.KeyedStateStore)9 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)1 AggregateFunction (org.apache.flink.api.common.functions.AggregateFunction)1 AggregatingStateDescriptor (org.apache.flink.api.common.state.AggregatingStateDescriptor)1 MapStateDescriptor (org.apache.flink.api.common.state.MapStateDescriptor)1 ReducingStateDescriptor (org.apache.flink.api.common.state.ReducingStateDescriptor)1 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)1 TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)1 LongSerializer (org.apache.flink.api.common.typeutils.base.LongSerializer)1 StringSerializer (org.apache.flink.api.common.typeutils.base.StringSerializer)1 OperatorSubtaskState (org.apache.flink.runtime.checkpoint.OperatorSubtaskState)1 Watermark (org.apache.flink.streaming.api.watermark.Watermark)1 MergingWindowAssigner (org.apache.flink.streaming.api.windowing.assigners.MergingWindowAssigner)1