GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — [SPARK-56279][CORE] Enable zero-copy sendfile for FileRegion in native Netty transports by LuciferYang · Pull Request #55087 · apache/spark Conversation
|
| Scenario | Before (ms) | After (ms) | Improvement |
|---|---|---|---|
| AUTO, sequential fetch (JDK17) | 2075 | 362 | ~5.7x faster |
| AUTO, parallel fetch (JDK17) | 676 | 202 | ~3.3x faster |
| AUTO, sequential fetch (JDK21) | 2093 | 360 | ~5.8x faster |
| AUTO, parallel fetch (JDK21) | 677 | 204 | ~3.3x faster |
| AUTO, sequential fetch (JDK25) | 1918 | 411 | ~4.7x faster |
| AUTO, parallel fetch (JDK25) | 652 | 196 | ~3.3x faster |
With this fix, AUTO (EPOLL) mode now matches or exceeds NIO performance for file-backed shuffle fetches, achieving the expected ~1.0X relative performance (previously ~0.2X).
No. This is an internal optimization to the Netty transport layer. Users benefit from improved shuffle fetch performance when using native transports (the default on Linux) without any configuration changes.
MergedBlockMetaSuccessSuite to validate the new encoder output format (2 separate objects: ByteBuf header + DefaultFileRegion, instead of a single MessageWithHeader).NettyTransportBenchmark across JDK 17, JDK 21, and JDK 25 to confirm the performance improvement. Updated benchmark result files accordingly.Generated-by: Claude Code 4.6
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| AUTO, parallel fetch (4 clients) 677 787 113 0.0 6769621.7 0.5X | ||
| NIO, sequential fetch 366 372 9 0.0 3655386.3 1.0X | ||
| NIO, parallel fetch (4 clients) 200 211 11 0.0 2002026.0 1.8X | ||
| AUTO, sequential fetch 360 369 13 0.0 3600497.3 1.0X |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Nice!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| AUTO, parallel fetch (4 clients) 676 814 129 0.0 6757222.9 0.6X | ||
| NIO, sequential fetch 384 386 3 0.0 3844405.1 1.0X | ||
| NIO, parallel fetch (4 clients) 209 217 9 0.0 2091358.0 1.8X | ||
| AUTO, sequential fetch 362 372 14 0.0 3615964.2 1.1X |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Nice, too.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| AUTO, parallel fetch (4 clients) 652 730 116 0.0 6517014.3 0.6X | ||
| NIO, sequential fetch 411 419 13 0.0 4112689.7 1.0X | ||
| NIO, parallel fetch (4 clients) 213 214 1 0.0 2131475.4 1.9X | ||
| AUTO, sequential fetch 411 416 4 0.0 4112489.8 1.0X |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Nice!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much, @LuciferYang !
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| out.add(new MessageWithHeader(in.body(), header, body, bodyLength)); | ||
| if (body instanceof FileRegion && in.body() instanceof FileSegmentManagedBuffer) { | ||
| // Emit header and FileRegion as separate objects so that native transports | ||
| // (EPOLL, KQUEUE) can apply zero-copy sendfile/splice on the FileRegion directly. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thank you for adding this comment. It's really tricky.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
cc @yaooqinn , too |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Additionally, I have also submitted a PR in the Netty community to explore the possibility of using io_uring in Apache Spark going forward. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
+1, LGTM (Pending CIs).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I converted SPARK-56279 as a bug because this was a regression at 4.1.x. Can we have this at branch-4.1 too, @LuciferYang ? I'm also okay to backport the Netty benchmark to branch-4.1 too. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Okay, I will manually check them later and backport them to branch-4.1. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thank you so much! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
All test passed |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Merged into master. Thanks @dongjoon-hyun |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…e Netty transports This PR modifies `MessageEncoder` to emit the header `ByteBuf` and `FileRegion` as **separate objects** in the outbound message list when the body is a `FileRegion` backed by `FileSegmentManagedBuffer`, instead of wrapping them together in a `MessageWithHeader`. Previously, all messages with a body were unconditionally wrapped in `MessageWithHeader`. This caused native transports (EPOLL, KQUEUE) to fall into a generic `FileRegion.transferTo()` fallback path that copies data through user-space, bypassing the optimized `sendfile()` / `splice()` zero-copy path that Netty's native transports provide. The split is only applied when the `ManagedBuffer` is a `FileSegmentManagedBuffer`, whose `release()` is a no-op, making it safe to emit the `FileRegion` independently of write lifecycle management. Other `ManagedBuffer` types (e.g., `BlockManagerManagedBuffer`) still use the `MessageWithHeader` wrapper because they perform resource cleanup in `release()` that must be tied to `MessageWithHeader.deallocate()`. When using native transports (AUTO/EPOLL on Linux), file-backed shuffle fetch performance was severely degraded compared to NIO mode. The root cause lies in how Netty's native transports dispatch `FileRegion` writes. In `AbstractEpollStreamChannel.doWriteSingle()` (and the analogous KQueue path), Netty uses an `instanceof` check to choose between two write strategies: https://github.com/netty/netty/blob/eeb5674526f0b49a142580686a5a9a7147ddadec/transport-classes-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollStreamChannel.java#L474-L493 ```java } else if (msg instanceof DefaultFileRegion) { return writeDefaultFileRegion(in, (DefaultFileRegion) msg); // → socket.sendFile() (zero-copy) } else if (msg instanceof FileRegion) { return writeFileRegion(in, (FileRegion) msg); // → region.transferTo() (user-space copy) } ``` - **`writeDefaultFileRegion()`** calls `socket.sendFile()`, which maps directly to the Linux `sendfile()` syscall — a true zero-copy path where data is transferred from the file page cache to the socket buffer entirely within the kernel, with no user-space copy. https://github.com/netty/netty/blob/eeb5674526f0b49a142580686a5a9a7147ddadec/transport-classes-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollStreamChannel.java#L367-L386 ```java private int writeDefaultFileRegion(ChannelOutboundBuffer in, DefaultFileRegion region) throws Exception { final long offset = region.transferred(); final long regionCount = region.count(); if (offset >= regionCount) { in.remove(); return 0; } final long flushedAmount = socket.sendFile(region, region.position(), offset, regionCount - offset); if (flushedAmount > 0) { in.progress(flushedAmount); if (region.transferred() >= regionCount) { in.remove(); } return 1; } else if (flushedAmount == 0) { validateFileRegion(region, offset); } return WRITE_STATUS_SNDBUF_FULL; } ``` - **`writeFileRegion()`** falls back to `region.transferTo(WritableByteChannel)`, which writes data through a `SocketWritableByteChannel` wrapper — effectively a user-space copy path. https://github.com/netty/netty/blob/eeb5674526f0b49a142580686a5a9a7147ddadec/transport-classes-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollStreamChannel.java#L402-L420 ```java private int writeFileRegion(ChannelOutboundBuffer in, FileRegion region) throws Exception { if (region.transferred() >= region.count()) { in.remove(); return 0; } if (byteChannel == null) { byteChannel = new EpollSocketWritableByteChannel(); } final long flushedAmount = region.transferTo(byteChannel, region.transferred()); if (flushedAmount > 0) { in.progress(flushedAmount); if (region.transferred() >= region.count()) { in.remove(); } return 1; } return WRITE_STATUS_SNDBUF_FULL; } ``` Spark's `MessageWithHeader extends AbstractFileRegion` (not `DefaultFileRegion`). When `MessageEncoder` wraps a `DefaultFileRegion` body inside `MessageWithHeader`, the resulting object is a generic `FileRegion` from Netty's perspective. This means Netty dispatches it to the `writeFileRegion()` fallback, which calls `MessageWithHeader.transferTo()`: ```java // MessageWithHeader.java, line 121 if (body instanceof FileRegion fileRegion) { writtenBody = fileRegion.transferTo(target, totalBytesTransferred - headerLength); } ``` Here, even though the inner body is a `DefaultFileRegion`, its `transferTo()` is invoked with a `WritableByteChannel` (not a file descriptor), so the data is read from the file into a user-space buffer and then written to the socket — **the zero-copy opportunity is lost**. By emitting the `DefaultFileRegion` directly into Netty's outbound buffer (instead of wrapping it in `MessageWithHeader`), Netty's native transport recognizes it via `instanceof DefaultFileRegion` and routes it to `socket.sendFile()`, restoring the zero-copy `sendfile()` path. **Benchmark results (File-Backed Shuffle Fetch) show dramatic improvement:** | Scenario | Before (ms) | After (ms) | Improvement | |---|---|---|---| | AUTO, sequential fetch (JDK17) | 2075 | 362 | **~5.7x faster** | | AUTO, parallel fetch (JDK17) | 676 | 202 | **~3.3x faster** | | AUTO, sequential fetch (JDK21) | 2093 | 360 | **~5.8x faster** | | AUTO, parallel fetch (JDK21) | 677 | 204 | **~3.3x faster** | | AUTO, sequential fetch (JDK25) | 1918 | 411 | **~4.7x faster** | | AUTO, parallel fetch (JDK25) | 652 | 196 | **~3.3x faster** | With this fix, AUTO (EPOLL) mode now matches or exceeds NIO performance for file-backed shuffle fetches, achieving the expected ~1.0X relative performance (previously ~0.2X). No. This is an internal optimization to the Netty transport layer. Users benefit from improved shuffle fetch performance when using native transports (the default on Linux) without any configuration changes. - Updated `MergedBlockMetaSuccessSuite` to validate the new encoder output format (2 separate objects: `ByteBuf` header + `DefaultFileRegion`, instead of a single `MessageWithHeader`). - Re-ran `NettyTransportBenchmark` across JDK 17, JDK 21, and JDK 25 to confirm the performance improvement. Updated benchmark result files accordingly. Generated-by: Claude Code 4.6 Closes apache#55087 from LuciferYang/SPARK-56279. Authored-by: yangjie01 <[email protected]> Signed-off-by: yangjie01 <[email protected]>
…ion in Netty native transports ### What changes were proposed in this pull request? `MessageEncoder` enables zero-copy sendfile for `FileRegion` in Netty native transports, which emits the header `ByteBuf` and `FileRegion` as **separate objects** in the outbound message list when the body is a `FileRegion` backed by `FileSegmentManagedBuffer`, instead of wrapping them together in a `MessageWithHeader`. Previously, all messages with a body were unconditionally wrapped in `MessageWithHeader`. This caused native transports (EPOLL, KQUEUE) to fall into a generic `FileRegion.transferTo()` fallback path that copies data through user-space, bypassing the optimized `sendfile()` / `splice()` zero-copy path that Netty's native transports provide. The split is only applied when the `ManagedBuffer` is a `FileSegmentManagedBuffer`, whose `release()` is a no-op, making it safe to emit the `FileRegion` independently of write lifecycle management. Other `ManagedBuffer` types (e.g., `BlockManagerManagedBuffer`) still use the `MessageWithHeader` wrapper because they perform resource cleanup in `release()` that must be tied to `MessageWithHeader.deallocate()`. Backport: apache/spark#55087. ### Why are the changes needed? When using native transports (AUTO/EPOLL on Linux), file-backed shuffle fetch performance was severely degraded compared to NIO mode. The root cause lies in how Netty's native transports dispatch `FileRegion` writes. In `AbstractEpollStreamChannel.doWriteSingle()` (and the analogous KQueue path), Netty uses an `instanceof` check to choose between two write strategies: https://github.com/netty/netty/blob/eeb5674526f0b49a142580686a5a9a7147ddadec/transport-classes-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollStreamChannel.java#L474-L493 ```java } else if (msg instanceof DefaultFileRegion) { return writeDefaultFileRegion(in, (DefaultFileRegion) msg); // → socket.sendFile() (zero-copy) } else if (msg instanceof FileRegion) { return writeFileRegion(in, (FileRegion) msg); // → region.transferTo() (user-space copy) } ``` - **`writeDefaultFileRegion()`** calls `socket.sendFile()`, which maps directly to the Linux `sendfile()` syscall — a true zero-copy path where data is transferred from the file page cache to the socket buffer entirely within the kernel, with no user-space copy. https://github.com/netty/netty/blob/eeb5674526f0b49a142580686a5a9a7147ddadec/transport-classes-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollStreamChannel.java#L367-L386 ```java private int writeDefaultFileRegion(ChannelOutboundBuffer in, DefaultFileRegion region) throws Exception { final long offset = region.transferred(); final long regionCount = region.count(); if (offset >= regionCount) { in.remove(); return 0; } final long flushedAmount = socket.sendFile(region, region.position(), offset, regionCount - offset); if (flushedAmount > 0) { in.progress(flushedAmount); if (region.transferred() >= regionCount) { in.remove(); } return 1; } else if (flushedAmount == 0) { validateFileRegion(region, offset); } return WRITE_STATUS_SNDBUF_FULL; } ``` - **`writeFileRegion()`** falls back to `region.transferTo(WritableByteChannel)`, which writes data through a `SocketWritableByteChannel` wrapper — effectively a user-space copy path. https://github.com/netty/netty/blob/eeb5674526f0b49a142580686a5a9a7147ddadec/transport-classes-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollStreamChannel.java#L402-L420 ```java private int writeFileRegion(ChannelOutboundBuffer in, FileRegion region) throws Exception { if (region.transferred() >= region.count()) { in.remove(); return 0; } if (byteChannel == null) { byteChannel = new EpollSocketWritableByteChannel(); } final long flushedAmount = region.transferTo(byteChannel, region.transferred()); if (flushedAmount > 0) { in.progress(flushedAmount); if (region.transferred() >= region.count()) { in.remove(); } return 1; } return WRITE_STATUS_SNDBUF_FULL; } ``` Spark's `MessageWithHeader extends AbstractFileRegion` (not `DefaultFileRegion`). When `MessageEncoder` wraps a `DefaultFileRegion` body inside `MessageWithHeader`, the resulting object is a generic `FileRegion` from Netty's perspective. This means Netty dispatches it to the `writeFileRegion()` fallback, which calls `MessageWithHeader.transferTo()`: ```java // MessageWithHeader.java, line 121 if (body instanceof FileRegion fileRegion) { writtenBody = fileRegion.transferTo(target, totalBytesTransferred - headerLength); } ``` Here, even though the inner body is a `DefaultFileRegion`, its `transferTo()` is invoked with a `WritableByteChannel` (not a file descriptor), so the data is read from the file into a user-space buffer and then written to the socket — **the zero-copy opportunity is lost**. By emitting the `DefaultFileRegion` directly into Netty's outbound buffer (instead of wrapping it in `MessageWithHeader`), Netty's native transport recognizes it via `instanceof DefaultFileRegion` and routes it to `socket.sendFile()`, restoring the zero-copy `sendfile()` path. **Benchmark results (File-Backed Shuffle Fetch) show dramatic improvement:** | Scenario | Before (ms) | After (ms) | Improvement | |---|---|---|---| | EPOLL, sequential fetch (JDK8) | 524 | 134 | **~3.9x faster** | | EPOLL, parallel fetch (JDK8) | 191 | 55 | **~3.4x faster** | ### Does this PR resolve a correctness bug? No. ### Does this PR introduce _any_ user-facing change? No. This is an internal optimization to the Netty transport layer. Users benefit from improved shuffle fetch performance when using native transports (the default on Linux) without any configuration changes. ### How was this patch tested? - Re-ran `NettyTransportBenchmark` with JDK8 to confirm the performance improvement. Updated benchmark result files accordingly. Closes #3649 from SteNicholas/CELEBORN-2301. Lead-authored-by: SteNicholas <[email protected]> Co-authored-by: Cheng Pan <[email protected]> Signed-off-by: SteNicholas <[email protected]>
…ion in Netty native transports ### What changes were proposed in this pull request? `MessageEncoder` enables zero-copy sendfile for `FileRegion` in Netty native transports, which emits the header `ByteBuf` and `FileRegion` as **separate objects** in the outbound message list when the body is a `FileRegion` backed by `FileSegmentManagedBuffer`, instead of wrapping them together in a `MessageWithHeader`. Previously, all messages with a body were unconditionally wrapped in `MessageWithHeader`. This caused native transports (EPOLL, KQUEUE) to fall into a generic `FileRegion.transferTo()` fallback path that copies data through user-space, bypassing the optimized `sendfile()` / `splice()` zero-copy path that Netty's native transports provide. The split is only applied when the `ManagedBuffer` is a `FileSegmentManagedBuffer`, whose `release()` is a no-op, making it safe to emit the `FileRegion` independently of write lifecycle management. Other `ManagedBuffer` types (e.g., `BlockManagerManagedBuffer`) still use the `MessageWithHeader` wrapper because they perform resource cleanup in `release()` that must be tied to `MessageWithHeader.deallocate()`. Backport: apache/spark#55087. ### Why are the changes needed? When using native transports (AUTO/EPOLL on Linux), file-backed shuffle fetch performance was severely degraded compared to NIO mode. The root cause lies in how Netty's native transports dispatch `FileRegion` writes. In `AbstractEpollStreamChannel.doWriteSingle()` (and the analogous KQueue path), Netty uses an `instanceof` check to choose between two write strategies: https://github.com/netty/netty/blob/eeb5674526f0b49a142580686a5a9a7147ddadec/transport-classes-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollStreamChannel.java#L474-L493 ```java } else if (msg instanceof DefaultFileRegion) { return writeDefaultFileRegion(in, (DefaultFileRegion) msg); // → socket.sendFile() (zero-copy) } else if (msg instanceof FileRegion) { return writeFileRegion(in, (FileRegion) msg); // → region.transferTo() (user-space copy) } ``` - **`writeDefaultFileRegion()`** calls `socket.sendFile()`, which maps directly to the Linux `sendfile()` syscall — a true zero-copy path where data is transferred from the file page cache to the socket buffer entirely within the kernel, with no user-space copy. https://github.com/netty/netty/blob/eeb5674526f0b49a142580686a5a9a7147ddadec/transport-classes-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollStreamChannel.java#L367-L386 ```java private int writeDefaultFileRegion(ChannelOutboundBuffer in, DefaultFileRegion region) throws Exception { final long offset = region.transferred(); final long regionCount = region.count(); if (offset >= regionCount) { in.remove(); return 0; } final long flushedAmount = socket.sendFile(region, region.position(), offset, regionCount - offset); if (flushedAmount > 0) { in.progress(flushedAmount); if (region.transferred() >= regionCount) { in.remove(); } return 1; } else if (flushedAmount == 0) { validateFileRegion(region, offset); } return WRITE_STATUS_SNDBUF_FULL; } ``` - **`writeFileRegion()`** falls back to `region.transferTo(WritableByteChannel)`, which writes data through a `SocketWritableByteChannel` wrapper — effectively a user-space copy path. https://github.com/netty/netty/blob/eeb5674526f0b49a142580686a5a9a7147ddadec/transport-classes-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollStreamChannel.java#L402-L420 ```java private int writeFileRegion(ChannelOutboundBuffer in, FileRegion region) throws Exception { if (region.transferred() >= region.count()) { in.remove(); return 0; } if (byteChannel == null) { byteChannel = new EpollSocketWritableByteChannel(); } final long flushedAmount = region.transferTo(byteChannel, region.transferred()); if (flushedAmount > 0) { in.progress(flushedAmount); if (region.transferred() >= region.count()) { in.remove(); } return 1; } return WRITE_STATUS_SNDBUF_FULL; } ``` Spark's `MessageWithHeader extends AbstractFileRegion` (not `DefaultFileRegion`). When `MessageEncoder` wraps a `DefaultFileRegion` body inside `MessageWithHeader`, the resulting object is a generic `FileRegion` from Netty's perspective. This means Netty dispatches it to the `writeFileRegion()` fallback, which calls `MessageWithHeader.transferTo()`: ```java // MessageWithHeader.java, line 121 if (body instanceof FileRegion fileRegion) { writtenBody = fileRegion.transferTo(target, totalBytesTransferred - headerLength); } ``` Here, even though the inner body is a `DefaultFileRegion`, its `transferTo()` is invoked with a `WritableByteChannel` (not a file descriptor), so the data is read from the file into a user-space buffer and then written to the socket — **the zero-copy opportunity is lost**. By emitting the `DefaultFileRegion` directly into Netty's outbound buffer (instead of wrapping it in `MessageWithHeader`), Netty's native transport recognizes it via `instanceof DefaultFileRegion` and routes it to `socket.sendFile()`, restoring the zero-copy `sendfile()` path. **Benchmark results (File-Backed Shuffle Fetch) show dramatic improvement:** | Scenario | Before (ms) | After (ms) | Improvement | |---|---|---|---| | EPOLL, sequential fetch (JDK8) | 524 | 134 | **~3.9x faster** | | EPOLL, parallel fetch (JDK8) | 191 | 55 | **~3.4x faster** | ### Does this PR resolve a correctness bug? No. ### Does this PR introduce _any_ user-facing change? No. This is an internal optimization to the Netty transport layer. Users benefit from improved shuffle fetch performance when using native transports (the default on Linux) without any configuration changes. ### How was this patch tested? - Re-ran `NettyTransportBenchmark` with JDK8 to confirm the performance improvement. Updated benchmark result files accordingly. Closes apache#3649 from SteNicholas/CELEBORN-2301. Lead-authored-by: SteNicholas <[email protected]> Co-authored-by: Cheng Pan <[email protected]> Signed-off-by: SteNicholas <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.