Search in sources :

Example 1 with ByteState

use of org.opendaylight.controller.cluster.raft.persisted.ByteState 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 2 with ByteState

use of org.opendaylight.controller.cluster.raft.persisted.ByteState in project controller by opendaylight.

the class SnapshotManagerTest method testPersistWhenReplicatedToAllIndexMinusOne.

@Test
public void testPersistWhenReplicatedToAllIndexMinusOne() throws Exception {
    doReturn(7L).when(mockReplicatedLog).getSnapshotIndex();
    doReturn(1L).when(mockReplicatedLog).getSnapshotTerm();
    doReturn(true).when(mockRaftActorContext).hasFollowers();
    doReturn(8L).when(mockRaftActorContext).getLastApplied();
    ReplicatedLogEntry lastLogEntry = new SimpleReplicatedLogEntry(9L, 3L, new MockRaftActorContext.MockPayload());
    ReplicatedLogEntry lastAppliedEntry = new SimpleReplicatedLogEntry(8L, 2L, new MockRaftActorContext.MockPayload());
    doReturn(lastAppliedEntry).when(mockReplicatedLog).get(8L);
    doReturn(Arrays.asList(lastLogEntry)).when(mockReplicatedLog).getFrom(9L);
    // when replicatedToAllIndex = -1
    snapshotManager.capture(lastLogEntry, -1);
    ByteState snapshotState = ByteState.of(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
    snapshotManager.persist(snapshotState, Optional.empty(), Runtime.getRuntime().totalMemory());
    ArgumentCaptor<Snapshot> snapshotArgumentCaptor = ArgumentCaptor.forClass(Snapshot.class);
    verify(mockDataPersistenceProvider).saveSnapshot(snapshotArgumentCaptor.capture());
    Snapshot snapshot = snapshotArgumentCaptor.getValue();
    assertEquals("getLastTerm", 3L, snapshot.getLastTerm());
    assertEquals("getLastIndex", 9L, snapshot.getLastIndex());
    assertEquals("getLastAppliedTerm", 2L, snapshot.getLastAppliedTerm());
    assertEquals("getLastAppliedIndex", 8L, snapshot.getLastAppliedIndex());
    assertEquals("getState", snapshotState, snapshot.getState());
    assertEquals("getUnAppliedEntries", Arrays.asList(lastLogEntry), snapshot.getUnAppliedEntries());
    assertEquals("electionTerm", mockElectionTerm.getCurrentTerm(), snapshot.getElectionTerm());
    assertEquals("electionVotedFor", mockElectionTerm.getVotedFor(), snapshot.getElectionVotedFor());
    verify(mockReplicatedLog).snapshotPreCommit(7L, 1L);
}
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) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ByteState(org.opendaylight.controller.cluster.raft.persisted.ByteState) Test(org.junit.Test)

Example 3 with ByteState

use of org.opendaylight.controller.cluster.raft.persisted.ByteState in project controller by opendaylight.

the class SnapshotManagerTest method testPersistWhenReplicatedToAllIndexNotMinus.

@Test
public void testPersistWhenReplicatedToAllIndexNotMinus() throws Exception {
    doReturn(45L).when(mockReplicatedLog).getSnapshotIndex();
    doReturn(6L).when(mockReplicatedLog).getSnapshotTerm();
    ReplicatedLogEntry replicatedLogEntry = mock(ReplicatedLogEntry.class);
    doReturn(replicatedLogEntry).when(mockReplicatedLog).get(9);
    doReturn(6L).when(replicatedLogEntry).getTerm();
    doReturn(9L).when(replicatedLogEntry).getIndex();
    // when replicatedToAllIndex != -1
    snapshotManager.capture(new SimpleReplicatedLogEntry(9, 6, new MockRaftActorContext.MockPayload()), 9);
    ByteState snapshotState = ByteState.of(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
    snapshotManager.persist(snapshotState, Optional.empty(), Runtime.getRuntime().totalMemory());
    ArgumentCaptor<Snapshot> snapshotArgumentCaptor = ArgumentCaptor.forClass(Snapshot.class);
    verify(mockDataPersistenceProvider).saveSnapshot(snapshotArgumentCaptor.capture());
    Snapshot snapshot = snapshotArgumentCaptor.getValue();
    assertEquals("getLastTerm", 6L, snapshot.getLastTerm());
    assertEquals("getLastIndex", 9L, snapshot.getLastIndex());
    assertEquals("getLastAppliedTerm", 6L, snapshot.getLastAppliedTerm());
    assertEquals("getLastAppliedIndex", 9L, snapshot.getLastAppliedIndex());
    assertEquals("getState", snapshotState, snapshot.getState());
    assertEquals("getUnAppliedEntries size", 0, snapshot.getUnAppliedEntries().size());
    verify(mockReplicatedLog).snapshotPreCommit(9L, 6L);
    verify(mockRaftActorBehavior).setReplicatedToAllIndex(9);
}
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) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ByteState(org.opendaylight.controller.cluster.raft.persisted.ByteState) Test(org.junit.Test)

Example 4 with ByteState

use of org.opendaylight.controller.cluster.raft.persisted.ByteState in project controller by opendaylight.

the class RaftActorSnapshotMessageSupportTest method testOnCaptureSnapshotReply.

@Test
public void testOnCaptureSnapshotReply() {
    ByteState state = ByteState.of(new byte[] { 1, 2, 3, 4, 5 });
    Optional<OutputStream> optionalStream = Optional.of(mock(OutputStream.class));
    sendMessageToSupport(new CaptureSnapshotReply(state, optionalStream));
    verify(mockSnapshotManager).persist(eq(state), eq(optionalStream), anyLong());
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) OutputStream(java.io.OutputStream) ByteState(org.opendaylight.controller.cluster.raft.persisted.ByteState) Test(org.junit.Test)

Example 5 with ByteState

use of org.opendaylight.controller.cluster.raft.persisted.ByteState in project controller by opendaylight.

the class ShardManagerGetSnapshotReplyActorTest method testSuccess.

@Test
public void testSuccess() {
    TestKit kit = new TestKit(getSystem());
    List<String> shardList = Arrays.asList("shard1", "shard2", "shard3");
    ShardManagerSnapshot shardManagerSnapshot = new ShardManagerSnapshot(shardList, Collections.emptyMap());
    ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(shardList, "config", shardManagerSnapshot, kit.getRef(), "shard-manager", Duration.create(100, TimeUnit.SECONDS)), "testSuccess");
    kit.watch(replyActor);
    ByteState shard1SnapshotState = ByteState.of(new byte[] { 1, 2, 3 });
    replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(), Snapshot.create(shard1SnapshotState, Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
    ByteState shard2SnapshotState = ByteState.of(new byte[] { 4, 5, 6 });
    replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard2", MEMBER_1, "config").toString(), Snapshot.create(shard2SnapshotState, Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
    kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
    ByteState shard3SnapshotState = ByteState.of(new byte[] { 7, 8, 9 });
    replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard3", MEMBER_1, "config").toString(), Snapshot.create(shard3SnapshotState, Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
    DatastoreSnapshot datastoreSnapshot = kit.expectMsgClass(DatastoreSnapshot.class);
    assertEquals("getType", "config", datastoreSnapshot.getType());
    assertEquals("getShardManagerSnapshot", shardManagerSnapshot.getShardList(), datastoreSnapshot.getShardManagerSnapshot().getShardList());
    List<ShardSnapshot> shardSnapshots = datastoreSnapshot.getShardSnapshots();
    assertEquals("ShardSnapshot size", 3, shardSnapshots.size());
    assertEquals("ShardSnapshot 1 getName", "shard1", shardSnapshots.get(0).getName());
    assertEquals("ShardSnapshot 1 getSnapshot", shard1SnapshotState, shardSnapshots.get(0).getSnapshot().getState());
    assertEquals("ShardSnapshot 2 getName", "shard2", shardSnapshots.get(1).getName());
    assertEquals("ShardSnapshot 2 getSnapshot", shard2SnapshotState, shardSnapshots.get(1).getSnapshot().getState());
    assertEquals("ShardSnapshot 3 getName", "shard3", shardSnapshots.get(2).getName());
    assertEquals("ShardSnapshot 3 getSnapshot", shard3SnapshotState, shardSnapshots.get(2).getSnapshot().getState());
    kit.expectMsgClass(Terminated.class);
}
Also used : ShardSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot) ShardManagerSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot) ActorRef(akka.actor.ActorRef) TestKit(akka.testkit.javadsl.TestKit) DatastoreSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot) ByteState(org.opendaylight.controller.cluster.raft.persisted.ByteState) GetSnapshotReply(org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 ByteState (org.opendaylight.controller.cluster.raft.persisted.ByteState)5 CaptureSnapshot (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot)3 SendInstallSnapshot (org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot)3 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)3 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)3 ActorRef (akka.actor.ActorRef)2 OutputStream (java.io.OutputStream)2 TestKit (akka.testkit.javadsl.TestKit)1 Optional (java.util.Optional)1 AbstractActorTest (org.opendaylight.controller.cluster.datastore.AbstractActorTest)1 DatastoreSnapshot (org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot)1 ShardSnapshot (org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot)1 ShardManagerSnapshot (org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot)1 CaptureSnapshotReply (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply)1 GetSnapshotReply (org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply)1