Search in sources :

Example 11 with NetworkBuffer

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

the class EventSerializer method toBufferConsumer.

public static BufferConsumer toBufferConsumer(AbstractEvent event, boolean hasPriority) throws IOException {
    final ByteBuffer serializedEvent = EventSerializer.toSerializedEvent(event);
    MemorySegment data = MemorySegmentFactory.wrap(serializedEvent.array());
    return new BufferConsumer(new NetworkBuffer(data, FreeingBufferRecycler.INSTANCE, getDataType(event, hasPriority)), data.size());
}
Also used : NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) BufferConsumer(org.apache.flink.runtime.io.network.buffer.BufferConsumer) ByteBuffer(java.nio.ByteBuffer) MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 12 with NetworkBuffer

use of org.apache.flink.runtime.io.network.buffer.NetworkBuffer 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)

Example 13 with NetworkBuffer

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

the class NetworkBufferAllocator method allocateUnPooledNetworkBuffer.

/**
 * Allocates an un-pooled network buffer with the specific size.
 *
 * @param size The requested buffer size.
 * @param dataType The data type this buffer represents.
 * @return The un-pooled network buffer.
 */
Buffer allocateUnPooledNetworkBuffer(int size, Buffer.DataType dataType) {
    checkArgument(size > 0, "Illegal buffer size, must be positive.");
    byte[] byteArray = new byte[size];
    MemorySegment memSeg = MemorySegmentFactory.wrap(byteArray);
    return new NetworkBuffer(memSeg, FreeingBufferRecycler.INSTANCE, dataType);
}
Also used : NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 14 with NetworkBuffer

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

the class AsynchronousBufferFileWriterTest method testAddWithFailingWriter.

@Test
public void testAddWithFailingWriter() throws Exception {
    AsynchronousBufferFileWriter writer = new AsynchronousBufferFileWriter(ioManager.createChannel(), new RequestQueue<>());
    writer.close();
    exception.expect(IOException.class);
    Buffer buffer = new NetworkBuffer(MemorySegmentFactory.allocateUnpooledSegment(4096), FreeingBufferRecycler.INSTANCE);
    try {
        writer.writeBlock(buffer);
    } finally {
        if (!buffer.isRecycled()) {
            buffer.recycleBuffer();
            Assert.fail("buffer not recycled");
        }
        assertEquals("Shouln't increment number of outstanding requests.", 0, writer.getNumberOfOutstandingRequests());
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) Test(org.junit.Test)

Example 15 with NetworkBuffer

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

the class PartitionedFileWriteReadTest method testNotWriteDataOfTheSameSubpartitionTogether.

@Test(expected = IllegalStateException.class)
public void testNotWriteDataOfTheSameSubpartitionTogether() throws Exception {
    PartitionedFileWriter partitionedFileWriter = createPartitionedFileWriter(2);
    try {
        MemorySegment segment = MemorySegmentFactory.allocateUnpooledSegment(1024);
        NetworkBuffer buffer1 = new NetworkBuffer(segment, (buf) -> {
        });
        partitionedFileWriter.writeBuffers(getBufferWithChannels(buffer1, 1));
        NetworkBuffer buffer2 = new NetworkBuffer(segment, (buf) -> {
        });
        partitionedFileWriter.writeBuffers(getBufferWithChannels(buffer2, 0));
        NetworkBuffer buffer3 = new NetworkBuffer(segment, (buf) -> {
        });
        partitionedFileWriter.writeBuffers(getBufferWithChannels(buffer3, 1));
    } finally {
        partitionedFileWriter.finish();
    }
}
Also used : NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) MemorySegment(org.apache.flink.core.memory.MemorySegment) Test(org.junit.Test)

Aggregations

NetworkBuffer (org.apache.flink.runtime.io.network.buffer.NetworkBuffer)39 MemorySegment (org.apache.flink.core.memory.MemorySegment)21 Test (org.junit.Test)14 Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)11 ByteBuffer (java.nio.ByteBuffer)8 IOException (java.io.IOException)4 ChannelStateWriteResult (org.apache.flink.runtime.checkpoint.channel.ChannelStateWriter.ChannelStateWriteResult)4 BufferConsumer (org.apache.flink.runtime.io.network.buffer.BufferConsumer)4 DataOutputStream (java.io.DataOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 ArrayDeque (java.util.ArrayDeque)2 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 Nullable (javax.annotation.Nullable)2 MemorySegmentFactory (org.apache.flink.core.memory.MemorySegmentFactory)2 NetworkBufferPool (org.apache.flink.runtime.io.network.buffer.NetworkBufferPool)2 ByteStreamStateHandle (org.apache.flink.runtime.state.memory.ByteStreamStateHandle)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertTrue (org.junit.Assert.assertTrue)2