-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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] [client] Prevent reserve memory with a negative memory size to avoid send task stuck #21804
[improve] [client] Prevent reserve memory with a negative memory size to avoid send task stuck #21804
Conversation
… to avoid send task stuck
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.
Thanks.
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.
@poorbarcode I think that it would make sense to guard the release memory methods with a similar check that ensures that the value is positive.
Good suggestion. Added |
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.
LGTM
@@ -68,6 +72,15 @@ public boolean tryReserveMemory(long size) { | |||
} | |||
} | |||
|
|||
private static void ensureReservedMemoryIsPositive(long reservedMemorySize) { | |||
if (reservedMemorySize < 0) { |
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.
Nit: Zero is not positive but can pass this method. So change to reservedMemorySize <= 0
here or change the method name:)
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.
Good suggestion, skipped all logic if the argument is 0
@@ -81,6 +100,10 @@ private void checkTrigger(long prevUsage, long newUsage) { | |||
} | |||
|
|||
public void reserveMemory(long size) throws InterruptedException { | |||
ensureReservedMemoryIsPositive(size); |
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.
checkPositive
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.
Done
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.
And I think we can add checkPositive
instead of ensureReservedMemoryIsPositive
for the needed method. Error log seems redundant and makes this class ugly
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## master #21804 +/- ##
============================================
+ Coverage 73.07% 73.54% +0.47%
+ Complexity 32573 32251 -322
============================================
Files 1897 1858 -39
Lines 140622 138040 -2582
Branches 15487 15116 -371
============================================
- Hits 102754 101528 -1226
+ Misses 29783 28645 -1138
+ Partials 8085 7867 -218
Flags with carried forward coverage won't be shown. Click here to find out more.
|
… to avoid send task stuck (#21804)
… to avoid send task stuck (#21804)
… to avoid send task stuck (apache#21804) (cherry picked from commit 4806281)
… to avoid send task stuck (apache#21804) (cherry picked from commit 4806281)
Motivation
Context : The previous PR is #21790
Background of MemoryLimitController
MemoryLimitController.reserveMemory
will stuck until some memory is released[1].MemoryLimitController.releaseMemory
, it would wake up all stuck threads.Issue
tryReserveMemory
,reserveMemory
,forceReserveMemory
will also release the memory if it accepted a negative value, but it will not wake up the stuck threads. This would cause the stuck threads to remain stuck forever.Make sure that all use cases are correct
reserveMemory
ProducerImpl.canEnqueueRequest
, will not set a negative argumenttryReserveMemory
ProducerImpl.canEnqueueRequest
, will not set a negative argumentforceReserveMemory
BatchMessageContainerImpl.updateAndReserveBatchAllocatedSize
, will not set a negative argumentConsumerBase.enqueueMessageAndCheckBatchReceive
, will not set a negative argumentJust like @lhotari's suggestion, we should add a check to prevent new PRs incorrectly using
tryReserveMemory
,reserveMemory
,forceReserveMemory
Footnotes
[1]:
https://github.com/apache/pulsar/blob/master/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MemoryLimitController.java#L88
Modifications
Just like @lhotari's suggestion, add validation to prevent calling
tryReserveMemory
,reserveMemory
,forceReserveMemory
with a negative argument.Documentation
doc
doc-required
doc-not-needed
doc-coplete
Matching PR in forked repository
PR in forked repository: x