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 akka.util.Timeout;
14 import com.google.common.annotations.VisibleForTesting;
15 import java.util.Collections;
16 import java.util.Optional;
17 import java.util.concurrent.TimeUnit;
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.opendaylight.controller.cluster.raft.persisted.EmptyState;
24 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
25 import org.slf4j.Logger;
26 import scala.concurrent.duration.FiniteDuration;
29 * Handles snapshot related messages for a RaftActor.
31 * @author Thomas Pantelis
33 class RaftActorSnapshotMessageSupport {
34 static final Object COMMIT_SNAPSHOT = new Object() {
36 public String toString() {
37 return "commit_snapshot";
41 private final RaftActorContext context;
42 private final RaftActorSnapshotCohort cohort;
43 private final Logger log;
45 private FiniteDuration snapshotReplyActorTimeout = FiniteDuration.create(30, TimeUnit.SECONDS);
47 RaftActorSnapshotMessageSupport(final RaftActorContext context, final RaftActorSnapshotCohort cohort) {
48 this.context = context;
50 this.log = context.getLogger();
52 context.getSnapshotManager().setCreateSnapshotConsumer(
53 outputStream -> cohort.createSnapshot(context.getActor(), outputStream));
54 context.getSnapshotManager().setSnapshotCohort(cohort);
57 RaftActorSnapshotCohort getSnapshotCohort() {
61 boolean handleSnapshotMessage(Object message, ActorRef sender) {
62 if (message instanceof ApplySnapshot) {
63 onApplySnapshot((ApplySnapshot) message);
64 } else if (message instanceof SaveSnapshotSuccess) {
65 onSaveSnapshotSuccess((SaveSnapshotSuccess) message);
66 } else if (message instanceof SaveSnapshotFailure) {
67 onSaveSnapshotFailure((SaveSnapshotFailure) message);
68 } else if (message instanceof CaptureSnapshotReply) {
69 onCaptureSnapshotReply((CaptureSnapshotReply) message);
70 } else if (COMMIT_SNAPSHOT.equals(message)) {
71 context.getSnapshotManager().commit(-1, -1);
72 } else if (message instanceof GetSnapshot) {
73 onGetSnapshot(sender, (GetSnapshot) message);
81 private void onCaptureSnapshotReply(CaptureSnapshotReply reply) {
82 log.debug("{}: CaptureSnapshotReply received by actor", context.getId());
84 context.getSnapshotManager().persist(reply.getSnapshotState(), reply.getInstallSnapshotStream(),
85 context.getTotalMemory());
88 private void onSaveSnapshotFailure(SaveSnapshotFailure saveSnapshotFailure) {
89 log.error("{}: SaveSnapshotFailure received for snapshot Cause:",
90 context.getId(), saveSnapshotFailure.cause());
92 context.getSnapshotManager().rollback();
95 private void onSaveSnapshotSuccess(SaveSnapshotSuccess success) {
96 long sequenceNumber = success.metadata().sequenceNr();
98 log.info("{}: SaveSnapshotSuccess received for snapshot, sequenceNr: {}", context.getId(), sequenceNumber);
100 context.getSnapshotManager().commit(sequenceNumber, success.metadata().timestamp());
103 private void onApplySnapshot(ApplySnapshot message) {
104 log.info("{}: Applying snapshot on follower: {}", context.getId(), message.getSnapshot());
106 context.getSnapshotManager().apply(message);
109 private void onGetSnapshot(ActorRef sender, GetSnapshot getSnapshot) {
110 log.debug("{}: onGetSnapshot", context.getId());
113 if (context.getPersistenceProvider().isRecoveryApplicable()) {
114 CaptureSnapshot captureSnapshot = context.getSnapshotManager().newCaptureSnapshot(
115 context.getReplicatedLog().last(), -1, true);
117 final FiniteDuration timeout =
118 getSnapshot.getTimeout().map(Timeout::duration).orElse(snapshotReplyActorTimeout);
120 ActorRef snapshotReplyActor = context.actorOf(GetSnapshotReplyActor.props(captureSnapshot,
121 ImmutableElectionTerm.copyOf(context.getTermInformation()), sender, timeout, context.getId(),
122 context.getPeerServerInfo(true)));
124 cohort.createSnapshot(snapshotReplyActor, Optional.empty());
126 Snapshot snapshot = Snapshot.create(
127 EmptyState.INSTANCE, Collections.<ReplicatedLogEntry>emptyList(),
129 context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(),
130 context.getPeerServerInfo(true));
132 sender.tell(new GetSnapshotReply(context.getId(), snapshot), context.getActor());
137 void setSnapshotReplyActorTimeout(FiniteDuration snapshotReplyActorTimeout) {
138 this.snapshotReplyActorTimeout = snapshotReplyActorTimeout;