GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Optimize wrap buffer cumulation in SslHandler and don't mutate input buffers by lhotari · Pull Request #14086 · netty/netty

Conversation

@lhotari

@lhotari lhotari commented May 30, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

semistone reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
lhotari added 3 commits May 30, 2024 14:51
@lhotari
Optimize wrap buffer cumulation in SslHandler without mutating inputs
3fdce31
@lhotari
Revert change to replace internalNioBuffer with nioBuffer
964f0a5
- AbstractDerivedByteBuf implementations will handle internalNioBuffer
  calls in a thread-safe way
@lhotari
Add more test scenarios for cumulation test
9c4b4ef
This was referenced May 30, 2024
[fix][broker] Fix data corruption issues when TLS is enabled and optimize TLS between Pulsar client and brokers apache/pulsar#22810
Closed
Fix TLS stability issues with V2 protocol that caused data corruption apache/bookkeeper#4404
Merged
SslHandler doesn't support a use case where the input ByteBuf is shared across multiple threads #14069
Closed
Mqtt payload for publish message copied 2 times during lifetime #13844
Closed
@normanmaurer
normanmaurer requested a review from franz1981 May 31, 2024 08:24
normanmaurer
normanmaurer requested changes May 31, 2024
View reviewed changes
Comment thread transport/src/main/java/io/netty/channel/AbstractCoalescingBufferQueue.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread transport/src/main/java/io/netty/channel/AbstractCoalescingBufferQueue.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread handler/src/main/java/io/netty/handler/ssl/SslHandlerCoalescingBufferQueue.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@lhotari @normanmaurer
Apply suggestions from code review
34ac4cd
Co-authored-by: Norman Maurer <[email protected]>
@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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

lhotari commented Jun 1, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lhotari

lhotari commented Jun 1, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

chrisvest
chrisvest approved these changes Jun 3, 2024
View reviewed changes
@chrisvest
chrisvest merged commit 20a11b3 into netty:4.1 Jun 3, 2024
chrisvest pushed a commit that referenced this pull request Jun 3, 2024
@lhotari @normanmaurer @chrisvest
Optimize wrap buffer cumulation in SslHandler and don't mutate input …
4565f94
…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]>
This was referenced Jun 11, 2024
[improve][misc] Upgrade to Netty 4.1.111.Final and switch to use grpc-netty-shaded apache/pulsar#22892
Merged
Upgrade to Netty 4.1.111.Final and switch to use grpc-netty-shaded apache/bookkeeper#4426
Merged
hezhangjian pushed a commit to apache/bookkeeper that referenced this pull request Jun 13, 2024
@lhotari
Upgrade to Netty 4.1.111.Final and switch to use grpc-netty-shaded (#…
a95f698
…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
@lhotari
Upgrade to Netty 4.1.111.Final and switch to use grpc-netty-shaded (#…
7efcbd2
…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
@lhotari
Upgrade to Netty 4.1.111.Final and switch to use grpc-netty-shaded (#…
f5dfd89
…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
@lhotari Anup Ghatage
Upgrade to Netty 4.1.111.Final and switch to use grpc-netty-shaded (a…
67c870f
…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
@lhotari
[improve][misc] Optimize TLS performance by omitting extra buffer copies
937b3e3
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
@lhotari lhotari mentioned this pull request Aug 1, 2024
[improve][misc] Optimize TLS performance by omitting extra buffer copies apache/pulsar#23115
Merged
1 of 4 tasks
This was referenced Sep 14, 2026
Http2: Reserve promised stream for PUSH_PROMISE after local RST_STREAM #17471
Closed
cache failure 1233e4a5 #17565
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@chrisvest chrisvest chrisvest approved these changes
@normanmaurer normanmaurer normanmaurer approved these changes
@franz1981 franz1981 Awaiting requested review from franz1981

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.1.111.Final

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

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@lhotari @chrisvest @normanmaurer
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.