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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]