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.Optional;
16 import java.util.concurrent.TimeUnit;
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.opendaylight.controller.cluster.raft.persisted.EmptyState;
23 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
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 Object COMMIT_SNAPSHOT = new Object() {
35 public String toString() {
36 return "commit_snapshot";
40 private final RaftActorContext context;
41 private final RaftActorSnapshotCohort cohort;
42 private final Logger log;
44 private Duration snapshotReplyActorTimeout = Duration.create(30, TimeUnit.SECONDS);
46 RaftActorSnapshotMessageSupport(final RaftActorContext context, final RaftActorSnapshotCohort cohort) {
47 this.context = context;
49 this.log = context.getLogger();
51 context.getSnapshotManager().setCreateSnapshotConsumer(
52 outputStream -> cohort.createSnapshot(context.getActor(), outputStream));
53 context.getSnapshotManager().setSnapshotCohort(cohort);
56 RaftActorSnapshotCohort getSnapshotCohort() {
60 boolean handleSnapshotMessage(Object message, ActorRef sender) {
61 if (message instanceof ApplySnapshot) {
62 onApplySnapshot((ApplySnapshot) message);
63 } else if (message instanceof SaveSnapshotSuccess) {
64 onSaveSnapshotSuccess((SaveSnapshotSuccess) message);
65 } else if (message instanceof SaveSnapshotFailure) {
66 onSaveSnapshotFailure((SaveSnapshotFailure) message);
67 } else if (message instanceof CaptureSnapshotReply) {
68 onCaptureSnapshotReply((CaptureSnapshotReply) message);
69 } else if (COMMIT_SNAPSHOT.equals(message)) {
70 context.getSnapshotManager().commit(-1, -1);
71 } else if (message instanceof GetSnapshot) {
72 onGetSnapshot(sender);
80 private void onCaptureSnapshotReply(CaptureSnapshotReply reply) {
81 log.debug("{}: CaptureSnapshotReply received by actor", context.getId());
83 context.getSnapshotManager().persist(reply.getSnapshotState(), reply.getInstallSnapshotStream(),
84 context.getTotalMemory());
87 private void onSaveSnapshotFailure(SaveSnapshotFailure saveSnapshotFailure) {
88 log.error("{}: SaveSnapshotFailure received for snapshot Cause:",
89 context.getId(), saveSnapshotFailure.cause());
91 context.getSnapshotManager().rollback();
94 private void onSaveSnapshotSuccess(SaveSnapshotSuccess success) {
95 long sequenceNumber = success.metadata().sequenceNr();
97 log.info("{}: SaveSnapshotSuccess received for snapshot, sequenceNr: {}", context.getId(), sequenceNumber);
99 context.getSnapshotManager().commit(sequenceNumber, success.metadata().timestamp());
102 private void onApplySnapshot(ApplySnapshot message) {
103 log.info("{}: Applying snapshot on follower: {}", context.getId(), message.getSnapshot());
105 context.getSnapshotManager().apply(message);
108 private void onGetSnapshot(ActorRef sender) {
109 log.debug("{}: onGetSnapshot", context.getId());
111 if (context.getPersistenceProvider().isRecoveryApplicable()) {
112 CaptureSnapshot captureSnapshot = context.getSnapshotManager().newCaptureSnapshot(
113 context.getReplicatedLog().last(), -1);
115 ActorRef snapshotReplyActor = context.actorOf(GetSnapshotReplyActor.props(captureSnapshot,
116 ImmutableElectionTerm.copyOf(context.getTermInformation()), sender,
117 snapshotReplyActorTimeout, context.getId(), context.getPeerServerInfo(true)));
119 cohort.createSnapshot(snapshotReplyActor, Optional.empty());
121 Snapshot snapshot = Snapshot.create(
122 EmptyState.INSTANCE, Collections.<ReplicatedLogEntry>emptyList(),
124 context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(),
125 context.getPeerServerInfo(true));
127 sender.tell(new GetSnapshotReply(context.getId(), snapshot), context.getActor());
132 void setSnapshotReplyActorTimeout(Duration snapshotReplyActorTimeout) {
133 this.snapshotReplyActorTimeout = snapshotReplyActorTimeout;