Search in sources :

Example 66 with Buffer

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

the class EventSerializerTest method testToBuffer.

@Test
public void testToBuffer() throws IOException {
    for (AbstractEvent evt : events) {
        Buffer buffer = EventSerializer.toBuffer(evt, false);
        assertFalse(buffer.isBuffer());
        assertTrue(buffer.readableBytes() > 0);
        assertFalse(buffer.isRecycled());
        if (evt instanceof CheckpointBarrier) {
            assertTrue(buffer.getDataType().isBlockingUpstream());
        } else {
            assertEquals(Buffer.DataType.EVENT_BUFFER, buffer.getDataType());
        }
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer) CheckpointBarrier(org.apache.flink.runtime.io.network.api.CheckpointBarrier) AbstractEvent(org.apache.flink.runtime.event.AbstractEvent) Test(org.junit.Test)

Example 67 with Buffer

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

the class SpanningWrapperTest method toByteArray.

private byte[] toByteArray(CloseableIterator<Buffer> unconsumed) {
    final List<Buffer> buffers = new ArrayList<>();
    try {
        unconsumed.forEachRemaining(buffers::add);
        byte[] result = new byte[buffers.stream().mapToInt(Buffer::readableBytes).sum()];
        int offset = 0;
        for (Buffer buffer : buffers) {
            int len = buffer.readableBytes();
            buffer.getNioBuffer(0, len).get(result, offset, len);
            offset += len;
        }
        return result;
    } finally {
        buffers.forEach(Buffer::recycleBuffer);
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) ArrayList(java.util.ArrayList)

Example 68 with Buffer

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

the class SpanningWrapperTest method testLargeUnconsumedSegment.

@Test
public void testLargeUnconsumedSegment() throws Exception {
    int recordLen = 100;
    int firstChunk = (int) (recordLen * .9);
    int spillingThreshold = (int) (firstChunk * .9);
    byte[] record1 = recordBytes(recordLen);
    byte[] record2 = recordBytes(recordLen * 2);
    File canNotEecutableFile = folder.newFolder();
    canNotEecutableFile.setExecutable(false);
    // Always pick 'canNotEecutableFile' first as the Spilling Channel TmpDir. Thus trigger an
    // IOException.
    SpanningWrapper spanningWrapper = new SpanningWrapper(new String[] { folder.newFolder().getAbsolutePath(), canNotEecutableFile.getAbsolutePath() + File.separator + "pathdonotexit" }, spillingThreshold, recordLen);
    spanningWrapper.transferFrom(wrapNonSpanning(record1, firstChunk), recordLen);
    spanningWrapper.addNextChunkFromMemorySegment(wrap(record1), firstChunk, recordLen - firstChunk + LENGTH_BYTES);
    spanningWrapper.addNextChunkFromMemorySegment(wrap(record2), 0, record2.length);
    CloseableIterator<Buffer> unconsumedSegment = spanningWrapper.getUnconsumedSegment();
    spanningWrapper.getInputView().readFully(new byte[recordLen], 0, // read out from file
    recordLen);
    // clear any leftover
    spanningWrapper.transferLeftOverTo(new NonSpanningWrapper());
    spanningWrapper.transferFrom(wrapNonSpanning(recordBytes(recordLen), recordLen), // overwrite with new data
    recordLen);
    canNotEecutableFile.setExecutable(true);
    assertArrayEquals(concat(record1, record2), toByteArray(unconsumedSegment));
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) File(java.io.File) Test(org.junit.Test)

Example 69 with Buffer

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

the class ChannelPersistenceITCase method collectBytes.

private <T> byte[] collectBytes(SupplierWithException<Optional<T>, Exception> entrySupplier, Function<T, Buffer> bufferExtractor) throws Exception {
    ArrayList<Buffer> buffers = new ArrayList<>();
    for (Optional<T> entry = entrySupplier.get(); entry.isPresent(); entry = entrySupplier.get()) {
        entry.map(bufferExtractor).filter(buffer -> buffer.getDataType().isBuffer()).ifPresent(buffers::add);
    }
    ByteBuffer result = ByteBuffer.wrap(new byte[buffers.stream().mapToInt(Buffer::getSize).sum()]);
    buffers.forEach(buffer -> {
        result.put(buffer.getNioBufferReadable());
        buffer.recycleBuffer();
    });
    return result.array();
}
Also used : NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) ByteBuffer(java.nio.ByteBuffer) Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) BufferAndBacklog(org.apache.flink.runtime.io.network.partition.ResultSubpartition.BufferAndBacklog) CHECKPOINT(org.apache.flink.runtime.checkpoint.CheckpointType.CHECKPOINT) ResultPartitionType(org.apache.flink.runtime.io.network.partition.ResultPartitionType) 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) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) RECOVERY_COMPLETION(org.apache.flink.runtime.io.network.buffer.Buffer.DataType.RECOVERY_COMPLETION) ResultPartition(org.apache.flink.runtime.io.network.partition.ResultPartition) Map(java.util.Map) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) SequentialChannelStateReader(org.apache.flink.runtime.checkpoint.channel.SequentialChannelStateReader) Collections.singletonMap(java.util.Collections.singletonMap) InputChannelInfo(org.apache.flink.runtime.checkpoint.channel.InputChannelInfo) 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) NonPersistentMetadataCheckpointStorageLocation(org.apache.flink.runtime.state.memory.NonPersistentMetadataCheckpointStorageLocation) SingleInputGateBuilder(org.apache.flink.runtime.io.network.partition.consumer.SingleInputGateBuilder) ChannelStateWriterImpl(org.apache.flink.runtime.checkpoint.channel.ChannelStateWriterImpl) ResultPartitionBuilder(org.apache.flink.runtime.io.network.partition.ResultPartitionBuilder) SEQUENCE_NUMBER_UNKNOWN(org.apache.flink.runtime.checkpoint.channel.ChannelStateWriter.SEQUENCE_NUMBER_UNKNOWN) InputChannelBuilder(org.apache.flink.runtime.io.network.partition.consumer.InputChannelBuilder) FreeingBufferRecycler(org.apache.flink.runtime.io.network.buffer.FreeingBufferRecycler) Test(org.junit.Test) IOException(java.io.IOException) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) ChannelStateWriteResult(org.apache.flink.runtime.checkpoint.channel.ChannelStateWriter.ChannelStateWriteResult) CloseableIterator.ofElements(org.apache.flink.util.CloseableIterator.ofElements) BufferWritingResultPartition(org.apache.flink.runtime.io.network.partition.BufferWritingResultPartition) Collectors(java.util.stream.Collectors) Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) NoOpBufferAvailablityListener(org.apache.flink.runtime.io.network.partition.NoOpBufferAvailablityListener) Assert.assertNull(org.junit.Assert.assertNull) SequentialChannelStateReaderImpl(org.apache.flink.runtime.checkpoint.channel.SequentialChannelStateReaderImpl) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) Optional(java.util.Optional) ResultSubpartitionView(org.apache.flink.runtime.io.network.partition.ResultSubpartitionView) StateObjectCollection(org.apache.flink.runtime.checkpoint.StateObjectCollection) BufferOrEvent(org.apache.flink.runtime.io.network.partition.consumer.BufferOrEvent) Assert.assertEquals(org.junit.Assert.assertEquals) ResultSubpartitionInfo(org.apache.flink.runtime.checkpoint.channel.ResultSubpartitionInfo) SupplierWithException(org.apache.flink.util.function.SupplierWithException) CHECKPOINT(org.apache.flink.runtime.checkpoint.CheckpointType.CHECKPOINT) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer)

