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.persistence.SaveSnapshotFailure;
12 import akka.persistence.SaveSnapshotSuccess;
13 import com.google.common.annotations.VisibleForTesting;
14 import java.util.Collections;
15 import java.util.concurrent.TimeUnit;
16 import org.apache.commons.lang3.SerializationUtils;
17 import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot;
18 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot;
19 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
20 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot;
21 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply;
22 import org.slf4j.Logger;
23 import scala.concurrent.duration.Duration;
26 * Handles snapshot related messages for a RaftActor.
28 * @author Thomas Pantelis
30 class RaftActorSnapshotMessageSupport {
31 static final Object COMMIT_SNAPSHOT = new Object() {
33 public String toString() {
34 return "commit_snapshot";
38 private final RaftActorContext context;
39 private final RaftActorSnapshotCohort cohort;
40 private final Logger log;
42 private Duration snapshotReplyActorTimeout = Duration.create(30, TimeUnit.SECONDS);
44 RaftActorSnapshotMessageSupport(final RaftActorContext context, final RaftActorSnapshotCohort cohort) {
45 this.context = context;
47 this.log = context.getLogger();
49 context.getSnapshotManager().setCreateSnapshotRunnable(() -> cohort.createSnapshot(context.getActor()));
50 context.getSnapshotManager().setApplySnapshotConsumer(cohort::applySnapshot);
53 boolean handleSnapshotMessage(Object message, ActorRef sender) {
54 if (message instanceof ApplySnapshot ) {
55 onApplySnapshot((ApplySnapshot) message);
56 } else if (message instanceof SaveSnapshotSuccess) {
57 onSaveSnapshotSuccess((SaveSnapshotSuccess) message);
58 } else if (message instanceof SaveSnapshotFailure) {
59 onSaveSnapshotFailure((SaveSnapshotFailure) message);
60 } else if (message instanceof CaptureSnapshotReply) {
61 onCaptureSnapshotReply(((CaptureSnapshotReply) message).getSnapshot());
62 } else if (COMMIT_SNAPSHOT.equals(message)) {
63 context.getSnapshotManager().commit(-1);
64 } else if (message instanceof GetSnapshot) {
65 onGetSnapshot(sender);
73 private void onCaptureSnapshotReply(byte[] snapshotBytes) {
74 log.debug("{}: CaptureSnapshotReply received by actor: snapshot size {}", context.getId(), snapshotBytes.length);
76 context.getSnapshotManager().persist(snapshotBytes, context.getTotalMemory());
79 private void onSaveSnapshotFailure(SaveSnapshotFailure saveSnapshotFailure) {
80 log.error("{}: SaveSnapshotFailure received for snapshot Cause:",
81 context.getId(), saveSnapshotFailure.cause());
83 context.getSnapshotManager().rollback();
86 private void onSaveSnapshotSuccess(SaveSnapshotSuccess success) {
87 log.info("{}: SaveSnapshotSuccess received for snapshot", context.getId());
89 long sequenceNumber = success.metadata().sequenceNr();
91 context.getSnapshotManager().commit(sequenceNumber);
94 private void onApplySnapshot(ApplySnapshot message) {
95 log.info("{}: Applying snapshot on follower: {}", context.getId(), message.getSnapshot());
97 context.getSnapshotManager().apply(message);
100 private void onGetSnapshot(ActorRef sender) {
101 log.debug("{}: onGetSnapshot", context.getId());
103 if(context.getPersistenceProvider().isRecoveryApplicable()) {
104 CaptureSnapshot captureSnapshot = context.getSnapshotManager().newCaptureSnapshot(
105 context.getReplicatedLog().last(), -1, false);
107 ActorRef snapshotReplyActor = context.actorOf(GetSnapshotReplyActor.props(captureSnapshot,
108 ImmutableElectionTerm.copyOf(context.getTermInformation()), sender,
109 snapshotReplyActorTimeout, context.getId(), context.getPeerServerInfo(true)));
111 cohort.createSnapshot(snapshotReplyActor);
113 Snapshot snapshot = Snapshot.create(new byte[0], Collections.<ReplicatedLogEntry>emptyList(), -1, -1, -1, -1,
114 context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(),
115 context.getPeerServerInfo(true));
117 sender.tell(new GetSnapshotReply(context.getId(), SerializationUtils.serialize(snapshot)),
123 void setSnapshotReplyActorTimeout(Duration snapshotReplyActorTimeout) {
124 this.snapshotReplyActorTimeout = snapshotReplyActorTimeout;