Search in sources :

Example 31 with DataOutputViewStreamWrapper

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

the class SerializerTestBase method testSnapshotConfigurationAndReconfigure.

@Test
public void testSnapshotConfigurationAndReconfigure() throws Exception {
    final TypeSerializer<T> serializer = getSerializer();
    final 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(), getSerializer());
    }
    TypeSerializerSchemaCompatibility<T> strategy = restoredConfig.resolveSchemaCompatibility(getSerializer());
    final TypeSerializer<T> restoreSerializer;
    if (strategy.isCompatibleAsIs()) {
        restoreSerializer = restoredConfig.restoreSerializer();
    } else if (strategy.isCompatibleWithReconfiguredSerializer()) {
        restoreSerializer = strategy.getReconfiguredSerializer();
    } else {
        throw new AssertionError("Unable to restore serializer with " + strategy);
    }
    assertEquals(serializer.getClass(), restoreSerializer.getClass());
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Example 32 with DataOutputViewStreamWrapper

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

the class EnumSerializerCompatibilityTest method checkCompatibility.

@SuppressWarnings("unchecked")
private static TypeSerializerSchemaCompatibility checkCompatibility(String enumSourceA, String enumSourceB) throws IOException, ClassNotFoundException {
    ClassLoader classLoader = ClassLoaderUtils.compileAndLoadJava(temporaryFolder.newFolder(), ENUM_NAME + ".java", enumSourceA);
    EnumSerializer enumSerializer = new EnumSerializer(classLoader.loadClass(ENUM_NAME));
    TypeSerializerSnapshot snapshot = enumSerializer.snapshotConfiguration();
    byte[] snapshotBytes;
    try (ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
        DataOutputViewStreamWrapper outputViewStreamWrapper = new DataOutputViewStreamWrapper(outBuffer)) {
        TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(outputViewStreamWrapper, snapshot, enumSerializer);
        snapshotBytes = outBuffer.toByteArray();
    }
    ClassLoader classLoader2 = ClassLoaderUtils.compileAndLoadJava(temporaryFolder.newFolder(), ENUM_NAME + ".java", enumSourceB);
    TypeSerializerSnapshot restoredSnapshot;
    try (ByteArrayInputStream inBuffer = new ByteArrayInputStream(snapshotBytes);
        DataInputViewStreamWrapper inputViewStreamWrapper = new DataInputViewStreamWrapper(inBuffer)) {
        restoredSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(inputViewStreamWrapper, classLoader2, enumSerializer);
    }
    EnumSerializer enumSerializer2 = new EnumSerializer(classLoader2.loadClass(ENUM_NAME));
    return restoredSnapshot.resolveSchemaCompatibility(enumSerializer2);
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) TypeSerializerSnapshot(org.apache.flink.api.common.typeutils.TypeSerializerSnapshot) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper)

Example 33 with DataOutputViewStreamWrapper

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

the class FullSnapshotAsyncWriter method writeKVStateData.

