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