Search in sources :

Example 86 with MockRaftActorContext

use of org.opendaylight.controller.cluster.raft.MockRaftActorContext 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 87 with MockRaftActorContext

use of org.opendaylight.controller.cluster.raft.MockRaftActorContext in project controller by opendaylight.

the class CandidateTest method testResponseToHandleAppendEntriesWithEqualTerm.

@Test
public void testResponseToHandleAppendEntriesWithEqualTerm() {
    MockRaftActorContext actorContext = createActorContext();
    candidate = new Candidate(actorContext);
    setupPeers(1);
    RaftActorBehavior newBehavior = candidate.handleMessage(peerActors[0], new AppendEntries(2, "test", 0, 0, Collections.<ReplicatedLogEntry>emptyList(), 0, -1, (short) 0));
    assertTrue("New Behavior : " + newBehavior + " term = " + actorContext.getTermInformation().getCurrentTerm(), newBehavior instanceof Follower);
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) Test(org.junit.Test)

Example 88 with MockRaftActorContext

use of org.opendaylight.controller.cluster.raft.MockRaftActorContext 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 89 with MockRaftActorContext

use of org.opendaylight.controller.cluster.raft.MockRaftActorContext 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 90 with MockRaftActorContext

use of org.opendaylight.controller.cluster.raft.MockRaftActorContext in project controller by opendaylight.

the class CandidateTest method testHandleElectionTimeoutWhenThereAreTwoNodeCluster.

@Test
public void testHandleElectionTimeoutWhenThereAreTwoNodeCluster() {
    MockRaftActorContext raftActorContext = createActorContext();
    raftActorContext.setPeerAddresses(setupPeers(1));
    candidate = new Candidate(raftActorContext);
    candidate = candidate.handleMessage(candidateActor, ElectionTimeout.INSTANCE);
    assertEquals("Behavior", RaftState.Candidate, candidate.state());
}
Also used : MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) Test(org.junit.Test)

Aggregations

MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)100 Test (org.junit.Test)93 AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)44 AppendEntriesReply (org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply)40 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)30 FiniteDuration (scala.concurrent.duration.FiniteDuration)29 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)28 ReplicatedLogEntry (org.opendaylight.controller.cluster.raft.ReplicatedLogEntry)24 ByteString (com.google.protobuf.ByteString)19 HashMap (java.util.HashMap)16 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)11 RequestVoteReply (org.opendaylight.controller.cluster.raft.messages.RequestVoteReply)11 FollowerInitialSyncUpStatus (org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus)9 RequestVote (org.opendaylight.controller.cluster.raft.messages.RequestVote)8 ArrayList (java.util.ArrayList)7 AbstractActorTest (org.opendaylight.controller.cluster.raft.AbstractActorTest)7 CaptureSnapshot (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot)7 FollowerLogInformation (org.opendaylight.controller.cluster.raft.FollowerLogInformation)6 SendInstallSnapshot (org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot)6 InstallSnapshotReply (org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply)6