Search in sources :

Example 21 with ValueStateDescriptor

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

the class StateBackendTestBase method testValueState.

@Test
@SuppressWarnings("unchecked")
public void testValueState() throws Exception {
    CheckpointStreamFactory streamFactory = createStreamFactory();
    AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);
    ValueStateDescriptor<String> kvId = new ValueStateDescriptor<>("id", String.class);
    kvId.initializeSerializerUnlessSet(new ExecutionConfig());
    TypeSerializer<Integer> keySerializer = IntSerializer.INSTANCE;
    TypeSerializer<VoidNamespace> namespaceSerializer = VoidNamespaceSerializer.INSTANCE;
    TypeSerializer<String> valueSerializer = kvId.getSerializer();
    ValueState<String> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
    @SuppressWarnings("unchecked") InternalKvState<VoidNamespace> kvState = (InternalKvState<VoidNamespace>) state;
    // some modifications to the state
    backend.setCurrentKey(1);
    assertNull(state.value());
    assertNull(getSerializedValue(kvState, 1, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer));
    state.update("1");
    backend.setCurrentKey(2);
    assertNull(state.value());
    assertNull(getSerializedValue(kvState, 2, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer));
    state.update("2");
    backend.setCurrentKey(1);
    assertEquals("1", state.value());
    assertEquals("1", getSerializedValue(kvState, 1, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer));
    // draw a snapshot
    KeyGroupsStateHandle snapshot1 = FutureUtil.runIfNotDoneAndGet(backend.snapshot(682375462378L, 2, streamFactory, CheckpointOptions.forFullCheckpoint()));
    // make some more modifications
    backend.setCurrentKey(1);
    state.update("u1");
    backend.setCurrentKey(2);
    state.update("u2");
    backend.setCurrentKey(3);
    state.update("u3");
    // draw another snapshot
    KeyGroupsStateHandle snapshot2 = FutureUtil.runIfNotDoneAndGet(backend.snapshot(682375462379L, 4, streamFactory, CheckpointOptions.forFullCheckpoint()));
    // validate the original state
    backend.setCurrentKey(1);
    assertEquals("u1", state.value());
    assertEquals("u1", getSerializedValue(kvState, 1, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer));
    backend.setCurrentKey(2);
    assertEquals("u2", state.value());
    assertEquals("u2", getSerializedValue(kvState, 2, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer));
    backend.setCurrentKey(3);
    assertEquals("u3", state.value());
    assertEquals("u3", getSerializedValue(kvState, 3, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer));
    backend.dispose();
    backend = restoreKeyedBackend(IntSerializer.INSTANCE, snapshot1);
    snapshot1.discardState();
    ValueState<String> restored1 = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
    @SuppressWarnings("unchecked") InternalKvState<VoidNamespace> restoredKvState1 = (InternalKvState<VoidNamespace>) restored1;
    backend.setCurrentKey(1);
    assertEquals("1", restored1.value());
    assertEquals("1", getSerializedValue(restoredKvState1, 1, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer));
    backend.setCurrentKey(2);
    assertEquals("2", restored1.value());
    assertEquals("2", getSerializedValue(restoredKvState1, 2, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer));
    backend.dispose();
    backend = restoreKeyedBackend(IntSerializer.INSTANCE, snapshot2);
    snapshot2.discardState();
    ValueState<String> restored2 = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
    @SuppressWarnings("unchecked") InternalKvState<VoidNamespace> restoredKvState2 = (InternalKvState<VoidNamespace>) restored2;
    backend.setCurrentKey(1);
    assertEquals("u1", restored2.value());
    assertEquals("u1", getSerializedValue(restoredKvState2, 1, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer));
    backend.setCurrentKey(2);
    assertEquals("u2", restored2.value());
    assertEquals("u2", getSerializedValue(restoredKvState2, 2, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer));
    backend.setCurrentKey(3);
    assertEquals("u3", restored2.value());
    assertEquals("u3", getSerializedValue(restoredKvState2, 3, keySerializer, VoidNamespace.INSTANCE, namespaceSerializer, valueSerializer));
    backend.dispose();
}
Also used : BlockerCheckpointStreamFactory(org.apache.flink.runtime.util.BlockerCheckpointStreamFactory) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) InternalKvState(org.apache.flink.runtime.state.internal.InternalKvState) Test(org.junit.Test)

Example 22 with ValueStateDescriptor

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

the class StateBackendTestBase method testValueStateRestoreWithWrongSerializers.

