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

   ## What changes were proposed in this pull request?
   
   **Problem.** All Raft groups that two servers share replicate over one gRPC 
connection.
   - `GrpcServicesImpl` keeps one `GrpcServerProtocolClient` per peer, shared 
by every division on the server.
   - `GrpcServerProtocolClient.appendEntries()` always uses the stub on its 
main channel. Heartbeats move to a separate
     channel only when `raft.grpc.server.heartbeat.channel` is set.
   
   Netty pins a connection to one event-loop thread. So with TLS, a single 
thread at each end encrypts, frames and
   decrypts the entire log stream between the two servers, state machine data 
included. That caps replication between
   two servers at about one core of TLS work.
   
   Apache Ozone hits this limit. A datanode is one RaftServer hosting 20–40 
RATIS/THREE groups (pipelines), and each
   `WriteChunk` entry carries up to 4 MB of data. The measurements below come 
from a large 3-way write on a 12-datanode
   cluster with gRPC TLS on (TestDFSIO 1000 × 1 GB):
   - Each datanode replicated 3–3.8 GB/s, about one data connection per peer.
   - The busiest connections carried 450–770 MB/s one way.
   - Of 80 event-loop threads per datanode, only 10–19 were busy. The hottest 
averaged 43–72% of a core, with 5 s peaks
     of 78–96%.
   - 79–87% of that thread's CPU samples were TLS record encryption and 
decryption.
   - Turning TLS off raised the write from 7.6 to 8.6–8.8 GB/s.
   
   **Change.**
   - **New key `raft.grpc.server.log-appender.connections` (K):**
     - default 1, which keeps today's behaviour;
     - allowed range 1–1024;
     - getter and setter in `GrpcConfigKeys.Server`.
   - **`GrpcServerProtocolClient`:**
     - keeps K appendEntries channels. The first is the existing main channel, 
so K = 1 opens no new connection.
     - Each group always uses the same channel: 
`Math.floorMod(groupId.hashCode(), K)`.
     - `close()` and `resetConnectBackoff()` cover all K channels.
   - **`GrpcLogAppender`:** passes its group id when it opens the appendEntries 
stream.
   - **`GrpcServicesImpl`:** reads the key and passes it to each per-peer 
client.
   
   **Key decisions.**
   - **A group is pinned to one connection.** Raft ordering is per appender 
stream, so pinning keeps each group's
     entries in order on one connection while different groups spread over up 
to K event-loop threads at each end. No
     ordering or retry logic changes.
   - **Dedicated channels instead of the RATIS-2325 stub pool.** The pool hands 
out stubs per call, which suits short
     `readIndex` calls. AppendEntries streams are long-lived and ordered, so 
they need a fixed channel per group.
   - **Heartbeats are unchanged.** With `heartbeat.channel` on, they keep their 
own channel; otherwise they share the
     group's appendEntries stream as before.
   - **The main channel is reused as channel 0.** K = 1 behaves exactly as 
before, and K > 1 adds K − 1 connections per
     peer.
   
   **Side effects.**
   - None with the default K = 1.
   - With K > 1, each peer gets K − 1 extra connections. Each extra connection 
costs an HTTP/2 flow-control window, a TLS
     session and a handshake when it connects.
   - **K > 1 costs event-loop CPU.** For the same throughput, event-loop CPU 
per MB rose about 15% at K = 2 and 26% at
     K = 4 in the microbenchmark below. The profile puts the increase mostly in 
waiting for socket events: the same bytes
     arrive over more connections and threads, so each wake-up carries less 
data. So K should be raised only where one
     connection's event-loop thread is the limit.
   - Hashing many groups into a small K can leave the connections unevenly 
loaded.
   
   Generated-by: Claude Code (Claude Opus 5.5)
   
   ## What is the link to the Apache JIRA
   
   https://issues.apache.org/jira/browse/RATIS-2787
   
   ## How was this patch tested?
   
   **New unit tests.** `TestGrpcServerProtocolClientChannels`:
   - `testOneConnectionUsesMainChannel`: K = 1 has one appendEntries channel, 
which is the main channel, and every group
     maps to index 0.
   - `testGroupsSpreadOverConnections`: with K = 4:
     - there are 4 distinct channels, the first being the main channel;
     - 100 random groups each map to an index in range, which is stable for 
equal group ids;
     - all 4 channels get used;
     - `close()` shuts down every channel.
   
   **Benchmarks.** Both were run on a 14-core laptop. Neither could make a 
single connection the bottleneck, so they show
   the change does not regress, and how it spreads the load, but not a 
throughput gain.
   
   1. **A local microbenchmark** (a `ratis-test` test class, not part of this 
change):
      - **Setup:**
        - 3 servers in one JVM, TLS on, an in-memory log;
        - every group led by the same server, so all replication goes over its 
connections to the two others;
        - each server with its own event-loop threads (8 client and 8 server 
workers);
        - 12 groups × 100 × 1 MB timed messages after a warm-up, 8 writes in 
flight per group;
        - each arm in a fresh JVM, the order of K rotated per round;
        - JaCoCo (`-Djacoco.skip=true`) and Netty leak detection off, since 
both distort the numbers.
   
      | K | MB/s (3 rounds) | mean MB/s | hottest event loop (%CPU) | 
event-loop CPU per MB |
      |---|---|---|---|---|
      | 1 | 969 / 987 / 906 | 954 | 57 / 57 / 54 | 4.96 ms |
      | 2 | 1,009 / 904 / 981 | 965 | 43 / 37 / 40 | 5.70 ms (+15%) |
      | 4 | 1,014 / 956 / 950 | 973 | 42 / 34 / 34 | 6.27 ms (+26%) |
   
      - **Throughput:** +1% at K = 2 and +2% at K = 4, within the up to 9% 
spread between rounds at the same K.
      - **Load spreading works as designed.** At K = 1 the hottest threads are 
the leader's two outgoing connections. More
        connections lower the hottest thread from about 56% to about 40% (K = 
2) and 37% (K = 4).
      - **No single connection is the limit here.** The JVM uses 10–12 of the 
14 cores, because every byte is TLS-handled
        several times in this one process. So no event-loop thread gets near 
100%.
      - **The flow-control window mattered more than K locally.** With 
`raft.grpc.flow.control.window` at 8 MB instead of
        the 1 MB default, K = 1 rose from 941 to 1,053 MB/s (2 rounds), and K = 
4 from 976 to 1,031 MB/s.
   
   2. **Ozone on a secure docker-compose cluster** (Ozone master built against 
this branch):
      - 3 datanodes, gRPC TLS on, datanode data on tmpfs;
      - TestDFSIO write of 7 × 512 MB.
   
      | K | run 1 (exec s) | run 2 (exec s) | Ratis server connections per 
datanode | hottest datanode gRPC thread (%CPU) |
      |---|---|---|---|---|
      | 1 | 22.68 | 22.59 | 4/4/4 | 30–31 |
      | 4 | 22.62 | 21.61 | 6–8 | 28–32 |
   
      - With K = 4 the datanodes opened more Ratis server connections. gRPC 
connects a channel lazily, on first use, so
        with 6 pipelines only the channels that some pipeline maps to are 
connected.
      - Writes ran the same with K = 4 as with K = 1.
   


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