Search in sources :

Example 46 with MemorySegment

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

the class SpillingBuffer method nextSegment.

@Override
protected MemorySegment nextSegment(MemorySegment current, int positionInCurrent) throws IOException {
    // check if we are still in memory
    if (this.writer == null) {
        this.fullSegments.add(current);
        final MemorySegment nextSeg = this.memorySource.nextSegment();
        if (nextSeg != null) {
            return nextSeg;
        } else {
            // out of memory, need to spill: create a writer
            this.writer = this.ioManager.createBlockChannelWriter(this.ioManager.createChannel());
            // add all segments to the writer
            this.blockCount = this.fullSegments.size();
            this.numMemorySegmentsInWriter = this.blockCount;
            for (int i = 0; i < this.fullSegments.size(); i++) {
                this.writer.writeBlock(this.fullSegments.get(i));
            }
            this.fullSegments.clear();
            final MemorySegment seg = this.writer.getNextReturnedBlock();
            this.numMemorySegmentsInWriter--;
            return seg;
        }
    } else {
        // spilling
        this.writer.writeBlock(current);
        this.blockCount++;
        return this.writer.getNextReturnedBlock();
    }
}
Also used : MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 47 with MemorySegment

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

the class ChannelWriterOutputView method close.

// --------------------------------------------------------------------------------------------
/**
 * Closes this OutputView, closing the underlying writer and returning all memory segments.
 *
 * @return A list containing all memory segments originally supplied to this view.
 * @throws IOException Thrown, if the underlying writer could not be properly closed.
 */
public List<MemorySegment> close() throws IOException {
    // send off set last segment
    writeSegment(getCurrentSegment(), getCurrentPositionInSegment(), true);
    clear();
    // close the writer and gather all segments
    final LinkedBlockingQueue<MemorySegment> queue = this.writer.getReturnQueue();
    this.writer.close();
    // re-collect all memory segments
    ArrayList<MemorySegment> list = new ArrayList<MemorySegment>(this.numSegments);
    for (int i = 0; i < this.numSegments; i++) {
        final MemorySegment m = queue.poll();
        if (m == null) {
            // properly closed.
            throw new RuntimeException("ChannelWriterOutputView: MemorySegments have been taken from return queue by different actor.");
        }
        list.add(m);
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 48 with MemorySegment

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

the class BufferDecompressor method decompressToOriginalBuffer.

/**
 * The difference between this method and {@link #decompressToIntermediateBuffer(Buffer)} is
 * that this method copies the decompressed data to the input {@link Buffer} starting from
 * offset 0.
 *
 * <p>The caller must guarantee that the input {@link Buffer} is writable and there's enough
 * space left.
 */
@VisibleForTesting
public Buffer decompressToOriginalBuffer(Buffer buffer) {
    int decompressedLen = decompress(buffer);
    // copy the decompressed data back
    int memorySegmentOffset = buffer.getMemorySegmentOffset();
    MemorySegment segment = buffer.getMemorySegment();
    segment.put(memorySegmentOffset, internalBuffer.array(), 0, decompressedLen);
    return new ReadOnlySlicedNetworkBuffer(buffer.asByteBuf(), 0, decompressedLen, memorySegmentOffset, false);
}
Also used : MemorySegment(org.apache.flink.core.memory.MemorySegment) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting)

Example 49 with MemorySegment

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

the class NonSpanningWrapper method getUnconsumedSegment.

CloseableIterator<Buffer> getUnconsumedSegment() {
    if (!hasRemaining()) {
        return CloseableIterator.empty();
    }
    MemorySegment segment = MemorySegmentFactory.allocateUnpooledSegment(remaining());
    this.segment.copyTo(position, segment, 0, remaining());
    return singleBufferIterator(segment);
}
Also used : MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 50 with MemorySegment

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

the class BufferReaderWriterUtil method sliceNextBuffer.

@Nullable
static Buffer sliceNextBuffer(ByteBuffer memory) {
    final int remaining = memory.remaining();
    // buffer underflow exceptions which will cause the read to fail.
    if (remaining == 0) {
        return null;
    }
    final boolean isEvent = memory.getShort() == HEADER_VALUE_IS_EVENT;
    final boolean isCompressed = memory.getShort() == BUFFER_IS_COMPRESSED;
    final int size = memory.getInt();
    memory.limit(memory.position() + size);
    ByteBuffer buf = memory.slice();
    memory.position(memory.limit());
    memory.limit(memory.capacity());
    MemorySegment memorySegment = MemorySegmentFactory.wrapOffHeapMemory(buf);
    Buffer.DataType dataType = isEvent ? Buffer.DataType.EVENT_BUFFER : Buffer.DataType.DATA_BUFFER;
    return new NetworkBuffer(memorySegment, FreeingBufferRecycler.INSTANCE, dataType, isCompressed, size);
}
Also used : FileRegionBuffer(org.apache.flink.runtime.io.network.buffer.FileRegionBuffer) Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) ByteBuffer(java.nio.ByteBuffer) NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) ByteBuffer(java.nio.ByteBuffer) MemorySegment(org.apache.flink.core.memory.MemorySegment) Nullable(javax.annotation.Nullable)

Aggregations

MemorySegment (org.apache.flink.core.memory.MemorySegment)375 Test (org.junit.Test)136 ArrayList (java.util.ArrayList)52 DummyInvokable (org.apache.flink.runtime.operators.testutils.DummyInvokable)44 IOException (java.io.IOException)37 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)29 Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)26 NetworkBuffer (org.apache.flink.runtime.io.network.buffer.NetworkBuffer)25 MemoryAllocationException (org.apache.flink.runtime.memory.MemoryAllocationException)24 IntPair (org.apache.flink.runtime.operators.testutils.types.IntPair)24 FileIOChannel (org.apache.flink.runtime.io.disk.iomanager.FileIOChannel)20 EOFException (java.io.EOFException)18 ByteBuffer (java.nio.ByteBuffer)18 AbstractInvokable (org.apache.flink.runtime.jobgraph.tasks.AbstractInvokable)18 TestData (org.apache.flink.runtime.operators.testutils.TestData)18 Random (java.util.Random)16 UniformIntPairGenerator (org.apache.flink.runtime.operators.testutils.UniformIntPairGenerator)16 Chunk (org.apache.flink.runtime.state.heap.space.Chunk)15 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)15 IOManagerAsync (org.apache.flink.runtime.io.disk.iomanager.IOManagerAsync)14