[
https://issues.apache.org/jira/browse/RATIS-2797?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Andrey Yarovoy reassigned RATIS-2797:
-------------------------------------
Assignee: Andrey Yarovoy
> ZeroCopyMessageMarshaller copies whole messages since gRPC coalesces small
> buffers
> ----------------------------------------------------------------------------------
>
> Key: RATIS-2797
> URL: https://issues.apache.org/jira/browse/RATIS-2797
> Project: Ratis
> Issue Type: Sub-task
> Components: gRPC
> Reporter: Andrey Yarovoy
> Assignee: Andrey Yarovoy
> Priority: Major
>
> *Summary*
> With the gRPC bundled in ratis-thirdparty 1.1.1 (gRPC 1.82.4),
> `ZeroCopyMessageMarshaller` falls back to a full copy
> for nearly every large message. gRPC now coalesces small network buffers into
> heap arrays, and one such array makes
> the marshaller give up on the whole message. Applications that use the
> marshaller to avoid copying large payloads
> silently copy them again.
> *Description*
> **How the marshaller decides.** `ZeroCopyMessageMarshaller.parseZeroCopy`
> takes the zero-copy path only if the gRPC
> stream is `KnownLength`, `Detachable` and `HasByteBuffer`, and
> `byteBufferSupported()` returns true.
> `CompositeReadableBuffer.byteBufferSupported()` is true only if every buffer
> of the message has a `ByteBuffer`.
> **What changed in gRPC.**
> - In gRPC 1.82.4, `CompositeReadableBuffer.enqueueBuffer` coalesces small
> buffers. When a buffer of 1,024 bytes or
> more is appended after more than one smaller buffer, the small ones are
> copied into a new `byte[]`, wrapped as a
> `ReadableBuffers$ByteArrayWrapper`. It also coalesces after 1,000 small
> buffers (`MIN_LARGE_BUFFER_SIZE = 1024`,
> `MAX_SMALL_BUFFERS = 1000`).
> - `ByteArrayWrapper` inherits `AbstractReadableBuffer`:
> `byteBufferSupported()` returns false and `getByteBuffer()`
> throws `UnsupportedOperationException`.
> - Checked in the bytecode: ratis-thirdparty 1.1.1 has
> `coalesceTailSmallBuffers`; 1.1.0 and older don't. Apache
> Ozone's pom gives ratis-thirdparty 1.1.0 as gRPC 1.82.1, so the change came
> in a gRPC patch release.
> **Why it hits large messages.**
> - Small buffers are common: TCP reads leave slices of a few bytes between
> HTTP/2 data frames.
> - Measured on Raft appendEntries with 1 MB entries:
> - a message arrived as 72–87 Netty slices;
> - among them were 1–4 coalesced arrays of 2–8 bytes, mostly right after a
> full 16,384-byte slice.
> - So a few bytes make `byteBufferSupported()` false for the whole message,
> and the marshaller copies all of it.
> **A second trap for a fix.** Copying just those few bytes to a heap array is
> not enough. Protobuf aliases `bytes`
> fields only when every buffer of the input is a direct `ByteBuffer`
> (`IterableDirectByteBufferDecoder`). With any heap
> piece, `RopeByteString.newCodedInput()` uses `StreamDecoder`, which copies
> every `bytes` field.
> **Measured.** Microbenchmark `GrpcLogAppenderConnectionsBenchmark`
> (RATIS-2787), with followers decoding appendEntries
> through the marshaller (the companion issue on zero-copy appendEntries):
> - **Setup:** 3 servers in one JVM, 12 groups, 1 MB entries, macOS.
> - **The fallback:**
> - At full speed (about 1 GB/s), nearly every appendEntries fell back, with
> TLS on and off alike.
> - The marshaller's debug log reported `stream is HasByteBuffer but not
> byteBufferSupported` as the reason every time.
> - In the allocation profile, 11 samples went through the zero-copy path
> against 2,685 through the fallback.
> - When the run was limited by fsync, about half fell back.
> - **After the fix below:**
> - 0–5 samples took a copying path;
> - sampled allocated bytes fell by about 70% (about 4,700 → 1,344);
> - every follower's state machine data matched the leader's, by checksum,
> entry by entry.
> **Who is affected.**
> - Any user of `ZeroCopyMessageMarshaller` with ratis-thirdparty 1.1.1.
> - **Apache Ozone** uses it on the datanode, for `GrpcXceiverService.send`
> (client requests to datanodes, including EC
> writes) and for container replication (`GrpcReplicationService`,
> `SendContainerRequestHandler`). Ozone moved to
> ratis-thirdparty 1.1.1 in HDDS-17646. Its zero-copy paths most likely copy
> again since then; that has not been
> measured in Ozone.
> *Proposal*
> Make `ZeroCopyMessageMarshaller` tolerate buffers without a `ByteBuffer`,
> instead of requiring
> `byteBufferSupported()` for the whole stream:
> - Walk the detached stream's buffers as today, wrapping each `ByteBuffer`.
> Where `getByteBuffer()` throws
> `UnsupportedOperationException`, read a small chunk (at most 1 KB) instead.
> - Put all the chunks copied from one message into a single direct buffer,
> sliced per chunk, so every piece of the
> message stays direct and protobuf keeps aliasing.
> - That costs at most one small `allocateDirect` per message, and nothing when
> every buffer has a `ByteBuffer`.
> Nothing else changes: the stream is still detached, marked and reset, and
> `release` still closes it.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)