-
Notifications
You must be signed in to change notification settings - Fork 6
/
CirconusSinkActor.java
623 lines (562 loc) · 25.6 KB
/
CirconusSinkActor.java
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
* Copyright 2014 Groupon.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arpnetworking.tsdcore.sinks.circonus;
import com.arpnetworking.logback.annotations.LogValue;
import com.arpnetworking.steno.LogValueMapFactory;
import com.arpnetworking.steno.Logger;
import com.arpnetworking.steno.LoggerFactory;
import com.arpnetworking.tsdcore.model.AggregatedData;
import com.arpnetworking.tsdcore.sinks.circonus.api.BrokerListResponse;
import com.arpnetworking.tsdcore.sinks.circonus.api.CheckBundle;
import com.arpnetworking.tsdcore.statistics.HistogramStatistic;
import com.arpnetworking.utility.partitioning.PartitionSet;
import com.google.common.collect.EvictingQueue;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import it.unimi.dsi.fastutil.doubles.Double2LongMap;
import org.apache.pekko.actor.AbstractActor;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.Props;
import org.apache.pekko.http.javadsl.model.StatusCodes;
import org.apache.pekko.pattern.PatternsCS;
import play.libs.ws.StandaloneWSResponse;
import scala.concurrent.ExecutionContextExecutor;
import scala.concurrent.duration.FiniteDuration;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.net.URI;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* Reports data to Circonus HttpTrap.
*
* This actor maintains a non-blocking HTTP client to Circonus internally. It is responsible for
* creating the necessary check bundles to post to and maintains a mapping of incoming aggregated data
* and the check bundles that accept that data.
*
* Messages:
* External -
* Aggregation - Sent to the actor to send AggregatedData to the sink.
* Internal -
* BrokerListResponse - Sent from the Circonus client. After receiving, used to decide which brokers to send
* the check bundle registrations for.
* ServiceCheckBinding - Sent internally after registration of a check bundle. The binding is stored internally
* to keep track of check bundle urls.
*
* @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
*/
@SuppressWarnings("deprecation")
public final class CirconusSinkActor extends AbstractActor {
/**
* Creates a {@link org.apache.pekko.actor.Props} for use in Pekko.
*
* @param client Circonus client
* @param broker Circonus broker to push to
* @param maximumConcurrency the maximum number of parallel metric submissions
* @param maximumQueueSize the maximum size of the pending metrics queue
* @param spreadDuration the maximum wait time before starting to send metrics
* @param enableHistograms true to turn on histogram publication
* @param partitionSet the partition set to partition the check bundles with
* @return A new {@link org.apache.pekko.actor.Props}
*/
public static Props props(
final CirconusClient client,
final String broker,
final int maximumConcurrency,
final int maximumQueueSize,
final Duration spreadDuration,
final boolean enableHistograms,
final PartitionSet partitionSet) {
return Props.create(
CirconusSinkActor.class,
client,
broker,
maximumConcurrency,
maximumQueueSize,
spreadDuration,
enableHistograms,
partitionSet);
}
/**
* Public constructor.
*
* @param client Circonus client
* @param broker Circonus broker to push to
* @param maximumConcurrency the maximum number of parallel metric submissions
* @param maximumQueueSize the maximum size of the pending metrics queue
* @param spreadDuration the maximum wait time before starting to send metrics
* @param enableHistograms true to turn on histogram publication
* @param partitionSet the partition set to partition the check bundles with
*/
@SuppressFBWarnings(value = "DMI_RANDOM_USED_ONLY_ONCE", justification = "Random is used to spread load, only used once is ok")
public CirconusSinkActor(
final CirconusClient client,
final String broker,
final int maximumConcurrency,
final int maximumQueueSize,
final Duration spreadDuration,
final boolean enableHistograms,
final PartitionSet partitionSet) {
_client = client;
_brokerName = broker;
_maximumConcurrency = maximumConcurrency;
_enableHistograms = enableHistograms;
_partitionSet = partitionSet;
_pendingRequests = EvictingQueue.create(maximumQueueSize);
if (Duration.ZERO.equals(spreadDuration)) {
_spreadingDelayMillis = 0;
} else {
_spreadingDelayMillis = new Random().nextInt((int) spreadDuration.toMillis());
}
_dispatcher = getContext().system().dispatcher();
context().actorOf(BrokerRefresher.props(_client), "broker-refresher");
_checkBundleRefresher = context().actorOf(CheckBundleActivator.props(_client), "check-bundle-refresher");
}
/**
* Generate a Steno log compatible representation.
*
* @return Steno log compatible representation.
*/
@LogValue
public Object toLogValue() {
return LogValueMapFactory.builder(this)
.put("actor", this.self())
.put("brokerName", _brokerName)
.build();
}
@Override
public String toString() {
return toLogValue().toString();
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(EmitAggregation.class, aggregation -> {
if (_selectedBrokerCid.isPresent()) {
final Collection<AggregatedData> data = aggregation.getData();
publish(data);
} else {
NO_BROKER_LOGGER.warn()
.setMessage("Unable to push data to Circonus")
.addData("reason", "desired broker not yet discovered")
.addData("actor", self())
.log();
}
})
.match(CheckBundleLookupResponse.class, response -> {
if (response.isSuccess()) {
_bundleMap.put(response.getKey(), response.getCheckBundle().getCid());
_checkBundles.put(response.getCheckBundle().getCid(), response.getCheckBundle());
_checkBundleRefresher.tell(
new CheckBundleActivator.NotifyCheckBundle(response.getCheckBundle()),
self());
} else {
LOGGER.error()
.setMessage("Error creating check bundle")
.addData("request", response.getCheckBundle())
.addData("actor", self())
.setThrowable(response.getCause().get())
.log();
_pendingLookups.remove(response.getKey());
}
})
.match(CheckBundleActivator.CheckBundleRefreshComplete.class, update -> {
_checkBundles.put(update.getCheckBundle().getCid(), update.getCheckBundle());
})
.match(BrokerRefresher.BrokerLookupComplete.class, this::handleBrokerLookupComplete)
.match(PostComplete.class, complete -> {
processCompletedRequest(complete);
dispatchPending();
})
.match(PostFailure.class, failure -> {
processFailedRequest(failure);
dispatchPending();
})
.match(WaitTimeExpired.class, message -> {
LOGGER.debug()
.setMessage("Received WaitTimeExpired message")
.addContext("actor", self())
.log();
_waiting = false;
dispatchPending();
})
.build();
}
private void handleBrokerLookupComplete(final BrokerRefresher.BrokerLookupComplete message) {
final BrokerListResponse response = message.getResponse();
final List<BrokerListResponse.Broker> brokers = response.getBrokers();
Optional<BrokerListResponse.Broker> selectedBroker = Optional.empty();
for (final BrokerListResponse.Broker broker : response.getBrokers()) {
if (broker.getName().equalsIgnoreCase(_brokerName)) {
selectedBroker = Optional.of(broker);
}
}
if (!selectedBroker.isPresent()) {
LOGGER.warn()
.setMessage("Broker list does not contain desired broker")
.addData("brokers", brokers)
.addData("desired", _brokerName)
.addData("actor", self())
.log();
} else {
LOGGER.info()
.setMessage("Broker list contains desired broker")
.addData("brokers", brokers)
.addData("desired", _brokerName)
.addData("actor", self())
.log();
_selectedBrokerCid = Optional.of(selectedBroker.get().getCid());
}
}
private void processCompletedRequest(final PostComplete complete) {
_inflightRequestsCount--;
final int responseStatusCode = complete.getResponse().getStatus();
if (responseStatusCode == StatusCodes.OK.intValue()) {
LOGGER.debug()
.setMessage("Data submission accepted")
.addData("status", responseStatusCode)
.addContext("actor", self())
.log();
} else {
LOGGER.warn()
.setMessage("Data submission rejected")
.addData("status", responseStatusCode)
.addContext("actor", self())
.log();
}
}
private void processFailedRequest(final PostFailure failure) {
_inflightRequestsCount--;
LOGGER.error()
.setMessage("Data submission error")
.addContext("actor", self())
.setThrowable(failure.getCause())
.log();
}
private Map<String, Object> serialize(final Collection<AggregatedData> data) {
final Map<String, Object> dataNode = Maps.newHashMap();
for (final AggregatedData aggregatedData : data) {
final String name = new StringBuilder()
.append(aggregatedData.getPeriod().toString())
.append("/")
.append(aggregatedData.getFQDSN().getMetric())
.append("/")
.append(aggregatedData.getFQDSN().getStatistic().getName())
.toString();
// For histograms, if they're enabled, we'll build the histogram data node
if (_enableHistograms && aggregatedData.getFQDSN().getStatistic() instanceof HistogramStatistic) {
final HistogramStatistic.HistogramSupportingData histogramSupportingData = (HistogramStatistic.HistogramSupportingData)
aggregatedData.getSupportingData();
final HistogramStatistic.HistogramSnapshot histogram = histogramSupportingData.getHistogramSnapshot();
final List<String> valueList = new ArrayList<>((int) histogram.getEntriesCount());
final MathContext context = new MathContext(2, RoundingMode.DOWN);
for (final Double2LongMap.Entry entry : histogram.getValues()) {
for (int i = 0; i < entry.getValue(); i++) {
final BigDecimal decimal = new BigDecimal(entry.getKey(), context);
final String bucketString = String.format("H[%s]=%d", decimal.toPlainString(), entry.getValue());
valueList.add(bucketString);
}
}
final Map<String, Object> histogramValueNode = Maps.newHashMap();
histogramValueNode.put("_type", "n"); // Histograms are type "n"
histogramValueNode.put("_value", valueList);
dataNode.put(name, histogramValueNode);
} else {
dataNode.put(name, aggregatedData.getValue().getValue());
}
}
return dataNode;
}
private String getMetricKey(final AggregatedData data) {
return String.format(
"%s_%s_%s_%s_%s_%s",
data.getFQDSN().getService(),
data.getFQDSN().getCluster(),
data.getHost(),
data.getPeriod().toString(),
data.getFQDSN().getMetric(),
data.getFQDSN().getStatistic().getName());
}
private String getCheckBundleKey(final AggregatedData data) {
final Integer partitionNumber = _partitionMap.get(getMetricKey(data));
return String.format(
"%s_%s_%s_%d",
data.getFQDSN().getService(),
data.getFQDSN().getCluster(),
data.getHost(),
partitionNumber);
}
private void registerMetricPartition(final AggregatedData data) {
final String metric = getMetricKey(data);
final Integer partition = _partitionMap.computeIfAbsent(
metric,
key -> _partitionSet.getOrCreatePartition(key).orElse(null));
if (partition == null) {
CANT_FIND_PARTITION_LOGGER.warn()
.setMessage("Cannot find or create partition for check bundle")
.addData("actor", self())
.addData("metric", metric)
.log();
}
}
/**
* Queues the messages for transmission.
*/
private void publish(final Collection<AggregatedData> data) {
final Map<String, List<AggregatedData>> dataMap = data.stream()
// First, we need to make sure that the partitions for each of metrics has been registered
.peek(this::registerMetricPartition)
// Collect the aggregated data by the "key". In this case the key is unique part of a check_bundle:
// service, cluster, host, and partition number
.collect(Collectors.groupingBy(this::getCheckBundleKey));
final boolean pendingWasEmpty = _pendingRequests.isEmpty();
final List<RequestQueueEntry> toQueue = Lists.newArrayList();
for (final Map.Entry<String, List<AggregatedData>> entry : dataMap.entrySet()) {
final String targetKey = entry.getKey();
final Collection<AggregatedData> serviceData = entry.getValue();
// Check to see if we already have a checkbundle for this metric
final String bundleCid = _bundleMap.get(targetKey);
if (bundleCid != null) {
final CheckBundle binding = _checkBundles.get(bundleCid);
// Queue the request(s)
toQueue.add(new RequestQueueEntry(binding, serialize(serviceData)));
} else {
if (!_pendingLookups.contains(targetKey)) {
// We don't have an outstanding request to lookup the URI, create one.
final AggregatedData aggregatedData = Iterables.get(serviceData, 0);
_pendingLookups.add(targetKey);
final CompletionStage<CheckBundleLookupResponse> response = createCheckBundle(targetKey, aggregatedData);
// Send the completed, mapped response back to ourselves.
PatternsCS.pipe(response, _dispatcher).to(self());
}
// We can't send the request to it right now, skip this service
NO_CHECK_BUNDLE_LOGGER.warn()
.setMessage("Unable to push data to Circonus")
.addData("reason", "check bundle not yet found or created")
.addData("actor", self())
.log();
}
}
final int evicted = Math.max(0, toQueue.size() - _pendingRequests.remainingCapacity());
_pendingRequests.addAll(toQueue);
if (evicted > 0) {
LOGGER.warn()
.setMessage("Evicted data from Circonus sink queue")
.addData("count", evicted)
.addContext("actor", self())
.log();
}
// If we don't currently have anything in-flight, we'll need to wait the spreading duration.
// If we're already waiting, these requests will be sent after the waiting is over, no need to do anything else.
if (pendingWasEmpty && !_waiting && _spreadingDelayMillis > 0) {
_waiting = true;
LOGGER.debug()
.setMessage("Scheduling http requests for later transmission")
.addData("delayMs", _spreadingDelayMillis)
.addContext("actor", self())
.log();
context().system().scheduler().scheduleOnce(
FiniteDuration.apply(_spreadingDelayMillis, TimeUnit.MILLISECONDS),
self(),
new WaitTimeExpired(),
context().dispatcher(),
self());
} else {
dispatchPending();
}
}
/**
* Dispatches the number of pending requests needed to drain the pendingRequests queue or meet the maximum concurrency.
*/
private void dispatchPending() {
LOGGER.debug()
.setMessage("Dispatching requests")
.addContext("actor", self())
.log();
while (_inflightRequestsCount < _maximumConcurrency && !_pendingRequests.isEmpty()) {
fireNextRequest();
}
}
private void fireNextRequest() {
final RequestQueueEntry request = _pendingRequests.poll();
if (request != null) {
_inflightRequestsCount++;
final CompletionStage<Object> responsePromise = _client.sendToHttpTrap(
request.getData(),
request.getBinding().getSubmissionUrl())
.<Object>thenApply(PostComplete::new)
.exceptionally(PostFailure::new);
PatternsCS.pipe(responsePromise, context().dispatcher()).to(self());
}
}
private CompletionStage<CheckBundleLookupResponse> createCheckBundle(
final String targetKey,
final AggregatedData aggregatedData) {
final Integer partition = _partitionMap.get(getMetricKey(aggregatedData));
final CheckBundle request = new CheckBundle.Builder()
.addBroker(_selectedBrokerCid.get())
.addTag("monitoring_agent:aint")
.addTag(String.format("monitoring_cluster:%s", aggregatedData.getFQDSN().getCluster()))
.addTag(String.format("service:%s", aggregatedData.getFQDSN().getService()))
.addTag(String.format("hostname:%s", aggregatedData.getHost()))
.setTarget(aggregatedData.getHost())
.setDisplayName(
String.format(
"%s/%s/%s",
aggregatedData.getFQDSN().getCluster(),
aggregatedData.getFQDSN().getService(),
partition))
.setStatus("active")
.build();
// Map the response to a ServiceCheckBinding
return _client.getOrCreateCheckBundle(request)
.thenApply(
response -> {
final URI result;
result = response.getSubmissionUrl();
return CheckBundleLookupResponse.success(targetKey, response);
})
.exceptionally(
failure -> CheckBundleLookupResponse.failure(targetKey, failure, request));
}
private Optional<String> _selectedBrokerCid = Optional.empty();
private ActorRef _checkBundleRefresher;
private int _inflightRequestsCount = 0;
private boolean _waiting = false;
private final ExecutionContextExecutor _dispatcher;
private final Set<String> _pendingLookups = Sets.newHashSet();
private final Map<String, String> _bundleMap = Maps.newHashMap(); // Holds check bundle name -> cid mapping
private final Map<String, Integer> _partitionMap = Maps.newHashMap(); // Holds metric name -> partition number
private final Map<String, CheckBundle> _checkBundles = Maps.newHashMap(); // Holds cid -> check bundle details
private final CirconusClient _client;
private final String _brokerName;
private final int _maximumConcurrency;
private final boolean _enableHistograms;
private final PartitionSet _partitionSet;
private final int _spreadingDelayMillis;
private final EvictingQueue<RequestQueueEntry> _pendingRequests;
private static final Logger LOGGER =
LoggerFactory.getLogger(CirconusSinkActor.class);
private static final Logger NO_BROKER_LOGGER =
LoggerFactory.getRateLimitLogger(CirconusSinkActor.class, Duration.ofSeconds(30));
private static final Logger NO_CHECK_BUNDLE_LOGGER =
LoggerFactory.getRateLimitLogger(CirconusSinkActor.class, Duration.ofSeconds(30));
private static final Logger CANT_FIND_PARTITION_LOGGER =
LoggerFactory.getRateLimitLogger(CirconusSinkActor.class, Duration.ofSeconds(30));
/**
* Message class to wrap a list of {@link com.arpnetworking.tsdcore.model.AggregatedData}.
*/
public static final class EmitAggregation {
/**
* Public constructor.
* @param data Data to emit.
*/
public EmitAggregation(final Collection<AggregatedData> data) {
_data = Lists.newArrayList(data);
}
public Collection<AggregatedData> getData() {
return Collections.unmodifiableList(_data);
}
private final List<AggregatedData> _data;
}
private static final class CheckBundleLookupResponse {
public static CheckBundleLookupResponse success(final String key, final CheckBundle checkBundle) {
return new CheckBundleLookupResponse(key, Optional.<Throwable>empty(), checkBundle);
}
public static CheckBundleLookupResponse failure(final String key, final Throwable throwable, final CheckBundle request) {
return new CheckBundleLookupResponse(key, Optional.of(throwable), request);
}
private CheckBundleLookupResponse(
final String key,
final Optional<Throwable> cause,
final CheckBundle request) {
_key = key;
_cause = cause;
_checkBundle = request;
}
public boolean isSuccess() {
return !_cause.isPresent();
}
public boolean isFailed() {
return _cause.isPresent();
}
public Optional<Throwable> getCause() {
return _cause;
}
public String getKey() {
return _key;
}
public CheckBundle getCheckBundle() {
return _checkBundle;
}
private final String _key;
private final Optional<Throwable> _cause;
private final CheckBundle _checkBundle;
}
private static final class RequestQueueEntry {
private RequestQueueEntry(final CheckBundle binding, final Map<String, Object> data) {
_binding = binding;
_data = data;
}
public CheckBundle getBinding() {
return _binding;
}
public Map<String, Object> getData() {
return _data;
}
private final CheckBundle _binding;
private final Map<String, Object> _data;
}
/**
* Message class to wrap a completed HTTP request.
*/
private static final class PostFailure {
private PostFailure(final Throwable throwable) {
_throwable = throwable;
}
public Throwable getCause() {
return _throwable;
}
private final Throwable _throwable;
}
/**
* Message class to wrap an errored HTTP request.
*/
private static final class PostComplete {
private PostComplete(final StandaloneWSResponse response) {
_response = response;
}
public StandaloneWSResponse getResponse() {
return _response;
}
private final StandaloneWSResponse _response;
}
private static final class WaitTimeExpired {}
}