fhan688 commented on code in PR #20189:
URL: https://github.com/apache/hudi/pull/20189#discussion_r4229322363
##########
hudi-common/src/main/java/org/apache/hudi/common/util/RateLimiter.java:
##########
@@ -68,27 +94,67 @@ public boolean tryAcquire(int numPermits) {
}
public boolean acquire(int numOps) {
+ ValidationUtils.checkArgument(numOps > 0 && numOps <= maxPermits,
+ "Number of permits must be between one and the configured maximum");
+ return acquireInternal(numOps);
+ }
+
+ public boolean acquire() {
+ return acquireInternal(1);
+ }
+
+ private boolean acquireInternal(int numOps) {
try {
- while (!semaphore.tryAcquire(numOps)) {
+ while (!stopped.get() && !semaphore.tryAcquire(numOps)) {
Thread.sleep(WAIT_BEFORE_NEXT_ACQUIRE_PERMIT_IN_MS);
}
+ ValidationUtils.checkState(!stopped.get(), "Rate limiter is stopped");
log.debug("acquire permits: {}, maxPermits: {}", numOps, maxPermits);
} catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
throw new RuntimeException("Unable to acquire permits", e);
}
return true;
}
- public void stop() {
- scheduler.shutdownNow();
+ public synchronized void stop() {
+ if (stopped.compareAndSet(false, true)) {
Review Comment:
Thanks for the suggestion. I’ve simplified the synchronization by replacing
`AtomicBoolean` with a `volatile boolean`, keeping `stop()` and scheduler
initialization synchronized, and removing the unnecessary volatile modifier and
local snapshot for the scheduler. This preserves the required visibility while
making the lifecycle logic clearer.
--
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]