Skip to content

Conversation

@lhotari
Copy link
Member

@lhotari lhotari commented Sep 25, 2025

PIP: #19074

Motivation

When multiple PulsarClient instances run within the same JVM, each client creates separate thread pools, timers, event loops, and DNS resolvers. This approach leads to significant resource waste, including:

  • Excessive memory usage due to duplicated thread pools and executors
  • High thread counts that can impact application performance
  • Increased DNS lookup load and latency from duplicate DNS resolvers
  • Inefficient resource utilization in applications requiring multiple client connections (e.g., Flink pipelines with multiple Pulsar Sinks/Sources)

This change addresses the resource sharing gap by introducing a public API for sharing core client infrastructure while maintaining isolated connection pools per client instance.

Modifications

This PR implements PIP-234 by adding the following components:

  • PulsarClientSharedResources - Main interface for managing shared resources across multiple PulsarClient instances
  • PulsarClientSharedResourcesBuilder - Fluent API for configuring and building shared resources with support for:
    • Thread pool configuration (listener, internal, scheduled, lookup executors)
    • Event loop group settings with busy-wait support
    • Timer configuration with customizable tick duration
    • DNS resolver settings with comprehensive options (TTL, timeouts, server addresses, etc.)
  • ClientBuilder.sharedResources() - Method to attach shared resources to a client instance
  • Resource sharing implementation - Internal changes to accept externally provided executors, timers, event loops, and DNS resolvers

Key Features

  • Selective sharing: Choose specific resources to share (event loops, thread pools, DNS resolver, timer)
  • Advanced DNS configuration: Full control over DNS resolver behavior including TTL settings, server addresses, and timeout configurations
  • HTTP client integration: Shared DNS resolver and timer are also used by HTTP-based lookup services
  • Service provider integration: AutoClusterFailover and ControlledClusterFailover also benefit from shared DNS resolution

Usage Example

// Create shared resources for 1000+ clients
PulsarClientSharedResources sharedResources = PulsarClientSharedResources.builder()
    .configureEventLoop(config -> config.numberOfThreads(10))
    .configureDnsResolver(config -> config
        .serverAddresses(List.of(new InetSocketAddress("8.8.8.8", 53)))
        .maxTtl(5))
    .build();

// Use shared resources across multiple clients
List<PulsarClient> clients = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
    clients.add(PulsarClient.builder()
        .serviceUrl("pulsar://localhost:6650")
        .sharedResources(sharedResources)
        .build());
}

Documentation

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

@lhotari lhotari added this to the 4.2.0 milestone Sep 25, 2025
@lhotari lhotari self-assigned this Sep 25, 2025
@github-actions github-actions bot added the doc Your PR contains doc changes, no matter whether the changes are in markdown or code files. label Sep 25, 2025
@codecov-commenter
Copy link

codecov-commenter commented Sep 26, 2025

Codecov Report

❌ Patch coverage is 81.71745% with 66 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.18%. Comparing base (1050f48) to head (e43edda).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...t/impl/PulsarClientSharedResourcesBuilderImpl.java 81.72% 14 Missing and 3 partials ⚠️
...ache/pulsar/common/util/netty/DnsResolverUtil.java 57.89% 11 Missing and 5 partials ⚠️
...apache/pulsar/common/util/netty/EventLoopUtil.java 20.00% 10 Missing and 6 partials ⚠️
...r/client/impl/PulsarClientSharedResourcesImpl.java 86.95% 4 Missing and 5 partials ⚠️
...apache/pulsar/client/impl/AutoClusterFailover.java 20.00% 4 Missing ⚠️
...g/apache/pulsar/client/impl/HttpLookupService.java 33.33% 2 Missing ⚠️
...pache/pulsar/client/impl/DnsResolverGroupImpl.java 96.66% 0 Missing and 1 partial ⚠️
...r/client/impl/PulsarClientResourcesConfigurer.java 97.14% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##             master   #24790       +/-   ##
=============================================
+ Coverage     38.67%   74.18%   +35.51%     
- Complexity    13237    33370    +20133     
=============================================
  Files          1850     1911       +61     
  Lines        144540   149024     +4484     
  Branches      16763    17290      +527     
