Search in sources :

Example 6 with SendInstallSnapshot

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

the class SnapshotManagerTest method testPersistSendInstallSnapshot.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testPersistSendInstallSnapshot() throws Exception {
    doReturn(Integer.MAX_VALUE).when(mockReplicatedLog).dataSize();
    doNothing().when(mockProcedure).accept(anyObject());
    // when replicatedToAllIndex = -1
    boolean capture = snapshotManager.captureToInstall(new SimpleReplicatedLogEntry(9, 6, new MockRaftActorContext.MockPayload()), -1, "follower-1");
    assertTrue(capture);
    ByteState snapshotState = ByteState.of(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
    ArgumentCaptor<Optional> installSnapshotStreamCapture = ArgumentCaptor.forClass(Optional.class);
    verify(mockProcedure).accept(installSnapshotStreamCapture.capture());
    Optional<OutputStream> installSnapshotStream = installSnapshotStreamCapture.getValue();
    assertEquals("isPresent", true, installSnapshotStream.isPresent());
    installSnapshotStream.get().write(snapshotState.getBytes());
    snapshotManager.persist(snapshotState, installSnapshotStream, Runtime.getRuntime().totalMemory());
    assertEquals(true, snapshotManager.isCapturing());
    verify(mockDataPersistenceProvider).saveSnapshot(any(Snapshot.class));
    verify(mockReplicatedLog).snapshotPreCommit(9L, 6L);
    ArgumentCaptor<SendInstallSnapshot> sendInstallSnapshotArgumentCaptor = ArgumentCaptor.forClass(SendInstallSnapshot.class);
    verify(mockRaftActorBehavior).handleMessage(any(ActorRef.class), sendInstallSnapshotArgumentCaptor.capture());
    SendInstallSnapshot sendInstallSnapshot = sendInstallSnapshotArgumentCaptor.getValue();
    assertEquals("state", snapshotState, sendInstallSnapshot.getSnapshot().getState());
    assertArrayEquals("state", snapshotState.getBytes(), sendInstallSnapshot.getSnapshotBytes().read());
}
Also used : CaptureSnapshot(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) Optional(java.util.Optional) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ActorRef(akka.actor.ActorRef) OutputStream(java.io.OutputStream) ByteState(org.opendaylight.controller.cluster.raft.persisted.ByteState) Test(org.junit.Test)

Example 7 with SendInstallSnapshot

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

the class LeaderTest method testHandleSnapshotSendsPreviousChunksHashCodeWhenSendingNextChunk.

@Test
public void testHandleSnapshotSendsPreviousChunksHashCodeWhenSendingNextChunk() throws Exception {
    logStart("testHandleSnapshotSendsPreviousChunksHashCodeWhenSendingNextChunk");
    MockRaftActorContext actorContext = createActorContextWithFollower();
    final int commitIndex = 3;
    final int snapshotIndex = 2;
    final int snapshotTerm = 1;
    final int currentTerm = 2;
    actorContext.setConfigParams(new DefaultConfigParamsImpl() {

        @Override
        public int getSnapshotChunkSize() {
            return 50;
        }
    });
    actorContext.setCommitIndex(commitIndex);
    leader = new Leader(actorContext);
    leader.getFollower(FOLLOWER_ID).setMatchIndex(-1);
    leader.getFollower(FOLLOWER_ID).setNextIndex(0);
    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);
    Snapshot snapshot = Snapshot.create(ByteState.of(bs.toByteArray()), Collections.<ReplicatedLogEntry>emptyList(), commitIndex, snapshotTerm, commitIndex, snapshotTerm, -1, null, null);
    leader.handleMessage(leaderActor, new SendInstallSnapshot(snapshot, ByteSource.wrap(bs.toByteArray())));
    InstallSnapshot installSnapshot = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
    assertEquals(1, installSnapshot.getChunkIndex());
    assertEquals(3, installSnapshot.getTotalChunks());
    assertEquals(LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE, installSnapshot.getLastChunkHashCode().get().intValue());
    final int hashCode = Arrays.hashCode(installSnapshot.getData());
    followerActor.underlyingActor().clear();
    leader.handleMessage(followerActor, new InstallSnapshotReply(installSnapshot.getTerm(), FOLLOWER_ID, 1, true));
    installSnapshot = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
    assertEquals(2, installSnapshot.getChunkIndex());
    assertEquals(3, installSnapshot.getTotalChunks());
    assertEquals(hashCode, installSnapshot.getLastChunkHashCode().get().intValue());
}
Also used : SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) HashMap(java.util.HashMap) InstallSnapshotReply(org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply) ByteString(com.google.protobuf.ByteString) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) ByteString(com.google.protobuf.ByteString) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) CaptureSnapshot(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) Test(org.junit.Test)

Aggregations

SendInstallSnapshot (org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot)7 Test (org.junit.Test)6 CaptureSnapshot (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot)6 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)6 ByteString (com.google.protobuf.ByteString)5 HashMap (java.util.HashMap)5 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)5 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)5 InstallSnapshotReply (org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply)4 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)3 ActorRef (akka.actor.ActorRef)1 OutputStream (java.io.OutputStream)1 Optional (java.util.Optional)1 CheckConsensusReached (org.opendaylight.controller.cluster.raft.base.messages.CheckConsensusReached)1 Replicate (org.opendaylight.controller.cluster.raft.base.messages.Replicate)1 SendHeartBeat (org.opendaylight.controller.cluster.raft.base.messages.SendHeartBeat)1 RaftRPC (org.opendaylight.controller.cluster.raft.messages.RaftRPC)1 RequestVote (org.opendaylight.controller.cluster.raft.messages.RequestVote)1 ByteState (org.opendaylight.controller.cluster.raft.persisted.ByteState)1 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)1