junrao commented on code in PR #24170:
URL: https://github.com/apache/kafka/pull/24170#discussion_r4606794246
##########
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 have a couple of concerns on this approach. First, it allocates more
memory to fit 2 instead of just 1 TLS record. Since this is per connection, it
can add up. Second, it doesn't address the TLS record overhead issue when
writing multiple small payload. For a FetchResponse with multiple partitions of
small payload (e.g. replica fetch response), SSLTransferLayer is called with an
interleaving write() and transferFrom() calls, each with a small payload. Each
of these calls will call SSLEngine.wrap() to generate a separate TLS record and
the overhead can be significant. Here is alternative approach.
We treat MultiRecordsSend specially. In SSLTransferLayer, we keep a list of
pending buffers. Each write() adds the buffer to the list. Each transferFrom()
call reads the bytes from disk to fileChannelBuffer, creates a slice of
ByteBuffer and adds it to the list. Once enough bytes is accumulated in the
list (>= the maximum plaintext per TLS record), we keep calling wrap() on the
list and wait until everything is flushed to socket. After that, we clear the
list and continues. At the end, the caller sends a endWriteBatch call to flush
the remaining in the list. With this approach, for the FetchResponse with
multiple partitions of small payload case, we will just call wrap() only once
and sends one TLS record in the socket. Claude suggested a few caveats to get
it right.
```
1. Pending bytes and completion.
Outside a batch, hasPendingWrites() must be netWriteBuffer.hasRemaining() ||
accumulated > 0. Inside a batch it returns false, so parts complete as soon as
their bytes are accepted.
The rest is the PR's existing logic: the maybeCompleteSend check, and
flushing only for an already-complete send.
2. Accounting in transferFrom. Bytes are now reported when read, not when
wrapped. That removes the "leftover is file data at pos" logic
(SslTransportLayer.java:1084-1090).
On a failed read, add no slice and leave the ring as it was.
Skip the final wrap in endWriteBatch when writeTo threw. This also avoids
masking the original exception.
Update the existing transferFrom tests for the new return values.
3. Threshold. Compare against the maximum plaintext per record (2^14 =
16,384), not getPacketBufferSize() (16,709).
On TLS 1.3 the JDK takes 16,367 per record, and the 17 bytes left over
simply stay in the list.
Wait for the threshold only within a send. The end of the send always
flushes whatever is left.
```
On the producer side, it uses ByteBufferSend which calls write() will all
buffers. So, it doesn't need the above approach since we can just feed all
buffers to wrap(). We also don't need to over allocate netWriteBuffer. If
netWriteBuffer is not empty when receiving BUFFER_OVERFLOW, we can just flush
it first.
--
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]