=============================================
+ Hits          55895   110556    +54661     
+ Misses        81156    29635    -51521     
- Partials       7489     8833     +1344     
Flag Coverage Δ
inttests 26.30% <11.35%> (-0.39%) ⬇️
systests 22.68% <11.35%> (-0.13%) ⬇️
unittests 73.72% <81.71%> (+38.85%) ⬆️

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

Files with missing lines Coverage Δ
...n/java/org/apache/pulsar/broker/PulsarService.java 83.79% <ø> (+13.03%) ⬆️
...pulsar/client/api/PulsarClientSharedResources.java 100.00% <100.00%> (ø)
...nt/internal/PulsarClientImplementationBinding.java 62.50% <ø> (+12.50%) ⬆️
...g/apache/pulsar/client/impl/ClientBuilderImpl.java 86.95% <100.00%> (+55.13%) ⬆️
.../org/apache/pulsar/client/impl/ConnectionPool.java 76.05% <100.00%> (+19.24%) ⬆️
.../pulsar/client/impl/ControlledClusterFailover.java 67.61% <100.00%> (+67.61%) ⬆️
...java/org/apache/pulsar/client/impl/HttpClient.java 82.19% <100.00%> (+42.33%) ⬆️
...rg/apache/pulsar/client/impl/PulsarClientImpl.java 76.11% <100.00%> (+17.32%) ⬆️
...nt/impl/PulsarClientImplementationBindingImpl.java 80.30% <100.00%> (+26.45%) ⬆️
...rg/apache/pulsar/client/util/ExecutorProvider.java 66.66% <100.00%> (+4.16%) ⬆️
... and 9 more

... and 1394 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.

@lhotari lhotari requested a review from Copilot September 26, 2025 19:40
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements PIP-234 to enable sharing of thread pools and DNS resolver/cache across multiple Pulsar Client instances in the same JVM. This addresses resource waste when many client instances create separate infrastructure components, reducing memory usage, thread counts, and DNS lookup load.

  • Introduces PulsarClientSharedResources API with builder pattern for configuring shared resources
  • Adds ClientBuilder.sharedResources() method to attach shared resources to clients
  • Refactors client initialization to support both shared and per-client resource management

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pulsar-client-api/src/main/java/org/apache/pulsar/client/api/PulsarClientSharedResources.java Defines the main interface for shared resources with resource types enum
pulsar-client-api/src/main/java/org/apache/pulsar/client/api/PulsarClientSharedResourcesBuilder.java Builder interface with configuration options for different resource types
pulsar-client-api/src/main/java/org/apache/pulsar/client/api/ClientBuilder.java Adds sharedResources() method to ClientBuilder API
pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarClientSharedResourcesImpl.java Implementation of shared resources with lifecycle management
pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarClientSharedResourcesBuilderImpl.java Builder implementation with resource configuration classes
pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarClientResourcesConfigurer.java Factory methods for creating various client resources
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ClientBuilderImpl.java Updates client builder to support shared resources
pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarClientImpl.java Refactors to use resource configurer factory methods
pulsar-client/src/main/java/org/apache/pulsar/client/util/ExecutorProvider.java Adds constructor with daemon thread parameter
pulsar-client/src/main/java/org/apache/pulsar/client/util/ScheduledExecutorProvider.java Adds constructor with daemon thread parameter
pulsar-client/src/test/java/org/apache/pulsar/client/impl/PulsarClientSharedResourcesBuilderImplTest.java Test cases for shared resources functionality

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

lhotari and others added 2 commits September 27, 2025 09:44
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@lhotari lhotari requested a review from Copilot September 27, 2025 06:45
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 4 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@lhotari lhotari requested a review from Copilot September 27, 2025 17:25
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 40 out of 40 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

