package org.opendaylight.controller.cluster.raft.behaviors;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.junit.After;
-import org.junit.Assert;
import org.junit.Test;
import org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl;
import org.opendaylight.controller.cluster.raft.FollowerLogInformation;
import org.opendaylight.controller.cluster.raft.RaftVersions;
import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
import org.opendaylight.controller.cluster.raft.ReplicatedLogImplEntry;
-import org.opendaylight.controller.cluster.raft.SerializationUtils;
import org.opendaylight.controller.cluster.raft.Snapshot;
import org.opendaylight.controller.cluster.raft.VotingState;
-import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries;
import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot;
import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
import org.opendaylight.controller.cluster.raft.base.messages.Replicate;
import org.opendaylight.controller.cluster.raft.base.messages.SendHeartBeat;
import org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot;
-import org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader.FollowerToSnapshot;
+import org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow;
import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
import org.opendaylight.controller.cluster.raft.messages.InstallSnapshot;
import org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply;
import org.opendaylight.controller.cluster.raft.messages.RaftRPC;
import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
+import org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries;
import org.opendaylight.controller.cluster.raft.policy.DefaultRaftPolicy;
import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
import org.opendaylight.controller.cluster.raft.utils.ForwardMessageToBehaviorActor;
import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
+import org.opendaylight.yangtools.concepts.Identifier;
import scala.concurrent.duration.FiniteDuration;
public class LeaderTest extends AbstractLeaderTest<Leader> {
leader = new Leader(createActorContext());
- // handle message should return the Leader state when it receives an
- // unknown message
- RaftActorBehavior behavior = leader.handleMessage(followerActor, "foo");
- Assert.assertTrue(behavior instanceof Leader);
+ // handle message should null when it receives an unknown message
+ assertNull(leader.handleMessage(followerActor, "foo"));
}
@Test
logStart("testThatLeaderSendsAHeartbeatMessageToAllFollowers");
MockRaftActorContext actorContext = createActorContextWithFollower();
+ actorContext.setCommitIndex(-1);
short payloadVersion = (short)5;
actorContext.setPayloadVersion(payloadVersion);
logStart("testHandleReplicateMessageWithHigherTermThanPreviousEntry");
MockRaftActorContext actorContext = createActorContextWithFollower();
+ actorContext.setCommitIndex(-1);
// The raft context is initialized with a couple log entries. However the commitIndex
// is -1, simulating that the leader previously didn't get consensus and thus the log entries weren't
actorContext.getReplicatedLog().append(newEntry);
- RaftActorBehavior raftBehavior = leader.handleMessage(leaderActor,
- new Replicate(leaderActor, "state-id", newEntry));
+ final Identifier id = new MockIdentifier("state-id");
+ RaftActorBehavior raftBehavior = leader.handleMessage(leaderActor, new Replicate(leaderActor, id, newEntry));
// State should not change
assertTrue(raftBehavior instanceof Leader);
ApplyState last = applyStateList.get((int) newLogIndex - 1);
assertEquals("getData", newEntry.getData(), last.getReplicatedLogEntry().getData());
- assertEquals("getIdentifier", "state-id", last.getIdentifier());
+ assertEquals("getIdentifier", id, last.getIdentifier());
}
@Test
ByteString bs = toByteString(leadersSnapshot);
leader.setSnapshot(Snapshot.create(bs.toByteArray(), Collections.<ReplicatedLogEntry>emptyList(),
commitIndex, snapshotTerm, commitIndex, snapshotTerm));
- FollowerToSnapshot fts = leader.new FollowerToSnapshot(bs);
- leader.setFollowerSnapshot(FOLLOWER_ID, fts);
+ LeaderInstallSnapshotState fts = new LeaderInstallSnapshotState(bs,
+ actorContext.getConfigParams().getSnapshotChunkSize(), leader.logName());
+ leader.getFollower(FOLLOWER_ID).setLeaderInstallSnapshotState(fts);
//send first chunk and no InstallSnapshotReply received yet
fts.getNextChunk();
leader.handleMessage(leaderActor, SendHeartBeat.INSTANCE);
- AppendEntries aeproto = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
-
- AppendEntries ae = (AppendEntries) SerializationUtils.fromSerializable(aeproto);
+ AppendEntries ae = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
assertTrue("AppendEntries should be sent with empty entries", ae.getEntries().isEmpty());
// this should invoke a sendinstallsnapshot as followersLastIndex < snapshotIndex
RaftActorBehavior raftBehavior = leader.handleMessage(
- leaderActor, new Replicate(null, "state-id", entry));
+ leaderActor, new Replicate(null, new MockIdentifier("state-id"), entry));
assertTrue(raftBehavior instanceof Leader);
//update follower timestamp
leader.markFollowerActive(FOLLOWER_ID);
- leader.handleMessage(leaderActor, new Replicate(null, "state-id", entry));
+ leader.handleMessage(leaderActor, new Replicate(null, new MockIdentifier("state-id"), entry));
assertEquals("isCapturing", true, actorContext.getSnapshotManager().isCapturing());
assertEquals(2, cs.getLastTerm());
// if an initiate is started again when first is in progress, it shouldnt initiate Capture
- leader.handleMessage(leaderActor, new Replicate(null, "state-id", entry));
+ leader.handleMessage(leaderActor, new Replicate(null, new MockIdentifier("state-id"), entry));
- Assert.assertSame("CaptureSnapshot instance", cs, actorContext.getSnapshotManager().getCaptureSnapshot());
+ assertSame("CaptureSnapshot instance", cs, actorContext.getSnapshotManager().getCaptureSnapshot());
}
@Test
assertEquals(4, cs.getLastIndex());
assertEquals(2, cs.getLastTerm());
- // if an initiate is started again when first is in progress, it shouldnt initiate Capture
- leader.handleMessage(leaderActor, new Replicate(null, "state-id", entry));
+ // if an initiate is started again when first is in progress, it should not initiate Capture
+ leader.handleMessage(leaderActor, new Replicate(null, new MockIdentifier("state-id"), entry));
- Assert.assertSame("CaptureSnapshot instance", cs, actorContext.getSnapshotManager().getCaptureSnapshot());
+ assertSame("CaptureSnapshot instance", cs, actorContext.getSnapshotManager().getCaptureSnapshot());
}
ByteString bs = toByteString(leadersSnapshot);
leader.setSnapshot(Snapshot.create(bs.toByteArray(), Collections.<ReplicatedLogEntry>emptyList(),
commitIndex, snapshotTerm, commitIndex, snapshotTerm));
- FollowerToSnapshot fts = leader.new FollowerToSnapshot(bs);
- leader.setFollowerSnapshot(FOLLOWER_ID, fts);
+ LeaderInstallSnapshotState fts = new LeaderInstallSnapshotState(bs,
+ actorContext.getConfigParams().getSnapshotChunkSize(), leader.logName());
+ leader.getFollower(FOLLOWER_ID).setLeaderInstallSnapshotState(fts);
while(!fts.isLastChunk(fts.getChunkIndex())) {
fts.getNextChunk();
fts.incrementChunkIndex();
assertTrue(raftBehavior instanceof Leader);
- assertEquals(0, leader.followerSnapshotSize());
assertEquals(1, leader.followerLogSize());
FollowerLogInformation fli = leader.getFollower(FOLLOWER_ID);
assertNotNull(fli);
+ assertNull(fli.getInstallSnapshotState());
assertEquals(commitIndex, fli.getMatchIndex());
assertEquals(commitIndex + 1, fli.getNextIndex());
+ assertFalse(leader.hasSnapshot());
}
@Test
installSnapshot = MessageCollectorActor.getFirstMatching(followerActor, InstallSnapshot.class);
- Assert.assertNull(installSnapshot);
+ assertNull(installSnapshot);
}
assertEquals(1, installSnapshot.getChunkIndex());
assertEquals(3, installSnapshot.getTotalChunks());
- assertEquals(AbstractLeader.INITIAL_LAST_CHUNK_HASH_CODE, installSnapshot.getLastChunkHashCode().get().intValue());
+ assertEquals(LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE,
+ installSnapshot.getLastChunkHashCode().get().intValue());
int hashCode = Arrays.hashCode(installSnapshot.getData());
}
@Test
- public void testFollowerToSnapshotLogic() {
- logStart("testFollowerToSnapshotLogic");
-
- MockRaftActorContext actorContext = createActorContext();
-
- actorContext.setConfigParams(new DefaultConfigParamsImpl() {
- @Override
- public int getSnapshotChunkSize() {
- return 50;
- }
- });
-
- leader = new Leader(actorContext);
+ public void testLeaderInstallSnapshotState() {
+ logStart("testLeaderInstallSnapshotState");
Map<String, String> leadersSnapshot = new HashMap<>();
leadersSnapshot.put("1", "A");
ByteString bs = toByteString(leadersSnapshot);
byte[] barray = bs.toByteArray();
- FollowerToSnapshot fts = leader.new FollowerToSnapshot(bs);
- leader.setFollowerSnapshot(FOLLOWER_ID, fts);
+ LeaderInstallSnapshotState fts = new LeaderInstallSnapshotState(bs, 50, "test");
assertEquals(bs.size(), barray.length);
appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
assertEquals("getLeaderCommit", leaderCommitIndex, appendEntries.getLeaderCommit());
- assertEquals("getPrevLogIndex", -1, appendEntries.getPrevLogIndex());
+ assertEquals("getPrevLogIndex", 0, appendEntries.getPrevLogIndex());
assertEquals("Log entries size", 2, appendEntries.getEntries().size());
assertEquals("First entry index", 1, appendEntries.getEntries().get(0).getIndex());
appendEntries = appendEntriesList.get(1);
assertEquals("getLeaderCommit", leaderCommitIndex, appendEntries.getLeaderCommit());
- assertEquals("getPrevLogIndex", -1, appendEntries.getPrevLogIndex());
+ assertEquals("getPrevLogIndex", 1, appendEntries.getPrevLogIndex());
assertEquals("Log entries size", 2, appendEntries.getEntries().size());
assertEquals("First entry index", 2, appendEntries.getEntries().get(0).getIndex());
leader = new Leader(leaderActorContext);
RaftActorBehavior behavior = leader.handleMessage(leaderActor, Leader.ISOLATED_LEADER_CHECK);
- Assert.assertTrue(behavior instanceof Leader);
+ assertTrue(behavior instanceof Leader);
+ }
+
+ @Test
+ public void testIsolatedLeaderCheckNoVotingFollowers() {
+ logStart("testIsolatedLeaderCheckNoVotingFollowers");
+
+ MockRaftActorContext followerActorContext = createFollowerActorContextWithLeader();
+ Follower follower = new Follower(followerActorContext);
+ followerActor.underlyingActor().setBehavior(follower);
+
+ MockRaftActorContext leaderActorContext = createActorContextWithFollower();
+ ((DefaultConfigParamsImpl)leaderActorContext.getConfigParams()).setHeartBeatInterval(
+ new FiniteDuration(1000, TimeUnit.SECONDS));
+ leaderActorContext.getPeerInfo(FOLLOWER_ID).setVotingState(VotingState.NON_VOTING);
+
+ leader = new Leader(leaderActorContext);
+ leader.getFollower(FOLLOWER_ID).markFollowerActive();
+ RaftActorBehavior behavior = leader.handleMessage(leaderActor, Leader.ISOLATED_LEADER_CHECK);
+ assertTrue("Expected Leader", behavior instanceof Leader);
}
private RaftActorBehavior setupIsolatedLeaderCheckTestWithTwoFollowers(RaftPolicy raftPolicy){
leader.markFollowerActive("follower-1");
leader.markFollowerActive("follower-2");
RaftActorBehavior behavior = leader.handleMessage(leaderActor, Leader.ISOLATED_LEADER_CHECK);
- Assert.assertTrue("Behavior not instance of Leader when all followers are active",
- behavior instanceof Leader);
+ assertTrue("Behavior not instance of Leader when all followers are active", behavior instanceof Leader);
// kill 1 follower and verify if that got killed
final JavaTestKit probe = new JavaTestKit(getSystem());
leader.markFollowerInActive("follower-1");
leader.markFollowerActive("follower-2");
behavior = leader.handleMessage(leaderActor, Leader.ISOLATED_LEADER_CHECK);
- Assert.assertTrue("Behavior not instance of Leader when majority of followers are active",
- behavior instanceof Leader);
+ assertTrue("Behavior not instance of Leader when majority of followers are active", behavior instanceof Leader);
// kill 2nd follower and leader should change to Isolated leader
followerActor2.tell(PoisonPill.getInstance(), null);
RaftActorBehavior behavior = setupIsolatedLeaderCheckTestWithTwoFollowers(DefaultRaftPolicy.INSTANCE);
- Assert.assertTrue("Behavior not instance of IsolatedLeader when majority followers are inactive",
+ assertTrue("Behavior not instance of IsolatedLeader when majority followers are inactive",
behavior instanceof IsolatedLeader);
}
RaftActorBehavior behavior = setupIsolatedLeaderCheckTestWithTwoFollowers(createRaftPolicy(false, true));
- Assert.assertTrue("Behavior should not switch to IsolatedLeader because elections are disabled",
+ assertTrue("Behavior should not switch to IsolatedLeader because elections are disabled",
behavior instanceof Leader);
}
new FiniteDuration(1000, TimeUnit.SECONDS));
leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
+ leaderActorContext.setCommitIndex(-1);
String nonVotingFollowerId = "nonvoting-follower";
TestActorRef<ForwardMessageToBehaviorActor> nonVotingFollowerActor = actorFactory.createTestActor(
MessageCollectorActor.expectMatching(followerActor, AppendEntries.class, 2);
// Leader should force an election timeout
- MessageCollectorActor.expectFirstMatching(followerActor, ElectionTimeout.class);
+ MessageCollectorActor.expectFirstMatching(followerActor, TimeoutNow.class);
verify(mockTransferCohort).transferComplete();
}
MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
// Leader should force an election timeout
- MessageCollectorActor.expectFirstMatching(followerActor, ElectionTimeout.class);
+ MessageCollectorActor.expectFirstMatching(followerActor, TimeoutNow.class);
verify(mockTransferCohort).transferComplete();
}
leader.handleMessage(leaderActor, new AppendEntriesReply(FOLLOWER_ID, 1, true, 1, 1, (short)0));
// Leader should force an election timeout
- MessageCollectorActor.expectFirstMatching(followerActor, ElectionTimeout.class);
+ MessageCollectorActor.expectFirstMatching(followerActor, TimeoutNow.class);
verify(mockTransferCohort).transferComplete();
}