Search in sources :

Example 1 with StateSnapshotTransformer

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

the class CopyOnWriteSkipListStateMapTestUtils method verifySnapshotWithTransform.

static <K, N, S> void verifySnapshotWithTransform(@Nonnull Map<N, Map<K, S>> referenceStates, @Nonnull CopyOnWriteSkipListStateMapSnapshot<K, N, S> snapshot, StateSnapshotTransformer<S> transformer, TypeSerializer<K> keySerializer, TypeSerializer<N> namespaceSerializer, TypeSerializer<S> stateSerializer, SnapshotVerificationMode verificationMode) throws IOException {
    ByteArrayOutputStreamWithPos outputStream = new ByteArrayOutputStreamWithPos();
    DataOutputView outputView = new DataOutputViewStreamWrapper(outputStream);
    Map<N, Map<K, S>> transformedStates = new HashMap<>();
    for (Map.Entry<N, Map<K, S>> namespaceEntry : referenceStates.entrySet()) {
        for (Map.Entry<K, S> keyEntry : namespaceEntry.getValue().entrySet()) {
            S state = transformer.filterOrTransform(keyEntry.getValue());
            if (state != null) {
                transformedStates.computeIfAbsent(namespaceEntry.getKey(), (none) -> new HashMap<>()).put(keyEntry.getKey(), state);
            }
        }
    }
    if (verificationMode == SnapshotVerificationMode.SERIALIZED) {
        snapshot.writeState(keySerializer, namespaceSerializer, stateSerializer, outputView, transformer);
        Map<N, Map<K, S>> actualStates = readStateFromSnapshot(outputStream.toByteArray(), keySerializer, namespaceSerializer, stateSerializer);
        assertEquals(transformedStates, actualStates);
    } else {
        Iterator<StateEntry<K, N, S>> iterator = snapshot.getIterator(keySerializer, namespaceSerializer, stateSerializer, null);
        assertThat(() -> iterator, containsInAnyOrder(toMatchers(referenceStates)));
    }
}
Also used : HashMap(java.util.HashMap) StringSerializer(org.apache.flink.api.common.typeutils.base.StringSerializer) DataOutputView(org.apache.flink.core.memory.DataOutputView) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) ArrayList(java.util.ArrayList) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) HashSet(java.util.HashSet) Assert.assertThat(org.junit.Assert.assertThat) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) DEFAULT_MAX_KEYS_TO_DELETE_ONE_TIME(org.apache.flink.runtime.state.heap.CopyOnWriteSkipListStateMap.DEFAULT_MAX_KEYS_TO_DELETE_ONE_TIME) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) StateSnapshotTransformer(org.apache.flink.runtime.state.StateSnapshotTransformer) LongSerializer(org.apache.flink.api.common.typeutils.base.LongSerializer) DataInputView(org.apache.flink.core.memory.DataInputView) InternalKvState(org.apache.flink.runtime.state.internal.InternalKvState) StateEntryMatcher.entry(org.apache.flink.runtime.state.testutils.StateEntryMatcher.entry) Allocator(org.apache.flink.runtime.state.heap.space.Allocator) Nonnull(javax.annotation.Nonnull) NIL_NODE(org.apache.flink.runtime.state.heap.SkipListUtils.NIL_NODE) DEFAULT_LOGICAL_REMOVED_KEYS_RATIO(org.apache.flink.runtime.state.heap.CopyOnWriteSkipListStateMap.DEFAULT_LOGICAL_REMOVED_KEYS_RATIO) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) Iterator(java.util.Iterator) Collection(java.util.Collection) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) Collectors(java.util.stream.Collectors) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matcher(org.hamcrest.Matcher) StateEntry(org.apache.flink.runtime.state.StateEntry) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) DataOutputView(org.apache.flink.core.memory.DataOutputView) StateEntry(org.apache.flink.runtime.state.StateEntry) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with StateSnapshotTransformer

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

the class MockKeyedStateBackend method createInternalState.

