iatsuk commented on code in PR #24170:
URL: https://github.com/apache/kafka/pull/24170#discussion_r4563445718


##########
clients/src/main/java/org/apache/kafka/common/network/KafkaChannel.java:
##########
@@ -437,7 +439,20 @@ public long write() throws IOException {
             return 0;
 
         midWrite = true;
-        return send.writeTo(transportLayer);
+        // A ByteBufferSend is written with one call, but SSL writes each of 
its buffers as its own TLS record
+        boolean singleWrite = send.send() instanceof ByteBufferSend && 
!(transportLayer instanceof SslTransportLayer);
+        if (!OperatingSystem.IS_LINUX || singleWrite)
+            return send.writeTo(transportLayer);
+
+        // With TCP_NODELAY off the kernel joins the small parts of a send 
into full segments, and on Linux
+        // turning it back on sends the queued data at once instead of waiting 
for an ACK (see tcp(7)).
+        SocketChannel socketChannel = transportLayer.socketChannel();
+        socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, false);

Review Comment:
   Thanks for the review and for the idea. I tried it and measured four 
variants: trunk, the toggle only (this PR until now), your proposal alone 
(gathering `wrap`, no toggle), and both together (gathering `wrap`, and the 
toggle only for a send that is not a `ByteBufferSend`).
   
   **Setup.** This time not on loopback: 3 brokers on 3 different VMs (16 vCPU) 
of a Kubernetes cluster, the clients in a fourth pod. Packets, retransmissions 
and bytes are from `/proc/net/snmp` and `/proc/net/netstat` of the pods, CPU is 
from the cgroup.
   
   With a restart between the runs the broker CPU changes by 1–3% from run to 
run, and this is more than the effect we discuss. So for the CPU question I 
made an experiment-only build where the four variants are switched at run time 
(it is not part of the PR). The brokers stay up, every variant gets a 30 s 
window per round, and the order changes every round. The tables show the 
difference to trunk with a 95% interval, paired by round.
   
   **Low volume, SSL**: 3,000 partitions, 3,000 records/s of 100 bytes, 
`acks=all` (the benchmark from the notes), 16 rounds.
   
   | | trunk | toggle only | gathering wrap only | both |
   |---|---|---|---|---|
   | broker CPU (3 brokers) | 546% | -2.0% ± 1.2% | +0.1% ± 1.2% | -0.7% ± 1.6% 
|
   | clients CPU | 128% | -5.4% ± 1.4% | -10.9% ± 1.2% | -9.1% ± 2.7% |
   | packets/s | 37,926 | -65.6% ± 0.4% | -40.0% ± 0.4% | -72.5% ± 0.4% |
   | bytes on the wire | 6.0 MB/s | -20.8% ± 0.3% | -20.8% ± 0.3% | -31.3% ± 
0.3% |
   | TCP retransmissions/s | 113 | 69 | 11 | 10 |
   | probe p50 | 7.0 ms | 6.6 ms | 6.7 ms | 6.6 ms |
   
   The same load without SSL, 24 rounds: with the toggle the broker CPU is 
-0.9% ± 1.3%, packets are -56% and bytes on the wire -16%.
   
   **High load**: 20 MB/s into 30 partitions, RF=3, default batching, 2 
consumers, 12 rounds.
   
   | | trunk | toggle only | gathering wrap only | both |
   |---|---|---|---|---|
   | SSL: broker CPU | 526% | +1.0% ± 2.5% | -0.2% ± 2.2% | -0.8% ± 2.4% |
   | SSL: packets/s | 88,059 | -8.0% ± 0.5% | -4.1% ± 0.5% | -9.3% ± 0.5% |
   | PLAINTEXT: broker CPU | 507% | -0.7% ± 2.6% | same code as trunk | +0.2% ± 
2.2% |
   | PLAINTEXT: packets/s | 84,111 | -5.5% ± 0.3% | same code as trunk | -5.5% 
± 0.4% |
   
   **Small sends, SSL** (on one machine over loopback, normal builds with 
restarts): one broker, one partition, `acks=1`, one record per request, 20,000 
requests/s, 9 runs each. Every send here is one small `ByteBufferSend`.
   
   | | trunk | toggle only | both |
   |---|---|---|---|
   | broker CPU | 132% | 133% | 128% |
   | producers CPU | 95% | 88% | 73% |
   | packets/s | 167k | 112k | 85k |
   | max throughput, records/s (3 runs) | 27,245 | 27,349 | 29,943 |
   
   Two of the nine runs of "both" had 166% and 172% broker CPU with the same 
packet count. They ran one after the other and the other seven were between 
124% and 132%, so I count them as a disturbance on the machine and left them 
out of the mean.
   
   What I see:
   - I can't find a CPU cost of the toggle. In no scenario the broker CPU with 
the toggle is above trunk by more than the interval. The 516% vs 528% in the 
notes were 4 runs with restarts, and the difference is inside their run-to-run 
range (± 13).
   - Your proposal is clearly good. In the low-volume case it gives -40% 
packets, -21% bytes on the wire, 10 times fewer retransmissions and -11% 
clients CPU, with no extra system call. I did not look for the reason of the 
retransmissions on trunk yet.
   - Alone it leaves 60% of the packets. They come from the FetchResponse: a 
`MultiRecordsSend` has one part per partition, and every part is written with 
its own call. This is the same without SSL.
   - Under high load the packets are already full, so all variants are close to 
trunk.
   
   So I added your proposal to the PR and made the toggle smaller, as two 
commits:
   1. `SslTransportLayer` uses the gathering `wrap`, so the buffers of a send 
go into one TLS record.
   2. The toggle is used only for a send that is not a `ByteBufferSend`, with 
and without SSL. Requests and all responses without records never have the 
extra system calls now, only a fetch response with records has them. The check 
for `SslTransportLayer` in `KafkaChannel` is gone.
   
   This is the "both" column. If you prefer the SSL part as a separate PR, I 
will move the first commit out, it does not depend on the second one.
   
   With SSL the parts of a `MultiRecordsSend` could also be joined inside 
`SslTransportLayer` (keep the records in `netWriteBuffer` and flush once at the 
end of the send). That needs a flush step in the `TransportLayer` interface and 
does not help PLAINTEXT, but I can try it if you prefer it to the toggle.
   



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