Search in sources :

Example 6 with CaptureSnapshot

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

the class SnapshotManagerTest method testCapture.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testCapture() throws Exception {
    boolean capture = snapshotManager.capture(new SimpleReplicatedLogEntry(9, 1, new MockRaftActorContext.MockPayload()), 9);
    assertTrue(capture);
    assertEquals(true, snapshotManager.isCapturing());
    ArgumentCaptor<Optional> outputStream = ArgumentCaptor.forClass(Optional.class);
    verify(mockProcedure).accept(outputStream.capture());
    assertEquals("isPresent", false, outputStream.getValue().isPresent());
    CaptureSnapshot captureSnapshot = snapshotManager.getCaptureSnapshot();
    // LastIndex and LastTerm are picked up from the lastLogEntry
    assertEquals(9L, captureSnapshot.getLastIndex());
    assertEquals(1L, captureSnapshot.getLastTerm());
    // Since the actor does not have any followers (no peer addresses) lastApplied will be from lastLogEntry
    assertEquals(9L, captureSnapshot.getLastAppliedIndex());
    assertEquals(1L, captureSnapshot.getLastAppliedTerm());
    // 
    assertEquals(-1L, captureSnapshot.getReplicatedToAllIndex());
    assertEquals(-1L, captureSnapshot.getReplicatedToAllTerm());
    MessageCollectorActor.clearMessages(actorRef);
}
Also used : Optional(java.util.Optional) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) CaptureSnapshot(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot) Test(org.junit.Test)

Example 7 with CaptureSnapshot

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

the class SnapshotManagerTest method testCaptureToInstall.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testCaptureToInstall() throws Exception {
    // Force capturing toInstall = true
    snapshotManager.captureToInstall(new SimpleReplicatedLogEntry(0, 1, new MockRaftActorContext.MockPayload()), 0, "follower-1");
    assertEquals(true, snapshotManager.isCapturing());
    ArgumentCaptor<Optional> outputStream = ArgumentCaptor.forClass(Optional.class);
    verify(mockProcedure).accept(outputStream.capture());
    assertEquals("isPresent", true, outputStream.getValue().isPresent());
    CaptureSnapshot captureSnapshot = snapshotManager.getCaptureSnapshot();
    // LastIndex and LastTerm are picked up from the lastLogEntry
    assertEquals(0L, captureSnapshot.getLastIndex());
    assertEquals(1L, captureSnapshot.getLastTerm());
    // Since the actor does not have any followers (no peer addresses) lastApplied will be from lastLogEntry
    assertEquals(0L, captureSnapshot.getLastAppliedIndex());
    assertEquals(1L, captureSnapshot.getLastAppliedTerm());
    // 
    assertEquals(-1L, captureSnapshot.getReplicatedToAllIndex());
    assertEquals(-1L, captureSnapshot.getReplicatedToAllTerm());
    MessageCollectorActor.clearMessages(actorRef);
}
Also used : Optional(java.util.Optional) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) CaptureSnapshot(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot) Test(org.junit.Test)

Example 8 with CaptureSnapshot

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

the class LeaderTest method testInitiateInstallSnapshot.

@Test
public void testInitiateInstallSnapshot() throws Exception {
    logStart("testInitiateInstallSnapshot");
    MockRaftActorContext actorContext = createActorContextWithFollower();
    // clears leaders log
    actorContext.getReplicatedLog().removeFrom(0);
    final int followersLastIndex = 2;
    final int snapshotIndex = 3;
    final int newEntryIndex = 4;
    final int snapshotTerm = 1;
    final int currentTerm = 2;
    // set the snapshot variables in replicatedlog
    actorContext.getReplicatedLog().setSnapshotIndex(snapshotIndex);
    actorContext.getReplicatedLog().setSnapshotTerm(snapshotTerm);
    actorContext.setLastApplied(3);
    actorContext.setCommitIndex(followersLastIndex);
    leader = new Leader(actorContext);
    // Leader will send an immediate heartbeat - ignore it.
    MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    // set the snapshot as absent and check if capture-snapshot is invoked.
    leader.setSnapshotHolder(null);
    // new entry
    SimpleReplicatedLogEntry entry = new SimpleReplicatedLogEntry(newEntryIndex, currentTerm, new MockRaftActorContext.MockPayload("D"));
    actorContext.getReplicatedLog().append(entry);
    // update follower timestamp
    leader.markFollowerActive(FOLLOWER_ID);
    leader.handleMessage(leaderActor, new Replicate(null, new MockIdentifier("state-id"), entry, true));
    assertEquals("isCapturing", true, actorContext.getSnapshotManager().isCapturing());
    CaptureSnapshot cs = actorContext.getSnapshotManager().getCaptureSnapshot();
    assertEquals(3, cs.getLastAppliedIndex());
    assertEquals(1, cs.getLastAppliedTerm());
    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, new MockIdentifier("state-id"), entry, true));
    assertSame("CaptureSnapshot instance", cs, actorContext.getSnapshotManager().getCaptureSnapshot());
}
Also used : Replicate(org.opendaylight.controller.cluster.raft.base.messages.Replicate) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) CaptureSnapshot(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot) Test(org.junit.Test)

Aggregations

CaptureSnapshot (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot)8 Test (org.junit.Test)6 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)5 Optional (java.util.Optional)3 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)2 MockPayload (org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload)2 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)2 ApplyState (org.opendaylight.controller.cluster.raft.base.messages.ApplyState)2 Replicate (org.opendaylight.controller.cluster.raft.base.messages.Replicate)2 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)2 ActorRef (akka.actor.ActorRef)1 Optional (com.google.common.base.Optional)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 GetSnapshot (org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot)1 GetSnapshotReply (org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply)1 AppendEntriesReply (org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply)1 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)1