Migrate to AbstractActor
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 19 Feb 2019 14:58:28 +0000 (15:58 +0100)
committerRobert Varga <nite@hq.sk>
Fri, 29 Mar 2019 10:46:50 +0000 (10:46 +0000)
UntypedActor has been deprecated, this patch migrates to using
AbstractActor so we eliminate a slew of deprecatation warnings.

Change-Id: I4e3274ce92d5f68df1937f91f823eed905da4d6c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
Signed-off-by: Ajay Lele <ajayslele@gmail.com>
java/org/opendaylight/controller/cluster/PersistentDataProvider.java
java/org/opendaylight/controller/cluster/common/actor/AbstractUntypedActor.java
java/org/opendaylight/controller/cluster/common/actor/AbstractUntypedPersistentActor.java
java/org/opendaylight/controller/cluster/common/actor/MeteringBehavior.java

index 1d676fa4a6577f7ff00fef4359fde56f485db3eb..21102f1f0e368a504bef81e526762ebd79464672 100644 (file)
@@ -7,20 +7,21 @@
  */
 package org.opendaylight.controller.cluster;
 
+import static java.util.Objects.requireNonNull;
+
 import akka.japi.Procedure;
+import akka.persistence.AbstractPersistentActor;
 import akka.persistence.SnapshotSelectionCriteria;
-import akka.persistence.UntypedPersistentActor;
-import com.google.common.base.Preconditions;
 
 /**
  * A DataPersistenceProvider implementation with persistence enabled.
  */
 public class PersistentDataProvider implements DataPersistenceProvider {
 
-    private final UntypedPersistentActor persistentActor;
+    private final AbstractPersistentActor persistentActor;
 
-    public PersistentDataProvider(UntypedPersistentActor persistentActor) {
-        this.persistentActor = Preconditions.checkNotNull(persistentActor, "persistentActor can't be null");
+    public PersistentDataProvider(AbstractPersistentActor persistentActor) {
+        this.persistentActor = requireNonNull(persistentActor, "persistentActor can't be null");
     }
 
     @Override
index c0e260ae66a9f2dcc6c73e254449da1167f403ff..6af52fbd04e2af4a60ce5865e46a0b51570347bd 100644 (file)
@@ -8,14 +8,14 @@
 
 package org.opendaylight.controller.cluster.common.actor;
 
+import akka.actor.AbstractActor;
 import akka.actor.ActorRef;
-import akka.actor.UntypedActor;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import org.eclipse.jdt.annotation.NonNull;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public abstract class AbstractUntypedActor extends UntypedActor implements ExecuteInSelfActor {
+public abstract class AbstractUntypedActor extends AbstractActor implements ExecuteInSelfActor {
     // The member name should be lower case but it's referenced in many subclasses. Suppressing the CS warning for now.
     @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_PRIVATE")
     @SuppressWarnings("checkstyle:MemberName")
@@ -33,12 +33,11 @@ public abstract class AbstractUntypedActor extends UntypedActor implements Execu
     }
 
     @Override
-    public final void onReceive(final Object message) {
-        if (message instanceof ExecuteInSelfMessage) {
-            ((ExecuteInSelfMessage) message).run();
-        } else {
-            handleReceive(message);
-        }
+    public Receive createReceive() {
+        return receiveBuilder()
+                .match(ExecuteInSelfMessage.class, ExecuteInSelfMessage::run)
+                .matchAny(this::handleReceive)
+                .build();
     }
 
     /**
index 5ee3c499881e5a54bc0d6a7e4858a24edad1604b..711a43159a376a3c289271cab06cedd9409d9c06 100644 (file)
@@ -5,17 +5,16 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.controller.cluster.common.actor;
 
 import akka.actor.ActorRef;
-import akka.persistence.UntypedPersistentActor;
+import akka.persistence.AbstractPersistentActor;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import org.eclipse.jdt.annotation.NonNull;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public abstract class AbstractUntypedPersistentActor extends UntypedPersistentActor implements ExecuteInSelfActor {
+public abstract class AbstractUntypedPersistentActor extends AbstractPersistentActor implements ExecuteInSelfActor {
 
     // The member name should be lower case but it's referenced in many subclasses. Suppressing the CS warning for now.
     @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_PRIVATE")
@@ -35,26 +34,16 @@ public abstract class AbstractUntypedPersistentActor extends UntypedPersistentAc
     }
 
     @Override
-    public final void onReceiveCommand(final Object message) throws Exception {
-        final String messageType = message.getClass().getSimpleName();
-        LOG.trace("Received message {}", messageType);
-
-        if (message instanceof ExecuteInSelfMessage) {
-            LOG.trace("Executing {}", message);
-            ((ExecuteInSelfMessage) message).run();
-        } else {
-            handleCommand(message);
-        }
-
-        LOG.trace("Done handling message {}", messageType);
+    public final Receive createReceive() {
+        return receiveBuilder()
+                .match(ExecuteInSelfMessage.class, ExecuteInSelfMessage::run)
+                .matchAny(this::handleCommand)
+                .build();
     }
 
     @Override
-    public final void onReceiveRecover(final Object message) throws Exception {
-        final String messageType = message.getClass().getSimpleName();
-        LOG.trace("Received message {}", messageType);
-        handleRecover(message);
-        LOG.trace("Done handling message {}", messageType);
+    public final Receive createReceiveRecover() {
+        return receiveBuilder().matchAny(this::handleRecover).build();
     }
 
     protected abstract void handleRecover(Object message) throws Exception;
index 63958912c48e8832a4c4b144eab84822e1b24b7a..a49bc30427ee4f323dcb681a7d6448f5aee95f70 100644 (file)
@@ -7,13 +7,13 @@
  */
 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}
@@ -26,17 +26,21 @@ import org.opendaylight.controller.cluster.reporting.MetricsReporter;
  * </ul>
  * The information is reported to {@link org.opendaylight.controller.cluster.reporting.MetricsReporter}
  */
-public class MeteringBehavior implements Procedure<Object> {
+public class MeteringBehavior extends AbstractPartialFunction<Object, BoxedUnit> {
     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<Object, BoxedUnit> 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<Object> {
      * @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<Object> {
      *     http://dropwizard.github.io/metrics/manual/core/#timers</a>
      *
      * @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(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<Object> {
         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();