-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathStatsDAggregator.java
More file actions
157 lines (132 loc) · 4.94 KB
/
Copy pathStatsDAggregator.java
File metadata and controls
157 lines (132 loc) · 4.94 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.timgroup.statsd;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class StatsDAggregator {
public static int DEFAULT_FLUSH_INTERVAL = 2000; // 2s
public static int DEFAULT_SHARDS = 4; // 4 partitions to reduce contention.
protected final String AGGREGATOR_THREAD_NAME = "statsd-aggregator-thread";
protected final ArrayList<Map<Message, Message>> aggregateMetrics;
private final Lock[] locks;
protected final int shardGranularity;
protected final long flushInterval;
private final StatsDProcessor processor;
protected Timer scheduler = null;
private Telemetry telemetry;
private class FlushTask extends TimerTask {
@Override
public void run() {
flush();
}
}
/**
* StatsDAggregtor constructor.
*
* @param processor the message processor, aggregated messages will be queued in the high
* priority queue.
* @param shards number of shards for the aggregation map.
* @param flushInterval flush interval in miliseconds, 0 disables message aggregation.
*/
public StatsDAggregator(
final StatsDProcessor processor, final int shards, final long flushInterval) {
this.processor = processor;
this.flushInterval = flushInterval;
this.shardGranularity = shards;
this.aggregateMetrics = new ArrayList<>(shards);
this.locks = new ReentrantLock[shards];
if (flushInterval > 0) {
this.scheduler = new Timer(AGGREGATOR_THREAD_NAME, true);
}
for (int i = 0; i < this.shardGranularity; i++) {
this.aggregateMetrics.add(i, new HashMap<Message, Message>());
this.locks[i] = new ReentrantLock();
}
}
/** Start the aggregator flushing scheduler. */
public void start() {
if (flushInterval > 0) {
// snapshot of processor telemetry - avoid volatile reference to harness CPU cache
// caller responsible of setting telemetry before starting
telemetry = processor.getTelemetry();
scheduler.scheduleAtFixedRate(new FlushTask(), flushInterval, flushInterval);
}
}
/** Stop the aggregator flushing scheduler. */
public void stop() {
if (flushInterval > 0) {
scheduler.cancel();
}
}
/**
* Aggregate a message if possible.
*
* @param message the dogstatsd Message we wish to aggregate.
* @return a boolean reflecting if the message was aggregated.
*/
public boolean aggregateMessage(Message message) {
if (flushInterval == 0 || !message.canAggregate() || message.getDone()) {
return false;
}
int hash = message.hashCode();
int bucket = Math.abs(hash % this.shardGranularity);
Map<Message, Message> map = aggregateMetrics.get(bucket);
locks[bucket].lock();
try {
// For now let's just put the message in the map
Message msg = MapUtils.putIfAbsent(map, message);
if (msg != null) {
msg.aggregate(message);
if (telemetry != null) {
telemetry.incrAggregatedContexts(1);
// developer metrics
switch (message.getType()) {
case GAUGE:
telemetry.incrAggregatedGaugeContexts(1);
break;
case COUNT:
telemetry.incrAggregatedCountContexts(1);
break;
case SET:
telemetry.incrAggregatedSetContexts(1);
break;
default:
break;
}
}
}
} finally {
locks[bucket].unlock();
}
return true;
}
public final long getFlushInterval() {
return this.flushInterval;
}
public final int getShardGranularity() {
return this.shardGranularity;
}
protected void flush() {
for (int i = 0; i < shardGranularity; i++) {
Map<Message, Message> map = aggregateMetrics.get(i);
locks[i].lock();
try {
Iterator<Map.Entry<Message, Message>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Message msg = iter.next().getValue();
msg.setDone(true);
if (!processor.sendHighPrio(msg) && (telemetry != null)) {
telemetry.incrPacketDroppedQueue(1);
}
iter.remove();
}
} finally {
locks[i].unlock();
}
}
}
}