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 930a4b0..754bf30 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 6eded7c..28c83db 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 9823521..9c09574 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 e7ab144..37726d2 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 55024b2..cab7b01 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 668f56b..3a46398 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);