Search in sources :

Example 26 with ResultSubpartitionView

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

the class ChannelPersistenceITCase method testReadWritten.

@Test
public void testReadWritten() throws Exception {
    byte[] inputChannelInfoData = randomBytes(1024);
    byte[] resultSubpartitionInfoData = randomBytes(1024);
    int partitionIndex = 0;
    SequentialChannelStateReader reader = new SequentialChannelStateReaderImpl(toTaskStateSnapshot(write(1L, singletonMap(new InputChannelInfo(0, 0), inputChannelInfoData), singletonMap(new ResultSubpartitionInfo(partitionIndex, 0), resultSubpartitionInfoData))));
    NetworkBufferPool networkBufferPool = new NetworkBufferPool(4, 1024);
    try {
        int numChannels = 1;
        InputGate gate = buildGate(networkBufferPool, numChannels);
        reader.readInputData(new InputGate[] { gate });
        assertArrayEquals(inputChannelInfoData, collectBytes(gate::pollNext, BufferOrEvent::getBuffer));
        BufferWritingResultPartition resultPartition = buildResultPartition(networkBufferPool, ResultPartitionType.PIPELINED, partitionIndex, numChannels);
        reader.readOutputData(new BufferWritingResultPartition[] { resultPartition }, false);
        ResultSubpartitionView view = resultPartition.createSubpartitionView(0, new NoOpBufferAvailablityListener());
        assertArrayEquals(resultSubpartitionInfoData, collectBytes(() -> Optional.ofNullable(view.getNextBuffer()), BufferAndBacklog::buffer));
    } finally {
        networkBufferPool.destroy();
    }
}
Also used : InputChannelInfo(org.apache.flink.runtime.checkpoint.channel.InputChannelInfo) BufferWritingResultPartition(org.apache.flink.runtime.io.network.partition.BufferWritingResultPartition) ResultSubpartitionView(org.apache.flink.runtime.io.network.partition.ResultSubpartitionView) SequentialChannelStateReaderImpl(org.apache.flink.runtime.checkpoint.channel.SequentialChannelStateReaderImpl) ResultSubpartitionInfo(org.apache.flink.runtime.checkpoint.channel.ResultSubpartitionInfo) SequentialChannelStateReader(org.apache.flink.runtime.checkpoint.channel.SequentialChannelStateReader) NoOpBufferAvailablityListener(org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener) NetworkBufferPool(org.apache.flink.runtime.io.network.buffer.NetworkBufferPool) SingleInputGate(org.apache.flink.runtime.io.network.partition.consumer.SingleInputGate) InputGate(org.apache.flink.runtime.io.network.partition.consumer.InputGate) Test(org.junit.Test)

Example 27 with ResultSubpartitionView

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

the class CancelPartitionRequestTest method testCancelPartitionRequest.

/**
 * Verifies that requests for non-existing (failed/cancelled) input channels are properly
 * cancelled. The receiver receives data, but there is no input channel to receive the data.
 * This should cancel the request.
 */
@Test
public void testCancelPartitionRequest() throws Exception {
    NettyServerAndClient serverAndClient = null;
    try {
        TestPooledBufferProvider outboundBuffers = new TestPooledBufferProvider(16);
        ResultPartitionManager partitions = mock(ResultPartitionManager.class);
        ResultPartitionID pid = new ResultPartitionID();
        CountDownLatch sync = new CountDownLatch(1);
        final ResultSubpartitionView view = spy(new InfiniteSubpartitionView(outboundBuffers, sync));
        // Return infinite subpartition
        when(partitions.createSubpartitionView(eq(pid), eq(0), any(BufferAvailabilityListener.class))).thenAnswer(new Answer<ResultSubpartitionView>() {

            @Override
            public ResultSubpartitionView answer(InvocationOnMock invocationOnMock) throws Throwable {
                BufferAvailabilityListener listener = (BufferAvailabilityListener) invocationOnMock.getArguments()[2];
                listener.notifyDataAvailable();
                return view;
            }
        });
        NettyProtocol protocol = new NettyProtocol(partitions, mock(TaskEventDispatcher.class));
        serverAndClient = initServerAndClient(protocol);
        Channel ch = connect(serverAndClient);
        // Request for non-existing input channel => results in cancel request
        ch.writeAndFlush(new PartitionRequest(pid, 0, new InputChannelID(), Integer.MAX_VALUE)).await();
        // Wait for the notification
        if (!sync.await(TestingUtils.TESTING_DURATION.toMillis(), TimeUnit.MILLISECONDS)) {
            fail("Timed out after waiting for " + TestingUtils.TESTING_DURATION.toMillis() + " ms to be notified about cancelled partition.");
        }
        verify(view, times(1)).releaseAllResources();
    } finally {
        shutdown(serverAndClient);
    }
}
Also used : TestPooledBufferProvider(org.apache.flink.runtime.io.network.util.TestPooledBufferProvider) ResultSubpartitionView(org.apache.flink.runtime.io.network.partition.ResultSubpartitionView) Channel(org.apache.flink.shaded.netty4.io.netty.channel.Channel) PartitionRequest(org.apache.flink.runtime.io.network.netty.NettyMessage.PartitionRequest) CancelPartitionRequest(org.apache.flink.runtime.io.network.netty.NettyMessage.CancelPartitionRequest) ResultPartitionManager(org.apache.flink.runtime.io.network.partition.ResultPartitionManager) CountDownLatch(java.util.concurrent.CountDownLatch) InvocationOnMock(org.mockito.invocation.InvocationOnMock) InputChannelID(org.apache.flink.runtime.io.network.partition.consumer.InputChannelID) BufferAvailabilityListener(org.apache.flink.runtime.io.network.partition.BufferAvailabilityListener) ResultPartitionID(org.apache.flink.runtime.io.network.partition.ResultPartitionID) TaskEventDispatcher(org.apache.flink.runtime.io.network.TaskEventDispatcher) NettyServerAndClient(org.apache.flink.runtime.io.network.netty.NettyTestUtil.NettyServerAndClient) Test(org.junit.Test)

