You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "BewareMyPower (via GitHub)" <gi...@apache.org> on 2025/02/06 12:57:41 UTC
[PR] [improve][ml] Do not switch thread to execute asyncAddEntry's core logic [pulsar]
BewareMyPower opened a new pull request, #23940:
URL: https://github.com/apache/pulsar/pull/23940
### Motivation
In protocol handler's implementation, sometimes it needs to record the current base offset before the actual `asyncAddEntry` call (or the wrapped `PersistentTopic#publishMessages` call), for example:
```java
private final PersistentTopic persistentTopic;
private final Set<Long> pendingBaseOffsets = ConcurrentHashMap.newKeySet();
private synchronized void add(ByteBuf buffer, int batchSize) {
final var ml = (ManagedLedgerImpl) persistentTopic.getManagedLedger();
final var interceptor = (ManagedLedgerInterceptorImpl) ml.getManagedLedgerInterceptor();
final var baseOffset = interceptor.getIndex(); // the base offset of the next batch to write
pendingBaseOffsets.add(baseOffset);
ml.asyncAddEntry(buffer, batchSize, new AsyncCallbacks.AddEntryCallback() {
@Override
public void addComplete(Position position, ByteBuf entryData, Object ctx) {
if (!pendingBaseOffsets.remove(baseOffset)) {
log.error("Failed to remove {}", baseOffset);
}
```
However, the existing implementation won't work as expected that even if the downstream protocol handler guarantees that `asyncAddEntry` is called in a single thread. See https://github.com/apache/pulsar/blob/a19eaa2b972ab8fe1a2a45e65df1566cf28fb336/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java#L804-L809
First, the code above does not make sense because `internalAsyncAddEntry` itself is already synchronized.
Second, to guarantee thread safety for concurrent add operations, switching to another thread is not more efficient than synchronizing the method call. Mostly, it's less efficient because `execute` involves with at least two method calls (`offer` and `poll`) on a `BlockingQueue`, as well as some other operations (like CAS). Take `ArrayBlockingQueue` as example, both its `offer` and `poll` implementations need to acquire the internal lock.
Third, switching to another thread to execute the core logic is anti-intuitive, especially for modifying some fields that represent the current states. For example, to achieve the same goal at the beginning, I have to write the code like:
```java
ml.getExecutor().execute(() -> {
pendingBaseOffsets.add(baseOffset);
ml.asyncAddEntry(buffer, batchSize, callback, ctx);
});
```
### Modifications
Split `internalAsyncAddEntry` to two methods `beforeAddEntryToQueue` and `afterAddEntryToQueue` that are called before and after adding the operation to `pendingAddEntries`. Then simplify the code by throwing an exception and pass the exception to the callback. It's also more efficient because `OpAddEntry#failed` won't be called in the synchronized block.
Add `testBeforeAddEntry` to protect the change.
### 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: <!-- ENTER URL HERE -->
<!--
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-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [improve][ml] Do not switch thread to execute asyncAddEntry's core logic [pulsar]
Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on PR #23940:
URL: https://github.com/apache/pulsar/pull/23940#issuecomment-2655792456
@BewareMyPower Please also remove the comment "Jump to specific thread to avoid contention from writers writing from different threads" that is now obsolete: https://github.com/apache/pulsar/blob/cc7b3816ce14a2ddff18dbf04216769fdb8751db/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java#L804
--
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] [improve][ml] Do not switch thread to execute asyncAddEntry's core logic [pulsar]
Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on PR #23940:
URL: https://github.com/apache/pulsar/pull/23940#issuecomment-2642000795
@dao-jun I found this issue when I implemented the Kafka transaction. In Kafka, `analyzeAndValidateProducerState` is called before writing messages, this method will update a map that maps the next message's offset (key) to an object and it will remove the key after the record batch is written.
For example, assuming there are 3 ongoing writes, the flow looks like the following pseudo code:
```java
for (int i = 0; i < 3; i++) {
long nextOffset = interceptor.getIndex() + 1; // LEO
ongoingTxns.put(nextOffset, txn); // record the ongoing txn of this record batch
asyncAddEntry().thenAccept(__ -> ongoingTxns.remove(nextOffset));
}
```
Assuming each record batch has only 1 message, ideally, after these 3 writes, `ongoingTxns` will have 3 keys (0, 1, 2). However, since `asyncAddEntry` switches thread to the ML's executor to call interceptor's `beforeAddEntry` method, there is a chance that `nextOffset` is always 0 in these 3 loops and `ongoingTxns.remove(nextOffset)` will take effect only once. Then it could hit [here](https://github.com/streamnative/kop/blob/14f9832d8a437c5b717e3c74fd7e4cda9810aff7/kafka-impl/src/main/java/io/streamnative/pulsar/handlers/kop/storage/ProducerStateManager.java#L312) in the actual code.
The details above are beyond the scope of this PR but could help you understand the motivation.
--
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] [improve][ml] Do not switch thread to execute asyncAddEntry's core logic [pulsar]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #23940:
URL: https://github.com/apache/pulsar/pull/23940#issuecomment-2644734861
## [Codecov](https://app.codecov.io/gh/apache/pulsar/pull/23940?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
Attention: Patch coverage is `84.00000%` with `4 lines` in your changes missing coverage. Please review.
> Project coverage is 74.19%. Comparing base [(`bbc6224`)](https://app.codecov.io/gh/apache/pulsar/commit/bbc62245c5ddba1de4b1e7cee4ab49334bc36277?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) to head [(`4f28483`)](https://app.codecov.io/gh/apache/pulsar/commit/4f284835bea9ffbae0c757fc11a832699b72834c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
> Report is 885 commits behind head on master.
| [Files with missing lines](https://app.codecov.io/gh/apache/pulsar/pull/23940?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
|---|---|---|
| [...okkeeper/mledger/impl/ShadowManagedLedgerImpl.java](https://app.codecov.io/gh/apache/pulsar/pull/23940?src=pr&el=tree&filepath=managed-ledger%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fbookkeeper%2Fmledger%2Fimpl%2FShadowManagedLedgerImpl.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-bWFuYWdlZC1sZWRnZXIvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL2Jvb2trZWVwZXIvbWxlZGdlci9pbXBsL1NoYWRvd01hbmFnZWRMZWRnZXJJbXBsLmphdmE=) | 25.00% | [3 Missing :warning: ](https://app.codecov.io/gh/apache/pulsar/pull/23940?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...che/bookkeeper/mledger/impl/ManagedLedgerImpl.java](https://app.codecov.io/gh/apache/pulsar/pull/23940?src=pr&el=tree&filepath=managed-ledger%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fbookkeeper%2Fmledger%2Fimpl%2FManagedLedgerImpl.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-bWFuYWdlZC1sZWRnZXIvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL2Jvb2trZWVwZXIvbWxlZGdlci9pbXBsL01hbmFnZWRMZWRnZXJJbXBsLmphdmE=) | 95.23% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/pulsar/pull/23940?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
<details><summary>Additional details and impacted files</summary>
[](https://app.codecov.io/gh/apache/pulsar/pull/23940?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
```diff
@@ Coverage Diff @@
## master #23940 +/- ##
============================================
+ Coverage 73.57% 74.19% +0.61%
+ Complexity 32624 32236 -388
============================================
Files 1877 1853 -24
Lines 139502 143739 +4237
Branches 15299 16332 +1033
============================================
+ Hits 102638 106643 +4005
+ Misses 28908 28692 -216
- Partials 7956 8404 +448
```
| [Flag](https://app.codecov.io/gh/apache/pulsar/pull/23940/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [inttests](https://app.codecov.io/gh/apache/pulsar/pull/23940/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `26.71% <52.00%> (+2.13%)` | :arrow_up: |
| [systests](https://app.codecov.io/gh/apache/pulsar/pull/23940/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `23.20% <44.00%> (-1.12%)` | :arrow_down: |
| [unittests](https://app.codecov.io/gh/apache/pulsar/pull/23940/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `73.71% <84.00%> (+0.86%)` | :arrow_up: |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
| [Files with missing lines](https://app.codecov.io/gh/apache/pulsar/pull/23940?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [...che/bookkeeper/mledger/impl/ManagedLedgerImpl.java](https://app.codecov.io/gh/apache/pulsar/pull/23940?src=pr&el=tree&filepath=managed-ledger%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fbookkeeper%2Fmledger%2Fimpl%2FManagedLedgerImpl.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-bWFuYWdlZC1sZWRnZXIvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL2Jvb2trZWVwZXIvbWxlZGdlci9pbXBsL01hbmFnZWRMZWRnZXJJbXBsLmphdmE=) | `81.87% <95.23%> (+1.21%)` | :arrow_up: |
| [...okkeeper/mledger/impl/ShadowManagedLedgerImpl.java](https://app.codecov.io/gh/apache/pulsar/pull/23940?src=pr&el=tree&filepath=managed-ledger%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fbookkeeper%2Fmledger%2Fimpl%2FShadowManagedLedgerImpl.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-bWFuYWdlZC1sZWRnZXIvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL2Jvb2trZWVwZXIvbWxlZGdlci9pbXBsL1NoYWRvd01hbmFnZWRMZWRnZXJJbXBsLmphdmE=) | `59.70% <25.00%> (+4.04%)` | :arrow_up: |
... and [1032 files with indirect coverage changes](https://app.codecov.io/gh/apache/pulsar/pull/23940/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
</details>
--
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] [improve][ml] Do not switch thread to execute asyncAddEntry's core logic [pulsar]
Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower merged PR #23940:
URL: https://github.com/apache/pulsar/pull/23940
--
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] [improve][ml] Do not switch thread to execute asyncAddEntry's core logic [pulsar]
Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on PR #23940:
URL: https://github.com/apache/pulsar/pull/23940#issuecomment-2655795476
@BewareMyPower This logic doesn't either make sense any more after the changes:
https://github.com/apache/pulsar/blob/cc7b3816ce14a2ddff18dbf04216769fdb8751db/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java#L801-L806
--
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] [improve][ml] Do not switch thread to execute asyncAddEntry's core logic [pulsar]
Posted by "dao-jun (via GitHub)" <gi...@apache.org>.
dao-jun commented on PR #23940:
URL: https://github.com/apache/pulsar/pull/23940#issuecomment-2643481585
> @dao-jun I found this issue when I implemented the Kafka transaction. In Kafka, `analyzeAndValidateProducerState` is called before writing messages, this method will update a map that maps the next message's offset (key) to an object and it will remove the key after the record batch is written.
>
> For example, assuming there are 3 ongoing writes, the flow looks like the following pseudo code:
>
> ```java
> for (int i = 0; i < 3; i++) {
> long nextOffset = interceptor.getIndex() + 1; // LEO
> ongoingTxns.put(nextOffset, txn); // record the ongoing txn of this record batch
> asyncAddEntry().thenAccept(__ -> ongoingTxns.remove(nextOffset));
> }
> ```
>
> Assuming each record batch has only 1 message, ideally, after these 3 writes, `ongoingTxns` will have 3 keys (0, 1, 2). However, since `asyncAddEntry` switches thread to the ML's executor to call interceptor's `beforeAddEntry` method, there is a chance that `nextOffset` is always 0 in these 3 loops and `ongoingTxns.remove(nextOffset)` will take effect only once. Then it could hit [here](https://github.com/streamnative/kop/blob/14f9832d8a437c5b717e3c74fd7e4cda9810aff7/kafka-impl/src/main/java/io/streamnative/pulsar/handlers/kop/storage/ProducerStateManager.java#L312) in the actual code, which is much different from the OSS KoP.
>
> The details above are beyond the scope of this PR but could help you understand the motivation.
Thanks for your explain!
--
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] [improve][ml] Do not switch thread to execute asyncAddEntry's core logic [pulsar]
Posted by "dao-jun (via GitHub)" <gi...@apache.org>.
dao-jun commented on PR #23940:
URL: https://github.com/apache/pulsar/pull/23940#issuecomment-2641977951
> In protocol handler's implementation, sometimes it needs to record the current base offset before the actual asyncAddEntry call (or the wrapped PersistentTopic#publishMessages call)
I don't understand this part, could you pls explain more clear?
--
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] [improve][ml] Do not switch thread to execute asyncAddEntry's core logic [pulsar]
Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on PR #23940:
URL: https://github.com/apache/pulsar/pull/23940#issuecomment-2644549430
Some shadow topic related tests failed, I will fix them
--
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] [improve][ml] Do not switch thread to execute asyncAddEntry's core logic [pulsar]
Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on PR #23940:
URL: https://github.com/apache/pulsar/pull/23940#issuecomment-2655810763
@BewareMyPower There's a high chance that this change causes performance regressions:
https://github.com/apache/pulsar/blob/cc7b3816ce14a2ddff18dbf04216769fdb8751db/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java#L809-L811
In Pulsar use cases, this could possibly happen when there's a large number of producers producing to a topic.
Blocking the IO threads with synchronization will have a larger impact since it will impact Netty IO of all connections sharing the same IO thread.
--
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