2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.raft.behaviors;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertSame;
16 import static org.junit.Assert.assertTrue;
17 import static org.mockito.Matchers.any;
18 import static org.mockito.Mockito.never;
19 import static org.mockito.Mockito.spy;
20 import static org.mockito.Mockito.verify;
22 import akka.actor.ActorRef;
23 import akka.actor.Props;
24 import akka.dispatch.Dispatchers;
25 import akka.testkit.JavaTestKit;
26 import akka.testkit.TestActorRef;
27 import com.google.common.base.Optional;
28 import com.google.common.base.Stopwatch;
29 import com.google.common.base.Throwables;
30 import com.google.common.collect.ImmutableList;
31 import com.google.common.collect.ImmutableMap;
32 import com.google.common.io.ByteSource;
33 import com.google.common.util.concurrent.Uninterruptibles;
34 import com.google.protobuf.ByteString;
35 import java.io.OutputStream;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.concurrent.TimeUnit;
42 import java.util.concurrent.atomic.AtomicReference;
43 import org.junit.After;
44 import org.junit.Assert;
45 import org.junit.Test;
46 import org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl;
47 import org.opendaylight.controller.cluster.raft.MockRaftActor;
48 import org.opendaylight.controller.cluster.raft.MockRaftActor.Builder;
49 import org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState;
50 import org.opendaylight.controller.cluster.raft.MockRaftActorContext;
51 import org.opendaylight.controller.cluster.raft.RaftActorContext;
52 import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort;
53 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
54 import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot;
55 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
56 import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
57 import org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus;
58 import org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow;
59 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
60 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
61 import org.opendaylight.controller.cluster.raft.messages.InstallSnapshot;
62 import org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply;
63 import org.opendaylight.controller.cluster.raft.messages.RaftRPC;
64 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
65 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
66 import org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries;
67 import org.opendaylight.controller.cluster.raft.persisted.ByteState;
68 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
69 import org.opendaylight.controller.cluster.raft.persisted.ServerInfo;
70 import org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry;
71 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
72 import org.opendaylight.controller.cluster.raft.persisted.Snapshot.State;
73 import org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm;
74 import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy;
75 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
76 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
77 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
78 import scala.concurrent.duration.FiniteDuration;
80 public class FollowerTest extends AbstractRaftActorBehaviorTest<Follower> {
82 private final TestActorRef<MessageCollectorActor> followerActor = actorFactory.createTestActor(
83 Props.create(MessageCollectorActor.class), actorFactory.generateActorId("follower"));
85 private final TestActorRef<MessageCollectorActor> leaderActor = actorFactory.createTestActor(
86 Props.create(MessageCollectorActor.class), actorFactory.generateActorId("leader"));
88 private Follower follower;
90 private final short payloadVersion = 5;
94 public void tearDown() throws Exception {
95 if (follower != null) {
103 protected Follower createBehavior(RaftActorContext actorContext) {
104 return spy(new Follower(actorContext));
108 protected MockRaftActorContext createActorContext() {
109 return createActorContext(followerActor);
113 protected MockRaftActorContext createActorContext(ActorRef actorRef) {
114 MockRaftActorContext context = new MockRaftActorContext("follower", getSystem(), actorRef);
115 context.setPayloadVersion(payloadVersion);
120 public void testThatAnElectionTimeoutIsTriggered() {
121 MockRaftActorContext actorContext = createActorContext();
122 follower = new Follower(actorContext);
124 MessageCollectorActor.expectFirstMatching(followerActor, TimeoutNow.class,
125 actorContext.getConfigParams().getElectionTimeOutInterval().$times(6).toMillis());
129 public void testHandleElectionTimeoutWhenNoLeaderMessageReceived() {
130 logStart("testHandleElectionTimeoutWhenNoLeaderMessageReceived");
132 MockRaftActorContext context = createActorContext();
133 follower = new Follower(context);
135 Uninterruptibles.sleepUninterruptibly(context.getConfigParams().getElectionTimeOutInterval().toMillis(),
136 TimeUnit.MILLISECONDS);
137 RaftActorBehavior raftBehavior = follower.handleMessage(leaderActor, ElectionTimeout.INSTANCE);
139 assertTrue(raftBehavior instanceof Candidate);
143 public void testHandleElectionTimeoutWhenLeaderMessageReceived() {
144 logStart("testHandleElectionTimeoutWhenLeaderMessageReceived");
146 MockRaftActorContext context = createActorContext();
147 ((DefaultConfigParamsImpl) context.getConfigParams())
148 .setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS));
149 ((DefaultConfigParamsImpl) context.getConfigParams()).setElectionTimeoutFactor(4);
151 follower = new Follower(context);
152 context.setCurrentBehavior(follower);
154 Uninterruptibles.sleepUninterruptibly(context.getConfigParams()
155 .getElectionTimeOutInterval().toMillis() - 100, TimeUnit.MILLISECONDS);
156 follower.handleMessage(leaderActor, new AppendEntries(1, "leader", -1, -1, Collections.emptyList(),
159 Uninterruptibles.sleepUninterruptibly(130, TimeUnit.MILLISECONDS);
160 RaftActorBehavior raftBehavior = follower.handleMessage(leaderActor, ElectionTimeout.INSTANCE);
161 assertTrue(raftBehavior instanceof Follower);
163 Uninterruptibles.sleepUninterruptibly(context.getConfigParams()
164 .getElectionTimeOutInterval().toMillis() - 150, TimeUnit.MILLISECONDS);
165 follower.handleMessage(leaderActor, new AppendEntries(1, "leader", -1, -1, Collections.emptyList(),
168 Uninterruptibles.sleepUninterruptibly(200, TimeUnit.MILLISECONDS);
169 raftBehavior = follower.handleMessage(leaderActor, ElectionTimeout.INSTANCE);
170 assertTrue(raftBehavior instanceof Follower);
174 public void testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForIsNull() {
175 logStart("testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForIsNull");
177 MockRaftActorContext context = createActorContext();
179 context.getTermInformation().update(term, null);
181 follower = createBehavior(context);
183 follower.handleMessage(leaderActor, new RequestVote(term, "test", 10000, 999));
185 RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, RequestVoteReply.class);
187 assertEquals("isVoteGranted", true, reply.isVoteGranted());
188 assertEquals("getTerm", term, reply.getTerm());
189 verify(follower).scheduleElection(any(FiniteDuration.class));
193 public void testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForIsNotTheSameAsCandidateId() {
194 logStart("testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForIsNotTheSameAsCandidateId");
196 MockRaftActorContext context = createActorContext();
198 context.getTermInformation().update(term, "test");
200 follower = createBehavior(context);
202 follower.handleMessage(leaderActor, new RequestVote(term, "candidate", 10000, 999));
204 RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, RequestVoteReply.class);
206 assertEquals("isVoteGranted", false, reply.isVoteGranted());
207 verify(follower, never()).scheduleElection(any(FiniteDuration.class));
212 public void testHandleFirstAppendEntries() throws Exception {
213 logStart("testHandleFirstAppendEntries");
215 MockRaftActorContext context = createActorContext();
216 context.getReplicatedLog().clear(0,2);
217 context.getReplicatedLog().append(newReplicatedLogEntry(1,100, "bar"));
218 context.getReplicatedLog().setSnapshotIndex(99);
220 List<ReplicatedLogEntry> entries = Arrays.asList(
221 newReplicatedLogEntry(2, 101, "foo"));
223 Assert.assertEquals(1, context.getReplicatedLog().size());
225 // The new commitIndex is 101
226 AppendEntries appendEntries = new AppendEntries(2, "leader-1", 100, 1, entries, 101, 100, (short)0);
228 follower = createBehavior(context);
229 follower.handleMessage(leaderActor, appendEntries);
231 FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor,
232 FollowerInitialSyncUpStatus.class);
233 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
235 assertFalse(syncStatus.isInitialSyncDone());
236 assertTrue("append entries reply should be true", reply.isSuccess());
240 public void testHandleFirstAppendEntriesWithPrevIndexMinusOne() throws Exception {
241 logStart("testHandleFirstAppendEntries");
243 MockRaftActorContext context = createActorContext();
245 List<ReplicatedLogEntry> entries = Arrays.asList(
246 newReplicatedLogEntry(2, 101, "foo"));
248 // The new commitIndex is 101
249 AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 101, 100, (short) 0);
251 follower = createBehavior(context);
252 follower.handleMessage(leaderActor, appendEntries);
254 FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor,
255 FollowerInitialSyncUpStatus.class);
256 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
258 assertFalse(syncStatus.isInitialSyncDone());
259 assertFalse("append entries reply should be false", reply.isSuccess());
263 public void testHandleFirstAppendEntriesWithPrevIndexMinusOneAndReplicatedToAllIndexPresentInLog()
265 logStart("testHandleFirstAppendEntries");
267 MockRaftActorContext context = createActorContext();
268 context.getReplicatedLog().clear(0,2);
269 context.getReplicatedLog().append(newReplicatedLogEntry(1, 100, "bar"));
270 context.getReplicatedLog().setSnapshotIndex(99);
272 List<ReplicatedLogEntry> entries = Arrays.asList(
273 newReplicatedLogEntry(2, 101, "foo"));
275 // The new commitIndex is 101
276 AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 101, 100, (short) 0);
278 follower = createBehavior(context);
279 follower.handleMessage(leaderActor, appendEntries);
281 FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor,
282 FollowerInitialSyncUpStatus.class);
283 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
285 assertFalse(syncStatus.isInitialSyncDone());
286 assertTrue("append entries reply should be true", reply.isSuccess());
290 public void testHandleFirstAppendEntriesWithPrevIndexMinusOneAndReplicatedToAllIndexPresentInSnapshot()
292 logStart("testHandleFirstAppendEntries");
294 MockRaftActorContext context = createActorContext();
295 context.getReplicatedLog().clear(0,2);
296 context.getReplicatedLog().setSnapshotIndex(100);
298 List<ReplicatedLogEntry> entries = Arrays.asList(
299 newReplicatedLogEntry(2, 101, "foo"));
301 // The new commitIndex is 101
302 AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 101, 100, (short) 0);
304 follower = createBehavior(context);
305 follower.handleMessage(leaderActor, appendEntries);
307 FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor,
308 FollowerInitialSyncUpStatus.class);
309 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
311 assertFalse(syncStatus.isInitialSyncDone());
312 assertTrue("append entries reply should be true", reply.isSuccess());
316 public void testFirstAppendEntriesWithNoPrevIndexAndReplicatedToAllPresentInSnapshotButCalculatedPrevEntryMissing()
319 "testFirstAppendEntriesWithNoPrevIndexAndReplicatedToAllPresentInSnapshotButCalculatedPrevEntryMissing");
321 MockRaftActorContext context = createActorContext();
322 context.getReplicatedLog().clear(0,2);
323 context.getReplicatedLog().setSnapshotIndex(100);
325 List<ReplicatedLogEntry> entries = Arrays.asList(
326 newReplicatedLogEntry(2, 105, "foo"));
328 // The new commitIndex is 101
329 AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 105, 100, (short) 0);
331 follower = createBehavior(context);
332 follower.handleMessage(leaderActor, appendEntries);
334 FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor,
335 FollowerInitialSyncUpStatus.class);
336 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
338 assertFalse(syncStatus.isInitialSyncDone());
339 assertFalse("append entries reply should be false", reply.isSuccess());
343 public void testHandleSyncUpAppendEntries() throws Exception {
344 logStart("testHandleSyncUpAppendEntries");
346 MockRaftActorContext context = createActorContext();
348 List<ReplicatedLogEntry> entries = Arrays.asList(
349 newReplicatedLogEntry(2, 101, "foo"));
351 // The new commitIndex is 101
352 AppendEntries appendEntries = new AppendEntries(2, "leader-1", 100, 1, entries, 101, 100, (short)0);
354 follower = createBehavior(context);
355 follower.handleMessage(leaderActor, appendEntries);
357 FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor,
358 FollowerInitialSyncUpStatus.class);
360 assertFalse(syncStatus.isInitialSyncDone());
362 // Clear all the messages
363 followerActor.underlyingActor().clear();
365 context.setLastApplied(101);
366 context.setCommitIndex(101);
367 setLastLogEntry(context, 1, 101,
368 new MockRaftActorContext.MockPayload(""));
370 entries = Arrays.asList(
371 newReplicatedLogEntry(2, 101, "foo"));
373 // The new commitIndex is 101
374 appendEntries = new AppendEntries(2, "leader-1", 101, 1, entries, 102, 101, (short)0);
375 follower.handleMessage(leaderActor, appendEntries);
377 syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
379 assertTrue(syncStatus.isInitialSyncDone());
381 followerActor.underlyingActor().clear();
383 // Sending the same message again should not generate another message
385 follower.handleMessage(leaderActor, appendEntries);
387 syncStatus = MessageCollectorActor.getFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
389 assertNull(syncStatus);
394 public void testHandleAppendEntriesLeaderChangedBeforeSyncUpComplete() throws Exception {
395 logStart("testHandleAppendEntriesLeaderChangedBeforeSyncUpComplete");
397 MockRaftActorContext context = createActorContext();
399 List<ReplicatedLogEntry> entries = Arrays.asList(
400 newReplicatedLogEntry(2, 101, "foo"));
402 // The new commitIndex is 101
403 AppendEntries appendEntries = new AppendEntries(2, "leader-1", 100, 1, entries, 101, 100, (short)0);
405 follower = createBehavior(context);
406 follower.handleMessage(leaderActor, appendEntries);
408 FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor,
409 FollowerInitialSyncUpStatus.class);
411 assertFalse(syncStatus.isInitialSyncDone());
413 // Clear all the messages
414 followerActor.underlyingActor().clear();
416 context.setLastApplied(100);
417 setLastLogEntry(context, 1, 100,
418 new MockRaftActorContext.MockPayload(""));
420 entries = Arrays.asList(
421 newReplicatedLogEntry(2, 101, "foo"));
423 // leader-2 is becoming the leader now and it says the commitIndex is 45
424 appendEntries = new AppendEntries(2, "leader-2", 45, 1, entries, 46, 100, (short)0);
425 follower.handleMessage(leaderActor, appendEntries);
427 syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
429 // We get a new message saying initial status is not done
430 assertFalse(syncStatus.isInitialSyncDone());
436 public void testHandleAppendEntriesLeaderChangedAfterSyncUpComplete() throws Exception {
437 logStart("testHandleAppendEntriesLeaderChangedAfterSyncUpComplete");
439 MockRaftActorContext context = createActorContext();
441 List<ReplicatedLogEntry> entries = Arrays.asList(
442 newReplicatedLogEntry(2, 101, "foo"));
444 // The new commitIndex is 101
445 AppendEntries appendEntries = new AppendEntries(2, "leader-1", 100, 1, entries, 101, 100, (short)0);
447 follower = createBehavior(context);
448 follower.handleMessage(leaderActor, appendEntries);
450 FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor,
451 FollowerInitialSyncUpStatus.class);
453 assertFalse(syncStatus.isInitialSyncDone());
455 // Clear all the messages
456 followerActor.underlyingActor().clear();
458 context.setLastApplied(101);
459 context.setCommitIndex(101);
460 setLastLogEntry(context, 1, 101,
461 new MockRaftActorContext.MockPayload(""));
463 entries = Arrays.asList(
464 newReplicatedLogEntry(2, 101, "foo"));
466 // The new commitIndex is 101
467 appendEntries = new AppendEntries(2, "leader-1", 101, 1, entries, 102, 101, (short)0);
468 follower.handleMessage(leaderActor, appendEntries);
470 syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
472 assertTrue(syncStatus.isInitialSyncDone());
474 // Clear all the messages
475 followerActor.underlyingActor().clear();
477 context.setLastApplied(100);
478 setLastLogEntry(context, 1, 100,
479 new MockRaftActorContext.MockPayload(""));
481 entries = Arrays.asList(
482 newReplicatedLogEntry(2, 101, "foo"));
484 // leader-2 is becoming the leader now and it says the commitIndex is 45
485 appendEntries = new AppendEntries(2, "leader-2", 45, 1, entries, 46, 100, (short)0);
486 follower.handleMessage(leaderActor, appendEntries);
488 syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
490 // We get a new message saying initial status is not done
491 assertFalse(syncStatus.isInitialSyncDone());
497 * This test verifies that when an AppendEntries RPC is received by a RaftActor
498 * with a commitIndex that is greater than what has been applied to the
499 * state machine of the RaftActor, the RaftActor applies the state and
500 * sets it current applied state to the commitIndex of the sender.
503 public void testHandleAppendEntriesWithNewerCommitIndex() throws Exception {
504 logStart("testHandleAppendEntriesWithNewerCommitIndex");
506 MockRaftActorContext context = createActorContext();
508 context.setLastApplied(100);
509 setLastLogEntry(context, 1, 100,
510 new MockRaftActorContext.MockPayload(""));
511 context.getReplicatedLog().setSnapshotIndex(99);
513 List<ReplicatedLogEntry> entries = Arrays.<ReplicatedLogEntry>asList(
514 newReplicatedLogEntry(2, 101, "foo"));
516 // The new commitIndex is 101
517 AppendEntries appendEntries = new AppendEntries(2, "leader-1", 100, 1, entries, 101, 100, (short)0);
519 follower = createBehavior(context);
520 follower.handleMessage(leaderActor, appendEntries);
522 assertEquals("getLastApplied", 101L, context.getLastApplied());
526 * This test verifies that when an AppendEntries is received a specific prevLogTerm
527 * which does not match the term that is in RaftActors log entry at prevLogIndex
528 * then the RaftActor does not change it's state and it returns a failure.
531 public void testHandleAppendEntriesSenderPrevLogTermNotSameAsReceiverPrevLogTerm() {
532 logStart("testHandleAppendEntriesSenderPrevLogTermNotSameAsReceiverPrevLogTerm");
534 MockRaftActorContext context = createActorContext();
536 // First set the receivers term to lower number
537 context.getTermInformation().update(95, "test");
539 // AppendEntries is now sent with a bigger term
540 // this will set the receivers term to be the same as the sender's term
541 AppendEntries appendEntries = new AppendEntries(100, "leader", 0, 0, null, 101, -1, (short)0);
543 follower = createBehavior(context);
545 RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries);
547 Assert.assertSame(follower, newBehavior);
549 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor,
550 AppendEntriesReply.class);
552 assertEquals("isSuccess", false, reply.isSuccess());
556 * This test verifies that when a new AppendEntries message is received with
557 * new entries and the logs of the sender and receiver match that the new
558 * entries get added to the log and the log is incremented by the number of
559 * entries received in appendEntries.
562 public void testHandleAppendEntriesAddNewEntries() {
563 logStart("testHandleAppendEntriesAddNewEntries");
565 MockRaftActorContext context = createActorContext();
567 // First set the receivers term to lower number
568 context.getTermInformation().update(1, "test");
570 // Prepare the receivers log
571 MockRaftActorContext.SimpleReplicatedLog log = new MockRaftActorContext.SimpleReplicatedLog();
572 log.append(newReplicatedLogEntry(1, 0, "zero"));
573 log.append(newReplicatedLogEntry(1, 1, "one"));
574 log.append(newReplicatedLogEntry(1, 2, "two"));
576 context.setReplicatedLog(log);
578 // Prepare the entries to be sent with AppendEntries
579 List<ReplicatedLogEntry> entries = new ArrayList<>();
580 entries.add(newReplicatedLogEntry(1, 3, "three"));
581 entries.add(newReplicatedLogEntry(1, 4, "four"));
583 // Send appendEntries with the same term as was set on the receiver
584 // before the new behavior was created (1 in this case)
585 // This will not work for a Candidate because as soon as a Candidate
586 // is created it increments the term
587 short leaderPayloadVersion = 10;
588 String leaderId = "leader-1";
589 AppendEntries appendEntries = new AppendEntries(1, leaderId, 2, 1, entries, 4, -1, leaderPayloadVersion);
591 follower = createBehavior(context);
593 RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries);
595 Assert.assertSame(follower, newBehavior);
597 assertEquals("Next index", 5, log.last().getIndex() + 1);
598 assertEquals("Entry 3", entries.get(0), log.get(3));
599 assertEquals("Entry 4", entries.get(1), log.get(4));
601 assertEquals("getLeaderPayloadVersion", leaderPayloadVersion, newBehavior.getLeaderPayloadVersion());
602 assertEquals("getLeaderId", leaderId, newBehavior.getLeaderId());
604 expectAndVerifyAppendEntriesReply(1, true, context.getId(), 1, 4);
608 * This test verifies that when a new AppendEntries message is received with
609 * new entries and the logs of the sender and receiver are out-of-sync that
610 * the log is first corrected by removing the out of sync entries from the
611 * log and then adding in the new entries sent with the AppendEntries message.
614 public void testHandleAppendEntriesCorrectReceiverLogEntries() {
615 logStart("testHandleAppendEntriesCorrectReceiverLogEntries");
617 MockRaftActorContext context = createActorContext();
619 // First set the receivers term to lower number
620 context.getTermInformation().update(1, "test");
622 // Prepare the receivers log
623 MockRaftActorContext.SimpleReplicatedLog log = new MockRaftActorContext.SimpleReplicatedLog();
624 log.append(newReplicatedLogEntry(1, 0, "zero"));
625 log.append(newReplicatedLogEntry(1, 1, "one"));
626 log.append(newReplicatedLogEntry(1, 2, "two"));
628 context.setReplicatedLog(log);
630 // Prepare the entries to be sent with AppendEntries
631 List<ReplicatedLogEntry> entries = new ArrayList<>();
632 entries.add(newReplicatedLogEntry(2, 2, "two-1"));
633 entries.add(newReplicatedLogEntry(2, 3, "three"));
635 // Send appendEntries with the same term as was set on the receiver
636 // before the new behavior was created (1 in this case)
637 // This will not work for a Candidate because as soon as a Candidate
638 // is created it increments the term
639 AppendEntries appendEntries = new AppendEntries(2, "leader", 1, 1, entries, 3, -1, (short)0);
641 follower = createBehavior(context);
643 RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries);
645 Assert.assertSame(follower, newBehavior);
647 // The entry at index 2 will be found out-of-sync with the leader
648 // and will be removed
649 // Then the two new entries will be added to the log
650 // Thus making the log to have 4 entries
651 assertEquals("Next index", 4, log.last().getIndex() + 1);
652 //assertEquals("Entry 2", entries.get(0), log.get(2));
654 assertEquals("Entry 1 data", "one", log.get(1).getData().toString());
656 // Check that the entry at index 2 has the new data
657 assertEquals("Entry 2", entries.get(0), log.get(2));
659 assertEquals("Entry 3", entries.get(1), log.get(3));
661 expectAndVerifyAppendEntriesReply(2, true, context.getId(), 2, 3);
665 public void testHandleAppendEntriesWhenOutOfSyncLogDetectedRequestForceInstallSnapshot() {
666 logStart("testHandleAppendEntriesWhenOutOfSyncLogDetectedRequestForceInstallSnapshot");
668 MockRaftActorContext context = createActorContext();
670 // First set the receivers term to lower number
671 context.getTermInformation().update(1, "test");
673 // Prepare the receivers log
674 MockRaftActorContext.SimpleReplicatedLog log = new MockRaftActorContext.SimpleReplicatedLog();
675 log.append(newReplicatedLogEntry(1, 0, "zero"));
676 log.append(newReplicatedLogEntry(1, 1, "one"));
677 log.append(newReplicatedLogEntry(1, 2, "two"));
679 context.setReplicatedLog(log);
681 // Prepare the entries to be sent with AppendEntries
682 List<ReplicatedLogEntry> entries = new ArrayList<>();
683 entries.add(newReplicatedLogEntry(2, 2, "two-1"));
684 entries.add(newReplicatedLogEntry(2, 3, "three"));
686 // Send appendEntries with the same term as was set on the receiver
687 // before the new behavior was created (1 in this case)
688 // This will not work for a Candidate because as soon as a Candidate
689 // is created it increments the term
690 AppendEntries appendEntries = new AppendEntries(2, "leader", 1, 1, entries, 3, -1, (short)0);
692 context.setRaftPolicy(createRaftPolicy(false, true));
693 follower = createBehavior(context);
695 RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries);
697 Assert.assertSame(follower, newBehavior);
699 expectAndVerifyAppendEntriesReply(2, false, context.getId(), 1, 2, true);
703 public void testHandleAppendEntriesPreviousLogEntryMissing() {
704 logStart("testHandleAppendEntriesPreviousLogEntryMissing");
706 final MockRaftActorContext context = createActorContext();
708 // Prepare the receivers log
709 MockRaftActorContext.SimpleReplicatedLog log = new MockRaftActorContext.SimpleReplicatedLog();
710 log.append(newReplicatedLogEntry(1, 0, "zero"));
711 log.append(newReplicatedLogEntry(1, 1, "one"));
712 log.append(newReplicatedLogEntry(1, 2, "two"));
714 context.setReplicatedLog(log);
716 // Prepare the entries to be sent with AppendEntries
717 List<ReplicatedLogEntry> entries = new ArrayList<>();
718 entries.add(newReplicatedLogEntry(1, 4, "four"));
720 AppendEntries appendEntries = new AppendEntries(1, "leader", 3, 1, entries, 4, -1, (short)0);
722 follower = createBehavior(context);
724 RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries);
726 Assert.assertSame(follower, newBehavior);
728 expectAndVerifyAppendEntriesReply(1, false, context.getId(), 1, 2);
732 public void testHandleAppendEntriesWithExistingLogEntry() {
733 logStart("testHandleAppendEntriesWithExistingLogEntry");
735 MockRaftActorContext context = createActorContext();
737 context.getTermInformation().update(1, "test");
739 // Prepare the receivers log
740 MockRaftActorContext.SimpleReplicatedLog log = new MockRaftActorContext.SimpleReplicatedLog();
741 log.append(newReplicatedLogEntry(1, 0, "zero"));
742 log.append(newReplicatedLogEntry(1, 1, "one"));
744 context.setReplicatedLog(log);
746 // Send the last entry again.
747 List<ReplicatedLogEntry> entries = Arrays.asList(newReplicatedLogEntry(1, 1, "one"));
749 follower = createBehavior(context);
751 follower.handleMessage(leaderActor, new AppendEntries(1, "leader", 0, 1, entries, 1, -1, (short)0));
753 assertEquals("Next index", 2, log.last().getIndex() + 1);
754 assertEquals("Entry 1", entries.get(0), log.get(1));
756 expectAndVerifyAppendEntriesReply(1, true, context.getId(), 1, 1);
758 // Send the last entry again and also a new one.
760 entries = Arrays.asList(newReplicatedLogEntry(1, 1, "one"), newReplicatedLogEntry(1, 2, "two"));
762 leaderActor.underlyingActor().clear();
763 follower.handleMessage(leaderActor, new AppendEntries(1, "leader", 0, 1, entries, 2, -1, (short)0));
765 assertEquals("Next index", 3, log.last().getIndex() + 1);
766 assertEquals("Entry 1", entries.get(0), log.get(1));
767 assertEquals("Entry 2", entries.get(1), log.get(2));
769 expectAndVerifyAppendEntriesReply(1, true, context.getId(), 1, 2);
773 public void testHandleAppendEntriesAfterInstallingSnapshot() {
774 logStart("testHandleAppendAfterInstallingSnapshot");
776 MockRaftActorContext context = createActorContext();
778 // Prepare the receivers log
779 MockRaftActorContext.SimpleReplicatedLog log = new MockRaftActorContext.SimpleReplicatedLog();
781 // Set up a log as if it has been snapshotted
782 log.setSnapshotIndex(3);
783 log.setSnapshotTerm(1);
785 context.setReplicatedLog(log);
787 // Prepare the entries to be sent with AppendEntries
788 List<ReplicatedLogEntry> entries = new ArrayList<>();
789 entries.add(newReplicatedLogEntry(1, 4, "four"));
791 AppendEntries appendEntries = new AppendEntries(1, "leader", 3, 1, entries, 4, 3, (short)0);
793 follower = createBehavior(context);
795 RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries);
797 Assert.assertSame(follower, newBehavior);
799 expectAndVerifyAppendEntriesReply(1, true, context.getId(), 1, 4);
804 * This test verifies that when InstallSnapshot is received by
805 * the follower its applied correctly.
808 public void testHandleInstallSnapshot() throws Exception {
809 logStart("testHandleInstallSnapshot");
811 MockRaftActorContext context = createActorContext();
812 context.getTermInformation().update(1, "leader");
814 follower = createBehavior(context);
816 ByteString bsSnapshot = createSnapshot();
818 int snapshotLength = bsSnapshot.size();
820 int totalChunks = snapshotLength / chunkSize + (snapshotLength % chunkSize > 0 ? 1 : 0);
821 int lastIncludedIndex = 1;
823 InstallSnapshot lastInstallSnapshot = null;
825 for (int i = 0; i < totalChunks; i++) {
826 byte[] chunkData = getNextChunk(bsSnapshot, offset, chunkSize);
827 lastInstallSnapshot = new InstallSnapshot(1, "leader", lastIncludedIndex, 1,
828 chunkData, chunkIndex, totalChunks);
829 follower.handleMessage(leaderActor, lastInstallSnapshot);
830 offset = offset + 50;
835 ApplySnapshot applySnapshot = MessageCollectorActor.expectFirstMatching(followerActor,
836 ApplySnapshot.class);
837 Snapshot snapshot = applySnapshot.getSnapshot();
838 assertNotNull(lastInstallSnapshot);
839 assertEquals("getLastIndex", lastInstallSnapshot.getLastIncludedIndex(), snapshot.getLastIndex());
840 assertEquals("getLastIncludedTerm", lastInstallSnapshot.getLastIncludedTerm(),
841 snapshot.getLastAppliedTerm());
842 assertEquals("getLastAppliedIndex", lastInstallSnapshot.getLastIncludedIndex(),
843 snapshot.getLastAppliedIndex());
844 assertEquals("getLastTerm", lastInstallSnapshot.getLastIncludedTerm(), snapshot.getLastTerm());
845 assertEquals("getState type", ByteState.class, snapshot.getState().getClass());
846 Assert.assertArrayEquals("getState", bsSnapshot.toByteArray(), ((ByteState)snapshot.getState()).getBytes());
847 assertEquals("getElectionTerm", 1, snapshot.getElectionTerm());
848 assertEquals("getElectionVotedFor", "leader", snapshot.getElectionVotedFor());
849 applySnapshot.getCallback().onSuccess();
851 List<InstallSnapshotReply> replies = MessageCollectorActor.getAllMatching(
852 leaderActor, InstallSnapshotReply.class);
853 assertEquals("InstallSnapshotReply count", totalChunks, replies.size());
856 for (InstallSnapshotReply reply: replies) {
857 assertEquals("getChunkIndex", chunkIndex++, reply.getChunkIndex());
858 assertEquals("getTerm", 1, reply.getTerm());
859 assertEquals("isSuccess", true, reply.isSuccess());
860 assertEquals("getFollowerId", context.getId(), reply.getFollowerId());
863 assertNull("Expected null SnapshotTracker", follower.getSnapshotTracker());
868 * Verify that when an AppendEntries is sent to a follower during a snapshot install
869 * the Follower short-circuits the processing of the AppendEntries message.
872 public void testReceivingAppendEntriesDuringInstallSnapshot() throws Exception {
873 logStart("testReceivingAppendEntriesDuringInstallSnapshot");
875 MockRaftActorContext context = createActorContext();
877 follower = createBehavior(context);
879 ByteString bsSnapshot = createSnapshot();
880 int snapshotLength = bsSnapshot.size();
882 int totalChunks = snapshotLength / chunkSize + (snapshotLength % chunkSize > 0 ? 1 : 0);
883 int lastIncludedIndex = 1;
885 // Check that snapshot installation is not in progress
886 assertNull(follower.getSnapshotTracker());
888 // Make sure that we have more than 1 chunk to send
889 assertTrue(totalChunks > 1);
891 // Send an install snapshot with the first chunk to start the process of installing a snapshot
892 byte[] chunkData = getNextChunk(bsSnapshot, 0, chunkSize);
893 follower.handleMessage(leaderActor, new InstallSnapshot(1, "leader", lastIncludedIndex, 1,
894 chunkData, 1, totalChunks));
896 // Check if snapshot installation is in progress now
897 assertNotNull(follower.getSnapshotTracker());
899 // Send an append entry
900 AppendEntries appendEntries = new AppendEntries(1, "leader", 1, 1,
901 Arrays.asList(newReplicatedLogEntry(2, 1, "3")), 2, -1, (short)1);
903 follower.handleMessage(leaderActor, appendEntries);
905 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
906 assertEquals("isSuccess", true, reply.isSuccess());
907 assertEquals("getLogLastIndex", context.getReplicatedLog().lastIndex(), reply.getLogLastIndex());
908 assertEquals("getLogLastTerm", context.getReplicatedLog().lastTerm(), reply.getLogLastTerm());
909 assertEquals("getTerm", context.getTermInformation().getCurrentTerm(), reply.getTerm());
911 assertNotNull(follower.getSnapshotTracker());
915 public void testReceivingAppendEntriesDuringInstallSnapshotFromDifferentLeader() throws Exception {
916 logStart("testReceivingAppendEntriesDuringInstallSnapshotFromDifferentLeader");
918 MockRaftActorContext context = createActorContext();
920 follower = createBehavior(context);
922 ByteString bsSnapshot = createSnapshot();
923 int snapshotLength = bsSnapshot.size();
925 int totalChunks = snapshotLength / chunkSize + (snapshotLength % chunkSize > 0 ? 1 : 0);
926 int lastIncludedIndex = 1;
928 // Check that snapshot installation is not in progress
929 assertNull(follower.getSnapshotTracker());
931 // Make sure that we have more than 1 chunk to send
932 assertTrue(totalChunks > 1);
934 // Send an install snapshot with the first chunk to start the process of installing a snapshot
935 byte[] chunkData = getNextChunk(bsSnapshot, 0, chunkSize);
936 follower.handleMessage(leaderActor, new InstallSnapshot(1, "leader", lastIncludedIndex, 1,
937 chunkData, 1, totalChunks));
939 // Check if snapshot installation is in progress now
940 assertNotNull(follower.getSnapshotTracker());
942 // Send appendEntries with a new term and leader.
943 AppendEntries appendEntries = new AppendEntries(2, "new-leader", 1, 1,
944 Arrays.asList(newReplicatedLogEntry(2, 2, "3")), 2, -1, (short)1);
946 follower.handleMessage(leaderActor, appendEntries);
948 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
949 assertEquals("isSuccess", true, reply.isSuccess());
950 assertEquals("getLogLastIndex", 2, reply.getLogLastIndex());
951 assertEquals("getLogLastTerm", 2, reply.getLogLastTerm());
952 assertEquals("getTerm", 2, reply.getTerm());
954 assertNull(follower.getSnapshotTracker());
958 public void testInitialSyncUpWithHandleInstallSnapshotFollowedByAppendEntries() throws Exception {
959 logStart("testInitialSyncUpWithHandleInstallSnapshot");
961 MockRaftActorContext context = createActorContext();
962 context.setCommitIndex(-1);
964 follower = createBehavior(context);
966 ByteString bsSnapshot = createSnapshot();
968 int snapshotLength = bsSnapshot.size();
970 int totalChunks = snapshotLength / chunkSize + (snapshotLength % chunkSize > 0 ? 1 : 0);
971 int lastIncludedIndex = 1;
973 InstallSnapshot lastInstallSnapshot = null;
975 for (int i = 0; i < totalChunks; i++) {
976 byte[] chunkData = getNextChunk(bsSnapshot, offset, chunkSize);
977 lastInstallSnapshot = new InstallSnapshot(1, "leader", lastIncludedIndex, 1,
978 chunkData, chunkIndex, totalChunks);
979 follower.handleMessage(leaderActor, lastInstallSnapshot);
980 offset = offset + 50;
985 FollowerInitialSyncUpStatus syncStatus =
986 MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
988 assertFalse(syncStatus.isInitialSyncDone());
990 // Clear all the messages
991 followerActor.underlyingActor().clear();
993 context.setLastApplied(101);
994 context.setCommitIndex(101);
995 setLastLogEntry(context, 1, 101,
996 new MockRaftActorContext.MockPayload(""));
998 List<ReplicatedLogEntry> entries = Arrays.asList(
999 newReplicatedLogEntry(2, 101, "foo"));
1001 // The new commitIndex is 101
1002 AppendEntries appendEntries = new AppendEntries(2, "leader", 101, 1, entries, 102, 101, (short)0);
1003 follower.handleMessage(leaderActor, appendEntries);
1005 syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
1007 assertTrue(syncStatus.isInitialSyncDone());
1011 public void testHandleOutOfSequenceInstallSnapshot() throws Exception {
1012 logStart("testHandleOutOfSequenceInstallSnapshot");
1014 MockRaftActorContext context = createActorContext();
1016 follower = createBehavior(context);
1018 ByteString bsSnapshot = createSnapshot();
1020 InstallSnapshot installSnapshot = new InstallSnapshot(1, "leader", 3, 1,
1021 getNextChunk(bsSnapshot, 10, 50), 3, 3);
1022 follower.handleMessage(leaderActor, installSnapshot);
1024 InstallSnapshotReply reply = MessageCollectorActor.expectFirstMatching(leaderActor,
1025 InstallSnapshotReply.class);
1027 assertEquals("isSuccess", false, reply.isSuccess());
1028 assertEquals("getChunkIndex", -1, reply.getChunkIndex());
1029 assertEquals("getTerm", 1, reply.getTerm());
1030 assertEquals("getFollowerId", context.getId(), reply.getFollowerId());
1032 assertNull("Expected null SnapshotTracker", follower.getSnapshotTracker());
1036 public void testFollowerSchedulesElectionTimeoutImmediatelyWhenItHasNoPeers() {
1037 MockRaftActorContext context = createActorContext();
1039 Stopwatch stopwatch = Stopwatch.createStarted();
1041 follower = createBehavior(context);
1043 TimeoutNow timeoutNow = MessageCollectorActor.expectFirstMatching(followerActor, TimeoutNow.class);
1045 long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
1047 assertTrue(elapsed < context.getConfigParams().getElectionTimeOutInterval().toMillis());
1049 RaftActorBehavior newBehavior = follower.handleMessage(ActorRef.noSender(), timeoutNow);
1050 assertTrue("Expected Candidate", newBehavior instanceof Candidate);
1054 public void testFollowerSchedulesElectionIfAutomaticElectionsAreDisabled() {
1055 MockRaftActorContext context = createActorContext();
1056 context.setConfigParams(new DefaultConfigParamsImpl() {
1058 public FiniteDuration getElectionTimeOutInterval() {
1059 return FiniteDuration.apply(100, TimeUnit.MILLISECONDS);
1063 context.setRaftPolicy(createRaftPolicy(false, false));
1065 follower = createBehavior(context);
1067 TimeoutNow timeoutNow = MessageCollectorActor.expectFirstMatching(followerActor, TimeoutNow.class);
1068 RaftActorBehavior newBehavior = follower.handleMessage(ActorRef.noSender(), timeoutNow);
1069 assertSame("handleMessage result", follower, newBehavior);
1073 public void testFollowerSchedulesElectionIfNonVoting() {
1074 MockRaftActorContext context = createActorContext();
1075 context.updatePeerIds(new ServerConfigurationPayload(Arrays.asList(new ServerInfo(context.getId(), false))));
1076 ((DefaultConfigParamsImpl)context.getConfigParams()).setHeartBeatInterval(
1077 FiniteDuration.apply(100, TimeUnit.MILLISECONDS));
1078 ((DefaultConfigParamsImpl)context.getConfigParams()).setElectionTimeoutFactor(1);
1080 follower = new Follower(context, "leader", (short)1);
1082 ElectionTimeout electionTimeout = MessageCollectorActor.expectFirstMatching(followerActor,
1083 ElectionTimeout.class);
1084 RaftActorBehavior newBehavior = follower.handleMessage(ActorRef.noSender(), electionTimeout);
1085 assertSame("handleMessage result", follower, newBehavior);
1086 assertNull("Expected null leaderId", follower.getLeaderId());
1090 public void testElectionScheduledWhenAnyRaftRPCReceived() {
1091 MockRaftActorContext context = createActorContext();
1092 follower = createBehavior(context);
1093 follower.handleMessage(leaderActor, new RaftRPC() {
1094 private static final long serialVersionUID = 1L;
1097 public long getTerm() {
1101 verify(follower).scheduleElection(any(FiniteDuration.class));
1105 public void testElectionNotScheduledWhenNonRaftRPCMessageReceived() {
1106 MockRaftActorContext context = createActorContext();
1107 follower = createBehavior(context);
1108 follower.handleMessage(leaderActor, "non-raft-rpc");
1109 verify(follower, never()).scheduleElection(any(FiniteDuration.class));
1113 public void testCaptureSnapshotOnLastEntryInAppendEntries() throws Exception {
1114 String id = "testCaptureSnapshotOnLastEntryInAppendEntries";
1117 InMemoryJournal.addEntry(id, 1, new UpdateElectionTerm(1, null));
1119 DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
1120 config.setSnapshotBatchCount(2);
1121 config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
1123 final AtomicReference<MockRaftActor> followerRaftActor = new AtomicReference<>();
1124 RaftActorSnapshotCohort snapshotCohort = newRaftActorSnapshotCohort(followerRaftActor);
1125 Builder builder = MockRaftActor.builder().persistent(Optional.of(true)).id(id)
1126 .peerAddresses(ImmutableMap.of("leader", "")).config(config).snapshotCohort(snapshotCohort);
1127 TestActorRef<MockRaftActor> followerActorRef = actorFactory.createTestActor(builder.props()
1128 .withDispatcher(Dispatchers.DefaultDispatcherId()), id);
1129 followerRaftActor.set(followerActorRef.underlyingActor());
1130 followerRaftActor.get().waitForInitializeBehaviorComplete();
1132 InMemorySnapshotStore.addSnapshotSavedLatch(id);
1133 InMemoryJournal.addDeleteMessagesCompleteLatch(id);
1135 List<ReplicatedLogEntry> entries = Arrays.asList(
1136 newReplicatedLogEntry(1, 0, "one"), newReplicatedLogEntry(1, 1, "two"));
1138 AppendEntries appendEntries = new AppendEntries(1, "leader", -1, -1, entries, 1, -1, (short)0);
1140 followerActorRef.tell(appendEntries, leaderActor);
1142 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
1143 assertEquals("isSuccess", true, reply.isSuccess());
1145 final Snapshot snapshot = InMemorySnapshotStore.waitForSavedSnapshot(id, Snapshot.class);
1147 InMemoryJournal.waitForDeleteMessagesComplete(id);
1148 // We expect the ApplyJournalEntries for index 1 to remain in the persisted log b/c it's still queued for
1149 // persistence by the time we initiate capture so the last persisted journal sequence number doesn't include it.
1150 // This is OK - on recovery it will be a no-op since index 1 has already been applied.
1151 List<Object> journalEntries = InMemoryJournal.get(id, Object.class);
1152 assertEquals("Persisted journal entries size: " + journalEntries, 1, journalEntries.size());
1153 assertEquals("Persisted journal entry type", ApplyJournalEntries.class, journalEntries.get(0).getClass());
1154 assertEquals("ApplyJournalEntries index", 1, ((ApplyJournalEntries)journalEntries.get(0)).getToIndex());
1156 assertEquals("Snapshot unapplied size", 0, snapshot.getUnAppliedEntries().size());
1157 assertEquals("Snapshot getLastAppliedTerm", 1, snapshot.getLastAppliedTerm());
1158 assertEquals("Snapshot getLastAppliedIndex", 1, snapshot.getLastAppliedIndex());
1159 assertEquals("Snapshot getLastTerm", 1, snapshot.getLastTerm());
1160 assertEquals("Snapshot getLastIndex", 1, snapshot.getLastIndex());
1161 assertEquals("Snapshot state", ImmutableList.of(entries.get(0).getData(), entries.get(1).getData()),
1162 MockRaftActor.fromState(snapshot.getState()));
1166 public void testCaptureSnapshotOnMiddleEntryInAppendEntries() throws Exception {
1167 String id = "testCaptureSnapshotOnMiddleEntryInAppendEntries";
1170 InMemoryJournal.addEntry(id, 1, new UpdateElectionTerm(1, null));
1172 DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
1173 config.setSnapshotBatchCount(2);
1174 config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
1176 final AtomicReference<MockRaftActor> followerRaftActor = new AtomicReference<>();
1177 RaftActorSnapshotCohort snapshotCohort = newRaftActorSnapshotCohort(followerRaftActor);
1178 Builder builder = MockRaftActor.builder().persistent(Optional.of(true)).id(id)
1179 .peerAddresses(ImmutableMap.of("leader", "")).config(config).snapshotCohort(snapshotCohort);
1180 TestActorRef<MockRaftActor> followerActorRef = actorFactory.createTestActor(builder.props()
1181 .withDispatcher(Dispatchers.DefaultDispatcherId()), id);
1182 followerRaftActor.set(followerActorRef.underlyingActor());
1183 followerRaftActor.get().waitForInitializeBehaviorComplete();
1185 InMemorySnapshotStore.addSnapshotSavedLatch(id);
1186 InMemoryJournal.addDeleteMessagesCompleteLatch(id);
1188 List<ReplicatedLogEntry> entries = Arrays.asList(
1189 newReplicatedLogEntry(1, 0, "one"), newReplicatedLogEntry(1, 1, "two"),
1190 newReplicatedLogEntry(1, 2, "three"));
1192 AppendEntries appendEntries = new AppendEntries(1, "leader", -1, -1, entries, 2, -1, (short)0);
1194 followerActorRef.tell(appendEntries, leaderActor);
1196 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
1197 assertEquals("isSuccess", true, reply.isSuccess());
1199 final Snapshot snapshot = InMemorySnapshotStore.waitForSavedSnapshot(id, Snapshot.class);
1201 InMemoryJournal.waitForDeleteMessagesComplete(id);
1202 // We expect the ApplyJournalEntries for index 2 to remain in the persisted log b/c it's still queued for
1203 // persistence by the time we initiate capture so the last persisted journal sequence number doesn't include it.
1204 // This is OK - on recovery it will be a no-op since index 2 has already been applied.
1205 List<Object> journalEntries = InMemoryJournal.get(id, Object.class);
1206 assertEquals("Persisted journal entries size: " + journalEntries, 1, journalEntries.size());
1207 assertEquals("Persisted journal entry type", ApplyJournalEntries.class, journalEntries.get(0).getClass());
1208 assertEquals("ApplyJournalEntries index", 2, ((ApplyJournalEntries)journalEntries.get(0)).getToIndex());
1210 assertEquals("Snapshot unapplied size", 0, snapshot.getUnAppliedEntries().size());
1211 assertEquals("Snapshot getLastAppliedTerm", 1, snapshot.getLastAppliedTerm());
1212 assertEquals("Snapshot getLastAppliedIndex", 2, snapshot.getLastAppliedIndex());
1213 assertEquals("Snapshot getLastTerm", 1, snapshot.getLastTerm());
1214 assertEquals("Snapshot getLastIndex", 2, snapshot.getLastIndex());
1215 assertEquals("Snapshot state", ImmutableList.of(entries.get(0).getData(), entries.get(1).getData(),
1216 entries.get(2).getData()), MockRaftActor.fromState(snapshot.getState()));
1218 assertEquals("Journal size", 0, followerRaftActor.get().getReplicatedLog().size());
1219 assertEquals("Snapshot index", 2, followerRaftActor.get().getReplicatedLog().getSnapshotIndex());
1221 // Reinstate the actor from persistence
1223 actorFactory.killActor(followerActorRef, new JavaTestKit(getSystem()));
1225 followerActorRef = actorFactory.createTestActor(builder.props()
1226 .withDispatcher(Dispatchers.DefaultDispatcherId()), id);
1227 followerRaftActor.set(followerActorRef.underlyingActor());
1228 followerRaftActor.get().waitForInitializeBehaviorComplete();
1230 assertEquals("Journal size", 0, followerRaftActor.get().getReplicatedLog().size());
1231 assertEquals("Last index", 2, followerRaftActor.get().getReplicatedLog().lastIndex());
1232 assertEquals("Last applied index", 2, followerRaftActor.get().getRaftActorContext().getLastApplied());
1233 assertEquals("Commit index", 2, followerRaftActor.get().getRaftActorContext().getCommitIndex());
1234 assertEquals("State", ImmutableList.of(entries.get(0).getData(), entries.get(1).getData(),
1235 entries.get(2).getData()), followerRaftActor.get().getState());
1239 public void testCaptureSnapshotOnAppendEntriesWithUnapplied() throws Exception {
1240 String id = "testCaptureSnapshotOnAppendEntriesWithUnapplied";
1243 InMemoryJournal.addEntry(id, 1, new UpdateElectionTerm(1, null));
1245 DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
1246 config.setSnapshotBatchCount(1);
1247 config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
1249 final AtomicReference<MockRaftActor> followerRaftActor = new AtomicReference<>();
1250 RaftActorSnapshotCohort snapshotCohort = newRaftActorSnapshotCohort(followerRaftActor);
1251 Builder builder = MockRaftActor.builder().persistent(Optional.of(true)).id(id)
1252 .peerAddresses(ImmutableMap.of("leader", "")).config(config).snapshotCohort(snapshotCohort);
1253 TestActorRef<MockRaftActor> followerActorRef = actorFactory.createTestActor(builder.props()
1254 .withDispatcher(Dispatchers.DefaultDispatcherId()), id);
1255 followerRaftActor.set(followerActorRef.underlyingActor());
1256 followerRaftActor.get().waitForInitializeBehaviorComplete();
1258 InMemorySnapshotStore.addSnapshotSavedLatch(id);
1259 InMemoryJournal.addDeleteMessagesCompleteLatch(id);
1261 List<ReplicatedLogEntry> entries = Arrays.asList(
1262 newReplicatedLogEntry(1, 0, "one"), newReplicatedLogEntry(1, 1, "two"),
1263 newReplicatedLogEntry(1, 2, "three"));
1265 AppendEntries appendEntries = new AppendEntries(1, "leader", -1, -1, entries, 0, -1, (short)0);
1267 followerActorRef.tell(appendEntries, leaderActor);
1269 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
1270 assertEquals("isSuccess", true, reply.isSuccess());
1272 final Snapshot snapshot = InMemorySnapshotStore.waitForSavedSnapshot(id, Snapshot.class);
1274 InMemoryJournal.waitForDeleteMessagesComplete(id);
1275 // We expect the ApplyJournalEntries for index 0 to remain in the persisted log b/c it's still queued for
1276 // persistence by the time we initiate capture so the last persisted journal sequence number doesn't include it.
1277 // This is OK - on recovery it will be a no-op since index 0 has already been applied.
1278 List<Object> journalEntries = InMemoryJournal.get(id, Object.class);
1279 assertEquals("Persisted journal entries size: " + journalEntries, 1, journalEntries.size());
1280 assertEquals("Persisted journal entry type", ApplyJournalEntries.class, journalEntries.get(0).getClass());
1281 assertEquals("ApplyJournalEntries index", 0, ((ApplyJournalEntries)journalEntries.get(0)).getToIndex());
1283 assertEquals("Snapshot unapplied size", 2, snapshot.getUnAppliedEntries().size());
1284 assertEquals("Snapshot unapplied entry index", 1, snapshot.getUnAppliedEntries().get(0).getIndex());
1285 assertEquals("Snapshot unapplied entry index", 2, snapshot.getUnAppliedEntries().get(1).getIndex());
1286 assertEquals("Snapshot getLastAppliedTerm", 1, snapshot.getLastAppliedTerm());
1287 assertEquals("Snapshot getLastAppliedIndex", 0, snapshot.getLastAppliedIndex());
1288 assertEquals("Snapshot getLastTerm", 1, snapshot.getLastTerm());
1289 assertEquals("Snapshot getLastIndex", 2, snapshot.getLastIndex());
1290 assertEquals("Snapshot state", ImmutableList.of(entries.get(0).getData()),
1291 MockRaftActor.fromState(snapshot.getState()));
1294 @SuppressWarnings("checkstyle:IllegalCatch")
1295 private RaftActorSnapshotCohort newRaftActorSnapshotCohort(final AtomicReference<MockRaftActor> followerRaftActor) {
1296 RaftActorSnapshotCohort snapshotCohort = new RaftActorSnapshotCohort() {
1298 public void createSnapshot(ActorRef actorRef, java.util.Optional<OutputStream> installSnapshotStream) {
1300 actorRef.tell(new CaptureSnapshotReply(new MockSnapshotState(followerRaftActor.get().getState()),
1301 installSnapshotStream), actorRef);
1302 } catch (Exception e) {
1303 Throwables.propagate(e);
1308 public void applySnapshot(State snapshotState) {
1312 public State deserializeSnapshot(ByteSource snapshotBytes) {
1313 throw new UnsupportedOperationException();
1316 return snapshotCohort;
1319 public byte[] getNextChunk(ByteString bs, int offset, int chunkSize) {
1320 int snapshotLength = bs.size();
1322 int size = chunkSize;
1323 if (chunkSize > snapshotLength) {
1324 size = snapshotLength;
1326 if (start + chunkSize > snapshotLength) {
1327 size = snapshotLength - start;
1331 byte[] nextChunk = new byte[size];
1332 bs.copyTo(nextChunk, start, 0, size);
1336 private void expectAndVerifyAppendEntriesReply(int expTerm, boolean expSuccess,
1337 String expFollowerId, long expLogLastTerm, long expLogLastIndex) {
1338 expectAndVerifyAppendEntriesReply(expTerm, expSuccess, expFollowerId, expLogLastTerm, expLogLastIndex, false);
1341 private void expectAndVerifyAppendEntriesReply(int expTerm, boolean expSuccess,
1342 String expFollowerId, long expLogLastTerm, long expLogLastIndex,
1343 boolean expForceInstallSnapshot) {
1345 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor,
1346 AppendEntriesReply.class);
1348 assertEquals("isSuccess", expSuccess, reply.isSuccess());
1349 assertEquals("getTerm", expTerm, reply.getTerm());
1350 assertEquals("getFollowerId", expFollowerId, reply.getFollowerId());
1351 assertEquals("getLogLastTerm", expLogLastTerm, reply.getLogLastTerm());
1352 assertEquals("getLogLastIndex", expLogLastIndex, reply.getLogLastIndex());
1353 assertEquals("getPayloadVersion", payloadVersion, reply.getPayloadVersion());
1354 assertEquals("isForceInstallSnapshot", expForceInstallSnapshot, reply.isForceInstallSnapshot());
1358 private static ReplicatedLogEntry newReplicatedLogEntry(long term, long index, String data) {
1359 return new SimpleReplicatedLogEntry(index, term,
1360 new MockRaftActorContext.MockPayload(data));
1363 private ByteString createSnapshot() {
1364 HashMap<String, String> followerSnapshot = new HashMap<>();
1365 followerSnapshot.put("1", "A");
1366 followerSnapshot.put("2", "B");
1367 followerSnapshot.put("3", "C");
1369 return toByteString(followerSnapshot);
1373 protected void assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(MockRaftActorContext actorContext,
1374 ActorRef actorRef, RaftRPC rpc) throws Exception {
1375 super.assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(actorContext, actorRef, rpc);
1377 String expVotedFor = rpc instanceof RequestVote ? ((RequestVote)rpc).getCandidateId() : null;
1378 assertEquals("New votedFor", expVotedFor, actorContext.getTermInformation().getVotedFor());
1382 protected void handleAppendEntriesAddSameEntryToLogReply(final TestActorRef<MessageCollectorActor> replyActor)
1384 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(replyActor, AppendEntriesReply.class);
1385 assertEquals("isSuccess", true, reply.isSuccess());