Search in sources :

Example 11 with ByteArrayOutputStreamWithPos

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

the class TypeSerializerSerializationProxy method write.

@Override
public void write(DataOutputView out) throws IOException {
    super.write(out);
    if (typeSerializer instanceof ClassNotFoundDummyTypeSerializer) {
        ClassNotFoundDummyTypeSerializer<T> dummyTypeSerializer = (ClassNotFoundDummyTypeSerializer<T>) this.typeSerializer;
        byte[] serializerBytes = dummyTypeSerializer.getActualBytes();
        out.write(serializerBytes.length);
        out.write(serializerBytes);
    } else {
        // write in a way that allows the stream to recover from exceptions
        try (ByteArrayOutputStreamWithPos streamWithPos = new ByteArrayOutputStreamWithPos()) {
            InstantiationUtil.serializeObject(streamWithPos, typeSerializer);
            out.writeInt(streamWithPos.getPosition());
            out.write(streamWithPos.getBuf(), 0, streamWithPos.getPosition());
        }
    }
}
Also used : ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos)

Example 12 with ByteArrayOutputStreamWithPos

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

the class VersionedIOWriteableTest method testReadCompatibleVersion.

@Test
public void testReadCompatibleVersion() throws Exception {
    String payload = "test";
    TestWriteable testWriteable = new TestWriteable(1, payload);
    byte[] serialized;
    try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
        testWriteable.write(new DataOutputViewStreamWrapper(out));
        serialized = out.toByteArray();
    }
    testWriteable = new TestWriteable(2) {

        @Override
        public boolean isCompatibleVersion(int version) {
            return getVersion() >= version;
        }
    };
    try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
        testWriteable.read(new DataInputViewStreamWrapper(in));
    }
    Assert.assertEquals(payload, testWriteable.getData());
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Example 13 with ByteArrayOutputStreamWithPos

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

the class SerializationProxiesTest method testOperatorStateMetaInfoSerialization.

@Test
public void testOperatorStateMetaInfoSerialization() throws Exception {
    String name = "test";
    TypeSerializer<?> stateSerializer = DoubleSerializer.INSTANCE;
    OperatorBackendSerializationProxy.StateMetaInfo<?> metaInfo = new OperatorBackendSerializationProxy.StateMetaInfo<>(name, stateSerializer, OperatorStateHandle.Mode.BROADCAST);
    byte[] serialized;
    try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
        metaInfo.write(new DataOutputViewStreamWrapper(out));
        serialized = out.toByteArray();
    }
    metaInfo = new OperatorBackendSerializationProxy.StateMetaInfo<>(Thread.currentThread().getContextClassLoader());
    try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
        metaInfo.read(new DataInputViewStreamWrapper(in));
    }
    Assert.assertEquals(name, metaInfo.getName());
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Example 14 with ByteArrayOutputStreamWithPos

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

the class SerializationProxiesTest method testOperatorBackendSerializationProxyRoundtrip.

@Test
public void testOperatorBackendSerializationProxyRoundtrip() throws Exception {
    TypeSerializer<?> stateSerializer = DoubleSerializer.INSTANCE;
    List<OperatorBackendSerializationProxy.StateMetaInfo<?>> stateMetaInfoList = new ArrayList<>();
    stateMetaInfoList.add(new OperatorBackendSerializationProxy.StateMetaInfo<>("a", stateSerializer, OperatorStateHandle.Mode.SPLIT_DISTRIBUTE));
    stateMetaInfoList.add(new OperatorBackendSerializationProxy.StateMetaInfo<>("b", stateSerializer, OperatorStateHandle.Mode.SPLIT_DISTRIBUTE));
    stateMetaInfoList.add(new OperatorBackendSerializationProxy.StateMetaInfo<>("c", stateSerializer, OperatorStateHandle.Mode.BROADCAST));
    OperatorBackendSerializationProxy serializationProxy = new OperatorBackendSerializationProxy(stateMetaInfoList);
    byte[] serialized;
    try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
        serializationProxy.write(new DataOutputViewStreamWrapper(out));
        serialized = out.toByteArray();
    }
    serializationProxy = new OperatorBackendSerializationProxy(Thread.currentThread().getContextClassLoader());
    try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
        serializationProxy.read(new DataInputViewStreamWrapper(in));
    }
    Assert.assertEquals(stateMetaInfoList, serializationProxy.getNamedStateSerializationProxies());
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) ArrayList(java.util.ArrayList) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Example 15 with ByteArrayOutputStreamWithPos

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

the class SerializationProxiesTest method testKeyedBackendSerializationProxyRoundtrip.

@Test
public void testKeyedBackendSerializationProxyRoundtrip() throws Exception {
    TypeSerializer<?> keySerializer = IntSerializer.INSTANCE;
    TypeSerializer<?> namespaceSerializer = LongSerializer.INSTANCE;
    TypeSerializer<?> stateSerializer = DoubleSerializer.INSTANCE;
    List<KeyedBackendSerializationProxy.StateMetaInfo<?, ?>> stateMetaInfoList = new ArrayList<>();
    stateMetaInfoList.add(new KeyedBackendSerializationProxy.StateMetaInfo<>(StateDescriptor.Type.VALUE, "a", namespaceSerializer, stateSerializer));
    stateMetaInfoList.add(new KeyedBackendSerializationProxy.StateMetaInfo<>(StateDescriptor.Type.VALUE, "b", namespaceSerializer, stateSerializer));
    stateMetaInfoList.add(new KeyedBackendSerializationProxy.StateMetaInfo<>(StateDescriptor.Type.VALUE, "c", namespaceSerializer, stateSerializer));
    KeyedBackendSerializationProxy serializationProxy = new KeyedBackendSerializationProxy(keySerializer, stateMetaInfoList);
    byte[] serialized;
    try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
        serializationProxy.write(new DataOutputViewStreamWrapper(out));
        serialized = out.toByteArray();
    }
    serializationProxy = new KeyedBackendSerializationProxy(Thread.currentThread().getContextClassLoader());
    try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
        serializationProxy.read(new DataInputViewStreamWrapper(in));
    }
    Assert.assertEquals(keySerializer, serializationProxy.getKeySerializerProxy().getTypeSerializer());
    Assert.assertEquals(stateMetaInfoList, serializationProxy.getNamedStateSerializationProxies());
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) ArrayList(java.util.ArrayList) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Aggregations

ByteArrayOutputStreamWithPos (org.apache.flink.core.memory.ByteArrayOutputStreamWithPos)16 DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)14 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)11 ByteArrayInputStreamWithPos (org.apache.flink.core.memory.ByteArrayInputStreamWithPos)10 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)4 Random (java.util.Random)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 URLClassLoader (java.net.URLClassLoader)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 OperatorStateStore (org.apache.flink.api.common.state.OperatorStateStore)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)1 DataOutputView (org.apache.flink.core.memory.DataOutputView)1