X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fcommon%2Factor%2FMeteringBehavior.java;h=30331930d6b8f882a9fab12835c5ea20da0bc6fd;hp=c04e2e93402ad12915828812d4365016fabbda51;hb=3bdf1493c0ce4218a1a3e26cdf7c5c4af1d2aeeb;hpb=dea515c8870769408b9bea29f555d6b71ff43211 diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/common/actor/MeteringBehavior.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/common/actor/MeteringBehavior.java index c04e2e9340..30331930d6 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/common/actor/MeteringBehavior.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/common/actor/MeteringBehavior.java @@ -7,97 +7,94 @@ */ package org.opendaylight.controller.cluster.common.actor; -import akka.actor.UntypedActor; -import akka.japi.Procedure; +import akka.actor.AbstractActor; import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.Timer; -import com.google.common.base.Preconditions; import org.opendaylight.controller.cluster.reporting.MetricsReporter; +import scala.PartialFunction; +import scala.runtime.AbstractPartialFunction; +import scala.runtime.BoxedUnit; /** - * Represents behaviour that can be exhibited by actors of type {@link akka.actor.UntypedActor} - *

+ * Represents behaviour that can be exhibited by actors of type {@link AbstractActor} + * + *

* This behaviour meters actor's default behaviour. It captures 2 metrics: *

- * * The information is reported to {@link org.opendaylight.controller.cluster.reporting.MetricsReporter} */ -public class MeteringBehavior implements Procedure { +public class MeteringBehavior extends AbstractPartialFunction { public static final String DOMAIN = "org.opendaylight.controller.actor.metric"; - private final UntypedActor meteredActor; + private static final String MSG_PROCESSING_RATE = "msg-rate"; - private final MetricRegistry METRICREGISTRY = MetricsReporter.getInstance(DOMAIN).getMetricsRegistry(); - private final String MSG_PROCESSING_RATE = "msg-rate"; + private final MetricRegistry metricRegistry = MetricsReporter.getInstance(DOMAIN).getMetricsRegistry(); + private final String actorQualifiedName; + private final Timer msgProcessingTimer; + private final PartialFunction receive; - private String actorQualifiedName; - private Timer msgProcessingTimer; + private MeteringBehavior(final String actorName, final AbstractActor meteredActor) { + actorQualifiedName = meteredActor.getSelf().path().parent().toStringWithoutAddress() + "/" + actorName; + msgProcessingTimer = metricRegistry.timer(MetricRegistry.name(actorQualifiedName, MSG_PROCESSING_RATE)); + receive = meteredActor.createReceive().onMessage(); + } /** + * Constructs an instance. * * @param actor whose behaviour needs to be metered */ - public MeteringBehavior(AbstractUntypedActorWithMetering actor){ - Preconditions.checkArgument(actor != null, "actor must not be null"); - this.meteredActor = actor; - - String actorName = actor.getActorNameOverride() != null ? actor.getActorNameOverride() - : actor.getSelf().path().name(); - init(actorName); + public MeteringBehavior(final AbstractUntypedActorWithMetering actor) { + this(actor.getActorNameOverride() != null ? actor.getActorNameOverride() : actor.getSelf().path().name(), + actor); } - public MeteringBehavior(UntypedActor actor){ - Preconditions.checkArgument(actor != null, "actor must not be null"); - this.meteredActor = actor; - - String actorName = actor.getSelf().path().name(); - init(actorName); + public MeteringBehavior(final AbstractActor actor) { + this(actor.getSelf().path().name(), actor); } - private void init(String actorName){ - actorQualifiedName = new StringBuilder(meteredActor.getSelf().path().parent().toStringWithoutAddress()). - append("/").append(actorName).toString(); - - final String msgProcessingTime = MetricRegistry.name(actorQualifiedName, MSG_PROCESSING_RATE); - msgProcessingTimer = METRICREGISTRY.timer(msgProcessingTime); + @Override + public boolean isDefinedAt(final Object obj) { + return receive.isDefinedAt(obj); } /** * Uses 2 timers to measure message processing rate. One for overall message processing rate and * another to measure rate by message type. The timers are re-used if they were previously created. - *

+ * + *

* {@link com.codahale.metrics.MetricRegistry} maintains a reservoir for different timers where * collected timings are kept. It exposes various metrics for each timer based on collected * data. Eg: count of messages, 99, 95, 50... percentiles, max, mean etc. - *

+ * + *

* These metrics are exposed as JMX bean. * * @see * http://dropwizard.github.io/metrics/manual/core/#timers * - * @param message - * @throws Exception + * @param message the message to process */ @Override - public void apply(Object message) throws Exception { + public BoxedUnit apply(final Object message) { final String messageType = message.getClass().getSimpleName(); - final String msgProcessingTimeByMsgType = MetricRegistry.name(actorQualifiedName, MSG_PROCESSING_RATE, messageType); - - final Timer msgProcessingTimerByMsgType = METRICREGISTRY.timer(msgProcessingTimeByMsgType); + final Timer msgProcessingTimerByMsgType = metricRegistry.timer(msgProcessingTimeByMsgType); //start timers final Timer.Context context = msgProcessingTimer.time(); final Timer.Context contextByMsgType = msgProcessingTimerByMsgType.time(); - meteredActor.onReceive(message); - - //stop timers - contextByMsgType.stop(); - context.stop(); + try { + return receive.apply(message); + } finally { + //stop timers + contextByMsgType.stop(); + context.stop(); + } } }