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.assertTrue;
14 import akka.actor.ActorRef;
15 import akka.actor.Props;
16 import akka.testkit.TestActorRef;
17 import com.google.common.base.Stopwatch;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.concurrent.TimeUnit;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.opendaylight.controller.cluster.NonPersistentDataProvider;
29 import org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl;
30 import org.opendaylight.controller.cluster.raft.ElectionTerm;
31 import org.opendaylight.controller.cluster.raft.MockRaftActorContext;
32 import org.opendaylight.controller.cluster.raft.RaftActorContext;
33 import org.opendaylight.controller.cluster.raft.RaftActorContextImpl;
34 import org.opendaylight.controller.cluster.raft.RaftState;
35 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
36 import org.opendaylight.controller.cluster.raft.VotingState;
37 import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
38 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
39 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
40 import org.opendaylight.controller.cluster.raft.messages.RaftRPC;
41 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
42 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
43 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class CandidateTest extends AbstractRaftActorBehaviorTest<Candidate> {
48 static final Logger LOG = LoggerFactory.getLogger(CandidateTest.class);
50 private final TestActorRef<MessageCollectorActor> candidateActor = actorFactory.createTestActor(
51 Props.create(MessageCollectorActor.class), actorFactory.generateActorId("candidate"));
53 private TestActorRef<MessageCollectorActor>[] peerActors;
55 private RaftActorBehavior candidate;
63 public void tearDown() throws Exception {
64 if (candidate != null) {
72 public void testWhenACandidateIsCreatedItIncrementsTheCurrentTermAndVotesForItself() {
73 RaftActorContext raftActorContext = createActorContext();
74 long expectedTerm = raftActorContext.getTermInformation().getCurrentTerm();
76 candidate = new Candidate(raftActorContext);
78 assertEquals("getCurrentTerm", expectedTerm + 1, raftActorContext.getTermInformation().getCurrentTerm());
79 assertEquals("getVotedFor", raftActorContext.getId(), raftActorContext.getTermInformation().getVotedFor());
83 public void testThatAnElectionTimeoutIsTriggered() {
84 MockRaftActorContext actorContext = createActorContext();
85 candidate = new Candidate(actorContext);
87 MessageCollectorActor.expectFirstMatching(candidateActor, ElectionTimeout.class,
88 actorContext.getConfigParams().getElectionTimeOutInterval().$times(6).toMillis());
92 public void testHandleElectionTimeoutWhenThereAreZeroPeers() {
93 RaftActorContext raftActorContext = createActorContext();
94 candidate = new Candidate(raftActorContext);
96 RaftActorBehavior newBehavior =
97 candidate.handleMessage(candidateActor, ElectionTimeout.INSTANCE);
99 assertEquals("Behavior", RaftState.Leader, newBehavior.state());
103 public void testHandleElectionTimeoutWhenThereAreTwoNodeCluster() {
104 MockRaftActorContext raftActorContext = createActorContext();
105 raftActorContext.setPeerAddresses(setupPeers(1));
106 candidate = new Candidate(raftActorContext);
108 candidate = candidate.handleMessage(candidateActor, ElectionTimeout.INSTANCE);
110 assertEquals("Behavior", RaftState.Candidate, candidate.state());
114 public void testBecomeLeaderOnReceivingMajorityVotesInThreeNodeCluster() {
115 MockRaftActorContext raftActorContext = createActorContext();
116 raftActorContext.setLastApplied(raftActorContext.getReplicatedLog().lastIndex());
117 raftActorContext.setPeerAddresses(setupPeers(2));
118 candidate = new Candidate(raftActorContext);
120 candidate = candidate.handleMessage(peerActors[0], new RequestVoteReply(1, true));
122 assertEquals("Behavior", RaftState.Leader, candidate.state());
126 public void testBecomePreLeaderOnReceivingMajorityVotesInThreeNodeCluster() {
127 MockRaftActorContext raftActorContext = createActorContext();
128 raftActorContext.setLastApplied(-1);
129 raftActorContext.setPeerAddresses(setupPeers(2));
130 candidate = new Candidate(raftActorContext);
132 candidate = candidate.handleMessage(peerActors[0], new RequestVoteReply(1, true));
134 // LastApplied is -1 and behind the last index.
135 assertEquals("Behavior", RaftState.PreLeader, candidate.state());
139 public void testBecomeLeaderOnReceivingMajorityVotesInFiveNodeCluster() {
140 MockRaftActorContext raftActorContext = createActorContext();
141 raftActorContext.getTermInformation().update(2L, "other");
142 raftActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder()
143 .createEntries(0, 5, 1).build());
144 raftActorContext.setCommitIndex(raftActorContext.getReplicatedLog().lastIndex());
145 raftActorContext.setLastApplied(raftActorContext.getReplicatedLog().lastIndex());
146 raftActorContext.setPeerAddresses(setupPeers(4));
147 candidate = new Candidate(raftActorContext);
149 RequestVote requestVote = MessageCollectorActor.expectFirstMatching(peerActors[0], RequestVote.class);
150 assertEquals("getTerm", 3L, requestVote.getTerm());
151 assertEquals("getCandidateId", raftActorContext.getId(), requestVote.getCandidateId());
152 assertEquals("getLastLogTerm", 1L, requestVote.getLastLogTerm());
153 assertEquals("getLastLogIndex", 4L, requestVote.getLastLogIndex());
155 MessageCollectorActor.expectFirstMatching(peerActors[1], RequestVote.class);
156 MessageCollectorActor.expectFirstMatching(peerActors[2], RequestVote.class);
157 MessageCollectorActor.expectFirstMatching(peerActors[3], RequestVote.class);
159 // First peers denies the vote.
160 candidate = candidate.handleMessage(peerActors[0], new RequestVoteReply(1, false));
162 assertEquals("Behavior", RaftState.Candidate, candidate.state());
164 candidate = candidate.handleMessage(peerActors[1], new RequestVoteReply(1, true));
166 assertEquals("Behavior", RaftState.Candidate, candidate.state());
168 candidate = candidate.handleMessage(peerActors[2], new RequestVoteReply(1, true));
170 assertEquals("Behavior", RaftState.Leader, candidate.state());
174 public void testBecomeLeaderOnReceivingMajorityVotesWithNonVotingPeers() {
175 ElectionTerm mockElectionTerm = Mockito.mock(ElectionTerm.class);
176 Mockito.doReturn(1L).when(mockElectionTerm).getCurrentTerm();
177 RaftActorContext raftActorContext = new RaftActorContextImpl(candidateActor, candidateActor.actorContext(),
178 "candidate", mockElectionTerm, -1, -1, setupPeers(4), new DefaultConfigParamsImpl(),
179 new NonPersistentDataProvider(), LOG);
180 raftActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
181 raftActorContext.getPeerInfo("peer1").setVotingState(VotingState.NON_VOTING);
182 raftActorContext.getPeerInfo("peer4").setVotingState(VotingState.NON_VOTING);
183 candidate = new Candidate(raftActorContext);
185 MessageCollectorActor.expectFirstMatching(peerActors[1], RequestVote.class);
186 MessageCollectorActor.expectFirstMatching(peerActors[2], RequestVote.class);
187 MessageCollectorActor.assertNoneMatching(peerActors[0], RequestVote.class, 300);
188 MessageCollectorActor.assertNoneMatching(peerActors[3], RequestVote.class, 100);
190 candidate = candidate.handleMessage(peerActors[1], new RequestVoteReply(1, false));
192 assertEquals("Behavior", RaftState.Candidate, candidate.state());
194 candidate = candidate.handleMessage(peerActors[2], new RequestVoteReply(1, true));
196 assertEquals("Behavior", RaftState.Leader, candidate.state());
200 public void testResponseToHandleAppendEntriesWithLowerTerm() {
201 candidate = new Candidate(createActorContext());
204 RaftActorBehavior newBehavior = candidate.handleMessage(peerActors[0], new AppendEntries(1, "test", 0, 0,
205 Collections.<ReplicatedLogEntry>emptyList(), 0, -1, (short) 0));
207 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(
208 peerActors[0], AppendEntriesReply.class);
209 assertEquals("isSuccess", false, reply.isSuccess());
210 assertEquals("getTerm", 2, reply.getTerm());
211 assertTrue("New Behavior : " + newBehavior, newBehavior instanceof Candidate);
215 public void testResponseToHandleAppendEntriesWithHigherTerm() {
216 candidate = new Candidate(createActorContext());
219 RaftActorBehavior newBehavior = candidate.handleMessage(peerActors[0], new AppendEntries(5, "test", 0, 0,
220 Collections.<ReplicatedLogEntry>emptyList(), 0, -1, (short) 0));
222 assertTrue("New Behavior : " + newBehavior, newBehavior instanceof Follower);
226 public void testResponseToHandleAppendEntriesWithEqualTerm() {
227 MockRaftActorContext actorContext = createActorContext();
229 candidate = new Candidate(actorContext);
232 RaftActorBehavior newBehavior = candidate.handleMessage(peerActors[0], new AppendEntries(2, "test", 0, 0,
233 Collections.<ReplicatedLogEntry>emptyList(), 0, -1, (short) 0));
235 assertTrue("New Behavior : " + newBehavior + " term = " + actorContext.getTermInformation().getCurrentTerm(),
236 newBehavior instanceof Follower);
241 public void testResponseToRequestVoteWithLowerTerm() {
242 candidate = new Candidate(createActorContext());
245 candidate.handleMessage(peerActors[0], new RequestVote(1, "test", 0, 0));
247 RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(
248 peerActors[0], RequestVoteReply.class);
249 assertEquals("isVoteGranted", false, reply.isVoteGranted());
250 assertEquals("getTerm", 2, reply.getTerm());
254 public void testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForMatches() {
255 MockRaftActorContext context = createActorContext();
256 context.getTermInformation().update(1000, null);
258 // Once a candidate is created it will immediately increment the current term so after
259 // construction the currentTerm should be 1001
260 candidate = new Candidate(context);
263 candidate.handleMessage(peerActors[0], new RequestVote(1001, context.getId(), 10000, 999));
265 RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(
266 peerActors[0], RequestVoteReply.class);
267 assertEquals("isVoteGranted", true, reply.isVoteGranted());
268 assertEquals("getTerm", 1001, reply.getTerm());
272 public void testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForDoesNotMatch() {
273 MockRaftActorContext context = createActorContext();
274 context.getTermInformation().update(1000, null);
276 // Once a candidate is created it will immediately increment the current term so after
277 // construction the currentTerm should be 1001
278 candidate = new Candidate(context);
282 // RequestVote candidate ID ("candidate2") does not match this candidate's votedFor
283 // (it votes for itself)
284 candidate.handleMessage(peerActors[0], new RequestVote(1001, "candidate2", 10000, 999));
286 RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(
287 peerActors[0], RequestVoteReply.class);
288 assertEquals("isVoteGranted", false, reply.isVoteGranted());
289 assertEquals("getTerm", 1001, reply.getTerm());
293 public void testCandidateSchedulesElectionTimeoutImmediatelyWhenItHasNoPeers() {
294 MockRaftActorContext context = createActorContext();
296 Stopwatch stopwatch = Stopwatch.createStarted();
298 candidate = createBehavior(context);
300 MessageCollectorActor.expectFirstMatching(candidateActor, ElectionTimeout.class);
302 long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
304 assertTrue(elapsed < context.getConfigParams().getElectionTimeOutInterval().toMillis());
309 public void testHandleAppendEntriesAddSameEntryToLog() throws Exception {
310 MockRaftActorContext context = createActorContext();
312 context.getTermInformation().update(2, "test");
314 // Prepare the receivers log
315 MockRaftActorContext.MockPayload payload = new MockRaftActorContext.MockPayload("zero");
316 setLastLogEntry(context, 2, 0, payload);
318 List<ReplicatedLogEntry> entries = new ArrayList<>();
319 entries.add(new MockRaftActorContext.MockReplicatedLogEntry(2, 0, payload));
321 final AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 2, -1, (short)0);
323 behavior = createBehavior(context);
325 // Resetting the Candidates term to make sure it will match
326 // the term sent by AppendEntries. If this was not done then
327 // the test will fail because the Candidate will assume that
328 // the message was sent to it from a lower term peer and will
329 // thus respond with a failure
330 context.getTermInformation().update(2, "test");
332 // Send an unknown message so that the state of the RaftActor remains unchanged
333 behavior.handleMessage(candidateActor, "unknown");
335 RaftActorBehavior raftBehavior = behavior.handleMessage(candidateActor, appendEntries);
337 assertEquals("Raft state", RaftState.Follower, raftBehavior.state());
339 assertEquals("ReplicatedLog size", 1, context.getReplicatedLog().size());
341 handleAppendEntriesAddSameEntryToLogReply(candidateActor);
345 protected Candidate createBehavior(final RaftActorContext actorContext) {
346 return new Candidate(actorContext);
349 @Override protected MockRaftActorContext createActorContext() {
350 return new MockRaftActorContext("candidate", getSystem(), candidateActor);
353 @SuppressWarnings("unchecked")
354 private Map<String, String> setupPeers(final int count) {
355 Map<String, String> peerMap = new HashMap<>();
356 peerActors = new TestActorRef[count];
357 for (int i = 0; i < count; i++) {
358 peerActors[i] = actorFactory.createTestActor(Props.create(MessageCollectorActor.class),
359 actorFactory.generateActorId("peer"));
360 peerMap.put("peer" + (i + 1), peerActors[i].path().toString());
367 protected void assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(final MockRaftActorContext actorContext,
368 final ActorRef actorRef, final RaftRPC rpc) throws Exception {
369 super.assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(actorContext, actorRef, rpc);
370 if (rpc instanceof RequestVote) {
371 assertEquals("New votedFor", ((RequestVote)rpc).getCandidateId(),
372 actorContext.getTermInformation().getVotedFor());
374 assertEquals("New votedFor", null, actorContext.getTermInformation().getVotedFor());