2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.common.actor;
11 import akka.actor.ActorPath;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSystem;
14 import akka.dispatch.BoundedDequeBasedMailbox;
15 import akka.dispatch.MailboxType;
16 import akka.dispatch.ProducesMessageQueue;
17 import com.codahale.metrics.Gauge;
18 import com.codahale.metrics.MetricRegistry;
19 import com.google.common.base.Preconditions;
20 import com.typesafe.config.Config;
21 import org.opendaylight.controller.common.reporting.MetricsReporter;
22 import scala.concurrent.duration.FiniteDuration;
24 import java.util.concurrent.TimeUnit;
26 public class MeteredBoundedMailbox implements MailboxType, ProducesMessageQueue<MeteredBoundedMailbox.MeteredMessageQueue> {
28 private MeteredMessageQueue queue;
29 private Integer capacity;
30 private FiniteDuration pushTimeOut;
31 private ActorPath actorPath;
32 private MetricsReporter reporter;
34 private final String QUEUE_SIZE = "queue-size";
35 private final String CAPACITY = "mailbox-capacity";
36 private final String TIMEOUT = "mailbox-push-timeout-time";
37 private final Long DEFAULT_TIMEOUT = 10L;
39 public MeteredBoundedMailbox(ActorSystem.Settings settings, Config config) {
40 Preconditions.checkArgument( config.hasPath("mailbox-capacity"), "Missing configuration [mailbox-capacity]" );
41 this.capacity = config.getInt(CAPACITY);
42 Preconditions.checkArgument( this.capacity > 0, "mailbox-capacity must be > 0");
45 if ( config.hasPath(TIMEOUT) ){
46 timeout = config.getDuration(TIMEOUT, TimeUnit.NANOSECONDS);
48 timeout = DEFAULT_TIMEOUT;
50 Preconditions.checkArgument( timeout > 0, "mailbox-push-timeout-time must be > 0");
51 this.pushTimeOut = new FiniteDuration(timeout, TimeUnit.NANOSECONDS);
53 reporter = MetricsReporter.getInstance();
58 public MeteredMessageQueue create(final scala.Option<ActorRef> owner, scala.Option<ActorSystem> system) {
59 this.queue = new MeteredMessageQueue(this.capacity, this.pushTimeOut);
60 monitorQueueSize(owner, this.queue);
64 private void monitorQueueSize(scala.Option<ActorRef> owner, final MeteredMessageQueue monitoredQueue) {
65 if (owner.isEmpty()) {
66 return; //there's no actor to monitor
68 actorPath = owner.get().path();
69 String actorInstanceId = Integer.toString(owner.get().hashCode());
71 MetricRegistry registry = reporter.getMetricsRegistry();
72 String actorName = registry.name(actorPath.toString(), actorInstanceId, QUEUE_SIZE);
74 if (registry.getMetrics().containsKey(actorName))
75 return; //already registered
77 registry.register(actorName,
78 new Gauge<Integer>() {
80 public Integer getValue() {
81 return monitoredQueue.size();
87 public static class MeteredMessageQueue extends BoundedDequeBasedMailbox.MessageQueue {
89 public MeteredMessageQueue(int capacity, FiniteDuration pushTimeOut) {
90 super(capacity, pushTimeOut);