Skip to content

[improve][broker] Implement PIP-430 Pulsar Broker cache improvements - #24623

Merged
lhotari merged 75 commits into
apache:masterfrom
lhotari:lh-PIP-430-implementation
Aug 28, 2025
Merged

lhotari merged 75 commits into
apache:masterfrom
lhotari:lh-PIP-430-implementation

Conversation

@lhotari

@lhotari lhotari commented Aug 12, 2025 •

Copy link
Copy Markdown
Member

Fixes #16421
Fixes #24656

Motivation

This is the 2nd implementation PR for "PIP-430: Pulsar Broker cache improvements: refactoring eviction and adding a new cache strategy based on expected read count"

Modifications

Documentation

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

@lhotari lhotari added this to the 4.1.0 milestone Aug 12, 2025
@github-actions github-actions Bot added PIP doc Your PR contains doc changes, no matter whether the changes are in markdown or code files. labels Aug 12, 2025
@codecov-commenter

codecov-commenter commented Aug 13, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.53388% with 287 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.18%. Comparing base (829df71) to head (bbca447).
⚠️ Report is 49 commits behind head on master.

Files with missing lines Patch % Lines
...mledger/impl/ActiveManagedCursorContainerImpl.java 63.61% 122 Missing and 21 partials ⚠️
.../ActiveManagedCursorContainerNavigableSetImpl.java 37.50% 68 Missing and 7 partials ⚠️
...eeper/mledger/impl/ManagedCursorContainerImpl.java 89.43% 13 Missing and 2 partials ⚠️
...mledger/impl/cache/RangeEntryCacheManagerImpl.java 41.66% 12 Missing and 2 partials ⚠️
...per/mledger/impl/cache/RangeCacheRemovalQueue.java 83.33% 5 Missing and 5 partials ⚠️
...keeper/mledger/impl/cache/RangeEntryCacheImpl.java 92.00% 2 Missing and 4 partials ⚠️
...rg/apache/pulsar/broker/service/BrokerService.java 57.14% 6 Missing ⚠️
...kkeeper/mledger/impl/ManagedLedgerFactoryImpl.java 20.00% 4 Missing ⚠️
...ache/pulsar/broker/ManagedLedgerClientFactory.java 57.14% 2 Missing and 1 partial ⚠️
...pache/bookkeeper/mledger/ManagedLedgerFactory.java 0.00% 2 Missing ⚠️
... and 7 more
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             master   #24623      +/-   ##
============================================
- Coverage     74.26%   74.18%   -0.09%     
- Complexity    33213    33377     +164     
============================================
  Files          1885     1893       +8     
  Lines        146953   147722     +769     
  Branches      16928    17080     +152     
============================================
+ Hits         109136   109582     +446     
- Misses        29116    29395     +279     
- Partials       8701     8745      +44     
Flag Coverage Δ
inttests 26.64% <35.11%> (+0.05%) ⬆️
systests 22.74% <32.54%> (+0.11%) ⬆️
unittests 73.67% <70.53%> (-0.09%) ⬇️

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

Files with missing lines Coverage Δ
...ache/bookkeeper/mledger/EntryReadCountHandler.java 100.00% <100.00%> (ø)
...apache/bookkeeper/mledger/ManagedLedgerConfig.java 96.57% <100.00%> (+0.01%) ⬆️
...bookkeeper/mledger/ManagedLedgerFactoryConfig.java 100.00% <ø> (ø)
.../org/apache/bookkeeper/mledger/impl/EntryImpl.java 89.39% <100.00%> (+2.47%) ⬆️
...ookkeeper/mledger/impl/ManagedCursorContainer.java 87.50% <ø> (-9.82%) ⬇️
...che/bookkeeper/mledger/impl/ManagedCursorImpl.java 78.09% <100.00%> (+0.08%) ⬆️
...org/apache/bookkeeper/mledger/impl/OpAddEntry.java 77.46% <100.00%> (+0.54%) ⬆️
...he/bookkeeper/mledger/impl/ReadOnlyCursorImpl.java 95.83% <100.00%> (+0.37%) ⬆️
...kkeeper/mledger/impl/cache/EntryCacheDisabled.java 75.55% <100.00%> (ø)
...keeper/mledger/impl/cache/EntryLengthFunction.java 100.00% <100.00%> (ø)
... and 26 more

... and 82 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 marked this pull request as draft August 13, 2025 11:51
@lhotari

lhotari commented Aug 14, 2025 •

Copy link
Copy Markdown
Member Author

Here's the JMH results for the latest changes. The new getNumberOfCursorsAtSamePositionOrBefore behavior required for cacheEvictionByExpectedReadCount in ActiveManagedCursorContainerImpl is not causing an overhead compared to the previous ManagedCursorContainer tracking for slowest reader position.

Here's a visualization of the results:
JMH Visualizer: ActiveManagedCursorContainerBenchmark

text results

One interesting detail is that ActiveManagedCursorContainerImpl's algorithm would be faster than ManagedCursorContainer's algorithm for tracking the slowest cursor, assuming that most cursors are moving forward (which is the case).

@lhotari
lhotari marked this pull request as ready for review August 14, 2025 11:44
@lhotari

lhotari commented Aug 18, 2025 •

Copy link
Copy Markdown
Member Author

