Class EventThrottler

  • All Implemented Interfaces:
    VeniceRateLimiter

    public class EventThrottler
    extends java.lang.Object
    implements VeniceRateLimiter
    A class to throttle Events to a certain rate This class takes a maximum rate in events/sec and a minimum interval over which to check the rate. The rate is measured over two rolling windows: one full window, and one in-flight window. Each window is bounded to the provided interval in ms, therefore, the total interval measured over is up to twice the provided interval parameter. If the current event rate exceeds the maximum, the call to maybeThrottle(double) will handle this case based on the given throttling strategy. This is a generalized IoThrottler as it existed before, which can be used to throttle Bytes read or written, number of entries scanned, etc.
    • Constructor Detail

      • EventThrottler

        public EventThrottler​(long maxRatePerSecond)
        Parameters:
        maxRatePerSecond - Maximum rate that this throttler should allow (-1 is unlimited)
      • EventThrottler

        public EventThrottler​(long maxRatePerSecond,
                              java.lang.String throttlerName,
                              boolean checkQuotaBeforeRecording,
                              EventThrottlingStrategy throttlingStrategy)
        Parameters:
        maxRatePerSecond - Maximum rate that this throttler should allow (-1 is unlimited)
        throttlerName - if specified, the throttler will share its limit with others named the same if null, the throttler will be independent of the others
        throttlingStrategy - the strategy how throttler handle the quota exceeding case.
        checkQuotaBeforeRecording - if true throttler will check the quota before recording usage, otherwise throttler will record usage then check the quota.
      • EventThrottler

        public EventThrottler​(long maxRatePerSecond,
                              long intervalMs,
                              java.lang.String throttlerName,
                              boolean checkQuotaBeforeRecording,
                              EventThrottlingStrategy throttlingStrategy)
        Parameters:
        maxRatePerSecond - Maximum rate that this throttler should allow (-1 is unlimited)
        intervalMs - Minimum interval over which the rate is measured (maximum is twice that)
        throttlerName - if specified, the throttler will share its limit with others named the same if null, the throttler will be independent of the others
        checkQuotaBeforeRecording - if true throttler will check the quota before recording usage, otherwise throttler will record usage then check the quota.
        throttlingStrategy - the strategy how throttler handle the quota exceeding case.
      • EventThrottler

        public EventThrottler​(io.tehuti.utils.Time time,
                              long maxRatePerSecond,
                              long intervalMs,
                              java.lang.String throttlerName,
                              boolean checkQuotaBeforeRecording,
                              EventThrottlingStrategy throttlingStrategy)
        Parameters:
        time - Used to inject a Time in tests
        maxRatePerSecond - Maximum rate that this throttler should allow (-1 is unlimited)
        intervalMs - Minimum interval over which the rate is measured (maximum is twice that)
        throttlerName - if specified, the throttler will share its limit with others named the same if null, the throttler will be independent of the others
        checkQuotaBeforeRecording - if true throttler will check the quota before recording usage, otherwise throttler will record usage then check the quota.
        throttlingStrategy - the strategy how throttler handle the quota exceeding case.
      • EventThrottler

        public EventThrottler​(java.util.function.LongSupplier maxRatePerSecondProvider,
                              long intervalMs,
                              java.lang.String throttlerName,
                              boolean checkQuotaBeforeRecording,
                              EventThrottlingStrategy throttlingStrategy)
        Parameters:
        maxRatePerSecondProvider - Provider for maximum rate that this throttler should allow (-1 is unlimited)
        intervalMs - Minimum interval over which the rate is measured (maximum is twice that)
        throttlerName - if specified, the throttler will share its limit with others named the same if null, the throttler will be independent of the others
        checkQuotaBeforeRecording - if true throttler will check the quota before recording usage, otherwise throttler will record usage then check the quota.
        throttlingStrategy - the strategy how throttler handle the quota exceeding case.
      • EventThrottler

        public EventThrottler​(@Nonnull
                              io.tehuti.utils.Time time,
                              java.util.function.LongSupplier maxRatePerSecondProvider,
                              long intervalMs,
                              java.lang.String throttlerName,
                              boolean checkQuotaBeforeRecording,
                              @Nonnull
                              EventThrottlingStrategy throttlingStrategy)
        Parameters:
        time - Used to inject a Time in tests
        maxRatePerSecondProvider - Provider for maximum rate that this throttler should allow (-1 is unlimited)
        intervalMs - Minimum interval over which the rate is measured (maximum is twice that)
        throttlerName - if specified, the throttler will share its limit with others named the same if null, the throttler will be independent of the others
        checkQuotaBeforeRecording - if true throttler will check the quota before recording usage, otherwise throttler will record usage then check the quota.
        throttlingStrategy - the strategy how throttler handle the quota exceeding case.
    • Method Detail

      • maybeThrottle

        public void maybeThrottle​(double eventsSeen)
        Sleeps if necessary to slow down the caller.
        Parameters:
        eventsSeen - Number of events seen since last invocation. Basis for determining whether its necessary to sleep.
      • getMaxRatePerSecond

        public final long getMaxRatePerSecond()
      • getTime

        protected io.tehuti.utils.Time getTime()
      • getRateConfig

        protected io.tehuti.metrics.MetricConfig getRateConfig()
      • getConfiguredMaxRatePerSecond

        protected long getConfiguredMaxRatePerSecond()
      • isCheckQuotaBeforeRecording

        protected boolean isCheckQuotaBeforeRecording()
      • 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 interface VeniceRateLimiter
        Parameters:
        units - Number of units to acquire.
        Returns:
        true if permit is acquired, false otherwise.
      • 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 interface VeniceRateLimiter
      • toString

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