Search in sources :

Example 91 with MockRaftActorContext

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

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

the class CandidateTest method testHandleAppendEntriesAddSameEntryToLog.

@Test
@Override
public void testHandleAppendEntriesAddSameEntryToLog() {
    MockRaftActorContext context = createActorContext();
    context.getTermInformation().update(2, "test");
    // Prepare the receivers log
    MockRaftActorContext.MockPayload payload = new MockRaftActorContext.MockPayload("zero");
    setLastLogEntry(context, 2, 0, payload);
    List<ReplicatedLogEntry> entries = new ArrayList<>();
    entries.add(new SimpleReplicatedLogEntry(0, 2, payload));
    final AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 2, -1, (short) 0);
    behavior = createBehavior(context);
    // Resetting the Candidates term to make sure it will match
    // the term sent by AppendEntries. If this was not done then
    // the test will fail because the Candidate will assume that
    // the message was sent to it from a lower term peer and will
    // thus respond with a failure
    context.getTermInformation().update(2, "test");
    // Send an unknown message so that the state of the RaftActor remains unchanged
    behavior.handleMessage(candidateActor, "unknown");
    RaftActorBehavior raftBehavior = behavior.handleMessage(candidateActor, appendEntries);
    assertEquals("Raft state", RaftState.Follower, raftBehavior.state());
    assertEquals("ReplicatedLog size", 1, context.getReplicatedLog().size());
    handleAppendEntriesAddSameEntryToLogReply(candidateActor);
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) ArrayList(java.util.ArrayList) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) Test(org.junit.Test)

Example 93 with MockRaftActorContext

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

the class FollowerTest method testFollowerSchedulesElectionIfNonVoting.

@Test
public void testFollowerSchedulesElectionIfNonVoting() {
    MockRaftActorContext context = createActorContext();
    context.updatePeerIds(new ServerConfigurationPayload(Arrays.asList(new ServerInfo(context.getId(), false))));
    ((DefaultConfigParamsImpl) context.getConfigParams()).setHeartBeatInterval(FiniteDuration.apply(100, TimeUnit.MILLISECONDS));
    ((DefaultConfigParamsImpl) context.getConfigParams()).setElectionTimeoutFactor(1);
    follower = new Follower(context, "leader", (short) 1);
    ElectionTimeout electionTimeout = MessageCollectorActor.expectFirstMatching(followerActor, ElectionTimeout.class);
    RaftActorBehavior newBehavior = follower.handleMessage(ActorRef.noSender(), electionTimeout);
    assertSame("handleMessage result", follower, newBehavior);
    assertNull("Expected null leaderId", follower.getLeaderId());
}
Also used : ServerConfigurationPayload(org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload) ServerInfo(org.opendaylight.controller.cluster.raft.persisted.ServerInfo) ElectionTimeout(org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) Test(org.junit.Test)

Example 94 with MockRaftActorContext

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

the class FollowerTest method testHandleFirstAppendEntries.

@Test
public void testHandleFirstAppendEntries() {
    logStart("testHandleFirstAppendEntries");
    MockRaftActorContext context = createActorContext();
    context.getReplicatedLog().clear(0, 2);
    context.getReplicatedLog().append(newReplicatedLogEntry(1, 100, "bar"));
    context.getReplicatedLog().setSnapshotIndex(99);
    List<ReplicatedLogEntry> entries = Arrays.asList(newReplicatedLogEntry(2, 101, "foo"));
    Assert.assertEquals(1, context.getReplicatedLog().size());
    // The new commitIndex is 101
    AppendEntries appendEntries = new AppendEntries(2, "leader-1", 100, 1, entries, 101, 100, (short) 0);
    follower = createBehavior(context);
    follower.handleMessage(leaderActor, appendEntries);
    FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
    AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
    assertFalse(syncStatus.isInitialSyncDone());
    assertTrue("append entries reply should be true", reply.isSuccess());
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) FollowerInitialSyncUpStatus(org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus) Test(org.junit.Test)

Example 95 with MockRaftActorContext

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

the class FollowerTest method testHandleAppendEntriesSenderPrevLogTermNotSameAsReceiverPrevLogTerm.

/**
 * This test verifies that when an AppendEntries is received a specific prevLogTerm
 * which does not match the term that is in RaftActors log entry at prevLogIndex
 * then the RaftActor does not change it's state and it returns a failure.
 */
@Test
public void testHandleAppendEntriesSenderPrevLogTermNotSameAsReceiverPrevLogTerm() {
    logStart("testHandleAppendEntriesSenderPrevLogTermNotSameAsReceiverPrevLogTerm");
    MockRaftActorContext context = createActorContext();
    // First set the receivers term to lower number
    context.getTermInformation().update(95, "test");
    // AppendEntries is now sent with a bigger term
    // this will set the receivers term to be the same as the sender's term
    AppendEntries appendEntries = new AppendEntries(100, "leader", 0, 0, Collections.emptyList(), 101, -1, (short) 0);
    follower = createBehavior(context);
    RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries);
    Assert.assertSame(follower, newBehavior);
    AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
    assertEquals("isSuccess", false, reply.isSuccess());
}
Also used : AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) 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