Search in sources :

Example 26 with VoidNamespace

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

the class SavepointReader method readKeyedState.

/**
 * Read keyed state from an operator in a {@code Savepoint}.
 *
 * @param uid The uid of the operator.
 * @param function The {@link KeyedStateReaderFunction} that is called for each key in state.
 * @param keyTypeInfo The type information of the key in state.
 * @param outTypeInfo The type information of the output of the transform reader function.
 * @param <K> The type of the key in state.
 * @param <OUT> The output type of the transform function.
 * @return A {@code DataStream} of objects read from keyed state.
 * @throws IOException If the savepoint does not contain operator state with the given uid.
 */
public <K, OUT> DataStream<OUT> readKeyedState(String uid, KeyedStateReaderFunction<K, OUT> function, TypeInformation<K> keyTypeInfo, TypeInformation<OUT> outTypeInfo) throws IOException {
    OperatorState operatorState = metadata.getOperatorState(uid);
    KeyedStateInputFormat<K, VoidNamespace, OUT> inputFormat = new KeyedStateInputFormat<>(operatorState, stateBackend, MutableConfig.of(env.getConfiguration()), new KeyedStateReaderOperator<>(function, keyTypeInfo));
    return SourceBuilder.fromFormat(env, inputFormat, outTypeInfo);
}
Also used : KeyedStateInputFormat(org.apache.flink.state.api.input.KeyedStateInputFormat) VoidNamespace(org.apache.flink.runtime.state.VoidNamespace) OperatorState(org.apache.flink.runtime.checkpoint.OperatorState)

Example 27 with VoidNamespace

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

the class ExistingSavepoint method readKeyedState.

/**
 * Read keyed state from an operator in a {@code Savepoint}.
 *
 * @param uid The uid of the operator.
 * @param function The {@link KeyedStateReaderFunction} that is called for each key in state.
 * @param keyTypeInfo The type information of the key in state.
 * @param outTypeInfo The type information of the output of the transform reader function.
 * @param <K> The type of the key in state.
 * @param <OUT> The output type of the transform function.
 * @return A {@code DataSet} of objects read from keyed state.
 * @throws IOException If the savepoint does not contain operator state with the given uid.
 */
public <K, OUT> DataSource<OUT> readKeyedState(String uid, KeyedStateReaderFunction<K, OUT> function, TypeInformation<K> keyTypeInfo, TypeInformation<OUT> outTypeInfo) throws IOException {
    OperatorState operatorState = metadata.getOperatorState(uid);
    KeyedStateInputFormat<K, VoidNamespace, OUT> inputFormat = new KeyedStateInputFormat<>(operatorState, stateBackend, env.getConfiguration(), new KeyedStateReaderOperator<>(function, keyTypeInfo));
    return env.createInput(inputFormat, outTypeInfo);
}
Also used : KeyedStateInputFormat(org.apache.flink.state.api.input.KeyedStateInputFormat) VoidNamespace(org.apache.flink.runtime.state.VoidNamespace) OperatorState(org.apache.flink.runtime.checkpoint.OperatorState)

Example 28 with VoidNamespace

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

the class BatchExecutionInternalTimeServiceTest method testSettingSameKeyDoesNotFireTimers.

@Test
public void testSettingSameKeyDoesNotFireTimers() {
    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<>();
    InternalTimerService<VoidNamespace> timerService = timeServiceManager.getInternalTimerService("test", KEY_SERIALIZER, new VoidNamespaceSerializer(), LambdaTrigger.eventTimeTrigger(timer -> timers.add(timer.getTimestamp())));
    keyedStatedBackend.setCurrentKey(1);
    timerService.registerEventTimeTimer(VoidNamespace.INSTANCE, 123);
    keyedStatedBackend.setCurrentKey(1);
    assertThat(timers, equalTo(Collections.emptyList()));
}
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)

Example 29 with VoidNamespace

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

the class BatchExecutionInternalTimeServiceTest method testFiringEventTimeTimers.

@Test
public void testFiringEventTimeTimers() 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<>();
    InternalTimerService<VoidNamespace> timerService = timeServiceManager.getInternalTimerService("test", KEY_SERIALIZER, new VoidNamespaceSerializer(), LambdaTrigger.eventTimeTrigger(timer -> timers.add(timer.getTimestamp())));
    keyedStatedBackend.setCurrentKey(1);
    timerService.registerEventTimeTimer(VoidNamespace.INSTANCE, 123);
    // advancing the watermark should not fire timers
    timeServiceManager.advanceWatermark(new Watermark(1000));
    timerService.deleteEventTimeTimer(VoidNamespace.INSTANCE, 123);
    timerService.registerEventTimeTimer(VoidNamespace.INSTANCE, 150);
    // 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) Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test)

Example 30 with VoidNamespace

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

the class CoBroadcastWithKeyedOperator method open.

@Override
public void open() throws Exception {
    super.open();
    InternalTimerService<VoidNamespace> internalTimerService = getInternalTimerService("user-timers", VoidNamespaceSerializer.INSTANCE, this);
    TimerService timerService = new SimpleTimerService(internalTimerService);
    collector = new TimestampedCollector<>(output);
    this.broadcastStates = new HashMap<>(broadcastStateDescriptors.size());
    for (MapStateDescriptor<?, ?> descriptor : broadcastStateDescriptors) {
        broadcastStates.put(descriptor, getOperatorStateBackend().getBroadcastState(descriptor));
    }
    rwContext = new ReadWriteContextImpl(getExecutionConfig(), getKeyedStateBackend(), userFunction, broadcastStates, timerService);
    rContext = new ReadOnlyContextImpl(getExecutionConfig(), userFunction, broadcastStates, timerService);
    onTimerContext = new OnTimerContextImpl(getExecutionConfig(), userFunction, broadcastStates, timerService);
}
Also used : SimpleTimerService(org.apache.flink.streaming.api.SimpleTimerService) VoidNamespace(org.apache.flink.runtime.state.VoidNamespace) SimpleTimerService(org.apache.flink.streaming.api.SimpleTimerService) InternalTimerService(org.apache.flink.streaming.api.operators.InternalTimerService) TimerService(org.apache.flink.streaming.api.TimerService)

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