yashmayya commented on code in PR #14847:
URL: https://github.com/apache/pinot/pull/14847#discussion_r1926939846
##########
pinot-common/src/main/java/org/apache/pinot/common/concurrency/AdjustableSemaphore.java:
##########
@@ -20,32 +20,44 @@
import com.google.common.base.Preconditions;
import java.util.concurrent.Semaphore;
+import java.util.concurrent.atomic.AtomicInteger;
/**
* A semaphore that allows adjusting the number of permits in a non-blocking
way.
*/
public class AdjustableSemaphore extends Semaphore {
- private int _totalPermits;
+ private final AtomicInteger _totalPermits;
public AdjustableSemaphore(int permits) {
super(permits);
- _totalPermits = permits;
+ _totalPermits = new AtomicInteger(permits);
}
public AdjustableSemaphore(int permits, boolean fair) {
super(permits, fair);
- _totalPermits = permits;
+ _totalPermits = new AtomicInteger(permits);
}
+ /**
+ * Sets the total number of permits to the given value without blocking.
+ */
public void setPermits(int permits) {
Preconditions.checkArgument(permits > 0, "Permits must be a positive
integer");
- if (permits < _totalPermits) {
- reducePermits(_totalPermits - permits);
- } else if (permits > _totalPermits) {
- release(permits - _totalPermits);
+ if (permits < _totalPermits.get()) {
+ reducePermits(_totalPermits.get() - permits);
+ } else if (permits > _totalPermits.get()) {
+ release(permits - _totalPermits.get());
Review Comment:
You're right, we can't actually have concurrent modifications. Only
potential concurrent reads during modifications. I think we can simply use a
volatile int instead (I guess even the volatile is not strictly necessary as we
can tolerate stale values without many issues tbh).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]