Skip to content

[improve][broker,proxy] Use ChannelVoidPromise to avoid useless promise objects creation - #19141

Merged
nicoloboschi merged 4 commits into
apache:masterfrom
nicoloboschi:netty-void-promise
Jan 10, 2023
Merged

[improve][broker,proxy] Use ChannelVoidPromise to avoid useless promise objects creation#19141
nicoloboschi merged 4 commits into
apache:masterfrom
nicoloboschi:netty-void-promise

Conversation

@nicoloboschi

Copy link
Copy Markdown
Contributor

Motivation

In the broker and proxy most of the writes callback are not considered. In those cases we can save useless allocations of default channel promises by using the voidPromise() method.

Modifications

  • Use VoidChannelPromise if the promise result is never checked

Verifying this change

  • Make sure that the change passes the CI checks.

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

Comment thread pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/LookupProxyHandler.java Outdated
@nodece

nodece commented Jan 6, 2023

Copy link
Copy Markdown
Member

Maybe you should move the writeAndFlushVoidPromise method to pulsar-common module to eliminate the duplication.

@nicoloboschi

Copy link
Copy Markdown
Contributor Author

@nodece done, PTAL again

@lhotari

lhotari commented Jan 10, 2023

Copy link
Copy Markdown
Member

Related to #16113.

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really great improvement. The only small nag is about naming. writeAndFlushVoidPromise is a very clumsy name. I don't have a good name to suggest immediately, but perhaps we could think about something.

It's not only the "useless promise objects creation" that is the reason to use voidPromise. Another reason to use a ctx.voidPromise() is because it adds proper error handling. Exceptions won't be propagated in Netty 4.x unless the the ChannelFuture's listener handles errors or something like ctx.voidPromise() is used.
(Related to SO Q&A https://stackoverflow.com/q/54169262 and https://stackoverflow.com/a/9030420 .)

I think renaming writeAndFlushVoidPromise in PulsarCommandSender and ServerCnx to writeAndFlush would simplify naming. Renaming writeAndFlushVoidPromise to writeAndFlushWithVoidPromise in the utility class would improve the naming.

* Contains utility methods for working with Netty Channels.
*/
public class NettyChannelUtil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add proper javadoc to this method. I'd suggest explaining that besides reducing unnecessary object creation, using ctx.voidPromise() will propagate exceptions properly. Related to SO Q&A https://stackoverflow.com/q/54169262 and https://stackoverflow.com/a/9030420 .

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also replace any occurrences of .addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE) with the use of ctx.voidPromise() solution. I realised that I had added .addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE) in #14713 based on SO answer https://stackoverflow.com/a/54377238 . (I added a new answer to SO with the voidPromise solution.)

Comment thread pulsar-common/src/main/java/org/apache/pulsar/common/protocol/PulsarDecoder.java Outdated
Comment thread pulsar-common/src/main/java/org/apache/pulsar/common/protocol/PulsarDecoder.java Outdated
Comment thread pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/LookupProxyHandler.java Outdated
Comment thread pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/LookupProxyHandler.java Outdated
@nicoloboschi

Copy link
Copy Markdown
Contributor Author

Thanks @lhotari
I've renamed the methods and javadoc as you suggested. I've also replaced the .addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE) usages and added unit tests.

Lastly, I've also added a convenience method for closing the channel after the write.
PTAL again

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work @nicoloboschi

@lhotari

lhotari commented Jan 10, 2023

Copy link
Copy Markdown
Member

/pulsarbot rerun-failure-checks

}

private void writeAndFlush(ByteBuf cmd) {
NettyChannelUtil.writeAndFlushWithVoidPromise(outboundChannel, cmd);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you miss FIRE_EXCEPTION_ON_FAILURE?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nodece VoidChannelPromise will propagate exceptions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@codecov-commenter

codecov-commenter commented Jan 10, 2023

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 45.89041% with 79 lines in your changes missing coverage. Please review.
✅ Project coverage is 47.13%. Comparing base (9ef54fd) to head (d0e0aa4).
⚠️ Report is 3019 commits behind head on master.

Files with missing lines Patch % Lines
...va/org/apache/pulsar/broker/service/ServerCnx.java 35.48% 40 Missing ⚠️
...rg/apache/pulsar/proxy/server/ProxyConnection.java 34.78% 14 Missing and 1 partial ⚠️
...apache/pulsar/proxy/server/LookupProxyHandler.java 39.13% 14 Missing ⚠️
...pulsar/broker/service/PulsarCommandSenderImpl.java 74.07% 7 Missing ⚠️
...apache/pulsar/proxy/server/DirectProxyHandler.java 72.72% 3 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             master   #19141      +/-   ##
============================================
- Coverage     47.22%   47.13%   -0.10%     
+ Complexity    10713    10672      -41     
============================================
  Files           713      713              
  Lines         69697    69703       +6     
  Branches       7485     7485              
============================================
- Hits          32914    32854      -60     
- Misses        33096    33143      +47     
- Partials       3687     3706      +19     
Flag Coverage Δ
unittests 47.13% <45.89%> (-0.10%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...apache/pulsar/proxy/server/DirectProxyHandler.java 63.79% <72.72%> (+0.15%) ⬆️
...pulsar/broker/service/PulsarCommandSenderImpl.java 68.36% <74.07%> (+6.43%) ⬆️
...apache/pulsar/proxy/server/LookupProxyHandler.java 57.02% <39.13%> (+0.55%) ⬆️
...rg/apache/pulsar/proxy/server/ProxyConnection.java 55.77% <34.78%> (-0.57%) ⬇️
...va/org/apache/pulsar/broker/service/ServerCnx.java 47.31% <35.48%> (+2.38%) ⬆️

... and 41 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nicoloboschi
nicoloboschi merged commit d6fcdb8 into apache:master Jan 10, 2023
@nicoloboschi nicoloboschi self-assigned this Jan 10, 2023
@nicoloboschi nicoloboschi added this to the 2.12.0 milestone Jan 10, 2023

@michaeljmarshall michaeljmarshall left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LTMG. Great catch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/broker area/proxy doc-not-needed Your PR changes do not impact docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants