From: Robert Varga Date: Sat, 13 Feb 2016 21:27:47 +0000 (+0100) Subject: Make Shutdown message a proper singleton X-Git-Tag: release/boron~381 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=2e12efc12dc33154790ae92c33f5fae65053ef33 Make Shutdown message a proper singleton It holds no state and thus can be readily reused. Change-Id: I7aba9266a476ddb79f456ce66dd46697c2e5ddb5 Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/client/messages/Shutdown.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/client/messages/Shutdown.java index 930a4b0471..754bf30c23 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/client/messages/Shutdown.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/client/messages/Shutdown.java @@ -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; + } } diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/LeadershipTransferIntegrationTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/LeadershipTransferIntegrationTest.java index 6eded7cc63..28c83dbcde 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/LeadershipTransferIntegrationTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/LeadershipTransferIntegrationTest.java @@ -67,7 +67,7 @@ public class LeadershipTransferIntegrationTest extends AbstractRaftActorIntegrat testLog.info("sendShutDown for {} starting", actor.path()); FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS); - Future stopFuture = Patterns.gracefulStop(actor, duration, new Shutdown()); + Future 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 stopFuture = Patterns.gracefulStop(leaderActor, duration, new Shutdown()); + Future stopFuture = Patterns.gracefulStop(leaderActor, duration, Shutdown.INSTANCE); assertNullLeaderIdChange(leaderNotifierActor); assertNullLeaderIdChange(follower1NotifierActor); diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardManager.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardManager.java index 9823521e47..9c095745f3 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardManager.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardManager.java @@ -259,14 +259,13 @@ public class ShardManager extends AbstractUntypedPersistentActorWithMetering { } private void onShutDown() { - Shutdown shutdown = new Shutdown(); List> 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(); diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/ActorContext.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/ActorContext.java index e7ab144a5a..37726d201b 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/ActorContext.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/ActorContext.java @@ -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); } diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreRemotingIntegrationTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreRemotingIntegrationTest.java index 55024b265a..cab7b01cfd 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreRemotingIntegrationTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreRemotingIntegrationTest.java @@ -778,7 +778,7 @@ public class DistributedDataStoreRemotingIntegrationTest { Future future = leaderDistributedDataStore.getActorContext().findLocalShardAsync("cars"); ActorRef leaderActor = Await.result(future, duration); - Future stopFuture = Patterns.gracefulStop(leaderActor, duration, new Shutdown()); + Future stopFuture = Patterns.gracefulStop(leaderActor, duration, Shutdown.INSTANCE); // Commit the 2 transactions. They should finish and succeed. diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardManagerTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardManagerTest.java index 668f56bbc7..3a463980fc 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardManagerTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardManagerTest.java @@ -1937,7 +1937,7 @@ public class ShardManagerTest extends AbstractActorTest { shardManager.tell(new ActorInitialized(), shard2); FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS); - Future stopFuture = Patterns.gracefulStop(shardManager, duration, new Shutdown()); + Future stopFuture = Patterns.gracefulStop(shardManager, duration, Shutdown.INSTANCE); MessageCollectorActor.expectFirstMatching(shard1, Shutdown.class); MessageCollectorActor.expectFirstMatching(shard2, Shutdown.class);