Make private methods static
[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.MetricRegistry;
18 import com.typesafe.config.Config;
19 import org.opendaylight.controller.cluster.reporting.MetricsReporter;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import scala.concurrent.duration.FiniteDuration;
23
24 public class MeteredBoundedMailbox implements MailboxType, ProducesMessageQueue<MeteredBoundedMailbox.MeteredMessageQueue> {
25
26     private final Logger LOG = LoggerFactory.getLogger(MeteredBoundedMailbox.class);
27
28     private MeteredMessageQueue queue;
29     private final Integer capacity;
30     private final FiniteDuration pushTimeOut;
31     private final MetricRegistry registry;
32
33     private final String QUEUE_SIZE = "q-size";
34
35     public MeteredBoundedMailbox(ActorSystem.Settings settings, Config config) {
36
37         CommonConfig commonConfig = new CommonConfig(settings.config());
38         this.capacity = commonConfig.getMailBoxCapacity();
39         this.pushTimeOut = commonConfig.getMailBoxPushTimeout();
40
41         MetricsReporter reporter = MetricsReporter.getInstance(MeteringBehavior.DOMAIN);
42         registry = reporter.getMetricsRegistry();
43     }
44
45
46     @Override
47     public MeteredMessageQueue create(final scala.Option<ActorRef> owner, scala.Option<ActorSystem> system) {
48         this.queue = new MeteredMessageQueue(this.capacity, this.pushTimeOut);
49         monitorQueueSize(owner, this.queue);
50         return this.queue;
51     }
52
53     private void monitorQueueSize(scala.Option<ActorRef> owner, final MeteredMessageQueue monitoredQueue) {
54         if (owner.isEmpty()) {
55             return; //there's no actor to monitor
56         }
57         String actorName = owner.get().path().toStringWithoutAddress();
58         String metricName = MetricRegistry.name(actorName, QUEUE_SIZE);
59
60         if (registry.getMetrics().containsKey(metricName))
61         {
62             return; //already registered
63         }
64
65         Gauge<Integer> queueSize = getQueueSizeGuage(monitoredQueue);
66         registerQueueSizeMetric(metricName, queueSize);
67     }
68
69
70     public static class MeteredMessageQueue extends BoundedDequeBasedMailbox.MessageQueue {
71         private static final long serialVersionUID = 1L;
72
73         public MeteredMessageQueue(int capacity, FiniteDuration pushTimeOut) {
74             super(capacity, pushTimeOut);
75         }
76     }
77
78     private static Gauge<Integer> getQueueSizeGuage(final MeteredMessageQueue monitoredQueue ){
79         return new Gauge<Integer>() {
80             @Override
81             public Integer getValue() {
82                 return monitoredQueue.size();
83             }
84         };
85     }
86
87     private void registerQueueSizeMetric(String metricName, Gauge<Integer> metric){
88         try {
89             registry.register(metricName,metric);
90         } catch (IllegalArgumentException e) {
91             LOG.warn("Unable to register queue size in metrics registry. Failed with exception {}. ", e);
92         }
93     }
94 }
95

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