[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.
Is better to change to:
if (consumer != null && !consumer.isBlocked()) {
return consumer.getAvailablePermits();
}
So that we don't need to check availablePermits twice
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. |
…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
isConsumerAvailableModifications
availablePermits > 0before returning