Update to akka 2.4.11
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / common / actor / MeteringBehavior.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 package org.opendaylight.controller.cluster.common.actor;
9
10 import akka.actor.UntypedActor;
11 import akka.japi.Procedure;
12 import com.codahale.metrics.MetricRegistry;
13 import com.codahale.metrics.Timer;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Throwables;
16 import org.opendaylight.controller.cluster.reporting.MetricsReporter;
17
18 /**
19  * Represents behaviour that can be exhibited by actors of type {@link akka.actor.UntypedActor}
20  * <p/>
21  * This behaviour meters actor's default behaviour. It captures 2 metrics:
22  * <ul>
23  *     <li>message processing rate of actor's receive block</li>
24  *     <li>message processing rate by message type</li>
25  * </ul>
26  *
27  * The information is reported to {@link org.opendaylight.controller.cluster.reporting.MetricsReporter}
28  */
29 public class MeteringBehavior implements Procedure<Object> {
30     public static final String DOMAIN = "org.opendaylight.controller.actor.metric";
31
32     private final UntypedActor meteredActor;
33
34     private final MetricRegistry METRICREGISTRY = MetricsReporter.getInstance(DOMAIN).getMetricsRegistry();
35     private final String MSG_PROCESSING_RATE = "msg-rate";
36
37     private String actorQualifiedName;
38     private Timer msgProcessingTimer;
39
40     /**
41      *
42      * @param actor whose behaviour needs to be metered
43      */
44     public MeteringBehavior(final AbstractUntypedActorWithMetering actor) {
45         Preconditions.checkArgument(actor != null, "actor must not be null");
46         this.meteredActor = actor;
47
48         String actorName = actor.getActorNameOverride() != null ? actor.getActorNameOverride()
49                                                                 : actor.getSelf().path().name();
50         init(actorName);
51     }
52
53     public MeteringBehavior(final UntypedActor actor) {
54         Preconditions.checkArgument(actor != null, "actor must not be null");
55         this.meteredActor = actor;
56
57         String actorName = actor.getSelf().path().name();
58         init(actorName);
59     }
60
61     private void init(final String actorName) {
62         actorQualifiedName = new StringBuilder(meteredActor.getSelf().path().parent().toStringWithoutAddress()).
63                 append("/").append(actorName).toString();
64
65         final String msgProcessingTime = MetricRegistry.name(actorQualifiedName, MSG_PROCESSING_RATE);
66         msgProcessingTimer = METRICREGISTRY.timer(msgProcessingTime);
67     }
68
69     /**
70      * Uses 2 timers to measure message processing rate. One for overall message processing rate and
71      * another to measure rate by message type. The timers are re-used if they were previously created.
72      * <p/>
73      * {@link com.codahale.metrics.MetricRegistry} maintains a reservoir for different timers where
74      * collected timings are kept. It exposes various metrics for each timer based on collected
75      * data. Eg: count of messages, 99, 95, 50... percentiles, max, mean etc.
76      * <p/>
77      * These metrics are exposed as JMX bean.
78      *
79      * @see <a href="http://dropwizard.github.io/metrics/manual/core/#timers">
80      *     http://dropwizard.github.io/metrics/manual/core/#timers</a>
81      *
82      * @param message
83      * @throws Exception
84      */
85     @Override
86     public void apply(final Object message) throws Exception {
87         final String messageType = message.getClass().getSimpleName();
88
89         final String msgProcessingTimeByMsgType =
90                 MetricRegistry.name(actorQualifiedName, MSG_PROCESSING_RATE, messageType);
91
92         final Timer msgProcessingTimerByMsgType = METRICREGISTRY.timer(msgProcessingTimeByMsgType);
93
94         //start timers
95         final Timer.Context context = msgProcessingTimer.time();
96         final Timer.Context contextByMsgType = msgProcessingTimerByMsgType.time();
97
98         try {
99             meteredActor.onReceive(message);
100         } catch (Throwable t) {
101             Throwables.propagateIfPossible(t, Exception.class);
102             throw Throwables.propagate(t);
103         }
104
105         //stop timers
106         contextByMsgType.stop();
107         context.stop();
108     }
109 }