yandrey321 opened a new pull request, #1680:
URL: https://github.com/apache/ratis/pull/1680

   ## What changes were proposed in this pull request?
   
   **Problem.** A follower decoding an appendEntries request copies every log 
entry's state machine data into a new heap
   array, only to write it and drop it.
   - `GrpcServerProtocolService` decodes appendEntries with gRPC's default 
protobuf marshaller, which copies each `bytes`
     field.
   - With `raft.server.log.statemachine.data.caching.enabled = true`, Ratis 
strips the state machine data before caching
     or writing the log entry, and gives the full entry only to 
`StateMachine.DataApi.write`. So once the follower has
     replied, nothing in Ratis needs that data.
   - That is how Apache Ozone runs. There the data is the up to 4 MB of each 
`WriteChunk`, for every pipeline a datanode
     follows.
   
   **Change.**
   - **New key `raft.grpc.server.zero-copy.append-entries.enabled`, default 
false**, in `GrpcConfigKeys.Server`.
     `GrpcServicesImpl` accepts it only with state machine data caching on. 
Otherwise it logs a warning and leaves it
     off.
   - **`GrpcServerProtocolService`:**
     - `bindServiceDefinition()` replaces only appendEntries' request 
marshaller with `ZeroCopyMessageMarshaller`;
     - before a request reaches the server, `copyAllButStateMachineData` copies 
everything in it except the state
       machine data: entry metadata, log data, configuration entries and commit 
infos;
     - the buffers are released when `appendEntriesAsync` completes 
successfully, or right away if it fails before
       appending anything;
     - if the reply fails, the buffers are not released, and a counter is 
logged.
   
   **Key decisions.**
   - **No reference counting.** Only the state machine data refers to gRPC's 
buffers, only on followers, and only where
     Ratis itself doesn't keep that data. That avoids the reference counting 
the earlier RATIS-1931 work needed and that
     was reverted (RATIS-1983).
   - **Release after the reply.** The follower's reply already waits for the 
log entry to be written and for
     `DataApi.write`. With append composition on (the default), a duplicate 
request waits for the earlier append. So the
     data is no longer used by then.
   - **Leak rather than corrupt on failure.** If the reply fails, the log or 
the state machine could still be writing
     the data, so its buffers are kept.
   - **A state machine contract**, documented with the key. The state machine 
must not keep the state machine data after
     `write` completes. That includes the follower's `TransactionContext`: 
`startTransaction(LogEntryProto,
     RaftPeerRole)` keeps the whole entry by default, so a state machine using 
the key strips the data there, e.g. with
     `LogProtoUtils.removeStateMachineData`.
   
   **Side effects.** None with the key off, the default. With it on:
   - gRPC's buffers are held until the follower replies;
   - the leader's client requests still copy, since the leader keeps the data 
to replicate it.
   
   **Depends on RATIS-2797.** With ratis-thirdparty 1.1.1 (gRPC 1.82.4), 
`ZeroCopyMessageMarshaller` falls back to a
   full copy for nearly every large message until RATIS-2797 is in. This change 
is correct without it, but mostly
   copies.
   
   Generated-by: Claude Code (Claude Opus 5.5)
   
   ## What is the link to the Apache JIRA
   
   https://issues.apache.org/jira/browse/RATIS-2798
   
   ## How was this patch tested?
   
   Unit and integration tests.
   
   **Benchmark** (a standalone harness, not part of this change):
   - **Setup:**
     - 3 Ratis servers, each in its own JVM, plus a client JVM, on a 2 × Xeon 
Silver 4416+ host, pinned to one NUMA
       node;
     - JDK 17, mutual TLS, Ozone's 5 MB flow-control window, the log on tmpfs 
with fsync awaited;
     - 12 groups led by one server, 1 MB entries as state machine data.
   - **Build:** RATIS-2787 (K appendEntries connections per peer) and 
RATIS-2797 applied, with zero-copy off vs on.
   - **Checks:** after every run, every follower's data was checked against the 
leader's by checksum.
   
   | K | zero-copy | rounds | mean MB/s | change | follower CPU per MB |
   |---|---|---|---|---|---|
   | 1 | off / on | 3 | 968 / 970 | 0% | no consistent change |
   | 4 | off / on | 5 | 1,211 / 1,306 | +7.9% (median +6.5%), faster in 4 of 5 
| 5.64 → 4.88 ms (−13%) |
   
   - **K = 1 is limited by the one connection per peer,** whose event-loop 
threads zero-copy doesn't touch, so its
     throughput doesn't move.
   - **At K = 4 the followers decode much more data per second, and the saving 
shows up.** A 3-round run gave +9.0% and
     −12% follower CPU per MB. Across both runs zero-copy was faster in 7 of 8 
rounds.
   - **On a 14-core laptop,** where CPU was the limit, it gave +5–6.5% at one 
connection per peer, in 15 of 15 rounds,
     with the log on SSD (fsync not awaited) and on a RAM disk (fsync awaited).
   


-- 
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]

Reply via email to