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=39548dc6d5e426dad36fa5fe30eb3b5b448a7bc0;hp=bf0fc10aad944ae8539c8b3b2a4da70020500944;hb=250f3f77c80284536cc32e96739f713d21844103;hpb=3fda1a923defdbf18849c6080c3aa19f1ebf2c5f 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 bf0fc10aad..39548dc6d5 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.actor.ActorRef; import akka.japi.Procedure; import akka.persistence.SaveSnapshotFailure; import akka.persistence.SaveSnapshotSuccess; +import com.google.common.annotations.VisibleForTesting; +import java.util.Collections; +import java.util.concurrent.TimeUnit; +import org.apache.commons.lang3.SerializationUtils; 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.slf4j.Logger; +import scala.concurrent.duration.Duration; /** * Handles snapshot related messages for a RaftActor. @@ -42,6 +51,8 @@ class RaftActorSnapshotMessageSupport { } }; + private Duration snapshotReplyActorTimeout = Duration.create(30, TimeUnit.SECONDS); + RaftActorSnapshotMessageSupport(RaftActorContext context, RaftActorBehavior currentBehavior, RaftActorSnapshotCohort cohort) { this.context = context; @@ -53,7 +64,7 @@ class RaftActorSnapshotMessageSupport { context.getSnapshotManager().setApplySnapshotProcedure(applySnapshotProcedure); } - boolean handleSnapshotMessage(Object message) { + boolean handleSnapshotMessage(Object message, ActorRef sender) { if(message instanceof ApplySnapshot ) { onApplySnapshot((ApplySnapshot) message); return true; @@ -69,6 +80,9 @@ class RaftActorSnapshotMessageSupport { } else if (message.equals(COMMIT_SNAPSHOT)) { context.getSnapshotManager().commit(-1, currentBehavior); return true; + } else if (message instanceof GetSnapshot) { + onGetSnapshot(sender); + return true; } else { return false; } @@ -101,4 +115,31 @@ class RaftActorSnapshotMessageSupport { 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, false); + + ActorRef snapshotReplyActor = context.actorOf(GetSnapshotReplyActor.props(captureSnapshot, + ImmutableElectionTerm.copyOf(context.getTermInformation()), sender, + snapshotReplyActorTimeout, context.getId(), context.getPeerServerInfo())); + + cohort.createSnapshot(snapshotReplyActor); + } else { + Snapshot snapshot = Snapshot.create(new byte[0], Collections.emptyList(), -1, -1, -1, -1, + context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(), + context.getPeerServerInfo()); + + sender.tell(new GetSnapshotReply(context.getId(), SerializationUtils.serialize(snapshot)), + context.getActor()); + } + } + + @VisibleForTesting + void setSnapshotReplyActorTimeout(Duration snapshotReplyActorTimeout) { + this.snapshotReplyActorTimeout = snapshotReplyActorTimeout; + } }