Bug 5740: Add Deque-based control-aware mailbox
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / common / actor / MeteredBoundedMailbox.java
index 31feac2..2586721 100644 (file)
@@ -14,6 +14,7 @@ import akka.dispatch.BoundedDequeBasedMailbox;
 import akka.dispatch.MailboxType;
 import akka.dispatch.ProducesMessageQueue;
 import com.codahale.metrics.Gauge;
+import com.codahale.metrics.Metric;
 import com.codahale.metrics.MetricRegistry;
 import com.typesafe.config.Config;
 import org.opendaylight.controller.cluster.reporting.MetricsReporter;
@@ -28,16 +29,12 @@ public class MeteredBoundedMailbox implements MailboxType,
 
     private final Integer capacity;
     private final FiniteDuration pushTimeOut;
-    private final MetricRegistry registry;
 
     public MeteredBoundedMailbox(ActorSystem.Settings settings, Config config) {
 
         CommonConfig commonConfig = new CommonConfig(settings.config());
         this.capacity = commonConfig.getMailBoxCapacity();
         this.pushTimeOut = commonConfig.getMailBoxPushTimeout();
-
-        MetricsReporter reporter = MetricsReporter.getInstance(MeteringBehavior.DOMAIN);
-        registry = reporter.getMetricsRegistry();
     }
 
 
@@ -49,21 +46,37 @@ public class MeteredBoundedMailbox implements MailboxType,
     }
 
     private void monitorQueueSize(scala.Option<ActorRef> owner, final MeteredMessageQueue monitoredQueue) {
+        registerMetric(owner, QUEUE_SIZE, getQueueSizeGuage(monitoredQueue));
+    }
+
+    private static Gauge<Integer> getQueueSizeGuage(final MeteredMessageQueue monitoredQueue) {
+        return monitoredQueue::size;
+    }
+
+    static <T extends Metric> void registerMetric(scala.Option<ActorRef> owner, String metricName, T metric) {
         if (owner.isEmpty()) {
-            return; //there's no actor to monitor
+           // there's no actor to monitor
+            return;
         }
+
         String actorName = owner.get().path().toStringWithoutAddress();
-        String metricName = MetricRegistry.name(actorName, QUEUE_SIZE);
+        String fullName = MetricRegistry.name(actorName, metricName);
+
+        MetricRegistry registry = MetricsReporter.getInstance(MeteringBehavior.DOMAIN).getMetricsRegistry();
 
-        if (registry.getMetrics().containsKey(metricName)) {
-            return; //already registered
+        if (registry.getMetrics().containsKey(fullName)) {
+            // already registered
+            return;
         }
 
-        Gauge<Integer> queueSize = getQueueSizeGuage(monitoredQueue);
-        registerQueueSizeMetric(metricName, queueSize);
+        try {
+            registry.register(fullName, metric);
+        } catch (IllegalArgumentException e) {
+            // already registered - shouldn't happen here since we check above...
+            LOG.debug("Unable to register '{}' in metrics registry: {}", e);
+        }
     }
 
-
     public static class MeteredMessageQueue extends BoundedDequeBasedMailbox.MessageQueue {
         private static final long serialVersionUID = 1L;
 
@@ -71,17 +84,5 @@ public class MeteredBoundedMailbox implements MailboxType,
             super(capacity, pushTimeOut);
         }
     }
-
-    private static Gauge<Integer> getQueueSizeGuage(final MeteredMessageQueue monitoredQueue) {
-        return monitoredQueue::size;
-    }
-
-    private void registerQueueSizeMetric(String metricName, Gauge<Integer> metric) {
-        try {
-            registry.register(metricName,metric);
-        } catch (IllegalArgumentException e) {
-            LOG.warn("Unable to register queue size in metrics registry. Failed with exception {}. ", e);
-        }
-    }
 }
 

©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.