Search in sources :

Example 11 with CaptureSnapshotReply

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

the class RaftActorTest method testRealSnapshotWhenReplicatedToAllIndexNotInReplicatedLog.

@Test
public void testRealSnapshotWhenReplicatedToAllIndexNotInReplicatedLog() 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.getReplicatedLog().setSnapshotIndex(3);
    leaderActor.waitForInitializeBehaviorComplete();
    Leader leader = new Leader(leaderActor.getRaftActorContext());
    leaderActor.setCurrentBehavior(leader);
    leader.setReplicatedToAllIndex(3);
    assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
    // Persist another entry (this will cause a CaptureSnapshot to be triggered
    leaderActor.persistData(mockActorRef, new MockIdentifier("x"), new MockRaftActorContext.MockPayload("duh"), false);
    // 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(3, leaderActor.getReplicatedLog().getSnapshotIndex());
    assertTrue(leaderActor.getRaftActorContext().getSnapshotManager().isCapturing());
    assertEquals(3, leader.getReplicatedToAllIndex());
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) Leader(org.opendaylight.controller.cluster.raft.behaviors.Leader) DataPersistenceProvider(org.opendaylight.controller.cluster.DataPersistenceProvider) FiniteDuration(scala.concurrent.duration.FiniteDuration) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 12 with CaptureSnapshotReply

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

the class MigratedMessagesTest method doTestSnapshotAfterStartupWithMigratedMessage.

@SuppressWarnings("checkstyle:IllegalCatch")
private TestActorRef<MockRaftActor> doTestSnapshotAfterStartupWithMigratedMessage(String id, boolean persistent, Consumer<Snapshot> snapshotVerifier, final State snapshotState) {
    InMemorySnapshotStore.addSnapshotSavedLatch(id);
    InMemoryJournal.addDeleteMessagesCompleteLatch(id);
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
    RaftActorSnapshotCohort snapshotCohort = new RaftActorSnapshotCohort() {

        @Override
        public void createSnapshot(ActorRef actorRef, java.util.Optional<OutputStream> installSnapshotStream) {
            actorRef.tell(new CaptureSnapshotReply(snapshotState, installSnapshotStream), actorRef);
        }

        @Override
        public void applySnapshot(State newState) {
        }

        @Override
        public State deserializeSnapshot(ByteSource snapshotBytes) {
            throw new UnsupportedOperationException();
        }
    };
    TestActorRef<MockRaftActor> raftActorRef = factory.createTestActor(MockRaftActor.builder().id(id).config(config).snapshotCohort(snapshotCohort).persistent(Optional.of(persistent)).peerAddresses(ImmutableMap.of("peer", "")).props().withDispatcher(Dispatchers.DefaultDispatcherId()), id);
    MockRaftActor mockRaftActor = raftActorRef.underlyingActor();
    mockRaftActor.waitForRecoveryComplete();
    Snapshot snapshot = InMemorySnapshotStore.waitForSavedSnapshot(id, Snapshot.class);
    snapshotVerifier.accept(snapshot);
    InMemoryJournal.waitForDeleteMessagesComplete(id);
    assertEquals("InMemoryJournal size", 0, InMemoryJournal.get(id).size());
    return raftActorRef;
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) Optional(com.google.common.base.Optional) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) ByteState(org.opendaylight.controller.cluster.raft.persisted.ByteState) State(org.opendaylight.controller.cluster.raft.persisted.Snapshot.State) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) ByteSource(com.google.common.io.ByteSource)

Example 13 with CaptureSnapshotReply

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

the class ShardSnapshotActor method onSerializeSnapshot.

private void onSerializeSnapshot(final SerializeSnapshot request) {
    Optional<OutputStream> installSnapshotStream = request.getInstallSnapshotStream();
    if (installSnapshotStream.isPresent()) {
        try (ObjectOutputStream out = new ObjectOutputStream(installSnapshotStream.get())) {
            request.getSnapshot().serialize(out);
        } catch (IOException e) {
            // TODO - we should communicate the failure in the CaptureSnapshotReply.
            LOG.error("Error serializing snapshot", e);
        }
    }
    request.getReplyTo().tell(new CaptureSnapshotReply(new ShardSnapshotState(request.getSnapshot()), installSnapshotStream), ActorRef.noSender());
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) ShardSnapshotState(org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState) OutputStream(java.io.OutputStream) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 14 with CaptureSnapshotReply

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

the class GetSnapshotReplyActor method onReceive.

@Override
public void onReceive(final Object message) {
    if (message instanceof CaptureSnapshotReply) {
        Snapshot snapshot = Snapshot.create(((CaptureSnapshotReply) message).getSnapshotState(), params.captureSnapshot.getUnAppliedEntries(), params.captureSnapshot.getLastIndex(), params.captureSnapshot.getLastTerm(), params.captureSnapshot.getLastAppliedIndex(), params.captureSnapshot.getLastAppliedTerm(), params.electionTerm.getCurrentTerm(), params.electionTerm.getVotedFor(), params.peerInformation);
        LOG.debug("{}: Received CaptureSnapshotReply, sending {}", params.id, snapshot);
        params.replyToActor.tell(new GetSnapshotReply(params.id, snapshot), getSelf());
        getSelf().tell(PoisonPill.getInstance(), getSelf());
    } else if (message instanceof ReceiveTimeout) {
        LOG.warn("{}: Got ReceiveTimeout for inactivity - did not receive CaptureSnapshotReply within {} ms", params.id, params.receiveTimeout.toMillis());
        params.replyToActor.tell(new akka.actor.Status.Failure(new TimeoutException(String.format("Timed out after %d ms while waiting for CaptureSnapshotReply", params.receiveTimeout.toMillis()))), getSelf());
        getSelf().tell(PoisonPill.getInstance(), getSelf());
    }
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) CaptureSnapshot(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) ReceiveTimeout(akka.actor.ReceiveTimeout) GetSnapshotReply(org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply) TimeoutException(java.util.concurrent.TimeoutException)

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