Search in sources :

Example 16 with RequestVoteReply

use of org.opendaylight.controller.cluster.raft.messages.RequestVoteReply in project controller by opendaylight.

the class CandidateTest method testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForMatches.

@Test
public void testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForMatches() {
    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);
    candidate.handleMessage(peerActors[0], new RequestVote(1001, context.getId(), 10000, 999));
    RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(peerActors[0], RequestVoteReply.class);
    assertEquals("isVoteGranted", true, 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 17 with RequestVoteReply

use of org.opendaylight.controller.cluster.raft.messages.RequestVoteReply in project controller by opendaylight.

the class CandidateTest method testBecomeLeaderOnReceivingMajorityVotesInFiveNodeCluster.

@Test
public void testBecomeLeaderOnReceivingMajorityVotesInFiveNodeCluster() {
    MockRaftActorContext raftActorContext = createActorContext();
    raftActorContext.getTermInformation().update(2L, "other");
    raftActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 5, 1).build());
    raftActorContext.setCommitIndex(raftActorContext.getReplicatedLog().lastIndex());
    raftActorContext.setLastApplied(raftActorContext.getReplicatedLog().lastIndex());
    raftActorContext.setPeerAddresses(setupPeers(4));
    candidate = new Candidate(raftActorContext);
    RequestVote requestVote = MessageCollectorActor.expectFirstMatching(peerActors[0], RequestVote.class);
    assertEquals("getTerm", 3L, requestVote.getTerm());
    assertEquals("getCandidateId", raftActorContext.getId(), requestVote.getCandidateId());
    assertEquals("getLastLogTerm", 1L, requestVote.getLastLogTerm());
    assertEquals("getLastLogIndex", 4L, requestVote.getLastLogIndex());
    MessageCollectorActor.expectFirstMatching(peerActors[1], RequestVote.class);
    MessageCollectorActor.expectFirstMatching(peerActors[2], RequestVote.class);
    MessageCollectorActor.expectFirstMatching(peerActors[3], RequestVote.class);
    // First peers denies the vote.
    candidate = candidate.handleMessage(peerActors[0], new RequestVoteReply(1, false));
    assertEquals("Behavior", RaftState.Candidate, candidate.state());
    candidate = candidate.handleMessage(peerActors[1], new RequestVoteReply(1, true));
    assertEquals("Behavior", RaftState.Candidate, candidate.state());
    candidate = candidate.handleMessage(peerActors[2], 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) RequestVote(org.opendaylight.controller.cluster.raft.messages.RequestVote) Test(org.junit.Test)

Example 18 with RequestVoteReply

use of org.opendaylight.controller.cluster.raft.messages.RequestVoteReply in project controller by opendaylight.

the class CandidateTest method testBecomePreLeaderOnReceivingMajorityVotesInThreeNodeCluster.

@Test
public void testBecomePreLeaderOnReceivingMajorityVotesInThreeNodeCluster() {
    MockRaftActorContext raftActorContext = createActorContext();
    raftActorContext.setLastApplied(-1);
    raftActorContext.setPeerAddresses(setupPeers(2));
    candidate = new Candidate(raftActorContext);
    candidate = candidate.handleMessage(peerActors[0], new RequestVoteReply(1, true));
    // LastApplied is -1 and behind the last index.
    assertEquals("Behavior", RaftState.PreLeader, candidate.state());
}
Also used : MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) RequestVoteReply(org.opendaylight.controller.cluster.raft.messages.RequestVoteReply) Test(org.junit.Test)

Example 19 with RequestVoteReply

use of org.opendaylight.controller.cluster.raft.messages.RequestVoteReply in project controller by opendaylight.

the class CandidateTest method testResponseToRequestVoteWithLowerTerm.

@Test
public void testResponseToRequestVoteWithLowerTerm() {
    candidate = new Candidate(createActorContext());
    setupPeers(1);
    candidate.handleMessage(peerActors[0], new RequestVote(1, "test", 0, 0));
    RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(peerActors[0], RequestVoteReply.class);
    assertEquals("isVoteGranted", false, reply.isVoteGranted());
    assertEquals("getTerm", 2, reply.getTerm());
}
Also used : RequestVoteReply(org.opendaylight.controller.cluster.raft.messages.RequestVoteReply) RequestVote(org.opendaylight.controller.cluster.raft.messages.RequestVote) Test(org.junit.Test)

Example 20 with RequestVoteReply

use of org.opendaylight.controller.cluster.raft.messages.RequestVoteReply in project controller by opendaylight.

the class AbstractRaftActorBehavior method requestVote.

/**
 * Handles the logic for the RequestVote message that is common for all behaviors.
 *
 * @param sender the ActorRef that sent the message
 * @param requestVote the message
 * @return a new behavior if it was changed or the current behavior
 */
protected RaftActorBehavior requestVote(ActorRef sender, RequestVote requestVote) {
    log.debug("{}: In requestVote:  {} - currentTerm: {}, votedFor: {}, lastIndex: {}, lastTerm: {}", logName(), requestVote, currentTerm(), votedFor(), lastIndex(), lastTerm());
    boolean grantVote = canGrantVote(requestVote);
    if (grantVote) {
        context.getTermInformation().updateAndPersist(requestVote.getTerm(), requestVote.getCandidateId());
    }
    RequestVoteReply reply = new RequestVoteReply(currentTerm(), grantVote);
    log.debug("{}: requestVote returning: {}", logName(), reply);
    sender.tell(reply, actor());
    return this;
}
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