Search in sources :

Example 1 with NoOpBufferAvailablityListener

use of org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener in project flink by apache.

the class SequentialChannelStateReaderImplTest method collectBuffers.

private Map<ResultSubpartitionInfo, List<Buffer>> collectBuffers(BufferWritingResultPartition[] resultPartitions) throws IOException {
    Map<ResultSubpartitionInfo, List<Buffer>> actual = new HashMap<>();
    for (BufferWritingResultPartition resultPartition : resultPartitions) {
        for (int i = 0; i < resultPartition.getNumberOfSubpartitions(); i++) {
            ResultSubpartitionInfo info = resultPartition.getAllPartitions()[i].getSubpartitionInfo();
            ResultSubpartitionView view = resultPartition.createSubpartitionView(info.getSubPartitionIdx(), new NoOpBufferAvailablityListener());
            for (BufferAndBacklog buffer = view.getNextBuffer(); buffer != null; buffer = view.getNextBuffer()) {
                if (buffer.buffer().isBuffer()) {
                    actual.computeIfAbsent(info, unused -> new ArrayList<>()).add(buffer.buffer());
                }
            }
        }
    }
    return actual;
}
Also used : BufferAndBacklog(org.apache.flink.runtime.io.network.partition.ResultSubpartition.BufferAndBacklog) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Tuple2(org.apache.flink.api.java.tuple.Tuple2) IntStream.range(java.util.stream.IntStream.range) BiFunction(java.util.function.BiFunction) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Random(java.util.Random) Function(java.util.function.Function) NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) NetworkBufferPool(org.apache.flink.runtime.io.network.buffer.NetworkBufferPool) ArrayList(java.util.ArrayList) ResultPartition(org.apache.flink.runtime.io.network.partition.ResultPartition) DataOutputStream(java.io.DataOutputStream) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) ThrowingConsumer(org.apache.flink.util.function.ThrowingConsumer) Parameterized(org.junit.runners.Parameterized) SingleInputGate(org.apache.flink.runtime.io.network.partition.consumer.SingleInputGate) TaskStateSnapshot(org.apache.flink.runtime.checkpoint.TaskStateSnapshot) InputGate(org.apache.flink.runtime.io.network.partition.consumer.InputGate) MemorySegmentFactory(org.apache.flink.core.memory.MemorySegmentFactory) SingleInputGateBuilder(org.apache.flink.runtime.io.network.partition.consumer.SingleInputGateBuilder) ResultPartitionBuilder(org.apache.flink.runtime.io.network.partition.ResultPartitionBuilder) InputChannelStateHandle(org.apache.flink.runtime.state.InputChannelStateHandle) Closer(org.apache.flink.shaded.guava30.com.google.common.io.Closer) Assert.assertTrue(org.junit.Assert.assertTrue) FreeingBufferRecycler(org.apache.flink.runtime.io.network.buffer.FreeingBufferRecycler) Test(org.junit.Test) IOException(java.io.IOException) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) ResultSubpartitionStateHandle(org.apache.flink.runtime.state.ResultSubpartitionStateHandle) BufferWritingResultPartition(org.apache.flink.runtime.io.network.partition.BufferWritingResultPartition) Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) NoOpBufferAvailablityListener(org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ByteStreamStateHandle(org.apache.flink.runtime.state.memory.ByteStreamStateHandle) Function.identity(java.util.function.Function.identity) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) Optional(java.util.Optional) ResultSubpartitionView(org.apache.flink.runtime.io.network.partition.ResultSubpartitionView) Collections(java.util.Collections) StateObjectCollection(org.apache.flink.runtime.checkpoint.StateObjectCollection) BufferOrEvent(org.apache.flink.runtime.io.network.partition.consumer.BufferOrEvent) Assert.assertEquals(org.junit.Assert.assertEquals) BufferWritingResultPartition(org.apache.flink.runtime.io.network.partition.BufferWritingResultPartition) ResultSubpartitionView(org.apache.flink.runtime.io.network.partition.ResultSubpartitionView) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NoOpBufferAvailablityListener(org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) BufferAndBacklog(org.apache.flink.runtime.io.network.partition.ResultSubpartition.BufferAndBacklog)

Example 2 with NoOpBufferAvailablityListener

use of org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener in project flink by apache.

the class PartitionRequestQueueTest method testNotifyNewBufferSize.

