Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache ssl contexts and reuse them #12404

Merged
merged 4 commits into from
Feb 16, 2024
Merged

Conversation

zhtaoxiang
Copy link
Contributor

performance

Instead of creating a new SslContext for the same TlsConfig multiple times, this PR reuses the same SslContext if it's already created.

  1. This saves memory by
    a. reducing the number of objects created.
    b. only one thread (https://github.com/apache/pinot/blob/master/pinot-common/src/main/java/org/apache/pinot/common/utils/TlsUtils.java#L385) will be created for reloading the same tls store and trust store when tlsconfigs are the same.
  2. This can also speed up execution because SslContext only needs to be created ones for the same TlsConfig

Reusing the same SslContext does not change the validation logic and does not introduce security flaws because the same key and ca are used in the old and new implementation.

public static final String SSL = "ssl";
private static final Map<TlsConfig, SslContext> CLIENT_SSL_CONTEXTS_CACHE = new ConcurrentHashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

what about just use hashcode as the key to avoid the object copy?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a great idea!

return TlsUtils.buildClientContext(tlsConfig).newHandler(ch.alloc());
// Make a copy of the TlsConfig because the TlsConfig is mutable, when the TlsConfig is used as the key of the
// CLIENT_SSL_CONTEXTS_CACHE, the TlsConfig should not be changed.
TlsConfig tlsConfigCopy = new TlsConfig(tlsConfig);
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the frequency of this call and object copy?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The frequency should be low, only at the time when the channel is created. It is the same as how many times the channel should be created.

Anyway, changed the map to use hashCode as the key, so the object copy can be avoided.

Copy link
Contributor

@xiangfu0 xiangfu0 left a comment

Choose a reason for hiding this comment

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

Overall lgtm, minor optimization might be possible, up for discussion.

@codecov-commenter
Copy link

codecov-commenter commented Feb 13, 2024

Codecov Report

Attention: 36 lines in your changes are missing coverage. Please review.

Comparison is base (38d86b0) 61.74% compared to head (4cd757b) 0.00%.

Files Patch % Lines
...che/pinot/core/transport/grpc/GrpcQueryServer.java 0.00% 15 Missing ⚠️
...pache/pinot/common/utils/grpc/GrpcQueryClient.java 0.00% 11 Missing ⚠️
...he/pinot/core/transport/ChannelHandlerFactory.java 0.00% 8 Missing ⚠️
...java/org/apache/pinot/common/config/TlsConfig.java 0.00% 2 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             master   #12404       +/-   ##
=============================================
- Coverage     61.74%    0.00%   -61.75%     
=============================================
  Files          2436     2360       -76     
  Lines        133108   129354     -3754     
  Branches      20617    20059      -558     
=============================================
- Hits          82191        0    -82191     
- Misses        44876   129354    +84478     
+ Partials       6041        0     -6041     
Flag Coverage Δ
custom-integration1 ?
integration 0.00% <0.00%> (-0.01%) ⬇️
integration1 ?
integration2 0.00% <0.00%> (ø)
java-11 ?
java-21 0.00% <0.00%> (-61.63%) ⬇️
skip-bytebuffers-false ?
skip-bytebuffers-true 0.00% <0.00%> (-61.61%) ⬇️
temurin 0.00% <0.00%> (-61.75%) ⬇️
unittests ?
unittests1 ?
unittests2 ?

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@xiangfu0 xiangfu0 merged commit 24f48e3 into apache:master Feb 16, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants