Class UniformTimeReservoir
- java.lang.Object
-
- org.glassfish.jersey.server.internal.monitoring.core.UniformTimeReservoir
-
- All Implemented Interfaces:
TimeReservoir<java.lang.Long>
public class UniformTimeReservoir extends java.lang.Object implements TimeReservoir<java.lang.Long>
A random sampling reservoir of a stream oflong
s. Uses Vitter's Algorithm R to produce a statistically representative sample.
-
-
Field Summary
Fields Modifier and Type Field Description private static int
BITS_PER_LONG
private java.util.concurrent.atomic.AtomicLong
count
private static int
DEFAULT_SIZE
private long
startTime
private java.util.concurrent.TimeUnit
startTimeUnit
private java.util.concurrent.atomic.AtomicLongArray
values
-
Constructor Summary
Constructors Constructor Description UniformTimeReservoir(int size, long startTime, java.util.concurrent.TimeUnit startTimeUnit)
Creates a newUniformTimeReservoir
instance.UniformTimeReservoir(long startTime, java.util.concurrent.TimeUnit startTimeUnit)
Creates a newUniformTimeReservoir
instance of 1024 elements, which offers a 99.9% confidence level with a 5% margin of error assuming a normal distribution.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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 static long
nextLong(long n)
Get a pseudo-random long uniformly between 0 and n-1.int
size(long time, java.util.concurrent.TimeUnit timeUnit)
Returns the number of values recorded at given time or newer.void
update(java.lang.Long value, long time, java.util.concurrent.TimeUnit timeUnit)
Adds a new recorded value to the reservoir bound to a given time.
-
-
-
Field Detail
-
startTime
private final long startTime
-
startTimeUnit
private final java.util.concurrent.TimeUnit startTimeUnit
-
DEFAULT_SIZE
private static final int DEFAULT_SIZE
- See Also:
- Constant Field Values
-
BITS_PER_LONG
private static final int BITS_PER_LONG
- See Also:
- Constant Field Values
-
count
private final java.util.concurrent.atomic.AtomicLong count
-
values
private final java.util.concurrent.atomic.AtomicLongArray values
-
-
Constructor Detail
-
UniformTimeReservoir
public UniformTimeReservoir(long startTime, java.util.concurrent.TimeUnit startTimeUnit)
Creates a newUniformTimeReservoir
instance of 1024 elements, which offers a 99.9% confidence level with a 5% margin of error assuming a normal distribution.- Parameters:
startTime
- The start timestartTimeUnit
- The start time unit
-
UniformTimeReservoir
public UniformTimeReservoir(int size, long startTime, java.util.concurrent.TimeUnit startTimeUnit)
Creates a newUniformTimeReservoir
instance.- Parameters:
size
- the number of samples to keep in the sampling reservoirstartTime
- The start timestartTimeUnit
- The start time unit
-
-
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 interfaceTimeReservoir<java.lang.Long>
- Parameters:
time
- The time to get the size fortimeUnit
- Time unit of the provided time- Returns:
- the number of values recorded for given time or newer
-
update
public void update(java.lang.Long 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 interfaceTimeReservoir<java.lang.Long>
- Parameters:
value
- a new recorded valuetime
- The time the recorded value occurred attimeUnit
- Time unit of the provided time
-
nextLong
private static long nextLong(long n)
Get a pseudo-random long uniformly between 0 and n-1. Stolen fromRandom.nextInt()
.- Parameters:
n
- the bound- Returns:
- a value select randomly from the range
[0..n)
.
-
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 interfaceTimeReservoir<java.lang.Long>
- Parameters:
time
- The time for which to get the snapshottimeUnit
- Time unit of the provided time- Returns:
- a snapshot of the reservoir's values for given time or newer
-
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 interfaceTimeReservoir<java.lang.Long>
- Parameters:
timeUnit
- The time unit in which to get the interval- Returns:
- The time interval of this time reservoir
-
-