Make Shutdown message a proper singleton 06/34606/3
authorRobert Varga <rovarga@cisco.com>
Sat, 13 Feb 2016 21:27:47 +0000 (22:27 +0100)
committerRobert Varga <rovarga@cisco.com>
Sat, 13 Feb 2016 21:38:01 +0000 (22:38 +0100)
It holds no state and thus can be readily reused.

Change-Id: I7aba9266a476ddb79f456ce66dd46697c2e5ddb5
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/client/messages/Shutdown.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/LeadershipTransferIntegrationTest.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardManager.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/ActorContext.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreRemotingIntegrationTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardManagerTest.java

index 930a4b0471f5152b4abe3f08b45b81198c67f675..754bf30c23e831d1e9861ba77523a5faa30fe2a6 100644 (file)
@@ -15,6 +15,16 @@ import java.io.Serializable;
  *
  * @author Thomas Pantelis
  */
-public class Shutdown implements Serializable {
+public final class Shutdown implements Serializable {
     private static final long serialVersionUID = 1L;
+    public static final Shutdown INSTANCE = new Shutdown();
+
+    private Shutdown() {
+        // Hidden on purpose
+    }
+
+    @SuppressWarnings({ "static-method", "unused" })
+    private Shutdown readResolve() {
+        return INSTANCE;
+    }
 }
index 6eded7cc63f00a207f3acfa337e1561d63fe2864..28c83dbcde8020adf43cfe81c217aa0a53541f9f 100644 (file)
@@ -67,7 +67,7 @@ public class LeadershipTransferIntegrationTest extends AbstractRaftActorIntegrat
         testLog.info("sendShutDown for {} starting", actor.path());
 
         FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
-        Future<Boolean> stopFuture = Patterns.gracefulStop(actor, duration, new Shutdown());
+        Future<Boolean> stopFuture = Patterns.gracefulStop(actor, duration, Shutdown.INSTANCE);
 
         Boolean stopped = Await.result(stopFuture, duration);
         assertEquals("Stopped", Boolean.TRUE, stopped);
@@ -84,7 +84,7 @@ public class LeadershipTransferIntegrationTest extends AbstractRaftActorIntegrat
         clearMessages(follower3NotifierActor);
 
         FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
-        Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, new Shutdown());
+        Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, Shutdown.INSTANCE);
 
         assertNullLeaderIdChange(leaderNotifierActor);
         assertNullLeaderIdChange(follower1NotifierActor);
index 9823521e4722a7d77da5ea3b446c011fee53a4fa..9c095745f3bad475d5c97b4ddc988b5ec4cd8607 100644 (file)
@@ -259,14 +259,13 @@ public class ShardManager extends AbstractUntypedPersistentActorWithMetering {
     }
 
     private void onShutDown() {
-        Shutdown shutdown = new Shutdown();
         List<Future<Boolean>> stopFutures = new ArrayList<>(localShards.size());
         for (ShardInformation info : localShards.values()) {
             if (info.getActor() != null) {
                 LOG.debug("{}: Issuing gracefulStop to shard {}", persistenceId(), info.getShardId());
 
                 FiniteDuration duration = info.getDatastoreContext().getShardRaftConfig().getElectionTimeOutInterval().$times(2);
-                stopFutures.add(Patterns.gracefulStop(info.getActor(), duration, shutdown));
+                stopFutures.add(Patterns.gracefulStop(info.getActor(), duration, Shutdown.INSTANCE));
             }
         }
 
@@ -385,7 +384,7 @@ public class ShardManager extends AbstractUntypedPersistentActorWithMetering {
             return;
         } else if(shardInformation.getActor() != null) {
             LOG.debug("{} : Sending Shutdown to Shard actor {}", persistenceId(), shardInformation.getActor());
-            shardInformation.getActor().tell(new Shutdown(), self());
+            shardInformation.getActor().tell(Shutdown.INSTANCE, self());
         }
         LOG.debug("{} : Local Shard replica for shard {} has been removed", persistenceId(), shardId.getShardName());
         persistShardList();
index e7ab144a5a034dfd0b868d33f34bd81c9aeafbb5..37726d201b1a80de0e03643b2e2ccabbf7639c39 100644 (file)
@@ -381,7 +381,7 @@ public class ActorContext {
     public void shutdown() {
         FiniteDuration duration = datastoreContext.getShardRaftConfig().getElectionTimeOutInterval().$times(3);
         try {
-            Await.ready(Patterns.gracefulStop(shardManager, duration, new Shutdown()), duration);
+            Await.ready(Patterns.gracefulStop(shardManager, duration, Shutdown.INSTANCE), duration);
         } catch(Exception e) {
             LOG.warn("ShardManager for {} data store did not shutdown gracefully", getDataStoreName(), e);
         }
index 55024b265aea8cb9b9d29d6a667207b51f669e90..cab7b01cfdf49af15fae6eb19453d344bd79a6be 100644 (file)
@@ -778,7 +778,7 @@ public class DistributedDataStoreRemotingIntegrationTest {
         Future<ActorRef> future = leaderDistributedDataStore.getActorContext().findLocalShardAsync("cars");
         ActorRef leaderActor = Await.result(future, duration);
 
-        Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, new Shutdown());
+        Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, Shutdown.INSTANCE);
 
         // Commit the 2 transactions. They should finish and succeed.
 
index 668f56bbc72f26541dd4e84a34ced611fa174113..3a463980fc11723fe4d0c278fb8c78f351dea2f3 100644 (file)
@@ -1937,7 +1937,7 @@ public class ShardManagerTest extends AbstractActorTest {
             shardManager.tell(new ActorInitialized(), shard2);
 
             FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
-            Future<Boolean> stopFuture = Patterns.gracefulStop(shardManager, duration, new Shutdown());
+            Future<Boolean> stopFuture = Patterns.gracefulStop(shardManager, duration, Shutdown.INSTANCE);
 
             MessageCollectorActor.expectFirstMatching(shard1, Shutdown.class);
             MessageCollectorActor.expectFirstMatching(shard2, Shutdown.class);