Search in sources :

Example 6 with RequestVoteReply

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());
}
Also used : MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) RequestVoteReply(org.opendaylight.controller.cluster.raft.messages.RequestVoteReply) RequestVote(org.opendaylight.controller.cluster.raft.messages.RequestVote) AbstractActorTest(org.opendaylight.controller.cluster.raft.AbstractActorTest) Test(org.junit.Test)

Example 7 with RequestVoteReply

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());
}
Also used : ElectionTerm(org.opendaylight.controller.cluster.raft.ElectionTerm) RaftActorContextImpl(org.opendaylight.controller.cluster.raft.RaftActorContextImpl) NonPersistentDataProvider(org.opendaylight.controller.cluster.NonPersistentDataProvider) RaftActorContext(org.opendaylight.controller.cluster.raft.RaftActorContext) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) RequestVoteReply(org.opendaylight.controller.cluster.raft.messages.RequestVoteReply) Test(org.junit.Test)

Example 8 with RequestVoteReply

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());
}
Also used : MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) RequestVoteReply(org.opendaylight.controller.cluster.raft.messages.RequestVoteReply) RequestVote(org.opendaylight.controller.cluster.raft.messages.RequestVote) Test(org.junit.Test)

Example 9 with RequestVoteReply

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());
}
Also used : MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) RequestVoteReply(org.opendaylight.controller.cluster.raft.messages.RequestVoteReply) Test(org.junit.Test)

Example 10 with RequestVoteReply

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");
}
Also used : RequestVoteReply(org.opendaylight.controller.cluster.raft.messages.RequestVoteReply)

Aggregations

RequestVoteReply (org.opendaylight.controller.cluster.raft.messages.RequestVoteReply)20 Test (org.junit.Test)13 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)12 RequestVote (org.opendaylight.controller.cluster.raft.messages.RequestVote)10 AbstractActorTest (org.opendaylight.controller.cluster.raft.AbstractActorTest)3 FiniteDuration (scala.concurrent.duration.FiniteDuration)2 NonPersistentDataProvider (org.opendaylight.controller.cluster.NonPersistentDataProvider)1 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)1 ElectionTerm (org.opendaylight.controller.cluster.raft.ElectionTerm)1 RaftActorContext (org.opendaylight.controller.cluster.raft.RaftActorContext)1 RaftActorContextImpl (org.opendaylight.controller.cluster.raft.RaftActorContextImpl)1 AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)1 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)1