Class RateLimiter

java.lang.Object
org.apache.hadoop.hbase.quotas.RateLimiter
Direct Known Subclasses:
AverageIntervalRateLimiter, FixedIntervalRateLimiter

@Private @Evolving public abstract class RateLimiter extends Object
Simple rate limiter. Usage Example: // At this point you have a unlimited resource limiter RateLimiter limiter = new AverageIntervalRateLimiter(); // or new FixedIntervalRateLimiter(); limiter.set(10, TimeUnit.SECONDS); // set 10 resources/sec while (limiter.getWaitIntervalMs > 0) { // wait until waitInterval == 0 Thread.sleep(limiter.getWaitIntervalMs()); } // ...execute the work and consume the resource... limiter.consume();
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private long
     
    static final long
     
    private long
     
    static final String
     
    private long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    consume one available unit.
    void
    consume(long amount)
    consume amount available units, amount could be a negative number
    long
     
    long
     
    abstract long
     
    protected long
     
    (package private) abstract long
    getWaitInterval(long limit, long available, long amount)
    Time in milliseconds to wait for before requesting to consume 'amount' resource.
    long
    Is there at least one resource available to allow execution?
    long
    getWaitIntervalMs(long amount)
    Are there enough available resources to allow execution?
    protected boolean
    isAvailable(long amount)
    Are there enough available resources to allow execution?
    boolean
     
    (package private) abstract long
    refill(long limit)
    Refill the available units w.r.t the elapsed time.
    void
    set(long limit, TimeUnit timeUnit)
    Set the RateLimiter max available resources and refill period.
    abstract void
    setNextRefillTime(long nextRefillTime)
     
     
    void
    Sets the current instance of RateLimiter to a new values.
    long
    Returns estimate of the ms required to wait before being able to provide 1 resource.
    long
    waitInterval(long amount)
    Returns estimate of the ms required to wait before being able to provide "amount" resources.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • refill

      abstract long refill(long limit)
      Refill the available units w.r.t the elapsed time.
      Parameters:
      limit - Maximum available resource units that can be refilled to.
      Returns:
      how many resource units may be refilled ?
    • getWaitInterval

      abstract long getWaitInterval(long limit, long available, long amount)
      Time in milliseconds to wait for before requesting to consume 'amount' resource.
      Parameters:
      limit - Maximum available resource units that can be refilled to.
      available - Currently available resource units
      amount - Resources for which time interval to calculate for
      Returns:
      estimate of the ms required to wait before being able to provide 'amount' resources.
    • set

      public void set(long limit, TimeUnit timeUnit)
      Set the RateLimiter max available resources and refill period.
      Parameters:
      limit - The max value available resource units can be refilled to.
      timeUnit - Timeunit factor for translating to ms.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • update

      public void update(RateLimiter other)
      Sets the current instance of RateLimiter to a new values. if current limit is smaller than the new limit, bump up the available resources. Otherwise allow clients to use up the previously available resources.
    • isBypass

      public boolean isBypass()
    • getLimit

      public long getLimit()
    • getAvailable

      public long getAvailable()
    • getTimeUnitInMillis

      protected long getTimeUnitInMillis()
    • getWaitIntervalMs

      public long getWaitIntervalMs()
      Is there at least one resource available to allow execution?
      Returns:
      the waitInterval to backoff, or 0 if execution is allowed
    • getWaitIntervalMs

      public long getWaitIntervalMs(long amount)
      Are there enough available resources to allow execution?
      Parameters:
      amount - the number of required resources, a non-negative number
      Returns:
      the waitInterval to backoff, or 0 if execution is allowed
    • isAvailable

      protected boolean isAvailable(long amount)
      Are there enough available resources to allow execution?
      Parameters:
      amount - the number of required resources, a non-negative number
      Returns:
      true if there are enough available resources, otherwise false
    • consume

      public void consume()
      consume one available unit.
    • consume

      public void consume(long amount)
      consume amount available units, amount could be a negative number
      Parameters:
      amount - the number of units to consume
    • waitInterval

      public long waitInterval()
      Returns estimate of the ms required to wait before being able to provide 1 resource.
    • waitInterval

      public long waitInterval(long amount)
      Returns estimate of the ms required to wait before being able to provide "amount" resources.
    • setNextRefillTime

      public abstract void setNextRefillTime(long nextRefillTime)
    • getNextRefillTime

      public abstract long getNextRefillTime()