@Test
public void testNotifyNewBufferSize() throws Exception {
    // given: Result partition and the reader for subpartition 0.
    ResultPartition parent = createResultPartition();
    BufferAvailabilityListener bufferAvailabilityListener = new NoOpBufferAvailablityListener();
    ResultSubpartitionView view = parent.createSubpartitionView(0, bufferAvailabilityListener);
    ResultPartitionProvider partitionProvider = (partitionId, index, availabilityListener) -> view;
    InputChannelID receiverId = new InputChannelID();
    PartitionRequestQueue queue = new PartitionRequestQueue();
    CreditBasedSequenceNumberingViewReader reader = new CreditBasedSequenceNumberingViewReader(receiverId, 2, queue);
    EmbeddedChannel channel = new EmbeddedChannel(queue);
    reader.requestSubpartitionView(partitionProvider, new ResultPartitionID(), 0);
    queue.notifyReaderCreated(reader);
    // when: New buffer size received.
    queue.notifyNewBufferSize(receiverId, 65);
    // and: New records emit.
    parent.emitRecord(ByteBuffer.allocate(128), 0);
    parent.emitRecord(ByteBuffer.allocate(10), 0);
    parent.emitRecord(ByteBuffer.allocate(60), 0);
    reader.notifyDataAvailable();
    channel.runPendingTasks();
    // then: Buffers of received size will be in outbound channel.
    Object data1 = channel.readOutbound();
    // The size can not be less than the first record in buffer.
    assertEquals(128, ((NettyMessage.BufferResponse) data1).buffer.getSize());
    Object data2 = channel.readOutbound();
    // The size should shrink up to notified buffer size.
    assertEquals(65, ((NettyMessage.BufferResponse) data2).buffer.getSize());
}
Also used : BufferAndBacklog(org.apache.flink.runtime.io.network.partition.ResultSubpartition.BufferAndBacklog) TestBufferFactory(org.apache.flink.runtime.io.network.util.TestBufferFactory) BeforeClass(org.junit.BeforeClass) ResultPartitionType(org.apache.flink.runtime.io.network.partition.ResultPartitionType) ByteBuffer(java.nio.ByteBuffer) FileChannelManager(org.apache.flink.runtime.io.disk.FileChannelManager) NetworkSequenceViewReader(org.apache.flink.runtime.io.network.NetworkSequenceViewReader) ResultPartition(org.apache.flink.runtime.io.network.partition.ResultPartition) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ResultPartitionID(org.apache.flink.runtime.io.network.partition.ResultPartitionID) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BufferAvailabilityListener(org.apache.flink.runtime.io.network.partition.BufferAvailabilityListener) BufferBuilderTestUtils.createEventBufferConsumer(org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils.createEventBufferConsumer) ClassRule(org.junit.ClassRule) Nullable(javax.annotation.Nullable) NoOpResultSubpartitionView(org.apache.flink.runtime.io.network.partition.NoOpResultSubpartitionView) AfterClass(org.junit.AfterClass) PipelinedSubpartitionView(org.apache.flink.runtime.io.network.partition.PipelinedSubpartitionView) NettyShuffleEnvironmentBuilder(org.apache.flink.runtime.io.network.NettyShuffleEnvironmentBuilder) Assert.assertNotNull(org.junit.Assert.assertNotNull) Unpooled(org.apache.flink.shaded.netty4.io.netty.buffer.Unpooled) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) NettyShuffleEnvironment(org.apache.flink.runtime.io.network.NettyShuffleEnvironment) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) NoOpBufferAvailablityListener(org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) PipelinedSubpartition(org.apache.flink.runtime.io.network.partition.PipelinedSubpartition) Assert.assertNull(org.junit.Assert.assertNull) Matchers.contains(org.hamcrest.Matchers.contains) Assert.assertFalse(org.junit.Assert.assertFalse) NoOpFileChannelManager(org.apache.flink.runtime.io.disk.NoOpFileChannelManager) PipelinedSubpartitionTest(org.apache.flink.runtime.io.network.partition.PipelinedSubpartitionTest) FileChannelManagerImpl(org.apache.flink.runtime.io.disk.FileChannelManagerImpl) ResultPartitionManager(org.apache.flink.runtime.io.network.partition.ResultPartitionManager) ResultSubpartitionView(org.apache.flink.runtime.io.network.partition.ResultSubpartitionView) EmbeddedChannel(org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel) PartitionTestUtils.createPartition(org.apache.flink.runtime.io.network.partition.PartitionTestUtils.createPartition) ResultPartitionProvider(org.apache.flink.runtime.io.network.partition.ResultPartitionProvider) InputChannelID(org.apache.flink.runtime.io.network.partition.consumer.InputChannelID) TemporaryFolder(org.junit.rules.TemporaryFolder) Assert.assertEquals(org.junit.Assert.assertEquals) NoOpResultSubpartitionView(org.apache.flink.runtime.io.network.partition.NoOpResultSubpartitionView) ResultSubpartitionView(org.apache.flink.runtime.io.network.partition.ResultSubpartitionView) EmbeddedChannel(org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel) ResultPartition(org.apache.flink.runtime.io.network.partition.ResultPartition) InputChannelID(org.apache.flink.runtime.io.network.partition.consumer.InputChannelID) BufferAvailabilityListener(org.apache.flink.runtime.io.network.partition.BufferAvailabilityListener) NoOpBufferAvailablityListener(org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener) ResultPartitionID(org.apache.flink.runtime.io.network.partition.ResultPartitionID) ResultPartitionProvider(org.apache.flink.runtime.io.network.partition.ResultPartitionProvider) Test(org.junit.Test) PipelinedSubpartitionTest(org.apache.flink.runtime.io.network.partition.PipelinedSubpartitionTest)

