Use ReusableNormalizedNodePruner in PruningDataTreeModification
[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(final ActorSystem.Settings settings, final 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, final 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 static void monitorQueueSize(final 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(final scala.Option<ActorRef> owner, final String metricName,
57             final T metric) {
58         if (owner.isEmpty()) {
59            // there's no actor to monitor
60             return;
61         }
62
63         String actorName = owner.get().path().toStringWithoutAddress();
64         String fullName = MetricRegistry.name(actorName, metricName);
65
66         MetricRegistry registry = MetricsReporter.getInstance(MeteringBehavior.DOMAIN).getMetricsRegistry();
67
68         if (registry.getMetrics().containsKey(fullName)) {
69             // already registered
70             return;
71         }
72
73         try {
74             registry.register(fullName, metric);
75         } catch (IllegalArgumentException e) {
76             // already registered - shouldn't happen here since we check above...
77             LOG.debug("Unable to register '{}' in metrics registry: {}", e);
78         }
79     }
80
81     public static class MeteredMessageQueue extends BoundedDequeBasedMailbox.MessageQueue {
82         private static final long serialVersionUID = 1L;
83
84         public MeteredMessageQueue(final int capacity, final FiniteDuration pushTimeOut) {
85             super(capacity, pushTimeOut);
86         }
87     }
88 }
89

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