2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.raft;
10 import akka.actor.ActorRef;
11 import akka.japi.Procedure;
12 import akka.persistence.SaveSnapshotFailure;
13 import akka.persistence.SaveSnapshotSuccess;
14 import com.google.common.annotations.VisibleForTesting;
15 import java.util.Collections;
16 import java.util.concurrent.TimeUnit;
17 import org.apache.commons.lang3.SerializationUtils;
18 import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot;
19 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot;
20 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
21 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
22 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot;
23 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply;
24 import org.slf4j.Logger;
25 import scala.concurrent.duration.Duration;
28 * Handles snapshot related messages for a RaftActor.
30 * @author Thomas Pantelis
32 class RaftActorSnapshotMessageSupport {
33 static final String COMMIT_SNAPSHOT = "commit_snapshot";
35 private final RaftActorContext context;
36 private final RaftActorBehavior currentBehavior;
37 private final RaftActorSnapshotCohort cohort;
38 private final Logger log;
40 private final Procedure<Void> createSnapshotProcedure = new Procedure<Void>() {
42 public void apply(Void notUsed) {
43 cohort.createSnapshot(context.getActor());
47 private final Procedure<byte[]> applySnapshotProcedure = new Procedure<byte[]>() {
49 public void apply(byte[] state) {
50 cohort.applySnapshot(state);
54 private Duration snapshotReplyActorTimeout = Duration.create(30, TimeUnit.SECONDS);
56 RaftActorSnapshotMessageSupport(RaftActorContext context, RaftActorBehavior currentBehavior,
57 RaftActorSnapshotCohort cohort) {
58 this.context = context;
59 this.currentBehavior = currentBehavior;
61 this.log = context.getLogger();
63 context.getSnapshotManager().setCreateSnapshotCallable(createSnapshotProcedure);
64 context.getSnapshotManager().setApplySnapshotProcedure(applySnapshotProcedure);
67 boolean handleSnapshotMessage(Object message, ActorRef sender) {
68 if(message instanceof ApplySnapshot ) {
69 onApplySnapshot((ApplySnapshot) message);
71 } else if (message instanceof SaveSnapshotSuccess) {
72 onSaveSnapshotSuccess((SaveSnapshotSuccess) message);
74 } else if (message instanceof SaveSnapshotFailure) {
75 onSaveSnapshotFailure((SaveSnapshotFailure) message);
77 } else if (message instanceof CaptureSnapshotReply) {
78 onCaptureSnapshotReply(((CaptureSnapshotReply) message).getSnapshot());
80 } else if (message.equals(COMMIT_SNAPSHOT)) {
81 context.getSnapshotManager().commit(-1, currentBehavior);
83 } else if (message instanceof GetSnapshot) {
84 onGetSnapshot(sender);
91 private void onCaptureSnapshotReply(byte[] snapshotBytes) {
92 log.debug("{}: CaptureSnapshotReply received by actor: snapshot size {}", context.getId(), snapshotBytes.length);
94 context.getSnapshotManager().persist(snapshotBytes, currentBehavior, context.getTotalMemory());
97 private void onSaveSnapshotFailure(SaveSnapshotFailure saveSnapshotFailure) {
98 log.error("{}: SaveSnapshotFailure received for snapshot Cause:",
99 context.getId(), saveSnapshotFailure.cause());
101 context.getSnapshotManager().rollback();
104 private void onSaveSnapshotSuccess(SaveSnapshotSuccess success) {
105 log.info("{}: SaveSnapshotSuccess received for snapshot", context.getId());
107 long sequenceNumber = success.metadata().sequenceNr();
109 context.getSnapshotManager().commit(sequenceNumber, currentBehavior);
112 private void onApplySnapshot(ApplySnapshot message) {
113 log.info("{}: Applying snapshot on follower with snapshotIndex: {}, snapshotTerm: {}", context.getId(),
114 message.getSnapshot().getLastAppliedIndex(), message.getSnapshot().getLastAppliedTerm());
116 context.getSnapshotManager().apply(message);
119 private void onGetSnapshot(ActorRef sender) {
120 log.debug("{}: onGetSnapshot", context.getId());
122 if(context.getPersistenceProvider().isRecoveryApplicable()) {
123 CaptureSnapshot captureSnapshot = context.getSnapshotManager().newCaptureSnapshot(
124 context.getReplicatedLog().last(), -1, false);
126 ActorRef snapshotReplyActor = context.actorOf(GetSnapshotReplyActor.props(captureSnapshot,
127 ImmutableElectionTerm.copyOf(context.getTermInformation()), sender,
128 snapshotReplyActorTimeout, context.getId(), context.getPeerServerInfo(true)));
130 cohort.createSnapshot(snapshotReplyActor);
132 Snapshot snapshot = Snapshot.create(new byte[0], Collections.<ReplicatedLogEntry>emptyList(), -1, -1, -1, -1,
133 context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(),
134 context.getPeerServerInfo(true));
136 sender.tell(new GetSnapshotReply(context.getId(), SerializationUtils.serialize(snapshot)),
142 void setSnapshotReplyActorTimeout(Duration snapshotReplyActorTimeout) {
143 this.snapshotReplyActorTimeout = snapshotReplyActorTimeout;