Skip to content

Optimize wrap buffer cumulation in SslHandler and don't mutate input buffers - #14086

Merged
chrisvest merged 4 commits into
netty:4.1from
lhotari:lh-dont-mutate-sslhandler-input-buffers-by-default
Jun 3, 2024
Merged

chrisvest merged 4 commits into
netty:4.1from
lhotari:lh-dont-mutate-sslhandler-input-buffers-by-default

Conversation

@lhotari

@lhotari lhotari commented May 30, 2024 •

Copy link
Copy Markdown
Contributor

Motivation:

SslHandler doesn't support sharing the input buffers since the input buffers get mutated. This issue has been reported as #14069. Fixes have already been made to support the use case using read-only buffers as input. However, this is not useful for existing Netty applications.

I checked the Netty code once again and noticed that it's possible to optimize the current implementation in SslHandler and avoid the buffer mutation.

The default behavior in SslHandler causes very hard to debug problems. In Pulsar, there have been issues open for multiple years that have been caused by the SslHandler's default behavior of mutating the input buffers (apache/pulsar#22601 is one starting point to the issues).

The current implementation of SslHandler will mutate the input buffers by using the input buffer as a cumulation buffer when there's available capacity. This is not optimal at all. This PR contains an improvement where the cumulation buffer is allocated to the final size immediately so that cumulation is more optimal. That's why I think that this will be more optimal in the end and won't cause performance regressions.

After these changes, it should be possible to pass a slice or duplicate of a buffer and the input buffer doesn't get mutated. The input buffer's readIndex will be modified so multiple threads cannot share the same input buffer without a slice or duplicate.

I created a failing test case that shows that a duplicate buffer doesn't prevent input buffer mutation.
image
It's a very surprising behavior of SslHandler that passing a .retainedDuplicate() buffer could result in mutations to the original buffer. That's another reason why I think that SslHandler shouldn't ever mutate the inputs.

Modification:

  • add protected ByteBuf composeFirst(ByteBufAllocator allocator, ByteBuf first, int bufferSize) to AbstractCoalescingBufferQueue to support allocating an optimal cumulation buffer instead of using the input buffer as a cumulation buffer
  • remove dead code from compose where there was handling for CompositeByteBuf
    • composeFirst will be called before compose so this case isn't needed.

Result:

Fixes #14069 without the need to set the input buffer as read-only

@lhotari
lhotari requested a review from normanmaurer May 31, 2024 09:50
@lhotari

lhotari commented May 31, 2024

Copy link
Copy Markdown
Contributor Author

@normanmaurer Thanks for the review. I applied your suggestions. PTAL

@normanmaurer normanmaurer added this to the 4.1.111.Final milestone May 31, 2024
@normanmaurer
normanmaurer requested a review from chrisvest May 31, 2024 11:57
@lhotari

lhotari commented Jun 1, 2024 •

Copy link
Copy Markdown
Contributor Author

I have been wondering the root cause of the problems in Apache Pulsar. I finally found the explanation how it's possible that SslHandler is able to corrupt buffers handled by Apache Pulsar.

The issue #12919 is titled "ByteBuf slice duplicates are not slices". This behavior is very surprising. It explains why the SslHandler has been causing issues in Apache Pulsar. The incoming messages come from the network and use the LengthFieldBasedFrameDecoder, which slices the buffer into frame buffers. Pulsar later calls retainedDuplicate. This removes the effect of slicing (as described in issue #12919) and makes the buffer writable. The impact of this is that the original buffer used by the LengthFieldBasedFrameDecoder gets mutated by the SslHandler later when the sliced frame buffer (which is made writable with .retainedDuplicate) is passed to the Apache Bookkeeper client, which also uses Netty and contains an SslHandler. The SslHandler's cumulation in the attemptCopyToCumulation method thinks that it's fine to mutate the input buffer since it's writable and that's what causes data corruption.

Issue #12919 references the vert.x issue eclipse-vertx/vert.x#4509. It seems vert.x applied a workaround in eclipse-vertx/vert.x#4517.

I also found a workaround for a data corruption issue within Netty's http codecs, #11802. That was to address #11792.

Such workarounds won't be needed when we eliminate the surprising behaviour. The PR will address it for Sslhandler and also optimise the cumulation. I think #12919 should also be addressed so that a duplicating a slice doesn't provide write access to the parent buffer outside of the slice.

@lhotari

lhotari commented Jun 1, 2024 •

