Class ThreadDrivenTokenPool

java.lang.Object
io.nosqlbench.engine.api.activityapi.ratelimits.ThreadDrivenTokenPool
All Implemented Interfaces:
TokenPool

public class ThreadDrivenTokenPool
extends java.lang.Object
implements TokenPool

Synopsis

This TokenPool represents a finite quantity which can be replenished with regular refills. Extra tokens that do not fit within the active token pool are saved in a waiting token pool and used to backfill when allowed according to the backfill rate. A detailed explanation for how this works will be included at @link "http://docs.nosqlbench.io/" under dev notes.

This is the basis for the token-based rate limiters in NB. This mechanism is easily adaptable to bursting capability as well as a degree of stricter timing at speed. Various methods for doing this in a lock free way were investigated, but the intrinsic locks provided by synchronized method won out for now. This may be revisited when EB is retrofitted for J11.

  • Field Summary

    Fields 
    Modifier and Type Field Description
    static double MIN_CONCURRENT_OPS  
  • Constructor Summary

    Constructors 
    Constructor Description
    ThreadDrivenTokenPool​(RateSpec rateSpec, io.nosqlbench.engine.api.activityimpl.ActivityDef activityDef)
    This constructor tries to pick reasonable defaults for the token pool for a given rate spec.
  • Method Summary

    Modifier and Type Method Description
    TokenPool apply​(RateSpec rateSpec)
    Change the settings of this token pool, and wake any blocked callers just in case it allows them to proceed.
    long blockAndTake()
    wait for the given number of tokens to be available, and then remove them from the pool.
    long blockAndTake​(long tokens)  
    long getActivePool()  
    double getBurstRatio()  
    RateSpec getRateSpec()  
    long getWaitPool()  
    long getWaitTime()  
    long refill​(long newTokens)
    Add the given number of new tokens to the pool, forcing any amount that would spill over the current pool size into the wait token pool, but moving up to the configured burst tokens back from the wait token pool otherwise.
    long restart()  
    void start()  
    long takeUpTo​(long amt)
    Take tokens up to amt tokens form the pool and report the amount of token removed.
    java.lang.String toString()  

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • ThreadDrivenTokenPool

      public ThreadDrivenTokenPool​(RateSpec rateSpec, io.nosqlbench.engine.api.activityimpl.ActivityDef activityDef)
      This constructor tries to pick reasonable defaults for the token pool for a given rate spec. The active pool must be large enough to contain one op worth of time, and the burst ratio
      Parameters:
      rateSpec - a RateSpec
  • Method Details

    • apply

      public TokenPool apply​(RateSpec rateSpec)
      Change the settings of this token pool, and wake any blocked callers just in case it allows them to proceed.
      Specified by:
      apply in interface TokenPool
      Parameters:
      rateSpec - The rate specifier.
    • getBurstRatio

      public double getBurstRatio()
      Specified by:
      getBurstRatio in interface TokenPool
    • takeUpTo

      public long takeUpTo​(long amt)
      Take tokens up to amt tokens form the pool and report the amount of token removed.
      Specified by:
      takeUpTo in interface TokenPool
      Parameters:
      amt - tokens requested
      Returns:
      actual number of tokens removed, greater to or equal to zero
    • blockAndTake

      public long blockAndTake()
      wait for the given number of tokens to be available, and then remove them from the pool.
      Specified by:
      blockAndTake in interface TokenPool
      Returns:
      the total number of tokens untaken, including wait tokens
    • blockAndTake

      public long blockAndTake​(long tokens)
      Specified by:
      blockAndTake in interface TokenPool
    • getWaitTime

      public long getWaitTime()
      Specified by:
      getWaitTime in interface TokenPool
    • getWaitPool

      public long getWaitPool()
      Specified by:
      getWaitPool in interface TokenPool
    • getActivePool

      public long getActivePool()
      Specified by:
      getActivePool in interface TokenPool
    • refill

      public long refill​(long newTokens)
      Add the given number of new tokens to the pool, forcing any amount that would spill over the current pool size into the wait token pool, but moving up to the configured burst tokens back from the wait token pool otherwise. The amount of backfilling that occurs is controlled by the backfill ratio, based on the number of tokens submitted. This causes normalizes the backfilling rate to the fill rate, so that it is not sensitive to refill scheduling.
      Parameters:
      newTokens - The number of new tokens to add to the token pools
      Returns:
      the total number of tokens in all pools
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object
    • getRateSpec

      public RateSpec getRateSpec()
      Specified by:
      getRateSpec in interface TokenPool
    • restart

      public long restart()
      Specified by:
      restart in interface TokenPool
    • start

      public void start()
      Specified by:
      start in interface TokenPool