From becdeba10cc43fc7a136e5a3007dad079d23b367 Mon Sep 17 00:00:00 2001 From: guanziyue Date: Sun, 8 May 2022 23:32:39 +0800 Subject: [PATCH] [HUDI-4055]refactor ratelimiter to avoid stack overflow --- .../apache/hudi/common/util/RateLimiter.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/hudi-common/src/main/java/org/apache/hudi/common/util/RateLimiter.java b/hudi-common/src/main/java/org/apache/hudi/common/util/RateLimiter.java index e156ccffdbb9..4915e454af21 100644 --- a/hudi-common/src/main/java/org/apache/hudi/common/util/RateLimiter.java +++ b/hudi-common/src/main/java/org/apache/hudi/common/util/RateLimiter.java @@ -53,19 +53,22 @@ private RateLimiter(int permits, TimeUnit timePeriod) { } public boolean tryAcquire(int numPermits) { - if (numPermits > maxPermits) { - acquire(maxPermits); - return tryAcquire(numPermits - maxPermits); - } else { - return acquire(numPermits); + int remainingPermits = numPermits; + while (remainingPermits > 0) { + if (remainingPermits > maxPermits) { + acquire(maxPermits); + remainingPermits -= maxPermits; + } else { + return acquire(remainingPermits); + } } + return true; } public boolean acquire(int numOps) { try { - if (!semaphore.tryAcquire(numOps)) { + while (!semaphore.tryAcquire(numOps)) { Thread.sleep(WAIT_BEFORE_NEXT_ACQUIRE_PERMIT_IN_MS); - return acquire(numOps); } LOG.debug(String.format("acquire permits: %s, maxPremits: %s", numOps, maxPermits)); } catch (InterruptedException e) {