@Override
@SuppressWarnings("unchecked")
@Nonnull
public <N, SV, SEV, S extends State, IS extends S> IS createInternalState(@Nonnull TypeSerializer<N> namespaceSerializer, @Nonnull StateDescriptor<S, SV> stateDesc, @Nonnull StateSnapshotTransformFactory<SEV> snapshotTransformFactory) throws Exception {
    StateFactory stateFactory = STATE_FACTORIES.get(stateDesc.getType());
    if (stateFactory == null) {
        String message = String.format("State %s is not supported by %s", stateDesc.getClass(), TtlStateFactory.class);
        throw new FlinkRuntimeException(message);
    }
    IS state = stateFactory.createInternalState(namespaceSerializer, stateDesc);
    stateSnapshotFilters.put(stateDesc.getName(), (StateSnapshotTransformer<Object>) getStateSnapshotTransformer(stateDesc, snapshotTransformFactory));
    ((MockInternalKvState<K, N, SV>) state).values = () -> stateValues.computeIfAbsent(stateDesc.getName(), n -> new HashMap<>()).computeIfAbsent(getCurrentKey(), k -> new HashMap<>());
    return state;
}
Also used : Tuple2(org.apache.flink.api.java.tuple.Tuple2) SharedStateRegistry(org.apache.flink.runtime.state.SharedStateRegistry) StateHandleID(org.apache.flink.runtime.state.StateHandleID) FutureTask(java.util.concurrent.FutureTask) HashMap(java.util.HashMap) MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) KeyExtractorFunction(org.apache.flink.runtime.state.KeyExtractorFunction) InternalKeyContext(org.apache.flink.runtime.state.heap.InternalKeyContext) ArrayList(java.util.ArrayList) StateSnapshotTransformers(org.apache.flink.runtime.state.StateSnapshotTransformers) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) HeapPriorityQueueSet(org.apache.flink.runtime.state.heap.HeapPriorityQueueSet) Map(java.util.Map) StateSnapshotTransformer(org.apache.flink.runtime.state.StateSnapshotTransformer) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) KeyGroupedInternalPriorityQueue(org.apache.flink.runtime.state.KeyGroupedInternalPriorityQueue) Nonnull(javax.annotation.Nonnull) LatencyTrackingStateConfig(org.apache.flink.runtime.state.metrics.LatencyTrackingStateConfig) State(org.apache.flink.api.common.state.State) RunnableFuture(java.util.concurrent.RunnableFuture) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) SnapshotResult(org.apache.flink.runtime.state.SnapshotResult) TaskKvStateRegistry(org.apache.flink.runtime.query.TaskKvStateRegistry) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) StateDescriptor(org.apache.flink.api.common.state.StateDescriptor) Keyed(org.apache.flink.runtime.state.Keyed) StateSnapshotTransformFactory(org.apache.flink.runtime.state.StateSnapshotTransformer.StateSnapshotTransformFactory) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) KeyedStateHandle(org.apache.flink.runtime.state.KeyedStateHandle) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) Collectors(java.util.stream.Collectors) HeapPriorityQueueElement(org.apache.flink.runtime.state.heap.HeapPriorityQueueElement) Serializable(java.io.Serializable) TtlTimeProvider(org.apache.flink.runtime.state.ttl.TtlTimeProvider) List(java.util.List) PriorityComparator(org.apache.flink.runtime.state.PriorityComparator) Stream(java.util.stream.Stream) PriorityComparable(org.apache.flink.runtime.state.PriorityComparable) AbstractKeyedStateBackend(org.apache.flink.runtime.state.AbstractKeyedStateBackend) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Optional(java.util.Optional) CheckpointStreamFactory(org.apache.flink.runtime.state.CheckpointStreamFactory) SavepointResources(org.apache.flink.runtime.state.SavepointResources) TtlStateFactory(org.apache.flink.runtime.state.ttl.TtlStateFactory) TtlStateFactory(org.apache.flink.runtime.state.ttl.TtlStateFactory) HashMap(java.util.HashMap) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) Nonnull(javax.annotation.Nonnull)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Nonnull (javax.annotation.Nonnull)2 TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)2 StateSnapshotTransformer (org.apache.flink.runtime.state.StateSnapshotTransformer)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Optional (java.util.Optional)1 Set (java.util.Set)1 FutureTask (java.util.concurrent.FutureTask)1 RunnableFuture (java.util.concurrent.RunnableFuture)1 Stream (java.util.stream.Stream)1 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)1 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)1