The recently added "experimental" test case BrokerEntryCacheMultiBrokerTest.testTailingReadsRollingRestart shows the real value of PIP-430 changes. It demonstrates a rolling restart where a producer is producing to a topic and there are 10 connected consumers with unique subscriptions. During the 30 second test period the producer produces as fast as it can and the consumers consume as fast as they can. There are 2 brokers in the cluster and a broker is restarted 3 times during the test period.

The with PIP-430 defaults, Cache hits 98.56%, Cache misses 1.44% :

2025-08-18T19:39:44,894 - INFO  - [main:BrokerEntryCacheMultiBrokerTest] - Produced 21655 and Consumed 206425 messages (across 10 consumers with unique subscriptions) in total. Number of BK reads 74 with 2972 entries. Cache hits 98.56%, Cache misses 1.44%, Number of restarts 3

with cacheEvictionByExpectedReadCount=false, Cache hits 81.66%, Cache misses 18.34%

2025-08-18T19:41:17,119 - INFO  - [main:BrokerEntryCacheMultiBrokerTest] - Produced 20767 and Consumed 197466 messages (across 10 consumers with unique subscriptions) in total. Number of BK reads 848 with 36220 entries. Cache hits 81.66%, Cache misses 18.34%, Number of restarts 3

with cacheEvictionByExpectedReadCount=false and managedLedgerCacheEvictionExtendTTLOfRecentlyAccessed=false, Cache hits 72.97%, Cache misses 27.03%

2025-08-18T19:42:34,214 - INFO  - [main:BrokerEntryCacheMultiBrokerTest] - Produced 20888 and Consumed 198727 messages (across 10 consumers with unique subscriptions) in total. Number of BK reads 1309 with 53708 entries. Cache hits 72.97%, Cache misses 27.03%, Number of restarts 3

The results vary quite a lot and these are values from single runs. However, it demonstrates a significant improvement in reducing the amount of cache misses in rolling restarts when PIP-430 defaults are used.

@lhotari
lhotari requested a review from pdolif August 27, 2025 19:41

@merlimat merlimat left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The improvements looks super-impressive! Having the micro-benchmarks make a great case.

Only minor comments

@lhotari
lhotari merged commit 490ba0c into apache:master Aug 28, 2025
98 of 100 checks passed
@lhotari

lhotari commented Sep 3, 2025 •

Copy link
Copy Markdown
Member Author

While doing some validation, I noticed that Pulsar has a long time issue that consumers aren't able to keep with producers even when there are available resources unless dispatcherMaxReadBatchSize (and possibly also dispatcherMaxReadSizeBytes) are tuned. The default value for dispatcherMaxReadBatchSize is 100 which seems to be too small. Increasing it to 1000 could help in increasing cache hits and performance.
It's also useful to set preciseDispatcherFlowControl=true while increasing dispatcherMaxReadBatchSize so that unnecessary reads are avoided.

I created #24695 about this.

Demogorgon314 added a commit to Demogorgon314/kop that referenced this pull request Apr 15, 2026
### Motivation

apache/pulsar#24623:
Updated:
```
public static EntryImpl create(Position position, ByteBuf data)
```
to 
```
public static EntryImpl create(Position position, ByteBuf data, int expectedReadCount)
```

apache/pulsar#24427: 
Updated: 
```
CompletableFuture<Void> updateTopicPoliciesAsync(TopicName topicName, TopicPolicies policies);
```
to
```
    CompletableFuture<Void> updateTopicPoliciesAsync(TopicName topicName,
                                                     boolean isGlobalPolicy,
                                                     boolean skipUpdateWhenTopicPolicyDoesntExist,
                                                     Consumer<TopicPolicies> policyUpdater);
```

### Modifications

Fix build issue caused by upstream interface changes
lhotari added a commit that referenced this pull request Jun 12, 2026
…arMockBookKeeper

Partial cherry-pick of the testmocks changes from
490ba0c ([improve][broker] Implement PIP-430 Pulsar Broker cache
improvements (#24623)), without the JFR read event interceptor parts
which aren't needed on branch-4.0.

Required so that CompactionTest compiles after cherry-picking
ded1e42d352 (#25998), which uses
PulsarMockBookKeeper.setDefaultReadEntriesDelayMillis.
sandeep-ctds pushed a commit to datastax/pulsar that referenced this pull request Jul 31, 2026
…arMockBookKeeper

Partial cherry-pick of the testmocks changes from
490ba0c ([improve][broker] Implement PIP-430 Pulsar Broker cache
improvements (apache#24623)), without the JFR read event interceptor parts
which aren't needed on branch-4.0.

Required so that CompactionTest compiles after cherry-picking
ded1e42d352 (apache#25998), which uses
PulsarMockBookKeeper.setDefaultReadEntriesDelayMillis.
nodece pushed a commit to ascentstream/pulsar that referenced this pull request Aug 28, 2026
…arMockBookKeeper

Partial cherry-pick of the testmocks changes from
490ba0c ([improve][broker] Implement PIP-430 Pulsar Broker cache
improvements (apache#24623)), without the JFR read event interceptor parts
which aren't needed on branch-4.0.

Required so that CompactionTest compiles after cherry-picking
ded1e42d352 (apache#25998), which uses
PulsarMockBookKeeper.setDefaultReadEntriesDelayMillis.
dao-jun pushed a commit to ascentstream/pulsar that referenced this pull request Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc Your PR contains doc changes, no matter whether the changes are in markdown or code files. PIP ready-to-test type/PIP

Projects

None yet

6 participants