X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FIsolationScenarioTest.java;h=471ea375899487b3aea1b46f916c7e001c316d04;hp=22dff6a6d3036af5fda962920db7df06e9bdd71d;hb=HEAD;hpb=74524984b8e8625f6b8e8c791c584844d49ccf45 diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/IsolationScenarioTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/IsolationScenarioTest.java index 22dff6a6d3..471ea37589 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/IsolationScenarioTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/IsolationScenarioTest.java @@ -9,16 +9,13 @@ package org.opendaylight.controller.cluster.raft; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import static org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor.assertNoneMatching; import static org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor.clearMessages; -import static org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor.expectMatching; import static org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor.expectFirstMatching; +import static org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor.expectMatching; import static org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor.getAllMatching; -import static org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor.assertNoneMatching; -import akka.actor.Actor; import akka.actor.ActorRef; -import akka.actor.Props; -import akka.testkit.TestActorRef; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import java.util.List; @@ -41,8 +38,8 @@ import scala.concurrent.duration.FiniteDuration; * @author Thomas Pantelis */ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { - private TestActorRef follower1NotifierActor; - private TestActorRef leaderNotifierActor; + private ActorRef follower1NotifierActor; + private ActorRef leaderNotifierActor; /** * Isolates the leader after all initial payload entries have been committed and applied on all nodes. While @@ -51,15 +48,15 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { * appropriately. */ @Test - public void testLeaderIsolationWithAllPriorEntriesCommitted() throws Exception { + public void testLeaderIsolationWithAllPriorEntriesCommitted() { testLog.info("testLeaderIsolationWithAllPriorEntriesCommitted starting"); createRaftActors(); // Send an initial payloads and verify replication. - MockPayload payload0 = sendPayloadData(leaderActor, "zero"); - MockPayload payload1 = sendPayloadData(leaderActor, "one"); + final MockPayload payload0 = sendPayloadData(leaderActor, "zero"); + final MockPayload payload1 = sendPayloadData(leaderActor, "one"); verifyApplyJournalEntries(leaderCollectorActor, 1); verifyApplyJournalEntries(follower1CollectorActor, 1); verifyApplyJournalEntries(follower2CollectorActor, 1); @@ -70,7 +67,7 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { testLog.info("Sending payload to isolated leader"); - MockPayload isolatedLeaderPayload2 = sendPayloadData(leaderActor, "two"); + final MockPayload isolatedLeaderPayload2 = sendPayloadData(leaderActor, "two"); // Wait for the isolated leader to send AppendEntries to follower1 with the entry at index 2. Note the message // is collected but not forwarded to the follower RaftActor. @@ -84,15 +81,16 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { // The leader should transition to IsolatedLeader. expectFirstMatching(leaderNotifierActor, RoleChanged.class, - rc -> rc.getNewRole().equals(RaftState.IsolatedLeader.name())); + rc -> rc.getNewRole().equals(RaftState.IsolatedLeader.name())); forceElectionOnFollower1(); - // Send a payload to the new leader follower1 with index 2 and verify it's replicated to follower2 and committed. + // Send a payload to the new leader follower1 with index 2 and verify it's replicated to follower2 + // and committed. testLog.info("Sending payload to new leader"); - MockPayload newLeaderPayload2 = sendPayloadData(follower1Actor, "two-new"); + final MockPayload newLeaderPayload2 = sendPayloadData(follower1Actor, "two-new"); verifyApplyJournalEntries(follower1CollectorActor, 2); verifyApplyJournalEntries(follower2CollectorActor, 2); @@ -109,7 +107,8 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { // Previous leader should switch to follower b/c it will receive either an AppendEntries or AppendEntriesReply // with a higher term. - expectFirstMatching(leaderNotifierActor, RoleChanged.class, rc -> rc.getNewRole().equals(RaftState.Follower.name())); + expectFirstMatching(leaderNotifierActor, RoleChanged.class, + rc -> rc.getNewRole().equals(RaftState.Follower.name())); // The previous leader has a conflicting log entry at index 2 with a different term which should get // replaced by the new leader's index 1 entry. @@ -134,14 +133,14 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { * sides should reconcile their logs appropriately. */ @Test - public void testLeaderIsolationWithPriorUncommittedEntryAndOneConflictingEntry() throws Exception { + public void testLeaderIsolationWithPriorUncommittedEntryAndOneConflictingEntry() { testLog.info("testLeaderIsolationWithPriorUncommittedEntryAndOneConflictingEntry starting"); createRaftActors(); // Submit an initial payload that is committed/applied on all nodes. - MockPayload payload0 = sendPayloadData(leaderActor, "zero"); + final MockPayload payload0 = sendPayloadData(leaderActor, "zero"); verifyApplyJournalEntries(leaderCollectorActor, 0); verifyApplyJournalEntries(follower1CollectorActor, 0); verifyApplyJournalEntries(follower2CollectorActor, 0); @@ -158,15 +157,13 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { // Wait for the isolated leader to send AppendEntries to the followers with the new entry with index 1. This // message is forwarded to the followers. - expectFirstMatching(follower1CollectorActor, AppendEntries.class, ae -> { - return ae.getEntries().size() == 1 && ae.getEntries().get(0).getIndex() == 1 && - ae.getEntries().get(0).getData().equals(payload1); - }); + expectFirstMatching(follower1CollectorActor, AppendEntries.class, ae -> + ae.getEntries().size() == 1 && ae.getEntries().get(0).getIndex() == 1 + && ae.getEntries().get(0).getData().equals(payload1)); - expectFirstMatching(follower2CollectorActor, AppendEntries.class, ae -> { - return ae.getEntries().size() == 1 && ae.getEntries().get(0).getIndex() == 1 && - ae.getEntries().get(0).getData().equals(payload1); - }); + expectFirstMatching(follower2CollectorActor, AppendEntries.class, ae -> + ae.getEntries().size() == 1 && ae.getEntries().get(0).getIndex() == 1 + && ae.getEntries().get(0).getData().equals(payload1)); verifyApplyJournalEntries(leaderCollectorActor, 1); @@ -176,7 +173,7 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { testLog.info("Sending payload to isolated leader"); - MockPayload isolatedLeaderPayload2 = sendPayloadData(leaderActor, "two"); + final MockPayload isolatedLeaderPayload2 = sendPayloadData(leaderActor, "two"); // Wait for the isolated leader to send AppendEntries to follower1 with the entry at index 2. Note the message // is collected but not forwarded to the follower RaftActor. @@ -190,7 +187,7 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { // The leader should transition to IsolatedLeader. expectFirstMatching(leaderNotifierActor, RoleChanged.class, - rc -> rc.getNewRole().equals(RaftState.IsolatedLeader.name())); + rc -> rc.getNewRole().equals(RaftState.IsolatedLeader.name())); forceElectionOnFollower1(); @@ -200,7 +197,7 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { testLog.info("Sending payload to new leader"); - MockPayload newLeaderPayload2 = sendPayloadData(follower1Actor, "two-new"); + final MockPayload newLeaderPayload2 = sendPayloadData(follower1Actor, "two-new"); verifyApplyJournalEntries(follower1CollectorActor, 3); verifyApplyJournalEntries(follower2CollectorActor, 3); @@ -217,7 +214,8 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { // Previous leader should switch to follower b/c it will receive either an AppendEntries or AppendEntriesReply // with a higher term. - expectFirstMatching(leaderNotifierActor, RoleChanged.class, rc -> rc.getNewRole().equals(RaftState.Follower.name())); + expectFirstMatching(leaderNotifierActor, RoleChanged.class, + rc -> rc.getNewRole().equals(RaftState.Follower.name())); // The previous leader has a conflicting log entry at index 2 with a different term which should get // replaced by the new leader's entry. @@ -236,8 +234,8 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { // Ensure the prior leader didn't apply its conflicting entry with index 2, term 1. List applyState = getAllMatching(leaderCollectorActor, ApplyState.class); - for(ApplyState as: applyState) { - if(as.getReplicatedLogEntry().getIndex() == 2 && as.getReplicatedLogEntry().getTerm() == 1) { + for (ApplyState as: applyState) { + if (as.getReplicatedLogEntry().getIndex() == 2 && as.getReplicatedLogEntry().getTerm() == 1) { fail("Got unexpected ApplyState: " + as); } } @@ -256,14 +254,14 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { * and both sides should reconcile their logs appropriately. */ @Test - public void testLeaderIsolationWithPriorUncommittedEntryAndMultipleConflictingEntries() throws Exception { + public void testLeaderIsolationWithPriorUncommittedEntryAndMultipleConflictingEntries() { testLog.info("testLeaderIsolationWithPriorUncommittedEntryAndMultipleConflictingEntries starting"); createRaftActors(); // Submit an initial payload that is committed/applied on all nodes. - MockPayload payload0 = sendPayloadData(leaderActor, "zero"); + final MockPayload payload0 = sendPayloadData(leaderActor, "zero"); verifyApplyJournalEntries(leaderCollectorActor, 0); verifyApplyJournalEntries(follower1CollectorActor, 0); verifyApplyJournalEntries(follower2CollectorActor, 0); @@ -280,15 +278,13 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { // Wait for the isolated leader to send AppendEntries to the followers with the new entry with index 1. This // message is forwarded to the followers. - expectFirstMatching(follower1CollectorActor, AppendEntries.class, ae -> { - return ae.getEntries().size() == 1 && ae.getEntries().get(0).getIndex() == 1 && - ae.getEntries().get(0).getData().equals(payload1); - }); + expectFirstMatching(follower1CollectorActor, AppendEntries.class, ae -> + ae.getEntries().size() == 1 && ae.getEntries().get(0).getIndex() == 1 + && ae.getEntries().get(0).getData().equals(payload1)); - expectFirstMatching(follower2CollectorActor, AppendEntries.class, ae -> { - return ae.getEntries().size() == 1 && ae.getEntries().get(0).getIndex() == 1 && - ae.getEntries().get(0).getData().equals(payload1); - }); + expectFirstMatching(follower2CollectorActor, AppendEntries.class, ae -> + ae.getEntries().size() == 1 && ae.getEntries().get(0).getIndex() == 1 + && ae.getEntries().get(0).getData().equals(payload1)); verifyApplyJournalEntries(leaderCollectorActor, 1); @@ -306,8 +302,8 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { // are collected but not forwarded to the follower RaftActor. expectFirstMatching(follower1CollectorActor, AppendEntries.class, ae -> { - for(ReplicatedLogEntry e: ae.getEntries()) { - if(e.getIndex() == 4) { + for (ReplicatedLogEntry e: ae.getEntries()) { + if (e.getIndex() == 4) { return true; } } @@ -317,7 +313,7 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { // The leader should transition to IsolatedLeader. expectFirstMatching(leaderNotifierActor, RoleChanged.class, - rc -> rc.getNewRole().equals(RaftState.IsolatedLeader.name())); + rc -> rc.getNewRole().equals(RaftState.IsolatedLeader.name())); forceElectionOnFollower1(); @@ -327,9 +323,9 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { testLog.info("Sending 3 payloads to new leader"); - MockPayload newLeaderPayload2 = sendPayloadData(follower1Actor, "two-new"); - MockPayload newLeaderPayload3 = sendPayloadData(follower1Actor, "three-new"); - MockPayload newLeaderPayload4 = sendPayloadData(follower1Actor, "four-new"); + final MockPayload newLeaderPayload2 = sendPayloadData(follower1Actor, "two-new"); + final MockPayload newLeaderPayload3 = sendPayloadData(follower1Actor, "three-new"); + final MockPayload newLeaderPayload4 = sendPayloadData(follower1Actor, "four-new"); verifyApplyJournalEntries(follower1CollectorActor, 5); verifyApplyJournalEntries(follower2CollectorActor, 5); @@ -346,7 +342,8 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { // Previous leader should switch to follower b/c it will receive either an AppendEntries or AppendEntriesReply // with a higher term. - expectFirstMatching(leaderNotifierActor, RoleChanged.class, rc -> rc.getNewRole().equals(RaftState.Follower.name())); + expectFirstMatching(leaderNotifierActor, RoleChanged.class, + rc -> rc.getNewRole().equals(RaftState.Follower.name())); // The previous leader has conflicting log entries starting at index 2 with different terms which should get // replaced by the new leader's entries. @@ -365,8 +362,8 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { // Ensure the prior leader didn't apply any of its conflicting entries with term 1. List applyState = getAllMatching(leaderCollectorActor, ApplyState.class); - for(ApplyState as: applyState) { - if(as.getReplicatedLogEntry().getTerm() == 1) { + for (ApplyState as: applyState) { + if (as.getReplicatedLogEntry().getTerm() == 1) { fail("Got unexpected ApplyState: " + as); } } @@ -398,7 +395,7 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { follower1Actor.tell(TimeoutNow.INSTANCE, ActorRef.noSender()); expectFirstMatching(follower1NotifierActor, RoleChanged.class, - rc -> rc.getNewRole().equals(RaftState.Leader.name())); + rc -> rc.getNewRole().equals(RaftState.Leader.name())); currentTerm = follower1Context.getTermInformation().getCurrentTerm(); } @@ -411,8 +408,10 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { leaderActor.underlyingActor().startDropMessages(AppendEntries.class); leaderActor.underlyingActor().startDropMessages(RequestVote.class); - follower1Actor.underlyingActor().startDropMessages(AppendEntries.class, ae -> ae.getLeaderId().equals(leaderId)); - follower2Actor.underlyingActor().startDropMessages(AppendEntries.class, ae -> ae.getLeaderId().equals(leaderId)); + follower1Actor.underlyingActor().startDropMessages(AppendEntries.class, + ae -> ae.getLeaderId().equals(leaderId)); + follower2Actor.underlyingActor().startDropMessages(AppendEntries.class, + ae -> ae.getLeaderId().equals(leaderId)); clearMessages(follower1CollectorActor); clearMessages(follower1NotifierActor); @@ -422,31 +421,31 @@ public class IsolationScenarioTest extends AbstractRaftActorIntegrationTest { private void createRaftActors() { testLog.info("createRaftActors starting"); - follower1NotifierActor = factory.createTestActor(Props.create(MessageCollectorActor.class), + follower1NotifierActor = factory.createActor(MessageCollectorActor.props(), factory.generateActorId(follower1Id + "-notifier")); DefaultConfigParamsImpl followerConfigParams = new DefaultConfigParamsImpl(); followerConfigParams.setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS)); followerConfigParams.setElectionTimeoutFactor(1000); follower1Actor = newTestRaftActor(follower1Id, TestRaftActor.newBuilder().peerAddresses( - ImmutableMap.of(leaderId, testActorPath(leaderId), follower2Id, testActorPath(follower2Id))). - config(followerConfigParams).roleChangeNotifier(follower1NotifierActor)); + ImmutableMap.of(leaderId, testActorPath(leaderId), follower2Id, testActorPath(follower2Id))) + .config(followerConfigParams).roleChangeNotifier(follower1NotifierActor)); follower2Actor = newTestRaftActor(follower2Id, ImmutableMap.of(leaderId, testActorPath(leaderId), follower1Id, testActorPath(follower1Id)), followerConfigParams); - peerAddresses = ImmutableMap.builder(). - put(follower1Id, follower1Actor.path().toString()). - put(follower2Id, follower2Actor.path().toString()).build(); + peerAddresses = ImmutableMap.builder() + .put(follower1Id, follower1Actor.path().toString()) + .put(follower2Id, follower2Actor.path().toString()).build(); leaderConfigParams = newLeaderConfigParams(); leaderConfigParams.setIsolatedLeaderCheckInterval(new FiniteDuration(500, TimeUnit.MILLISECONDS)); - leaderNotifierActor = factory.createTestActor(Props.create(MessageCollectorActor.class), + leaderNotifierActor = factory.createActor(MessageCollectorActor.props(), factory.generateActorId(leaderId + "-notifier")); - leaderActor = newTestRaftActor(leaderId, TestRaftActor.newBuilder().peerAddresses(peerAddresses). - config(leaderConfigParams).roleChangeNotifier(leaderNotifierActor)); + leaderActor = newTestRaftActor(leaderId, TestRaftActor.newBuilder().peerAddresses(peerAddresses) + .config(leaderConfigParams).roleChangeNotifier(leaderNotifierActor)); follower1CollectorActor = follower1Actor.underlyingActor().collectorActor(); follower2CollectorActor = follower2Actor.underlyingActor().collectorActor();