Skip to content
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

Merged

Conversation

poorbarcode
Copy link
Contributor

@poorbarcode poorbarcode commented Dec 26, 2023

Motivation

Context : The previous PR is #21790

Background of MemoryLimitController

  • When there is not enough memory to use, the method MemoryLimitController.reserveMemory will stuck until some memory is released[1].
  • After calling MemoryLimitController.releaseMemory, it would wake up all stuck threads.

Issue

Make sure that all use cases are correct

  • reserveMemory
    • used by ProducerImpl.canEnqueueRequest, will not set a negative argument
  • tryReserveMemory
    • used by ProducerImpl.canEnqueueRequest, will not set a negative argument
  • forceReserveMemory
    • used by BatchMessageContainerImpl.updateAndReserveBatchAllocatedSize, will not set a negative argument
    • used by ConsumerBase.enqueueMessageAndCheckBatchReceive, will not set a negative argument

Just 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

if (!tryReserveMemory(size)) {
    while (!tryReserveMemory(size)) {
        condition.await();
    }
}

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

@poorbarcode poorbarcode self-assigned this Dec 26, 2023
@poorbarcode poorbarcode added this to the 3.3.0 milestone Dec 26, 2023
@github-actions github-actions bot added the doc-not-needed Your PR changes do not impact docs label Dec 26, 2023
@poorbarcode poorbarcode added release/3.0.3 release/3.1.3 and removed doc-not-needed Your PR changes do not impact docs labels Dec 26, 2023
@github-actions github-actions bot added the doc-not-needed Your PR changes do not impact docs label Dec 26, 2023
Copy link
Member

@wenbingshen wenbingshen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

Copy link
Member

@lhotari lhotari left a 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.

@lhotari lhotari modified the milestones: 3.3.0, 3.2.0 Dec 26, 2023
@poorbarcode
Copy link
Contributor Author

@lhotari

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

Copy link
Member

@lhotari lhotari left a 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) {
Copy link
Contributor

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:)

Copy link
Contributor Author

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkPositive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@Technoboy- Technoboy- left a 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-commenter
Copy link

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (4bfc786) 73.07% compared to head (8544705) 73.54%.
Report is 18 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@             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     
Flag Coverage Δ
inttests 24.17% <47.36%> (-0.10%) ⬇️
systests 23.71% <63.15%> (-1.01%) ⬇️
unittests 72.84% <89.47%> (+0.51%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...ache/pulsar/client/impl/MemoryLimitController.java 93.75% <89.47%> (-1.81%) ⬇️

... and 181 files with indirect coverage changes

@lhotari lhotari merged commit 1d83b84 into apache:master Dec 26, 2023
50 checks passed
Technoboy- pushed a commit that referenced this pull request Jan 16, 2024
Technoboy- pushed a commit that referenced this pull request Jan 16, 2024
mukesh-ctds pushed a commit to datastax/pulsar that referenced this pull request Mar 1, 2024
mukesh-ctds pushed a commit to datastax/pulsar that referenced this pull request Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants