V
- The type of values to store in this sliding window reservoirpublic abstract class AbstractSlidingWindowTimeReservoir<V> extends java.lang.Object implements TimeReservoir<V>
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.Modifier and Type | Class and Description |
---|---|
private static class |
AbstractSlidingWindowTimeReservoir.DefaultSlidingWindowTrimmerHolder
The holder of the lazy loaded instance of the default trimmer.
|
Modifier and Type | Field and 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 and 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.
|
Modifier and Type | Method and 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.
|
private final java.util.concurrent.ConcurrentNavigableMap<java.lang.Long,V> measurements
private final long window
private final java.util.concurrent.atomic.AtomicLong greatestTick
private final java.util.concurrent.atomic.AtomicLong updateCount
private final java.util.concurrent.atomic.AtomicLong startTick
private final java.util.concurrent.atomic.AtomicInteger trimOff
private final SlidingWindowTrimmer<V> trimmer
private final long interval
private final java.util.concurrent.TimeUnit intervalUnit
public AbstractSlidingWindowTimeReservoir(long window, java.util.concurrent.TimeUnit windowUnit, long startTime, java.util.concurrent.TimeUnit startTimeUnit)
SlidingWindowTimeReservoir
with the start time and window of startTime.window
- The window of startTimewindowUnit
- The unit of window
startTime
- The start time from which this reservoir calculates measurementsstartTimeUnit
- The start time unitpublic AbstractSlidingWindowTimeReservoir(long window, java.util.concurrent.TimeUnit windowUnit, long startTime, java.util.concurrent.TimeUnit startTimeUnit, SlidingWindowTrimmer<V> trimmer)
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.public int size(long time, java.util.concurrent.TimeUnit timeUnit)
TimeReservoir
size
in interface TimeReservoir<V>
time
- The time to get the size fortimeUnit
- Time unit of the provided timepublic void update(V value, long time, java.util.concurrent.TimeUnit timeUnit)
TimeReservoir
update
in interface TimeReservoir<V>
value
- a new recorded valuetime
- The time the recorded value occurred attimeUnit
- Time unit of the provided timepublic long interval(java.util.concurrent.TimeUnit timeUnit)
TimeReservoir
interval
in interface TimeReservoir<V>
timeUnit
- The time unit in which to get the intervalprivate long conditionallyUpdateGreatestTick(long tick)
private void conditionallyUpdateStartTick(java.util.Map.Entry<java.lang.Long,V> firstEntry)
firstEntry
- The first entry of the windowed measurmentsprotected abstract UniformTimeSnapshot snapshot(java.util.Collection<V> values, long timeInterval, java.util.concurrent.TimeUnit timeIntervalUnit, long time, java.util.concurrent.TimeUnit timeUnit)
UniformTimeSnapshot
on their own.values
- The values to create the snapshot fromtimeInterval
- The time interval this snapshot conforms totimeIntervalUnit
- The interval unit of the time intervaltime
- The time of the request of the snapshottimeUnit
- The unit of the time of the snapshot requestpublic UniformTimeSnapshot getSnapshot(long time, java.util.concurrent.TimeUnit timeUnit)
TimeReservoir
getSnapshot
in interface TimeReservoir<V>
time
- The time for which to get the snapshottimeUnit
- Time unit of the provided timeprivate long tick(long time, java.util.concurrent.TimeUnit timeUnit)
private void trim()
private void trim(long baselineTick)
private boolean trimEnabled()
private long roundTick(long tick)
tick
- The tick