use of org.opendaylight.controller.cluster.raft.messages.RequestVoteReply in project controller by opendaylight.
the class AbstractRaftActorBehaviorTest method testHandleRequestVoteWhenSenderLogLessUptoDate.
/**
* This test verifies that when a RaftActor receives a RequestVote message
* with a term that is greater than it's currentTerm but a less up-to-date
* log then the receiving RaftActor will not grant the vote to the sender.
*/
@Test
public void testHandleRequestVoteWhenSenderLogLessUptoDate() {
MockRaftActorContext context = createActorContext();
behavior = createBehavior(context);
context.getTermInformation().update(1, "test");
int index = 2000;
setLastLogEntry(context, context.getTermInformation().getCurrentTerm(), index, new MockRaftActorContext.MockPayload(""));
behavior.handleMessage(behaviorActor, new RequestVote(context.getTermInformation().getCurrentTerm(), "test", index - 1, context.getTermInformation().getCurrentTerm()));
RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(behaviorActor, RequestVoteReply.class);
assertEquals("isVoteGranted", false, reply.isVoteGranted());
}
use of org.opendaylight.controller.cluster.raft.messages.RequestVoteReply in project controller by opendaylight.
the class CandidateTest method testBecomeLeaderOnReceivingMajorityVotesWithNonVotingPeers.
@Test
public void testBecomeLeaderOnReceivingMajorityVotesWithNonVotingPeers() {
ElectionTerm mockElectionTerm = Mockito.mock(ElectionTerm.class);
Mockito.doReturn(1L).when(mockElectionTerm).getCurrentTerm();
RaftActorContext raftActorContext = new RaftActorContextImpl(candidateActor, candidateActor.actorContext(), "candidate", mockElectionTerm, -1, -1, setupPeers(4), new DefaultConfigParamsImpl(), new NonPersistentDataProvider(Runnable::run), applyState -> {
}, LOG);
raftActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
raftActorContext.getPeerInfo("peer1").setVotingState(VotingState.NON_VOTING);
raftActorContext.getPeerInfo("peer4").setVotingState(VotingState.NON_VOTING);
candidate = new Candidate(raftActorContext);
MessageCollectorActor.expectFirstMatching(peerActors[1], RequestVote.class);
MessageCollectorActor.expectFirstMatching(peerActors[2], RequestVote.class);
MessageCollectorActor.assertNoneMatching(peerActors[0], RequestVote.class, 300);
MessageCollectorActor.assertNoneMatching(peerActors[3], RequestVote.class, 100);
candidate = candidate.handleMessage(peerActors[1], new RequestVoteReply(1, false));
assertEquals("Behavior", RaftState.Candidate, candidate.state());
candidate = candidate.handleMessage(peerActors[2], new RequestVoteReply(1, true));
assertEquals("Behavior", RaftState.Leader, candidate.state());
}
use of org.opendaylight.controller.cluster.raft.messages.RequestVoteReply in project controller by opendaylight.
the class CandidateTest method testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForDoesNotMatch.
@Test
public void testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForDoesNotMatch() {
MockRaftActorContext context = createActorContext();
context.getTermInformation().update(1000, null);
// Once a candidate is created it will immediately increment the current term so after
// construction the currentTerm should be 1001
candidate = new Candidate(context);
setupPeers(1);
// RequestVote candidate ID ("candidate2") does not match this candidate's votedFor
// (it votes for itself)
candidate.handleMessage(peerActors[0], new RequestVote(1001, "candidate2", 10000, 999));
RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(peerActors[0], RequestVoteReply.class);
assertEquals("isVoteGranted", false, reply.isVoteGranted());
assertEquals("getTerm", 1001, reply.getTerm());
}
use of org.opendaylight.controller.cluster.raft.messages.RequestVoteReply in project controller by opendaylight.
the class CandidateTest method testBecomeLeaderOnReceivingMajorityVotesInThreeNodeCluster.
@Test
public void testBecomeLeaderOnReceivingMajorityVotesInThreeNodeCluster() {
MockRaftActorContext raftActorContext = createActorContext();
raftActorContext.setLastApplied(raftActorContext.getReplicatedLog().lastIndex());
raftActorContext.setPeerAddresses(setupPeers(2));
candidate = new Candidate(raftActorContext);
candidate = candidate.handleMessage(peerActors[0], new RequestVoteReply(1, true));
assertEquals("Behavior", RaftState.Leader, candidate.state());
}
use of org.opendaylight.controller.cluster.raft.messages.RequestVoteReply in project controller by opendaylight.
the class DelayedMessagesElectionScenarioTest method sendElectionTimeoutToFollowerMember3.
private void sendElectionTimeoutToFollowerMember3() {
testLog.info("sendElectionTimeoutToFollowerMember3 starting");
// Send ElectionTimeout to member 3 to simulate missing heartbeat from a Leader. member 3
// should switch to Candidate and send out RequestVote messages. member 1 should grant the
// vote and send a reply. After receiving the RequestVoteReply, member 3 should switch to leader.
member2Actor.expectBehaviorStateChange();
member3Actor.clear();
member3Actor.expectMessageClass(RequestVoteReply.class, 1);
member3Actor.expectMessageClass(AppendEntriesReply.class, 2);
member3ActorRef.tell(TimeoutNow.INSTANCE, ActorRef.noSender());
member3Actor.waitForExpectedMessages(RequestVoteReply.class);
RequestVoteReply requestVoteReply = member3Actor.getCapturedMessage(RequestVoteReply.class);
assertEquals("getTerm", member3Context.getTermInformation().getCurrentTerm(), requestVoteReply.getTerm());
assertEquals("isVoteGranted", true, requestVoteReply.isVoteGranted());
verifyBehaviorState("member 3", member3Actor, RaftState.Leader);
// member 2 should've switched to Follower as member 3's RequestVote term (3) was greater
// than member 2's term (2).
member2Actor.waitForBehaviorStateChange();
verifyBehaviorState("member 2", member2Actor, RaftState.Follower);
// The switch to leader should cause an immediate AppendEntries heartbeat from member 3.
member3Actor.waitForExpectedMessages(AppendEntriesReply.class);
assertEquals("member 1 election term", 3, member1Context.getTermInformation().getCurrentTerm());
assertEquals("member 2 election term", 3, member2Context.getTermInformation().getCurrentTerm());
assertEquals("member 3 election term", 3, member3Context.getTermInformation().getCurrentTerm());
testLog.info("sendElectionTimeoutToFollowerMember3 ending");
}
Aggregations