Search in sources :

Example 96 with Buffer

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

the class StreamMockEnvironment method addOutput.

public <T> void addOutput(final Queue<Object> outputList, final TypeSerializer<T> serializer) {
    try {
        // The record-oriented writers wrap the buffer writer. We mock it
        // to collect the returned buffers and deserialize the content to
        // the output list
        BufferProvider mockBufferProvider = mock(BufferProvider.class);
        when(mockBufferProvider.requestBufferBlocking()).thenAnswer(new Answer<Buffer>() {

            @Override
            public Buffer answer(InvocationOnMock invocationOnMock) throws Throwable {
                return new Buffer(MemorySegmentFactory.allocateUnpooledSegment(bufferSize), mock(BufferRecycler.class));
            }
        });
        ResultPartitionWriter mockWriter = mock(ResultPartitionWriter.class);
        when(mockWriter.getNumberOfOutputChannels()).thenReturn(1);
        when(mockWriter.getBufferProvider()).thenReturn(mockBufferProvider);
        final RecordDeserializer<DeserializationDelegate<T>> recordDeserializer = new AdaptiveSpanningRecordDeserializer<DeserializationDelegate<T>>();
        final NonReusingDeserializationDelegate<T> delegate = new NonReusingDeserializationDelegate<T>(serializer);
        // Add records and events from the buffer to the output list
        doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
                Buffer buffer = (Buffer) invocationOnMock.getArguments()[0];
                addBufferToOutputList(recordDeserializer, delegate, buffer, outputList);
                return null;
            }
        }).when(mockWriter).writeBuffer(any(Buffer.class), anyInt());
        doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
                Buffer buffer = (Buffer) invocationOnMock.getArguments()[0];
                addBufferToOutputList(recordDeserializer, delegate, buffer, outputList);
                return null;
            }
        }).when(mockWriter).writeBufferToAllChannels(any(Buffer.class));
        outputs.add(mockWriter);
    } catch (Throwable t) {
        t.printStackTrace();
        fail(t.getMessage());
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) AdaptiveSpanningRecordDeserializer(org.apache.flink.runtime.io.network.api.serialization.AdaptiveSpanningRecordDeserializer) ResultPartitionWriter(org.apache.flink.runtime.io.network.api.writer.ResultPartitionWriter) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NonReusingDeserializationDelegate(org.apache.flink.runtime.plugable.NonReusingDeserializationDelegate) BufferProvider(org.apache.flink.runtime.io.network.buffer.BufferProvider) DeserializationDelegate(org.apache.flink.runtime.plugable.DeserializationDelegate) NonReusingDeserializationDelegate(org.apache.flink.runtime.plugable.NonReusingDeserializationDelegate)

Example 97 with Buffer

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

the class SpilledBufferOrEventSequenceTest method validateBuffer.

