GeezerVison is showing github.com — [improve][broker] Add pulsar_subscription_storage_backlog_age_seconds metric by Technoboy- · Pull Request #26313 · apache/pulsar
Conversation
Motivation
Pulsar currently exposes pulsar_storage_backlog_age_seconds to report the age of the oldest unacknowledged message for a topic. This metric is useful for detecting consumer lag based on actual message age instead of only backlog size.
However, the metric is only available at the topic level. When a topic has multiple subscriptions, operators cannot tell which subscription is contributing to the backlog age. This is a common production case because different subscriptions on the same topic can have different latency expectations. Some subscriptions are expected to consume messages in real time, while others may intentionally retain backlog for a longer period.
Without a subscription-level backlog age metric, users have to either alert on the topic-level metric and tolerate false positives, or build complex Prometheus rules to exclude known slow subscriptions. This makes monitoring harder and less accurate.
This change adds pulsar_subscription_storage_backlog_age_seconds, allowing users to monitor backlog age per subscription and configure alerts based on each subscription's expected latency.
Modifications
Verifying this change
- Make sure that the change passes the CI checks.
(Please pick either of the following options)
This change is a trivial rework / code cleanup without any test coverage.
(or)
This change is already covered by existing tests, such as (please describe tests).
(or)
This change added tests and can be verified as follows:
(example:)
- Added integration tests for end-to-end deployment with large payloads (10MB)
- Extended integration test for recovery after broker failure
Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes
- 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.
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.
Pull request overview
This PR adds a subscription-level Prometheus metric, pulsar_subscription_storage_backlog_age_seconds, to help operators identify which subscription is driving backlog age (consumer lag) on multi-subscription topics.
Changes:
- Extend subscription stats with
oldestBacklogMessageAgeSeconds and aggregation/reset behavior.
- Compute per-subscription “best-effort oldest unacked message age” in the broker and propagate it into Prometheus subscription metrics.
- Add unit/test coverage for stats reset/aggregation and Prometheus metric emission.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File |
Description |
| pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/stats/SubscriptionStatsImplTest.java |
Adds tests for reset and aggregation behavior of the new subscription backlog-age stat. |
| pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/stats/SubscriptionStatsImpl.java |
Introduces oldestBacklogMessageAgeSeconds, resets it, and aggregates it via max. |
| pulsar-client-admin-api/src/main/java/org/apache/pulsar/common/policies/data/SubscriptionStats.java |
Adds the new getOldestBacklogMessageAgeSeconds() accessor to the subscription stats API. |
| pulsar-broker/src/test/java/org/apache/pulsar/broker/stats/prometheus/NamespaceStatsAggregatorTest.java |
Verifies the new pulsar_subscription_storage_backlog_age_seconds metric is emitted. |
| pulsar-broker/src/main/java/org/apache/pulsar/broker/stats/prometheus/TopicStats.java |
Emits the new subscription-level backlog-age metric in Prometheus output. |
| pulsar-broker/src/main/java/org/apache/pulsar/broker/stats/prometheus/NamespaceStatsAggregator.java |
Maps subscription stats’ oldestBacklogMessageAgeSeconds into aggregated subscription metric state. |
| pulsar-broker/src/main/java/org/apache/pulsar/broker/stats/prometheus/AggregatedSubscriptionStats.java |
Adds storage for per-subscription backlog age used by Prometheus output. |
| pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java |
Adds per-subscription cached oldest-position tracking and an overload to read per-subscription backlog age. |
| pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java |
Plumbs per-subscription backlog age into SubscriptionStatsImpl. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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 working on this — per-subscription backlog age is genuinely useful, and the cache design mirrors the topic-level one closely.
My main request is a dedicated configuration key, defaulting to false, that gates the computation, not just the metric emission (details inline at PersistentTopic.java:4127). Beyond that I found several correctness issues that I think need fixing regardless of the flag — the most significant being that non-durable (Reader) subscriptions report a wrong value in two easily-reachable topologies.
Why the flag
updateSubscriptionOldPositionInfos() is called unconditionally, so every persistent topic with any backlog pays a full walk of every cursor every backlogQuotaCheckIntervalInSeconds (default 60) on the single pulsar-backlog-quota-checker thread — including when no backlog quota is configured at all.
To be precise about what is new: the pass already began with hasBacklogs(), which is a short-circuiting anyMatch over subscriptions, and then did one estimate — or one asyncReadEntry for the single oldest cursor — for the topic. What this PR adds is a full, non-short-circuiting walk of the whole ManagedCursorContainer, with a hasBacklog() call for every cursor rather than up to the first backlogged one, plus an estimate or read per backlogged cursor whose mark-delete moved.
Meanwhile the metric is already gated. Emission goes through TopicStats.printTopicStats, which NamespaceStatsAggregator only reaches when exposeTopicLevelMetricsInPrometheus is true. Operators who turn topic-level metrics off — typically the large clusters — would pay the whole cost and never scrape the metric. (The cache also backs an ungated admin-API field, SubscriptionStats.oldestBacklogMessageAgeSeconds, so a default-false flag would blank that to -1 as well. I think that is the right trade, but it should be a conscious one.) The only existing knob is backlogQuotaCheckEnabled=false, and it only stops the periodic checker — producer-creation checks still trigger the same walk.
PIP-323 justified the topic-level metric as "cheap to retrieve (no additional I/O involved)" precisely because it piggybacks on the oldest-cursor calculation the checker already had to do, and its Alternatives section rejected a costlier variant because it "might result in more frequent I/O calls, especially with many topics". This change breaks that property.
There is good precedent for default-off on expensive per-entity stats: exposePreciseBacklogInPrometheus, exposeSubscriptionBacklogSizeInPrometheus, exposeConsumerLevelMetricsInPrometheus and exposeManagedCursorMetricsInPrometheus are all false by default.
In fairness to the design, the reusable-position check on line 4023 blunts the steady state: an idle, abandoned backlogged subscription is a cache hit forever after the first pass, which is the main case this metric exists for, so it costs no repeat read. And reads go through InflightReadsLimiter, so they are not unbounded — though they now contend with consumer reads for that shared permit budget.
Minor
Map<String, Boolean> activeSubscriptionNames at PersistentTopic.java:4010 could be a Set<String>.
- The
catch (Exception e) at line 4044 swallows InterruptedException without restoring the interrupt flag.
- The PR description still has the template Modifications and Verifying this change sections unfilled, and the public API / metrics boxes unchecked — this does add a public admin-api method and a new metric, so a docs PR against
apache/pulsar-site will be needed.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Thanks for working on this — per-subscription backlog age is genuinely useful, and the cache design mirrors the topic-level one closely.
My main request is a dedicated configuration key, defaulting to false, that gates the computation, not just the metric emission (details inline at PersistentTopic.java:4127). Beyond that I found several correctness issues that I think need fixing regardless of the flag — the most significant being that non-durable (Reader) subscriptions report a wrong value in two easily-reachable topologies.
Why the flag
updateSubscriptionOldPositionInfos() is called unconditionally, so every persistent topic with any backlog pays a full walk of every cursor every backlogQuotaCheckIntervalInSeconds (default 60) on the single pulsar-backlog-quota-checker thread — including when no backlog quota is configured at all.
To be precise about what is new: the pass already began with hasBacklogs(), which is a short-circuiting anyMatch over subscriptions, and then did one estimate — or one asyncReadEntry for the single oldest cursor — for the topic. What this PR adds is a full, non-short-circuiting walk of the whole ManagedCursorContainer, with a hasBacklog() call for every cursor rather than up to the first backlogged one, plus an estimate or read per backlogged cursor whose mark-delete moved.
Meanwhile the metric is already gated. Emission goes through TopicStats.printTopicStats, which NamespaceStatsAggregator only reaches when exposeTopicLevelMetricsInPrometheus is true. Operators who turn topic-level metrics off — typically the large clusters — would pay the whole cost and never scrape the metric. (The cache also backs an ungated admin-API field, SubscriptionStats.oldestBacklogMessageAgeSeconds, so a default-false flag would blank that to -1 as well. I think that is the right trade, but it should be a conscious one.) The only existing knob is backlogQuotaCheckEnabled=false, and it only stops the periodic checker — producer-creation checks still trigger the same walk.
PIP-323 justified the topic-level metric as "cheap to retrieve (no additional I/O involved)" precisely because it piggybacks on the oldest-cursor calculation the checker already had to do, and its Alternatives section rejected a costlier variant because it "might result in more frequent I/O calls, especially with many topics". This change breaks that property.
There is good precedent for default-off on expensive per-entity stats: exposePreciseBacklogInPrometheus, exposeSubscriptionBacklogSizeInPrometheus, exposeConsumerLevelMetricsInPrometheus and exposeManagedCursorMetricsInPrometheus are all false by default.
In fairness to the design, the reusable-position check on line 4023 blunts the steady state: an idle, abandoned backlogged subscription is a cache hit forever after the first pass, which is the main case this metric exists for, so it costs no repeat read. And reads go through InflightReadsLimiter, so they are not unbounded — though they now contend with consumer reads for that shared permit budget.
Minor
Map<String, Boolean> activeSubscriptionNames at PersistentTopic.java:4010 could be a Set<String>.
- The
catch (Exception e) at line 4044 swallows InterruptedException without restoring the interrupt flag.
- The PR description still has the template Modifications and Verifying this change sections unfilled, and the public API / metrics boxes unchecked — this does add a public admin-api method and a new metric, so a docs PR against
apache/pulsar-site will be needed.
please help review again
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks for the update. For this metric, caching by subscription and mark-delete position makes sense, especially for inactive subscriptions with long-lived backlog.
I have three remaining suggestions:
-
Move subscription backlog-age refresh out of updateOldPositionInfo().
updateOldPositionInfo() is part of the backlog quota path, and can also be reached during producer creation through message-age backlog quota checks. With this change enabled, those paths may wait for per-subscription reads, even though subscription backlog age is only a best-effort metric.
It would be cleaner to keep updateOldPositionInfo() focused on the existing topic-level oldest-position cache used by quota logic, and refresh the subscription-level cache from a separate background task. Prometheus/admin stats can then read the cache only.
-
Bound the background refresh with a simple broker-level concurrency limiter.
This metric may need to read one entry per subscription when the cached mark-delete position changes. On a broker with many topics or many subscriptions, refreshing everything at once could create a burst of BookKeeper/offload reads and compete with normal consumer reads.
A simple broker-level limiter should be enough. If no permit is available, skip that subscription and retry in a later refresh. Since this is a best-effort metric, skipping is better than queueing unbounded work or adding read pressure.
-
Re-check state before writing async read results.
Async reads can complete after the subscription state has already changed. For example, a subscription may catch up, be deleted, or be recreated while the read is still in flight. In that case, the old result should not be written back into the cache.
Before updating the cache, the callback should verify that the subscription still exists, the cursor is the same one, the mark-delete position is unchanged, and the subscription still has backlog. Otherwise, drop the result.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Thanks for the update. For this metric, caching by subscription and mark-delete position makes sense, especially for inactive subscriptions with long-lived backlog.
I have three remaining suggestions:
- Move subscription backlog-age refresh out of
updateOldPositionInfo().
updateOldPositionInfo() is part of the backlog quota path, and can also be reached during producer creation through message-age backlog quota checks. With this change enabled, those paths may wait for per-subscription reads, even though subscription backlog age is only a best-effort metric.
It would be cleaner to keep updateOldPositionInfo() focused on the existing topic-level oldest-position cache used by quota logic, and refresh the subscription-level cache from a separate background task. Prometheus/admin stats can then read the cache only.
- Bound the background refresh with a simple broker-level concurrency limiter.
This metric may need to read one entry per subscription when the cached mark-delete position changes. On a broker with many topics or many subscriptions, refreshing everything at once could create a burst of BookKeeper/offload reads and compete with normal consumer reads.
A simple broker-level limiter should be enough. If no permit is available, skip that subscription and retry in a later refresh. Since this is a best-effort metric, skipping is better than queueing unbounded work or adding read pressure.
- Re-check state before writing async read results.
Async reads can complete after the subscription state has already changed. For example, a subscription may catch up, be deleted, or be recreated while the read is still in flight. In that case, the old result should not be written back into the cache.
Before updating the cache, the callback should verify that the subscription still exists, the cursor is the same one, the mark-delete position is unchanged, and the subscription still has backlog. Otherwise, drop the result.
thanks for the good suggestions, addressed 1 and 3 for this patch.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
It may be worth adding top-level exception handling around refreshSubscriptionBacklogAge().join(). Since scheduleAtFixedRateNonConcurrently does not wrap the task with SafeRunnable, an unexpected unchecked exception could prevent subsequent executions from being requeued. Adding a guard here would make the periodic refresh more resilient.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
It may be worth adding top-level exception handling around refreshSubscriptionBacklogAge().join(). Since scheduleAtFixedRateNonConcurrently does not wrap the task with SafeRunnable, an unexpected unchecked exception could prevent subsequent executions from being requeued. Adding a guard here would make the periodic refresh more resilient.
thanks 🌸
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
nodece
pushed a commit
to ascentstream/pulsar
that referenced
this pull request
Aug 28, 2026
dao-jun
added a commit
to ascentstream/pulsar
that referenced
this pull request
Oct 5, 2026
Surgical backport of the MockedPulsarServiceBaseTest change from apache#25680:
deleteNamespaceWithRetry relied on Awaitility's default 10-second timeout,
so a starved-out cleanup cascaded into dozens of 'admin is null' failures
under CI load (e.g. TopicEventsListenerTest 24-failure cascades).
The BacklogQuotaManagerTest portion of apache#25680 is not carried over: this
branch already has the richer createNamespaceForTest handling from apache#26313.
(cherry picked from commit 02d12a6)
Partial pick: only the MockedPulsarServiceBaseTest line.
nodece
pushed a commit
to ascentstream/pulsar
that referenced
this pull request
Oct 13, 2026
Surgical backport of the MockedPulsarServiceBaseTest change from apache#25680:
deleteNamespaceWithRetry relied on Awaitility's default 10-second timeout,
so a starved-out cleanup cascaded into dozens of 'admin is null' failures
under CI load (e.g. TopicEventsListenerTest 24-failure cascades).
The BacklogQuotaManagerTest portion of apache#25680 is not carried over: this
branch already has the richer createNamespaceForTest handling from apache#26313.
(cherry picked from commit 02d12a6)
Partial pick: only the MockedPulsarServiceBaseTest line.
nodece
pushed a commit
to ascentstream/pulsar
that referenced
this pull request
Nov 16, 2026
Reviewers
coderzc
coderzc approved these changes
lhotari
lhotari left review comments
Uh oh!
There was an error while loading. Please reload this page.
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|