-
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] Don't call ManagedLedger#asyncAddEntry in Netty I/O thread #23983
Draft
BewareMyPower
wants to merge
10
commits into
apache:master
Choose a base branch
from
BewareMyPower:bewaremypower/async-add-entry-regression
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+108
−48
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0260324
[improve][broker] Don't call ManagedLedger#asyncAddEntry in Netty I/O…
BewareMyPower 67b31ea
Add synchronized to afterAddEntryToQueue
BewareMyPower 61dcac7
Clal buffer.release() in asyncAddEntry
BewareMyPower c0e844a
Synchronize all addEntry operations
BewareMyPower 73fb146
Fix testBrokerClosedProducerClientRecreatesProducerThenSendCommand
BewareMyPower d709574
Fix tests
BewareMyPower c4940d8
Fix memory leak of PersistentTopic#publishMessage
BewareMyPower 5f920a9
Improve API docs
BewareMyPower 01656af
Add more tests for the corner case
BewareMyPower 8106bac
Fix tests
BewareMyPower 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -798,34 +798,30 @@ public void asyncAddEntry(ByteBuf buffer, int numberOfMessages, AddEntryCallback | |
log.debug("[{}] asyncAddEntry size={} state={}", name, buffer.readableBytes(), state); | ||
} | ||
|
||
// retain buffer in this thread | ||
// The buffer will be queued in `pendingAddEntries` and might be polled later in a different thread. However, | ||
// the caller could release it after this method returns. To ensure the buffer is not released when it's polled, | ||
// increase the reference count, which should be decreased by `OpAddEntry`'s methods later. | ||
buffer.retain(); | ||
|
||
// Jump to specific thread to avoid contention from writers writing from different threads | ||
final var addOperation = OpAddEntry.createNoRetainBuffer(this, buffer, numberOfMessages, callback, ctx, | ||
currentLedgerTimeoutTriggered); | ||
var added = false; | ||
try { | ||
// Use synchronized to ensure if `addOperation` is added to queue and fails later, it will be the first | ||
// element in `pendingAddEntries`. | ||
synchronized (this) { | ||
if (managedLedgerInterceptor != null) { | ||
managedLedgerInterceptor.beforeAddEntry(addOperation, addOperation.getNumberOfMessages()); | ||
} | ||
final var state = STATE_UPDATER.get(this); | ||
beforeAddEntryToQueue(state); | ||
pendingAddEntries.add(addOperation); | ||
added = true; | ||
afterAddEntryToQueue(state, addOperation); | ||
if (managedLedgerInterceptor != null) { | ||
managedLedgerInterceptor.beforeAddEntry(addOperation, addOperation.getNumberOfMessages()); | ||
} | ||
beforeAddEntryToQueue(); | ||
pendingAddEntries.add(addOperation); | ||
added = true; | ||
afterAddEntryToQueue(addOperation); | ||
} catch (Throwable throwable) { | ||
if (!added) { | ||
addOperation.failed(ManagedLedgerException.getManagedLedgerException(throwable)); | ||
} // else: all elements of `pendingAddEntries` will fail in another thread | ||
} | ||
} | ||
|
||
protected void beforeAddEntryToQueue(State state) throws ManagedLedgerException { | ||
protected void beforeAddEntryToQueue() throws ManagedLedgerException { | ||
final var state = STATE_UPDATER.get(this); | ||
if (state.isFenced()) { | ||
throw new ManagedLedgerFencedException(); | ||
} | ||
|
@@ -836,7 +832,9 @@ protected void beforeAddEntryToQueue(State state) throws ManagedLedgerException | |
} | ||
} | ||
|
||
protected void afterAddEntryToQueue(State state, OpAddEntry addOperation) throws ManagedLedgerException { | ||
// TODO: does this method really need to be synchronized? | ||
protected synchronized void afterAddEntryToQueue(OpAddEntry addOperation) throws ManagedLedgerException { | ||
final var state = STATE_UPDATER.get(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using the STATE_UPDATER doesn't make any difference for plain reads of the field value. |
||
if (state == State.ClosingLedger || state == State.CreatingLedger) { | ||
// We don't have a ready ledger to write into | ||
// We are waiting for a new ledger to be created | ||
|
@@ -895,22 +893,6 @@ protected void afterFailedAddEntry(int numOfMessages) { | |
managedLedgerInterceptor.afterFailedAddEntry(numOfMessages); | ||
} | ||
|
||
protected boolean beforeAddEntry(OpAddEntry addOperation) { | ||
// if no interceptor, just return true to make sure addOperation will be initiate() | ||
if (managedLedgerInterceptor == null) { | ||
return true; | ||
} | ||
try { | ||
managedLedgerInterceptor.beforeAddEntry(addOperation, addOperation.getNumberOfMessages()); | ||
return true; | ||
} catch (Exception e) { | ||
addOperation.failed( | ||
new ManagedLedgerInterceptException("Interceptor managed ledger before add to bookie failed.")); | ||
log.error("[{}] Failed to intercept adding an entry to bookie.", name, e); | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public void readyToCreateNewLedger() { | ||
// only set transition state to ClosedLedger if current state is WriteFailed | ||
|
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
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
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
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
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
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
Oops, something went wrong.
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.
using the STATE_UPDATER doesn't make any difference for plain reads of the field value.