Search in sources :

Example 11 with RequestVote

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

the class FollowerTest method assertStateChangesToFollowerWhenRaftRPCHasNewerTerm.

@Override
protected void assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(final MockRaftActorContext actorContext, final ActorRef actorRef, final RaftRPC rpc) {
    super.assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(actorContext, actorRef, rpc);
    String expVotedFor = rpc instanceof RequestVote ? ((RequestVote) rpc).getCandidateId() : null;
    assertEquals("New votedFor", expVotedFor, actorContext.getTermInformation().getVotedFor());
}
Also used : ByteString(com.google.protobuf.ByteString) RequestVote(org.opendaylight.controller.cluster.raft.messages.RequestVote)

Example 12 with RequestVote

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

the class FollowerTest method testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForIsNotTheSameAsCandidateId.

@Test
public void testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForIsNotTheSameAsCandidateId() {
    logStart("testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForIsNotTheSameAsCandidateId");
    MockRaftActorContext context = createActorContext();
    long term = 1000;
    context.getTermInformation().update(term, "test");
    follower = createBehavior(context);
    follower.handleMessage(leaderActor, new RequestVote(term, "candidate", 10000, 999));
    RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, RequestVoteReply.class);
    assertEquals("isVoteGranted", false, reply.isVoteGranted());
    verify(follower, never()).scheduleElection(any(FiniteDuration.class));
}
Also used : MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) FiniteDuration(scala.concurrent.duration.FiniteDuration) RequestVoteReply(org.opendaylight.controller.cluster.raft.messages.RequestVoteReply) RequestVote(org.opendaylight.controller.cluster.raft.messages.RequestVote) Test(org.junit.Test)

Example 13 with RequestVote

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

the class AbstractRaftActorBehaviorTest method testHandleRequestVoteWhenSenderLogMoreUpToDate.

/**
 * This test verifies that when a RequestVote is received by the RaftActor
 * with the senders' log is more up to date than the receiver that the receiver grants
 * the vote to the sender.
 */
@Test
public void testHandleRequestVoteWhenSenderLogMoreUpToDate() {
    MockRaftActorContext context = createActorContext();
    behavior = createBehavior(context);
    context.getTermInformation().update(1, "test");
    behavior.handleMessage(behaviorActor, new RequestVote(context.getTermInformation().getCurrentTerm(), "test", 10000, 999));
    RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(behaviorActor, RequestVoteReply.class);
    assertEquals("isVoteGranted", true, 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 14 with RequestVote

use of org.opendaylight.controller.cluster.raft.messages.RequestVote 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 15 with RequestVote

use of org.opendaylight.controller.cluster.raft.messages.RequestVote 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)

Aggregations

RequestVote (org.opendaylight.controller.cluster.raft.messages.RequestVote)17 Test (org.junit.Test)11 RequestVoteReply (org.opendaylight.controller.cluster.raft.messages.RequestVoteReply)10 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)8 AbstractActorTest (org.opendaylight.controller.cluster.raft.AbstractActorTest)3 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)3 RaftRPC (org.opendaylight.controller.cluster.raft.messages.RaftRPC)3 FiniteDuration (scala.concurrent.duration.FiniteDuration)3 ActorRef (akka.actor.ActorRef)2 TestActorRef (akka.testkit.TestActorRef)2 ElectionTimeout (org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout)2 TimeoutNow (org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow)2 AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)2 ActorSelection (akka.actor.ActorSelection)1 Props (akka.actor.Props)1 UntypedActor (akka.actor.UntypedActor)1 Dispatchers (akka.dispatch.Dispatchers)1 TestKit (akka.testkit.javadsl.TestKit)1 Optional (com.google.common.base.Optional)1 Stopwatch (com.google.common.base.Stopwatch)1