Package com.linkedin.venice.throttle
Class TokenBucket
- java.lang.Object
-
- com.linkedin.venice.throttle.TokenBucket
-
- All Implemented Interfaces:
VeniceRateLimiter
public class TokenBucket extends java.lang.Object implements VeniceRateLimiter
- See Also:
- Token Bucket on Wikipedia This implementation aims to be very high performance with the goal of supporting a very large number of TokenBuckets in an application; thus avoiding an auxilliary thread to refill the bucket.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.linkedin.venice.throttle.VeniceRateLimiter
VeniceRateLimiter.RateLimiterType
-
-
Constructor Summary
Constructors Constructor Description TokenBucket(long capacity, long refillAmount, long refillInterval, java.util.concurrent.TimeUnit refillUnit, java.time.Clock clock)
This constructor should only be used by tests.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description float
getAmortizedRefillPerSecond()
long
getCapacity()
long
getEnforcementInterval()
long
getQuota()
long
getRefillAmount()
long
getStaleTokenCount()
This method does not call #update(), so it is only accurate as of the last time #tryConsume() was calleddouble
getStaleUsageRatio()
void
setQuota(long quota)
The following methods are used only when checking if the new quota requests are different from the existing quota.static TokenBucket
tokenBucketFromRcuPerSecond(long totalRcuPerSecond, double thisBucketProportionOfTotalRcu, long enforcementIntervalMilliseconds, int enforcementCapacityMultiple, java.time.Clock clock)
java.lang.String
toString()
boolean
tryAcquirePermit(int units)
Try to acquire permit for the given rcu.boolean
tryConsume(long tokensToConsume)
-
-
-
Constructor Detail
-
TokenBucket
public TokenBucket(long capacity, long refillAmount, long refillInterval, java.util.concurrent.TimeUnit refillUnit, java.time.Clock clock)
This constructor should only be used by tests. Application should not specify it's own instance of Clock- Parameters:
capacity
-refillAmount
-refillInterval
-refillUnit
-clock
-
-
-
Method Detail
-
getStaleTokenCount
public long getStaleTokenCount()
This method does not call #update(), so it is only accurate as of the last time #tryConsume() was called- Returns:
- number of tokens remaining in the bucket
-
tryConsume
public boolean tryConsume(long tokensToConsume)
-
getAmortizedRefillPerSecond
public float getAmortizedRefillPerSecond()
-
getStaleUsageRatio
public double getStaleUsageRatio()
-
tryAcquirePermit
public boolean tryAcquirePermit(int units)
Description copied from interface:VeniceRateLimiter
Try to acquire permit for the given rcu. Will not block if permit is not available.- Specified by:
tryAcquirePermit
in interfaceVeniceRateLimiter
- Parameters:
units
- Number of units to acquire.- Returns:
- true if permit is acquired, false otherwise.
-
tokenBucketFromRcuPerSecond
public static TokenBucket tokenBucketFromRcuPerSecond(long totalRcuPerSecond, double thisBucketProportionOfTotalRcu, long enforcementIntervalMilliseconds, int enforcementCapacityMultiple, java.time.Clock clock)
- Parameters:
totalRcuPerSecond
- Number of units per second to allowthisBucketProportionOfTotalRcu
- For maximum fidelity of calculations. If you need a smaller portion of the RCU to be applied then set this to an appropriate multiplier. Otherwise set this to 1.- Returns:
- See Also:
for an explanation of TokenBucket capacity, refill amount, and refill interval This method takes a rate in units per second, and scales it according to the desired refill interval in milliseconds, and the multiple of refill we wish to use for capacity. Since the resulting token bucket must have a whole number of tokens in its refill amount and capacity, if we only accept one parameter in this method (localRcuPerSecond) then we might have suboptimal results given integer division. For example, with a global rate of 1 Rcu per second, and a proportion of 0.1, along with an enforcement interval of 10 seconds (10000 milliseconds), we would be forced to pass 1 localRcuPerSecond, which is 10x the desired quota. By passing both the global rate of 1 Rcu per second and a desired proportion, this method can multiply by the enforcement interval first before shrinking by the proportion, allowing the Bucket to be configured with the correct 1 refill every 10 seconds.
-
setQuota
public void setQuota(long quota)
Description copied from interface:VeniceRateLimiter
The following methods are used only when checking if the new quota requests are different from the existing quota.- Specified by:
setQuota
in interfaceVeniceRateLimiter
-
getQuota
public long getQuota()
- Specified by:
getQuota
in interfaceVeniceRateLimiter
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
getCapacity
public long getCapacity()
-
getRefillAmount
public long getRefillAmount()
-
getEnforcementInterval
public long getEnforcementInterval()
-
-