@Test
@SuppressWarnings("unchecked")
public void testValueStateRestoreWithWrongSerializers() {
    try {
        CheckpointStreamFactory streamFactory = createStreamFactory();
        AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);
        ValueStateDescriptor<String> kvId = new ValueStateDescriptor<>("id", String.class);
        kvId.initializeSerializerUnlessSet(new ExecutionConfig());
        ValueState<String> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
        backend.setCurrentKey(1);
        state.update("1");
        backend.setCurrentKey(2);
        state.update("2");
        // draw a snapshot
        KeyGroupsStateHandle snapshot1 = FutureUtil.runIfNotDoneAndGet(backend.snapshot(682375462378L, 2, streamFactory, CheckpointOptions.forFullCheckpoint()));
        backend.dispose();
        // restore the first snapshot and validate it
        backend = restoreKeyedBackend(IntSerializer.INSTANCE, snapshot1);
        snapshot1.discardState();
        @SuppressWarnings("unchecked") TypeSerializer<String> fakeStringSerializer = (TypeSerializer<String>) (TypeSerializer<?>) FloatSerializer.INSTANCE;
        try {
            kvId = new ValueStateDescriptor<>("id", fakeStringSerializer);
            state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
            state.value();
            fail("should recognize wrong serializers");
        } catch (IOException e) {
            if (!e.getMessage().contains("Trying to access state using wrong")) {
                fail("wrong exception " + e);
            }
        // expected
        } catch (Exception e) {
            fail("wrong exception " + e);
        }
        backend.dispose();
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : BlockerCheckpointStreamFactory(org.apache.flink.runtime.util.BlockerCheckpointStreamFactory) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) Test(org.junit.Test)

Example 23 with ValueStateDescriptor

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

the class StateBackendTestBase method testCopyDefaultValue.

@Test
public void testCopyDefaultValue() throws Exception {
    AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);
    ValueStateDescriptor<IntValue> kvId = new ValueStateDescriptor<>("id", IntValue.class, new IntValue(-1));
    kvId.initializeSerializerUnlessSet(new ExecutionConfig());
    ValueState<IntValue> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
    backend.setCurrentKey(1);
    IntValue default1 = state.value();
    backend.setCurrentKey(2);
    IntValue default2 = state.value();
    assertNotNull(default1);
    assertNotNull(default2);
    assertEquals(default1, default2);
    assertFalse(default1 == default2);
    backend.dispose();
}
Also used : ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) IntValue(org.apache.flink.types.IntValue) Test(org.junit.Test)

Example 24 with ValueStateDescriptor

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

the class StateBackendTestBase method testValueStateDefaultValue.

/**
	 * Verify that an empty {@code ValueState} will yield the default value.
	 */
@Test
public void testValueStateDefaultValue() throws Exception {
    AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);
    ValueStateDescriptor<String> kvId = new ValueStateDescriptor<>("id", String.class, "Hello");
    kvId.initializeSerializerUnlessSet(new ExecutionConfig());
    ValueState<String> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
    backend.setCurrentKey(1);
    assertEquals("Hello", state.value());
    state.update("Ciao");
    assertEquals("Ciao", state.value());
    state.clear();
    assertEquals("Hello", state.value());
    backend.dispose();
}
Also used : ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Test(org.junit.Test)

Example 25 with ValueStateDescriptor

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

the class StreamGroupedFold method open.

@Override
public void open() throws Exception {
    super.open();
    if (serializedInitialValue == null) {
        throw new RuntimeException("No initial value was serialized for the fold " + "operator. Probably the setOutputType method was not called.");
    }
    try (ByteArrayInputStream bais = new ByteArrayInputStream(serializedInitialValue);
        DataInputViewStreamWrapper in = new DataInputViewStreamWrapper(bais)) {
        initialValue = outTypeSerializer.deserialize(in);
    }
    ValueStateDescriptor<OUT> stateId = new ValueStateDescriptor<>(STATE_NAME, outTypeSerializer);
    values = getPartitionedState(stateId);
}
Also used : ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper)

Aggregations

ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)28 Test (org.junit.Test)25 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)14 DummyEnvironment (org.apache.flink.runtime.operators.testutils.DummyEnvironment)13 JobID (org.apache.flink.api.common.JobID)12 KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)9 KvStateRegistry (org.apache.flink.runtime.query.KvStateRegistry)8 MemoryStateBackend (org.apache.flink.runtime.state.memory.MemoryStateBackend)8 BlockerCheckpointStreamFactory (org.apache.flink.runtime.util.BlockerCheckpointStreamFactory)7 ByteBuf (io.netty.buffer.ByteBuf)6 AbstractStateBackend (org.apache.flink.runtime.state.AbstractStateBackend)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 KeySelector (org.apache.flink.api.java.functions.KeySelector)3 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)3 JobGraph (org.apache.flink.runtime.jobgraph.JobGraph)3 CancellationSuccess (org.apache.flink.runtime.messages.JobManagerMessages.CancellationSuccess)3 QueryableStateClient (org.apache.flink.runtime.query.QueryableStateClient)3 KvStateRequestFailure (org.apache.flink.runtime.query.netty.message.KvStateRequestFailure)3 InternalKvState (org.apache.flink.runtime.state.internal.InternalKvState)3