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);
70 } else if (message instanceof SaveSnapshotSuccess) {
71 onSaveSnapshotSuccess((SaveSnapshotSuccess) message);
72 } else if (message instanceof SaveSnapshotFailure) {
73 onSaveSnapshotFailure((SaveSnapshotFailure) message);
74 } else if (message instanceof CaptureSnapshotReply) {
75 onCaptureSnapshotReply(((CaptureSnapshotReply) message).getSnapshot());
76 } else if (message.equals(COMMIT_SNAPSHOT)) {
77 context.getSnapshotManager().commit(-1, currentBehavior);
78 } else if (message instanceof GetSnapshot) {
79 onGetSnapshot(sender);
87 private void onCaptureSnapshotReply(byte[] snapshotBytes) {
88 log.debug("{}: CaptureSnapshotReply received by actor: snapshot size {}", context.getId(), snapshotBytes.length);
90 context.getSnapshotManager().persist(snapshotBytes, currentBehavior, context.getTotalMemory());
93 private void onSaveSnapshotFailure(SaveSnapshotFailure saveSnapshotFailure) {
94 log.error("{}: SaveSnapshotFailure received for snapshot Cause:",
95 context.getId(), saveSnapshotFailure.cause());
97 context.getSnapshotManager().rollback();
100 private void onSaveSnapshotSuccess(SaveSnapshotSuccess success) {
101 log.info("{}: SaveSnapshotSuccess received for snapshot", context.getId());
103 long sequenceNumber = success.metadata().sequenceNr();
105 context.getSnapshotManager().commit(sequenceNumber, currentBehavior);
108 private void onApplySnapshot(ApplySnapshot message) {
109 log.info("{}: Applying snapshot on follower with snapshotIndex: {}, snapshotTerm: {}", context.getId(),
110 message.getSnapshot().getLastAppliedIndex(), message.getSnapshot().getLastAppliedTerm());
112 context.getSnapshotManager().apply(message);
115 private void onGetSnapshot(ActorRef sender) {
116 log.debug("{}: onGetSnapshot", context.getId());
118 if(context.getPersistenceProvider().isRecoveryApplicable()) {
119 CaptureSnapshot captureSnapshot = context.getSnapshotManager().newCaptureSnapshot(
120 context.getReplicatedLog().last(), -1, false);
122 ActorRef snapshotReplyActor = context.actorOf(GetSnapshotReplyActor.props(captureSnapshot,
123 ImmutableElectionTerm.copyOf(context.getTermInformation()), sender,
124 snapshotReplyActorTimeout, context.getId(), context.getPeerServerInfo(true)));
126 cohort.createSnapshot(snapshotReplyActor);
128 Snapshot snapshot = Snapshot.create(new byte[0], Collections.<ReplicatedLogEntry>emptyList(), -1, -1, -1, -1,
129 context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(),
130 context.getPeerServerInfo(true));
132 sender.tell(new GetSnapshotReply(context.getId(), SerializationUtils.serialize(snapshot)),
138 void setSnapshotReplyActorTimeout(Duration snapshotReplyActorTimeout) {
139 this.snapshotReplyActorTimeout = snapshotReplyActorTimeout;