Skip to content

[improve][broker] Reduce persistent topic backlog check overhead with ManagedCursor.hasBacklog#26058

Open
void-ptr974 wants to merge 1 commit into
apache:masterfrom
void-ptr974:issue-26036-managed-cursor-has-backlog
Open

[improve][broker] Reduce persistent topic backlog check overhead with ManagedCursor.hasBacklog#26058
void-ptr974 wants to merge 1 commit into
apache:masterfrom
void-ptr974:issue-26036-managed-cursor-has-backlog

Conversation

@void-ptr974

@void-ptr974 void-ptr974 commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Motivation

Fixes #26036.

Some persistent topic paths only need to know whether backlog exists, but currently call
getNumberOfEntriesInBacklog(true), which computes an exact backlog count. This can add unnecessary
overhead for yes/no decisions such as migration cleanup, replicator backlog checks, and subscription
backlog checks.

Modifications

  • Add backward-compatible hasBacklog methods to ManagedCursor, Subscription, and Replicator.
  • Implement ManagedCursorImpl.hasBacklog() to short-circuit once it finds one readable backlog entry.
  • Preserve correctness for individually deleted messages by skipping deleted ranges.
  • Replace exact backlog counts with hasBacklog in persistent topic, subscription, and replicator paths.
  • Add managed-ledger tests that compare hasBacklog() / hasBacklog(true) with the original
    getNumberOfEntriesInBacklog(true) > 0 semantics across empty, caught-up, mixed ack, cross-ledger,
    batch-index, reset, recovery, deleted-range-to-tail, negative-counter fallback, and uninitialized
    mark-delete states.
  • Add broker tests for default method fallback, subscription delegation, replicator usage, and migration cleanup.

Performance impact

This reduces persistent topic backlog check overhead by avoiding exact backlog counting when callers only
need a boolean result. In the common case, the check can stop at the first valid backlog entry.

Verifications

  • :managed-ledger:test --tests 'org.apache.bookkeeper.mledger.impl.ManagedCursorTest.hasBacklog*'
  • :pulsar-broker:test targeted PersistentTopicTest and PersistentSubscriptionTest backlog tests
  • :pulsar-broker:checkstyleTest
  • :managed-ledger:checkstyleTest
  • git diff --check

@void-ptr974 void-ptr974 force-pushed the issue-26036-managed-cursor-has-backlog branch 5 times, most recently from 093665c to da0836b Compare June 18, 2026 14:05
… ManagedCursor.hasBacklog

Motivation:
Issue apache#26036 points out that several broker paths only need to know whether a cursor has any backlog, but they currently call getNumberOfEntriesInBacklog(true), which computes an exact backlog count. For boolean decisions in persistent topic paths, this can add unnecessary overhead.

Modifications:
- Add backward-compatible hasBacklog methods to ManagedCursor, Subscription, and Replicator.
- Implement an optimized precise ManagedCursorImpl.hasBacklog() that skips individually deleted ranges and stops as soon as it finds one readable backlog entry.
- Use hasBacklog in persistent subscription, replicator, topic migration, and backlog checks instead of computing precise counts for yes/no decisions.
- Add managed-ledger tests that compare hasBacklog with the original getNumberOfEntriesInBacklog count semantics across empty, caught-up, mixed ack, cross-ledger, batch-index, reset, recovery, deleted-range-to-tail, negative-counter fallback, and uninitialized mark-delete states.
- Add broker tests for subscription delegation, replicator usage, and migration cleanup behavior.

Performance impact:
This adds a fast existence check for persistent topic backlog paths, avoiding exact backlog counting when callers only need a boolean result.

Fixes apache#26036
@void-ptr974 void-ptr974 force-pushed the issue-26036-managed-cursor-has-backlog branch from da0836b to d203c4e Compare June 18, 2026 14:23
@void-ptr974 void-ptr974 marked this pull request as ready for review June 18, 2026 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement] Optimize code that uses cursor.getNumberOfEntriesInBacklog(true) > 0

1 participant