Denovo1998 opened a new pull request, #24739:
URL: https://github.com/apache/pulsar/pull/24739

   <!--
   ### Contribution Checklist
     
     - PR title format should be *[type][component] summary*. For details, see 
*[Guideline - Pulsar PR Naming 
Convention](https://pulsar.apache.org/contribute/develop-semantic-title/)*. 
   
     - Fill out the template below to describe the changes contributed by the 
pull request. That will give reviewers the context they need to do the review.
     
     - Each pull request should address only one issue, not mix up code from 
multiple issues.
     
     - Each commit in the pull request has a meaningful commit message
   
     - Once all items of the checklist are addressed, remove the above text and 
this checklist, leaving only the filled out template below.
   -->
   
   <!-- Either this PR fixes an issue, -->
   
   Fixes #xyz
   
   <!-- or this PR is one task of an issue -->
   
   Main Issue: #24600
   
   <!-- If the PR belongs to a PIP, please add the PIP link here -->
   
   PIP: #xyz 
   
   <!-- Details of when a PIP is required and how the PIP process work, please 
see: https://github.com/apache/pulsar/blob/master/pip/README.md -->
   
   ### Motivation
   
   The current concurrent control model of `BucketDelayedDeliveryTracker` has 
serious performance bottlenecks and thread safety issues. It mainly relies on 
coarse-grained synchronized keywords to protect its internal state, which leads 
to several key problems:
   
   1. **High lock contention**
   
   `All operations, whether simple read-only checks (like containsMessage) or 
complex I/O-intensive operations (like creating a Bucket snapshot), contend for 
the same object lock. This leads to operations being unnecessarily serialized, 
limiting overall throughput.
   
   2. **Read-write operations mutually block**
   
   - Write operations block read operations: An executing `addMessage` or 
`getScheduledMessages` will block all `containsMessage` and `nextDeliveryTime` 
and other read-only checks.
   - Read-write mixed operations block all other operations: for example, the 
`getScheduledMessages` method, which appears to be reading messages but 
actually includes multiple write operations such as modifying the queue, 
updating the bitmap, and removing mappings. When executed within a synchronized 
block, it holds the lock for a long time, blocking the addition of all new 
messages.
   
   3. I/O operations block the critical path
   Within the synchronized block of `addMessage`, the creation and persistence 
of Bucket snapshots are executed synchronously. This is a time-consuming I/O 
operation that blocks all subsequent message addition and read requests until 
the snapshot is completed, significantly increasing the message publishing 
delay.
   
   This PR is the first step in optimizing and enhancing the 
`BucketDelayedDeliveryTracker`, aiming to address the above performance 
bottlenecks by introducing more refined concurrency control mechanisms and 
asynchronizing I/O operations.
   
   ### Modifications
   
   This change is mainly focused on the `addMessage` method, because it is one 
of the most critical bottlenecks under high throughput scenarios. Subsequent 
PRs will continue to optimize other methods based on this foundation, such as 
`getScheduledMessages`.
   
   1. **Introducing fine-grained locks (`ReentrantReadWriteLock`)**:
   
   * Removed `StampedLock` and the `synchronized` keyword on multiple methods, 
unifying the use of `ReentrantReadWriteLock` as the core concurrency control 
mechanism.
   
   * This implements read-write separation:
   
       * **Read operations** (`containsMessage`, `nextDeliveryTime`) now use a 
**read lock**, allowing multiple threads to execute concurrently without 
blocking each other.
   
       * **Write operations** (`addMessage`, `asyncMergeBucketSnapshot`) use a 
**write lock**, ensuring atomicity and consistency of data modifications.
   
   * `addMessage` internally adopts an optimized pattern of "read lock check -> 
write lock modification" to reduce the holding time of the write lock.
   
   2. **Asynchronous Bucket Snapshot Creation**:
   
   * Introduced a dedicated single-threaded `ExecutorService` 
(`bucketSnapshotExecutor`) to handle time-consuming Bucket snapshot persistence 
tasks.
   
   * Refactored the snapshot creation logic in `addMessage`:
   
       * When a snapshot needs to be created, no longer wait synchronously. 
Instead, mark the current `MutableBucket` as `bucketBeingSealed`.
   
       * Immediately create a new `lastMutableBucket` to handle subsequent 
incoming messages, ensuring that `addMessage` calls can return quickly.
   
       * Submit the actual snapshot creation and persistence task to the 
background `bucketSnapshotExecutor` for asynchronous execution.
   
   * This change removes I/O operations from the critical path of message 
publishing, greatly reducing publish latency and increasing throughput.
   
   ### Verifying this change
   
   - [x] 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:
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   *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
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   - [ ] `doc` <!-- Your PR contains doc changes. -->
   - [ ] `doc-required` <!-- Your PR changes impact docs and you will update 
later -->
   - [x] `doc-not-needed` <!-- Your PR changes do not impact docs -->
   - [ ] `doc-complete` <!-- Docs have been already added -->
   
   ### Matching PR in forked repository
   
   PR in forked repository: https://github.com/Denovo1998/pulsar/pull/10
   
   <!--
   After opening this PR, the build in apache/pulsar will fail and instructions 
will
   be provided for opening a PR in the PR author's forked repository.
   
   apache/pulsar pull requests should be first tested in your own fork since 
the 
   apache/pulsar CI based on GitHub Actions has constrained resources and quota.
   GitHub Actions provides separate quota for pull requests that are executed 
in 
   a forked repository.
   
   The tests will be run in the forked repository until all PR review comments 
have
   been handled, the tests pass and the PR is approved by a reviewer.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to