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%2Fbehaviors%2FFollowerTest.java;h=ab8218808f3cd5f65dfa33ead6825c881efd0603;hp=b01fd33914f9500c8537cc86032aee924b54f7ed;hb=6dbf8f82cfa9fe8c35e4085213a55cb887cc3aee;hpb=95d7b8820236d16cb7e37c4a95fcae6f6d55581e diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerTest.java index b01fd33914..ab8218808f 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerTest.java @@ -18,27 +18,39 @@ import static org.mockito.Matchers.any; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; + import akka.actor.ActorRef; -import akka.actor.Props; +import akka.dispatch.Dispatchers; +import akka.testkit.JavaTestKit; import akka.testkit.TestActorRef; +import com.google.common.base.Optional; import com.google.common.base.Stopwatch; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.io.ByteSource; import com.google.common.util.concurrent.Uninterruptibles; import com.google.protobuf.ByteString; +import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; import org.junit.After; import org.junit.Assert; import org.junit.Test; import org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl; +import org.opendaylight.controller.cluster.raft.MockRaftActor; +import org.opendaylight.controller.cluster.raft.MockRaftActor.Builder; +import org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState; import org.opendaylight.controller.cluster.raft.MockRaftActorContext; import org.opendaylight.controller.cluster.raft.RaftActorContext; +import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort; import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry; -import org.opendaylight.controller.cluster.raft.Snapshot; import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot; +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.FollowerInitialSyncUpStatus; import org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow; @@ -49,18 +61,27 @@ import org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply; import org.opendaylight.controller.cluster.raft.messages.RaftRPC; import org.opendaylight.controller.cluster.raft.messages.RequestVote; import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply; +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; +import org.opendaylight.controller.cluster.raft.persisted.Snapshot; +import org.opendaylight.controller.cluster.raft.persisted.Snapshot.State; +import org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm; +import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy; +import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal; +import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore; import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor; import scala.concurrent.duration.FiniteDuration; public class FollowerTest extends AbstractRaftActorBehaviorTest { - private final TestActorRef followerActor = actorFactory.createTestActor( - Props.create(MessageCollectorActor.class), actorFactory.generateActorId("follower")); + private final ActorRef followerActor = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("follower")); - private final TestActorRef leaderActor = actorFactory.createTestActor( - Props.create(MessageCollectorActor.class), actorFactory.generateActorId("leader")); + private final ActorRef leaderActor = actorFactory.createActor( + MessageCollectorActor.props(), actorFactory.generateActorId("leader")); private Follower follower; @@ -68,8 +89,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { @Override @After - public void tearDown() throws Exception { - if(follower != null) { + public void tearDown() { + if (follower != null) { follower.close(); } @@ -77,7 +98,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Override - protected Follower createBehavior(RaftActorContext actorContext) { + protected Follower createBehavior(final RaftActorContext actorContext) { return spy(new Follower(actorContext)); } @@ -87,14 +108,14 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Override - protected MockRaftActorContext createActorContext(ActorRef actorRef){ + protected MockRaftActorContext createActorContext(final ActorRef actorRef) { MockRaftActorContext context = new MockRaftActorContext("follower", getSystem(), actorRef); - context.setPayloadVersion(payloadVersion ); + context.setPayloadVersion(payloadVersion); return context; } @Test - public void testThatAnElectionTimeoutIsTriggered(){ + public void testThatAnElectionTimeoutIsTriggered() { MockRaftActorContext actorContext = createActorContext(); follower = new Follower(actorContext); @@ -121,15 +142,15 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { logStart("testHandleElectionTimeoutWhenLeaderMessageReceived"); MockRaftActorContext context = createActorContext(); - ((DefaultConfigParamsImpl) context.getConfigParams()). - setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS)); + ((DefaultConfigParamsImpl) context.getConfigParams()) + .setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS)); ((DefaultConfigParamsImpl) context.getConfigParams()).setElectionTimeoutFactor(4); follower = new Follower(context); context.setCurrentBehavior(follower); - Uninterruptibles.sleepUninterruptibly(context.getConfigParams(). - getElectionTimeOutInterval().toMillis() - 100, TimeUnit.MILLISECONDS); + Uninterruptibles.sleepUninterruptibly(context.getConfigParams() + .getElectionTimeOutInterval().toMillis() - 100, TimeUnit.MILLISECONDS); follower.handleMessage(leaderActor, new AppendEntries(1, "leader", -1, -1, Collections.emptyList(), -1, -1, (short) 1)); @@ -137,8 +158,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { RaftActorBehavior raftBehavior = follower.handleMessage(leaderActor, ElectionTimeout.INSTANCE); assertTrue(raftBehavior instanceof Follower); - Uninterruptibles.sleepUninterruptibly(context.getConfigParams(). - getElectionTimeOutInterval().toMillis() - 150, TimeUnit.MILLISECONDS); + Uninterruptibles.sleepUninterruptibly(context.getConfigParams() + .getElectionTimeOutInterval().toMillis() - 150, TimeUnit.MILLISECONDS); follower.handleMessage(leaderActor, new AppendEntries(1, "leader", -1, -1, Collections.emptyList(), -1, -1, (short) 1)); @@ -167,7 +188,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForIsNotTheSameAsCandidateId(){ + public void testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForIsNotTheSameAsCandidateId() { logStart("testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForIsNotTheSameAsCandidateId"); MockRaftActorContext context = createActorContext(); @@ -186,7 +207,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { @Test - public void testHandleFirstAppendEntries() throws Exception { + public void testHandleFirstAppendEntries() { logStart("testHandleFirstAppendEntries"); MockRaftActorContext context = createActorContext(); @@ -205,7 +226,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { follower = createBehavior(context); follower.handleMessage(leaderActor, appendEntries); - FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class); + FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, + FollowerInitialSyncUpStatus.class); AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class); assertFalse(syncStatus.isInitialSyncDone()); @@ -213,7 +235,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testHandleFirstAppendEntriesWithPrevIndexMinusOne() throws Exception { + public void testHandleFirstAppendEntriesWithPrevIndexMinusOne() { logStart("testHandleFirstAppendEntries"); MockRaftActorContext context = createActorContext(); @@ -227,7 +249,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { follower = createBehavior(context); follower.handleMessage(leaderActor, appendEntries); - FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class); + FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, + FollowerInitialSyncUpStatus.class); AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class); assertFalse(syncStatus.isInitialSyncDone()); @@ -235,7 +258,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testHandleFirstAppendEntriesWithPrevIndexMinusOneAndReplicatedToAllIndexPresentInLog() throws Exception { + public void testHandleFirstAppendEntriesWithPrevIndexMinusOneAndReplicatedToAllIndexPresentInLog() { logStart("testHandleFirstAppendEntries"); MockRaftActorContext context = createActorContext(); @@ -252,7 +275,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { follower = createBehavior(context); follower.handleMessage(leaderActor, appendEntries); - FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class); + FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, + FollowerInitialSyncUpStatus.class); AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class); assertFalse(syncStatus.isInitialSyncDone()); @@ -260,7 +284,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testHandleFirstAppendEntriesWithPrevIndexMinusOneAndReplicatedToAllIndexPresentInSnapshot() throws Exception { + public void testHandleFirstAppendEntriesWithPrevIndexMinusOneAndReplicatedToAllIndexPresentInSnapshot() { logStart("testHandleFirstAppendEntries"); MockRaftActorContext context = createActorContext(); @@ -276,7 +300,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { follower = createBehavior(context); follower.handleMessage(leaderActor, appendEntries); - FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class); + FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, + FollowerInitialSyncUpStatus.class); AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class); assertFalse(syncStatus.isInitialSyncDone()); @@ -284,8 +309,9 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testHandleFirstAppendEntriesWithPrevIndexMinusOneAndReplicatedToAllIndexPresentInSnapshotButCalculatedPreviousEntryMissing() throws Exception { - logStart("testHandleFirstAppendEntries"); + public void testFirstAppendEntriesWithNoPrevIndexAndReplToAllPresentInSnapshotButCalculatedPrevEntryMissing() { + logStart( + "testFirstAppendEntriesWithNoPrevIndexAndReplicatedToAllPresentInSnapshotButCalculatedPrevEntryMissing"); MockRaftActorContext context = createActorContext(); context.getReplicatedLog().clear(0,2); @@ -300,7 +326,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { follower = createBehavior(context); follower.handleMessage(leaderActor, appendEntries); - FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class); + FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, + FollowerInitialSyncUpStatus.class); AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class); assertFalse(syncStatus.isInitialSyncDone()); @@ -308,7 +335,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testHandleSyncUpAppendEntries() throws Exception { + public void testHandleSyncUpAppendEntries() { logStart("testHandleSyncUpAppendEntries"); MockRaftActorContext context = createActorContext(); @@ -322,20 +349,19 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { follower = createBehavior(context); follower.handleMessage(leaderActor, appendEntries); - FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class); + FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, + FollowerInitialSyncUpStatus.class); assertFalse(syncStatus.isInitialSyncDone()); // Clear all the messages - followerActor.underlyingActor().clear(); + MessageCollectorActor.clearMessages(followerActor); context.setLastApplied(101); context.setCommitIndex(101); - setLastLogEntry(context, 1, 101, - new MockRaftActorContext.MockPayload("")); + setLastLogEntry(context, 1, 101, new MockRaftActorContext.MockPayload("")); - entries = Arrays.asList( - newReplicatedLogEntry(2, 101, "foo")); + entries = Arrays.asList(newReplicatedLogEntry(2, 101, "foo")); // The new commitIndex is 101 appendEntries = new AppendEntries(2, "leader-1", 101, 1, entries, 102, 101, (short)0); @@ -345,7 +371,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { assertTrue(syncStatus.isInitialSyncDone()); - followerActor.underlyingActor().clear(); + MessageCollectorActor.clearMessages(followerActor); // Sending the same message again should not generate another message @@ -354,11 +380,10 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { syncStatus = MessageCollectorActor.getFirstMatching(followerActor, FollowerInitialSyncUpStatus.class); assertNull(syncStatus); - } @Test - public void testHandleAppendEntriesLeaderChangedBeforeSyncUpComplete() throws Exception { + public void testHandleAppendEntriesLeaderChangedBeforeSyncUpComplete() { logStart("testHandleAppendEntriesLeaderChangedBeforeSyncUpComplete"); MockRaftActorContext context = createActorContext(); @@ -372,12 +397,13 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { follower = createBehavior(context); follower.handleMessage(leaderActor, appendEntries); - FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class); + FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, + FollowerInitialSyncUpStatus.class); assertFalse(syncStatus.isInitialSyncDone()); // Clear all the messages - followerActor.underlyingActor().clear(); + MessageCollectorActor.clearMessages(followerActor); context.setLastApplied(100); setLastLogEntry(context, 1, 100, @@ -394,12 +420,10 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { // We get a new message saying initial status is not done assertFalse(syncStatus.isInitialSyncDone()); - } - @Test - public void testHandleAppendEntriesLeaderChangedAfterSyncUpComplete() throws Exception { + public void testHandleAppendEntriesLeaderChangedAfterSyncUpComplete() { logStart("testHandleAppendEntriesLeaderChangedAfterSyncUpComplete"); MockRaftActorContext context = createActorContext(); @@ -413,12 +437,13 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { follower = createBehavior(context); follower.handleMessage(leaderActor, appendEntries); - FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class); + FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, + FollowerInitialSyncUpStatus.class); assertFalse(syncStatus.isInitialSyncDone()); // Clear all the messages - followerActor.underlyingActor().clear(); + MessageCollectorActor.clearMessages(followerActor); context.setLastApplied(101); context.setCommitIndex(101); @@ -437,7 +462,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { assertTrue(syncStatus.isInitialSyncDone()); // Clear all the messages - followerActor.underlyingActor().clear(); + MessageCollectorActor.clearMessages(followerActor); context.setLastApplied(100); setLastLogEntry(context, 1, 100, @@ -454,20 +479,16 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { // We get a new message saying initial status is not done assertFalse(syncStatus.isInitialSyncDone()); - } - /** * This test verifies that when an AppendEntries RPC is received by a RaftActor * with a commitIndex that is greater than what has been applied to the * state machine of the RaftActor, the RaftActor applies the state and * sets it current applied state to the commitIndex of the sender. - * - * @throws Exception */ @Test - public void testHandleAppendEntriesWithNewerCommitIndex() throws Exception { + public void testHandleAppendEntriesWithNewerCommitIndex() { logStart("testHandleAppendEntriesWithNewerCommitIndex"); MockRaftActorContext context = createActorContext(); @@ -493,8 +514,6 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { * This test verifies that when an AppendEntries is received a specific prevLogTerm * which does not match the term that is in RaftActors log entry at prevLogIndex * then the RaftActor does not change it's state and it returns a failure. - * - * @throws Exception */ @Test public void testHandleAppendEntriesSenderPrevLogTermNotSameAsReceiverPrevLogTerm() { @@ -507,7 +526,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { // AppendEntries is now sent with a bigger term // this will set the receivers term to be the same as the sender's term - AppendEntries appendEntries = new AppendEntries(100, "leader", 0, 0, null, 101, -1, (short)0); + AppendEntries appendEntries = new AppendEntries(100, "leader", 0, 0, Collections.emptyList(), 101, -1, + (short)0); follower = createBehavior(context); @@ -525,9 +545,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { * This test verifies that when a new AppendEntries message is received with * new entries and the logs of the sender and receiver match that the new * entries get added to the log and the log is incremented by the number of - * entries received in appendEntries - * - * @throws Exception + * entries received in appendEntries. */ @Test public void testHandleAppendEntriesAddNewEntries() { @@ -579,7 +597,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { * This test verifies that when a new AppendEntries message is received with * new entries and the logs of the sender and receiver are out-of-sync that * the log is first corrected by removing the out of sync entries from the - * log and then adding in the new entries sent with the AppendEntries message + * log and then adding in the new entries sent with the AppendEntries message. */ @Test public void testHandleAppendEntriesCorrectReceiverLogEntries() { @@ -671,10 +689,10 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testHandleAppendEntriesPreviousLogEntryMissing(){ + public void testHandleAppendEntriesPreviousLogEntryMissing() { logStart("testHandleAppendEntriesPreviousLogEntryMissing"); - MockRaftActorContext context = createActorContext(); + final MockRaftActorContext context = createActorContext(); // Prepare the receivers log MockRaftActorContext.SimpleReplicatedLog log = new MockRaftActorContext.SimpleReplicatedLog(); @@ -730,7 +748,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { entries = Arrays.asList(newReplicatedLogEntry(1, 1, "one"), newReplicatedLogEntry(1, 2, "two")); - leaderActor.underlyingActor().clear(); + MessageCollectorActor.clearMessages(leaderActor); follower.handleMessage(leaderActor, new AppendEntries(1, "leader", 0, 1, entries, 2, -1, (short)0)); assertEquals("Next index", 3, log.last().getIndex() + 1); @@ -741,7 +759,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testHandleAppendEntriesAfterInstallingSnapshot(){ + public void testHandleAppendEntriesAfterInstallingSnapshot() { logStart("testHandleAppendAfterInstallingSnapshot"); MockRaftActorContext context = createActorContext(); @@ -774,11 +792,9 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { /** * This test verifies that when InstallSnapshot is received by * the follower its applied correctly. - * - * @throws Exception */ @Test - public void testHandleInstallSnapshot() throws Exception { + public void testHandleInstallSnapshot() { logStart("testHandleInstallSnapshot"); MockRaftActorContext context = createActorContext(); @@ -786,7 +802,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { follower = createBehavior(context); - ByteString bsSnapshot = createSnapshot(); + ByteString bsSnapshot = createSnapshot(); int offset = 0; int snapshotLength = bsSnapshot.size(); int chunkSize = 50; @@ -795,7 +811,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { int chunkIndex = 1; InstallSnapshot lastInstallSnapshot = null; - for(int i = 0; i < totalChunks; i++) { + for (int i = 0; i < totalChunks; i++) { byte[] chunkData = getNextChunk(bsSnapshot, offset, chunkSize); lastInstallSnapshot = new InstallSnapshot(1, "leader", lastIncludedIndex, 1, chunkData, chunkIndex, totalChunks); @@ -815,7 +831,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { assertEquals("getLastAppliedIndex", lastInstallSnapshot.getLastIncludedIndex(), snapshot.getLastAppliedIndex()); assertEquals("getLastTerm", lastInstallSnapshot.getLastIncludedTerm(), snapshot.getLastTerm()); - Assert.assertArrayEquals("getState", bsSnapshot.toByteArray(), snapshot.getState()); + assertEquals("getState type", ByteState.class, snapshot.getState().getClass()); + Assert.assertArrayEquals("getState", bsSnapshot.toByteArray(), ((ByteState)snapshot.getState()).getBytes()); assertEquals("getElectionTerm", 1, snapshot.getElectionTerm()); assertEquals("getElectionVotedFor", "leader", snapshot.getElectionVotedFor()); applySnapshot.getCallback().onSuccess(); @@ -825,7 +842,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { assertEquals("InstallSnapshotReply count", totalChunks, replies.size()); chunkIndex = 1; - for(InstallSnapshotReply reply: replies) { + for (InstallSnapshotReply reply: replies) { assertEquals("getChunkIndex", chunkIndex++, reply.getChunkIndex()); assertEquals("getTerm", 1, reply.getTerm()); assertEquals("isSuccess", true, reply.isSuccess()); @@ -835,15 +852,12 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { assertNull("Expected null SnapshotTracker", follower.getSnapshotTracker()); } - /** * Verify that when an AppendEntries is sent to a follower during a snapshot install * the Follower short-circuits the processing of the AppendEntries message. - * - * @throws Exception */ @Test - public void testReceivingAppendEntriesDuringInstallSnapshot() throws Exception { + public void testReceivingAppendEntriesDuringInstallSnapshot() { logStart("testReceivingAppendEntriesDuringInstallSnapshot"); MockRaftActorContext context = createActorContext(); @@ -886,7 +900,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testReceivingAppendEntriesDuringInstallSnapshotFromDifferentLeader() throws Exception { + public void testReceivingAppendEntriesDuringInstallSnapshotFromDifferentLeader() { logStart("testReceivingAppendEntriesDuringInstallSnapshotFromDifferentLeader"); MockRaftActorContext context = createActorContext(); @@ -929,7 +943,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testInitialSyncUpWithHandleInstallSnapshotFollowedByAppendEntries() throws Exception { + public void testInitialSyncUpWithHandleInstallSnapshotFollowedByAppendEntries() { logStart("testInitialSyncUpWithHandleInstallSnapshot"); MockRaftActorContext context = createActorContext(); @@ -946,7 +960,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { int chunkIndex = 1; InstallSnapshot lastInstallSnapshot = null; - for(int i = 0; i < totalChunks; i++) { + for (int i = 0; i < totalChunks; i++) { byte[] chunkData = getNextChunk(bsSnapshot, offset, chunkSize); lastInstallSnapshot = new InstallSnapshot(1, "leader", lastIncludedIndex, 1, chunkData, chunkIndex, totalChunks); @@ -962,7 +976,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { assertFalse(syncStatus.isInitialSyncDone()); // Clear all the messages - followerActor.underlyingActor().clear(); + MessageCollectorActor.clearMessages(followerActor); context.setLastApplied(101); context.setCommitIndex(101); @@ -982,7 +996,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testHandleOutOfSequenceInstallSnapshot() throws Exception { + public void testHandleOutOfSequenceInstallSnapshot() { logStart("testHandleOutOfSequenceInstallSnapshot"); MockRaftActorContext context = createActorContext(); @@ -1007,7 +1021,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testFollowerSchedulesElectionTimeoutImmediatelyWhenItHasNoPeers(){ + public void testFollowerSchedulesElectionTimeoutImmediatelyWhenItHasNoPeers() { MockRaftActorContext context = createActorContext(); Stopwatch stopwatch = Stopwatch.createStarted(); @@ -1025,9 +1039,9 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testFollowerSchedulesElectionIfAutomaticElectionsAreDisabled(){ + public void testFollowerSchedulesElectionIfAutomaticElectionsAreDisabled() { MockRaftActorContext context = createActorContext(); - context.setConfigParams(new DefaultConfigParamsImpl(){ + context.setConfigParams(new DefaultConfigParamsImpl() { @Override public FiniteDuration getElectionTimeOutInterval() { return FiniteDuration.apply(100, TimeUnit.MILLISECONDS); @@ -1044,7 +1058,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testFollowerSchedulesElectionIfNonVoting(){ + public void testFollowerSchedulesElectionIfNonVoting() { MockRaftActorContext context = createActorContext(); context.updatePeerIds(new ServerConfigurationPayload(Arrays.asList(new ServerInfo(context.getId(), false)))); ((DefaultConfigParamsImpl)context.getConfigParams()).setHeartBeatInterval( @@ -1061,7 +1075,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testElectionScheduledWhenAnyRaftRPCReceived(){ + public void testElectionScheduledWhenAnyRaftRPCReceived() { MockRaftActorContext context = createActorContext(); follower = createBehavior(context); follower.handleMessage(leaderActor, new RaftRPC() { @@ -1076,14 +1090,231 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Test - public void testElectionNotScheduledWhenNonRaftRPCMessageReceived(){ + public void testElectionNotScheduledWhenNonRaftRPCMessageReceived() { MockRaftActorContext context = createActorContext(); follower = createBehavior(context); follower.handleMessage(leaderActor, "non-raft-rpc"); verify(follower, never()).scheduleElection(any(FiniteDuration.class)); } - public byte[] getNextChunk (ByteString bs, int offset, int chunkSize){ + @Test + public void testCaptureSnapshotOnLastEntryInAppendEntries() { + String id = "testCaptureSnapshotOnLastEntryInAppendEntries"; + logStart(id); + + InMemoryJournal.addEntry(id, 1, new UpdateElectionTerm(1, null)); + + DefaultConfigParamsImpl config = new DefaultConfigParamsImpl(); + config.setSnapshotBatchCount(2); + config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName()); + + final AtomicReference followerRaftActor = new AtomicReference<>(); + RaftActorSnapshotCohort snapshotCohort = newRaftActorSnapshotCohort(followerRaftActor); + Builder builder = MockRaftActor.builder().persistent(Optional.of(true)).id(id) + .peerAddresses(ImmutableMap.of("leader", "")).config(config).snapshotCohort(snapshotCohort); + TestActorRef followerActorRef = actorFactory.createTestActor(builder.props() + .withDispatcher(Dispatchers.DefaultDispatcherId()), id); + followerRaftActor.set(followerActorRef.underlyingActor()); + followerRaftActor.get().waitForInitializeBehaviorComplete(); + + InMemorySnapshotStore.addSnapshotSavedLatch(id); + InMemoryJournal.addDeleteMessagesCompleteLatch(id); + InMemoryJournal.addWriteMessagesCompleteLatch(id, 1, ApplyJournalEntries.class); + + List entries = Arrays.asList( + newReplicatedLogEntry(1, 0, "one"), newReplicatedLogEntry(1, 1, "two")); + + AppendEntries appendEntries = new AppendEntries(1, "leader", -1, -1, entries, 1, -1, (short)0); + + followerActorRef.tell(appendEntries, leaderActor); + + AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class); + assertEquals("isSuccess", true, reply.isSuccess()); + + final Snapshot snapshot = InMemorySnapshotStore.waitForSavedSnapshot(id, Snapshot.class); + + InMemoryJournal.waitForDeleteMessagesComplete(id); + InMemoryJournal.waitForWriteMessagesComplete(id); + // We expect the ApplyJournalEntries for index 1 to remain in the persisted log b/c it's still queued for + // persistence by the time we initiate capture so the last persisted journal sequence number doesn't include it. + // This is OK - on recovery it will be a no-op since index 1 has already been applied. + List journalEntries = InMemoryJournal.get(id, Object.class); + assertEquals("Persisted journal entries size: " + journalEntries, 1, journalEntries.size()); + assertEquals("Persisted journal entry type", ApplyJournalEntries.class, journalEntries.get(0).getClass()); + assertEquals("ApplyJournalEntries index", 1, ((ApplyJournalEntries)journalEntries.get(0)).getToIndex()); + + assertEquals("Snapshot unapplied size", 0, snapshot.getUnAppliedEntries().size()); + assertEquals("Snapshot getLastAppliedTerm", 1, snapshot.getLastAppliedTerm()); + assertEquals("Snapshot getLastAppliedIndex", 1, snapshot.getLastAppliedIndex()); + assertEquals("Snapshot getLastTerm", 1, snapshot.getLastTerm()); + assertEquals("Snapshot getLastIndex", 1, snapshot.getLastIndex()); + assertEquals("Snapshot state", ImmutableList.of(entries.get(0).getData(), entries.get(1).getData()), + MockRaftActor.fromState(snapshot.getState())); + } + + @Test + public void testCaptureSnapshotOnMiddleEntryInAppendEntries() { + String id = "testCaptureSnapshotOnMiddleEntryInAppendEntries"; + logStart(id); + + InMemoryJournal.addEntry(id, 1, new UpdateElectionTerm(1, null)); + + DefaultConfigParamsImpl config = new DefaultConfigParamsImpl(); + config.setSnapshotBatchCount(2); + config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName()); + + final AtomicReference followerRaftActor = new AtomicReference<>(); + RaftActorSnapshotCohort snapshotCohort = newRaftActorSnapshotCohort(followerRaftActor); + Builder builder = MockRaftActor.builder().persistent(Optional.of(true)).id(id) + .peerAddresses(ImmutableMap.of("leader", "")).config(config).snapshotCohort(snapshotCohort); + TestActorRef followerActorRef = actorFactory.createTestActor(builder.props() + .withDispatcher(Dispatchers.DefaultDispatcherId()), id); + followerRaftActor.set(followerActorRef.underlyingActor()); + followerRaftActor.get().waitForInitializeBehaviorComplete(); + + InMemorySnapshotStore.addSnapshotSavedLatch(id); + InMemoryJournal.addDeleteMessagesCompleteLatch(id); + InMemoryJournal.addWriteMessagesCompleteLatch(id, 1, ApplyJournalEntries.class); + + List entries = Arrays.asList( + newReplicatedLogEntry(1, 0, "one"), newReplicatedLogEntry(1, 1, "two"), + newReplicatedLogEntry(1, 2, "three")); + + AppendEntries appendEntries = new AppendEntries(1, "leader", -1, -1, entries, 2, -1, (short)0); + + followerActorRef.tell(appendEntries, leaderActor); + + AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class); + assertEquals("isSuccess", true, reply.isSuccess()); + + final Snapshot snapshot = InMemorySnapshotStore.waitForSavedSnapshot(id, Snapshot.class); + + InMemoryJournal.waitForDeleteMessagesComplete(id); + InMemoryJournal.waitForWriteMessagesComplete(id); + // We expect the ApplyJournalEntries for index 2 to remain in the persisted log b/c it's still queued for + // persistence by the time we initiate capture so the last persisted journal sequence number doesn't include it. + // This is OK - on recovery it will be a no-op since index 2 has already been applied. + List journalEntries = InMemoryJournal.get(id, Object.class); + assertEquals("Persisted journal entries size: " + journalEntries, 1, journalEntries.size()); + assertEquals("Persisted journal entry type", ApplyJournalEntries.class, journalEntries.get(0).getClass()); + assertEquals("ApplyJournalEntries index", 2, ((ApplyJournalEntries)journalEntries.get(0)).getToIndex()); + + assertEquals("Snapshot unapplied size", 0, snapshot.getUnAppliedEntries().size()); + assertEquals("Snapshot getLastAppliedTerm", 1, snapshot.getLastAppliedTerm()); + assertEquals("Snapshot getLastAppliedIndex", 2, snapshot.getLastAppliedIndex()); + assertEquals("Snapshot getLastTerm", 1, snapshot.getLastTerm()); + assertEquals("Snapshot getLastIndex", 2, snapshot.getLastIndex()); + assertEquals("Snapshot state", ImmutableList.of(entries.get(0).getData(), entries.get(1).getData(), + entries.get(2).getData()), MockRaftActor.fromState(snapshot.getState())); + + assertEquals("Journal size", 0, followerRaftActor.get().getReplicatedLog().size()); + assertEquals("Snapshot index", 2, followerRaftActor.get().getReplicatedLog().getSnapshotIndex()); + + // Reinstate the actor from persistence + + actorFactory.killActor(followerActorRef, new JavaTestKit(getSystem())); + + followerActorRef = actorFactory.createTestActor(builder.props() + .withDispatcher(Dispatchers.DefaultDispatcherId()), id); + followerRaftActor.set(followerActorRef.underlyingActor()); + followerRaftActor.get().waitForInitializeBehaviorComplete(); + + assertEquals("Journal size", 0, followerRaftActor.get().getReplicatedLog().size()); + assertEquals("Last index", 2, followerRaftActor.get().getReplicatedLog().lastIndex()); + assertEquals("Last applied index", 2, followerRaftActor.get().getRaftActorContext().getLastApplied()); + assertEquals("Commit index", 2, followerRaftActor.get().getRaftActorContext().getCommitIndex()); + assertEquals("State", ImmutableList.of(entries.get(0).getData(), entries.get(1).getData(), + entries.get(2).getData()), followerRaftActor.get().getState()); + } + + @Test + public void testCaptureSnapshotOnAppendEntriesWithUnapplied() { + String id = "testCaptureSnapshotOnAppendEntriesWithUnapplied"; + logStart(id); + + InMemoryJournal.addEntry(id, 1, new UpdateElectionTerm(1, null)); + + DefaultConfigParamsImpl config = new DefaultConfigParamsImpl(); + config.setSnapshotBatchCount(1); + config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName()); + + final AtomicReference followerRaftActor = new AtomicReference<>(); + RaftActorSnapshotCohort snapshotCohort = newRaftActorSnapshotCohort(followerRaftActor); + Builder builder = MockRaftActor.builder().persistent(Optional.of(true)).id(id) + .peerAddresses(ImmutableMap.of("leader", "")).config(config).snapshotCohort(snapshotCohort); + TestActorRef followerActorRef = actorFactory.createTestActor(builder.props() + .withDispatcher(Dispatchers.DefaultDispatcherId()), id); + followerRaftActor.set(followerActorRef.underlyingActor()); + followerRaftActor.get().waitForInitializeBehaviorComplete(); + + InMemorySnapshotStore.addSnapshotSavedLatch(id); + InMemoryJournal.addDeleteMessagesCompleteLatch(id); + InMemoryJournal.addWriteMessagesCompleteLatch(id, 1, ApplyJournalEntries.class); + + List entries = Arrays.asList( + newReplicatedLogEntry(1, 0, "one"), newReplicatedLogEntry(1, 1, "two"), + newReplicatedLogEntry(1, 2, "three")); + + AppendEntries appendEntries = new AppendEntries(1, "leader", -1, -1, entries, 0, -1, (short)0); + + followerActorRef.tell(appendEntries, leaderActor); + + AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class); + assertEquals("isSuccess", true, reply.isSuccess()); + + final Snapshot snapshot = InMemorySnapshotStore.waitForSavedSnapshot(id, Snapshot.class); + + InMemoryJournal.waitForDeleteMessagesComplete(id); + InMemoryJournal.waitForWriteMessagesComplete(id); + // We expect the ApplyJournalEntries for index 0 to remain in the persisted log b/c it's still queued for + // persistence by the time we initiate capture so the last persisted journal sequence number doesn't include it. + // This is OK - on recovery it will be a no-op since index 0 has already been applied. + List journalEntries = InMemoryJournal.get(id, Object.class); + assertEquals("Persisted journal entries size: " + journalEntries, 1, journalEntries.size()); + assertEquals("Persisted journal entry type", ApplyJournalEntries.class, journalEntries.get(0).getClass()); + assertEquals("ApplyJournalEntries index", 0, ((ApplyJournalEntries)journalEntries.get(0)).getToIndex()); + + assertEquals("Snapshot unapplied size", 2, snapshot.getUnAppliedEntries().size()); + assertEquals("Snapshot unapplied entry index", 1, snapshot.getUnAppliedEntries().get(0).getIndex()); + assertEquals("Snapshot unapplied entry index", 2, snapshot.getUnAppliedEntries().get(1).getIndex()); + assertEquals("Snapshot getLastAppliedTerm", 1, snapshot.getLastAppliedTerm()); + assertEquals("Snapshot getLastAppliedIndex", 0, snapshot.getLastAppliedIndex()); + assertEquals("Snapshot getLastTerm", 1, snapshot.getLastTerm()); + assertEquals("Snapshot getLastIndex", 2, snapshot.getLastIndex()); + assertEquals("Snapshot state", ImmutableList.of(entries.get(0).getData()), + MockRaftActor.fromState(snapshot.getState())); + } + + @SuppressWarnings("checkstyle:IllegalCatch") + private static RaftActorSnapshotCohort newRaftActorSnapshotCohort( + final AtomicReference followerRaftActor) { + RaftActorSnapshotCohort snapshotCohort = new RaftActorSnapshotCohort() { + @Override + public void createSnapshot(final ActorRef actorRef, + final java.util.Optional installSnapshotStream) { + try { + actorRef.tell(new CaptureSnapshotReply(new MockSnapshotState(followerRaftActor.get().getState()), + installSnapshotStream), actorRef); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public void applySnapshot(final State snapshotState) { + } + + @Override + public State deserializeSnapshot(final ByteSource snapshotBytes) { + throw new UnsupportedOperationException(); + } + }; + return snapshotCohort; + } + + public byte[] getNextChunk(final ByteString bs, final int offset, final int chunkSize) { int snapshotLength = bs.size(); int start = offset; int size = chunkSize; @@ -1100,14 +1331,14 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { return nextChunk; } - private void expectAndVerifyAppendEntriesReply(int expTerm, boolean expSuccess, - String expFollowerId, long expLogLastTerm, long expLogLastIndex) { + private void expectAndVerifyAppendEntriesReply(final int expTerm, final boolean expSuccess, + final String expFollowerId, final long expLogLastTerm, final long expLogLastIndex) { expectAndVerifyAppendEntriesReply(expTerm, expSuccess, expFollowerId, expLogLastTerm, expLogLastIndex, false); } - private void expectAndVerifyAppendEntriesReply(int expTerm, boolean expSuccess, - String expFollowerId, long expLogLastTerm, long expLogLastIndex, - boolean expForceInstallSnapshot) { + private void expectAndVerifyAppendEntriesReply(final int expTerm, final boolean expSuccess, + final String expFollowerId, final long expLogLastTerm, final long expLogLastIndex, + final boolean expForceInstallSnapshot) { AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class); @@ -1122,12 +1353,12 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } - private static ReplicatedLogEntry newReplicatedLogEntry(long term, long index, String data) { - return new MockRaftActorContext.MockReplicatedLogEntry(term, index, + private static ReplicatedLogEntry newReplicatedLogEntry(final long term, final long index, final String data) { + return new SimpleReplicatedLogEntry(index, term, new MockRaftActorContext.MockPayload(data)); } - private ByteString createSnapshot(){ + private ByteString createSnapshot() { HashMap followerSnapshot = new HashMap<>(); followerSnapshot.put("1", "A"); followerSnapshot.put("2", "B"); @@ -1137,8 +1368,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Override - protected void assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(MockRaftActorContext actorContext, - ActorRef actorRef, RaftRPC rpc) throws Exception { + protected void assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(final MockRaftActorContext actorContext, + final ActorRef actorRef, final RaftRPC rpc) { super.assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(actorContext, actorRef, rpc); String expVotedFor = rpc instanceof RequestVote ? ((RequestVote)rpc).getCandidateId() : null; @@ -1146,8 +1377,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } @Override - protected void handleAppendEntriesAddSameEntryToLogReply(final TestActorRef replyActor) - throws Exception { + protected void handleAppendEntriesAddSameEntryToLogReply(final ActorRef replyActor) { AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(replyActor, AppendEntriesReply.class); assertEquals("isSuccess", true, reply.isSuccess()); }