Class AbstractSlidingWindowTimeReservoir<V>

  • Type Parameters:
    V - The type of values to store in this sliding window reservoir
    All Implemented Interfaces:
    TimeReservoir<V>
    Direct Known Subclasses:
    AggregatedSlidingWindowTimeReservoir, SlidingWindowTimeReservoir

    public abstract class AbstractSlidingWindowTimeReservoir<V>
    extends java.lang.Object
    implements TimeReservoir<V>
    An abstract TimeReservoir implementation backed by a sliding window that stores only the measurements made in the last N seconds (or other startTime unit) and allows an update with data that happened in past (which is what makes it different from Dropwizard's Metrics SlidingTimeWindowReservoir.

    The snapshot this reservoir returns has limitations as mentioned in TimeReservoir.

    This reservoir is capable to store up to 2^ReservoirConstants.COLLISION_BUFFER_POWER, that is 256, in a granularity of nanoseconds. In other words, up to 256 values that occurred at the same nanosecond can be stored in this reservoir. For particular nanosecond, if the collision buffer exceeds, newly added values are thrown away.

    See Also:
    Dropwizard's
     Metrics SlidingTimeWindowReservoir
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.atomic.AtomicLong greatestTick  
      private long interval  
      private java.util.concurrent.TimeUnit intervalUnit  
      private java.util.concurrent.ConcurrentNavigableMap<java.lang.Long,​V> measurements  
      private java.util.concurrent.atomic.AtomicLong startTick  
      private SlidingWindowTrimmer<V> trimmer  
      private java.util.concurrent.atomic.AtomicInteger trimOff  
      private java.util.concurrent.atomic.AtomicLong updateCount  
      private long window  
    • Constructor Summary

      Constructors 
      Constructor Description
      AbstractSlidingWindowTimeReservoir​(long window, java.util.concurrent.TimeUnit windowUnit, long startTime, java.util.concurrent.TimeUnit startTimeUnit)
      Creates a new SlidingWindowTimeReservoir with the start time and window of startTime.
      AbstractSlidingWindowTimeReservoir​(long window, java.util.concurrent.TimeUnit windowUnit, long startTime, java.util.concurrent.TimeUnit startTimeUnit, SlidingWindowTrimmer<V> trimmer)
      Creates a new base sliding time window reservoir with the start time and a specified time window.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      private long conditionallyUpdateGreatestTick​(long tick)  
      private void conditionallyUpdateStartTick​(java.util.Map.Entry<java.lang.Long,​V> firstEntry)
      Updates the startTick in case that the sliding window was created AFTER the time of a value that updated this window.
      UniformTimeSnapshot getSnapshot​(long time, java.util.concurrent.TimeUnit timeUnit)
      Returns a snapshot of the reservoir's values at given time or newer.
      long interval​(java.util.concurrent.TimeUnit timeUnit)
      The time interval this reservoir stores data of.
      private long roundTick​(long tick)
      The purpose of this method is to deal with the fact that data for the same nanosecond can be distributed in an interval [0,256).
      int size​(long time, java.util.concurrent.TimeUnit timeUnit)
      Returns the number of values recorded at given time or newer.
      protected abstract UniformTimeSnapshot snapshot​(java.util.Collection<V> values, long timeInterval, java.util.concurrent.TimeUnit timeIntervalUnit, long time, java.util.concurrent.TimeUnit timeUnit)
      Subclasses are required to instantiate UniformTimeSnapshot on their own.
      private long tick​(long time, java.util.concurrent.TimeUnit timeUnit)  
      private void trim()  
      private void trim​(long baselineTick)  
      private boolean trimEnabled()  
      void update​(V value, long time, java.util.concurrent.TimeUnit timeUnit)
      Adds a new recorded value to the reservoir bound to a given time.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • measurements

        private final java.util.concurrent.ConcurrentNavigableMap<java.lang.Long,​V> measurements
      • window

        private final long window
      • greatestTick

        private final java.util.concurrent.atomic.AtomicLong greatestTick
      • updateCount

        private final java.util.concurrent.atomic.AtomicLong updateCount
      • startTick

        private final java.util.concurrent.atomic.AtomicLong startTick
      • trimOff

        private final java.util.concurrent.atomic.AtomicInteger trimOff
      • interval

        private final long interval
      • intervalUnit

        private final java.util.concurrent.TimeUnit intervalUnit
    • Constructor Detail

      • AbstractSlidingWindowTimeReservoir

        public AbstractSlidingWindowTimeReservoir​(long window,
                                                  java.util.concurrent.TimeUnit windowUnit,
                                                  long startTime,
                                                  java.util.concurrent.TimeUnit startTimeUnit)
        Creates a new SlidingWindowTimeReservoir with the start time and window of startTime.
        Parameters:
        window - The window of startTime
        windowUnit - The unit of window
        startTime - The start time from which this reservoir calculates measurements
        startTimeUnit - The start time unit
      • AbstractSlidingWindowTimeReservoir

        public AbstractSlidingWindowTimeReservoir​(long window,
                                                  java.util.concurrent.TimeUnit windowUnit,
                                                  long startTime,
                                                  java.util.concurrent.TimeUnit startTimeUnit,
                                                  SlidingWindowTrimmer<V> trimmer)
        Creates a new base sliding time window reservoir with the start time and a specified time window.
        Parameters:
        window - The window of startTime.
        windowUnit - The unit of window.
        startTime - The start time from which this reservoir calculates measurements.
        startTimeUnit - The start time unit.
        trimmer - The trimmer to use for trimming, if null, default trimmer is used.
    • Method Detail

      • size

        public int size​(long time,
                        java.util.concurrent.TimeUnit timeUnit)
        Description copied from interface: TimeReservoir
        Returns the number of values recorded at given time or newer. It may not be supported to return a size in past due to performance optimizations.
        Specified by:
        size in interface TimeReservoir<V>
        Parameters:
        time - The time to get the size for
        timeUnit - Time unit of the provided time
        Returns:
        the number of values recorded for given time or newer
      • update

        public void update​(V value,
                           long time,
                           java.util.concurrent.TimeUnit timeUnit)
        Description copied from interface: TimeReservoir
        Adds a new recorded value to the reservoir bound to a given time.
        Specified by:
        update in interface TimeReservoir<V>
        Parameters:
        value - a new recorded value
        time - The time the recorded value occurred at
        timeUnit - Time unit of the provided time
      • interval

        public long interval​(java.util.concurrent.TimeUnit timeUnit)
        Description copied from interface: TimeReservoir
        The time interval this reservoir stores data of.
        Specified by:
        interval in interface TimeReservoir<V>
        Parameters:
        timeUnit - The time unit in which to get the interval
        Returns:
        The time interval of this time reservoir
      • conditionallyUpdateGreatestTick

        private long conditionallyUpdateGreatestTick​(long tick)
      • conditionallyUpdateStartTick

        private void conditionallyUpdateStartTick​(java.util.Map.Entry<java.lang.Long,​V> firstEntry)
        Updates the startTick in case that the sliding window was created AFTER the time of a value that updated this window.
        Parameters:
        firstEntry - The first entry of the windowed measurments
      • snapshot

        protected abstract UniformTimeSnapshot snapshot​(java.util.Collection<V> values,
                                                        long timeInterval,
                                                        java.util.concurrent.TimeUnit timeIntervalUnit,
                                                        long time,
                                                        java.util.concurrent.TimeUnit timeUnit)
        Subclasses are required to instantiate UniformTimeSnapshot on their own.
        Parameters:
        values - The values to create the snapshot from
        timeInterval - The time interval this snapshot conforms to
        timeIntervalUnit - The interval unit of the time interval
        time - The time of the request of the snapshot
        timeUnit - The unit of the time of the snapshot request
        Returns:
        The snapshot
      • getSnapshot

        public UniformTimeSnapshot getSnapshot​(long time,
                                               java.util.concurrent.TimeUnit timeUnit)
        Description copied from interface: TimeReservoir
        Returns a snapshot of the reservoir's values at given time or newer. It may not be supported to return a snapshot in past due to performance optimizations.
        Specified by:
        getSnapshot in interface TimeReservoir<V>
        Parameters:
        time - The time for which to get the snapshot
        timeUnit - Time unit of the provided time
        Returns:
        a snapshot of the reservoir's values for given time or newer
      • tick

        private long tick​(long time,
                          java.util.concurrent.TimeUnit timeUnit)
      • trim

        private void trim()
      • trim

        private void trim​(long baselineTick)
      • trimEnabled

        private boolean trimEnabled()
      • roundTick

        private long roundTick​(long tick)
        The purpose of this method is to deal with the fact that data for the same nanosecond can be distributed in an interval [0,256). By rounding the tick, we get the tick to which all the other ticks from the same interval belong.
        Parameters:
        tick - The tick
        Returns:
        The rounded tick