You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "lhotari (via GitHub)" <gi...@apache.org> on 2026/09/09 08:59:17 UTC
[PR] [fix][ml] Fix active cursor retention and deferred-update synchronization [pulsar]
lhotari opened a new pull request, #26511:
URL: https://github.com/apache/pulsar/pull/26511
Replacement for #26506.
### Motivation
`ActiveManagedCursorContainerImpl` defers cursor ordering until a position query needs it. With `cacheEvictionByExpectedReadCount` enabled, cursor creation and deletion can continue without such queries. Removed nodes then accumulate in `pendingPositionUpdates` or remain in the ordered list and `pendingRemovedCursors`, retaining their cursor objects and associated object graphs.
This PR replaces the approach in #26506 with immediate cursor-reference release and bounded, batched node compaction. The ten-event flush in #26506 counts cursors removed before their first position flush; already-tracked removals are outside that cleanup trigger. Preserving lazy position updates is important to the efficiency of this data structure.
This also fixes a pre-existing synchronization bug: `getSlowestCursorPosition()` held a **read lock** while calling `processPendingPositions()`, which mutates the priority queue, linked list, and counters. Concurrent readers could therefore flush the same state simultaneously. The internal state-checking method had the same locking problem.
### Modifications
- Clear the cursor reference immediately on removal. Make it volatile for the existing lock-free iterator, skip cleared references during iteration, and install the current cursor instance when reusing a node.
- Compact removed nodes after `max(64, live cursor count)` removals, or immediately when the container becomes empty. Prune the queue and unlink removed list nodes in linear time, repairing shared counters in one pass. Compaction preserves pending position updates and does not sort or flush them.
- Track queue membership separately from `pendingPosition`, so cancellation and reactivation cannot enqueue the same node repeatedly.
- Use a validated optimistic read for clean slowest-position lookups. Fall back to the write lock when pending changes need flushing or concurrent mutation invalidates the snapshot. The state-checking method also uses the write lock when flushing.
- Add deterministic retention, compaction, reactivation, iterator, and slowest-position tests. Extend `ActiveManagedCursorContainerBenchmark` with cursor-churn and read-only slowest-position workloads.
### Verifying this change
- [ ] Make sure that the change passes the CI checks.
Local validation passed:
```shell
./gradlew :managed-ledger:test \
--tests ActiveManagedCursorContainerTest \
--tests ActiveManagedCursorContainerRetentionTest \
-PtestRetryCount=0 -PtestFailFast=false
./gradlew quickCheck
./gradlew :microbench:shadowJar
```
The two test classes run 27 cases. The 11 retention regression cases fail on the unpatched implementation with only read-only testing accessors added. Coverage includes removal before and after position tracking, bounded retention without queries, preserving deferred updates and shared counters during compaction, repeated reactivation, replacement cursor identity, iterator behavior, and cached reads completing while another reader holds the lock.
Local JMH comparisons used macOS arm64 and Corretto 21.0.11, with two forks, two 1-second warmup iterations and three 1-second measurement iterations per fork. These are short development measurements; Linux x86_64 validation is still needed for production performance conclusions.
The churn workload repeatedly replaces one cursor with a distinct name and instance while the other cursors remain active, without ordering queries. Both variants use the same benchmark workload, a 256 MiB heap, and the GC profiler. The comparison uses #26506 at `bbf855c72c33ab5f2568146df9e0cd1ad8f99492`.
| Active cursors | #26506 throughput (M ops/s) | This PR (M ops/s) | Change |
|---:|---:|---:|---:|
| 10 | 13.49 | 15.63 | +15.8% |
| 100 | 15.41 | 16.65 | +8.0% |
| 500 | 15.00 | 16.27 | +8.5% |
The existing seek benchmarks were also compared with unmodified master at 10, 100, and 500 cursors, with no ordering queries and with one query per 100 updates. Single-thread throughput varied from -1.5% to +4.5%; ten-thread measurements were noisier, from -6.4% to +18.1%.
### 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
- [x] The threading model — enforce exclusive deferred-state mutation and allow validated optimistic reads when no changes are pending.
- [ ] The binary protocol
- [ ] The REST endpoints
- [ ] The admin CLI options
- [ ] The metrics
- [ ] Anything that affects deployment
--
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-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [fix][ml] Fix active cursor retention and deferred-update synchronization [pulsar]
Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari merged PR #26511:
URL: https://github.com/apache/pulsar/pull/26511
--
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-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org