Expose more fine-grained shutdown methods 34/87934/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 21 Feb 2020 05:28:55 +0000 (06:28 +0100)
committerRobert Varga <nite@hq.sk>
Sat, 22 Feb 2020 09:52:58 +0000 (09:52 +0000)
We want to the user to be in control of timing of shutdown, expose
appropriate methods to do that.

Change-Id: Id0a787a1b707a3eb6c2909c52023cc5eac3c1402
JIRA: CONTROLLER-1929
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/akka/impl/ActorSystemProviderImpl.java

index 6e805b3..4868d04 100644 (file)
@@ -9,9 +9,12 @@ package org.opendaylight.controller.cluster.akka.impl;
 
 import akka.actor.ActorSystem;
 import akka.actor.Props;
+import akka.actor.Terminated;
+import akka.dispatch.OnComplete;
 import com.typesafe.config.Config;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.controller.cluster.ActorSystemProvider;
 import org.opendaylight.controller.cluster.ActorSystemProviderListener;
 import org.opendaylight.controller.cluster.common.actor.QuarantinedMonitorActor;
@@ -21,13 +24,15 @@ import org.opendaylight.yangtools.util.ListenerRegistry;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import scala.concurrent.Await;
+import scala.concurrent.ExecutionContext;
+import scala.concurrent.Future;
 import scala.concurrent.duration.FiniteDuration;
 
 public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseable {
     private static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
     private static final Logger LOG = LoggerFactory.getLogger(ActorSystemProviderImpl.class);
 
-    private final ActorSystem actorSystem;
+    private final @NonNull ActorSystem actorSystem;
     private final ListenerRegistry<ActorSystemProviderListener> listeners = ListenerRegistry.create();
 
     public ActorSystemProviderImpl(
@@ -35,7 +40,6 @@ public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseab
         LOG.info("Creating new ActorSystem");
 
         actorSystem = ActorSystem.create(ACTOR_SYSTEM_NAME, akkaConfig, classLoader);
-
         actorSystem.actorOf(Props.create(TerminationMonitor.class), TerminationMonitor.ADDRESS);
         actorSystem.actorOf(quarantinedMonitorActorProps, QuarantinedMonitorActor.ADDRESS);
     }
@@ -51,9 +55,29 @@ public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseab
         return listeners.register(listener);
     }
 
+    public Future<Terminated> asyncClose() {
+        LOG.info("Shutting down ActorSystem");
+
+        final Future<Terminated> ret = actorSystem.terminate();
+        ret.onComplete(new OnComplete<Terminated>() {
+            @Override
+            public void onComplete(final Throwable failure, final Terminated success) throws Throwable {
+                if (failure != null) {
+                    LOG.warn("ActorSystem failed to shut down", failure);
+                } else {
+                    LOG.info("ActorSystem shut down");
+                }
+            }
+        }, ExecutionContext.global());
+        return ret;
+    }
+
+    public void close(final FiniteDuration wait) throws TimeoutException, InterruptedException {
+        Await.result(asyncClose(), wait);
+    }
+
     @Override
     public void close() throws TimeoutException, InterruptedException {
-        LOG.info("Shutting down ActorSystem");
-        Await.result(actorSystem.terminate(), FiniteDuration.create(10, TimeUnit.SECONDS));
+        close(FiniteDuration.create(10, TimeUnit.SECONDS));
     }
 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.