-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathStatsDBlockingProcessor.java
More file actions
163 lines (128 loc) · 5.52 KB
/
StatsDBlockingProcessor.java
File metadata and controls
163 lines (128 loc) · 5.52 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
package com.timgroup.statsd;
import com.timgroup.statsd.Message;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class StatsDBlockingProcessor extends StatsDProcessor {
private final BlockingQueue<Message>[] messages;
private final BlockingQueue<Integer>[] processorWorkQueue;
private class ProcessingTask extends StatsDProcessor.ProcessingTask {
public ProcessingTask(int id) {
super(id);
}
@Override
public void run() {
ByteBuffer sendBuffer;
boolean empty = true;
boolean emptyHighPrio = true;
try {
sendBuffer = bufferPool.borrow();
} catch (final InterruptedException e) {
handler.handle(e);
return;
}
aggregator.start();
while (!((emptyHighPrio = highPrioMessages.isEmpty())
&& processorWorkQueue[this.processorQueueId].isEmpty() && shutdown)) {
try {
final Message message;
if (!emptyHighPrio) {
message = highPrioMessages.poll();
} else {
final int messageQueueIdx = processorWorkQueue[this.processorQueueId].poll();
message = messages[messageQueueIdx].poll(WAIT_SLEEP_MS, TimeUnit.MILLISECONDS);
}
if (message != null) {
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();
}
}
StatsDBlockingProcessor(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.messages = new ArrayBlockingQueue[lockShardGrain];
for (int i = 0 ; i < lockShardGrain ; i++) {
this.messages[i] = new ArrayBlockingQueue<Message>(queueSize);
}
this.processorWorkQueue = new ArrayBlockingQueue[workers];
for (int i = 0 ; i < workers ; i++) {
this.processorWorkQueue[i] = new ArrayBlockingQueue<Integer>(queueSize);
}
}
@Override
protected ProcessingTask createProcessingTask(int id) {
return new ProcessingTask(id);
}
StatsDBlockingProcessor(final StatsDBlockingProcessor processor)
throws Exception {
super(processor);
this.messages = new ArrayBlockingQueue[lockShardGrain];
for (int i = 0 ; i < lockShardGrain ; i++) {
this.messages[i] = new ArrayBlockingQueue<Message>(getQcapacity());
}
this.processorWorkQueue = new ArrayBlockingQueue[workers];
for (int i = 0 ; i < workers ; i++) {
this.processorWorkQueue[i] = new ArrayBlockingQueue<Integer>(getQcapacity());
}
}
@Override
protected boolean send(final Message message) {
try {
int threadId = getThreadId();
int shard = threadId % lockShardGrain;
int processQueue = threadId % workers;
if (!shutdown) {
messages[shard].put(message);
processorWorkQueue[processQueue].put(shard);
return true;
}
} catch (InterruptedException e) {
// NOTHING
}
return false;
}
}