Repository navigation
Port 5.0: SslHandler: Stop wrapping when the transport can not keep up (#18253) - #18321
Merged
Merged
Conversation
…8253) Motivation: SslHandler queues the plaintext that is written to it and encrypts all of it in one loop when it is flushed. This loop does not stop when the transport can not keep up, so the encrypted copy of everything that was flushed is allocated at once and kept in the ChannelOutboundBuffer until it is written. When streaming to many slow peers this needs a lot of (direct) memory, see netty#17304. Channel.isWritable() can not be used to stop, as the plaintext that is queued in SslHandler counts towards it as well. The channel would stay unwritable because of our own queue, and we would never continue. Modification: - Track the writes of encrypted application data that are not complete yet, oldest first. Stop wrapping once more than the write buffer high water mark of the channel is pending, and continue once not more than the low water mark is pending anymore, like the writability of the channel. Only the oldest pending write gets a listener. The promise that is allocated for each record anyway knows how many bytes it contains, and small writes are tracked together, so no additional objects are allocated per record. - Wrap the data for which wrapping was suspended before the outbound side of the SSLEngine is closed by close(), disconnect() and closeOutbound(), as it would be failed otherwise. - Like flush(), don't wrap while a delegated task is running. Continue, and close the outbound side if that was requested in the meantime, once the task completed. - If the transport fails to write the encrypted data, fail the data that was not wrapped yet instead of encrypting it. - Don't suspend before the first message was written with startTls, as flush() would write the data we did not wrap unencrypted. - Add SslHandlerWrapBackpressureTest. - Add SslHandlerSlowTransportBenchmark, which writes to a transport that can only write a part of the data each time it is writable, like a socket whose send buffer is full. It reports the most encrypted data that was waiting to be written at the same time. Result: SslHandler encrypts at most about the write buffer high water mark ahead of what the transport has written. The bound does not apply when the channel is closed or closeOutbound() is called while data is held back: everything that is queued is encrypted at once before the close_notify, as the SSLEngine would not encrypt it anymore once its outbound side is closed. This is the same as before this change. This changes the behaviour of SslHandler in some cases: - flush() may return while plaintext is still queued in SslHandler. It is written as the transport makes progress, without another flush(). - On a fast transport a large flush is written in batches of about the high water mark, which means more writes. - A transport or handler that never completes the promise of a write stops the encrypted output once about the high water mark is pending. - If the channel is closed while data that was held back is still queued, data that was written after the last flush() is written before the close_notify as well. - If SslHandler is removed while wrapping is suspended, the data that was not wrapped yet is failed. - If the peer sends close_notify while wrapping is suspended, the data that was not wrapped yet is failed, as the outbound side of the SSLEngine is closed then as well (the OpenSSL SSLEngine, and the JDK one for TLSv1.2 and before). Before, it was already encrypted at that point and so still written. - If unwrapping fails while the SSLEngine can still wrap, everything that is queued is wrapped at once before the channel is closed, like when the channel is closed while wrapping is suspended. This is the same as before. Port notes for 5.0: - ChannelPromise / ChannelFuture / ChannelFutureListener are replaced by Promise<Void> / Future<Void> / FutureListener<Void>, and EncryptedWritePromise extends DefaultPromise<Void>. - The water marks are read through ChannelConfig.getWriteBufferWaterMark().high() / low(). - In the test, handlers can not have their own EventExecutorGroup in 5.0, so testWrapIsSuspendedWhenSslHandlerUsesOtherExecutors only runs the delegated tasks on another executor. The transport is closed from the first handler of the pipeline instead of through Channel.unsafe(), and the plaintext queued in SslHandler is read with SslHandler.pendingOutboundBytes(...). (cherry picked from commit abe6408) Co-authored-by: Violeta Georgieva <[email protected]>
🚨 All tests passed but jobs failed 🚨Failed Jobs without Test Failures❌ Verify native library loading on musl / musl-verify-alpine-x86_64-gcompat 🏷️ Commit: 41bd1b7 Rerun ControlsClick the checkbox to trigger a rerun:
Learn more about TestLens at testlens.app/docs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation:
SslHandler queues the plaintext that is written to it and encrypts all of it in one loop when it is flushed. This loop does not stop when the transport can not keep up, so the encrypted copy of everything that was flushed is allocated at once and kept in the ChannelOutboundBuffer until it is written. When streaming to many slow peers this needs a lot of (direct) memory, see #17304.
Channel.isWritable() can not be used to stop, as the plaintext that is queued in SslHandler counts towards it as well. The channel would stay unwritable because of our own queue, and we would never continue.
Modification:
Result:
SslHandler encrypts at most about the write buffer high water mark ahead of what the transport has written.
The bound does not apply when the channel is closed or closeOutbound() is called while data is held back: everything that is queued is encrypted at once before the close_notify, as the SSLEngine would not encrypt it anymore once its outbound side is closed. This is the same as before this change.
This changes the behaviour of SslHandler in some cases:
Port notes for 5.0:
(cherry picked from commit abe6408)