-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathStatsDNonBlockingProcessor.java
More file actions
183 lines (144 loc) · 6.29 KB
/
StatsDNonBlockingProcessor.java
File metadata and controls
183 lines (144 loc) · 6.29 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.timgroup.statsd;
import com.timgroup.statsd.Message;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
public class StatsDNonBlockingProcessor extends StatsDProcessor {
private final Queue<Message>[] messages;
private final Queue<Integer>[] processorWorkQueue;
private final AtomicInteger[] qsize; // qSize will not reflect actual size, but a close estimate.
private class ProcessingTask extends StatsDProcessor.ProcessingTask {
public ProcessingTask(int id) {
super(id);
}
@Override
public void run() {
ByteBuffer sendBuffer;
boolean empty = true;
boolean emptyHighPrio = true;
int messageQueueIdx = 0;
try {
sendBuffer = bufferPool.borrow();
} catch (final InterruptedException e) {
handler.handle(e);
return;
}
aggregator.start();
while (!((emptyHighPrio = highPrioMessages.isEmpty())
&& (empty = processorWorkQueue[this.processorQueueId].isEmpty()) && shutdown)) {
try {
if (emptyHighPrio && empty) {
Thread.sleep(WAIT_SLEEP_MS);
continue;
}
if (Thread.interrupted()) {
return;
}
final Message message;
if (!emptyHighPrio) {
message = highPrioMessages.poll();
} else {
messageQueueIdx = processorWorkQueue[this.processorQueueId].poll();
message = messages[messageQueueIdx].poll();
}
if (message != null) {
qsize[messageQueueIdx].decrementAndGet();
// TODO: Aggregate and fix, there's some duplicate logic
if (aggregator.aggregateMessage(message)) {
continue;
}
builder.setLength(0);
message.writeTo(builder);
int lowerBoundSize = builder.length();
if (sendBuffer.capacity() < lowerBoundSize) {
throw new InvalidMessageException(MESSAGE_TOO_LONG, builder.toString());
}
if (sendBuffer.remaining() < (lowerBoundSize + 1)) {
outboundQueue.put(sendBuffer);
sendBuffer = bufferPool.borrow();
}
sendBuffer.mark();
if (sendBuffer.position() > 0) {
sendBuffer.put((byte) '\n');
}
try {
writeBuilderToSendBuffer(sendBuffer);
} catch (BufferOverflowException boe) {
outboundQueue.put(sendBuffer);
sendBuffer = bufferPool.borrow();
writeBuilderToSendBuffer(sendBuffer);
}
if (null == processorWorkQueue[this.processorQueueId].peek()) {
outboundQueue.put(sendBuffer);
sendBuffer = bufferPool.borrow();
}
}
} catch (final InterruptedException e) {
if (shutdown) {
endSignal.countDown();
return;
}
} catch (final Exception e) {
handler.handle(e);
}
}
builder.setLength(0);
builder.trimToSize();
aggregator.stop();
endSignal.countDown();
}
}
StatsDNonBlockingProcessor(final int queueSize, final StatsDClientErrorHandler handler,
final int maxPacketSizeBytes, final int poolSize, final int workers,
final int lockShardGrain, final int aggregatorFlushInterval,
final int aggregatorShards) throws Exception {
super(queueSize, handler, maxPacketSizeBytes, poolSize, workers,
lockShardGrain, aggregatorFlushInterval, aggregatorShards);
this.qsize = new AtomicInteger[lockShardGrain];
this.messages = new ConcurrentLinkedQueue[lockShardGrain];
for (int i = 0 ; i < lockShardGrain ; i++) {
this.qsize[i] = new AtomicInteger();
this.messages[i] = new ConcurrentLinkedQueue<Message>();
this.qsize[i].set(0);
}
this.processorWorkQueue = new ConcurrentLinkedQueue[workers];
for (int i = 0 ; i < workers ; i++) {
this.processorWorkQueue[i] = new ConcurrentLinkedQueue<Integer>();
}
}
@Override
protected ProcessingTask createProcessingTask(int id) {
return new ProcessingTask(id);
}
StatsDNonBlockingProcessor(final StatsDNonBlockingProcessor processor) throws Exception {
super(processor);
this.qsize = new AtomicInteger[lockShardGrain];
this.messages = new ConcurrentLinkedQueue[lockShardGrain];
for (int i = 0 ; i < lockShardGrain ; i++) {
this.qsize[i] = new AtomicInteger();
this.messages[i] = new ConcurrentLinkedQueue<Message>();
this.qsize[i].set(0);
}
this.processorWorkQueue = new ConcurrentLinkedQueue[workers];
for (int i = 0 ; i < workers ; i++) {
this.processorWorkQueue[i] = new ConcurrentLinkedQueue<Integer>();
}
}
@Override
protected boolean send(final Message message) {
if (!shutdown) {
int threadId = getThreadId();
int shard = threadId % lockShardGrain;
int processQueue = threadId % workers;
if (qsize[shard].get() < qcapacity) {
messages[shard].offer(message);
qsize[shard].incrementAndGet();
processorWorkQueue[processQueue].offer(shard);
return true;
}
}
return false;
}
}