private static void validateBuffer(BufferOrEvent boe, int expectedSize, int expectedChannelIndex) {
    assertEquals("wrong channel index", expectedChannelIndex, boe.getChannelIndex());
    assertTrue("is not buffer", boe.isBuffer());
    Buffer buf = boe.getBuffer();
    assertEquals("wrong buffer size", expectedSize, buf.getSize());
    MemorySegment seg = buf.getMemorySegment();
    for (int i = 0; i < expectedSize; i++) {
        assertEquals("wrong buffer contents", (byte) i, seg.get(i));
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer) MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 98 with Buffer

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

the class StreamRecordWriterTest method getMockWriter.

private static ResultPartitionWriter getMockWriter(int numPartitions) throws Exception {
    BufferProvider mockProvider = mock(BufferProvider.class);
    when(mockProvider.requestBufferBlocking()).thenAnswer(new Answer<Buffer>() {

        @Override
        public Buffer answer(InvocationOnMock invocation) {
            return new Buffer(MemorySegmentFactory.allocateUnpooledSegment(4096), FreeingBufferRecycler.INSTANCE);
        }
    });
    ResultPartitionWriter mockWriter = mock(ResultPartitionWriter.class);
    when(mockWriter.getBufferProvider()).thenReturn(mockProvider);
    when(mockWriter.getNumberOfOutputChannels()).thenReturn(numPartitions);
    return mockWriter;
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResultPartitionWriter(org.apache.flink.runtime.io.network.api.writer.ResultPartitionWriter) BufferProvider(org.apache.flink.runtime.io.network.buffer.BufferProvider)

Example 99 with Buffer

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

the class BarrierBufferAlignmentLimitTest method createBuffer.

// ------------------------------------------------------------------------
//  Utilities
// ------------------------------------------------------------------------
private static BufferOrEvent createBuffer(int channel, int size) {
    byte[] bytes = new byte[size];
    RND.nextBytes(bytes);
    MemorySegment memory = MemorySegmentFactory.allocateUnpooledSegment(PAGE_SIZE);
    memory.put(0, bytes);
    Buffer buf = new Buffer(memory, FreeingBufferRecycler.INSTANCE);
    buf.setSize(size);
    // retain an additional time so it does not get disposed after being read by the input gate
    buf.retain();
    return new BufferOrEvent(buf, channel);
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) MemorySegment(org.apache.flink.core.memory.MemorySegment) BufferOrEvent(org.apache.flink.runtime.io.network.partition.consumer.BufferOrEvent)

Example 100 with Buffer

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

the class BarrierBufferTest method createBuffer.

private static BufferOrEvent createBuffer(int channel) {
    final int size = SIZE_COUNTER++;
    byte[] bytes = new byte[size];
    RND.nextBytes(bytes);
    MemorySegment memory = MemorySegmentFactory.allocateUnpooledSegment(PAGE_SIZE);
    memory.put(0, bytes);
    Buffer buf = new Buffer(memory, FreeingBufferRecycler.INSTANCE);
    buf.setSize(size);
    // retain an additional time so it does not get disposed after being read by the input gate
    buf.retain();
    return new BufferOrEvent(buf, channel);
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) MemorySegment(org.apache.flink.core.memory.MemorySegment) BufferOrEvent(org.apache.flink.runtime.io.network.partition.consumer.BufferOrEvent)

Aggregations

Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)172 Test (org.junit.Test)91 NetworkBuffer (org.apache.flink.runtime.io.network.buffer.NetworkBuffer)45 ByteBuffer (java.nio.ByteBuffer)44 TestBufferFactory.createBuffer (org.apache.flink.runtime.io.network.util.TestBufferFactory.createBuffer)35 EventSerializer.toBuffer (org.apache.flink.runtime.io.network.api.serialization.EventSerializer.toBuffer)34 IOException (java.io.IOException)27 MemorySegment (org.apache.flink.core.memory.MemorySegment)27 BufferPool (org.apache.flink.runtime.io.network.buffer.BufferPool)18 TestCheckpointedInputGateBuilder (org.apache.flink.streaming.util.TestCheckpointedInputGateBuilder)18 NetworkBufferPool (org.apache.flink.runtime.io.network.buffer.NetworkBufferPool)16 ArrayList (java.util.ArrayList)15 Nullable (javax.annotation.Nullable)15 BufferBuilderTestUtils.buildSingleBuffer (org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils.buildSingleBuffer)14 InputChannelTestUtils.createSingleInputGate (org.apache.flink.runtime.io.network.partition.InputChannelTestUtils.createSingleInputGate)13 Random (java.util.Random)12 RemoteInputChannel (org.apache.flink.runtime.io.network.partition.consumer.RemoteInputChannel)10 CheckpointBarrier (org.apache.flink.runtime.io.network.api.CheckpointBarrier)9 BufferConsumer (org.apache.flink.runtime.io.network.buffer.BufferConsumer)9 BufferProvider (org.apache.flink.runtime.io.network.buffer.BufferProvider)9