iatsuk commented on code in PR #24170:
URL: https://github.com/apache/kafka/pull/24170#discussion_r4574734206
##########
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:
Thank you very much for the detailed answer and for the sketch. I appreciate
a lot that you spend your time on this PR, and the sketch helped me a lot.
I followed your proposal: the toggle is removed, and the write batch is
added to `SslTransportLayer`. PLAINTEXT is now written as on trunk.
**In short.** With SSL the parts of a response are now collected inside
`SslTransportLayer` and written to the socket with one call. Nothing depends on
the kernel and there is no config. In my tests it is never worse than trunk,
and it is better than the version with the toggle: with many partitions 79% of
the packets and 5% of the broker CPU are gone.
**Two differences to the sketch**
- I did not add the `flushBlocked` flag, because I think it is not needed.
When the socket is full, `write()` cannot take all bytes of the current part.
So this part is not complete, and `MultiRecordsSend` stops by itself. Please
tell me if I miss a case here.
- The buffer has to be bigger. The sketch says to verify how much room
`wrap` needs. I tested it on JDK 21: `wrap` wants a whole free packet (about
16.7 KB) for every record, also for a very small one. So a buffer of one packet
cannot hold records and take a new one. Now the buffer grows to two packets
when a batch really holds records. The price is 16.7 KB more memory for such a
connection. These are the connections that send fetch responses, or requests
bigger than one packet. A shared buffer per network thread would save this
memory. I could try to implement it, but it will make the solution more
complex. What do you think?
**Measurements.** I measured in the same cluster and in the same way as
before. The tables show the difference to trunk with a 95% interval, so a
negative number means less than on trunk. The variants are switched at run time
in a test build, which is not part of the PR.
Low volume, SSL: many partitions and small records (3,000 partitions, 3,000
records/s of 100 bytes, `acks=all`), 16 rounds. This is the case where the
small packets are the problem:
| | trunk | gathering wrap only | wrap + toggle (PR before) | wrap + write
batch (PR now) |
|---|---|---|---|---|
| broker CPU (3 brokers) | 555% | -1.4% ± 0.9% | -2.1% ± 1.3% | -5.2% ± 1.5%
|
| clients CPU | 100% | -8.3% ± 1.3% | -7.7% ± 2.0% | -6.5% ± 2.3% |
| packets/s | 37,938 | -40.2% ± 0.2% | -73.0% ± 0.2% | -79.3% ± 0.2% |
| bytes on the wire | 6.0 MB/s | -20.9% ± 0.2% | -31.5% ± 0.2% | -34.4% ±
0.2% |
| TCP retransmissions/s | 132 | 11 | 9 | 6 |
| probe p50 | 7.1 ms | 6.8 ms | 6.6 ms | 6.6 ms |
High load, SSL: few partitions and a lot of data (20 MB/s into 30
partitions, RF=3, 2 consumers), 12 rounds. Here the packets are already full,
so the question is only if something gets worse:
| | trunk | gathering wrap only | wrap + toggle (PR before) | wrap + write
batch (PR now) |
|---|---|---|---|---|
| broker CPU (3 brokers) | 527% | +1.0% ± 1.4% | +0.5% ± 0.9% | -1.5% ± 1.7%
|
| packets/s | 88,485 | -3.5% ± 0.7% | -9.3% ± 0.3% | -12.3% ± 0.3% |
| probe p50 | 8.4 ms | 8.3 ms | 8.1 ms | 8.0 ms |
The clients CPU under high load is the same as on trunk (+2.8% ± 3.2%). I
left out the first round for this number, because the consumers were still
starting.
Small sends: one record per request, so there is nothing to collect and only
the cost could show. I ran this one on one machine over loopback, with the
normal build of this PR and a restart before every run:
| | trunk | PR now |
|---|---|---|
| SSL, 20,000 requests/s: broker CPU (6 runs) | 127% ± 3 | 123% ± 3 |
| SSL: producers CPU | 90% ± 3 | 66% ± 2 |
| SSL: packets/s | 166,913 | 84,759 |
| SSL: max throughput, records/s (3 runs) | 35,511 | 39,277 |
| PLAINTEXT, 40,000 requests/s: broker CPU (3 runs) | 220% ± 2 | 222% ± 2 |
| PLAINTEXT: packets/s | 163,826 | 163,833 |
With PLAINTEXT nothing changes, as expected. I compared trunk and this PR in
the cluster for both loads, 8 rounds each. Packets and broker CPU are the same
within the measurement error (below 1% for packets, below 3% for CPU).
I also ran the normal build of this PR, without the switch, in the cluster.
It gives the same numbers as the switch: 7,885 packets/s and 3.9 MB/s.
**Tests.** I added `SslTransportLayerTest#testWriteBatch` and two tests in
`KafkaChannelTest`. All tests of `clients` pass. In `core` I ran
`SocketServerTest` and the SSL integration tests (`SslConsumerTest`,
`SslProducerSendTest`, `SslEndToEndAuthorizationTest`, `SaslSslConsumerTest`,
`SslAdminIntegrationTest`), and they pass too.
For PLAINTEXT I would like to try the same batch in user space later, as a
separate change: small parts are collected and written with one call, large
parts still go with zero-copy.
Thanks again for the review and for the patience with all the versions.
--
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]