Example 70 with Buffer

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

the class DemultiplexingRecordDeserializerTest method testWatermarks.

/**
 * Tests that Watermarks are only forwarded when all watermarks are received.
 */
@Test
public void testWatermarks() throws IOException {
    DemultiplexingRecordDeserializer<Long> deserializer = DemultiplexingRecordDeserializer.create(new InputChannelInfo(0, 0), rescalingDescriptor(to(0, 1), array(mappings(to(0, 1), to(4, 5))), emptySet()), unused -> new SpillingAdaptiveSpanningRecordDeserializer<>(ioManager.getSpillingDirectoriesPaths()), unused -> RecordFilter.all());
    assertEquals(4, deserializer.getVirtualChannelSelectors().size());
    for (Iterator<SubtaskConnectionDescriptor> iterator = deserializer.getVirtualChannelSelectors().iterator(); iterator.hasNext(); ) {
        SubtaskConnectionDescriptor selector = iterator.next();
        MemorySegment memorySegment = allocateUnpooledSegment(128);
        try (BufferBuilder bufferBuilder = createBufferBuilder(memorySegment)) {
            final long ts = 42L + selector.getInputSubtaskIndex() + selector.getOutputSubtaskIndex();
            Buffer buffer = write(bufferBuilder, new Watermark(ts));
            deserializer.select(selector);
            deserializer.setNextBuffer(buffer);
        }
        if (iterator.hasNext()) {
            assertEquals(Collections.emptyList(), read(deserializer));
        } else {
            // last channel, min should be 42 + 0 + 0
            assertEquals(Arrays.asList(new Watermark(42)), read(deserializer));
        }
        assertTrue(memorySegment.isFreed());
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) InputChannelInfo(org.apache.flink.runtime.checkpoint.channel.InputChannelInfo) BufferBuilderTestUtils.createBufferBuilder(org.apache.flink.runtime.io.network.buffer.BufferBuilderTestUtils.createBufferBuilder) BufferBuilder(org.apache.flink.runtime.io.network.buffer.BufferBuilder) SubtaskConnectionDescriptor(org.apache.flink.runtime.io.network.api.SubtaskConnectionDescriptor) Watermark(org.apache.flink.streaming.api.watermark.Watermark) MemorySegment(org.apache.flink.core.memory.MemorySegment) Test(org.junit.Test)

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