Copy link
Copy Markdown
Contributor Author

@chrisvest @franz1981 do you also have a chance to review this PR? Thanks!

@lhotari

lhotari commented Jun 3, 2024

Copy link
Copy Markdown
Contributor Author

As a followup, I also created #14093 with the title "Duplicate of slice should have the same capacity as the original slice so that it's not writable" to address the issue #12919 by @vietj, "ByteBuf slice duplicates are not slices".
(In Pulsar, that's why SslHandler was able to corrupt the input buffer of LengthFieldBasedFrameDecoder since calling .retainedDuplicate later in Pulsar code "removed" the slice created by LengthFieldBasedFrameDecoder.)

@chrisvest
chrisvest merged commit 20a11b3 into netty:4.1 Jun 3, 2024
chrisvest pushed a commit that referenced this pull request Jun 3, 2024
…buffers (#14086)

Motivation:

SslHandler doesn't support sharing the input buffers since the input
buffers get mutated. This issue has been reported as #14069. Fixes have
already been made to support the use case using read-only buffers as
input. However, this is not useful for existing Netty applications.

I checked the Netty code once again and noticed that it's possible to
optimize the current implementation in SslHandler and avoid the buffer
mutation.

The default behavior in SslHandler causes very hard to debug problems.
In Pulsar, there have been issues open for multiple years that have been
caused by the SslHandler's default behavior of mutating the input
buffers (apache/pulsar#22601 is one starting point to the issues).

The current implementation of SslHandler will mutate the input buffers
by using the input buffer as a cumulation buffer when there's available
capacity. This is not optimal at all. This PR contains an improvement
where the cumulation buffer is allocated to the final size immediately
so that cumulation is more optimal. That's why I think that this will be
more optimal in the end and won't cause performance regressions.

After these changes, it should be possible to pass a slice or duplicate
of a buffer and the input buffer doesn't get mutated. The input buffer's
readIndex will be modified so multiple threads cannot share the same
input buffer without a slice or duplicate.

I created a failing test case that shows that a duplicate buffer doesn't
prevent input buffer mutation.

![image](https://github.com/netty/netty/assets/66864/0abec333-6079-40bd-8bc1-ccfd3df6fae1)
It's a very surprising behavior of SslHandler that passing a
`.retainedDuplicate()` buffer could result in mutations to the original
buffer. That's another reason why I think that SslHandler shouldn't ever
mutate the inputs.

Modification:

- add `protected ByteBuf composeFirst(ByteBufAllocator allocator,
ByteBuf first, int bufferSize)` to AbstractCoalescingBufferQueue to
support allocating an optimal cumulation buffer instead of using the
input buffer as a cumulation buffer
- remove dead code from `compose` where there was handling for
CompositeByteBuf
- `composeFirst` will be called before `compose` so this case isn't
needed.

Result:

Fixes #14069 without the need to set the input buffer as read-only

---------

Co-authored-by: Norman Maurer <[email protected]>
hezhangjian pushed a commit to apache/bookkeeper that referenced this pull request Jun 13, 2024
…4426)

### Motivation

[Netty 4.1.111.Final](https://netty.io/news/2024/06/11/4-1-111-Final.html) contains important fixes:
- netty/netty#14086
- netty/netty#14093

On the Pulsar side these address Broker stability when TLS with Bookkeeper V2 protocol is used between Broker and Bookies. The Bookkeeper client didn't contain a workaround for the Netty SslHandler "feature" like Pulsar did. That's why Bookkeeper client was impacted. Netty 4.1.111.Final will address those stability issues with the Bookkeeper client when using V2 protocol over TLS.

### Changes

- Upgrade Netty to 4.1.111.Final
- Switch to use grpc-netty-shaded instead of grpc-netty
  - grpc-java is not compatible with Netty 4.1.111.Final. [grpc-java supports only specific Netty versions](https://github.com/grpc/grpc-java/blob/master/SECURITY.md#netty). The solution is to use grpc-netty-shaded instead of grpc-netty dependency.
- Add module to shade jetcd-core partially so that it can work with grpc-netty-shaded. 
  - jetcd-core library and it's transient dependency vertx-grpc has a direct dependency on grpc-netty and it doesn't work with grpc-netty-shaded without this solution.
  - this solution avoids the need to shade grpc libraries completely which would cause a lot of duplication and increase build times.
lhotari added a commit to apache/bookkeeper that referenced this pull request Jun 13, 2024
…4426)

[Netty 4.1.111.Final](https://netty.io/news/2024/06/11/4-1-111-Final.html) contains important fixes:
- netty/netty#14086
- netty/netty#14093

On the Pulsar side these address Broker stability when TLS with Bookkeeper V2 protocol is used between Broker and Bookies. The Bookkeeper client didn't contain a workaround for the Netty SslHandler "feature" like Pulsar did. That's why Bookkeeper client was impacted. Netty 4.1.111.Final will address those stability issues with the Bookkeeper client when using V2 protocol over TLS.

- Upgrade Netty to 4.1.111.Final
- Switch to use grpc-netty-shaded instead of grpc-netty
  - grpc-java is not compatible with Netty 4.1.111.Final. [grpc-java supports only specific Netty versions](https://github.com/grpc/grpc-java/blob/master/SECURITY.md#netty). The solution is to use grpc-netty-shaded instead of grpc-netty dependency.
- Add module to shade jetcd-core partially so that it can work with grpc-netty-shaded.
  - jetcd-core library and it's transient dependency vertx-grpc has a direct dependency on grpc-netty and it doesn't work with grpc-netty-shaded without this solution.
  - this solution avoids the need to shade grpc libraries completely which would cause a lot of duplication and increase build times.

(cherry picked from commit a95f698)
lhotari added a commit to apache/bookkeeper that referenced this pull request Jun 13, 2024
…4426)

[Netty 4.1.111.Final](https://netty.io/news/2024/06/11/4-1-111-Final.html) contains important fixes:
- netty/netty#14086
- netty/netty#14093

On the Pulsar side these address Broker stability when TLS with Bookkeeper V2 protocol is used between Broker and Bookies. The Bookkeeper client didn't contain a workaround for the Netty SslHandler "feature" like Pulsar did. That's why Bookkeeper client was impacted. Netty 4.1.111.Final will address those stability issues with the Bookkeeper client when using V2 protocol over TLS.

- Upgrade Netty to 4.1.111.Final
- Switch to use grpc-netty-shaded instead of grpc-netty
  - grpc-java is not compatible with Netty 4.1.111.Final. [grpc-java supports only specific Netty versions](https://github.com/grpc/grpc-java/blob/master/SECURITY.md#netty). The solution is to use grpc-netty-shaded instead of grpc-netty dependency.
- Add module to shade jetcd-core partially so that it can work with grpc-netty-shaded.
  - jetcd-core library and it's transient dependency vertx-grpc has a direct dependency on grpc-netty and it doesn't work with grpc-netty-shaded without this solution.
  - this solution avoids the need to shade grpc libraries completely which would cause a lot of duplication and increase build times.

(cherry picked from commit a95f698)
Ghatage pushed a commit to sijie/bookkeeper that referenced this pull request Jul 12, 2024
…pache#4426)

### Motivation

[Netty 4.1.111.Final](https://netty.io/news/2024/06/11/4-1-111-Final.html) contains important fixes:
- netty/netty#14086
- netty/netty#14093

On the Pulsar side these address Broker stability when TLS with Bookkeeper V2 protocol is used between Broker and Bookies. The Bookkeeper client didn't contain a workaround for the Netty SslHandler "feature" like Pulsar did. That's why Bookkeeper client was impacted. Netty 4.1.111.Final will address those stability issues with the Bookkeeper client when using V2 protocol over TLS.

### Changes

- Upgrade Netty to 4.1.111.Final
- Switch to use grpc-netty-shaded instead of grpc-netty
  - grpc-java is not compatible with Netty 4.1.111.Final. [grpc-java supports only specific Netty versions](https://github.com/grpc/grpc-java/blob/master/SECURITY.md#netty). The solution is to use grpc-netty-shaded instead of grpc-netty dependency.
- Add module to shade jetcd-core partially so that it can work with grpc-netty-shaded. 
  - jetcd-core library and it's transient dependency vertx-grpc has a direct dependency on grpc-netty and it doesn't work with grpc-netty-shaded without this solution.
  - this solution avoids the need to shade grpc libraries completely which would cause a lot of duplication and increase build times.
lhotari added a commit to lhotari/pulsar that referenced this pull request Aug 1, 2024
After Netty 4.1.111.Final, it's no longer necessary to make buffer copies when TLS is enabled.
netty/netty#14086 contains the fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SslHandler doesn't support a use case where the input ByteBuf is shared across multiple threads

3 participants