Search in sources :

Example 56 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper in project flink by apache.

the class CopyOnWriteStateTableTest method testSerializerDuplicationInSnapshot.

/**
 * This tests that serializers used for snapshots are duplicates of the ones used in processing
 * to avoid race conditions in stateful serializers.
 */
@Test
public void testSerializerDuplicationInSnapshot() throws IOException {
    final TestDuplicateSerializer namespaceSerializer = new TestDuplicateSerializer();
    final TestDuplicateSerializer stateSerializer = new TestDuplicateSerializer();
    final TestDuplicateSerializer keySerializer = new TestDuplicateSerializer();
    RegisteredKeyValueStateBackendMetaInfo<Integer, Integer> metaInfo = new RegisteredKeyValueStateBackendMetaInfo<>(StateDescriptor.Type.VALUE, "test", namespaceSerializer, stateSerializer);
    InternalKeyContext<Integer> mockKeyContext = new MockInternalKeyContext<>();
    CopyOnWriteStateTable<Integer, Integer, Integer> table = new CopyOnWriteStateTable<>(mockKeyContext, metaInfo, keySerializer);
    table.put(0, 0, 0, 0);
    table.put(1, 0, 0, 1);
    table.put(2, 0, 1, 2);
    final CopyOnWriteStateTableSnapshot<Integer, Integer, Integer> snapshot = table.stateSnapshot();
    final StateSnapshot.StateKeyGroupWriter partitionedSnapshot = snapshot.getKeyGroupWriter();
    namespaceSerializer.disable();
    keySerializer.disable();
    stateSerializer.disable();
    partitionedSnapshot.writeStateInKeyGroup(new DataOutputViewStreamWrapper(new ByteArrayOutputStreamWithPos(1024)), 0);
}
Also used : StateSnapshot(org.apache.flink.runtime.state.StateSnapshot) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) RegisteredKeyValueStateBackendMetaInfo(org.apache.flink.runtime.state.RegisteredKeyValueStateBackendMetaInfo) Test(org.junit.Test)

Example 57 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper in project flink by apache.

the class CopyOnWriteStateTableTest method testReleaseForSuccessfulSnapshot.

/**
 * This tests that resource can be released for a successful snapshot.
 */
@Test
public void testReleaseForSuccessfulSnapshot() throws IOException {
    int numberOfKeyGroups = 10;
    CopyOnWriteStateTable<Integer, Integer, Float> table = createStateTableForSnapshotRelease(numberOfKeyGroups);
    ByteArrayOutputStreamWithPos byteArrayOutputStreamWithPos = new ByteArrayOutputStreamWithPos();
    DataOutputView dataOutputView = new DataOutputViewStreamWrapper(byteArrayOutputStreamWithPos);
    CopyOnWriteStateTableSnapshot<Integer, Integer, Float> snapshot = table.stateSnapshot();
    for (int group = 0; group < numberOfKeyGroups; group++) {
        snapshot.writeStateInKeyGroup(dataOutputView, group);
        // resource used by one key group should be released after the snapshot is successful
        Assert.assertTrue(isResourceReleasedForKeyGroup(table, group));
    }
    snapshot.release();
    verifyResourceIsReleasedForAllKeyGroup(table, 1);
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) DataOutputView(org.apache.flink.core.memory.DataOutputView) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) Test(org.junit.Test)

Example 58 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper in project flink by apache.

the class CollectSinkFunction method serializeAccumulatorResult.

@VisibleForTesting
public static byte[] serializeAccumulatorResult(long offset, String version, long lastCheckpointedOffset, List<byte[]> buffer) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputViewStreamWrapper wrapper = new DataOutputViewStreamWrapper(baos);
    wrapper.writeLong(offset);
    CollectCoordinationResponse finalResponse = new CollectCoordinationResponse(version, lastCheckpointedOffset, buffer);
    finalResponse.serialize(wrapper);
    return baos.toByteArray();
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting)

Example 59 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper in project flink by apache.

the class SerializerTestUtil method snapshotAndReconfigure.

/**
 * Snapshot and restore the given serializer. Returns the restored serializer.
 */
public static <T> TypeSerializer<T> snapshotAndReconfigure(TypeSerializer<T> serializer, SerializerGetter<T> serializerGetter) throws IOException {
    TypeSerializerSnapshot<T> configSnapshot = serializer.snapshotConfiguration();
    byte[] serializedConfig;
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(new DataOutputViewStreamWrapper(out), configSnapshot, serializer);
        serializedConfig = out.toByteArray();
    }
    TypeSerializerSnapshot<T> restoredConfig;
    try (ByteArrayInputStream in = new ByteArrayInputStream(serializedConfig)) {
        restoredConfig = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(new DataInputViewStreamWrapper(in), Thread.currentThread().getContextClassLoader(), serializerGetter.getSerializer());
    }
    TypeSerializerSchemaCompatibility<T> strategy = restoredConfig.resolveSchemaCompatibility(serializerGetter.getSerializer());
    final TypeSerializer<T> restoredSerializer;
    if (strategy.isCompatibleAsIs()) {
        restoredSerializer = restoredConfig.restoreSerializer();
    } else if (strategy.isCompatibleWithReconfiguredSerializer()) {
        restoredSerializer = strategy.getReconfiguredSerializer();
    } else {
        throw new AssertionError("Unable to restore serializer with " + strategy);
    }
    assertEquals(serializer.getClass(), restoredSerializer.getClass());
    return restoredSerializer;
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper)

Example 60 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper in project flink by apache.

the class KeyedStateCheckpointOutputStreamTest method writeAllTestKeyGroups.

private KeyGroupsStateHandle writeAllTestKeyGroups(KeyedStateCheckpointOutputStream stream, KeyGroupRange keyRange) throws Exception {
    DataOutputView dov = new DataOutputViewStreamWrapper(stream);
    for (int kg : keyRange) {
        stream.startNewKeyGroup(kg);
        dov.writeInt(kg);
    }
    return stream.closeAndGetHandle();
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) DataOutputView(org.apache.flink.core.memory.DataOutputView)

Aggregations

DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)123 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)55 ByteArrayOutputStream (java.io.ByteArrayOutputStream)49 Test (org.junit.Test)43 ByteArrayOutputStreamWithPos (org.apache.flink.core.memory.ByteArrayOutputStreamWithPos)35 IOException (java.io.IOException)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26 ByteArrayInputStreamWithPos (org.apache.flink.core.memory.ByteArrayInputStreamWithPos)23 DataOutputView (org.apache.flink.core.memory.DataOutputView)18 HashMap (java.util.HashMap)13 ArrayList (java.util.ArrayList)12 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)11 Map (java.util.Map)10 TypeSerializerSnapshot (org.apache.flink.api.common.typeutils.TypeSerializerSnapshot)7 StateMetaInfoSnapshot (org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot)7 Before (org.junit.Before)6 Socket (java.net.Socket)5 PipedInputStream (java.io.PipedInputStream)4 PipedOutputStream (java.io.PipedOutputStream)4 List (java.util.List)4