Merge "Bug 1569 - [DEV] Too small variable for OF-Port-Number"
[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.BoundedDequeBasedMailbox;
15 import akka.dispatch.MailboxType;
16 import akka.dispatch.ProducesMessageQueue;
17 import com.codahale.metrics.Gauge;
18 import com.codahale.metrics.MetricRegistry;
19 import com.google.common.base.Preconditions;
20 import com.typesafe.config.Config;
21 import org.opendaylight.controller.common.reporting.MetricsReporter;
22 import scala.concurrent.duration.FiniteDuration;
23
24 import java.util.concurrent.TimeUnit;
25
26 public class MeteredBoundedMailbox implements MailboxType, ProducesMessageQueue<MeteredBoundedMailbox.MeteredMessageQueue> {
27
28     private MeteredMessageQueue queue;
29     private Integer capacity;
30     private FiniteDuration pushTimeOut;
31     private ActorPath actorPath;
32     private MetricsReporter reporter;
33
34     private final String QUEUE_SIZE = "queue-size";
35     private final String CAPACITY = "mailbox-capacity";
36     private final String TIMEOUT  = "mailbox-push-timeout-time";
37     private final Long DEFAULT_TIMEOUT = 10L;
38
39     public MeteredBoundedMailbox(ActorSystem.Settings settings, Config config) {
40         Preconditions.checkArgument( config.hasPath("mailbox-capacity"), "Missing configuration [mailbox-capacity]" );
41         this.capacity = config.getInt(CAPACITY);
42         Preconditions.checkArgument( this.capacity > 0, "mailbox-capacity must be > 0");
43
44         Long timeout = -1L;
45         if ( config.hasPath(TIMEOUT) ){
46             timeout = config.getDuration(TIMEOUT, TimeUnit.NANOSECONDS);
47         } else {
48             timeout = DEFAULT_TIMEOUT;
49         }
50         Preconditions.checkArgument( timeout > 0, "mailbox-push-timeout-time must be > 0");
51         this.pushTimeOut = new FiniteDuration(timeout, TimeUnit.NANOSECONDS);
52
53         reporter = MetricsReporter.getInstance();
54     }
55
56
57     @Override
58     public MeteredMessageQueue create(final scala.Option<ActorRef> owner, scala.Option<ActorSystem> system) {
59         this.queue = new MeteredMessageQueue(this.capacity, this.pushTimeOut);
60         monitorQueueSize(owner, this.queue);
61         return this.queue;
62     }
63
64     private void monitorQueueSize(scala.Option<ActorRef> owner, final MeteredMessageQueue monitoredQueue) {
65         if (owner.isEmpty()) {
66             return; //there's no actor to monitor
67         }
68         actorPath = owner.get().path();
69         String actorInstanceId = Integer.toString(owner.get().hashCode());
70
71         MetricRegistry registry = reporter.getMetricsRegistry();
72         String actorName = registry.name(actorPath.toString(), actorInstanceId, QUEUE_SIZE);
73
74         if (registry.getMetrics().containsKey(actorName))
75             return; //already registered
76
77         registry.register(actorName,
78                 new Gauge<Integer>() {
79                     @Override
80                     public Integer getValue() {
81                         return monitoredQueue.size();
82                     }
83                 });
84     }
85
86
87     public static class MeteredMessageQueue extends BoundedDequeBasedMailbox.MessageQueue {
88
89         public MeteredMessageQueue(int capacity, FiniteDuration pushTimeOut) {
90             super(capacity, pushTimeOut);
91         }
92     }
93
94 }
95