X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FRaftActorServerConfigurationSupportTest.java;h=97ab20a48b8a319e9f09903600edf5f6cb767c1d;hb=0f02b7edeb1454c1a568f0f1b050757e7503ddf7;hp=a19281ffd8cbedd86062c008edd65cc6c2bbb8f4;hpb=913ae866cd0cc82991e1f66ac80f6a42b0daaa48;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorServerConfigurationSupportTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorServerConfigurationSupportTest.java index a19281ffd8..97ab20a48b 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorServerConfigurationSupportTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorServerConfigurationSupportTest.java @@ -15,22 +15,26 @@ import static org.opendaylight.controller.cluster.raft.utils.MessageCollectorAct import static org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor.expectFirstMatching; import static org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor.expectMatching; +import akka.actor.AbstractActor; import akka.actor.ActorRef; import akka.actor.Props; -import akka.actor.UntypedActor; import akka.dispatch.Dispatchers; -import akka.testkit.JavaTestKit; import akka.testkit.TestActorRef; +import akka.testkit.javadsl.TestKit; import com.google.common.base.Optional; import com.google.common.base.Stopwatch; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import com.google.common.collect.Sets; +import com.google.common.io.ByteSource; +import java.io.OutputStream; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import org.apache.commons.lang3.SerializationUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -38,7 +42,6 @@ import org.opendaylight.controller.cluster.NonPersistentDataProvider; import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot; import org.opendaylight.controller.cluster.raft.base.messages.ApplyState; import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply; -import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout; import org.opendaylight.controller.cluster.raft.base.messages.InitiateCaptureSnapshot; import org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow; import org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader; @@ -58,6 +61,7 @@ import org.opendaylight.controller.cluster.raft.messages.ServerChangeStatus; import org.opendaylight.controller.cluster.raft.messages.ServerRemoved; import org.opendaylight.controller.cluster.raft.messages.UnInitializedFollowerSnapshotReply; import org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries; +import org.opendaylight.controller.cluster.raft.persisted.ByteState; import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload; import org.opendaylight.controller.cluster.raft.persisted.ServerInfo; import org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry; @@ -94,10 +98,10 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { actorFactory.generateActorId(FOLLOWER_ID)); private TestActorRef newFollowerRaftActor; - private TestActorRef newFollowerCollectorActor; + private ActorRef newFollowerCollectorActor; private RaftActorContext newFollowerActorContext; - private final JavaTestKit testKit = new JavaTestKit(getSystem()); + private final TestKit testKit = new TestKit(getSystem()); @Before public void setup() { @@ -109,8 +113,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { private void setupNewFollower() { DefaultConfigParamsImpl configParams = newFollowerConfigParams(); - newFollowerCollectorActor = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), + newFollowerCollectorActor = actorFactory.createActor(MessageCollectorActor.props(), actorFactory.generateActorId(NEW_SERVER_ID + "Collector")); newFollowerRaftActor = actorFactory.createTestActor(MockNewFollowerRaftActor.props( configParams, newFollowerCollectorActor).withDispatcher(Dispatchers.DefaultDispatcherId()), @@ -132,12 +135,12 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { } @After - public void tearDown() throws Exception { + public void tearDown() { actorFactory.close(); } @Test - public void testAddServerWithExistingFollower() throws Exception { + public void testAddServerWithExistingFollower() { LOG.info("testAddServerWithExistingFollower starting"); setupNewFollower(); RaftActorContextImpl followerActorContext = newFollowerContext(FOLLOWER_ID, followerActor); @@ -160,18 +163,17 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { clearMessages(followerActor); MockLeaderRaftActor leaderRaftActor = leaderActor.underlyingActor(); - final TestActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); + final ActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); leaderActor.tell(new AddServer(NEW_SERVER_ID, newFollowerRaftActor.path().toString(), true), testKit.getRef()); // Leader should install snapshot - capture and verify ApplySnapshot contents ApplySnapshot applySnapshot = expectFirstMatching(newFollowerCollectorActor, ApplySnapshot.class); - @SuppressWarnings("unchecked") - List snapshotState = (List) MockRaftActor.toObject(applySnapshot.getSnapshot().getState()); + List snapshotState = MockRaftActor.fromState(applySnapshot.getSnapshot().getState()); assertEquals("Snapshot state", snapshotState, leaderRaftActor.getState()); - AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + AddServerReply addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus()); assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint().get()); @@ -223,7 +225,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { } @Test - public void testAddServerWithNoExistingFollower() throws Exception { + public void testAddServerWithNoExistingFollower() { LOG.info("testAddServerWithNoExistingFollower starting"); setupNewFollower(); @@ -241,18 +243,17 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { MockLeaderRaftActor leaderRaftActor = leaderActor.underlyingActor(); final RaftActorContext leaderActorContext = leaderRaftActor.getRaftActorContext(); - final TestActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); + final ActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); leaderActor.tell(new AddServer(NEW_SERVER_ID, newFollowerRaftActor.path().toString(), true), testKit.getRef()); // Leader should install snapshot - capture and verify ApplySnapshot contents ApplySnapshot applySnapshot = expectFirstMatching(newFollowerCollectorActor, ApplySnapshot.class); - @SuppressWarnings("unchecked") - List snapshotState = (List) MockRaftActor.toObject(applySnapshot.getSnapshot().getState()); + List snapshotState = MockRaftActor.fromState(applySnapshot.getSnapshot().getState()); assertEquals("Snapshot state", snapshotState, leaderRaftActor.getState()); - AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + AddServerReply addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus()); assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint().get()); @@ -280,7 +281,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { } @Test - public void testAddServersAsNonVoting() throws Exception { + public void testAddServersAsNonVoting() { LOG.info("testAddServersAsNonVoting starting"); setupNewFollower(); @@ -294,11 +295,11 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { MockLeaderRaftActor leaderRaftActor = leaderActor.underlyingActor(); final RaftActorContext leaderActorContext = leaderRaftActor.getRaftActorContext(); - final TestActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); + final ActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); leaderActor.tell(new AddServer(NEW_SERVER_ID, newFollowerRaftActor.path().toString(), false), testKit.getRef()); - AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + AddServerReply addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus()); assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint().get()); @@ -335,7 +336,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { leaderActor.tell(new AddServer(NEW_SERVER_ID2, followerActor.path().toString(), false), testKit.getRef()); - addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus()); assertEquals("getLeaderHint", java.util.Optional.of(LEADER_ID), addServerReply.getLeaderHint()); @@ -350,7 +351,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { } @Test - public void testAddServerWithOperationInProgress() throws Exception { + public void testAddServerWithOperationInProgress() { LOG.info("testAddServerWithOperationInProgress starting"); setupNewFollower(); @@ -364,7 +365,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { MockLeaderRaftActor leaderRaftActor = leaderActor.underlyingActor(); final RaftActorContext leaderActorContext = leaderRaftActor.getRaftActorContext(); - final TestActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); + final ActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); RaftActorContext follower2ActorContext = newFollowerContext(NEW_SERVER_ID2, followerActor); Follower newFollower2 = new Follower(follower2ActorContext); @@ -380,7 +381,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { InstallSnapshot installSnapshot = expectFirstMatching(newFollowerCollectorActor, InstallSnapshot.class); // Send a second AddServer - should get queued - JavaTestKit testKit2 = new JavaTestKit(getSystem()); + TestKit testKit2 = new TestKit(getSystem()); leaderActor.tell(new AddServer(NEW_SERVER_ID2, followerActor.path().toString(), false), testKit2.getRef()); // Continue the first AddServer @@ -388,10 +389,10 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { newFollowerRaftActor.tell(installSnapshot, leaderActor); // Verify both complete successfully - AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + AddServerReply addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus()); - addServerReply = testKit2.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + addServerReply = testKit2.expectMsgClass(testKit2.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus()); // Verify ServerConfigurationPayload entries in leader's log @@ -413,7 +414,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { } @Test - public void testAddServerWithPriorSnapshotInProgress() throws Exception { + public void testAddServerWithPriorSnapshotInProgress() { LOG.info("testAddServerWithPriorSnapshotInProgress starting"); setupNewFollower(); @@ -427,7 +428,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { MockLeaderRaftActor leaderRaftActor = leaderActor.underlyingActor(); final RaftActorContext leaderActorContext = leaderRaftActor.getRaftActorContext(); - TestActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); + ActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); // Drop commit message for now to delay snapshot completion leaderRaftActor.setDropMessageOfType(String.class); @@ -441,7 +442,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { leaderRaftActor.setDropMessageOfType(null); leaderActor.tell(commitMsg, leaderActor); - AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + AddServerReply addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus()); assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint().get()); @@ -460,7 +461,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { } @Test - public void testAddServerWithPriorSnapshotCompleteTimeout() throws Exception { + public void testAddServerWithPriorSnapshotCompleteTimeout() { LOG.info("testAddServerWithPriorSnapshotCompleteTimeout starting"); setupNewFollower(); @@ -483,7 +484,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { leaderActor.tell(new AddServer(NEW_SERVER_ID, newFollowerRaftActor.path().toString(), true), testKit.getRef()); - AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + AddServerReply addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.TIMEOUT, addServerReply.getStatus()); assertEquals("Leader peers size", 0, leaderActorContext.getPeerIds().size()); @@ -492,7 +493,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { } @Test - public void testAddServerWithLeaderChangeBeforePriorSnapshotComplete() throws Exception { + public void testAddServerWithLeaderChangeBeforePriorSnapshotComplete() { LOG.info("testAddServerWithLeaderChangeBeforePriorSnapshotComplete starting"); setupNewFollower(); @@ -507,7 +508,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { RaftActorContext leaderActorContext = leaderRaftActor.getRaftActorContext(); ((DefaultConfigParamsImpl)leaderActorContext.getConfigParams()).setElectionTimeoutFactor(100); - final TestActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); + final ActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); // Drop the commit message so the snapshot doesn't complete yet. leaderRaftActor.setDropMessageOfType(COMMIT_MESSAGE_CLASS); @@ -531,7 +532,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { leaderActor.tell(new RaftActorServerConfigurationSupport.ServerOperationTimeout(NEW_SERVER_ID), leaderActor); - AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + AddServerReply addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.NO_LEADER, addServerReply.getStatus()); assertEquals("Leader peers size", 0, leaderActorContext.getPeerIds().size()); @@ -541,7 +542,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { } @Test - public void testAddServerWithLeaderChangeDuringInstallSnapshot() throws Exception { + public void testAddServerWithLeaderChangeDuringInstallSnapshot() { LOG.info("testAddServerWithLeaderChangeDuringInstallSnapshot starting"); setupNewFollower(); @@ -557,7 +558,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { ((DefaultConfigParamsImpl)leaderActorContext.getConfigParams()).setElectionTimeoutFactor(8); - TestActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); + ActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); // Drop the UnInitializedFollowerSnapshotReply to delay it. leaderRaftActor.setDropMessageOfType(UnInitializedFollowerSnapshotReply.class); @@ -577,7 +578,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { leaderRaftActor.setDropMessageOfType(null); leaderActor.tell(snapshotReply, leaderActor); - AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + AddServerReply addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.NO_LEADER, addServerReply.getStatus()); assertEquals("Leader peers size", 0, leaderActorContext.getPeerIds().size()); @@ -586,7 +587,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { } @Test - public void testAddServerWithInstallSnapshotTimeout() throws Exception { + public void testAddServerWithInstallSnapshotTimeout() { LOG.info("testAddServerWithInstallSnapshotTimeout starting"); setupNewFollower(); @@ -608,7 +609,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { leaderActor.tell(new UnInitializedFollowerSnapshotReply("bogus"), leaderActor); - AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + AddServerReply addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.TIMEOUT, addServerReply.getStatus()); assertEquals("Leader peers size", 0, leaderActorContext.getPeerIds().size()); @@ -635,7 +636,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { noLeaderActor.tell(new AddServer(NEW_SERVER_ID, newFollowerRaftActor.path().toString(), true), testKit.getRef()); - AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + AddServerReply addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.NO_LEADER, addServerReply.getStatus()); LOG.info("testAddServerWithNoLeader ending"); @@ -656,7 +657,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { MockLeaderRaftActor leaderRaftActor = leaderActor.underlyingActor(); final RaftActorContext leaderActorContext = leaderRaftActor.getRaftActorContext(); - final TestActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); + final ActorRef leaderCollectorActor = newLeaderCollectorActor(leaderRaftActor); // Drop UnInitializedFollowerSnapshotReply initially leaderRaftActor.setDropMessageOfType(UnInitializedFollowerSnapshotReply.class); @@ -682,7 +683,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { leaderActor.tell(new AddServer(NEW_SERVER_ID2, "", false), testKit.getRef()); // The first AddServer should succeed with OK even though consensus wasn't reached - AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + AddServerReply addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus()); assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint().get()); @@ -691,12 +692,12 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { votingServer(NEW_SERVER_ID)); // The second AddServer should fail since consensus wasn't reached for the first - addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.PRIOR_REQUEST_CONSENSUS_TIMEOUT, addServerReply.getStatus()); // Re-send the second AddServer - should also fail leaderActor.tell(new AddServer(NEW_SERVER_ID2, "", false), testKit.getRef()); - addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.PRIOR_REQUEST_CONSENSUS_TIMEOUT, addServerReply.getStatus()); LOG.info("testAddServerWithNoConsensusReached ending"); @@ -715,7 +716,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { leaderActor.tell(new AddServer(FOLLOWER_ID, followerActor.path().toString(), true), testKit.getRef()); - AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class); + AddServerReply addServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), AddServerReply.class); assertEquals("getStatus", ServerChangeStatus.ALREADY_EXISTS, addServerReply.getStatus()); LOG.info("testAddServerWithExistingServer ending"); @@ -729,9 +730,8 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl(); configParams.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS)); - TestActorRef leaderActor = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId(LEADER_ID)); + ActorRef leaderActor = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId(LEADER_ID)); TestActorRef followerRaftActor = actorFactory.createTestActor( MockRaftActor.builder().id(FOLLOWER_ID).peerAddresses(ImmutableMap.of(LEADER_ID, @@ -793,7 +793,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { leaderActor.underlyingActor().waitForInitializeBehaviorComplete(); leaderActor.tell(new RemoveServer(FOLLOWER_ID), testKit.getRef()); - RemoveServerReply removeServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), + RemoveServerReply removeServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), RemoveServerReply.class); assertEquals("getStatus", ServerChangeStatus.NO_LEADER, removeServerReply.getStatus()); @@ -812,7 +812,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { actorFactory.generateActorId(LEADER_ID)); leaderActor.tell(new RemoveServer(NEW_SERVER_ID), testKit.getRef()); - RemoveServerReply removeServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), + RemoveServerReply removeServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), RemoveServerReply.class); assertEquals("getStatus", ServerChangeStatus.DOES_NOT_EXIST, removeServerReply.getStatus()); @@ -826,9 +826,8 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl(); configParams.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS)); - TestActorRef leaderActor = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId(LEADER_ID)); + ActorRef leaderActor = actorFactory.createTestActor( + MessageCollectorActor.props(), actorFactory.generateActorId(LEADER_ID)); TestActorRef followerRaftActor = actorFactory.createTestActor( MockRaftActor.builder().id(FOLLOWER_ID).peerAddresses(ImmutableMap.of(LEADER_ID, @@ -854,41 +853,58 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { configParams.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS)); configParams.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName()); - final String followerActorId = actorFactory.generateActorId(FOLLOWER_ID); - final String followerActorPath = actorFactory.createTestActorPath(followerActorId); + final String follower1ActorId = actorFactory.generateActorId(FOLLOWER_ID); + final String follower1ActorPath = actorFactory.createTestActorPath(follower1ActorId); + final String follower2ActorId = actorFactory.generateActorId(FOLLOWER_ID2); + final String follower2ActorPath = actorFactory.createTestActorPath(follower2ActorId); RaftActorContext initialActorContext = new MockRaftActorContext(); - TestActorRef leaderActor = actorFactory.createTestActor( - MockLeaderRaftActor.props(ImmutableMap.of(FOLLOWER_ID, followerActorPath), + final String downNodeId = "downNode"; + TestActorRef leaderActor = actorFactory.createTestActor(MockLeaderRaftActor.props( + ImmutableMap.of(FOLLOWER_ID, follower1ActorPath, FOLLOWER_ID2, follower2ActorPath, downNodeId, ""), initialActorContext).withDispatcher(Dispatchers.DefaultDispatcherId()), actorFactory.generateActorId(LEADER_ID)); - final TestActorRef leaderCollector = - newLeaderCollectorActor(leaderActor.underlyingActor()); + final ActorRef leaderCollector = newLeaderCollectorActor(leaderActor.underlyingActor()); - TestActorRef collector = actorFactory.createTestActor(MessageCollectorActor.props() - .withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId("collector")); - actorFactory.createTestActor( - CollectingMockRaftActor.props(FOLLOWER_ID, ImmutableMap.of(LEADER_ID, leaderActor.path().toString()), - configParams, NO_PERSISTENCE, collector).withDispatcher(Dispatchers.DefaultDispatcherId()), - followerActorId); + ActorRef follower1Collector = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("collector")); + final TestActorRef follower1Actor = actorFactory.createTestActor( + CollectingMockRaftActor.props(FOLLOWER_ID, ImmutableMap.of(LEADER_ID, leaderActor.path().toString(), + FOLLOWER_ID2, follower2ActorPath, downNodeId, ""), configParams, NO_PERSISTENCE, + follower1Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), follower1ActorId); + + ActorRef follower2Collector = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("collector")); + final TestActorRef follower2Actor = actorFactory.createTestActor( + CollectingMockRaftActor.props(FOLLOWER_ID2, ImmutableMap.of(LEADER_ID, leaderActor.path().toString(), + FOLLOWER_ID, follower1ActorPath, downNodeId, ""), configParams, NO_PERSISTENCE, + follower2Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), follower2ActorId); + + leaderActor.underlyingActor().waitForInitializeBehaviorComplete(); + follower1Actor.underlyingActor().waitForInitializeBehaviorComplete(); + follower2Actor.underlyingActor().waitForInitializeBehaviorComplete(); leaderActor.tell(new RemoveServer(FOLLOWER_ID), testKit.getRef()); - RemoveServerReply removeServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), + RemoveServerReply removeServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), RemoveServerReply.class); assertEquals("getStatus", ServerChangeStatus.OK, removeServerReply.getStatus()); - final ApplyState applyState = MessageCollectorActor.expectFirstMatching(leaderCollector, ApplyState.class); + ApplyState applyState = MessageCollectorActor.expectFirstMatching(leaderCollector, ApplyState.class); + assertEquals(0L, applyState.getReplicatedLogEntry().getIndex()); + verifyServerConfigurationPayloadEntry(leaderActor.underlyingActor().getRaftActorContext().getReplicatedLog(), + votingServer(LEADER_ID), votingServer(FOLLOWER_ID2), votingServer(downNodeId)); + + applyState = MessageCollectorActor.expectFirstMatching(follower2Collector, ApplyState.class); assertEquals(0L, applyState.getReplicatedLogEntry().getIndex()); verifyServerConfigurationPayloadEntry(leaderActor.underlyingActor().getRaftActorContext().getReplicatedLog(), - votingServer(LEADER_ID)); + votingServer(LEADER_ID), votingServer(FOLLOWER_ID2), votingServer(downNodeId)); RaftActorBehavior currentBehavior = leaderActor.underlyingActor().getCurrentBehavior(); assertTrue("Expected Leader", currentBehavior instanceof Leader); - assertEquals("Follower ids size", 0, ((Leader)currentBehavior).getFollowerIds().size()); + assertEquals("Follower ids size", 2, ((Leader)currentBehavior).getFollowerIds().size()); - MessageCollectorActor.expectFirstMatching(collector, ServerRemoved.class); + MessageCollectorActor.expectFirstMatching(follower1Collector, ServerRemoved.class); LOG.info("testRemoveServer ending"); } @@ -910,12 +926,10 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { initialActorContext).withDispatcher(Dispatchers.DefaultDispatcherId()), actorFactory.generateActorId(LEADER_ID)); - final TestActorRef leaderCollector = - newLeaderCollectorActor(leaderActor.underlyingActor()); + final ActorRef leaderCollector = newLeaderCollectorActor(leaderActor.underlyingActor()); - final TestActorRef followerCollector = - actorFactory.createTestActor(MessageCollectorActor.props() - .withDispatcher(Dispatchers.DefaultDispatcherId()), actorFactory.generateActorId("collector")); + final ActorRef followerCollector = + actorFactory.createActor(MessageCollectorActor.props(), actorFactory.generateActorId("collector")); actorFactory.createTestActor( CollectingMockRaftActor.props(FOLLOWER_ID, ImmutableMap.of(LEADER_ID, leaderActor.path().toString()), configParams, NO_PERSISTENCE, followerCollector) @@ -923,7 +937,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { followerActorId); leaderActor.tell(new RemoveServer(LEADER_ID), testKit.getRef()); - RemoveServerReply removeServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), + RemoveServerReply removeServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), RemoveServerReply.class); assertEquals("getStatus", ServerChangeStatus.OK, removeServerReply.getStatus()); @@ -947,7 +961,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { actorFactory.generateActorId(LEADER_ID)); leaderActor.tell(new RemoveServer(LEADER_ID), testKit.getRef()); - RemoveServerReply removeServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), + RemoveServerReply removeServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), RemoveServerReply.class); assertEquals("getStatus", ServerChangeStatus.NOT_SUPPORTED, removeServerReply.getStatus()); @@ -971,19 +985,17 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { MockLeaderRaftActor.props(ImmutableMap.of(FOLLOWER_ID, follower1ActorPath, FOLLOWER_ID2, follower2ActorPath), new MockRaftActorContext()) .withDispatcher(Dispatchers.DefaultDispatcherId()), actorFactory.generateActorId(LEADER_ID)); - TestActorRef leaderCollector = newLeaderCollectorActor(leaderActor.underlyingActor()); + ActorRef leaderCollector = newLeaderCollectorActor(leaderActor.underlyingActor()); - TestActorRef follower1Collector = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId("collector")); + ActorRef follower1Collector = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("collector")); final TestActorRef follower1RaftActor = actorFactory.createTestActor( CollectingMockRaftActor.props(FOLLOWER_ID, ImmutableMap.of(LEADER_ID, leaderActor.path().toString(), FOLLOWER_ID2, follower2ActorPath), configParams, NO_PERSISTENCE, follower1Collector) .withDispatcher(Dispatchers.DefaultDispatcherId()), follower1ActorId); - TestActorRef follower2Collector = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId("collector")); + ActorRef follower2Collector = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("collector")); final TestActorRef follower2RaftActor = actorFactory.createTestActor( CollectingMockRaftActor.props(FOLLOWER_ID2, ImmutableMap.of(LEADER_ID, leaderActor.path().toString(), FOLLOWER_ID, follower1ActorPath), configParams, NO_PERSISTENCE, follower2Collector) @@ -993,7 +1005,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { leaderActor.tell(new ChangeServersVotingStatus(ImmutableMap.of(FOLLOWER_ID, false, FOLLOWER_ID2, false)), testKit.getRef()); - ServerChangeReply reply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), ServerChangeReply.class); + ServerChangeReply reply = testKit.expectMsgClass(testKit.duration("5 seconds"), ServerChangeReply.class); assertEquals("getStatus", ServerChangeStatus.OK, reply.getStatus()); final ApplyState applyState = MessageCollectorActor.expectFirstMatching(leaderCollector, ApplyState.class); @@ -1018,7 +1030,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { // Send second ChangeServersVotingStatus message leaderActor.tell(new ChangeServersVotingStatus(ImmutableMap.of(FOLLOWER_ID, true)), testKit.getRef()); - reply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), ServerChangeReply.class); + reply = testKit.expectMsgClass(testKit.duration("5 seconds"), ServerChangeReply.class); assertEquals("getStatus", ServerChangeStatus.OK, reply.getStatus()); MessageCollectorActor.expectFirstMatching(leaderCollector, ApplyState.class); @@ -1052,19 +1064,17 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { MockLeaderRaftActor.props(ImmutableMap.of(FOLLOWER_ID, follower1ActorPath, FOLLOWER_ID2, follower2ActorPath), new MockRaftActorContext()) .withDispatcher(Dispatchers.DefaultDispatcherId()), actorFactory.generateActorId(LEADER_ID)); - TestActorRef leaderCollector = newLeaderCollectorActor(leaderActor.underlyingActor()); + ActorRef leaderCollector = newLeaderCollectorActor(leaderActor.underlyingActor()); - TestActorRef follower1Collector = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId("collector")); + ActorRef follower1Collector = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("collector")); final TestActorRef follower1RaftActor = actorFactory.createTestActor( CollectingMockRaftActor.props(FOLLOWER_ID, ImmutableMap.of(LEADER_ID, leaderActor.path().toString(), FOLLOWER_ID2, follower2ActorPath), configParams, NO_PERSISTENCE, follower1Collector) .withDispatcher(Dispatchers.DefaultDispatcherId()), follower1ActorId); - TestActorRef follower2Collector = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId("collector")); + ActorRef follower2Collector = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("collector")); final TestActorRef follower2RaftActor = actorFactory.createTestActor( CollectingMockRaftActor.props(FOLLOWER_ID2, ImmutableMap.of(LEADER_ID, leaderActor.path().toString(), FOLLOWER_ID, follower1ActorPath), configParams, NO_PERSISTENCE, follower2Collector) @@ -1073,7 +1083,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { // Send ChangeServersVotingStatus message leaderActor.tell(new ChangeServersVotingStatus(ImmutableMap.of(LEADER_ID, false)), testKit.getRef()); - ServerChangeReply reply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), ServerChangeReply.class); + ServerChangeReply reply = testKit.expectMsgClass(testKit.duration("5 seconds"), ServerChangeReply.class); assertEquals("getStatus", ServerChangeStatus.OK, reply.getStatus()); MessageCollectorActor.expectFirstMatching(leaderCollector, ApplyState.class); @@ -1105,7 +1115,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { .withDispatcher(Dispatchers.DefaultDispatcherId()), actorFactory.generateActorId(LEADER_ID)); leaderActor.tell(new ChangeServersVotingStatus(ImmutableMap.of(LEADER_ID, false)), testKit.getRef()); - ServerChangeReply reply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), ServerChangeReply.class); + ServerChangeReply reply = testKit.expectMsgClass(testKit.duration("5 seconds"), ServerChangeReply.class); assertEquals("getStatus", ServerChangeStatus.INVALID_REQUEST, reply.getStatus()); LOG.info("testChangeLeaderToNonVotingInSingleNode ending"); @@ -1138,17 +1148,15 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { InMemoryJournal.addEntry(node2ID, 2, persistedServerConfigEntry); InMemoryJournal.addEntry(node2ID, 3, new ApplyJournalEntries(0)); - TestActorRef node1Collector = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId("collector")); + ActorRef node1Collector = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("collector")); TestActorRef node1RaftActorRef = actorFactory.createTestActor( CollectingMockRaftActor.props(node1ID, ImmutableMap.of(), configParams, PERSISTENT, node1Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), node1ID); CollectingMockRaftActor node1RaftActor = node1RaftActorRef.underlyingActor(); - TestActorRef node2Collector = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId("collector")); + ActorRef node2Collector = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("collector")); TestActorRef node2RaftActorRef = actorFactory.createTestActor( CollectingMockRaftActor.props(node2ID, ImmutableMap.of(), configParams, PERSISTENT, node2Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), node2ID); @@ -1180,28 +1188,28 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { ChangeServersVotingStatus changeServers = new ChangeServersVotingStatus(ImmutableMap.of(node1ID, true, node2ID, true, "downNode1", false, "downNode2", false)); node1RaftActorRef.tell(changeServers, testKit.getRef()); - ServerChangeReply reply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), ServerChangeReply.class); + ServerChangeReply reply = testKit.expectMsgClass(testKit.duration("5 seconds"), ServerChangeReply.class); assertEquals("getStatus", ServerChangeStatus.NO_LEADER, reply.getStatus()); + assertEquals("getRaftState", RaftState.Follower, node1RaftActor.getRaftState()); // Send an AppendEntries so node1 has a leaderId - MessageCollectorActor.clearMessages(node1Collector); - long term = node1RaftActor.getRaftActorContext().getTermInformation().getCurrentTerm(); node1RaftActorRef.tell(new AppendEntries(term, "downNode1", -1L, -1L, Collections.emptyList(), 0, -1, (short)1), ActorRef.noSender()); - // Wait for the ElectionTimeout to clear the leaderId. he leaderId must be null so on the + // Wait for the ElectionTimeout to clear the leaderId. The leaderId must be null so on the next // ChangeServersVotingStatus message, it will try to elect a leader. - MessageCollectorActor.expectFirstMatching(node1Collector, ElectionTimeout.class); + AbstractRaftActorIntegrationTest.verifyRaftState(node1RaftActorRef, + rs -> assertEquals("getLeader", null, rs.getLeader())); // Update node2's peer address and send the message again node1RaftActor.setPeerAddress(node2ID, node2RaftActorRef.path().toString()); node1RaftActorRef.tell(changeServers, testKit.getRef()); - reply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), ServerChangeReply.class); + reply = testKit.expectMsgClass(testKit.duration("5 seconds"), ServerChangeReply.class); assertEquals("getStatus", ServerChangeStatus.OK, reply.getStatus()); ApplyJournalEntries apply = MessageCollectorActor.expectFirstMatching(node1Collector, @@ -1248,9 +1256,8 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { configParams1.setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS)); configParams1.setElectionTimeoutFactor(1); configParams1.setPeerAddressResolver(peerAddressResolver); - TestActorRef node1Collector = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId("collector")); + ActorRef node1Collector = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("collector")); TestActorRef node1RaftActorRef = actorFactory.createTestActor( CollectingMockRaftActor.props(node1ID, ImmutableMap.of(), configParams1, PERSISTENT, node1Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), node1ID); @@ -1259,9 +1266,8 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { DefaultConfigParamsImpl configParams2 = new DefaultConfigParamsImpl(); configParams2.setElectionTimeoutFactor(1000000); configParams2.setPeerAddressResolver(peerAddressResolver); - TestActorRef node2Collector = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId("collector")); + ActorRef node2Collector = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("collector")); TestActorRef node2RaftActorRef = actorFactory.createTestActor( CollectingMockRaftActor.props(node2ID, ImmutableMap.of(), configParams2, PERSISTENT, node2Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), node2ID); @@ -1277,7 +1283,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { ChangeServersVotingStatus changeServers = new ChangeServersVotingStatus(ImmutableMap.of(node1ID, true)); node1RaftActorRef.tell(changeServers, testKit.getRef()); - ServerChangeReply reply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), ServerChangeReply.class); + ServerChangeReply reply = testKit.expectMsgClass(testKit.duration("5 seconds"), ServerChangeReply.class); assertEquals("getStatus", ServerChangeStatus.NO_LEADER, reply.getStatus()); assertEquals("Server config", Sets.newHashSet(nonVotingServer(node1ID), votingServer(node2ID)), @@ -1315,17 +1321,15 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { new MockRaftActorContext.MockPayload("2"))); InMemoryJournal.addEntry(node2ID, 4, new ApplyJournalEntries(1)); - TestActorRef node1Collector = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId("collector")); + ActorRef node1Collector = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("collector")); TestActorRef node1RaftActorRef = actorFactory.createTestActor( CollectingMockRaftActor.props(node1ID, ImmutableMap.of(), configParams, PERSISTENT, node1Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), node1ID); final CollectingMockRaftActor node1RaftActor = node1RaftActorRef.underlyingActor(); - TestActorRef node2Collector = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId("collector")); + ActorRef node2Collector = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("collector")); TestActorRef node2RaftActorRef = actorFactory.createTestActor( CollectingMockRaftActor.props(node2ID, ImmutableMap.of(), configParams, PERSISTENT, node2Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), node2ID); @@ -1339,7 +1343,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { ChangeServersVotingStatus changeServers = new ChangeServersVotingStatus( ImmutableMap.of(node1ID, true, node2ID, true)); node1RaftActorRef.tell(changeServers, testKit.getRef()); - ServerChangeReply reply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), ServerChangeReply.class); + ServerChangeReply reply = testKit.expectMsgClass(testKit.duration("5 seconds"), ServerChangeReply.class); assertEquals("getStatus", ServerChangeStatus.OK, reply.getStatus()); MessageCollectorActor.expectFirstMatching(node2Collector, ApplyJournalEntries.class); @@ -1380,17 +1384,15 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { InMemoryJournal.addEntry(node2ID, 1, new UpdateElectionTerm(1, "node1")); InMemoryJournal.addEntry(node2ID, 2, persistedServerConfigEntry); - TestActorRef node1Collector = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId("collector")); + ActorRef node1Collector = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("collector")); TestActorRef node1RaftActorRef = actorFactory.createTestActor( CollectingMockRaftActor.props(node1ID, ImmutableMap.of(), configParams, PERSISTENT, node1Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), node1ID); final CollectingMockRaftActor node1RaftActor = node1RaftActorRef.underlyingActor(); - TestActorRef node2Collector = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId("collector")); + ActorRef node2Collector = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("collector")); TestActorRef node2RaftActorRef = actorFactory.createTestActor( CollectingMockRaftActor.props(node2ID, ImmutableMap.of(), configParams, PERSISTENT, node2Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), node2ID); @@ -1411,7 +1413,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { node2RaftActorRef.tell(TimeoutNow.INSTANCE, ActorRef.noSender()); - ServerChangeReply reply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), ServerChangeReply.class); + ServerChangeReply reply = testKit.expectMsgClass(testKit.duration("5 seconds"), ServerChangeReply.class); assertEquals("getStatus", ServerChangeStatus.OK, reply.getStatus()); MessageCollectorActor.expectFirstMatching(node1Collector, ApplyJournalEntries.class); @@ -1449,14 +1451,13 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { return new ServerInfo(id, false); } - private TestActorRef newLeaderCollectorActor(MockLeaderRaftActor leaderRaftActor) { + private ActorRef newLeaderCollectorActor(MockLeaderRaftActor leaderRaftActor) { return newCollectorActor(leaderRaftActor, LEADER_ID); } - private TestActorRef newCollectorActor(AbstractMockRaftActor raftActor, String id) { - TestActorRef collectorActor = actorFactory.createTestActor( - MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), - actorFactory.generateActorId(id + "Collector")); + private ActorRef newCollectorActor(AbstractMockRaftActor raftActor, String id) { + ActorRef collectorActor = actorFactory.createTestActor( + MessageCollectorActor.props(), actorFactory.generateActorId(id + "Collector")); raftActor.setCollectorActor(collectorActor); return collectorActor; } @@ -1468,11 +1469,11 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { assertEquals("Server config", Sets.newHashSet(expected), Sets.newHashSet(payload.getServerConfig())); } - private static RaftActorContextImpl newFollowerContext(String id, TestActorRef actor) { + private static RaftActorContextImpl newFollowerContext(String id, TestActorRef actor) { DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl(); configParams.setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS)); configParams.setElectionTimeoutFactor(100000); - NonPersistentDataProvider noPersistence = new NonPersistentDataProvider(); + NonPersistentDataProvider noPersistence = new NonPersistentDataProvider(Runnable::run); ElectionTermImpl termInfo = new ElectionTermImpl(noPersistence, id, LOG); termInfo.update(1, LEADER_ID); return new RaftActorContextImpl(actor, actor.underlyingActor().getContext(), @@ -1481,11 +1482,11 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { } abstract static class AbstractMockRaftActor extends MockRaftActor { - private volatile TestActorRef collectorActor; + private volatile ActorRef collectorActor; private volatile Class dropMessageOfType; AbstractMockRaftActor(String id, Map peerAddresses, Optional config, - boolean persistent, TestActorRef collectorActor) { + boolean persistent, ActorRef collectorActor) { super(builder().id(id).peerAddresses(peerAddresses).config(config.get()) .persistent(Optional.of(persistent))); this.collectorActor = collectorActor; @@ -1495,7 +1496,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { this.dropMessageOfType = dropMessageOfType; } - void setCollectorActor(TestActorRef collectorActor) { + void setCollectorActor(ActorRef collectorActor) { this.collectorActor = collectorActor; } @@ -1514,22 +1515,29 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { public static class CollectingMockRaftActor extends AbstractMockRaftActor { CollectingMockRaftActor(String id, Map peerAddresses, Optional config, - boolean persistent, TestActorRef collectorActor) { + boolean persistent, ActorRef collectorActor) { super(id, peerAddresses, config, persistent, collectorActor); snapshotCohortDelegate = new RaftActorSnapshotCohort() { @Override - public void createSnapshot(ActorRef actorRef) { - actorRef.tell(new CaptureSnapshotReply(new byte[0]), actorRef); + public void createSnapshot(ActorRef actorRef, java.util.Optional installSnapshotStream) { + actorRef.tell(new CaptureSnapshotReply(ByteState.empty(), installSnapshotStream), actorRef); } @Override - public void applySnapshot(byte[] snapshotBytes) { + public void applySnapshot( + org.opendaylight.controller.cluster.raft.persisted.Snapshot.State snapshotState) { + } + + @Override + public org.opendaylight.controller.cluster.raft.persisted.Snapshot.State deserializeSnapshot( + ByteSource snapshotBytes) { + throw new UnsupportedOperationException(); } }; } public static Props props(final String id, final Map peerAddresses, - ConfigParams config, boolean persistent, TestActorRef collectorActor) { + ConfigParams config, boolean persistent, ActorRef collectorActor) { return Props.create(CollectingMockRaftActor.class, id, peerAddresses, Optional.of(config), persistent, collectorActor); @@ -1564,12 +1572,13 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { @Override @SuppressWarnings("checkstyle:IllegalCatch") - public void createSnapshot(ActorRef actorRef) { - try { - actorRef.tell(new CaptureSnapshotReply(RaftActorTest.fromObject(getState()).toByteArray()), actorRef); - } catch (Exception e) { - LOG.error("createSnapshot failed", e); + public void createSnapshot(ActorRef actorRef, java.util.Optional installSnapshotStream) { + MockSnapshotState snapshotState = new MockSnapshotState(new ArrayList<>(getState())); + if (installSnapshotStream.isPresent()) { + SerializationUtils.serialize(snapshotState, installSnapshotStream.get()); } + + actorRef.tell(new CaptureSnapshotReply(snapshotState, installSnapshotStream), actorRef); } static Props props(Map peerAddresses, RaftActorContext fromContext) { @@ -1581,13 +1590,13 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest { } public static class MockNewFollowerRaftActor extends AbstractMockRaftActor { - public MockNewFollowerRaftActor(ConfigParams config, TestActorRef collectorActor) { + public MockNewFollowerRaftActor(ConfigParams config, ActorRef collectorActor) { super(NEW_SERVER_ID, Maps.newHashMap(), Optional.of(config), NO_PERSISTENCE, collectorActor); setPersistence(false); } - static Props props(ConfigParams config, TestActorRef collectorActor) { + static Props props(ConfigParams config, ActorRef collectorActor) { return Props.create(MockNewFollowerRaftActor.class, config, collectorActor); } }