Search in sources :

Example 11 with DataInputView

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

the class FromElementsFunction method run.

@Override
public void run(SourceContext<T> ctx) throws Exception {
    Preconditions.checkState(serializer != null, "serializer not configured");
    ByteArrayInputStream bais = new ByteArrayInputStream(elementsSerialized);
    final DataInputView input = new DataInputViewStreamWrapper(bais);
    // if we are restored from a checkpoint and need to skip elements, skip them now.
    int toSkip = numElementsToSkip;
    if (toSkip > 0) {
        try {
            while (toSkip > 0) {
                serializer.deserialize(input);
                toSkip--;
            }
        } catch (Exception e) {
            throw new IOException("Failed to deserialize an element from the source. " + "If you are using user-defined serialization (Value and Writable types), check the " + "serialization functions.\nSerializer is " + serializer, e);
        }
        this.numElementsEmitted = this.numElementsToSkip;
    }
    final Object lock = ctx.getCheckpointLock();
    while (isRunning && numElementsEmitted < numElements) {
        T next;
        try {
            next = serializer.deserialize(input);
        } catch (Exception e) {
            throw new IOException("Failed to deserialize an element from the source. " + "If you are using user-defined serialization (Value and Writable types), check the " + "serialization functions.\nSerializer is " + serializer, e);
        }
        synchronized (lock) {
            ctx.collect(next);
            numElementsEmitted++;
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputView(org.apache.flink.core.memory.DataInputView) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 12 with DataInputView

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

the class DefaultOperatorStateBackend method deserializeStateValues.

private static <S> void deserializeStateValues(PartitionableListState<S> stateListForName, FSDataInputStream in, OperatorStateHandle.StateMetaInfo metaInfo) throws IOException {
    if (null != metaInfo) {
        long[] offsets = metaInfo.getOffsets();
        if (null != offsets) {
            DataInputView div = new DataInputViewStreamWrapper(in);
            TypeSerializer<S> serializer = stateListForName.getPartitionStateSerializer();
            for (long offset : offsets) {
                in.seek(offset);
                stateListForName.add(serializer.deserialize(div));
            }
        }
    }
}
Also used : DataInputView(org.apache.flink.core.memory.DataInputView) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper)

Example 13 with DataInputView

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

the class SpanningRecordSerializerTest method testEmptyRecords.

@Test
public void testEmptyRecords() {
    final int SEGMENT_SIZE = 11;
    final SpanningRecordSerializer<SerializationTestType> serializer = new SpanningRecordSerializer<SerializationTestType>();
    final Buffer buffer = new Buffer(MemorySegmentFactory.allocateUnpooledSegment(SEGMENT_SIZE), mock(BufferRecycler.class));
    try {
        Assert.assertEquals(RecordSerializer.SerializationResult.FULL_RECORD, serializer.setNextBuffer(buffer));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        SerializationTestType emptyRecord = new SerializationTestType() {

            @Override
            public SerializationTestType getRandom(Random rnd) {
                throw new UnsupportedOperationException();
            }

            @Override
            public int length() {
                throw new UnsupportedOperationException();
            }

            @Override
            public void write(DataOutputView out) {
            }

            @Override
            public void read(DataInputView in) {
            }

            @Override
            public int hashCode() {
                throw new UnsupportedOperationException();
            }

            @Override
            public boolean equals(Object obj) {
                throw new UnsupportedOperationException();
            }
        };
        RecordSerializer.SerializationResult result = serializer.addRecord(emptyRecord);
        Assert.assertEquals(RecordSerializer.SerializationResult.FULL_RECORD, result);
        result = serializer.addRecord(emptyRecord);
        Assert.assertEquals(RecordSerializer.SerializationResult.FULL_RECORD, result);
        result = serializer.addRecord(emptyRecord);
        Assert.assertEquals(RecordSerializer.SerializationResult.PARTIAL_RECORD_MEMORY_SEGMENT_FULL, result);
        result = serializer.setNextBuffer(buffer);
        Assert.assertEquals(RecordSerializer.SerializationResult.FULL_RECORD, result);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) DataInputView(org.apache.flink.core.memory.DataInputView) DataOutputView(org.apache.flink.core.memory.DataOutputView) IOException(java.io.IOException) IOException(java.io.IOException) SerializationTestType(org.apache.flink.runtime.io.network.api.serialization.types.SerializationTestType) Random(java.util.Random) BufferRecycler(org.apache.flink.runtime.io.network.buffer.BufferRecycler) Test(org.junit.Test)

Example 14 with DataInputView

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

the class PostVersionedIOReadableWritable method read.

/**
 * This read attempts to first identify if the input view contains the special {@link
 * #VERSIONED_IDENTIFIER} by reading and buffering the first few bytes. If identified to be
 * versioned, the usual version resolution read path in {@link
 * VersionedIOReadableWritable#read(DataInputView)} is invoked. Otherwise, we "reset" the input
 * stream by pushing back the read buffered bytes into the stream.
 */
public final void read(InputStream inputStream) throws IOException {
    byte[] tmp = new byte[VERSIONED_IDENTIFIER.length];
    int totalRead = IOUtils.tryReadFully(inputStream, tmp);
    if (Arrays.equals(tmp, VERSIONED_IDENTIFIER)) {
        DataInputView inputView = new DataInputViewStreamWrapper(inputStream);
        super.read(inputView);
        read(inputView, true);
    } else {
        InputStream streamToRead = inputStream;
        if (totalRead > 0) {
            PushbackInputStream resetStream = new PushbackInputStream(inputStream, totalRead);
            resetStream.unread(tmp, 0, totalRead);
            streamToRead = resetStream;
        }
        read(new DataInputViewStreamWrapper(streamToRead), false);
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) DataInputView(org.apache.flink.core.memory.DataInputView) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper)

Example 15 with DataInputView

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

the class TypeSerializerUpgradeTestBase method assertSerializerIsValid.

/**
 * Asserts that a given {@link TypeSerializer} is valid, given a {@link DataInputView} of
 * serialized data.
 *
 * <p>A serializer is valid, iff:
 *
 * <ul>
 *   <li>1. The serializer can read and then write again the given serialized data.
 *   <li>2. The serializer can produce a serializer snapshot which can be written and then read
 *       back again.
 *   <li>3. The serializer's produced snapshot is capable of creating a restore serializer.
 *   <li>4. The restore serializer created from the serializer snapshot can read and then write
 *       again data written by step 1. Given that the serializer is not a restore serializer
 *       already.
 * </ul>
 */
private static <T> void assertSerializerIsValid(TypeSerializer<T> serializer, DataInputView dataInput, Matcher<T> testDataMatcher) throws Exception {
    DataInputView serializedData = readAndThenWriteData(dataInput, serializer, serializer, testDataMatcher);
    TypeSerializerSnapshot<T> snapshot = writeAndThenReadSerializerSnapshot(serializer);
    TypeSerializer<T> restoreSerializer = snapshot.restoreSerializer();
    serializedData = readAndThenWriteData(serializedData, restoreSerializer, restoreSerializer, testDataMatcher);
    TypeSerializer<T> duplicateSerializer = snapshot.restoreSerializer().duplicate();
    readAndThenWriteData(serializedData, duplicateSerializer, duplicateSerializer, testDataMatcher);
}
Also used : DataInputView(org.apache.flink.core.memory.DataInputView)

Aggregations

DataInputView (org.apache.flink.core.memory.DataInputView)28 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)17 Test (org.junit.Test)13 FSDataInputStream (org.apache.flink.core.fs.FSDataInputStream)8 InputStream (java.io.InputStream)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)4 DataOutputView (org.apache.flink.core.memory.DataOutputView)4 MemorySegment (org.apache.flink.core.memory.MemorySegment)4 ListMemorySegmentSource (org.apache.flink.runtime.memory.ListMemorySegmentSource)4 TestData (org.apache.flink.runtime.operators.testutils.TestData)4 KeyGroupStatePartitionStreamProvider (org.apache.flink.runtime.state.KeyGroupStatePartitionStreamProvider)4 EOFException (java.io.EOFException)3 DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 ByteArrayInputStreamWithPos (org.apache.flink.core.memory.ByteArrayInputStreamWithPos)2 DataInputDeserializer (org.apache.flink.core.memory.DataInputDeserializer)2