[feat][client] PIP-234: Support shared resources in AuthenticationOAuth2 to reduce thread usage#25072
[feat][client] PIP-234: Support shared resources in AuthenticationOAuth2 to reduce thread usage#250723pacccccc wants to merge 10 commits intoapache:masterfrom
Conversation
lhotari
left a comment
There was a problem hiding this comment.
I think that there's a need to revisit the original design expressed in#24795. The comment in #24944 (comment) contains some details of the direction:
For authentication plugins, one possibility would be that there would be a way to directly get a HTTP client with given configuration (such as trustCertsFilePath) that is sufficiently abstracted so that there's no need to couple to AsyncHttpClient in authentication plugin implementations.
One of the problems in exposing Netty and AsyncHttpClient classes in the public interfaces is that the classes are different when using a shaded Pulsar client.
That is why there should be a sufficient abstraction for the HTTP client.
For Pulsar internal authentication plugins, it wouldn't be a problem to support the solution in this PR since the classes are also shaded in the shaded Pulsar client and there wouldn't be a problem with unshaded Netty and shaded Netty classes, for example. However, it might be work trying to find a more stable abstraction that expressed in the original design.
fix: #24795
🔗 Related links
Motivation
Currently, each instance of
AuthenticationOAuth2creates its own HTTP client with dedicated threadpoolsandDNS resolvers. In applications with multiple Pulsar clients or authentication providers, this leads to excessive thread creation and inefficient resource utilization. According to issues #24795 and #24944, there's a need for authentication implementations to share resources like thread pools to reduce system overhead.With this PR, the OAuth2 authentication flow is enhanced as follows:
1.Resource Sharing at Client Initialization:
PulsarClientis created, it builds anAuthenticationInitContextcontaining shared resourcesEventLoopGroup,Timer, andNameResolver2.Authentication Provider Initialization:
Authentication.start(AuthenticationInitContext)method is called with the contextcontext.getService(Class<T>)3.Simplified HTTP Client Creation:
AuthenticationHttpClientFactory.createHttpClient()to obtain pre-configured HTTP clientsAuthenticationInitContextwhen availabletimeouts,SSL certificates) if needed4.Backward-Compatible Factory Methods:
Modifications
1.New API Interface:
Introduced
AuthenticationInitContextinterface providing access to shared servicesAdded
start(AuthenticationInitContext)method to Authentication interface2.Implementation:
Created
AuthenticationInitContextImplimplementing the context interfaceModified
PulsarClientImplto build and pass context during authentication initializationUpdated
AuthenticationOAuth2to use shared HTTP client resources via the context3.HTTP Client Factory:
Created
AuthenticationHttpClientFactoryfor building HTTP clients with shared resourcesAdded
AuthenticationHttpClientConfigfor HTTP client configurationModified OAuth2 flow classes to accept pre-configured HTTP clients and DNS resolvers
4.Factory Updates:
Enhanced
AuthenticationFactoryOAuth2.ClientCredentialsBuilderto support custom HTTP clients and DNS resolversUpdated factory methods to leverage shared resources when available
Verifying this change
Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes
Documentation
docdoc-requireddoc-not-neededdoc-completeMatching PR in forked repository
PR in forked repository: 3pacccccc#35