Search in sources :

Example 6 with VoidNamespace

use of org.apache.flink.runtime.state.VoidNamespace 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 7 with VoidNamespace

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

the class BatchExecutionInternalTimeServiceTest method testIgnoringProcessingTimeTimersFromWithinCallback.

@Test
public void testIgnoringProcessingTimeTimersFromWithinCallback() {
    BatchExecutionKeyedStateBackend<Integer> keyedStatedBackend = new BatchExecutionKeyedStateBackend<>(KEY_SERIALIZER, new KeyGroupRange(0, 1));
    TestProcessingTimeService processingTimeService = new TestProcessingTimeService();
    InternalTimeServiceManager<Integer> timeServiceManager = BatchExecutionInternalTimeServiceManager.create(keyedStatedBackend, this.getClass().getClassLoader(), new DummyKeyContext(), processingTimeService, Collections.emptyList());
    List<Long> timers = new ArrayList<>();
    TriggerWithTimerServiceAccess<Integer, VoidNamespace> trigger = TriggerWithTimerServiceAccess.processingTimeTrigger((timer, ts) -> {
        timers.add(timer.getTimestamp());
        ts.registerProcessingTimeTimer(VoidNamespace.INSTANCE, timer.getTimestamp() + 20);
    });
    InternalTimerService<VoidNamespace> timerService = timeServiceManager.getInternalTimerService("test", KEY_SERIALIZER, new VoidNamespaceSerializer(), trigger);
    trigger.setTimerService(timerService);
    keyedStatedBackend.setCurrentKey(1);
    timerService.registerProcessingTimeTimer(VoidNamespace.INSTANCE, 150);
    // we should never register physical timers
    assertThat(processingTimeService.getNumActiveTimers(), equalTo(0));
    // changing the current key fires all timers
    keyedStatedBackend.setCurrentKey(2);
    // We check that the timer from the callback is ignored
    assertThat(timers, equalTo(Collections.singletonList(150L)));
}
Also used : KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) ArrayList(java.util.ArrayList) TestProcessingTimeService(org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService) VoidNamespace(org.apache.flink.runtime.state.VoidNamespace) VoidNamespaceSerializer(org.apache.flink.runtime.state.VoidNamespaceSerializer) Test(org.junit.Test)

Example 8 with VoidNamespace

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

the class BatchExecutionInternalTimeServiceTest method testCurrentWatermark.

@Test
public void testCurrentWatermark() throws Exception {
    BatchExecutionKeyedStateBackend<Integer> keyedStatedBackend = new BatchExecutionKeyedStateBackend<>(KEY_SERIALIZER, new KeyGroupRange(0, 1));
    InternalTimeServiceManager<Integer> timeServiceManager = BatchExecutionInternalTimeServiceManager.create(keyedStatedBackend, this.getClass().getClassLoader(), new DummyKeyContext(), new TestProcessingTimeService(), Collections.emptyList());
    List<Long> timers = new ArrayList<>();
    TriggerWithTimerServiceAccess<Integer, VoidNamespace> eventTimeTrigger = TriggerWithTimerServiceAccess.eventTimeTrigger((timer, timerService) -> {
        assertThat(timerService.currentWatermark(), equalTo(Long.MAX_VALUE));
        timers.add(timer.getTimestamp());
    });
    InternalTimerService<VoidNamespace> timerService = timeServiceManager.getInternalTimerService("test", KEY_SERIALIZER, new VoidNamespaceSerializer(), eventTimeTrigger);
    eventTimeTrigger.setTimerService(timerService);
    assertThat(timerService.currentWatermark(), equalTo(Long.MIN_VALUE));
    keyedStatedBackend.setCurrentKey(1);
    timerService.registerEventTimeTimer(VoidNamespace.INSTANCE, 123);
    assertThat(timerService.currentWatermark(), equalTo(Long.MIN_VALUE));
    // advancing the watermark to a value different than Long.MAX_VALUE should have no effect
    timeServiceManager.advanceWatermark(new Watermark(1000));
    assertThat(timerService.currentWatermark(), equalTo(Long.MIN_VALUE));
    // changing the current key fires all timers
    keyedStatedBackend.setCurrentKey(2);
    assertThat(timerService.currentWatermark(), equalTo(Long.MIN_VALUE));
    timerService.registerEventTimeTimer(VoidNamespace.INSTANCE, 124);
    // advancing the watermark to Long.MAX_VALUE should fire remaining key
    timeServiceManager.advanceWatermark(Watermark.MAX_WATERMARK);
    assertThat(timers, equalTo(Arrays.asList(123L, 124L)));
}
Also used : KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) ArrayList(java.util.ArrayList) TestProcessingTimeService(org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService) VoidNamespace(org.apache.flink.runtime.state.VoidNamespace) VoidNamespaceSerializer(org.apache.flink.runtime.state.VoidNamespaceSerializer) Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test)

Example 9 with VoidNamespace

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

the class BatchExecutionInternalTimeServiceTest method testIgnoringEventTimeTimersFromWithinCallback.

