X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fcommon%2Factor%2FMeteringBehavior.java;h=30331930d6b8f882a9fab12835c5ea20da0bc6fd;hb=8fc8b1ed975f1fbd1929630522f2fcb8f3a832d0;hp=63958912c48e8832a4c4b144eab84822e1b24b7a;hpb=81674d6fd50b419b868d0851062e23f34b34557d;p=controller.git 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 63958912c4..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,16 +7,16 @@ */ 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 com.google.common.base.Throwables; 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: @@ -26,17 +26,21 @@ import org.opendaylight.controller.cluster.reporting.MetricsReporter; * * 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 static final String MSG_PROCESSING_RATE = "msg-rate"; - private final UntypedActor meteredActor; - private final MetricRegistry metricRegistry = MetricsReporter.getInstance(DOMAIN).getMetricsRegistry(); - - private String actorQualifiedName; - private Timer msgProcessingTimer; + private final String actorQualifiedName; + private final Timer msgProcessingTimer; + private final PartialFunction receive; + + 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. @@ -44,28 +48,17 @@ public class MeteringBehavior implements Procedure { * @param actor whose behaviour needs to be metered */ public MeteringBehavior(final 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); + this(actor.getActorNameOverride() != null ? actor.getActorNameOverride() : actor.getSelf().path().name(), + actor); } - public MeteringBehavior(final 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(final String actorName) { - actorQualifiedName = meteredActor.getSelf().path().parent().toStringWithoutAddress() - + "/" + actorName; - - final String msgProcessingTime = MetricRegistry.name(actorQualifiedName, MSG_PROCESSING_RATE); - msgProcessingTimer = metricRegistry.timer(msgProcessingTime); + @Override + public boolean isDefinedAt(final Object obj) { + return receive.isDefinedAt(obj); } /** @@ -84,16 +77,12 @@ public class MeteringBehavior implements Procedure { * http://dropwizard.github.io/metrics/manual/core/#timers * * @param message the message to process - * @throws Exception on message failure */ - @SuppressWarnings("checkstyle:IllegalCatch") @Override - public void apply(final 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); //start timers @@ -101,10 +90,7 @@ public class MeteringBehavior implements Procedure { final Timer.Context contextByMsgType = msgProcessingTimerByMsgType.time(); try { - meteredActor.onReceive(message); - } catch (Throwable e) { - Throwables.propagateIfPossible(e, Exception.class); - throw new RuntimeException(e); + return receive.apply(message); } finally { //stop timers contextByMsgType.stop();