Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve][broker] Optimize PersistentTopic.getLastDispatchablePosition #22707

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dao-jun
Copy link
Member

@dao-jun dao-jun commented May 14, 2024

Motivation

PersistentTopic#getLastDispatchablePosition is using by
Reader#hasMessageAvailable , ConsumerImpl#hasMessageAvailable, Consumer#getLastMessageIdAsync.

The current implementation is read entries from Bookkeeper(or sth else), which leads to low throughput, high latency and heavy load, this PR is for the purpose of optimization.

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

Documentation

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

Matching PR in forked repository

PR in forked repository:

@dao-jun dao-jun added area/broker ready-to-test category/performance Performance issues fix or improvements labels May 14, 2024
@dao-jun dao-jun self-assigned this May 14, 2024
@github-actions github-actions bot added the doc-not-needed Your PR changes do not impact docs label May 14, 2024
@dao-jun dao-jun marked this pull request as draft May 14, 2024 07:04
@dao-jun dao-jun marked this pull request as ready for review May 14, 2024 08:43
@dao-jun dao-jun added this to the 3.4.0 milestone May 14, 2024
@dao-jun dao-jun closed this May 14, 2024
@dao-jun dao-jun reopened this May 14, 2024
Copy link
Member

@shibd shibd left a comment

Choose a reason for hiding this comment

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

Nice job! LGTM

@dao-jun dao-jun changed the title [improve][broker] Optimize getLastDispatchablePosition [improve][broker] Optimize PersistentTopic.getLastDispatchablePosition May 15, 2024
@dao-jun
Copy link
Member Author

dao-jun commented May 16, 2024

@coderzc PTAL

@@ -307,6 +307,7 @@ public CompletableFuture<Void> commitTxn(TxnID txnID, long lowWaterMark) {
txnBuffer.commitAt(committedAtLedgerId, committedAtEntryId);
addTxnToTxnIdex(txnID, committedAtLedgerId);
}
updateLastDispatchablePosition(null);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you update this position to null?

Copy link
Member Author

@dao-jun dao-jun Jun 3, 2024

Choose a reason for hiding this comment

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

please see the changes carefully. @liangyepianzhou

Copy link
Contributor

Choose a reason for hiding this comment

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

I just promote a question here, right? There is no point in me arguing with you just to waste my time. What we do together in our spare time is make Pulsar better.

Copy link
Contributor

Choose a reason for hiding this comment

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

You record a maximum stable position that can be used for consumption, but you set this position to null when a transaction is committed. This behavior puzzles me.
Because transaction committed will not change the previously recorded stable consumable position into a non-consumable position.

Copy link
Contributor

Choose a reason for hiding this comment

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

Especially for InMemTransactionBuffer.java.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. More understand achieved.

Copy link
Contributor

Choose a reason for hiding this comment

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

About why set lastDispatchablePosition to null when commit txn, it is because of it's hard to determine which position to set, so, set to null to make it fallback to search the position from BK.

This is a good way for cache lastDispatchablePosition. There are some ways to do that.

  1. your way
  2. When committing the transaction, keep the previous position because it is still the correct position. Then start a task to update, or mark it as needing to be updated from BK. Then the next time it is read, return to the previous position that is somewhat lagging but still valid. Then read from BK.

Copy link
Contributor

Choose a reason for hiding this comment

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

Another point is that the transaction in InMemTransactionBuffer does not effect the lastDispatchablePosition.

Copy link
Member Author

Choose a reason for hiding this comment

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

Another point is that the transaction in InMemTransactionBuffer does not effect the lastDispatchablePosition.

It is because of I don't want to introduce Separated solution to InMemoryTxnBuffer, just make it consistent with TopicTxnBuffer, let it easier to maintain.

Copy link
Contributor

Choose a reason for hiding this comment

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

You should do nothing here.

if (!isMarkerMessage && maxReadPositionCallBack != null) {
maxReadPositionCallBack.maxReadPositionMovedForward(null, position);
if (!isMarkerMessage) {
updateLastDispatchablePosition(position);
Copy link
Contributor

Choose a reason for hiding this comment

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

This write approach is strange. Please follow the previous way.

Copy link
Member Author

Choose a reason for hiding this comment

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

About this point,

       if (!isMarkerMessage) {
            updateLastDispatchablePosition(position);
            if (maxReadPositionCallBack != null) {
                maxReadPositionCallBack.maxReadPositionMovedForward(null, position);
            }
        }

I don't understand why this write approach is strange, could you please explain why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants