Search in sources :

Example 1 with CaptureSnapshotReply

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

the class ShardSnapshotActorTest method testSerializeSnapshot.

private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot, final boolean withInstallSnapshot) throws Exception {
    new TestKit(getSystem()) {

        {
            final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(), testName);
            watch(snapshotActor);
            final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
            ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
            ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot, Optional.ofNullable(installSnapshotStream), getRef());
            final CaptureSnapshotReply reply = expectMsgClass(duration("3 seconds"), CaptureSnapshotReply.class);
            assertNotNull("getSnapshotState is null", reply.getSnapshotState());
            assertEquals("SnapshotState type", ShardSnapshotState.class, reply.getSnapshotState().getClass());
            assertEquals("Snapshot", snapshot, ((ShardSnapshotState) reply.getSnapshotState()).getSnapshot());
            if (installSnapshotStream != null) {
                final ShardDataTreeSnapshot deserialized;
                try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(installSnapshotStream.toByteArray()))) {
                    deserialized = ShardDataTreeSnapshot.deserialize(in);
                }
                assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
                final Optional<NormalizedNode<?, ?>> maybeNode = deserialized.getRootNode();
                assertEquals("isPresent", true, maybeNode.isPresent());
                assertEquals("Root node", expectedRoot, maybeNode.get());
            }
        }
    };
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) ShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot) MetadataShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot) ByteArrayInputStream(java.io.ByteArrayInputStream) ActorRef(akka.actor.ActorRef) TestKit(akka.testkit.javadsl.TestKit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with CaptureSnapshotReply

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

the class RaftActorTest method testRaftActorForwardsToRaftActorSnapshotMessageSupport.

@Test
public void testRaftActorForwardsToRaftActorSnapshotMessageSupport() {
    String persistenceId = factory.generateActorId("leader-");
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
    RaftActorSnapshotMessageSupport mockSupport = mock(RaftActorSnapshotMessageSupport.class);
    TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.builder().id(persistenceId).config(config).snapshotMessageSupport(mockSupport).props());
    MockRaftActor mockRaftActor = mockActorRef.underlyingActor();
    // Wait for akka's recovery to complete so it doesn't interfere.
    mockRaftActor.waitForRecoveryComplete();
    ApplySnapshot applySnapshot = new ApplySnapshot(mock(Snapshot.class));
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(applySnapshot), any(ActorRef.class));
    mockRaftActor.handleCommand(applySnapshot);
    CaptureSnapshotReply captureSnapshotReply = new CaptureSnapshotReply(ByteState.empty(), java.util.Optional.empty());
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(captureSnapshotReply), any(ActorRef.class));
    mockRaftActor.handleCommand(captureSnapshotReply);
    SaveSnapshotSuccess saveSnapshotSuccess = new SaveSnapshotSuccess(new SnapshotMetadata("", 0L, 0L));
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(saveSnapshotSuccess), any(ActorRef.class));
    mockRaftActor.handleCommand(saveSnapshotSuccess);
    SaveSnapshotFailure saveSnapshotFailure = new SaveSnapshotFailure(new SnapshotMetadata("", 0L, 0L), new Throwable());
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(saveSnapshotFailure), any(ActorRef.class));
    mockRaftActor.handleCommand(saveSnapshotFailure);
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT), any(ActorRef.class));
    mockRaftActor.handleCommand(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT);
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(GetSnapshot.INSTANCE), any(ActorRef.class));
    mockRaftActor.handleCommand(GetSnapshot.INSTANCE);
    verify(mockSupport).handleSnapshotMessage(same(applySnapshot), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(captureSnapshotReply), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(saveSnapshotSuccess), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(saveSnapshotFailure), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(GetSnapshot.INSTANCE), any(ActorRef.class));
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) ApplySnapshot(org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) FiniteDuration(scala.concurrent.duration.FiniteDuration) ByteString(com.google.protobuf.ByteString) SaveSnapshotFailure(akka.persistence.SaveSnapshotFailure) ApplySnapshot(org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) GetSnapshot(org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot) SnapshotMetadata(akka.persistence.SnapshotMetadata) SaveSnapshotSuccess(akka.persistence.SaveSnapshotSuccess) Test(org.junit.Test)

Example 3 with CaptureSnapshotReply

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

the class RaftActorTest method testRealSnapshotWhenReplicatedToAllIndexMinusOne.

@Test
public void testRealSnapshotWhenReplicatedToAllIndexMinusOne() throws Exception {
    String persistenceId = factory.generateActorId("leader-");
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
    config.setIsolatedLeaderCheckInterval(new FiniteDuration(1, TimeUnit.DAYS));
    config.setSnapshotBatchCount(5);
    DataPersistenceProvider dataPersistenceProvider = createProvider();
    Map<String, String> peerAddresses = ImmutableMap.<String, String>builder().put("member1", "address").build();
    TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.props(persistenceId, peerAddresses, config, dataPersistenceProvider), persistenceId);
    MockRaftActor leaderActor = mockActorRef.underlyingActor();
    leaderActor.getRaftActorContext().setCommitIndex(3);
    leaderActor.getRaftActorContext().setLastApplied(3);
    leaderActor.getRaftActorContext().getTermInformation().update(1, persistenceId);
    leaderActor.waitForInitializeBehaviorComplete();
    for (int i = 0; i < 4; i++) {
        leaderActor.getReplicatedLog().append(new SimpleReplicatedLogEntry(i, 1, new MockRaftActorContext.MockPayload("A")));
    }
    Leader leader = new Leader(leaderActor.getRaftActorContext());
    leaderActor.setCurrentBehavior(leader);
    assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
    // Simulate an install snaphost to a follower.
    leaderActor.getRaftActorContext().getSnapshotManager().captureToInstall(leaderActor.getReplicatedLog().last(), -1, "member1");
    // Now send a CaptureSnapshotReply
    mockActorRef.tell(new CaptureSnapshotReply(ByteState.of(fromObject("foo").toByteArray()), java.util.Optional.empty()), mockActorRef);
    // Trimming log in this scenario is a no-op
    assertEquals(-1, leaderActor.getReplicatedLog().getSnapshotIndex());
    assertTrue(leaderActor.getRaftActorContext().getSnapshotManager().isCapturing());
    assertEquals(-1, leader.getReplicatedToAllIndex());
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) Leader(org.opendaylight.controller.cluster.raft.behaviors.Leader) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) DataPersistenceProvider(org.opendaylight.controller.cluster.DataPersistenceProvider) FiniteDuration(scala.concurrent.duration.FiniteDuration) ByteString(com.google.protobuf.ByteString) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) Test(org.junit.Test)

Example 4 with CaptureSnapshotReply

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

the class RaftActorTest method testGetSnapshot.

@Test
public void testGetSnapshot() throws Exception {
    TEST_LOG.info("testGetSnapshot starting");
    final TestKit kit = new TestKit(getSystem());
    String persistenceId = factory.generateActorId("test-actor-");
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
    long term = 3;
    long seqN = 1;
    InMemoryJournal.addEntry(persistenceId, seqN++, new UpdateElectionTerm(term, "member-1"));
    InMemoryJournal.addEntry(persistenceId, seqN++, new SimpleReplicatedLogEntry(0, term, new MockRaftActorContext.MockPayload("A")));
    InMemoryJournal.addEntry(persistenceId, seqN++, new SimpleReplicatedLogEntry(1, term, new MockRaftActorContext.MockPayload("B")));
    InMemoryJournal.addEntry(persistenceId, seqN++, new ApplyJournalEntries(1));
    InMemoryJournal.addEntry(persistenceId, seqN++, new SimpleReplicatedLogEntry(2, term, new MockRaftActorContext.MockPayload("C")));
    TestActorRef<MockRaftActor> raftActorRef = factory.createTestActor(MockRaftActor.props(persistenceId, ImmutableMap.<String, String>builder().put("member1", "address").build(), config).withDispatcher(Dispatchers.DefaultDispatcherId()), persistenceId);
    MockRaftActor mockRaftActor = raftActorRef.underlyingActor();
    mockRaftActor.waitForRecoveryComplete();
    mockRaftActor.snapshotCohortDelegate = mock(RaftActorSnapshotCohort.class);
    raftActorRef.tell(GetSnapshot.INSTANCE, kit.getRef());
    ArgumentCaptor<ActorRef> replyActor = ArgumentCaptor.forClass(ActorRef.class);
    verify(mockRaftActor.snapshotCohortDelegate, timeout(5000)).createSnapshot(replyActor.capture(), eq(java.util.Optional.empty()));
    byte[] stateSnapshot = new byte[] { 1, 2, 3 };
    replyActor.getValue().tell(new CaptureSnapshotReply(ByteState.of(stateSnapshot), java.util.Optional.empty()), ActorRef.noSender());
    GetSnapshotReply reply = kit.expectMsgClass(GetSnapshotReply.class);
    assertEquals("getId", persistenceId, reply.getId());
    Snapshot replySnapshot = reply.getSnapshot();
    assertEquals("getElectionTerm", term, replySnapshot.getElectionTerm());
    assertEquals("getElectionVotedFor", "member-1", replySnapshot.getElectionVotedFor());
    assertEquals("getLastAppliedIndex", 1L, replySnapshot.getLastAppliedIndex());
    assertEquals("getLastAppliedTerm", term, replySnapshot.getLastAppliedTerm());
    assertEquals("getLastIndex", 2L, replySnapshot.getLastIndex());
    assertEquals("getLastTerm", term, replySnapshot.getLastTerm());
    assertEquals("getState", ByteState.of(stateSnapshot), replySnapshot.getState());
    assertEquals("getUnAppliedEntries size", 1, replySnapshot.getUnAppliedEntries().size());
    assertEquals("UnApplied entry index ", 2L, replySnapshot.getUnAppliedEntries().get(0).getIndex());
    // Test with timeout
    mockRaftActor.getSnapshotMessageSupport().setSnapshotReplyActorTimeout(Duration.create(200, TimeUnit.MILLISECONDS));
    reset(mockRaftActor.snapshotCohortDelegate);
    raftActorRef.tell(GetSnapshot.INSTANCE, kit.getRef());
    Failure failure = kit.expectMsgClass(akka.actor.Status.Failure.class);
    assertEquals("Failure cause type", TimeoutException.class, failure.cause().getClass());
    mockRaftActor.getSnapshotMessageSupport().setSnapshotReplyActorTimeout(Duration.create(30, TimeUnit.SECONDS));
    // Test with persistence disabled.
    mockRaftActor.setPersistence(false);
    reset(mockRaftActor.snapshotCohortDelegate);
    raftActorRef.tell(GetSnapshot.INSTANCE, kit.getRef());
    reply = kit.expectMsgClass(GetSnapshotReply.class);
    verify(mockRaftActor.snapshotCohortDelegate, never()).createSnapshot(anyObject(), anyObject());
    assertEquals("getId", persistenceId, reply.getId());
    replySnapshot = reply.getSnapshot();
    assertEquals("getElectionTerm", term, replySnapshot.getElectionTerm());
    assertEquals("getElectionVotedFor", "member-1", replySnapshot.getElectionVotedFor());
    assertEquals("getLastAppliedIndex", -1L, replySnapshot.getLastAppliedIndex());
    assertEquals("getLastAppliedTerm", -1L, replySnapshot.getLastAppliedTerm());
    assertEquals("getLastIndex", -1L, replySnapshot.getLastIndex());
    assertEquals("getLastTerm", -1L, replySnapshot.getLastTerm());
    assertEquals("getState type", EmptyState.INSTANCE, replySnapshot.getState());
    assertEquals("getUnAppliedEntries size", 0, replySnapshot.getUnAppliedEntries().size());
    TEST_LOG.info("testGetSnapshot ending");
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) TestKit(akka.testkit.javadsl.TestKit) ByteString(com.google.protobuf.ByteString) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) ApplySnapshot(org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) GetSnapshot(org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) ApplyJournalEntries(org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries) UpdateElectionTerm(org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm) SaveSnapshotFailure(akka.persistence.SaveSnapshotFailure) Failure(akka.actor.Status.Failure) GetSnapshotReply(org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply) Test(org.junit.Test)

Example 5 with CaptureSnapshotReply

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

the class RecoveryIntegrationTest method testStatePersistedBetweenSnapshotCaptureAndPersist.

@Test
public void testStatePersistedBetweenSnapshotCaptureAndPersist() {
    send2InitialPayloads();
    // Block these messages initially so we can control the sequence.
    leaderActor.underlyingActor().startDropMessages(CaptureSnapshotReply.class);
    follower1Actor.underlyingActor().startDropMessages(AppendEntries.class);
    final MockPayload payload2 = sendPayloadData(leaderActor, "two");
    // This should trigger a snapshot.
    final MockPayload payload3 = sendPayloadData(leaderActor, "three");
    MessageCollectorActor.expectMatching(follower1CollectorActor, AppendEntries.class, 3);
    // Send another payload.
    final MockPayload payload4 = sendPayloadData(leaderActor, "four");
    // Now deliver the AppendEntries to the follower
    follower1Actor.underlyingActor().stopDropMessages(AppendEntries.class);
    MessageCollectorActor.expectMatching(leaderCollectorActor, ApplyJournalEntries.class, 1);
    // Now deliver the CaptureSnapshotReply to the leader.
    CaptureSnapshotReply captureSnapshotReply = MessageCollectorActor.expectFirstMatching(leaderCollectorActor, CaptureSnapshotReply.class);
    leaderActor.underlyingActor().stopDropMessages(CaptureSnapshotReply.class);
    leaderActor.tell(captureSnapshotReply, leaderActor);
    // Wait for snapshot complete.
    MessageCollectorActor.expectFirstMatching(leaderCollectorActor, SaveSnapshotSuccess.class);
    reinstateLeaderActor();
    assertEquals("Leader snapshot term", currentTerm, leaderContext.getReplicatedLog().getSnapshotTerm());
    assertEquals("Leader snapshot index", 1, leaderContext.getReplicatedLog().getSnapshotIndex());
    assertEquals("Leader journal log size", 3, leaderContext.getReplicatedLog().size());
    assertEquals("Leader journal last index", 4, leaderContext.getReplicatedLog().lastIndex());
    assertEquals("Leader commit index", 4, leaderContext.getCommitIndex());
    assertEquals("Leader last applied", 4, leaderContext.getLastApplied());
    assertEquals("Leader state", Arrays.asList(payload0, payload1, payload2, payload3, payload4), leaderActor.underlyingActor().getState());
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) Test(org.junit.Test)

Aggregations

CaptureSnapshotReply (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply)14 Test (org.junit.Test)9 ActorRef (akka.actor.ActorRef)7 TestActorRef (akka.testkit.TestActorRef)6 ByteString (com.google.protobuf.ByteString)6 MockPayload (org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload)6 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)6 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)5 FiniteDuration (scala.concurrent.duration.FiniteDuration)5 DataPersistenceProvider (org.opendaylight.controller.cluster.DataPersistenceProvider)4 Leader (org.opendaylight.controller.cluster.raft.behaviors.Leader)3 ByteState (org.opendaylight.controller.cluster.raft.persisted.ByteState)3 DisableElectionsRaftPolicy (org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy)3 SaveSnapshotFailure (akka.persistence.SaveSnapshotFailure)2 TestKit (akka.testkit.javadsl.TestKit)2 Optional (com.google.common.base.Optional)2 ByteSource (com.google.common.io.ByteSource)2 OutputStream (java.io.OutputStream)2 HashMap (java.util.HashMap)2 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)2