Search in sources :

Example 56 with BufferBuilder

use of org.apache.flink.runtime.io.network.buffer.BufferBuilder in project flink by apache.

the class SpanningRecordSerializationTest method appendLeftOverBytes.

private static Buffer appendLeftOverBytes(Buffer buffer, byte[] leftOverBytes) {
    try (BufferBuilder bufferBuilder = new BufferBuilder(MemorySegmentFactory.allocateUnpooledSegment(buffer.readableBytes() + leftOverBytes.length), FreeingBufferRecycler.INSTANCE)) {
        try (BufferConsumer bufferConsumer = bufferBuilder.createBufferConsumer()) {
            bufferBuilder.append(buffer.getNioBufferReadable());
            bufferBuilder.appendAndCommit(ByteBuffer.wrap(leftOverBytes));
            return bufferConsumer.build();
        }
    }
}
Also used : BufferBuilder(org.apache.flink.runtime.io.network.buffer.BufferBuilder) BufferBuilderTestUtils.createFilledBufferBuilder(org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils.createFilledBufferBuilder) BufferConsumer(org.apache.flink.runtime.io.network.buffer.BufferConsumer)

Example 57 with BufferBuilder

use of org.apache.flink.runtime.io.network.buffer.BufferBuilder in project flink by splunk.

the class SpanningRecordSerializationTest method setNextBufferForSerializer.

private static BufferAndSerializerResult setNextBufferForSerializer(ByteBuffer serializedRecord, int segmentSize) throws IOException {
    // create a bufferBuilder with some random starting offset to properly test handling buffer
    // slices in the
    // deserialization code.
    int startingOffset = segmentSize > 2 ? RANDOM.nextInt(segmentSize / 2) : 0;
    BufferBuilder bufferBuilder = createFilledBufferBuilder(segmentSize + startingOffset, startingOffset);
    BufferConsumer bufferConsumer = bufferBuilder.createBufferConsumer();
    bufferConsumer.build().recycleBuffer();
    // Closing the BufferBuilder here just allow to be sure that Buffer will be recovered when
    // BufferConsumer will be closed.
    bufferBuilder.close();
    bufferBuilder.appendAndCommit(serializedRecord);
    return new BufferAndSerializerResult(bufferBuilder, bufferConsumer, bufferBuilder.isFull(), !serializedRecord.hasRemaining());
}
Also used : BufferBuilder(org.apache.flink.runtime.io.network.buffer.BufferBuilder) BufferBuilderTestUtils.createFilledBufferBuilder(org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils.createFilledBufferBuilder) BufferConsumer(org.apache.flink.runtime.io.network.buffer.BufferConsumer)

Example 58 with BufferBuilder

use of org.apache.flink.runtime.io.network.buffer.BufferBuilder in project flink by splunk.

the class HashBasedDataBuffer method finish.

@Override
public void finish() {
    checkState(!isFull, "DataBuffer must not be full.");
    checkState(!isFinished, "DataBuffer is already finished.");
    isFull = true;
    isFinished = true;
    for (int channel = 0; channel < builders.length; ++channel) {
        BufferBuilder builder = builders[channel];
        if (builder != null) {
            builder.finish();
            buffers[channel].add(builder.createBufferConsumerFromBeginning());
            builder.close();
            builders[channel] = null;
        }
    }
}
Also used : BufferBuilder(org.apache.flink.runtime.io.network.buffer.BufferBuilder)

Example 59 with BufferBuilder

use of org.apache.flink.runtime.io.network.buffer.BufferBuilder in project flink by splunk.

the class BufferWritingResultPartition method appendUnicastDataForRecordContinuation.

private BufferBuilder appendUnicastDataForRecordContinuation(final ByteBuffer remainingRecordBytes, final int targetSubpartition) throws IOException {
    final BufferBuilder buffer = requestNewUnicastBufferBuilder(targetSubpartition);
    // !! Be aware, in case of partialRecordBytes != 0, partial length and data has to
    // `appendAndCommit` first
    // before consumer is created. Otherwise it would be confused with the case the buffer
    // starting
    // with a complete record.
    // !! The next two lines can not change order.
    final int partialRecordBytes = buffer.appendAndCommit(remainingRecordBytes);
    addToSubpartition(buffer, targetSubpartition, partialRecordBytes, partialRecordBytes);
    return buffer;
}
Also used : BufferBuilder(org.apache.flink.runtime.io.network.buffer.BufferBuilder)

Example 60 with BufferBuilder

use of org.apache.flink.runtime.io.network.buffer.BufferBuilder in project flink by splunk.

the class BufferWritingResultPartition method appendBroadcastDataForRecordContinuation.

private BufferBuilder appendBroadcastDataForRecordContinuation(final ByteBuffer remainingRecordBytes) throws IOException {
    final BufferBuilder buffer = requestNewBroadcastBufferBuilder();
    // !! Be aware, in case of partialRecordBytes != 0, partial length and data has to
    // `appendAndCommit` first
    // before consumer is created. Otherwise it would be confused with the case the buffer
    // starting
    // with a complete record.
    // !! The next two lines can not change order.
    final int partialRecordBytes = buffer.appendAndCommit(remainingRecordBytes);
    createBroadcastBufferConsumers(buffer, partialRecordBytes, partialRecordBytes);
    return buffer;
}
Also used : BufferBuilder(org.apache.flink.runtime.io.network.buffer.BufferBuilder)

Aggregations

BufferBuilder (org.apache.flink.runtime.io.network.buffer.BufferBuilder)84 BufferConsumer (org.apache.flink.runtime.io.network.buffer.BufferConsumer)27 Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)21 Test (org.junit.Test)21 BufferBuilderTestUtils.createBufferBuilder (org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils.createBufferBuilder)18 MemorySegment (org.apache.flink.core.memory.MemorySegment)12 InputChannelInfo (org.apache.flink.runtime.checkpoint.channel.InputChannelInfo)12 SubtaskConnectionDescriptor (org.apache.flink.runtime.io.network.api.SubtaskConnectionDescriptor)12 NetworkBuffer (org.apache.flink.runtime.io.network.buffer.NetworkBuffer)9 ByteBuffer (java.nio.ByteBuffer)6 DataOutputSerializer (org.apache.flink.core.memory.DataOutputSerializer)6 BufferBuilderTestUtils.createFilledBufferBuilder (org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils.createFilledBufferBuilder)6 BufferPool (org.apache.flink.runtime.io.network.buffer.BufferPool)6 NetworkBufferPool (org.apache.flink.runtime.io.network.buffer.NetworkBufferPool)6 CheckpointedResultSubpartition (org.apache.flink.runtime.io.network.partition.CheckpointedResultSubpartition)6 BufferAndAvailability (org.apache.flink.runtime.io.network.partition.consumer.InputChannel.BufferAndAvailability)6 BufferAndAvailabilityProvider (org.apache.flink.runtime.io.network.partition.consumer.TestInputChannel.BufferAndAvailabilityProvider)6 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3