Skip to content

Commit fe04fbe

Browse files
committed
Fix decay for quantile digests which are more frequently read than modified
1 parent 402174e commit fe04fbe

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

stats/src/main/java/com/facebook/airlift/stats/QuantileDigest.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,14 @@ public void scale(double scaleFactor)
328328
*/
329329
public List<Long> getQuantilesLowerBound(List<Double> quantiles)
330330
{
331+
if (alpha > 0.0) {
332+
long nowInSeconds = TimeUnit.NANOSECONDS.toSeconds(ticker.read());
333+
if (nowInSeconds - landmarkInSeconds >= RESCALE_THRESHOLD_SECONDS) {
334+
rescale(nowInSeconds);
335+
compress(); // rescale affects weights globally, so force compression
336+
}
337+
}
338+
331339
checkArgument(Ordering.natural().isOrdered(quantiles), "quantiles must be sorted in increasing order");
332340
for (double quantile : quantiles) {
333341
checkArgument(quantile >= 0 && quantile <= 1, "quantile must be between [0,1]");
@@ -379,6 +387,14 @@ public boolean process(int node)
379387
*/
380388
public List<Long> getQuantilesUpperBound(List<Double> quantiles)
381389
{
390+
if (alpha > 0.0) {
391+
long nowInSeconds = TimeUnit.NANOSECONDS.toSeconds(ticker.read());
392+
if (nowInSeconds - landmarkInSeconds >= RESCALE_THRESHOLD_SECONDS) {
393+
rescale(nowInSeconds);
394+
compress(); // rescale affects weights globally, so force compression
395+
}
396+
}
397+
382398
checkArgument(Ordering.natural().isOrdered(quantiles), "quantiles must be sorted in increasing order");
383399
for (double quantile : quantiles) {
384400
checkArgument(quantile >= 0 && quantile <= 1, "quantile must be between [0,1]");
@@ -411,9 +427,12 @@ public boolean process(int node)
411427

412428
// we finished the traversal without consuming all quantiles. This means the remaining quantiles
413429
// correspond to the max known value
414-
while (iterator.hasNext()) {
415-
builder.add(max);
416-
iterator.next();
430+
if (iterator.hasNext()) {
431+
long max = getMax();
432+
while (iterator.hasNext()) {
433+
builder.add(max);
434+
iterator.next();
435+
}
417436
}
418437

419438
return builder.build();
@@ -624,7 +643,14 @@ void compress()
624643
{
625644
double bound = Math.floor(weightedCount / calculateCompressionFactor());
626645

646+
AtomicLong max = new AtomicLong(Integer.MIN_VALUE);
647+
AtomicLong min = new AtomicLong(Integer.MAX_VALUE);
627648
postOrderTraversal(root, node -> {
649+
// If decay is enabled and this node has a non-zero weight, determine if the value is the new max or min
650+
if (counts[node] >= ZERO_WEIGHT_THRESHOLD && alpha > 0.0) {
651+
max.accumulateAndGet(upperBound(node), Math::max);
652+
min.accumulateAndGet(lowerBound(node), Math::min);
653+
}
628654
// if children's weights are 0 remove them and shift the weight to their parent
629655
int left = lefts[node];
630656
int right = rights[node];
@@ -655,6 +681,15 @@ void compress()
655681
// root's count may have decayed to ~0
656682
if (root != -1 && counts[root] < ZERO_WEIGHT_THRESHOLD) {
657683
root = tryRemove(root);
684+
if (root < 0) {
685+
this.min = Long.MAX_VALUE;
686+
this.max = Long.MIN_VALUE;
687+
}
688+
// If decay is being used, the min and max values may have decayed as well
689+
else if (alpha > 0) {
690+
this.min = min.get();
691+
this.max = max.get();
692+
}
658693
}
659694
}
660695

stats/src/test/java/com/facebook/airlift/stats/TestQuantileDigest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,31 @@ public void testDecayedCountsWithClockIncrementSmallerThanRescaleThreshold()
433433
assertEquals(digest.getCount(), 15.0);
434434
}
435435

436+
@Test
437+
public void testDecayedQuantilesWithNoMergeOrAdd()
438+
throws Exception
439+
{
440+
TestingTicker ticker = new TestingTicker();
441+
QuantileDigest digest = new QuantileDigest(0.01, ExponentialDecay.computeAlpha(0.5, 60), ticker);
442+
443+
addAll(digest, asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
444+
445+
assertEquals(digest.getConfidenceFactor(), 0.0);
446+
assertEquals(digest.getQuantile(0.5), 5);
447+
448+
// Decay the digest
449+
ticker.increment(60, TimeUnit.SECONDS);
450+
assertEquals(digest.getQuantile(0.5), 5);
451+
452+
// Allow further decay
453+
ticker.increment(6, TimeUnit.MINUTES);
454+
assertEquals(digest.getQuantile(0.5), 5);
455+
456+
// Values have decayed to 0
457+
ticker.increment(60, TimeUnit.MINUTES);
458+
assertEquals(digest.getQuantile(0.5), Long.MIN_VALUE); // Default value for empty digests
459+
}
460+
436461
@Test
437462
public void testMinMax()
438463
throws Exception

0 commit comments

Comments
 (0)