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] Skip draining hash collection for binary consumer stats by void-ptr974 · Pull Request #27751 · apache/pulsar

Conversation

@void-ptr974

@void-ptr974 void-ptr974 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Motivation

The binary CONSUMER_STATS response needs only scalar fields. Calling Consumer#getStats() to build it can traverse draining hashes and allocate a list of per-hash details for the full admin stats.

Modifications

  • Populate the binary response from the consumer's cached rates and connection metadata, current flow-control counters, and the existing subscription stats.

Verifying this change

  • Make sure that the change passes the CI checks.

  • Added ServerCnxTest.testConsumerStatsResponseSkipsDrainingHashes to check the serialized response and verify that only the full stats path invokes the draining-hash updater.

  • Ran the broker quickCheck task, both ServerCnxTest consumer-stats tests, and ConsumerStatsTest.testKeySharedDrainingHashesConsumerStats.

  • In a single-threaded JMH benchmark on JDK 21, with 1,024 tracked draining hashes, building and serializing each response allocated about 0.14 KiB, down from about 41 KiB.

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

Sorry, something went wrong.

Uh oh!

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

All reactions
@void-ptr974
[improve][broker] Skip draining hash collection for binary consumer s…
96c74f4
…tats

Assisted-by: OpenAI Codex (GPT-6)
lhotari
lhotari reviewed Nov 26, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks for the focused change and for including the allocation numbers. They make the motivation easy to follow.

I compared every field of the new CONSUMER_STATS response with the old getStats()-based code. They match. requestId is already set by Commands.newConsumerStatsResponseCommand. Permits, unacked messages and the blocked flag are read from the live fields. Consumer name, address and connectedSince come from the constructor, and the rates come from updateRates(). The only side effect the change drops is the refresh of the private stats object, and every other reader goes through getStats() anyway. As a bonus, the IO thread no longer writes to the shared stats object.

I have two small, non-blocking points: one about test strength, and one about follow-up scope for the same allocation on the periodic stats paths.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pulsar-broker/src/test/java/org/apache/pulsar/broker/service/ServerCnxTest.java
initialStats.blockedConsumerOnUnackedMsgs = true;
consumer.updateStats(initialStats);
consumer.updateRates();
ConsumerStatsImpl consumerStats = consumer.getStats();

@lhotari lhotari Nov 26, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

[QUALITY] The test cannot catch a regression that reads cached flow-control fields

consumer.getStats() here runs after updateStats(initialStats), so it copies availablePermits=10, unackedMessages=7 and blockedConsumerOnUnackedMsgs=true into the shared stats object (Consumer.java:1233-1235) before the CONSUMER_STATS request is sent. If populateStatsResponse were later changed to read stats.availablePermits (or the other two cached fields) instead of the live values, this test would still pass. In production, a flow command after the last full stats read would then leave the response stale.

Suggestion: after taking consumerStats, change the live counters again, for example with a second updateStats(...) using different permits, unacked count and blocked flag, and assert that the response carries the new values. Alternatively, take the getStats() sample before updateStats(initialStats).

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java
return stats;
}

void populateStatsResponse(CommandConsumerStatsResponse response) {

@lhotari lhotari Nov 26, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

[QUALITY] Follow-up: the periodic stats paths still build the per-hash list they discard

Not for this PR, but worth a follow-up issue. The same allocation the description measures still happens on broker paths that run much more often than a binary CONSUMER_STATS request:

  • PersistentTopic.java:3211: on every stats interval (statsUpdateFrequencyInSecs, default 60s) the loop calls consumer.getStats() for every consumer. It then uses only scalar rates and StreamingStats.writeConsumerStats, which never reads the draining-hash fields.
  • NamespaceStatsAggregator.java:245 → topic.getStats(...) → PersistentSubscription.java:1641 calls consumer.getStats() for every consumer on every Prometheus scrape. The Prometheus code does not read the draining-hash fields. NamespaceStatsAggregator.java:313 calls it a second time when exposeConsumerLevelMetricsInPrometheus is enabled.

For a Key_Shared consumer with many draining hashes, each of these calls runs DrainingHashesTracker.updateConsumerStats (DrainingHashesTracker.java:194-228). That method takes the read lock and allocates an ArrayList plus one DrainingHashImpl per hash. A scalar-only accessor next to populateStatsResponse, together with a counts-only variant of the tracker update for the subscription aggregation that needs only the three totals, would remove most of the remaining cost.

Sorry, something went wrong.

Uh oh!

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

All reactions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lhotari lhotari lhotari left review comments

At least 1 approving review is required to merge this pull request.

Uh oh!

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

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

2 participants

@void-ptr974 @lhotari
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.