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.BoundedMailbox;
15 import akka.dispatch.MailboxType;
16 import akka.dispatch.MessageQueue;
17 import akka.dispatch.ProducesMessageQueue;
18 import com.codahale.metrics.Gauge;
19 import com.codahale.metrics.MetricRegistry;
20 import com.google.common.base.Preconditions;
21 import com.typesafe.config.Config;
22 import org.opendaylight.controller.common.reporting.MetricsReporter;
23 import scala.concurrent.duration.FiniteDuration;
25 import java.util.concurrent.TimeUnit;
27 public class MeteredBoundedMailbox implements MailboxType, ProducesMessageQueue<BoundedMailbox.MessageQueue> {
29 private MeteredMessageQueue queue;
30 private Integer capacity;
31 private FiniteDuration pushTimeOut;
32 private ActorPath actorPath;
33 private MetricsReporter reporter;
35 private final String QUEUE_SIZE = "queue-size";
36 private final Long DEFAULT_TIMEOUT = 10L;
38 public MeteredBoundedMailbox(ActorSystem.Settings settings, Config config) {
39 Preconditions.checkArgument( config.hasPath("mailbox-capacity"), "Missing configuration [mailbox-capacity]" );
40 this.capacity = config.getInt("mailbox-capacity");
41 Preconditions.checkArgument( this.capacity > 0, "mailbox-capacity must be > 0");
44 if ( config.hasPath("mailbox-push-timeout-time") ){
45 timeout = config.getDuration("mailbox-push-timeout-time", TimeUnit.NANOSECONDS);
47 timeout = DEFAULT_TIMEOUT;
49 Preconditions.checkArgument( timeout > 0, "mailbox-push-timeout-time must be > 0");
50 this.pushTimeOut = new FiniteDuration(timeout, TimeUnit.NANOSECONDS);
52 reporter = MetricsReporter.getInstance();
57 public MessageQueue create(final scala.Option<ActorRef> owner, scala.Option<ActorSystem> system) {
58 this.queue = new MeteredMessageQueue(this.capacity, this.pushTimeOut);
59 monitorQueueSize(owner, this.queue);
63 private void monitorQueueSize(scala.Option<ActorRef> owner, final MeteredMessageQueue monitoredQueue) {
64 if (owner.isEmpty()) {
65 return; //there's no actor to monitor
67 actorPath = owner.get().path();
68 MetricRegistry registry = reporter.getMetricsRegistry();
70 String actorName = registry.name(actorPath.toString(), QUEUE_SIZE);
72 if (registry.getMetrics().containsKey(actorName))
73 return; //already registered
75 reporter.getMetricsRegistry().register(actorName,
76 new Gauge<Integer>() {
78 public Integer getValue() {
79 return monitoredQueue.size();
85 public static class MeteredMessageQueue extends BoundedMailbox.MessageQueue {
87 public MeteredMessageQueue(int capacity, FiniteDuration pushTimeOut) {
88 super(capacity, pushTimeOut);