yandrey321 opened a new pull request, #1677:
URL: https://github.com/apache/ratis/pull/1677
## What changes were proposed in this pull request?
**Problem.** With the gRPC in ratis-thirdparty 1.1.1 (gRPC 1.82.4),
`ZeroCopyMessageMarshaller` falls back to a full
copy for nearly every large message.
- The marshaller takes the zero-copy path only if the stream's
`HasByteBuffer.byteBufferSupported()` is true.
`CompositeReadableBuffer` returns true only if every buffer of the message
has a `ByteBuffer`.
- `CompositeReadableBuffer.enqueueBuffer` now coalesces small buffers. When
a buffer of 1,024 bytes or more follows
more than one smaller buffer, the small ones are copied into a new
`byte[]` (`ReadableBuffers$ByteArrayWrapper`).
That wrapper has no `ByteBuffer`: `getByteBuffer()` throws
`UnsupportedOperationException`. ratis-thirdparty 1.1.1
has this coalescing; 1.1.0 and older don't.
- Small buffers are common: TCP reads leave slices of a few bytes between
HTTP/2 data frames. A 1 MB message arrived as
72–87 slices, with 1–4 coalesced arrays of 2–8 bytes among them. So a few
bytes make the marshaller copy the whole
message.
**Change.** `getByteStrings` no longer requires a `ByteBuffer` for every
buffer.
- It walks the stream as before, keeping each `ByteBuffer` it gets. Where
`getByteBuffer()` throws
`UnsupportedOperationException`, it reads a chunk of at most 1 KB instead.
- `parseZeroCopy` no longer returns null when `byteBufferSupported()` is
false; that case is only logged at debug
level.
- The stream is still detached, marked and reset, and `release` still closes
it.
**Key decisions.**
- **The copied chunks go into one direct buffer per message**, sliced per
chunk. Protobuf aliases `bytes` fields only
when every buffer of the input is a direct `ByteBuffer`
(`IterableDirectByteBufferDecoder`). With a heap piece,
`RopeByteString.newCodedInput()` uses `StreamDecoder`, which copies every
`bytes` field. So copying the small
pieces to the heap would still copy the whole payload.
- **Chunks are at most 1 KB.** That covers the coalesced pieces. A read can
run past such a piece into the next
buffer, so up to 1 KB of a large buffer may be copied as well; the rest of
that buffer is then wrapped as usual.
- **Cost:** at most one small `allocateDirect` per message, and nothing when
every buffer has a `ByteBuffer`.
**Side effects.** None for messages whose buffers all have a `ByteBuffer`.
Messages that fell back to a full copy now
get a few KB copied into a small direct buffer instead.
**Who is affected.** Any user of `ZeroCopyMessageMarshaller` with
ratis-thirdparty 1.1.1. Apache Ozone uses it on the
datanode, for `GrpcXceiverService.send` and for container replication
(`GrpcReplicationService`,
`SendContainerRequestHandler`). Ozone moved to ratis-thirdparty 1.1.1 in
HDDS-17646.
Generated-by: Claude Code (Claude Opus 5.5)
## What is the link to the Apache JIRA
https://issues.apache.org/jira/browse/RATIS-2797
## How was this patch tested?
- **New `TestZeroCopyCoalescedBuffers`:**
- It builds a 300 KB message in direct buffers, with two 2-byte pieces in
the middle, which gRPC coalesces into an
array, and checks that `byteBufferSupported()` is false.
- It then checks that the parsed message is equal to the original, that
the state machine data is still in direct
buffers (not copied), and that `release` leaves no unclosed streams.
- Against the current marshaller it fails, with the data copied to a 300
KB heap buffer.
- **Existing tests:** `TestGrpcZeroCopy` (3 tests) passes. Checkstyle is
clean.
- **Benchmark:** Ratis itself doesn't use the marshaller on master. So it
was measured with followers decoding
appendEntries : 3 servers in one JVM,
12 groups, 1 MB entries.
- **Before this change:** at full speed nearly every appendEntries fell
back, with TLS on and off alike. In the
allocation profile, 11 samples went through the zero-copy path against
2,685 through the copying fallback, and
the debug log gave `stream is HasByteBuffer but not byteBufferSupported`
as the reason every time.
- **After:**
- 0–5 samples took a copying path;
- sampled allocated bytes fell by about 70%;
- every follower's state machine data matched the leader's, by checksum,
entry by entry.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]