Search in sources :

Example 6 with NetworkBuffer

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

the class ChannelStateCheckpointWriterTest method testSmallFilesNotWritten.

@Test
@SuppressWarnings("ConstantConditions")
public void testSmallFilesNotWritten() throws Exception {
    int threshold = 100;
    File checkpointsDir = temporaryFolder.newFolder("checkpointsDir");
    File sharedStateDir = temporaryFolder.newFolder("sharedStateDir");
    FsCheckpointStreamFactory checkpointStreamFactory = new FsCheckpointStreamFactory(getSharedInstance(), fromLocalFile(checkpointsDir), fromLocalFile(sharedStateDir), threshold, threshold);
    ChannelStateWriteResult result = new ChannelStateWriteResult();
    ChannelStateCheckpointWriter writer = createWriter(result, checkpointStreamFactory.createCheckpointStateOutputStream(EXCLUSIVE));
    NetworkBuffer buffer = new NetworkBuffer(MemorySegmentFactory.allocateUnpooledSegment(threshold / 2), FreeingBufferRecycler.INSTANCE);
    writer.writeInput(new InputChannelInfo(1, 2), buffer);
    writer.completeOutput();
    writer.completeInput();
    assertTrue(result.isDone());
    assertEquals(0, checkpointsDir.list().length);
    assertEquals(0, sharedStateDir.list().length);
}
Also used : FsCheckpointStreamFactory(org.apache.flink.runtime.state.filesystem.FsCheckpointStreamFactory) NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) File(java.io.File) Path.fromLocalFile(org.apache.flink.core.fs.Path.fromLocalFile) ChannelStateWriteResult(org.apache.flink.runtime.checkpoint.channel.ChannelStateWriter.ChannelStateWriteResult) Test(org.junit.Test)

Example 7 with NetworkBuffer

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

the class ChannelStateCheckpointWriterTest method write.

private void write(ChannelStateCheckpointWriter writer, InputChannelInfo channelInfo, byte[] data) throws Exception {
    MemorySegment segment = wrap(data);
    NetworkBuffer buffer = new NetworkBuffer(segment, FreeingBufferRecycler.INSTANCE, Buffer.DataType.DATA_BUFFER, segment.size());
    writer.writeInput(channelInfo, buffer);
}
Also used : NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 8 with NetworkBuffer

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

the class ChannelStateSerializerImplTest method readAndCheck.

private void readAndCheck(byte[] data, ChannelStateSerializerImpl serializer, ByteArrayInputStream is) throws IOException {
    serializer.readHeader(is);
    int size = serializer.readLength(is);
    assertEquals(data.length, size);
    NetworkBuffer buffer = new NetworkBuffer(MemorySegmentFactory.allocateUnpooledSegment(data.length), FreeingBufferRecycler.INSTANCE);
    try {
        int read = serializer.readData(is, wrap(buffer), size);
        assertEquals(size, read);
        assertArrayEquals(data, readBytes(buffer));
    } finally {
        buffer.release();
    }
}
Also used : NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer)

Example 9 with NetworkBuffer

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

the class SortMergeResultPartition method writeLargeRecord.

/**
 * Spills the large record into the target {@link PartitionedFile} as a separate data region.
 */
private void writeLargeRecord(ByteBuffer record, int targetSubpartition, DataType dataType, boolean isBroadcast) throws IOException {
    // for the hash-based data buffer implementation, a large record will be appended to the
    // data buffer directly and spilled to multiple data regions
    checkState(!useHashBuffer, "No buffers available for writing.");
    fileWriter.startNewRegion(isBroadcast);
    List<BufferWithChannel> toWrite = new ArrayList<>();
    Queue<MemorySegment> segments = new ArrayDeque<>(writeSegments);
    while (record.hasRemaining()) {
        if (segments.isEmpty()) {
            fileWriter.writeBuffers(toWrite);
            toWrite.clear();
            segments = new ArrayDeque<>(writeSegments);
        }
        int toCopy = Math.min(record.remaining(), networkBufferSize);
        MemorySegment writeBuffer = checkNotNull(segments.poll());
        writeBuffer.put(0, record, toCopy);
        NetworkBuffer buffer = new NetworkBuffer(writeBuffer, (buf) -> {
        }, dataType, toCopy);
        BufferWithChannel bufferWithChannel = new BufferWithChannel(buffer, targetSubpartition);
        updateStatistics(buffer, isBroadcast);
        toWrite.add(compressBufferIfPossible(bufferWithChannel));
    }
    fileWriter.writeBuffers(toWrite);
}
Also used : ArrayList(java.util.ArrayList) NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) MemorySegment(org.apache.flink.core.memory.MemorySegment) ArrayDeque(java.util.ArrayDeque)

Example 10 with NetworkBuffer

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

the class HashBasedDataBuffer method writeEvent.

private void writeEvent(ByteBuffer source, int targetChannel, Buffer.DataType dataType) {
    BufferBuilder builder = builders[targetChannel];
    if (builder != null) {
        builder.finish();
        buffers[targetChannel].add(builder.createBufferConsumerFromBeginning());
        builder.close();
        builders[targetChannel] = null;
    }
    MemorySegment segment = MemorySegmentFactory.allocateUnpooledOffHeapMemory(source.remaining());
    segment.put(0, source, segment.size());
    BufferConsumer consumer = new BufferConsumer(new NetworkBuffer(segment, FreeingBufferRecycler.INSTANCE, dataType), segment.size());
    buffers[targetChannel].add(consumer);
}
Also used : BufferBuilder(org.apache.flink.runtime.io.network.buffer.BufferBuilder) NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) BufferConsumer(org.apache.flink.runtime.io.network.buffer.BufferConsumer) MemorySegment(org.apache.flink.core.memory.MemorySegment)

Aggregations

NetworkBuffer (org.apache.flink.runtime.io.network.buffer.NetworkBuffer)39 MemorySegment (org.apache.flink.core.memory.MemorySegment)21 Test (org.junit.Test)14 Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)11 ByteBuffer (java.nio.ByteBuffer)8 IOException (java.io.IOException)4 ChannelStateWriteResult (org.apache.flink.runtime.checkpoint.channel.ChannelStateWriter.ChannelStateWriteResult)4 BufferConsumer (org.apache.flink.runtime.io.network.buffer.BufferConsumer)4 DataOutputStream (java.io.DataOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 ArrayDeque (java.util.ArrayDeque)2 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 Nullable (javax.annotation.Nullable)2 MemorySegmentFactory (org.apache.flink.core.memory.MemorySegmentFactory)2 NetworkBufferPool (org.apache.flink.runtime.io.network.buffer.NetworkBufferPool)2 ByteStreamStateHandle (org.apache.flink.runtime.state.memory.ByteStreamStateHandle)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertTrue (org.junit.Assert.assertTrue)2