Small fix to xsql dependencies
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / 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.common.actor;
10
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;
24
25 import java.util.concurrent.TimeUnit;
26
27 public class MeteredBoundedMailbox implements MailboxType, ProducesMessageQueue<BoundedMailbox.MessageQueue> {
28
29     private MeteredMessageQueue queue;
30     private Integer capacity;
31     private FiniteDuration pushTimeOut;
32     private ActorPath actorPath;
33     private MetricsReporter reporter;
34
35     private final String QUEUE_SIZE = "queue-size";
36     private final Long DEFAULT_TIMEOUT = 10L;
37
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");
42
43         Long timeout = -1L;
44         if ( config.hasPath("mailbox-push-timeout-time") ){
45             timeout = config.getDuration("mailbox-push-timeout-time", TimeUnit.NANOSECONDS);
46         } else {
47             timeout = DEFAULT_TIMEOUT;
48         }
49         Preconditions.checkArgument( timeout > 0, "mailbox-push-timeout-time must be > 0");
50         this.pushTimeOut = new FiniteDuration(timeout, TimeUnit.NANOSECONDS);
51
52         reporter = MetricsReporter.getInstance();
53     }
54
55
56     @Override
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);
60         return this.queue;
61     }
62
63     private void monitorQueueSize(scala.Option<ActorRef> owner, final MeteredMessageQueue monitoredQueue) {
64         if (owner.isEmpty()) {
65             return; //there's no actor to monitor
66         }
67         actorPath = owner.get().path();
68         MetricRegistry registry = reporter.getMetricsRegistry();
69
70         String actorName = registry.name(actorPath.toString(), QUEUE_SIZE);
71
72         if (registry.getMetrics().containsKey(actorName))
73             return; //already registered
74
75         reporter.getMetricsRegistry().register(actorName,
76                 new Gauge<Integer>() {
77                     @Override
78                     public Integer getValue() {
79                         return monitoredQueue.size();
80                     }
81                 });
82     }
83
84
85     public static class MeteredMessageQueue extends BoundedMailbox.MessageQueue {
86
87         public MeteredMessageQueue(int capacity, FiniteDuration pushTimeOut) {
88             super(capacity, pushTimeOut);
89         }
90     }
91
92 }
93