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