Example 28 with ResultSubpartitionView

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

the class RecordWriterTest method testBroadcastEmitRecord.

/**
 * Tests that records are broadcast via {@link RecordWriter#broadcastEmit(IOReadableWritable)}.
 */
@Test
public void testBroadcastEmitRecord() throws Exception {
    final int numberOfChannels = 4;
    final int bufferSize = 32;
    final int numValues = 8;
    final int serializationLength = 4;
    final ResultPartition partition = createResultPartition(bufferSize, numberOfChannels);
    final RecordWriter<SerializationTestType> writer = createRecordWriter(partition);
    final RecordDeserializer<SerializationTestType> deserializer = new SpillingAdaptiveSpanningRecordDeserializer<>(new String[] { tempFolder.getRoot().getAbsolutePath() });
    final ArrayDeque<SerializationTestType> serializedRecords = new ArrayDeque<>();
    final Iterable<SerializationTestType> records = Util.randomRecords(numValues, SerializationTestTypeFactory.INT);
    for (SerializationTestType record : records) {
        serializedRecords.add(record);
        writer.broadcastEmit(record);
    }
    final int numRequiredBuffers = numValues / (bufferSize / (4 + serializationLength));
    if (isBroadcastWriter) {
        assertEquals(numRequiredBuffers, partition.getBufferPool().bestEffortGetNumOfUsedBuffers());
    } else {
        assertEquals(numRequiredBuffers * numberOfChannels, partition.getBufferPool().bestEffortGetNumOfUsedBuffers());
    }
    for (int i = 0; i < numberOfChannels; i++) {
        assertEquals(numRequiredBuffers, partition.getNumberOfQueuedBuffers(i));
        ResultSubpartitionView view = partition.createSubpartitionView(i, new NoOpBufferAvailablityListener());
        verifyDeserializationResults(view, deserializer, serializedRecords.clone(), numRequiredBuffers, numValues);
    }
}
Also used : SerializationTestType(org.apache.flink.testutils.serialization.types.SerializationTestType) ResultSubpartitionView(org.apache.flink.runtime.io.network.partition.ResultSubpartitionView) SpillingAdaptiveSpanningRecordDeserializer(org.apache.flink.runtime.io.network.api.serialization.SpillingAdaptiveSpanningRecordDeserializer) NoOpBufferAvailablityListener(org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener) ArrayDeque(java.util.ArrayDeque) ResultPartition(org.apache.flink.runtime.io.network.partition.ResultPartition) Test(org.junit.Test)

Example 29 with ResultSubpartitionView

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

the class RecordWriterTest method testBroadcastEventNoRecords.

// ---------------------------------------------------------------------------------------------
// Resource release tests
// ---------------------------------------------------------------------------------------------
/**
 * Tests broadcasting events when no records have been emitted yet.
 */
@Test
public void testBroadcastEventNoRecords() throws Exception {
    int numberOfChannels = 4;
    int bufferSize = 32;
    ResultPartition partition = createResultPartition(bufferSize, numberOfChannels);
    RecordWriter<ByteArrayIO> writer = createRecordWriter(partition);
    CheckpointBarrier barrier = new CheckpointBarrier(Integer.MAX_VALUE + 919192L, Integer.MAX_VALUE + 18828228L, CheckpointOptions.forCheckpointWithDefaultLocation());
    // No records emitted yet, broadcast should not request a buffer
    writer.broadcastEvent(barrier);
    assertEquals(0, partition.getBufferPool().bestEffortGetNumOfUsedBuffers());
    for (int i = 0; i < numberOfChannels; i++) {
        assertEquals(1, partition.getNumberOfQueuedBuffers(i));
        ResultSubpartitionView view = partition.createSubpartitionView(i, new NoOpBufferAvailablityListener());
        BufferOrEvent boe = parseBuffer(view.getNextBuffer().buffer(), i);
        assertTrue(boe.isEvent());
        assertEquals(barrier, boe.getEvent());
        assertFalse(view.getAvailabilityAndBacklog(Integer.MAX_VALUE).isAvailable());
    }
}
Also used : CheckpointBarrier(org.apache.flink.runtime.io.network.api.CheckpointBarrier) 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) BufferOrEvent(org.apache.flink.runtime.io.network.partition.consumer.BufferOrEvent) Test(org.junit.Test)

