You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2021/03/14 03:38:27 UTC
[GitHub] [flink] wsry opened a new pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
wsry opened a new pull request #15192:
URL: https://github.com/apache/flink/pull/15192
## What is the purpose of the change
Currently, the sort-merge shuffle implementation uses 4M unmanaged direct memory as cache for data writing. This patch replaces the cache with writev system call which can reduce the unmanaged direct memory usage without any performance loss.
## Brief change log
- Remove the data writing cache in PartitionedFileWriter;
- Allocate a collection of network buffers as write buffer;
- Bulk write the data out when the write buffer is full.
## Verifying this change
This change is already covered by existing tests, such as PartitionedFileWriteReadTest, SortMergeResultPartitionTest and BlockingShuffleITCase.
## Does this pull request potentially affect one of the following parts:
- Dependencies (does it add or upgrade a dependency): (yes / **no**)
- The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (yes / **no**)
- The serializers: (yes / **no** / don't know)
- The runtime per-record code paths (performance sensitive): (yes / **no** / don't know)
- Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn/Mesos, ZooKeeper: (yes / **no** / don't know)
- The S3 file system connector: (yes / **no** / don't know)
## Documentation
- Does this pull request introduce a new feature? (yes / **no**)
- If yes, how is the feature documented? (**not applicable** / docs / JavaDocs / not documented)
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] gaoyunhaii commented on a change in pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
gaoyunhaii commented on a change in pull request #15192:
URL: https://github.com/apache/flink/pull/15192#discussion_r594192707
##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferReaderWriterUtil.java
##########
@@ -142,31 +137,12 @@ static long writeToByteChannelIfBelowSize(
return -1L;
}
- static long writeToByteChannel(
- FileChannel channel,
- Buffer buffer,
- ByteBuffer writeDataCache,
- ByteBuffer[] arrayWithHeaderBuffer)
- throws IOException {
-
- final long bytesToWrite = HEADER_LENGTH + buffer.readableBytes();
- if (bytesToWrite > writeDataCache.remaining()) {
- writeDataCache.flip();
- writeBuffer(channel, writeDataCache);
- writeDataCache.clear();
- }
-
- if (bytesToWrite > writeDataCache.remaining()) {
- return writeToByteChannel(channel, buffer, arrayWithHeaderBuffer);
- }
-
- writeDataCache.putShort(buffer.isBuffer() ? HEADER_VALUE_IS_BUFFER : HEADER_VALUE_IS_EVENT);
- writeDataCache.putShort(
- buffer.isCompressed() ? BUFFER_IS_COMPRESSED : BUFFER_IS_NOT_COMPRESSED);
- writeDataCache.putInt(buffer.getSize());
- writeDataCache.put(buffer.getNioBufferReadable());
-
- return bytesToWrite;
+ static void getByteChannelBufferHeader(Buffer buffer, ByteBuffer header) {
Review comment:
Might change to `setByteChannelBufferHeader` ?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] wsry commented on a change in pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
wsry commented on a change in pull request #15192:
URL: https://github.com/apache/flink/pull/15192#discussion_r595705324
##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/PartitionedFileWriter.java
##########
@@ -202,26 +190,51 @@ private void flushIndexBuffer() throws IOException {
}
/**
- * Writes a {@link Buffer} of the given subpartition to the this {@link PartitionedFile}. In a
- * data region, all data of the same subpartition must be written together.
+ * Writes a list of {@link Buffer}s to this {@link PartitionedFile}. It guarantees that after
+ * the return of this method, the target buffers can be released. In a data region, all data of
+ * the same subpartition must be written together.
*
- * <p>Note: The caller is responsible for recycling the target buffer and releasing the failed
+ * <p>Note: The caller is responsible for recycling the target buffers and releasing the failed
* {@link PartitionedFile} if any exception occurs.
*/
- public void writeBuffer(Buffer target, int targetSubpartition) throws IOException {
+ public void writeBuffers(List<SortBuffer.BufferWithChannel> bufferWithChannels)
+ throws IOException {
checkState(!isFinished, "File writer is already finished.");
checkState(!isClosed, "File writer is already closed.");
- if (targetSubpartition != currentSubpartition) {
- checkState(
- subpartitionBuffers[targetSubpartition] == 0,
- "Must write data of the same channel together.");
- subpartitionOffsets[targetSubpartition] = totalBytesWritten;
- currentSubpartition = targetSubpartition;
+ if (bufferWithChannels.isEmpty()) {
+ return;
}
- totalBytesWritten += writeToByteChannel(dataFileChannel, target, writeDataCache, header);
- ++subpartitionBuffers[targetSubpartition];
+ long expectedBytes = 0;
+ ByteBuffer[] bufferWithHeaders = new ByteBuffer[2 * bufferWithChannels.size()];
+
+ for (int i = 0; i < bufferWithChannels.size(); i++) {
+ SortBuffer.BufferWithChannel bufferWithChannel = bufferWithChannels.get(i);
+ Buffer buffer = bufferWithChannel.getBuffer();
+ int subpartitionIndex = bufferWithChannel.getChannelIndex();
+ if (subpartitionIndex != currentSubpartition) {
+ checkState(
+ subpartitionBuffers[subpartitionIndex] == 0,
+ "Must write data of the same channel together.");
+ subpartitionOffsets[subpartitionIndex] = totalBytesWritten;
+ currentSubpartition = subpartitionIndex;
+ }
+
+ ByteBuffer header = BufferReaderWriterUtil.allocatedHeaderBuffer();
+ BufferReaderWriterUtil.getByteChannelBufferHeader(buffer, header);
+ bufferWithHeaders[2 * i] = header;
+ bufferWithHeaders[2 * i + 1] = buffer.getNioBufferReadable();
+
+ int numBytes = header.remaining() + buffer.readableBytes();
+ expectedBytes += numBytes;
+ totalBytesWritten += numBytes;
+ ++subpartitionBuffers[subpartitionIndex];
+ }
+
+ if (dataFileChannel.write(bufferWithHeaders) < expectedBytes) {
Review comment:
Document of FileChannel.write does not say it guarantees to write all data out. BufferReaderWriterUtil already does the same thing and there is some comments explaining why we do that. I can extract the logic and the corresponding comments in BufferReaderWriterUtil to a method and directly call that method here.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] flinkbot edited a comment on pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #15192:
URL: https://github.com/apache/flink/pull/15192#issuecomment-798828549
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14606",
"triggerID" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* b33ebbeb181bb30510dcd28361fb9fff39a140a2 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14606)
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run travis` re-run the last Travis build
- `@flinkbot run azure` re-run the last Azure build
</details>
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] flinkbot edited a comment on pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #15192:
URL: https://github.com/apache/flink/pull/15192#issuecomment-798828549
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"status" : "DELETED",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14606",
"triggerID" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"triggerType" : "PUSH"
}, {
"hash" : "211ec9b4915d225512d6633084ac8f28b13410c8",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14630",
"triggerID" : "211ec9b4915d225512d6633084ac8f28b13410c8",
"triggerType" : "PUSH"
}, {
"hash" : "250bb0b85e3620b36e846c4cddc95b3ea3b81b36",
"status" : "PENDING",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14864",
"triggerID" : "250bb0b85e3620b36e846c4cddc95b3ea3b81b36",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* 211ec9b4915d225512d6633084ac8f28b13410c8 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14630)
* 250bb0b85e3620b36e846c4cddc95b3ea3b81b36 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14864)
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run travis` re-run the last Travis build
- `@flinkbot run azure` re-run the last Azure build
</details>
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] flinkbot commented on pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #15192:
URL: https://github.com/apache/flink/pull/15192#issuecomment-798828549
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* b33ebbeb181bb30510dcd28361fb9fff39a140a2 UNKNOWN
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run travis` re-run the last Travis build
- `@flinkbot run azure` re-run the last Azure build
</details>
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] flinkbot edited a comment on pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #15192:
URL: https://github.com/apache/flink/pull/15192#issuecomment-798828549
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"status" : "DELETED",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14606",
"triggerID" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"triggerType" : "PUSH"
}, {
"hash" : "211ec9b4915d225512d6633084ac8f28b13410c8",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14630",
"triggerID" : "211ec9b4915d225512d6633084ac8f28b13410c8",
"triggerType" : "PUSH"
}, {
"hash" : "250bb0b85e3620b36e846c4cddc95b3ea3b81b36",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "250bb0b85e3620b36e846c4cddc95b3ea3b81b36",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* 211ec9b4915d225512d6633084ac8f28b13410c8 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14630)
* 250bb0b85e3620b36e846c4cddc95b3ea3b81b36 UNKNOWN
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run travis` re-run the last Travis build
- `@flinkbot run azure` re-run the last Azure build
</details>
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] wsry commented on a change in pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
wsry commented on a change in pull request #15192:
URL: https://github.com/apache/flink/pull/15192#discussion_r595702663
##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/SortMergeResultPartition.java
##########
@@ -212,31 +269,46 @@ private void flushCurrentSortBuffer() throws IOException {
if (currentSortBuffer.hasRemaining()) {
fileWriter.startNewRegion();
+ List<BufferWithChannel> toWrite = new ArrayList<>();
+ Queue<MemorySegment> segments = getWriteBuffers();
+
while (currentSortBuffer.hasRemaining()) {
- BufferWithChannel bufferWithChannel =
- currentSortBuffer.copyIntoSegment(writeBuffer);
- Buffer buffer = bufferWithChannel.getBuffer();
- int subpartitionIndex = bufferWithChannel.getChannelIndex();
+ if (segments.isEmpty()) {
+ fileWriter.writeBuffers(toWrite);
+ toWrite.clear();
+ segments = getWriteBuffers();
+ }
- writeCompressedBufferIfPossible(buffer, subpartitionIndex);
+ BufferWithChannel bufferWithChannel =
+ currentSortBuffer.copyIntoSegment(checkNotNull(segments.poll()));
+ toWrite.add(compressBufferIfPossible(bufferWithChannel));
}
+
+ fileWriter.writeBuffers(toWrite);
}
currentSortBuffer.release();
}
- private void writeCompressedBufferIfPossible(Buffer buffer, int targetSubpartition)
- throws IOException {
- updateStatistics(buffer, targetSubpartition);
+ private Queue<MemorySegment> getWriteBuffers() {
+ synchronized (lock) {
+ checkState(!writeBuffers.isEmpty(), "Task has been canceled.");
+ return new ArrayDeque<>(writeBuffers);
+ }
+ }
- try {
- if (canBeCompressed(buffer)) {
- buffer = bufferCompressor.compressToIntermediateBuffer(buffer);
- }
- fileWriter.writeBuffer(buffer, targetSubpartition);
- } finally {
- buffer.recycleBuffer();
+ private BufferWithChannel compressBufferIfPossible(BufferWithChannel bufferWithChannel) {
+ Buffer buffer = bufferWithChannel.getBuffer();
+ int channelIndex = bufferWithChannel.getChannelIndex();
+
+ updateStatistics(buffer, channelIndex);
+
+ if (!canBeCompressed(buffer)) {
+ return bufferWithChannel;
}
+
+ buffer = checkNotNull(bufferCompressor).compressToOriginalBuffer(buffer);
Review comment:
We have only one IntermediateBuffer in the compressor, after this patch, we may cache multiple data buffers in the result partition, so the single IntermediateBuffer can not be shared by multiple buffers.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] flinkbot edited a comment on pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #15192:
URL: https://github.com/apache/flink/pull/15192#issuecomment-798828549
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"status" : "DELETED",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14606",
"triggerID" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"triggerType" : "PUSH"
}, {
"hash" : "211ec9b4915d225512d6633084ac8f28b13410c8",
"status" : "DELETED",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14630",
"triggerID" : "211ec9b4915d225512d6633084ac8f28b13410c8",
"triggerType" : "PUSH"
}, {
"hash" : "250bb0b85e3620b36e846c4cddc95b3ea3b81b36",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14864",
"triggerID" : "250bb0b85e3620b36e846c4cddc95b3ea3b81b36",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* 250bb0b85e3620b36e846c4cddc95b3ea3b81b36 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14864)
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run travis` re-run the last Travis build
- `@flinkbot run azure` re-run the last Azure build
</details>
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] flinkbot edited a comment on pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #15192:
URL: https://github.com/apache/flink/pull/15192#issuecomment-798828549
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14606",
"triggerID" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"triggerType" : "PUSH"
}, {
"hash" : "211ec9b4915d225512d6633084ac8f28b13410c8",
"status" : "PENDING",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14630",
"triggerID" : "211ec9b4915d225512d6633084ac8f28b13410c8",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* b33ebbeb181bb30510dcd28361fb9fff39a140a2 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14606)
* 211ec9b4915d225512d6633084ac8f28b13410c8 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14630)
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run travis` re-run the last Travis build
- `@flinkbot run azure` re-run the last Azure build
</details>
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] zhuzhurk commented on a change in pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
zhuzhurk commented on a change in pull request #15192:
URL: https://github.com/apache/flink/pull/15192#discussion_r594963510
##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/PartitionedFileWriter.java
##########
@@ -202,26 +190,51 @@ private void flushIndexBuffer() throws IOException {
}
/**
- * Writes a {@link Buffer} of the given subpartition to the this {@link PartitionedFile}. In a
- * data region, all data of the same subpartition must be written together.
+ * Writes a list of {@link Buffer}s to this {@link PartitionedFile}. It guarantees that after
+ * the return of this method, the target buffers can be released. In a data region, all data of
+ * the same subpartition must be written together.
*
- * <p>Note: The caller is responsible for recycling the target buffer and releasing the failed
+ * <p>Note: The caller is responsible for recycling the target buffers and releasing the failed
* {@link PartitionedFile} if any exception occurs.
*/
- public void writeBuffer(Buffer target, int targetSubpartition) throws IOException {
+ public void writeBuffers(List<SortBuffer.BufferWithChannel> bufferWithChannels)
+ throws IOException {
checkState(!isFinished, "File writer is already finished.");
checkState(!isClosed, "File writer is already closed.");
- if (targetSubpartition != currentSubpartition) {
- checkState(
- subpartitionBuffers[targetSubpartition] == 0,
- "Must write data of the same channel together.");
- subpartitionOffsets[targetSubpartition] = totalBytesWritten;
- currentSubpartition = targetSubpartition;
+ if (bufferWithChannels.isEmpty()) {
+ return;
}
- totalBytesWritten += writeToByteChannel(dataFileChannel, target, writeDataCache, header);
- ++subpartitionBuffers[targetSubpartition];
+ long expectedBytes = 0;
+ ByteBuffer[] bufferWithHeaders = new ByteBuffer[2 * bufferWithChannels.size()];
+
+ for (int i = 0; i < bufferWithChannels.size(); i++) {
+ SortBuffer.BufferWithChannel bufferWithChannel = bufferWithChannels.get(i);
+ Buffer buffer = bufferWithChannel.getBuffer();
+ int subpartitionIndex = bufferWithChannel.getChannelIndex();
+ if (subpartitionIndex != currentSubpartition) {
+ checkState(
+ subpartitionBuffers[subpartitionIndex] == 0,
+ "Must write data of the same channel together.");
+ subpartitionOffsets[subpartitionIndex] = totalBytesWritten;
+ currentSubpartition = subpartitionIndex;
+ }
+
+ ByteBuffer header = BufferReaderWriterUtil.allocatedHeaderBuffer();
+ BufferReaderWriterUtil.getByteChannelBufferHeader(buffer, header);
+ bufferWithHeaders[2 * i] = header;
+ bufferWithHeaders[2 * i + 1] = buffer.getNioBufferReadable();
+
+ int numBytes = header.remaining() + buffer.readableBytes();
+ expectedBytes += numBytes;
+ totalBytesWritten += numBytes;
+ ++subpartitionBuffers[subpartitionIndex];
+ }
+
+ if (dataFileChannel.write(bufferWithHeaders) < expectedBytes) {
Review comment:
is it possible that the buffers are partially written into the file?
I'm also wondering whether this check is needed because we did not do it for `FileChannel.writeBuffer(buffer)`.
##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/SortMergeResultPartition.java
##########
@@ -212,31 +269,46 @@ private void flushCurrentSortBuffer() throws IOException {
if (currentSortBuffer.hasRemaining()) {
fileWriter.startNewRegion();
+ List<BufferWithChannel> toWrite = new ArrayList<>();
+ Queue<MemorySegment> segments = getWriteBuffers();
+
while (currentSortBuffer.hasRemaining()) {
- BufferWithChannel bufferWithChannel =
- currentSortBuffer.copyIntoSegment(writeBuffer);
- Buffer buffer = bufferWithChannel.getBuffer();
- int subpartitionIndex = bufferWithChannel.getChannelIndex();
+ if (segments.isEmpty()) {
+ fileWriter.writeBuffers(toWrite);
+ toWrite.clear();
+ segments = getWriteBuffers();
+ }
- writeCompressedBufferIfPossible(buffer, subpartitionIndex);
+ BufferWithChannel bufferWithChannel =
+ currentSortBuffer.copyIntoSegment(checkNotNull(segments.poll()));
+ toWrite.add(compressBufferIfPossible(bufferWithChannel));
}
+
+ fileWriter.writeBuffers(toWrite);
}
currentSortBuffer.release();
}
- private void writeCompressedBufferIfPossible(Buffer buffer, int targetSubpartition)
- throws IOException {
- updateStatistics(buffer, targetSubpartition);
+ private Queue<MemorySegment> getWriteBuffers() {
+ synchronized (lock) {
+ checkState(!writeBuffers.isEmpty(), "Task has been canceled.");
+ return new ArrayDeque<>(writeBuffers);
+ }
+ }
- try {
- if (canBeCompressed(buffer)) {
- buffer = bufferCompressor.compressToIntermediateBuffer(buffer);
- }
- fileWriter.writeBuffer(buffer, targetSubpartition);
- } finally {
- buffer.recycleBuffer();
+ private BufferWithChannel compressBufferIfPossible(BufferWithChannel bufferWithChannel) {
+ Buffer buffer = bufferWithChannel.getBuffer();
+ int channelIndex = bufferWithChannel.getChannelIndex();
+
+ updateStatistics(buffer, channelIndex);
+
+ if (!canBeCompressed(buffer)) {
+ return bufferWithChannel;
}
+
+ buffer = checkNotNull(bufferCompressor).compressToOriginalBuffer(buffer);
Review comment:
why do we need to change `compressToIntermediateBuffer` to `compressToOriginalBuffer`?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] zhuzhurk merged pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
zhuzhurk merged pull request #15192:
URL: https://github.com/apache/flink/pull/15192
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] flinkbot edited a comment on pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #15192:
URL: https://github.com/apache/flink/pull/15192#issuecomment-798828549
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"status" : "DELETED",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14606",
"triggerID" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"triggerType" : "PUSH"
}, {
"hash" : "211ec9b4915d225512d6633084ac8f28b13410c8",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14630",
"triggerID" : "211ec9b4915d225512d6633084ac8f28b13410c8",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* 211ec9b4915d225512d6633084ac8f28b13410c8 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14630)
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run travis` re-run the last Travis build
- `@flinkbot run azure` re-run the last Azure build
</details>
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] flinkbot edited a comment on pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #15192:
URL: https://github.com/apache/flink/pull/15192#issuecomment-798828549
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"status" : "PENDING",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14606",
"triggerID" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* b33ebbeb181bb30510dcd28361fb9fff39a140a2 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14606)
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run travis` re-run the last Travis build
- `@flinkbot run azure` re-run the last Azure build
</details>
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] flinkbot edited a comment on pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #15192:
URL: https://github.com/apache/flink/pull/15192#issuecomment-798828549
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14606",
"triggerID" : "b33ebbeb181bb30510dcd28361fb9fff39a140a2",
"triggerType" : "PUSH"
}, {
"hash" : "211ec9b4915d225512d6633084ac8f28b13410c8",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "211ec9b4915d225512d6633084ac8f28b13410c8",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* b33ebbeb181bb30510dcd28361fb9fff39a140a2 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=14606)
* 211ec9b4915d225512d6633084ac8f28b13410c8 UNKNOWN
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run travis` re-run the last Travis build
- `@flinkbot run azure` re-run the last Azure build
</details>
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] gaoyunhaii commented on a change in pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
gaoyunhaii commented on a change in pull request #15192:
URL: https://github.com/apache/flink/pull/15192#discussion_r594938336
##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/SortMergeResultPartition.java
##########
@@ -70,15 +81,18 @@
/** Number of data buffers (excluding events) written for each subpartition. */
private final int[] numDataBuffers;
- /** A piece of unmanaged memory for data writing. */
- private final MemorySegment writeBuffer;
+ /** Buffers cut from the network buffer pool for data writing. */
+ private final List<MemorySegment> writeBuffers = new ArrayList<>();
Review comment:
@GuardBy("lock") ?
##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/SortMergeResultPartition.java
##########
@@ -212,31 +269,46 @@ private void flushCurrentSortBuffer() throws IOException {
if (currentSortBuffer.hasRemaining()) {
fileWriter.startNewRegion();
+ List<BufferWithChannel> toWrite = new ArrayList<>();
+ Queue<MemorySegment> segments = getWriteBuffers();
+
while (currentSortBuffer.hasRemaining()) {
- BufferWithChannel bufferWithChannel =
- currentSortBuffer.copyIntoSegment(writeBuffer);
- Buffer buffer = bufferWithChannel.getBuffer();
- int subpartitionIndex = bufferWithChannel.getChannelIndex();
+ if (segments.isEmpty()) {
+ fileWriter.writeBuffers(toWrite);
+ toWrite.clear();
+ segments = getWriteBuffers();
+ }
- writeCompressedBufferIfPossible(buffer, subpartitionIndex);
+ BufferWithChannel bufferWithChannel =
+ currentSortBuffer.copyIntoSegment(checkNotNull(segments.poll()));
+ toWrite.add(compressBufferIfPossible(bufferWithChannel));
}
+
+ fileWriter.writeBuffers(toWrite);
}
currentSortBuffer.release();
}
- private void writeCompressedBufferIfPossible(Buffer buffer, int targetSubpartition)
- throws IOException {
- updateStatistics(buffer, targetSubpartition);
+ private Queue<MemorySegment> getWriteBuffers() {
+ synchronized (lock) {
+ checkState(!writeBuffers.isEmpty(), "Task has been canceled.");
+ return new ArrayDeque<>(writeBuffers);
+ }
+ }
- try {
- if (canBeCompressed(buffer)) {
- buffer = bufferCompressor.compressToIntermediateBuffer(buffer);
- }
- fileWriter.writeBuffer(buffer, targetSubpartition);
- } finally {
- buffer.recycleBuffer();
+ private BufferWithChannel compressBufferIfPossible(BufferWithChannel bufferWithChannel) {
+ Buffer buffer = bufferWithChannel.getBuffer();
+ int channelIndex = bufferWithChannel.getChannelIndex();
+
+ updateStatistics(buffer, channelIndex);
Review comment:
After the modification, `updateStatistics` method called here seems to be a bit inconsistent with the name of this method. We might move the statistics out.
##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/SortMergeResultPartition.java
##########
@@ -289,8 +374,20 @@ public void finish() throws IOException {
super.finish();
}
+ private void releaseWriteBuffers() {
+ synchronized (lock) {
+ if (bufferPool != null) {
+ for (MemorySegment segment : writeBuffers) {
+ bufferPool.recycle(segment);
+ }
+ writeBuffers.clear();
+ }
+ }
+ }
+
@Override
public void close() {
+ releaseWriteBuffers();
releaseCurrentSortBuffer();
Review comment:
Although not fully related with this PR, currently the sort buffer seems to have different treatment with the write buffers on lock. We might add a comment on there would always be a close() call from the main thread or we might change currentSortBuffer to be volatile.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] flinkbot commented on pull request #15192: [FLINK-21777][network] Replace the 4M data writing cache of sort-merge shuffle with writev system call
Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #15192:
URL: https://github.com/apache/flink/pull/15192#issuecomment-798827237
Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community
to review your pull request. We will use this comment to track the progress of the review.
## Automated Checks
Last check on commit b33ebbeb181bb30510dcd28361fb9fff39a140a2 (Sun Mar 14 03:40:17 UTC 2021)
**Warnings:**
* No documentation files were touched! Remember to keep the Flink docs up to date!
* **This pull request references an unassigned [Jira ticket](https://issues.apache.org/jira/browse/FLINK-21777).** According to the [code contribution guide](https://flink.apache.org/contributing/contribute-code.html), tickets need to be assigned before starting with the implementation work.
<sub>Mention the bot in a comment to re-run the automated checks.</sub>
## Review Progress
* ❓ 1. The [description] looks good.
* ❓ 2. There is [consensus] that the contribution should go into to Flink.
* ❓ 3. Needs [attention] from.
* ❓ 4. The change fits into the overall [architecture].
* ❓ 5. Overall code [quality] is good.
Please see the [Pull Request Review Guide](https://flink.apache.org/contributing/reviewing-prs.html) for a full explanation of the review process.<details>
The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required <summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot approve description` to approve one or more aspects (aspects: `description`, `consensus`, `architecture` and `quality`)
- `@flinkbot approve all` to approve all aspects
- `@flinkbot approve-until architecture` to approve everything until `architecture`
- `@flinkbot attention @username1 [@username2 ..]` to require somebody's attention
- `@flinkbot disapprove architecture` to remove an approval you gave earlier
</details>
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org