|
15 | 15 | */
|
16 | 16 | package com.arpnetworking.clusteraggregator.aggregation;
|
17 | 17 |
|
18 |
| -import akka.actor.AbstractActor; |
| 18 | +import akka.actor.AbstractActorWithTimers; |
19 | 19 | import akka.actor.ActorRef;
|
20 | 20 | import akka.actor.Props;
|
21 | 21 | import akka.actor.ReceiveTimeout;
|
22 |
| -import akka.actor.Scheduler; |
23 | 22 | import akka.cluster.sharding.ShardRegion;
|
24 | 23 | import com.arpnetworking.clusteraggregator.AggregatorLifecycle;
|
25 | 24 | import com.arpnetworking.clusteraggregator.models.CombinedMetricData;
|
|
58 | 57 | *
|
59 | 58 | * @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
|
60 | 59 | */
|
61 |
| -public class StreamingAggregator extends AbstractActor { |
| 60 | +public class StreamingAggregator extends AbstractActorWithTimers { |
62 | 61 |
|
63 | 62 | /**
|
64 | 63 | * Creates a <code>Props</code> for use in Akka.
|
@@ -96,21 +95,9 @@ public StreamingAggregator(
|
96 | 95 | _clusterHostSuffix = clusterHostSuffix;
|
97 | 96 | context().setReceiveTimeout(FiniteDuration.apply(30, TimeUnit.MINUTES));
|
98 | 97 |
|
99 |
| - final Scheduler scheduler = getContext().system().scheduler(); |
100 |
| - scheduler.schedule( |
101 |
| - FiniteDuration.apply(5, TimeUnit.SECONDS), |
102 |
| - FiniteDuration.apply(5, TimeUnit.SECONDS), |
103 |
| - getSelf(), |
104 |
| - new BucketCheck(), |
105 |
| - getContext().dispatcher(), |
106 |
| - getSelf()); |
107 |
| - scheduler.schedule( |
108 |
| - FiniteDuration.apply(5, TimeUnit.SECONDS), |
109 |
| - FiniteDuration.apply(1, TimeUnit.HOURS), |
110 |
| - getSelf(), |
111 |
| - new UpdateBookkeeper(), |
112 |
| - getContext().dispatcher(), |
113 |
| - getSelf()); |
| 98 | + timers().startPeriodicTimer(BUCKET_CHECK_TIMER_KEY, BucketCheck.getInstance(), FiniteDuration.apply(5, TimeUnit.SECONDS)); |
| 99 | + timers().startSingleTimer(BOOKKEEPER_UPDATE_TIMER_KEY, UpdateBookkeeper.getInstance(), FiniteDuration.apply(5, TimeUnit.SECONDS)); |
| 100 | + |
114 | 101 | _emitter = emitter;
|
115 | 102 | }
|
116 | 103 |
|
@@ -188,10 +175,12 @@ public Receive createReceive() {
|
188 | 175 | }
|
189 | 176 | _statistics.clear();
|
190 | 177 | }
|
| 178 | + timers().startSingleTimer(BOOKKEEPER_UPDATE_TIMER_KEY, UpdateBookkeeper.getInstance(), |
| 179 | + FiniteDuration.apply(1, TimeUnit.HOURS)); |
191 | 180 | })
|
192 | 181 | .match(ShutdownAggregator.class, message -> context().stop(self()))
|
193 | 182 | .match(ReceiveTimeout.class, message -> {
|
194 |
| - getContext().parent().tell(new ShardRegion.Passivate(new ShutdownAggregator()), getSelf()); |
| 183 | + getContext().parent().tell(new ShardRegion.Passivate(ShutdownAggregator.getInstance()), getSelf()); |
195 | 184 | })
|
196 | 185 | .build();
|
197 | 186 | }
|
@@ -332,16 +321,48 @@ private String createHost() {
|
332 | 321 | !(entry.getKey().equals(CombinedMetricData.CLUSTER_KEY)
|
333 | 322 | || entry.getKey().equals(CombinedMetricData.HOST_KEY)
|
334 | 323 | || entry.getKey().equals(CombinedMetricData.SERVICE_KEY));
|
| 324 | + private static final String BUCKET_CHECK_TIMER_KEY = "bucketcheck"; |
| 325 | + private static final String BOOKKEEPER_UPDATE_TIMER_KEY = "updatebookkeeper"; |
335 | 326 |
|
336 | 327 | private static final class BucketCheck implements Serializable {
|
| 328 | + /** |
| 329 | + * Gets the singleton instance. |
| 330 | + * |
| 331 | + * @return singleton instance |
| 332 | + */ |
| 333 | + public static BucketCheck getInstance() { |
| 334 | + return INSTANCE; |
| 335 | + } |
| 336 | + |
| 337 | + private static final BucketCheck INSTANCE = new BucketCheck(); |
337 | 338 | private static final long serialVersionUID = 1L;
|
338 | 339 | }
|
339 | 340 |
|
340 | 341 | private static final class UpdateBookkeeper implements Serializable {
|
| 342 | + /** |
| 343 | + * Gets the singleton instance. |
| 344 | + * |
| 345 | + * @return singleton instance |
| 346 | + */ |
| 347 | + public static UpdateBookkeeper getInstance() { |
| 348 | + return INSTANCE; |
| 349 | + } |
| 350 | + |
| 351 | + private static final UpdateBookkeeper INSTANCE = new UpdateBookkeeper(); |
341 | 352 | private static final long serialVersionUID = 1L;
|
342 | 353 | }
|
343 | 354 |
|
344 | 355 | private static final class ShutdownAggregator implements Serializable {
|
| 356 | + /** |
| 357 | + * Gets the singleton instance. |
| 358 | + * |
| 359 | + * @return singleton instance |
| 360 | + */ |
| 361 | + public static ShutdownAggregator getInstance() { |
| 362 | + return INSTANCE; |
| 363 | + } |
| 364 | + |
| 365 | + private static final ShutdownAggregator INSTANCE = new ShutdownAggregator(); |
345 | 366 | private static final long serialVersionUID = 1L;
|
346 | 367 | }
|
347 | 368 | }
|
0 commit comments