Changes for 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  * The information is reported to {@link org.opendaylight.controller.cluster.reporting.MetricsReporter}
27  */
28 public class MeteringBehavior implements Procedure<Object> {
29     public static final String DOMAIN = "org.opendaylight.controller.actor.metric";
30
31     private static final String MSG_PROCESSING_RATE = "msg-rate";
32
33     private final UntypedActor meteredActor;
34
35     private final MetricRegistry metricRegistry = MetricsReporter.getInstance(DOMAIN).getMetricsRegistry();
36
37     private String actorQualifiedName;
38     private Timer msgProcessingTimer;
39
40     /**
41      * Constructs an instance.
42      *
43      * @param actor whose behaviour needs to be metered
44      */
45     public MeteringBehavior(final AbstractUntypedActorWithMetering actor) {
46         Preconditions.checkArgument(actor != null, "actor must not be null");
47         this.meteredActor = actor;
48
49         String actorName = actor.getActorNameOverride() != null ? actor.getActorNameOverride()
50                                                                 : actor.getSelf().path().name();
51         init(actorName);
52     }
53
54     public MeteringBehavior(final UntypedActor actor) {
55         Preconditions.checkArgument(actor != null, "actor must not be null");
56         this.meteredActor = actor;
57
58         String actorName = actor.getSelf().path().name();
59         init(actorName);
60     }
61
62     private void init(final String actorName) {
63         actorQualifiedName = new StringBuilder(meteredActor.getSelf().path().parent().toStringWithoutAddress())
64                 .append("/").append(actorName).toString();
65
66         final String msgProcessingTime = MetricRegistry.name(actorQualifiedName, MSG_PROCESSING_RATE);
67         msgProcessingTimer = metricRegistry.timer(msgProcessingTime);
68     }
69
70     /**
71      * Uses 2 timers to measure message processing rate. One for overall message processing rate and
72      * another to measure rate by message type. The timers are re-used if they were previously created.
73      * <p/>
74      * {@link com.codahale.metrics.MetricRegistry} maintains a reservoir for different timers where
75      * collected timings are kept. It exposes various metrics for each timer based on collected
76      * data. Eg: count of messages, 99, 95, 50... percentiles, max, mean etc.
77      * <p/>
78      * These metrics are exposed as JMX bean.
79      *
80      * @see <a href="http://dropwizard.github.io/metrics/manual/core/#timers">
81      *     http://dropwizard.github.io/metrics/manual/core/#timers</a>
82      *
83      * @param message the message to process
84      * @throws Exception on message failure
85      */
86     @SuppressWarnings("checkstyle:IllegalCatch")
87     @Override
88     public void apply(final Object message) throws Exception {
89         final String messageType = message.getClass().getSimpleName();
90
91         final String msgProcessingTimeByMsgType =
92                 MetricRegistry.name(actorQualifiedName, MSG_PROCESSING_RATE, messageType);
93
94         final Timer msgProcessingTimerByMsgType = metricRegistry.timer(msgProcessingTimeByMsgType);
95
96         //start timers
97         final Timer.Context context = msgProcessingTimer.time();
98         final Timer.Context contextByMsgType = msgProcessingTimerByMsgType.time();
99
100         try {
101             meteredActor.onReceive(message);
102         } catch (Throwable e) {
103             Throwables.propagateIfPossible(e, Exception.class);
104             throw Throwables.propagate(e);
105         } finally {
106             //stop timers
107             contextByMsgType.stop();
108             context.stop();
109         }
110     }
111 }