GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [improve][broker] Optimize consumer selection for shared subscriptions by lhotari · Pull Request #26593 · apache/pulsar

Conversation

@lhotari

@lhotari lhotari commented Sep 15, 2026

Copy link
Copy Markdown
Member

Motivation

Shared subscriptions perform priority checks during consumer selection even when every consumer has the same priority. With mixed priorities, round-robin wraparound can also rescan higher-priority consumers to locate the current priority group.

Reducing this work lowers selection overhead while preserving priority ordering and round-robin behavior.

Modifications

  • Extract selection logic into ConsumerPrioritySelector, allowing isolated unit tests and JMH benchmarks.
  • Track consumer counts by priority using Int2ObjectMap<MutableInt>. Remove entries when their counts reach zero and skip priority checks when only one priority remains.
  • Reuse priority-group boundaries and eliminate redundant list reads during mixed-priority selection.
  • Synchronize selection and membership updates on the dispatcher monitor. Preserve cleanup of inconsistent consumer list/set state by rebuilding counts during bulk-removal recovery.
  • Keep the refactoring, benchmark, and optimization in separate commits for reproducible comparisons.

Verifying this change

  • Make sure that the change passes the CI checks.

Added tests cover:

  • Selection and availability-check order across 262,144 priority-group, availability-mask, and cursor combinations.
  • Three complete round-robin cycles with 50 consumers, followed by exhaustion and recovery, at zero and nonzero priority.
  • Membership changes, priority-count transitions, equal consumer identities, and bulk-removal recovery.

Local validation passed:

  • ConsumerPrioritySelectorTest
  • The four existing persistent dispatcher priority tests, with quarantine exclusions cleared and retries disabled
  • Selected dispatcher add/remove and cleanup tests
  • ./gradlew quickCheck
  • ./gradlew :microbench:shadowJar

JMH comparison

Compared baseline 2d540fa0853 with optimization 9c88617c944 using identical benchmark sources and settings across 26 parameter combinations.

Environment: Apple M3 Max, macOS arm64, Corretto 25.0.4, JMH 1.37; two forks, three 500-ms warmup iterations, five 500-ms measurement iterations, one thread, and the GC profiler. Builds and tests did not overlap measurements.

Values below are mean nanoseconds per selection. Ranges cover priority offsets zero and three; speedups compare matching cases.

Consumers Priority levels Availability Baseline ns/op Optimized ns/op Speedup
50 1 All ready 3.13–3.96 2.82–2.84 1.10–1.40×
50 1 Sparse 17.36–21.28 8.52–8.94 2.04–2.38×
50 1 All blocked 90.27–91.28 46.20–49.91 1.81–1.98×
16 4 All blocked 43.08–44.12 20.89–21.03 2.05–2.11×
256 4 All blocked 609.33–614.69 292.88–296.06 2.06–2.10×

No GC occurred, and measured allocation was approximately zero. The 256-consumer mixed-priority FLOW case at offset zero had a wide confidence interval and remains inconclusive.

These measurements isolate selection cost and exclude dispatcher locking, membership updates, and message processing. Linux x86_64 confirmation is needed before extrapolating to broker throughput.

Does this pull request potentially affect one of the following parts:

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

Selection and membership helpers explicitly synchronize on the existing dispatcher monitor so the consumer list and priority counts remain consistent. Existing dispatch call sites already hold that monitor.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
lhotari added 3 commits September 15, 2026 15:35
@lhotari
[refactor][broker] Extract shared consumer priority selection
04edd0c
Preserve selection and availability-check order while isolating list traversal for benchmarking. Keep the dispatcher cursor and lifecycle checks in place.

Assisted-by: Codex
@lhotari
[test][broker] Benchmark shared consumer priority selection with JMH
2d540fa
Measure ready, sparse, lower-priority, blocked and changing-permit consumers with persistent round-robin state. This revision is the unoptimized baseline.

Assisted-by: Codex
@lhotari
[improve][broker] Skip priority checks for consumers at one level
9c88617
Track MutableInt priority counts with synchronized membership updates, removing entries when their count reaches zero. Use availability-only round-robin traversal for one priority and reuse group boundaries for mixed priorities. Preserve the bulk-removal repair path for inconsistent dispatcher membership.

Validate with JMH comparisons against the benchmark-only parent, including 50 consumers at one priority, selection-order and membership tests, dispatcher recovery tests, explicitly enabled priority tests, and quickCheck.

Assisted-by: Codex
@lhotari
lhotari requested review from Technoboy-, dao-jun, david-streamlio, merlimat and nodece September 15, 2026 13:49
merlimat
merlimat approved these changes Sep 15, 2026
View reviewed changes
@merlimat
merlimat merged commit e7cbb33 into apache:master Sep 15, 2026
44 checks passed

Uh oh!

There was an error while loading. Please reload this page.

dao-jun pushed a commit to ascentstream/pulsar that referenced this pull request Sep 20, 2026
@lhotari @dao-jun
[improve][broker] Optimize consumer selection for shared subscriptions (
5260fdf
apache#26593)

(cherry picked from commit e7cbb33)
dao-jun pushed a commit to ascentstream/pulsar that referenced this pull request Sep 20, 2026
@lhotari @dao-jun
[improve][broker] Optimize consumer selection for shared subscriptions (
3e14e34
apache#26593)

(cherry picked from commit e7cbb33)
dao-jun pushed a commit to ascentstream/pulsar that referenced this pull request Sep 20, 2026
@lhotari @dao-jun
[improve][broker] Optimize consumer selection for shared subscriptions (
c9f5b7f
apache#26593)

(cherry picked from commit e7cbb33)
@lhotari lhotari added this to the 5.0.0 milestone Oct 1, 2026
@dao-jun dao-jun mentioned this pull request Oct 4, 2026
[feat][ml] Port batch read and managed-ledger cache improvements (PIP-430) on BK 4.17.4.1 ascentstream/pulsar#105
Closed
Radiancebobo pushed a commit to Radiancebobo/pulsar that referenced this pull request Oct 8, 2026
@lhotari hechengbo
[improve][broker] Optimize consumer selection for shared subscriptions (
842f4c6
apache#26593)
@nodece nodece mentioned this pull request Oct 13, 2026
[feat][ml] Port batch read and managed-ledger cache improvements (PIP-430) on BK 4.17.4.1 (ml-only subset) ascentstream/pulsar#106
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@merlimat merlimat merlimat approved these changes
@nodece nodece Awaiting requested review from nodece
@dao-jun dao-jun Awaiting requested review from dao-jun
@Technoboy- Technoboy- Awaiting requested review from Technoboy-
@david-streamlio david-streamlio Awaiting requested review from david-streamlio

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

5.0.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@lhotari @merlimat
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.