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.client.messages.GetSnapshot;
22 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply;
23 import org.slf4j.Logger;
24 import scala.concurrent.duration.Duration;
27 * Handles snapshot related messages for a RaftActor.
29 * @author Thomas Pantelis
31 class RaftActorSnapshotMessageSupport {
32 static final Object COMMIT_SNAPSHOT = new Object() {
34 public String toString() {
35 return "commit_snapshot";
39 private final RaftActorContext context;
40 private final RaftActorSnapshotCohort cohort;
41 private final Logger log;
43 private final Procedure<Void> createSnapshotProcedure = new Procedure<Void>() {
45 public void apply(Void notUsed) {
46 cohort.createSnapshot(context.getActor());
50 private final Procedure<byte[]> applySnapshotProcedure = new Procedure<byte[]>() {
52 public void apply(byte[] state) {
53 cohort.applySnapshot(state);
57 private Duration snapshotReplyActorTimeout = Duration.create(30, TimeUnit.SECONDS);
59 RaftActorSnapshotMessageSupport(final RaftActorContext context, final RaftActorSnapshotCohort cohort) {
60 this.context = context;
62 this.log = context.getLogger();
64 context.getSnapshotManager().setCreateSnapshotCallable(createSnapshotProcedure);
65 context.getSnapshotManager().setApplySnapshotProcedure(applySnapshotProcedure);
68 boolean handleSnapshotMessage(Object message, ActorRef sender) {
69 if (message instanceof ApplySnapshot ) {
70 onApplySnapshot((ApplySnapshot) message);
71 } else if (message instanceof SaveSnapshotSuccess) {
72 onSaveSnapshotSuccess((SaveSnapshotSuccess) message);
73 } else if (message instanceof SaveSnapshotFailure) {
74 onSaveSnapshotFailure((SaveSnapshotFailure) message);
75 } else if (message instanceof CaptureSnapshotReply) {
76 onCaptureSnapshotReply(((CaptureSnapshotReply) message).getSnapshot());
77 } else if (COMMIT_SNAPSHOT.equals(message)) {
78 context.getSnapshotManager().commit(-1);
79 } else if (message instanceof GetSnapshot) {
80 onGetSnapshot(sender);
88 private void onCaptureSnapshotReply(byte[] snapshotBytes) {
89 log.debug("{}: CaptureSnapshotReply received by actor: snapshot size {}", context.getId(), snapshotBytes.length);
91 context.getSnapshotManager().persist(snapshotBytes, context.getTotalMemory());
94 private void onSaveSnapshotFailure(SaveSnapshotFailure saveSnapshotFailure) {
95 log.error("{}: SaveSnapshotFailure received for snapshot Cause:",
96 context.getId(), saveSnapshotFailure.cause());
98 context.getSnapshotManager().rollback();
101 private void onSaveSnapshotSuccess(SaveSnapshotSuccess success) {
102 log.info("{}: SaveSnapshotSuccess received for snapshot", context.getId());
104 long sequenceNumber = success.metadata().sequenceNr();
106 context.getSnapshotManager().commit(sequenceNumber);
109 private void onApplySnapshot(ApplySnapshot message) {
110 log.info("{}: Applying snapshot on follower with snapshotIndex: {}, snapshotTerm: {}", context.getId(),
111 message.getSnapshot().getLastAppliedIndex(), message.getSnapshot().getLastAppliedTerm());
113 context.getSnapshotManager().apply(message);
116 private void onGetSnapshot(ActorRef sender) {
117 log.debug("{}: onGetSnapshot", context.getId());
119 if(context.getPersistenceProvider().isRecoveryApplicable()) {
120 CaptureSnapshot captureSnapshot = context.getSnapshotManager().newCaptureSnapshot(
121 context.getReplicatedLog().last(), -1, false);
123 ActorRef snapshotReplyActor = context.actorOf(GetSnapshotReplyActor.props(captureSnapshot,
124 ImmutableElectionTerm.copyOf(context.getTermInformation()), sender,
125 snapshotReplyActorTimeout, context.getId(), context.getPeerServerInfo(true)));
127 cohort.createSnapshot(snapshotReplyActor);
129 Snapshot snapshot = Snapshot.create(new byte[0], Collections.<ReplicatedLogEntry>emptyList(), -1, -1, -1, -1,
130 context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(),
131 context.getPeerServerInfo(true));
133 sender.tell(new GetSnapshotReply(context.getId(), SerializationUtils.serialize(snapshot)),
139 void setSnapshotReplyActorTimeout(Duration snapshotReplyActorTimeout) {
140 this.snapshotReplyActorTimeout = snapshotReplyActorTimeout;