Skip to content

Commit 5ea85a6

Browse files
ronfarkashnicoloboschi
authored andcommitted
[Issue apache#11351] Parallel Precise Publish Rate Limiting Fix (apache#11372)
Hello, as far as I'm concerned it is well known that precise publish rate limiting does not function well. I believe my PR fixes problem number 3 stated in the issue above. @danielsinai: "3. Rate limit function passed only to the msg/s rate limiter (and that's in order to avoid calling it twice)" It was passed to message rate limiter only due to the fact that there was no implementation of a way to throttle the connection whenever only **one of the limiters was exceeded**. This PR will allow both message rate & byte rate to co-exist, limit and enable socket reading only when necessary. - _tryAcquire_ function in **PublishRateLimiterDisable** will return true. If publish rate was null, this function would get called and return false, thus throttling the client for no reason. If the publish rate is null, it means it was not set by anyone so there's no reason to throttle any connection. ```java public boolean tryAcquire(int numbers, long bytes) { return true; } ``` - **RateLimiter** _permits_ and _acquiredPermits_ were changed to volatile. ```java private volatile long permits; private volatile long acquiredPermits; ``` in order to allow reading access from multiple threads at the same time. also the removal of _synchronized_ keyword from _getAvailablePermits()_ function. ```java public long getAvailablePermits() { return Math.max(0, this.permits - this.acquiredPermits); } ``` **This is required, since a thread dead lock will happen if not.** - Created ~a HashMap to manage the byte and message rate limiters, and~ a function _releaseThrottle()_ to handle the auto read enable. If one of the rate limiters has no available permits we will not re-enable the auto read from the socket. (cherry picked from commit 7f2ca8f) (cherry picked from commit ab5fb72)
1 parent 0f08269 commit 5ea85a6

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

pulsar-common/src/main/java/org/apache/pulsar/common/util/RateLimiter.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public class RateLimiter implements AutoCloseable{
5555
private TimeUnit timeUnit;
5656
private final boolean externalExecutor;
5757
private ScheduledFuture<?> renewTask;
58-
private long permits;
59-
private long acquiredPermits;
58+
private volatile long permits;
59+
private volatile long acquiredPermits;
6060
private boolean isClosed;
6161
// permitUpdate helps to update permit-rate at runtime
6262
private Supplier<Long> permitUpdater;
@@ -239,7 +239,7 @@ public synchronized boolean tryAcquire(long acquirePermit) {
239239
*
240240
* @return returns 0 if permits is not available
241241
*/
242-
public synchronized long getAvailablePermits() {
242+
public long getAvailablePermits() {
243243
return Math.max(0, this.permits - this.acquiredPermits);
244244
}
245245

0 commit comments

Comments
 (0)