lhotari and others added 2 commits September 27, 2025 20:27
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@lhotari lhotari requested a review from Copilot September 27, 2025 17:36
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 40 out of 40 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarClientSharedResourcesBuilderImpl.java:1

  • The sharedResources field is declared in the wrong class. This field appears to be intended for ClientBuilderImpl class based on the usage pattern, but it's declared in PulsarClientSharedResourcesBuilderImpl.
/*

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@lhotari lhotari requested a review from Copilot September 27, 2025 17:42
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 40 out of 40 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@lhotari lhotari requested a review from Copilot September 27, 2025 17:47
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 40 out of 40 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@lhotari lhotari requested a review from Copilot September 27, 2025 18:14
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 40 out of 40 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (2)

pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarClientSharedResourcesBuilderImpl.java:1

  • The serialVersionUID is declared in ClientBuilderImpl but the class doesn't implement Serializable. Either implement Serializable or remove this unused field.
/*

pulsar-client/src/main/java/org/apache/pulsar/client/impl/ControlledClusterFailover.java:1

  • The comment mentions 'DNS lookup is performed again' but this logic seems to always force the endpoint to be unresolved. Consider clarifying why this is necessary or if there's a more direct way to ensure fresh DNS resolution.
/*

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@lhotari lhotari requested a review from Copilot September 27, 2025 18:36
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 40 out of 40 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@lhotari lhotari merged commit 8559b68 into apache:master Sep 27, 2025
186 of 192 checks passed
lhotari added a commit that referenced this pull request Oct 6, 2025
…olver/cache across multiple Pulsar Client instances (#24790)

(cherry picked from commit 8559b68)
walkinggo pushed a commit to walkinggo/pulsar that referenced this pull request Oct 8, 2025
…olver/cache across multiple Pulsar Client instances (apache#24790)
AradhyaSharma31 added a commit to AradhyaSharma31/pulsar that referenced this pull request Dec 14, 2025
…flows

Fixes: apache#24790

PIP: apache#234

Motivation
Authentication implementations such as AuthenticationOAuth2 create their own thread pools, which prevents resource sharing across multiple Pulsar client instances. Since PIP-234 adds support for sharing resources like Netty EventLoopGroup, we need to extend this capability to authentication flows to avoid unnecessary resource duplication.

Modifications
Added AuthenticationInitContext interface in the public API to pass shared resources to authentication providers
Implemented AuthenticationInitContextImpl with service registry for shared resources
Extended Authentication interface with new start(AuthenticationInitContext) method (backward compatible)
Modified PulsarClientImpl to create context and register shared resources (EventLoopGroup, Timer)
Updated AuthenticationOAuth2 to pass context to OAuth2 flows
Extended Flow interface in OAuth2 with context-aware initialization
Modified FlowBase to use shared EventLoopGroup and Timer from context for AsyncHttpClient
Key changes enable OAuth2 flows to reuse:

Shared Netty EventLoopGroup
Shared Timer
Verifying this change
This change is already covered by existing tests, such as:

OAuth2 authentication tests
Client connection tests
Added AuthenticationResourceSharingTest for new context functionality
Does this pull request potentially affect one of the following parts:
 The public API (adds AuthenticationInitContext interface and extends Authentication interface)
 Dependencies (add or upgrade a dependency)
 The schema
 The default values of configurations
 The threading model (enables sharing, doesn't change model)
 The binary protocol
 The REST endpoints
 The admin CLI options
 The metrics
 Anything that affects deployment
Documentation
 doc-required
Future Work
Potential extension to other authentication methods
Additional resource types can be added to the context as needed
Shared DNS resolver/cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/client cherry-picked/branch-4.1 doc Your PR contains doc changes, no matter whether the changes are in markdown or code files. ready-to-test release/4.1.2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants