-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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] Avoid producer latency rise during internal ledger trimming operations #20649
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
Both tasks
trim ledgers
andinternalAsyncAddEntry
will try to acquire the locksynchronized(ml)
, If the tasktrim ledgers
running in the threadexecutor
, it avoids lock contention.Then if we make the task
trim ledgers
run in another thread, the publish task will also wait for the locksynchronized(ml)
. Maybe we need improve this scenario firstThere 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.
Maybe we can resolve this busy thread problem first, because the synchronized symbol only affects one ManagedLedger object, but one bk main executor thread may handle a lot of trim tasks for different ManagedLedger objects.
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.
Why not in two steps?
Thread pool isolation is necessary, trim should not run in the main thread pool
Next, let's optimize the time-consuming of trim
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.
@315157973
Now the tasks
publish
andtrim ledgers
all run in the threadexecutor
( it is a single thread pool, and the tasks of same topic will be executed in the same thread[L-1] ), right?[Q-1]if yes[Q-1], the task
trim ledgers
blocks the taskpublish
, which this PR tries to fix, right?[Q-2]if yes[Q-2], but both tasks
trim ledgers
[L-2] andinternalAsyncAddEntry
[L-3] will try to acquire the same locksynchronized(ml)
, so the tasktrim ledgers
is still blocking the taskpublish
even if this patch is merged, right?[Q-3][L-1]: https://github.com/apache/pulsar/blob/master/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java#L348
[L-2]: https://github.com/apache/pulsar/blob/master/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java#L2608
[L-3]: https://github.com/apache/pulsar/blob/master/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java#L798
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.
This lock is segmented and does not block each producer for a long time.
But if they are in the same thread pool, the thread pool will be occupied for a long time
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.
@315157973
Make sense, the task
trim ledgers
has two sub-tasks: Memory changes and Persist changes, and Memory changes would take the lock, Persist changes is an async task that will not take the lock.But the sub-task Persist changes will not use the thread
executor
, right?[Q-1]if yes[Q-1], this patch doesn't have much effect on this issue, right?[Q-2]
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.
Assuming that one trim task increase 0.01ms, if there are tens of thousands of partitions, it will increase a lot.
Especially when there are many small packets sent to the cluster, this situation will get worse
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.
@315157973
I agree with you. But I'm wondering if the current changes will solve the issue.
In the picture above, the logic of
trim ledgers
can be split into two sections: Verification(not in the lock block) and Execution(in the lock block), and there have some async tasks in Section Execution which will run in other threads. The three tasks execute as the flow below.Since there is the lock
synchronized(ml)
, we can only reduce the executor thread cost of the taskVerification
, which is very simple. It only tries to acquire the lock and is done if it fails.(Highlight) However, this change also makes the scramble of the lock
synchronized(ml)
more frequent, which can worsen performance.For example, the
topic-a
andtopic-b
were assigned the same thread:executor-1.
And the tasktrim ledgers
will be executed in the threadscheduled executor
, these tasks will be executed as flow belowexecutor-1
scheduled executor
topic-a
topic-b
topic-b
trim ledgers
oftopic-a
topic-b
topic-a
topic-b
topic-a
topic-a
andtopic-b
will be blocked util the tasktrim ledgers
oftopic-a
is completeSo in step-4, if the task
trim ledgers
oftopic-a
is executed, the performance will be better; if the tasktrim ledgers
oftopic-a
is executed, the performance will be worse. If more and more topics are assigned to the same thread, the performance will be better(maybe we can add a config to disable this feature, it is helpful for the users who have few topics).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.
Users with fewer partitions submit fewer tasks to the thread pool
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.
This change can improve the performance if there are a lot of topics in the broker and there are few threads in the BK client thread pool.
Make sense to me.