258672114a9ac2ccce436b8623e4fd3f209796bb
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / common / actor / MeteredBoundedMailbox.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.common.actor;
10
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.dispatch.BoundedDequeBasedMailbox;
14 import akka.dispatch.MailboxType;
15 import akka.dispatch.ProducesMessageQueue;
16 import com.codahale.metrics.Gauge;
17 import com.codahale.metrics.Metric;
18 import com.codahale.metrics.MetricRegistry;
19 import com.typesafe.config.Config;
20 import org.opendaylight.controller.cluster.reporting.MetricsReporter;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import scala.concurrent.duration.FiniteDuration;
24
25 public class MeteredBoundedMailbox implements MailboxType,
26         ProducesMessageQueue<MeteredBoundedMailbox.MeteredMessageQueue> {
27     private static final Logger LOG = LoggerFactory.getLogger(MeteredBoundedMailbox.class);
28     private static final String QUEUE_SIZE = "q-size";
29
30     private final Integer capacity;
31     private final FiniteDuration pushTimeOut;
32
33     public MeteredBoundedMailbox(ActorSystem.Settings settings, Config config) {
34
35         CommonConfig commonConfig = new CommonConfig(settings.config());
36         this.capacity = commonConfig.getMailBoxCapacity();
37         this.pushTimeOut = commonConfig.getMailBoxPushTimeout();
38     }
39
40
41     @Override
42     public MeteredMessageQueue create(final scala.Option<ActorRef> owner, scala.Option<ActorSystem> system) {
43         final MeteredMessageQueue queue = new MeteredMessageQueue(this.capacity, this.pushTimeOut);
44         monitorQueueSize(owner, queue);
45         return queue;
46     }
47
48     private void monitorQueueSize(scala.Option<ActorRef> owner, final MeteredMessageQueue monitoredQueue) {
49         registerMetric(owner, QUEUE_SIZE, getQueueSizeGuage(monitoredQueue));
50     }
51
52     private static Gauge<Integer> getQueueSizeGuage(final MeteredMessageQueue monitoredQueue) {
53         return monitoredQueue::size;
54     }
55
56     static <T extends Metric> void registerMetric(scala.Option<ActorRef> owner, String metricName, T metric) {
57         if (owner.isEmpty()) {
58            // there's no actor to monitor
59             return;
60         }
61
62         String actorName = owner.get().path().toStringWithoutAddress();
63         String fullName = MetricRegistry.name(actorName, metricName);
64
65         MetricRegistry registry = MetricsReporter.getInstance(MeteringBehavior.DOMAIN).getMetricsRegistry();
66
67         if (registry.getMetrics().containsKey(fullName)) {
68             // already registered
69             return;
70         }
71
72         try {
73             registry.register(fullName, metric);
74         } catch (IllegalArgumentException e) {
75             // already registered - shouldn't happen here since we check above...
76             LOG.debug("Unable to register '{}' in metrics registry: {}", e);
77         }
78     }
79
80     public static class MeteredMessageQueue extends BoundedDequeBasedMailbox.MessageQueue {
81         private static final long serialVersionUID = 1L;
82
83         public MeteredMessageQueue(int capacity, FiniteDuration pushTimeOut) {
84             super(capacity, pushTimeOut);
85         }
86     }
87 }
88

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.