X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FRaftActorSnapshotMessageSupport.java;h=3b4c08c405bf02dace5565602d663166e4643e3f;hp=cab76e90ce69756f084668bd3f695c12a9a71012;hb=refs%2Fchanges%2F09%2F83009%2F6;hpb=228af4aa1ef1a802fd24e7e010f3bba959ee03dd diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorSnapshotMessageSupport.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorSnapshotMessageSupport.java index cab76e90ce..3b4c08c405 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorSnapshotMessageSupport.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorSnapshotMessageSupport.java @@ -7,13 +7,22 @@ */ package org.opendaylight.controller.cluster.raft; -import akka.japi.Procedure; +import akka.actor.ActorRef; import akka.persistence.SaveSnapshotFailure; import akka.persistence.SaveSnapshotSuccess; +import com.google.common.annotations.VisibleForTesting; +import java.util.Collections; +import java.util.Optional; +import java.util.concurrent.TimeUnit; import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot; +import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot; import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply; -import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior; +import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot; +import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply; +import org.opendaylight.controller.cluster.raft.persisted.EmptyState; +import org.opendaylight.controller.cluster.raft.persisted.Snapshot; import org.slf4j.Logger; +import scala.concurrent.duration.FiniteDuration; /** * Handles snapshot related messages for a RaftActor. @@ -21,55 +30,58 @@ import org.slf4j.Logger; * @author Thomas Pantelis */ class RaftActorSnapshotMessageSupport { - static final String COMMIT_SNAPSHOT = "commit_snapshot"; + static final Object COMMIT_SNAPSHOT = new Object() { + @Override + public String toString() { + return "commit_snapshot"; + } + }; private final RaftActorContext context; - private final RaftActorBehavior currentBehavior; private final RaftActorSnapshotCohort cohort; private final Logger log; - private final Procedure createSnapshotProcedure = new Procedure() { - @Override - public void apply(Void notUsed) throws Exception { - cohort.createSnapshot(context.getActor()); - } - }; + private FiniteDuration snapshotReplyActorTimeout = FiniteDuration.create(30, TimeUnit.SECONDS); - RaftActorSnapshotMessageSupport(RaftActorContext context, RaftActorBehavior currentBehavior, - RaftActorSnapshotCohort cohort) { + RaftActorSnapshotMessageSupport(final RaftActorContext context, final RaftActorSnapshotCohort cohort) { this.context = context; - this.currentBehavior = currentBehavior; this.cohort = cohort; this.log = context.getLogger(); - context.getSnapshotManager().setCreateSnapshotCallable(createSnapshotProcedure); + context.getSnapshotManager().setCreateSnapshotConsumer( + outputStream -> cohort.createSnapshot(context.getActor(), outputStream)); + context.getSnapshotManager().setSnapshotCohort(cohort); } - boolean handleSnapshotMessage(Object message) { - if(message instanceof ApplySnapshot ) { - onApplySnapshot(((ApplySnapshot) message).getSnapshot()); - return true; + RaftActorSnapshotCohort getSnapshotCohort() { + return cohort; + } + + boolean handleSnapshotMessage(Object message, ActorRef sender) { + if (message instanceof ApplySnapshot) { + onApplySnapshot((ApplySnapshot) message); } else if (message instanceof SaveSnapshotSuccess) { onSaveSnapshotSuccess((SaveSnapshotSuccess) message); - return true; } else if (message instanceof SaveSnapshotFailure) { onSaveSnapshotFailure((SaveSnapshotFailure) message); - return true; } else if (message instanceof CaptureSnapshotReply) { - onCaptureSnapshotReply(((CaptureSnapshotReply) message).getSnapshot()); - return true; - } else if (message.equals(COMMIT_SNAPSHOT)) { - context.getSnapshotManager().commit(-1); - return true; + onCaptureSnapshotReply((CaptureSnapshotReply) message); + } else if (COMMIT_SNAPSHOT.equals(message)) { + context.getSnapshotManager().commit(-1, -1); + } else if (message instanceof GetSnapshot) { + onGetSnapshot(sender); } else { return false; } + + return true; } - private void onCaptureSnapshotReply(byte[] snapshotBytes) { - log.debug("{}: CaptureSnapshotReply received by actor: snapshot size {}", context.getId(), snapshotBytes.length); + private void onCaptureSnapshotReply(CaptureSnapshotReply reply) { + log.debug("{}: CaptureSnapshotReply received by actor", context.getId()); - context.getSnapshotManager().persist(snapshotBytes, currentBehavior, context.getTotalMemory()); + context.getSnapshotManager().persist(reply.getSnapshotState(), reply.getInstallSnapshotStream(), + context.getTotalMemory()); } private void onSaveSnapshotFailure(SaveSnapshotFailure saveSnapshotFailure) { @@ -80,24 +92,44 @@ class RaftActorSnapshotMessageSupport { } private void onSaveSnapshotSuccess(SaveSnapshotSuccess success) { - log.info("{}: SaveSnapshotSuccess received for snapshot", context.getId()); - long sequenceNumber = success.metadata().sequenceNr(); - context.getSnapshotManager().commit(sequenceNumber); + log.info("{}: SaveSnapshotSuccess received for snapshot, sequenceNr: {}", context.getId(), sequenceNumber); + + context.getSnapshotManager().commit(sequenceNumber, success.metadata().timestamp()); } - private void onApplySnapshot(Snapshot snapshot) { - if(log.isDebugEnabled()) { - log.debug("{}: ApplySnapshot called on Follower Actor " + - "snapshotIndex:{}, snapshotTerm:{}", context.getId(), snapshot.getLastAppliedIndex(), - snapshot.getLastAppliedTerm()); - } + private void onApplySnapshot(ApplySnapshot message) { + log.info("{}: Applying snapshot on follower: {}", context.getId(), message.getSnapshot()); + + context.getSnapshotManager().apply(message); + } + + private void onGetSnapshot(ActorRef sender) { + log.debug("{}: onGetSnapshot", context.getId()); + + if (context.getPersistenceProvider().isRecoveryApplicable()) { + CaptureSnapshot captureSnapshot = context.getSnapshotManager().newCaptureSnapshot( + context.getReplicatedLog().last(), -1); - cohort.applySnapshot(snapshot.getState()); + ActorRef snapshotReplyActor = context.actorOf(GetSnapshotReplyActor.props(captureSnapshot, + ImmutableElectionTerm.copyOf(context.getTermInformation()), sender, + snapshotReplyActorTimeout, context.getId(), context.getPeerServerInfo(true))); + + cohort.createSnapshot(snapshotReplyActor, Optional.empty()); + } else { + Snapshot snapshot = Snapshot.create( + EmptyState.INSTANCE, Collections.emptyList(), + -1, -1, -1, -1, + context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(), + context.getPeerServerInfo(true)); + + sender.tell(new GetSnapshotReply(context.getId(), snapshot), context.getActor()); + } + } - //clears the followers log, sets the snapshot index to ensure adjusted-index works - context.setReplicatedLog(ReplicatedLogImpl.newInstance(snapshot, context, currentBehavior)); - context.setLastApplied(snapshot.getLastAppliedIndex()); + @VisibleForTesting + void setSnapshotReplyActorTimeout(FiniteDuration snapshotReplyActorTimeout) { + this.snapshotReplyActorTimeout = snapshotReplyActorTimeout; } }