Search in sources :

Example 21 with DataInputViewStreamWrapper

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

the class ContinuousFileReaderOperator method restoreState.

// ------------------------------------------------------------------------
//  Restoring / Migrating from an older Flink version.
// ------------------------------------------------------------------------
@Override
public void restoreState(FSDataInputStream in) throws Exception {
    LOG.info("{} (taskIdx={}) restoring state from an older Flink version.", getClass().getSimpleName(), getRuntimeContext().getIndexOfThisSubtask());
    // this is just to read the byte indicating if we have udf state or not
    int hasUdfState = in.read();
    Preconditions.checkArgument(hasUdfState == 0);
    final ObjectInputStream ois = new ObjectInputStream(in);
    final DataInputViewStreamWrapper div = new DataInputViewStreamWrapper(in);
    // read the split that was being read
    FileInputSplit currSplit = (FileInputSplit) ois.readObject();
    // read the pending splits list
    List<FileInputSplit> pendingSplits = new LinkedList<>();
    int noOfSplits = div.readInt();
    for (int i = 0; i < noOfSplits; i++) {
        FileInputSplit split = (FileInputSplit) ois.readObject();
        pendingSplits.add(split);
    }
    // read the state of the format
    Serializable formatState = (Serializable) ois.readObject();
    div.close();
    if (restoredReaderState == null) {
        restoredReaderState = new ArrayList<>();
    }
    // we do not know the modification time of the retrieved splits, so we assign them
    // artificial ones, with the only constraint that they respect the relative order of the
    // retrieved splits, because modification time is going to be used to sort the splits within
    // the "pending splits" priority queue.
    long now = getProcessingTimeService().getCurrentProcessingTime();
    long runningModTime = Math.max(now, noOfSplits + 1);
    TimestampedFileInputSplit currentSplit = createTimestampedFileSplit(currSplit, --runningModTime, formatState);
    restoredReaderState.add(currentSplit);
    for (FileInputSplit split : pendingSplits) {
        TimestampedFileInputSplit timestampedSplit = createTimestampedFileSplit(split, --runningModTime);
        restoredReaderState.add(timestampedSplit);
    }
    if (LOG.isDebugEnabled()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("{} (taskIdx={}) restored {} splits from legacy: {}.", getClass().getSimpleName(), getRuntimeContext().getIndexOfThisSubtask(), restoredReaderState.size(), restoredReaderState);
        }
    }
}
Also used : FileInputSplit(org.apache.flink.core.fs.FileInputSplit) Serializable(java.io.Serializable) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) LinkedList(java.util.LinkedList) ObjectInputStream(java.io.ObjectInputStream)

Example 22 with DataInputViewStreamWrapper

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

the class FromElementsFunction method run.

@Override
public void run(SourceContext<T> ctx) throws Exception {
    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);
        }
        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);
        }
        synchronized (lock) {
            ctx.collect(next);
            numElementsEmitted++;
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputView(org.apache.flink.core.memory.DataInputView) IOException(java.io.IOException) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) IOException(java.io.IOException)

Example 23 with DataInputViewStreamWrapper

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

the class FoldApplyAllWindowFunction method open.

@Override
public void open(Configuration configuration) throws Exception {
    super.open(configuration);
    if (accSerializer == null) {
        throw new RuntimeException("No serializer set for the fold accumulator type. " + "Probably the setOutputType method was not called.");
    }
    if (serializedInitialValue == null) {
        throw new RuntimeException("No initial value was serialized for the fold " + "window function. Probably the setOutputType method was not called.");
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(serializedInitialValue);
    DataInputViewStreamWrapper in = new DataInputViewStreamWrapper(bais);
    initialValue = accSerializer.deserialize(in);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper)

Example 24 with DataInputViewStreamWrapper

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

the class FoldApplyProcessAllWindowFunction method open.

@Override
public void open(Configuration configuration) throws Exception {
    FunctionUtils.openFunction(this.windowFunction, configuration);
    if (serializedInitialValue == null) {
        throw new RuntimeException("No initial value was serialized for the fold " + "window function. Probably the setOutputType method was not called.");
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(serializedInitialValue);
    DataInputViewStreamWrapper in = new DataInputViewStreamWrapper(bais);
    initialValue = accSerializer.deserialize(in);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper)

Example 25 with DataInputViewStreamWrapper

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

the class FoldApplyWindowFunction method open.

@Override
public void open(Configuration configuration) throws Exception {
    super.open(configuration);
    if (accSerializer == null) {
        throw new RuntimeException("No serializer set for the fold accumulator type. " + "Probably the setOutputType method was not called.");
    }
    if (serializedInitialValue == null) {
        throw new RuntimeException("No initial value was serialized for the fold " + "window function. Probably the setOutputType method was not called.");
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(serializedInitialValue);
    DataInputViewStreamWrapper in = new DataInputViewStreamWrapper(bais);
    initialValue = accSerializer.deserialize(in);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper)

Aggregations

DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)74 DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)25 ByteArrayInputStream (java.io.ByteArrayInputStream)23 IOException (java.io.IOException)21 ByteArrayInputStreamWithPos (org.apache.flink.core.memory.ByteArrayInputStreamWithPos)19 Test (org.junit.Test)19 FSDataInputStream (org.apache.flink.core.fs.FSDataInputStream)13 ByteArrayOutputStreamWithPos (org.apache.flink.core.memory.ByteArrayOutputStreamWithPos)11 DataInputView (org.apache.flink.core.memory.DataInputView)11 RocksDBException (org.rocksdb.RocksDBException)10 KeyGroupStatePartitionStreamProvider (org.apache.flink.runtime.state.KeyGroupStatePartitionStreamProvider)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 ArrayList (java.util.ArrayList)6 InputStream (java.io.InputStream)4 PipedInputStream (java.io.PipedInputStream)4 PipedOutputStream (java.io.PipedOutputStream)4 EOFException (java.io.EOFException)3 ObjectInputStream (java.io.ObjectInputStream)3 HashMap (java.util.HashMap)3 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)3