Example 30 with ResultSubpartitionView

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

the class PartitionRequestQueueTest method testEnqueueReaderByNotifyingEventBuffer.

/**
 * Tests {@link PartitionRequestQueue#enqueueAvailableReader(NetworkSequenceViewReader)},
 * verifying the reader would be enqueued in the pipeline if the next sending buffer is an event
 * even though it has no available credits.
 */
@Test
public void testEnqueueReaderByNotifyingEventBuffer() throws Exception {
    // setup
    final ResultSubpartitionView view = new NextIsEventResultSubpartitionView();
    ResultPartitionProvider partitionProvider = (partitionId, index, availabilityListener) -> view;
    final InputChannelID receiverId = new InputChannelID();
    final PartitionRequestQueue queue = new PartitionRequestQueue();
    final CreditBasedSequenceNumberingViewReader reader = new CreditBasedSequenceNumberingViewReader(receiverId, 0, queue);
    final EmbeddedChannel channel = new EmbeddedChannel(queue);
    reader.requestSubpartitionView(partitionProvider, new ResultPartitionID(), 0);
    // block the channel so that we see an intermediate state in the test
    ByteBuf channelBlockingBuffer = blockChannel(channel);
    assertNull(channel.readOutbound());
    // Notify an available event buffer to trigger enqueue the reader
    reader.notifyDataAvailable();
    channel.runPendingTasks();
    // The reader is enqueued in the pipeline because the next buffer is an event, even though
    // no credits are available
    // contains only (this) one!
    assertThat(queue.getAvailableReaders(), contains(reader));
    assertEquals(0, reader.getNumCreditsAvailable());
    // Flush the buffer to make the channel writable again and see the final results
    channel.flush();
    assertSame(channelBlockingBuffer, channel.readOutbound());
    assertEquals(0, queue.getAvailableReaders().size());
    assertEquals(0, reader.getNumCreditsAvailable());
    assertNull(channel.readOutbound());
}
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) InputChannelID(org.apache.flink.runtime.io.network.partition.consumer.InputChannelID) EmbeddedChannel(org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel) ResultPartitionID(org.apache.flink.runtime.io.network.partition.ResultPartitionID) ResultPartitionProvider(org.apache.flink.runtime.io.network.partition.ResultPartitionProvider) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) Test(org.junit.Test) PipelinedSubpartitionTest(org.apache.flink.runtime.io.network.partition.PipelinedSubpartitionTest)

Aggregations

ResultSubpartitionView (org.apache.flink.runtime.io.network.partition.ResultSubpartitionView)32 Test (org.junit.Test)22 NoOpBufferAvailablityListener (org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener)15 ResultPartition (org.apache.flink.runtime.io.network.partition.ResultPartition)13 Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)12 BufferAvailabilityListener (org.apache.flink.runtime.io.network.partition.BufferAvailabilityListener)10 ResultPartitionID (org.apache.flink.runtime.io.network.partition.ResultPartitionID)10 IOException (java.io.IOException)9 ResultPartitionManager (org.apache.flink.runtime.io.network.partition.ResultPartitionManager)9 InputChannelID (org.apache.flink.runtime.io.network.partition.consumer.InputChannelID)8 InputChannelTestUtils.createLocalInputChannel (org.apache.flink.runtime.io.network.partition.InputChannelTestUtils.createLocalInputChannel)6 BufferAndBacklog (org.apache.flink.runtime.io.network.partition.ResultSubpartition.BufferAndBacklog)6 TestingResultPartitionManager (org.apache.flink.runtime.io.network.partition.consumer.SingleInputGateTest.TestingResultPartitionManager)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 CancelTaskException (org.apache.flink.runtime.execution.CancelTaskException)5 NoOpFileChannelManager (org.apache.flink.runtime.io.disk.NoOpFileChannelManager)5 CheckpointBarrier (org.apache.flink.runtime.io.network.api.CheckpointBarrier)5 ResultPartitionType (org.apache.flink.runtime.io.network.partition.ResultPartitionType)5 TestBufferFactory (org.apache.flink.runtime.io.network.util.TestBufferFactory)5 ByteBuffer (java.nio.ByteBuffer)4