Search in sources :

Example 21 with NetworkBuffer

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

the class NettyMessageClientSideSerializationTest method testBufferResponse.

private void testBufferResponse(boolean testReadOnlyBuffer, boolean testCompressedBuffer) {
    checkArgument(!(testReadOnlyBuffer & testCompressedBuffer), "There are no cases with both readonly slice and compression.");
    NetworkBuffer buffer = new NetworkBuffer(MemorySegmentFactory.allocateUnpooledSegment(BUFFER_SIZE), FreeingBufferRecycler.INSTANCE);
    for (int i = 0; i < BUFFER_SIZE; i += 8) {
        buffer.writeLong(i);
    }
    Buffer testBuffer = buffer;
    if (testReadOnlyBuffer) {
        testBuffer = buffer.readOnlySlice();
    } else if (testCompressedBuffer) {
        testBuffer = COMPRESSOR.compressToOriginalBuffer(buffer);
    }
    BufferResponse expected = new BufferResponse(testBuffer, random.nextInt(Integer.MAX_VALUE), inputChannelId, random.nextInt(Integer.MAX_VALUE));
    BufferResponse actual = encodeAndDecode(expected, channel);
    assertTrue(buffer.isRecycled());
    assertTrue(testBuffer.isRecycled());
    assertNotNull("The request input channel should always have available buffers in this test.", actual.getBuffer());
    Buffer decodedBuffer = actual.getBuffer();
    if (testCompressedBuffer) {
        assertTrue(actual.isCompressed);
        decodedBuffer = decompress(decodedBuffer);
    }
    verifyBufferResponseHeader(expected, actual);
    assertEquals(BUFFER_SIZE, decodedBuffer.readableBytes());
    for (int i = 0; i < BUFFER_SIZE; i += 8) {
        assertEquals(i, decodedBuffer.asByteBuf().readLong());
    }
    // Release the received message.
    actual.releaseBuffer();
    if (testCompressedBuffer) {
        decodedBuffer.recycleBuffer();
    }
    assertTrue(actual.getBuffer().isRecycled());
}
Also used : NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) BufferResponse(org.apache.flink.runtime.io.network.netty.NettyMessage.BufferResponse)

Example 22 with NetworkBuffer

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

the class CompressedHeaderlessChannelWriterOutputView method writeCompressed.

private void writeCompressed(MemorySegment current, int size) throws IOException {
    MemorySegment compressedBuffer;
    try {
        compressedBuffer = compressedBuffers.take();
    } catch (InterruptedException e) {
        throw new IOException(e);
    }
    int compressedLen = compressor.compress(current.getArray(), 0, size, compressedBuffer.getArray(), 0);
    NetworkBuffer networkBuffer = new NetworkBuffer(compressedBuffer, this);
    networkBuffer.setSize(compressedLen);
    writer.writeBlock(networkBuffer);
    blockCount++;
    numBytes += size;
    numCompressedBytes += compressedLen;
}
Also used : NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) IOException(java.io.IOException) MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 23 with NetworkBuffer

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

the class ChannelStateCheckpointWriterTest method testRecyclingBuffers.

@Test
public void testRecyclingBuffers() throws Exception {
    ChannelStateCheckpointWriter writer = createWriter(new ChannelStateWriteResult());
    NetworkBuffer buffer = new NetworkBuffer(MemorySegmentFactory.allocateUnpooledSegment(10, null), FreeingBufferRecycler.INSTANCE);
    writer.writeInput(new InputChannelInfo(1, 2), buffer);
    assertTrue(buffer.isRecycled());
}
Also used : NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) ChannelStateWriteResult(org.apache.flink.runtime.checkpoint.channel.ChannelStateWriter.ChannelStateWriteResult) Test(org.junit.Test)

Example 24 with NetworkBuffer

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

the class ChannelStateSerializerImplTest method getBuffer.

private NetworkBuffer getBuffer(byte[] data) {
    NetworkBuffer buffer = new NetworkBuffer(MemorySegmentFactory.allocateUnpooledSegment(data.length, null), FreeingBufferRecycler.INSTANCE);
    buffer.writeBytes(data);
    return buffer;
}
Also used : NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer)

Example 25 with NetworkBuffer

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

the class ChannelStateSerializerImplTest method write.

private void write(byte[] data, ChannelStateSerializerImpl serializer, OutputStream baos) throws IOException {
    DataOutputStream out = new DataOutputStream(baos);
    serializer.writeHeader(out);
    NetworkBuffer buffer = new NetworkBuffer(MemorySegmentFactory.allocateUnpooledSegment(data.length), FreeingBufferRecycler.INSTANCE);
    try {
        buffer.writeBytes(data);
        serializer.writeData(out, buffer);
        out.flush();
    } finally {
        buffer.release();
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer)

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