Search in sources :

Example 26 with KeyGroupRange

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

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

Example 28 with KeyGroupRange

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

the class BatchExecutionStateBackendVerificationTest method verifySnapshotNotSupported.

@Test
public void verifySnapshotNotSupported() {
    expectedException.expect(UnsupportedOperationException.class);
    expectedException.expectMessage("Snapshotting is not supported in BATCH runtime mode.");
    BatchExecutionKeyedStateBackend<Long> stateBackend = new BatchExecutionKeyedStateBackend<>(LONG_SERIALIZER, new KeyGroupRange(0, 9));
    long checkpointId = 0L;
    CheckpointStreamFactory streamFactory = new MemCheckpointStreamFactory(10);
    stateBackend.snapshot(checkpointId, 0L, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation());
}
Also used : MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) CheckpointStreamFactory(org.apache.flink.runtime.state.CheckpointStreamFactory) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) Test(org.junit.Test)

Example 29 with KeyGroupRange

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

the class KvStateRegistryTest method testKvStateEntry.

@Test
public void testKvStateEntry() throws InterruptedException {
    final int threads = 10;
    final CountDownLatch latch1 = new CountDownLatch(threads);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final List<KvStateInfo<?, ?, ?>> infos = Collections.synchronizedList(new ArrayList<>());
    final JobID jobID = new JobID();
    final JobVertexID jobVertexId = new JobVertexID();
    final KeyGroupRange keyGroupRange = new KeyGroupRange(0, 1);
    final String registrationName = "foobar";
    final KvStateRegistry kvStateRegistry = new KvStateRegistry();
    final KvStateID stateID = kvStateRegistry.registerKvState(jobID, jobVertexId, keyGroupRange, registrationName, new DummyKvState(), getClass().getClassLoader());
    final AtomicReference<Throwable> exceptionHolder = new AtomicReference<>();
    for (int i = 0; i < threads; i++) {
        new Thread(() -> {
            final KvStateEntry<?, ?, ?> kvState = kvStateRegistry.getKvState(stateID);
            final KvStateInfo<?, ?, ?> stateInfo = kvState.getInfoForCurrentThread();
            infos.add(stateInfo);
            latch1.countDown();
            try {
                latch2.await();
            } catch (InterruptedException e) {
                // compare and set, so that we do not overwrite an exception
                // that was (potentially) already encountered.
                exceptionHolder.compareAndSet(null, e);
            }
        }).start();
    }
    latch1.await();
    final KvStateEntry<?, ?, ?> kvState = kvStateRegistry.getKvState(stateID);
    // verify that all the threads are done correctly.
    Assert.assertEquals(threads, infos.size());
    Assert.assertEquals(threads, kvState.getCacheSize());
    latch2.countDown();
    for (KvStateInfo<?, ?, ?> infoA : infos) {
        boolean instanceAlreadyFound = false;
        for (KvStateInfo<?, ?, ?> infoB : infos) {
            if (infoA == infoB) {
                if (instanceAlreadyFound) {
                    Assert.fail("More than one thread sharing the same serializer instance.");
                }
                instanceAlreadyFound = true;
            } else {
                Assert.assertEquals(infoA, infoB);
            }
        }
    }
    kvStateRegistry.unregisterKvState(jobID, jobVertexId, keyGroupRange, registrationName, stateID);
    Assert.assertEquals(0L, kvState.getCacheSize());
    Throwable t = exceptionHolder.get();
    if (t != null) {
        fail(t.getMessage());
    }
}
Also used : JobVertexID(org.apache.flink.runtime.jobgraph.JobVertexID) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) KvStateID(org.apache.flink.queryablestate.KvStateID) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 30 with KeyGroupRange

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

the class KvStateRegistryTest method testLegacyCodePathPreference.

/**
 * Tests that {@link KvStateRegistryListener} registered under {@link
 * HighAvailabilityServices#DEFAULT_JOB_ID} will be used for all notifications.
 */
@Test
public void testLegacyCodePathPreference() {
    final KvStateRegistry kvStateRegistry = new KvStateRegistry();
    final ArrayDeque<JobID> stateRegistrationNotifications = new ArrayDeque<>(2);
    final ArrayDeque<JobID> stateDeregistrationNotifications = new ArrayDeque<>(2);
    final TestingKvStateRegistryListener testingListener = new TestingKvStateRegistryListener(stateRegistrationNotifications, stateDeregistrationNotifications);
    final ArrayDeque<JobID> anotherQueue = new ArrayDeque<>(2);
    final TestingKvStateRegistryListener anotherListener = new TestingKvStateRegistryListener(anotherQueue, anotherQueue);
    final JobID jobId = new JobID();
    kvStateRegistry.registerListener(HighAvailabilityServices.DEFAULT_JOB_ID, testingListener);
    kvStateRegistry.registerListener(jobId, anotherListener);
    final JobVertexID jobVertexId = new JobVertexID();
    final KeyGroupRange keyGroupRange = new KeyGroupRange(0, 1);
    final String registrationName = "registrationName";
    final KvStateID kvStateID = kvStateRegistry.registerKvState(jobId, jobVertexId, keyGroupRange, registrationName, new DummyKvState(), getClass().getClassLoader());
    assertThat(stateRegistrationNotifications.poll(), equalTo(jobId));
    // another listener should not have received any notifications
    assertThat(anotherQueue.isEmpty(), is(true));
    kvStateRegistry.unregisterKvState(jobId, jobVertexId, keyGroupRange, registrationName, kvStateID);
    assertThat(stateDeregistrationNotifications.poll(), equalTo(jobId));
    // another listener should not have received any notifications
    assertThat(anotherQueue.isEmpty(), is(true));
}
Also used : JobVertexID(org.apache.flink.runtime.jobgraph.JobVertexID) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) KvStateID(org.apache.flink.queryablestate.KvStateID) JobID(org.apache.flink.api.common.JobID) ArrayDeque(java.util.ArrayDeque) Test(org.junit.Test)

Aggregations

KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)106 Test (org.junit.Test)67 JobID (org.apache.flink.api.common.JobID)46 JobVertexID (org.apache.flink.runtime.jobgraph.JobVertexID)38 ArrayList (java.util.ArrayList)26 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)23 MemoryStateBackend (org.apache.flink.runtime.state.memory.MemoryStateBackend)21 DummyEnvironment (org.apache.flink.runtime.operators.testutils.DummyEnvironment)18 KvStateRegistry (org.apache.flink.runtime.query.KvStateRegistry)18 UnregisteredMetricsGroup (org.apache.flink.metrics.groups.UnregisteredMetricsGroup)17 HashMap (java.util.HashMap)15 KeyGroupsStateHandle (org.apache.flink.runtime.state.KeyGroupsStateHandle)15 TestProcessingTimeService (org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService)15 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)14 ExecutionJobVertex (org.apache.flink.runtime.executiongraph.ExecutionJobVertex)14 KeyedStateHandle (org.apache.flink.runtime.state.KeyedStateHandle)14 AcknowledgeCheckpoint (org.apache.flink.runtime.messages.checkpoint.AcknowledgeCheckpoint)13 OperatorStateHandle (org.apache.flink.runtime.state.OperatorStateHandle)13 List (java.util.List)12 StreamStateHandle (org.apache.flink.runtime.state.StreamStateHandle)12