Search in sources :

Example 1 with SerializationTestType

use of org.apache.flink.runtime.io.network.api.serialization.types.SerializationTestType in project flink by apache.

the class LargeRecordsTest method testHandleMixedLargeRecords.

@Test
public void testHandleMixedLargeRecords() {
    try {
        final int NUM_RECORDS = 99;
        final int SEGMENT_SIZE = 32 * 1024;
        final RecordSerializer<SerializationTestType> serializer = new SpanningRecordSerializer<SerializationTestType>();
        final RecordDeserializer<SerializationTestType> deserializer = new AdaptiveSpanningRecordDeserializer<SerializationTestType>();
        final Buffer buffer = new Buffer(MemorySegmentFactory.allocateUnpooledSegment(SEGMENT_SIZE), mock(BufferRecycler.class));
        List<SerializationTestType> originalRecords = new ArrayList<SerializationTestType>((NUM_RECORDS + 1) / 2);
        List<SerializationTestType> deserializedRecords = new ArrayList<SerializationTestType>((NUM_RECORDS + 1) / 2);
        LargeObjectType genLarge = new LargeObjectType();
        Random rnd = new Random();
        for (int i = 0; i < NUM_RECORDS; i++) {
            if (i % 2 == 0) {
                originalRecords.add(new IntType(42));
                deserializedRecords.add(new IntType());
            } else {
                originalRecords.add(genLarge.getRandom(rnd));
                deserializedRecords.add(new LargeObjectType());
            }
        }
        // -------------------------------------------------------------------------------------------------------------
        serializer.setNextBuffer(buffer);
        int numRecordsDeserialized = 0;
        for (SerializationTestType record : originalRecords) {
            // serialize record
            if (serializer.addRecord(record).isFullBuffer()) {
                // buffer is full => move to deserializer
                deserializer.setNextMemorySegment(serializer.getCurrentBuffer().getMemorySegment(), SEGMENT_SIZE);
                // deserialize records, as many complete as there are
                while (numRecordsDeserialized < deserializedRecords.size()) {
                    SerializationTestType next = deserializedRecords.get(numRecordsDeserialized);
                    if (deserializer.getNextRecord(next).isFullRecord()) {
                        assertEquals(originalRecords.get(numRecordsDeserialized), next);
                        numRecordsDeserialized++;
                    } else {
                        break;
                    }
                }
                // move buffers as long as necessary (for long records)
                while (serializer.setNextBuffer(buffer).isFullBuffer()) {
                    deserializer.setNextMemorySegment(serializer.getCurrentBuffer().getMemorySegment(), SEGMENT_SIZE);
                }
                // deserialize records, as many as there are in the last buffer
                while (numRecordsDeserialized < deserializedRecords.size()) {
                    SerializationTestType next = deserializedRecords.get(numRecordsDeserialized);
                    if (deserializer.getNextRecord(next).isFullRecord()) {
                        assertEquals(originalRecords.get(numRecordsDeserialized), next);
                        numRecordsDeserialized++;
                    } else {
                        break;
                    }
                }
            }
        }
        // move the last (incomplete buffer)
        Buffer last = serializer.getCurrentBuffer();
        deserializer.setNextMemorySegment(last.getMemorySegment(), last.getSize());
        serializer.clear();
        // deserialize records, as many as there are in the last buffer
        while (numRecordsDeserialized < deserializedRecords.size()) {
            SerializationTestType next = deserializedRecords.get(numRecordsDeserialized);
            assertTrue(deserializer.getNextRecord(next).isFullRecord());
            assertEquals(originalRecords.get(numRecordsDeserialized), next);
            numRecordsDeserialized++;
        }
        // might be that the last big records has not yet been fully moved, and a small one is missing
        assertFalse(serializer.hasData());
        assertFalse(deserializer.hasUnfinishedData());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) SpillingAdaptiveSpanningRecordDeserializer(org.apache.flink.runtime.io.network.api.serialization.SpillingAdaptiveSpanningRecordDeserializer) AdaptiveSpanningRecordDeserializer(org.apache.flink.runtime.io.network.api.serialization.AdaptiveSpanningRecordDeserializer) ArrayList(java.util.ArrayList) SpanningRecordSerializer(org.apache.flink.runtime.io.network.api.serialization.SpanningRecordSerializer) IntType(org.apache.flink.runtime.io.network.api.serialization.types.IntType) SerializationTestType(org.apache.flink.runtime.io.network.api.serialization.types.SerializationTestType) Random(java.util.Random) LargeObjectType(org.apache.flink.runtime.io.network.serialization.types.LargeObjectType) BufferRecycler(org.apache.flink.runtime.io.network.buffer.BufferRecycler) Test(org.junit.Test)

Example 2 with SerializationTestType

use of org.apache.flink.runtime.io.network.api.serialization.types.SerializationTestType in project flink by apache.

the class PagedViewsTest method testSequenceOfTypes.

private static void testSequenceOfTypes(Iterable<SerializationTestType> sequence, int segmentSize) throws Exception {
    List<SerializationTestType> elements = new ArrayList<SerializationTestType>(512);
    TestOutputView outView = new TestOutputView(segmentSize);
    // write
    for (SerializationTestType type : sequence) {
        // serialize the record
        type.write(outView);
        elements.add(type);
    }
    outView.close();
    // check the records
    TestInputView inView = new TestInputView(outView.segments);
    for (SerializationTestType reference : elements) {
        SerializationTestType result = reference.getClass().newInstance();
        result.read(inView);
        assertEquals(reference, result);
    }
}
Also used : SerializationTestType(org.apache.flink.runtime.io.network.api.serialization.types.SerializationTestType) ArrayList(java.util.ArrayList)

Example 3 with SerializationTestType

use of org.apache.flink.runtime.io.network.api.serialization.types.SerializationTestType in project flink by apache.

the class SpanningRecordSerializationTest method test.

/**
	 * Iterates over the provided records and tests whether {@link SpanningRecordSerializer} and {@link AdaptiveSpanningRecordDeserializer}
	 * interact as expected.
	 * <p>
	 * Only a single {@link MemorySegment} will be allocated.
	 *
	 * @param records records to test
	 * @param segmentSize size for the {@link MemorySegment}
	 */
private void test(Util.MockRecords records, int segmentSize, RecordSerializer<SerializationTestType> serializer, RecordDeserializer<SerializationTestType> deserializer) throws Exception {
    // length encoding
    final int SERIALIZATION_OVERHEAD = 4;
    final Buffer buffer = new Buffer(MemorySegmentFactory.allocateUnpooledSegment(segmentSize), mock(BufferRecycler.class));
    final ArrayDeque<SerializationTestType> serializedRecords = new ArrayDeque<SerializationTestType>();
    // -------------------------------------------------------------------------------------------------------------
    serializer.setNextBuffer(buffer);
    int numBytes = 0;
    int numRecords = 0;
    for (SerializationTestType record : records) {
        serializedRecords.add(record);
        numRecords++;
        numBytes += record.length() + SERIALIZATION_OVERHEAD;
        // serialize record
        if (serializer.addRecord(record).isFullBuffer()) {
            // buffer is full => start deserializing
            deserializer.setNextMemorySegment(serializer.getCurrentBuffer().getMemorySegment(), segmentSize);
            while (!serializedRecords.isEmpty()) {
                SerializationTestType expected = serializedRecords.poll();
                SerializationTestType actual = expected.getClass().newInstance();
                if (deserializer.getNextRecord(actual).isFullRecord()) {
                    Assert.assertEquals(expected, actual);
                    numRecords--;
                } else {
                    serializedRecords.addFirst(expected);
                    break;
                }
            }
            while (serializer.setNextBuffer(buffer).isFullBuffer()) {
                deserializer.setNextMemorySegment(serializer.getCurrentBuffer().getMemorySegment(), segmentSize);
            }
        }
    }
    // deserialize left over records
    deserializer.setNextMemorySegment(serializer.getCurrentBuffer().getMemorySegment(), (numBytes % segmentSize));
    serializer.clear();
    while (!serializedRecords.isEmpty()) {
        SerializationTestType expected = serializedRecords.poll();
        SerializationTestType actual = expected.getClass().newInstance();
        RecordDeserializer.DeserializationResult result = deserializer.getNextRecord(actual);
        Assert.assertTrue(result.isFullRecord());
        Assert.assertEquals(expected, actual);
        numRecords--;
    }
    // assert that all records have been serialized and deserialized
    Assert.assertEquals(0, numRecords);
    Assert.assertFalse(serializer.hasData());
    Assert.assertFalse(deserializer.hasUnfinishedData());
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) SerializationTestType(org.apache.flink.runtime.io.network.api.serialization.types.SerializationTestType) BufferRecycler(org.apache.flink.runtime.io.network.buffer.BufferRecycler) ArrayDeque(java.util.ArrayDeque)

Example 4 with SerializationTestType

use of org.apache.flink.runtime.io.network.api.serialization.types.SerializationTestType in project flink by apache.

the class SpanningRecordSerializerTest method test.

// -----------------------------------------------------------------------------------------------------------------
/**
	 * Iterates over the provided records and tests whether the {@link SpanningRecordSerializer} returns the expected
	 * {@link RecordSerializer.SerializationResult} values.
	 * <p>
	 * Only a single {@link MemorySegment} will be allocated.
	 *
	 * @param records records to test
	 * @param segmentSize size for the {@link MemorySegment}
	 */
private void test(Util.MockRecords records, int segmentSize) throws Exception {
    // length encoding
    final int SERIALIZATION_OVERHEAD = 4;
    final SpanningRecordSerializer<SerializationTestType> serializer = new SpanningRecordSerializer<SerializationTestType>();
    final Buffer buffer = new Buffer(MemorySegmentFactory.allocateUnpooledSegment(segmentSize), mock(BufferRecycler.class));
    // -------------------------------------------------------------------------------------------------------------
    serializer.setNextBuffer(buffer);
    int numBytes = 0;
    for (SerializationTestType record : records) {
        RecordSerializer.SerializationResult result = serializer.addRecord(record);
        numBytes += record.length() + SERIALIZATION_OVERHEAD;
        if (numBytes < segmentSize) {
            Assert.assertEquals(RecordSerializer.SerializationResult.FULL_RECORD, result);
        } else if (numBytes == segmentSize) {
            Assert.assertEquals(RecordSerializer.SerializationResult.FULL_RECORD_MEMORY_SEGMENT_FULL, result);
            serializer.setNextBuffer(buffer);
            numBytes = 0;
        } else {
            Assert.assertEquals(RecordSerializer.SerializationResult.PARTIAL_RECORD_MEMORY_SEGMENT_FULL, result);
            while (result.isFullBuffer()) {
                numBytes -= segmentSize;
                result = serializer.setNextBuffer(buffer);
            }
        }
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) SerializationTestType(org.apache.flink.runtime.io.network.api.serialization.types.SerializationTestType) BufferRecycler(org.apache.flink.runtime.io.network.buffer.BufferRecycler)

Example 5 with SerializationTestType

use of org.apache.flink.runtime.io.network.api.serialization.types.SerializationTestType in project flink by apache.

the class DataInputOutputSerializerTest method testWrapAsByteBuffer.

@Test
public void testWrapAsByteBuffer() {
    SerializationTestType randomInt = Util.randomRecord(SerializationTestTypeFactory.INT);
    DataOutputSerializer serializer = new DataOutputSerializer(randomInt.length());
    MemorySegment segment = MemorySegmentFactory.allocateUnpooledSegment(randomInt.length());
    try {
        // empty buffer, read buffer should be empty
        ByteBuffer wrapper = serializer.wrapAsByteBuffer();
        Assert.assertEquals(0, wrapper.position());
        Assert.assertEquals(0, wrapper.limit());
        // write to data output, read buffer should still be empty
        randomInt.write(serializer);
        Assert.assertEquals(0, wrapper.position());
        Assert.assertEquals(0, wrapper.limit());
        // get updated read buffer, read buffer should contain written data
        wrapper = serializer.wrapAsByteBuffer();
        Assert.assertEquals(0, wrapper.position());
        Assert.assertEquals(randomInt.length(), wrapper.limit());
        // clear data output, read buffer should still contain written data
        serializer.clear();
        Assert.assertEquals(0, wrapper.position());
        Assert.assertEquals(randomInt.length(), wrapper.limit());
        // get updated read buffer, should be empty
        wrapper = serializer.wrapAsByteBuffer();
        Assert.assertEquals(0, wrapper.position());
        Assert.assertEquals(0, wrapper.limit());
        // write to data output and read back to memory
        randomInt.write(serializer);
        wrapper = serializer.wrapAsByteBuffer();
        segment.put(0, wrapper, randomInt.length());
        Assert.assertEquals(randomInt.length(), wrapper.position());
        Assert.assertEquals(randomInt.length(), wrapper.limit());
    } catch (IOException e) {
        e.printStackTrace();
        Assert.fail("Test encountered an unexpected exception.");
    }
}
Also used : SerializationTestType(org.apache.flink.runtime.io.network.api.serialization.types.SerializationTestType) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) MemorySegment(org.apache.flink.core.memory.MemorySegment) Test(org.junit.Test)

Aggregations

SerializationTestType (org.apache.flink.runtime.io.network.api.serialization.types.SerializationTestType)9 Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)6 BufferRecycler (org.apache.flink.runtime.io.network.buffer.BufferRecycler)6 Test (org.junit.Test)6 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)3 Random (java.util.Random)3 ArrayDeque (java.util.ArrayDeque)2 SpanningRecordSerializer (org.apache.flink.runtime.io.network.api.serialization.SpanningRecordSerializer)2 SpillingAdaptiveSpanningRecordDeserializer (org.apache.flink.runtime.io.network.api.serialization.SpillingAdaptiveSpanningRecordDeserializer)2 IntType (org.apache.flink.runtime.io.network.api.serialization.types.IntType)2 LargeObjectType (org.apache.flink.runtime.io.network.serialization.types.LargeObjectType)2 ByteBuffer (java.nio.ByteBuffer)1 DataInputView (org.apache.flink.core.memory.DataInputView)1 DataOutputView (org.apache.flink.core.memory.DataOutputView)1 MemorySegment (org.apache.flink.core.memory.MemorySegment)1 AdaptiveSpanningRecordDeserializer (org.apache.flink.runtime.io.network.api.serialization.AdaptiveSpanningRecordDeserializer)1