Class SlidingWindowQuantileDigest
- java.lang.Object
-
- org.pipecraft.infra.monitoring.quantile.SlidingWindowQuantileDigest
-
- All Implemented Interfaces:
ConcurrentQuantileEstimator
public class SlidingWindowQuantileDigest extends Object implements ConcurrentQuantileEstimator
High throughput, thread safe, quantile and cdf estimation with a sliding window of time.This structure maintains a live digest and a fixed capacity journal. The live digest deals with all
add()operations. It is then periodically appended as an entry to the journal and reset. If the journal is at capacity, the oldest entry is removed to make way for the new one. The size of the journal determines the length of the sliding window.length_of_sliding_window_in_milliseconds = capacity * journalingIntervalMillisTo improve throughput of the add operation, the live digest is a
MultiQuantileDigestwhich maintains multiple (liveDigestCount) live digests simultaneously and routesadd()requests to the different digests.Example:
- journalingIntervalMillis: 60000
- capacity: 120
- liveDigestCount: 10
The above specs would journal every minute (60000 milliseconds) for the last 120 minutes. Essentially only retaining data for the last 2 hours. Calling the
quantilefunction would give data for the complete 2 hour time period. If we required quantiles for the last 30 minutes, we would callsummarize(30).quantile().The
liveDigestCountof 10 determines that theMultiQuantileDigestmaintains 10 live digests simultaneously. Upon querying (quantile,cdf, ...), theMultiQuantileDigestalong with the journal aresummarized into one singleQuantileDigest, which then responds to all quantile and cdf queries.At journaling time, the 10 live digests are merged and appended as 1 entry to the journal.
- Author:
- Mojtaba Kohram
-
-
Constructor Summary
Constructors Constructor Description SlidingWindowQuantileDigest(int liveDigestCount, double compression, double compressionInflationMultiplier, int journalingIntervalMillis, int capacity, ScheduledExecutorService executorService)Fully specified constructorSlidingWindowQuantileDigest(int liveDigestCount, double compression, int journalingIntervalMillis, int capacity, ScheduledExecutorService executorService)Instantiate with default compressionInflation valueSlidingWindowQuantileDigest(int liveDigestCount, int journalingIntervalMillis, int capacity, ScheduledExecutorService executorService)Instantiate with default compression and default compressionInflation values
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(double x, int w)Add a weighted sample.doublecdf(double x)Get an estimate for the cdf of the distribution atx.List<Double>cdf(List<Double> coords)Get an estimate for the cdf of the distribution at every coordinate of input list.intgetCapacity()Gets the journal capacity.doublegetCompression()Get the compression factor.intgetJournalingIntervalMillis()Gets journaling interval.voidpublishToJournal()Squash live digest(s) and add to journal as latest entry.doublequantile(double q)Get an estimate of the quantile atq.List<Double>quantile(List<Double> qs)Get an estimated quantile for every value in the input list.longsize()Returns the number of samples added to the current Estimator.QuantileDigestsummarize()Get an aggregate view of the current state of this object as aQuantileDigest.QuantileDigestsummarize(int lookback)Get an aggregate view of the current state of thelookbackmost recent journal entries as aQuantileDigest.booleantryAdd(double x, int w)Attempts to add a weighted sample to this estimator.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.pipecraft.infra.monitoring.quantile.ConcurrentQuantileEstimator
add, tryAdd
-
-
-
-
Constructor Detail
-
SlidingWindowQuantileDigest
public SlidingWindowQuantileDigest(int liveDigestCount, int journalingIntervalMillis, int capacity, ScheduledExecutorService executorService)Instantiate with default compression and default compressionInflation values- Parameters:
liveDigestCount- the number of live digests to maintainjournalingIntervalMillis- interval between two journaling operations in milliseconds, equivalent to the resolution of the sliding window, set this to -1 to disable automated journalingcapacity- capacity of the journalexecutorService- the executor to schedule the journaling task to
-
SlidingWindowQuantileDigest
public SlidingWindowQuantileDigest(int liveDigestCount, double compression, int journalingIntervalMillis, int capacity, ScheduledExecutorService executorService)Instantiate with default compressionInflation value- Parameters:
liveDigestCount- the number of live digests to maintaincompression- the compression factor of the final digestjournalingIntervalMillis- interval between two journaling operations in milliseconds, equivalent to the resolution of the sliding window, set this to -1 to disable automated journalingcapacity- capacity of the journalexecutorService- the executor to schedule the journaling task to
-
SlidingWindowQuantileDigest
public SlidingWindowQuantileDigest(int liveDigestCount, double compression, double compressionInflationMultiplier, int journalingIntervalMillis, int capacity, ScheduledExecutorService executorService)Fully specified constructor- Parameters:
liveDigestCount- the number of live digests to maintaincompression- the compression factor of the final digestcompressionInflationMultiplier- the compression inflation multiplier, see:MultiQuantileDigest.getCompressionInflation()journalingIntervalMillis- interval between two journaling operations in milliseconds, equivalent to the resolution of the sliding window, set this to -1 to disable automated journalingcapacity- capacity of the journalexecutorService- the executor to schedule the journaling task to
-
-
Method Detail
-
getCompression
public double getCompression()
Get the compression factor. gi- Returns:
- the compression factor
-
getJournalingIntervalMillis
public int getJournalingIntervalMillis()
Gets journaling interval. Returns -1 if automated journaling is disabled.- Returns:
- the journaling interval in milliseconds
-
getCapacity
public int getCapacity()
Gets the journal capacity.- Returns:
- the capacity of the journal
-
summarize
public QuantileDigest summarize()
Get an aggregate view of the current state of this object as aQuantileDigest. The returnedQuantileDigestis independent of this object and the caller is free to modify the returned digest.- Returns:
- the digest representing all the data currently consumed by this object
-
summarize
public QuantileDigest summarize(int lookback)
Get an aggregate view of the current state of thelookbackmost recent journal entries as aQuantileDigest. In other words, journal entries are added in reverse chronological order. The returnedQuantileDigestis independent of this object and the caller is free to modify the returned digest.- Parameters:
lookback- the number of journal entries to add to the summary, must be less than the journal capacity- Returns:
- the digest representing
lookbackjournal entries
-
publishToJournal
public void publishToJournal()
Squash live digest(s) and add to journal as latest entry. If journal is at capacity removes oldest entry.
-
quantile
public double quantile(double q)
Get an estimate of the quantile atq. This function could be expensive depending on implementation. Consider usingConcurrentQuantileEstimator.quantile(List)when querying more than one quantile value.- Specified by:
quantilein interfaceConcurrentQuantileEstimator- Parameters:
q- quantile to query, must be between 0 and 1- Returns:
- the estimated quantile value at
q
-
quantile
public List<Double> quantile(List<Double> qs)
Get an estimated quantile for every value in the input list.- Specified by:
quantilein interfaceConcurrentQuantileEstimator- Parameters:
qs- list of quantiles to query, must all be between 0 and 1- Returns:
- the estimated quantile value for every element in
quantileList, in order
-
size
public long size()
Returns the number of samples added to the current Estimator.- Specified by:
sizein interfaceConcurrentQuantileEstimator- Returns:
- the number of samples currently added
-
cdf
public double cdf(double x)
Get an estimate for the cdf of the distribution atx. This function could be expensive depending on implementation. Consider usingConcurrentQuantileEstimator.cdf(List)when querying more than one cdf value.- Specified by:
cdfin interfaceConcurrentQuantileEstimator- Parameters:
x- the value to get cdf at- Returns:
- the estimated cumulative distribution function value at
x, always between 0 and 1
-
cdf
public List<Double> cdf(List<Double> coords)
Get an estimate for the cdf of the distribution at every coordinate of input list.- Specified by:
cdfin interfaceConcurrentQuantileEstimator- Parameters:
coords- the list of coordinates to compute the cdf at- Returns:
- the estimated cumulative distribution function value for every element in
coordinates, in order, results are always between 0 and 1
-
tryAdd
public boolean tryAdd(double x, int w)Attempts to add a weighted sample to this estimator. Returns false if a lock is held by another thread.- Specified by:
tryAddin interfaceConcurrentQuantileEstimator- Parameters:
x- data to addw- weight- Returns:
- false if this object's lock is held by another thread, true otherwise
-
add
public void add(double x, int w)Add a weighted sample. This is a blocking call, for non-blocking adds seetryAdd(double, int).- Specified by:
addin interfaceConcurrentQuantileEstimator- Parameters:
x- data to addw- weight
-
-