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


##########
clients/src/main/java/org/apache/kafka/common/network/SslTransportLayer.java:
##########
@@ -742,9 +783,17 @@ public int write(ByteBuffer src) throws IOException {
             if (wrapResult.getStatus() == Status.OK) {
                 written += wrapResult.bytesConsumed();
             } else if (wrapResult.getStatus() == Status.BUFFER_OVERFLOW) {
-                // BUFFER_OVERFLOW means that the last `wrap` call had no 
effect, so we expand the buffer and try again
-                netWriteBuffer = Utils.ensureCapacity(netWriteBuffer, 
netWriteBufferSize());
-                netWriteBuffer.position(netWriteBuffer.limit());
+                // BUFFER_OVERFLOW means that the last `wrap` call had no 
effect, so we expand the buffer and try again.
+                // `wrap` asks for the room of a whole packet, so a batch gets 
this room behind one packet of held
+                // records, and with more held records they are flushed first.
+                int size = netWriteBuffer.hasRemaining() ? 2 * 
netWriteBufferSize() : netWriteBufferSize();

Review Comment:
   I implemented your approach. In a batch `SslTransportLayer` now holds the 
plaintext and wraps it when it has enough for a full TLS record.
   
   `netWriteBuffer` stays one packet, so a connection needs the same memory as 
on trunk. A fetch response with many small partitions now leaves in full TLS 
records. While I tested it, I found a bug in the version before. Because of it 
the batch is now opened in another place, and I had to change two more files. I 
describe this below and I would like to know your opinion.
   
   **Memory.** The table shows the broker heap per open SSL connection. I 
opened 2,000 connections to one broker, sent one request on each and left them 
idle. Then I compared the live heap before and after. Every cell is the mean of 
two runs.
   
   | response on the connection | trunk | PR before | PR now |
   |---|---|---|---|
   | Metadata, 26 KB | 56.9 KB | 73.5 KB | 57.0 KB |
   | Fetch with 1,000 partitions, 3.4 MB | 58.0 KB | 74.3 KB | 57.8 KB |
   
   **TLS records.** I counted the TLS records of one fetch response on the 
client side. The response has the records of 1,000 partitions, 3.4 MB in total.
   
   | | trunk | PR before | PR now |
   |---|---|---|---|
   | TLS records | 2,001 | 2,001 | 311 |
   
   With only full records it would be 210. The others are short records. When 
`fileChannelBuffer` is full, the bytes that are still held in it are wrapped 
before the next read from the file. So there is about one short record per 32 
KB of file data. I could move the held bytes to the start of the buffer to 
avoid this. But it is more code for a small gain, so I did not do it.
   
   **The bug, and where the batch is opened now.** Inside a batch 
`hasPendingWrites()` returns false, so a send is completed when the transport 
layer has taken its bytes. In the version before `KafkaChannel.write()` opened 
the batch around every send, also around the `ByteBufferSend` of a client 
request. But `NetworkClient` reads `completed()` of this send directly, in 
`InFlightRequests.canSendMore()`. When the socket was full at the end of a 
batch, the client saw a completed send and sent the next request. Then 
`KafkaChannel.setSend()` threw "Attempt to begin a send operation with prior 
send operation still in progress", and the connection was closed. The producer 
retried, so the runs went on, and I did not look into the client logs. This was 
my mistake. With 20 MB/s on one machine I now see about 3,900 of these 
exceptions in 4 minutes with the version before, and none on trunk.
   
   Now `MultiRecordsSend.writeTo()` opens the batch, and `KafkaChannel` and 
`TransportLayer` are the same as on trunk again. For this I changed two files 
that the PR did not touch before:
   - `TransferableChannel` has `startWriteBatch()` and `endWriteBatch()` as 
default methods that do nothing. Only `SslTransportLayer` overrides them.
   - `MultiRecordsSend` calls them around its loop. After the batch it asks 
`hasPendingWrites()`, and it is completed only when nothing is pending.
   
   So only the parts of a `MultiRecordsSend` are written in a batch. For every 
send that `KafkaChannel` or `NetworkClient` sees, `completed()` means the same 
as on trunk. A client request never runs the batch code.
   
   I have doubts about this, and I am not sure which way is better:
   - `MultiRecordsSend` is in `common.record`, and now it knows about the batch 
of the channel. I think it fits, because only this class knows that its parts 
belong together. But the rule "inside a batch `hasPendingWrites()` returns 
false" is now part of `TransferableChannel`. A new implementation that holds 
bytes in a batch has to follow it.
   - The other way keeps everything in `common.network`. `KafkaChannel` opens 
the batch only when the send is not a `ByteBufferSend`. But then `KafkaChannel` 
needs a type check, and `MultiRecordsSend.completed()` returns true while the 
transport layer still holds bytes. This looks more fragile to me.
   
   What do you think?
   
   **Measurements.** I measured in the same kind of cluster and with the same 
loads as before. The tables show trunk and the difference to trunk with a 95% 
interval. The variants are switched at run time in a test build, which is not 
part of the PR. After every run I now check the client logs. There were no 
exceptions and no expired batches.
   
   Low volume, SSL: many partitions and small records (3,000 partitions, 3,000 
records/s of 100 bytes, `acks=all`), 12 rounds. The version before is in the 
same run, so the two versions can be compared directly.
   
   | | trunk | PR before | PR now |
   |---|---|---|---|
   | packets/s | 35,167 | -79.2% ± 0.5% | -80.3% ± 0.5% |
   | bytes on the wire | 5.8 MB/s | -33.6% ± 0.4% | -41.7% ± 0.4% |
   | broker CPU (3 brokers) | 640% | -0.9% ± 1.1% | -0.6% ± 1.5% |
   | clients CPU | 102% | -9.7% ± 2.4% | -7.9% ± 4.6% |
   | TCP retransmissions/s | 104 | 9 | 8 |
   | probe p50 | 8.7 ms | 7.8 ms | 7.7 ms |
   | probe p99 | 40.8 ms | -0.8 ± 5.6 ms | -4.6 ± 4.7 ms |
   
   So the new version sends 12% fewer bytes than the version before, and the 
broker CPU is the same.
   
   High load, SSL: few partitions and a lot of data (20 MB/s into 30 
partitions, RF=3, 2 consumers), 12 rounds.
   
   | | trunk | PR now |
   |---|---|---|
   | packets/s | 85,856 | -9.8% ± 0.3% |
   | bytes on the wire | 84.2 MB/s | -0.8% ± 0.2% |
   | broker CPU (3 brokers) | 540% | -2.0% ± 1.8% |
   | clients CPU | 129% | -5.4% ± 3.0% |
   | probe p50 | 10.3 ms | 9.7 ms |
   | probe p99 | 28.3 ms | +3.7 ± 11.1 ms |
   
   The p99 under high load is noisy. It was higher with the PR in the first 
three rounds and lower in the other nine. The median over the rounds is 26 ms 
with the PR and 27 ms on trunk, and the number of probes over 100 ms is the 
same (96 and 95 of 30,000). So I see no difference, but the interval is wide.
   
   With PLAINTEXT nothing changes. I ran the low volume load with PLAINTEXT for 
8 rounds: packets +0.2% ± 1.1%, broker CPU +1.1% ± 2.1%.
   
   I also ran the normal build of the PR, without the switch, in the cluster. 
It gives the same numbers as the switch: 6,923 packets/s and 3.4 MB/s with low 
volume, 77,362 packets/s with high load.
   
   Small sends: one record per request, so there is nothing to collect and only 
the cost could show. One machine over loopback, the normal build of the PR, a 
restart before every run.
   
   | | trunk | PR now |
   |---|---|---|
   | SSL, 20,000 requests/s: broker CPU (6 runs) | 130% ± 1 | 125% ± 3 |
   | SSL: producers CPU | 91% ± 2 | 69% ± 1 |
   | SSL: packets/s | 166,944 | 84,692 |
   | SSL: max throughput, records/s (3 runs) | 34,575 ± 1,658 | 39,064 ± 797 |
   | PLAINTEXT, 40,000 requests/s: broker CPU (4 runs) | 233% ± 3 | 236% ± 4 |
   | PLAINTEXT: packets/s | 162,955 | 163,868 |
   
   **Tests.** `SslTransportLayerTest` has `testWriteBatch`, 
`testWriteBatchWithFullSocket` and the new `testWriteBatchWithFileParts`, which 
writes buffers and file parts in turn with a real `SSLEngine`. 
`MultiRecordsSendTest#testCompletesAfterPendingWrites` is new. The two tests in 
`KafkaChannelTest` are removed, because `KafkaChannel` is as on trunk.
   
   Thank you for the time you spend on this PR, and for the detailed proposal.
   



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