@Test
public void testIgnoringEventTimeTimersFromWithinCallback() {
    BatchExecutionKeyedStateBackend<Integer> keyedStatedBackend = new BatchExecutionKeyedStateBackend<>(KEY_SERIALIZER, new KeyGroupRange(0, 1));
    TestProcessingTimeService processingTimeService = new TestProcessingTimeService();
    InternalTimeServiceManager<Integer> timeServiceManager = BatchExecutionInternalTimeServiceManager.create(keyedStatedBackend, this.getClass().getClassLoader(), new DummyKeyContext(), processingTimeService, Collections.emptyList());
    List<Long> timers = new ArrayList<>();
    TriggerWithTimerServiceAccess<Integer, VoidNamespace> trigger = TriggerWithTimerServiceAccess.eventTimeTrigger((timer, ts) -> {
        timers.add(timer.getTimestamp());
        ts.registerEventTimeTimer(VoidNamespace.INSTANCE, timer.getTimestamp() + 20);
    });
    InternalTimerService<VoidNamespace> timerService = timeServiceManager.getInternalTimerService("test", KEY_SERIALIZER, new VoidNamespaceSerializer(), trigger);
    trigger.setTimerService(timerService);
    keyedStatedBackend.setCurrentKey(1);
    timerService.registerEventTimeTimer(VoidNamespace.INSTANCE, 150);
    // we should never register physical timers
    assertThat(processingTimeService.getNumActiveTimers(), equalTo(0));
    // changing the current key fires all timers
    keyedStatedBackend.setCurrentKey(2);
    // We check that the timer from the callback is ignored
    assertThat(timers, equalTo(Collections.singletonList(150L)));
}
Also used : KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) ArrayList(java.util.ArrayList) TestProcessingTimeService(org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService) VoidNamespace(org.apache.flink.runtime.state.VoidNamespace) VoidNamespaceSerializer(org.apache.flink.runtime.state.VoidNamespaceSerializer) Test(org.junit.Test)

Example 10 with VoidNamespace

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

the class BatchExecutionInternalTimeServiceTest method testProcessingTimeTimers.

@Test
public void testProcessingTimeTimers() {
    BatchExecutionKeyedStateBackend<Integer> keyedStatedBackend = new BatchExecutionKeyedStateBackend<>(KEY_SERIALIZER, new KeyGroupRange(0, 1));
    TestProcessingTimeService processingTimeService = new TestProcessingTimeService();
    InternalTimeServiceManager<Integer> timeServiceManager = BatchExecutionInternalTimeServiceManager.create(keyedStatedBackend, this.getClass().getClassLoader(), new DummyKeyContext(), processingTimeService, Collections.emptyList());
    List<Long> timers = new ArrayList<>();
    InternalTimerService<VoidNamespace> timerService = timeServiceManager.getInternalTimerService("test", KEY_SERIALIZER, new VoidNamespaceSerializer(), LambdaTrigger.processingTimeTrigger(timer -> timers.add(timer.getTimestamp())));
    keyedStatedBackend.setCurrentKey(1);
    timerService.registerProcessingTimeTimer(VoidNamespace.INSTANCE, 150);
    // we should never register physical timers
    assertThat(processingTimeService.getNumActiveTimers(), equalTo(0));
    // changing the current key fires all timers
    keyedStatedBackend.setCurrentKey(2);
    assertThat(timers, equalTo(Collections.singletonList(150L)));
}
Also used : VoidNamespace(org.apache.flink.runtime.state.VoidNamespace) InternalTimeServiceManager(org.apache.flink.streaming.api.operators.InternalTimeServiceManager) Arrays(java.util.Arrays) UnregisteredMetricsGroup(org.apache.flink.metrics.groups.UnregisteredMetricsGroup) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Watermark(org.apache.flink.streaming.api.watermark.Watermark) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) VoidNamespaceSerializer(org.apache.flink.runtime.state.VoidNamespaceSerializer) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) InternalTimerService(org.apache.flink.streaming.api.operators.InternalTimerService) TestLogger(org.apache.flink.util.TestLogger) BiConsumer(java.util.function.BiConsumer) Assert.fail(org.junit.Assert.fail) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) ExpectedException(org.junit.rules.ExpectedException) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) KeyContext(org.apache.flink.streaming.api.operators.KeyContext) Triggerable(org.apache.flink.streaming.api.operators.Triggerable) InternalTimer(org.apache.flink.streaming.api.operators.InternalTimer) Test(org.junit.Test) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) TestProcessingTimeService(org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService) Consumer(java.util.function.Consumer) TtlTimeProvider(org.apache.flink.runtime.state.ttl.TtlTimeProvider) List(java.util.List) JobID(org.apache.flink.api.common.JobID) Rule(org.junit.Rule) AbstractKeyedStateBackend(org.apache.flink.runtime.state.AbstractKeyedStateBackend) Assert(org.junit.Assert) Collections(java.util.Collections) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) ArrayList(java.util.ArrayList) TestProcessingTimeService(org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService) VoidNamespace(org.apache.flink.runtime.state.VoidNamespace) VoidNamespaceSerializer(org.apache.flink.runtime.state.VoidNamespaceSerializer) Test(org.junit.Test)

Aggregations

VoidNamespace (org.apache.flink.runtime.state.VoidNamespace)31 Test (org.junit.Test)21 KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)11 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)10 ArrayList (java.util.ArrayList)9 SimpleTimerService (org.apache.flink.streaming.api.SimpleTimerService)8 JobID (org.apache.flink.api.common.JobID)6 VoidNamespaceSerializer (org.apache.flink.runtime.state.VoidNamespaceSerializer)6 InternalTimerService (org.apache.flink.streaming.api.operators.InternalTimerService)6 TestProcessingTimeService (org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService)6 List (java.util.List)5 MemoryStateBackend (org.apache.flink.runtime.state.memory.MemoryStateBackend)5 TimerService (org.apache.flink.streaming.api.TimerService)5 Arrays (java.util.Arrays)4 TaskKvStateRegistry (org.apache.flink.runtime.query.TaskKvStateRegistry)4 Watermark (org.apache.flink.streaming.api.watermark.Watermark)4 Assert (org.junit.Assert)4 Collections (java.util.Collections)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)3 BiConsumer (java.util.function.BiConsumer)3