Search in sources :

Example 86 with Buffer

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

the class PipelinedSubpartition method finish.

@Override
public void finish() throws IOException {
    final Buffer buffer = EventSerializer.toBuffer(EndOfPartitionEvent.INSTANCE);
    // view reference accessible outside the lock, but assigned inside the locked scope
    final PipelinedSubpartitionView reader;
    synchronized (buffers) {
        if (isFinished || isReleased) {
            return;
        }
        buffers.add(buffer);
        reader = readView;
        updateStatistics(buffer);
        isFinished = true;
    }
    LOG.debug("Finished {}.", this);
    // Notify the listener outside of the synchronized block
    if (reader != null) {
        reader.notifyBuffersAvailable(1);
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer)

Example 87 with Buffer

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

the class SpilledSubpartitionView method getNextBuffer.

@Override
public Buffer getNextBuffer() throws IOException, InterruptedException {
    if (fileReader.hasReachedEndOfFile() || isSpillInProgress) {
        return null;
    }
    // TODO This is fragile as we implicitly expect that multiple calls to
    // this method don't happen before recycling buffers returned earlier.
    Buffer buffer = bufferPool.requestBufferBlocking();
    fileReader.readInto(buffer);
    return buffer;
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer)

Example 88 with Buffer

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

the class AdaptiveSpanningRecordDeserializer method getCurrentBuffer.

@Override
public Buffer getCurrentBuffer() {
    Buffer tmp = currentBuffer;
    currentBuffer = null;
    return tmp;
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer)

Example 89 with Buffer

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

the class RecordWriterTest method testClearBuffersAfterInterruptDuringBlockingBufferRequest.

// ---------------------------------------------------------------------------------------------
// Resource release tests
// ---------------------------------------------------------------------------------------------
/**
	 * Tests a fix for FLINK-2089.
	 *
	 * @see <a href="https://issues.apache.org/jira/browse/FLINK-2089">FLINK-2089</a>
	 */
@Test
public void testClearBuffersAfterInterruptDuringBlockingBufferRequest() throws Exception {
    ExecutorService executor = null;
    try {
        executor = Executors.newSingleThreadExecutor();
        final CountDownLatch sync = new CountDownLatch(2);
        final Buffer buffer = spy(TestBufferFactory.createBuffer(4));
        // Return buffer for first request, but block for all following requests.
        Answer<Buffer> request = new Answer<Buffer>() {

            @Override
            public Buffer answer(InvocationOnMock invocation) throws Throwable {
                sync.countDown();
                if (sync.getCount() == 1) {
                    return buffer;
                }
                final Object o = new Object();
                synchronized (o) {
                    while (true) {
                        o.wait();
                    }
                }
            }
        };
        BufferProvider bufferProvider = mock(BufferProvider.class);
        when(bufferProvider.requestBufferBlocking()).thenAnswer(request);
        ResultPartitionWriter partitionWriter = createResultPartitionWriter(bufferProvider);
        final RecordWriter<IntValue> recordWriter = new RecordWriter<IntValue>(partitionWriter);
        Future<?> result = executor.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                IntValue val = new IntValue(0);
                try {
                    recordWriter.emit(val);
                    recordWriter.flush();
                    recordWriter.emit(val);
                } catch (InterruptedException e) {
                    recordWriter.clearBuffers();
                }
                return null;
            }
        });
        sync.await();
        // Interrupt the Thread.
        //
        // The second emit call requests a new buffer and blocks the thread.
        // When interrupting the thread at this point, clearing the buffers
        // should not recycle any buffer.
        result.cancel(true);
        recordWriter.clearBuffers();
        // Verify that buffer have been requested, but only one has been written out.
        verify(bufferProvider, times(2)).requestBufferBlocking();
        verify(partitionWriter, times(1)).writeBuffer(any(Buffer.class), anyInt());
        // Verify that the written out buffer has only been recycled once
        // (by the partition writer).
        assertTrue("Buffer not recycled.", buffer.isRecycled());
        verify(buffer, times(1)).recycle();
    } finally {
        if (executor != null) {
            executor.shutdown();
        }
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ExecutorService(java.util.concurrent.ExecutorService) TestInfiniteBufferProvider(org.apache.flink.runtime.io.network.util.TestInfiniteBufferProvider) BufferProvider(org.apache.flink.runtime.io.network.buffer.BufferProvider) IntValue(org.apache.flink.types.IntValue) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 90 with Buffer

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

the class ResultPartitionWriterTest method testWriteBufferToAllChannelsReferenceCounting.

// ---------------------------------------------------------------------------------------------
// Resource release tests
// ---------------------------------------------------------------------------------------------
/**
	 * Tests that event buffers are properly recycled when broadcasting events
	 * to multiple channels.
	 *
	 * @throws Exception
	 */
@Test
public void testWriteBufferToAllChannelsReferenceCounting() throws Exception {
    Buffer buffer = EventSerializer.toBuffer(EndOfPartitionEvent.INSTANCE);
    ResultPartition partition = new ResultPartition("TestTask", mock(TaskActions.class), new JobID(), new ResultPartitionID(), ResultPartitionType.PIPELINED, 2, 2, mock(ResultPartitionManager.class), mock(ResultPartitionConsumableNotifier.class), mock(IOManager.class), false);
    ResultPartitionWriter partitionWriter = new ResultPartitionWriter(partition);
    partitionWriter.writeBufferToAllChannels(buffer);
    // Verify added to all queues, i.e. two buffers in total
    assertEquals(2, partition.getTotalNumberOfBuffers());
    // release the buffers in the partition
    partition.release();
    assertTrue(buffer.isRecycled());
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) IOManager(org.apache.flink.runtime.io.disk.iomanager.IOManager) ResultPartitionID(org.apache.flink.runtime.io.network.partition.ResultPartitionID) TaskActions(org.apache.flink.runtime.taskmanager.TaskActions) ResultPartitionManager(org.apache.flink.runtime.io.network.partition.ResultPartitionManager) ResultPartitionConsumableNotifier(org.apache.flink.runtime.io.network.partition.ResultPartitionConsumableNotifier) JobID(org.apache.flink.api.common.JobID) ResultPartition(org.apache.flink.runtime.io.network.partition.ResultPartition) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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