private void writeKVStateData(final KeyValueStateIterator mergeIterator, final CheckpointStreamWithResultProvider checkpointStreamWithResultProvider, final KeyGroupRangeOffsets keyGroupRangeOffsets) throws IOException, InterruptedException {
    byte[] previousKey = null;
    byte[] previousValue = null;
    DataOutputView kgOutView = null;
    OutputStream kgOutStream = null;
    CheckpointStateOutputStream checkpointOutputStream = checkpointStreamWithResultProvider.getCheckpointOutputStream();
    try {
        // preamble: setup with first key-group as our lookahead
        if (mergeIterator.isValid()) {
            // begin first key-group by recording the offset
            keyGroupRangeOffsets.setKeyGroupOffset(mergeIterator.keyGroup(), checkpointOutputStream.getPos());
            // write the k/v-state id as metadata
            kgOutStream = snapshotResources.getStreamCompressionDecorator().decorateWithCompression(checkpointOutputStream);
            kgOutView = new DataOutputViewStreamWrapper(kgOutStream);
            // TODO this could be aware of keyGroupPrefixBytes and write only one byte
            // if possible
            kgOutView.writeShort(mergeIterator.kvStateId());
            previousKey = mergeIterator.key();
            previousValue = mergeIterator.value();
            mergeIterator.next();
        }
        // key-group offsets.
        while (mergeIterator.isValid()) {
            assert (!hasMetaDataFollowsFlag(previousKey));
            // after this k/v pair
            if (mergeIterator.isNewKeyGroup() || mergeIterator.isNewKeyValueState()) {
                // be cooperative and check for interruption from time to time in the
                // hot loop
                checkInterrupted();
                setMetaDataFollowsFlagInKey(previousKey);
            }
            writeKeyValuePair(previousKey, previousValue, kgOutView);
            // write meta data if we have to
            if (mergeIterator.isNewKeyGroup()) {
                // TODO this could be aware of keyGroupPrefixBytes and write only one
                // byte if possible
                kgOutView.writeShort(END_OF_KEY_GROUP_MARK);
                // this will just close the outer stream
                kgOutStream.close();
                // begin new key-group
                keyGroupRangeOffsets.setKeyGroupOffset(mergeIterator.keyGroup(), checkpointOutputStream.getPos());
                // write the kev-state
                // TODO this could be aware of keyGroupPrefixBytes and write only one
                // byte if possible
                kgOutStream = snapshotResources.getStreamCompressionDecorator().decorateWithCompression(checkpointOutputStream);
                kgOutView = new DataOutputViewStreamWrapper(kgOutStream);
                kgOutView.writeShort(mergeIterator.kvStateId());
            } else if (mergeIterator.isNewKeyValueState()) {
                // write the k/v-state
                // TODO this could be aware of keyGroupPrefixBytes and write only one
                // byte if possible
                kgOutView.writeShort(mergeIterator.kvStateId());
            }
            // request next k/v pair
            previousKey = mergeIterator.key();
            previousValue = mergeIterator.value();
            mergeIterator.next();
        }
        // epilogue: write last key-group
        if (previousKey != null) {
            assert (!hasMetaDataFollowsFlag(previousKey));
            setMetaDataFollowsFlagInKey(previousKey);
            writeKeyValuePair(previousKey, previousValue, kgOutView);
            // TODO this could be aware of keyGroupPrefixBytes and write only one byte if
            // possible
            kgOutView.writeShort(END_OF_KEY_GROUP_MARK);
            // this will just close the outer stream
            kgOutStream.close();
            kgOutStream = null;
        }
    } finally {
        // this will just close the outer stream
        IOUtils.closeQuietly(kgOutStream);
    }
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) OutputStream(java.io.OutputStream) DataOutputView(org.apache.flink.core.memory.DataOutputView)

Example 34 with DataOutputViewStreamWrapper

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

the class PartitionableListState method write.

public long[] write(FSDataOutputStream out) throws IOException {
    long[] partitionOffsets = new long[internalList.size()];
    DataOutputView dov = new DataOutputViewStreamWrapper(out);
    for (int i = 0; i < internalList.size(); ++i) {
        S element = internalList.get(i);
        partitionOffsets[i] = out.getPos();
        getStateMetaInfo().getPartitionStateSerializer().serialize(element, dov);
    }
    return partitionOffsets;
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) DataOutputView(org.apache.flink.core.memory.DataOutputView)

Example 35 with DataOutputViewStreamWrapper

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

the class StringValueSerializationTest method testSerialization.

public static void testSerialization(String[] values) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
    DataOutputViewStreamWrapper serializer = new DataOutputViewStreamWrapper(baos);
    for (String value : values) {
        StringValue sv = new StringValue(value);
        sv.write(serializer);
    }
    serializer.close();
    baos.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    DataInputViewStreamWrapper deserializer = new DataInputViewStreamWrapper(bais);
    int num = 0;
    while (bais.available() > 0) {
        StringValue deser = new StringValue();
        deser.read(deserializer);
        assertEquals("DeserializedString differs from original string.", values[num], deser.getValue());
        num++;
    }
    assertEquals("Wrong number of deserialized values", values.length, num);
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper)

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