Search in sources :

Example 36 with DataOutputSerializer

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

the class PulsarDeserializationSchemaTest method createFromFlinkTypeInformation.

@Test
void createFromFlinkTypeInformation() throws Exception {
    PulsarDeserializationSchema<String> schema = flinkTypeInfo(Types.STRING, null);
    schema.open(new TestingDeserializationContext(), mock(SourceConfiguration.class));
    assertDoesNotThrow(() -> InstantiationUtil.clone(schema));
    Message<byte[]> message = getMessage("test-content", s -> {
        DataOutputSerializer serializer = new DataOutputSerializer(10);
        StringValue.writeString(s, serializer);
        return serializer.getSharedBuffer();
    });
    SingleMessageCollector<String> collector = new SingleMessageCollector<>();
    schema.deserialize(message, collector);
    assertNotNull(collector.result);
    assertEquals(collector.result, "test-content");
}
Also used : DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) SourceConfiguration(org.apache.flink.connector.pulsar.source.config.SourceConfiguration) TestingDeserializationContext(org.apache.flink.connector.testutils.source.deserialization.TestingDeserializationContext) Test(org.junit.jupiter.api.Test)

Example 37 with DataOutputSerializer

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

the class LinkedOptionalMapSerializer method writeFramed.

private static <T> void writeFramed(DataOutputView out, BiConsumerWithException<DataOutputView, T, IOException> writer, T item) throws IOException {
    DataOutputSerializer frame = new DataOutputSerializer(64);
    writer.accept(frame, item);
    final byte[] buffer = frame.getSharedBuffer();
    final int bufferSize = frame.length();
    out.writeInt(bufferSize);
    out.write(buffer, 0, bufferSize);
}
Also used : DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer)

Example 38 with DataOutputSerializer

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

the class KryoSerializerConcurrencyTest method testConcurrentUseOfSerializer.

@Test
public void testConcurrentUseOfSerializer() throws Exception {
    final KryoSerializer<String> serializer = new KryoSerializer<>(String.class, new ExecutionConfig());
    final BlockerSync sync = new BlockerSync();
    final DataOutputView regularOut = new DataOutputSerializer(32);
    final DataOutputView lockingOut = new LockingView(sync);
    // this thread serializes and gets stuck there
    final CheckedThread thread = new CheckedThread("serializer") {

        @Override
        public void go() throws Exception {
            serializer.serialize("a value", lockingOut);
        }
    };
    thread.start();
    sync.awaitBlocker();
    // this should fail with an exception
    try {
        serializer.serialize("value", regularOut);
        fail("should have failed with an exception");
    } catch (IllegalStateException e) {
    // expected
    } finally {
        // release the thread that serializes
        sync.releaseBlocker();
    }
    // this propagates exceptions from the spawned thread
    thread.sync();
}
Also used : BlockerSync(org.apache.flink.core.testutils.BlockerSync) DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) DataOutputView(org.apache.flink.core.memory.DataOutputView) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) CheckedThread(org.apache.flink.core.testutils.CheckedThread) Test(org.junit.Test)

Example 39 with DataOutputSerializer

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

the class KryoSerializerSnapshotTest method unLoadableSnapshotBytes.

/**
 * This method returns the bytes of a serialized {@link KryoSerializerSnapshot}, that contains a
 * Kryo registration of a class that does not exists in the current classpath.
 */
private static byte[] unLoadableSnapshotBytes() throws IOException {
    final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    final ClassLoaderUtils.ObjectAndClassLoader<Serializable> outsideClassLoading = ClassLoaderUtils.createSerializableObjectFromNewClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(outsideClassLoading.getClassLoader());
        ExecutionConfig conf = new ExecutionConfig();
        conf.registerKryoType(outsideClassLoading.getObject().getClass());
        KryoSerializer<Animal> previousSerializer = new KryoSerializer<>(Animal.class, conf);
        TypeSerializerSnapshot<Animal> previousSnapshot = previousSerializer.snapshotConfiguration();
        DataOutputSerializer out = new DataOutputSerializer(4096);
        TypeSerializerSnapshot.writeVersionedSnapshot(out, previousSnapshot);
        return out.getCopyOfBuffer();
    } finally {
        Thread.currentThread().setContextClassLoader(originalClassLoader);
    }
}
Also used : Serializable(java.io.Serializable) DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) ClassLoaderUtils(org.apache.flink.testutils.ClassLoaderUtils) Animal(org.apache.flink.api.java.typeutils.runtime.kryo.KryoPojosForMigrationTests.Animal) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) DogKryoSerializer(org.apache.flink.api.java.typeutils.runtime.kryo.KryoPojosForMigrationTests.DogKryoSerializer) DogV2KryoSerializer(org.apache.flink.api.java.typeutils.runtime.kryo.KryoPojosForMigrationTests.DogV2KryoSerializer)

Example 40 with DataOutputSerializer

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

the class SimpleVersionedSerializationTest method testSerializeEmpty.

@Test
public void testSerializeEmpty() throws IOException {
    final String testString = "beeeep!";
    SimpleVersionedSerializer<String> emptySerializer = new SimpleVersionedSerializer<String>() {

        @Override
        public int getVersion() {
            return 42;
        }

        @Override
        public byte[] serialize(String obj) throws IOException {
            return new byte[0];
        }

        @Override
        public String deserialize(int version, byte[] serialized) throws IOException {
            assertEquals(42, version);
            assertEquals(0, serialized.length);
            return testString;
        }
    };
    final DataOutputSerializer out = new DataOutputSerializer(32);
    SimpleVersionedSerialization.writeVersionAndSerialize(emptySerializer, "abc", out);
    final byte[] outBytes = out.getCopyOfBuffer();
    final byte[] bytes = SimpleVersionedSerialization.writeVersionAndSerialize(emptySerializer, "abc");
    assertArrayEquals(bytes, outBytes);
    final DataInputDeserializer in = new DataInputDeserializer(bytes);
    final String deserialized = SimpleVersionedSerialization.readVersionAndDeSerialize(emptySerializer, in);
    final String deserializedFromBytes = SimpleVersionedSerialization.readVersionAndDeSerialize(emptySerializer, outBytes);
    assertEquals(testString, deserialized);
    assertEquals(testString, deserializedFromBytes);
}
Also used : DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) DataInputDeserializer(org.apache.flink.core.memory.DataInputDeserializer) Test(org.junit.Test)

Aggregations

DataOutputSerializer (org.apache.flink.core.memory.DataOutputSerializer)63 DataInputDeserializer (org.apache.flink.core.memory.DataInputDeserializer)15 Test (org.junit.Test)15 IOException (java.io.IOException)10 ByteBuffer (java.nio.ByteBuffer)6 List (java.util.List)4 IntSerializer (org.apache.flink.api.common.typeutils.base.IntSerializer)4 StringSerializer (org.apache.flink.api.common.typeutils.base.StringSerializer)4 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)4 ArrayList (java.util.ArrayList)3 IntStream (java.util.stream.IntStream)3 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)3 TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)3 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Stream (java.util.stream.Stream)2 ValueState (org.apache.flink.api.common.state.ValueState)2 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)2