Search in sources :

Example 1 with SnapshotHolder

use of org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader.SnapshotHolder in project controller by opendaylight.

the class LeaderTest method testSendAppendEntriesOnAnInProgressInstallSnapshot.

@Test
public void testSendAppendEntriesOnAnInProgressInstallSnapshot() throws Exception {
    logStart("testSendAppendEntriesOnAnInProgressInstallSnapshot");
    final MockRaftActorContext actorContext = createActorContextWithFollower();
    Map<String, String> leadersSnapshot = new HashMap<>();
    leadersSnapshot.put("1", "A");
    leadersSnapshot.put("2", "B");
    leadersSnapshot.put("3", "C");
    // clears leaders log
    actorContext.getReplicatedLog().removeFrom(0);
    final int commitIndex = 3;
    final int snapshotIndex = 2;
    final int snapshotTerm = 1;
    // set the snapshot variables in replicatedlog
    actorContext.getReplicatedLog().setSnapshotIndex(snapshotIndex);
    actorContext.getReplicatedLog().setSnapshotTerm(snapshotTerm);
    actorContext.setCommitIndex(commitIndex);
    // set follower timeout to 2 mins, helps during debugging
    actorContext.setConfigParams(new MockConfigParamsImpl(120000L, 10));
    leader = new Leader(actorContext);
    leader.getFollower(FOLLOWER_ID).setMatchIndex(-1);
    leader.getFollower(FOLLOWER_ID).setNextIndex(0);
    // update follower timestamp
    leader.markFollowerActive(FOLLOWER_ID);
    ByteString bs = toByteString(leadersSnapshot);
    leader.setSnapshotHolder(new SnapshotHolder(Snapshot.create(ByteState.of(bs.toByteArray()), Collections.<ReplicatedLogEntry>emptyList(), commitIndex, snapshotTerm, commitIndex, snapshotTerm, -1, null, null), ByteSource.wrap(bs.toByteArray())));
    LeaderInstallSnapshotState fts = new LeaderInstallSnapshotState(actorContext.getConfigParams().getSnapshotChunkSize(), leader.logName());
    fts.setSnapshotBytes(ByteSource.wrap(bs.toByteArray()));
    leader.getFollower(FOLLOWER_ID).setLeaderInstallSnapshotState(fts);
    // send first chunk and no InstallSnapshotReply received yet
    fts.getNextChunk();
    fts.incrementChunkIndex();
    Uninterruptibles.sleepUninterruptibly(actorContext.getConfigParams().getHeartBeatInterval().toMillis(), TimeUnit.MILLISECONDS);
    leader.handleMessage(leaderActor, SendHeartBeat.INSTANCE);
    AppendEntries ae = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    assertTrue("AppendEntries should be sent with empty entries", ae.getEntries().isEmpty());
    // InstallSnapshotReply received
    fts.markSendStatus(true);
    leader.handleMessage(leaderActor, SendHeartBeat.INSTANCE);
    InstallSnapshot is = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
    assertEquals(commitIndex, is.getLastIncludedIndex());
}
Also used : HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) ByteString(com.google.protobuf.ByteString) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) SnapshotHolder(org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader.SnapshotHolder) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) Test(org.junit.Test)

Example 2 with SnapshotHolder

use of org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader.SnapshotHolder in project controller by opendaylight.

the class LeaderTest method testHandleInstallSnapshotReplyLastChunk.

@Test
public void testHandleInstallSnapshotReplyLastChunk() throws Exception {
    logStart("testHandleInstallSnapshotReplyLastChunk");
    MockRaftActorContext actorContext = createActorContextWithFollower();
    final int commitIndex = 3;
    final int snapshotIndex = 2;
    final int snapshotTerm = 1;
    final int currentTerm = 2;
    actorContext.setCommitIndex(commitIndex);
    leader = new Leader(actorContext);
    actorContext.setCurrentBehavior(leader);
    leader.getFollower(FOLLOWER_ID).setMatchIndex(-1);
    leader.getFollower(FOLLOWER_ID).setNextIndex(0);
    // Ignore initial heartbeat.
    MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    Map<String, String> leadersSnapshot = new HashMap<>();
    leadersSnapshot.put("1", "A");
    leadersSnapshot.put("2", "B");
    leadersSnapshot.put("3", "C");
    // set the snapshot variables in replicatedlog
    actorContext.getReplicatedLog().setSnapshotIndex(snapshotIndex);
    actorContext.getReplicatedLog().setSnapshotTerm(snapshotTerm);
    actorContext.getTermInformation().update(currentTerm, leaderActor.path().toString());
    ByteString bs = toByteString(leadersSnapshot);
    leader.setSnapshotHolder(new SnapshotHolder(Snapshot.create(ByteState.of(bs.toByteArray()), Collections.<ReplicatedLogEntry>emptyList(), commitIndex, snapshotTerm, commitIndex, snapshotTerm, -1, null, null), ByteSource.wrap(bs.toByteArray())));
    LeaderInstallSnapshotState fts = new LeaderInstallSnapshotState(actorContext.getConfigParams().getSnapshotChunkSize(), leader.logName());
    fts.setSnapshotBytes(ByteSource.wrap(bs.toByteArray()));
    leader.getFollower(FOLLOWER_ID).setLeaderInstallSnapshotState(fts);
    while (!fts.isLastChunk(fts.getChunkIndex())) {
        fts.getNextChunk();
        fts.incrementChunkIndex();
    }
    // clears leaders log
    actorContext.getReplicatedLog().removeFrom(0);
    RaftActorBehavior raftBehavior = leader.handleMessage(followerActor, new InstallSnapshotReply(currentTerm, FOLLOWER_ID, fts.getChunkIndex(), true));
    assertTrue(raftBehavior instanceof Leader);
    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());
}
Also used : FollowerLogInformation(org.opendaylight.controller.cluster.raft.FollowerLogInformation) HashMap(java.util.HashMap) InstallSnapshotReply(org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply) ByteString(com.google.protobuf.ByteString) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) ByteString(com.google.protobuf.ByteString) SnapshotHolder(org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader.SnapshotHolder) Test(org.junit.Test)

Aggregations

ByteString (com.google.protobuf.ByteString)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)2 SnapshotHolder (org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader.SnapshotHolder)2 FollowerLogInformation (org.opendaylight.controller.cluster.raft.FollowerLogInformation)1 SendInstallSnapshot (org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot)1 AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)1 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)1 InstallSnapshotReply (org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply)1