blob: d5903eb5eefb1e8f497adef01d88d697354396aa (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.metrics.simple;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import com.yahoo.concurrent.ThreadLocalDirectory;
import com.yahoo.metrics.ManagerConfig;
/**
* Worker thread to collect the data stored in worker threads and build
* snapshots for external consumption. Using the correct executor gives the
* necessary guarantuees for this being invoked from only a single thread.
*
* @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
*/
class MetricAggregator implements Runnable {
private final ThreadLocalDirectory<Bucket, Sample> metricsCollection;
private final AtomicReference<Bucket> currentSnapshot;
private int generation = 0;
private final Bucket[] buffer;
private long fromMillis;
private final DimensionCache dimensions;
MetricAggregator(ThreadLocalDirectory<Bucket, Sample> metricsCollection, AtomicReference<Bucket> currentSnapshot,
ManagerConfig settings) {
if (settings.reportPeriodSeconds() < 10) {
throw new IllegalArgumentException(
"Do not use this metrics implementation"
+ " if report periods of less than 10 seconds is desired.");
}
buffer = new Bucket[settings.reportPeriodSeconds()];
dimensions = new DimensionCache(settings.pointsToKeepPerMetric());
fromMillis = System.currentTimeMillis();
this.metricsCollection = metricsCollection;
this.currentSnapshot = currentSnapshot;
}
@Override
public void run() {
Bucket toDelete = updateBuffer();
createSnapshot(toDelete);
}
private void createSnapshot(Bucket toDelete) {
final Bucket toPresent = new Bucket();
for (Bucket b : buffer) {
if (b == null) {
continue;
}
toPresent.merge(b);
}
dimensions.updateDimensionPersistence(toDelete, toPresent);
currentSnapshot.set(toPresent);
}
private Bucket updateBuffer() {
List<Bucket> buckets = metricsCollection.fetch();
final long toMillis = System.currentTimeMillis();
final int bucketIndex = generation++ % buffer.length;
Bucket bucketToDelete = buffer[bucketIndex];
Bucket latest = new Bucket(fromMillis, toMillis);
for (Bucket b : buckets) {
latest.merge(b, true);
}
buffer[bucketIndex] = latest;
this.fromMillis = toMillis;
return bucketToDelete;
}
}
|