-
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
[Broker] Fix possible data race in getFirstAvailableConsumerPermits #10831
[Broker] Fix possible data race in getFirstAvailableConsumerPermits #10831
Conversation
- there's a chance for a race so that the consumer's available permits goes below zero after it has been checked in "isConsumerAvailable"
@lhotari - in order to call |
if (isConsumerAvailable(consumer)) { | ||
return consumer.getAvailablePermits(); | ||
int availablePermits = consumer.getAvailablePermits(); | ||
if (availablePermits > 0) { | ||
return availablePermits; | ||
} | ||
} |
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.
Is better to change to:
if (consumer != null && !consumer.isBlocked()) {
return consumer.getAvailablePermits();
}
So that we don't need to check availablePermits
twice
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.
@codelipenghui Done. PTAL
In this part of code I believe that there's a goal of zero allocations since this is part of a hot code path. Therefore, omitting the usage of Optional is also desired. |
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
…pache#10831) * Fix possible race in getFirstAvailableConsumerPermits - there's a chance for a race so that the consumer's available permits goes below zero after it has been checked in "isConsumerAvailable" * Address review feedback (cherry picked from commit bd568bc)
…pache#10831) * Fix possible race in getFirstAvailableConsumerPermits - there's a chance for a race so that the consumer's available permits goes below zero after it has been checked in "isConsumerAvailable" * Address review feedback
This PR fixes the issues introduce by #10417, so don't need to cherry-pick to branch-2.7 |
…pache#10831) * Fix possible race in getFirstAvailableConsumerPermits - there's a chance for a race so that the consumer's available permits goes below zero after it has been checked in "isConsumerAvailable" * Address review feedback
Motivation
below zero after it has been checked in
isConsumerAvailable
Modifications
availablePermits > 0
before returning