Example 3 with NoOpBufferAvailablityListener

use of org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener in project flink by apache.

the class BroadcastRecordWriterTest method testBroadcastMixedRandomEmitRecord.

/**
 * Tests the number of requested buffers and results are correct in the case of switching modes
 * between {@link BroadcastRecordWriter#broadcastEmit(IOReadableWritable)} and {@link
 * BroadcastRecordWriter#randomEmit(IOReadableWritable)}.
 */
@Test
public void testBroadcastMixedRandomEmitRecord() throws Exception {
    final int numberOfChannels = 8;
    final int numberOfRecords = 8;
    final int bufferSize = 32;
    final ResultPartition partition = createResultPartition(bufferSize, numberOfChannels);
    final BroadcastRecordWriter<SerializationTestType> writer = new BroadcastRecordWriter<>(partition, -1, "test");
    final RecordDeserializer<SerializationTestType> deserializer = new SpillingAdaptiveSpanningRecordDeserializer<>(new String[] { tempFolder.getRoot().getAbsolutePath() });
    // generate the configured number of int values as global record set
    final Iterable<SerializationTestType> records = Util.randomRecords(numberOfRecords, SerializationTestTypeFactory.INT);
    // restore the corresponding record set for every input channel
    final Map<Integer, ArrayDeque<SerializationTestType>> serializedRecords = new HashMap<>();
    for (int i = 0; i < numberOfChannels; i++) {
        serializedRecords.put(i, new ArrayDeque<>());
    }
    // every record in global set would both emit into one random channel and broadcast to all
    // the channels
    int index = 0;
    for (SerializationTestType record : records) {
        int randomChannel = index++ % numberOfChannels;
        writer.emit(record, randomChannel);
        serializedRecords.get(randomChannel).add(record);
        writer.broadcastEmit(record);
        for (int i = 0; i < numberOfChannels; i++) {
            serializedRecords.get(i).add(record);
        }
    }
    final int numberOfCreatedBuffers = partition.getBufferPool().bestEffortGetNumOfUsedBuffers();
    // verify the expected number of requested buffers, and it would always request a new buffer
    // while random emitting
    assertEquals(2 * numberOfRecords, numberOfCreatedBuffers);
    for (int i = 0; i < numberOfChannels; i++) {
        // every channel would queue the number of above crated buffers
        assertEquals(numberOfRecords + 1, partition.getNumberOfQueuedBuffers(i));
        final int excessRandomRecords = i < numberOfRecords % numberOfChannels ? 1 : 0;
        final int numberOfRandomRecords = numberOfRecords / numberOfChannels + excessRandomRecords;
        final int numberOfTotalRecords = numberOfRecords + numberOfRandomRecords;
        // verify the data correctness in every channel queue
        verifyDeserializationResults(partition.createSubpartitionView(i, new NoOpBufferAvailablityListener()), deserializer, serializedRecords.get(i), numberOfRecords + 1, numberOfTotalRecords);
    }
}
Also used : HashMap(java.util.HashMap) ArrayDeque(java.util.ArrayDeque) ResultPartition(org.apache.flink.runtime.io.network.partition.ResultPartition) SerializationTestType(org.apache.flink.testutils.serialization.types.SerializationTestType) SpillingAdaptiveSpanningRecordDeserializer(org.apache.flink.runtime.io.network.api.serialization.SpillingAdaptiveSpanningRecordDeserializer) NoOpBufferAvailablityListener(org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener) Test(org.junit.Test)

Example 4 with NoOpBufferAvailablityListener

use of org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener in project flink by apache.

the class BroadcastRecordWriterTest method testRandomEmitAndBufferRecycling.

/**
 * FLINK-17780: Tests that a shared buffer(or memory segment) of a buffer builder is only freed
 * when all consumers are closed.
 */
@Test
public void testRandomEmitAndBufferRecycling() throws Exception {
    int recordSize = 8;
    int numberOfChannels = 2;
    ResultPartition partition = createResultPartition(2 * recordSize, numberOfChannels);
    BufferPool bufferPool = partition.getBufferPool();
    BroadcastRecordWriter<SerializationTestType> writer = new BroadcastRecordWriter<>(partition, -1, "test");
    // force materialization of both buffers for easier availability tests
    List<Buffer> buffers = Arrays.asList(bufferPool.requestBuffer(), bufferPool.requestBuffer());
    buffers.forEach(Buffer::recycleBuffer);
    assertEquals(3, bufferPool.getNumberOfAvailableMemorySegments());
    // fill first buffer
    writer.broadcastEmit(new IntType(1));
    writer.broadcastEmit(new IntType(2));
    assertEquals(2, bufferPool.getNumberOfAvailableMemorySegments());
    // simulate consumption of first buffer consumer; this should not free buffers
    assertEquals(1, partition.getNumberOfQueuedBuffers(0));
    ResultSubpartitionView view0 = partition.createSubpartitionView(0, new NoOpBufferAvailablityListener());
    closeConsumer(view0, 2 * recordSize);
    assertEquals(2, bufferPool.getNumberOfAvailableMemorySegments());
    // use second buffer
    writer.emit(new IntType(3), 0);
    assertEquals(1, bufferPool.getNumberOfAvailableMemorySegments());
    // fully free first buffer
    assertEquals(1, partition.getNumberOfQueuedBuffers(1));
    ResultSubpartitionView view1 = partition.createSubpartitionView(1, new NoOpBufferAvailablityListener());
    closeConsumer(view1, 2 * recordSize);
    assertEquals(2, bufferPool.getNumberOfAvailableMemorySegments());
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) BufferPool(org.apache.flink.runtime.io.network.buffer.BufferPool) SerializationTestType(org.apache.flink.testutils.serialization.types.SerializationTestType) ResultSubpartitionView(org.apache.flink.runtime.io.network.partition.ResultSubpartitionView) NoOpBufferAvailablityListener(org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener) ResultPartition(org.apache.flink.runtime.io.network.partition.ResultPartition) IntType(org.apache.flink.testutils.serialization.types.IntType) Test(org.junit.Test)

Example 5 with NoOpBufferAvailablityListener

use of org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener in project flink by apache.

the class RecordWriterDelegateTest method verifyBroadcastEvent.

private void verifyBroadcastEvent(RecordWriterDelegate writerDelegate, List<ResultPartition> partitions) throws Exception {
    final CancelCheckpointMarker message = new CancelCheckpointMarker(1);
    writerDelegate.broadcastEvent(message);
    // verify the added messages in all the queues
    for (ResultPartition partition : partitions) {
        for (int i = 0; i < partition.getNumberOfSubpartitions(); i++) {
            assertEquals(1, partition.getNumberOfQueuedBuffers(i));
            ResultSubpartitionView view = partition.createSubpartitionView(i, new NoOpBufferAvailablityListener());
            BufferOrEvent boe = RecordWriterTest.parseBuffer(view.getNextBuffer().buffer(), i);
            assertTrue(boe.isEvent());
            assertEquals(message, boe.getEvent());
        }
    }
}
Also used : ResultSubpartitionView(org.apache.flink.runtime.io.network.partition.ResultSubpartitionView) CancelCheckpointMarker(org.apache.flink.runtime.io.network.api.CancelCheckpointMarker) NoOpBufferAvailablityListener(org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener) ResultPartition(org.apache.flink.runtime.io.network.partition.ResultPartition) BufferOrEvent(org.apache.flink.runtime.io.network.partition.consumer.BufferOrEvent)

Aggregations

NoOpBufferAvailablityListener (org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener)15 ResultSubpartitionView (org.apache.flink.runtime.io.network.partition.ResultSubpartitionView)14 ResultPartition (org.apache.flink.runtime.io.network.partition.ResultPartition)12 Test (org.junit.Test)11 Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)8 IOException (java.io.IOException)4 BufferAndBacklog (org.apache.flink.runtime.io.network.partition.ResultSubpartition.BufferAndBacklog)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 ByteBuffer (java.nio.ByteBuffer)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Nullable (javax.annotation.Nullable)3 BufferOrEvent (org.apache.flink.runtime.io.network.partition.consumer.BufferOrEvent)3 HashMap (java.util.HashMap)2 Random (java.util.Random)2 TaskStateSnapshot (org.apache.flink.runtime.checkpoint.TaskStateSnapshot)2 FileChannelManager (org.apache.flink.runtime.io.disk.FileChannelManager)2 FileChannelManagerImpl (org.apache.flink.runtime.io.disk.FileChannelManagerImpl)2 NoOpFileChannelManager (org.apache.flink.runtime.io.disk.NoOpFileChannelManager)2 NettyShuffleEnvironment (org.apache.flink.runtime.io.network.NettyShuffleEnvironment)2 NettyShuffleEnvironmentBuilder (org.apache.flink.runtime.io.network.NettyShuffleEnvironmentBuilder)2