0fe9a1acc3126d316392ac34cce332f7c5b733a2
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActorSnapshotMessageSupport.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.raft;
9
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.FiniteDuration;
26
27 /**
28  * Handles snapshot related messages for a RaftActor.
29  *
30  * @author Thomas Pantelis
31  */
32 class RaftActorSnapshotMessageSupport {
33     static final Object COMMIT_SNAPSHOT = new Object() {
34         @Override
35         public String toString() {
36             return "commit_snapshot";
37         }
38     };
39
40     private final RaftActorContext context;
41     private final RaftActorSnapshotCohort cohort;
42     private final Logger log;
43
44     private FiniteDuration snapshotReplyActorTimeout = FiniteDuration.create(30, TimeUnit.SECONDS);
45
46     RaftActorSnapshotMessageSupport(final RaftActorContext context, final RaftActorSnapshotCohort cohort) {
47         this.context = context;
48         this.cohort = cohort;
49         this.log = context.getLogger();
50
51         context.getSnapshotManager().setCreateSnapshotConsumer(
52             outputStream -> cohort.createSnapshot(context.getActor(), outputStream));
53         context.getSnapshotManager().setSnapshotCohort(cohort);
54     }
55
56     RaftActorSnapshotCohort getSnapshotCohort() {
57         return cohort;
58     }
59
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);
73         } else {
74             return false;
75         }
76
77         return true;
78     }
79
80     private void onCaptureSnapshotReply(CaptureSnapshotReply reply) {
81         log.debug("{}: CaptureSnapshotReply received by actor", context.getId());
82
83         context.getSnapshotManager().persist(reply.getSnapshotState(), reply.getInstallSnapshotStream(),
84                 context.getTotalMemory());
85     }
86
87     private void onSaveSnapshotFailure(SaveSnapshotFailure saveSnapshotFailure) {
88         log.error("{}: SaveSnapshotFailure received for snapshot Cause:",
89                 context.getId(), saveSnapshotFailure.cause());
90
91         context.getSnapshotManager().rollback();
92     }
93
94     private void onSaveSnapshotSuccess(SaveSnapshotSuccess success) {
95         long sequenceNumber = success.metadata().sequenceNr();
96
97         log.info("{}: SaveSnapshotSuccess received for snapshot, sequenceNr: {}", context.getId(), sequenceNumber);
98
99         context.getSnapshotManager().commit(sequenceNumber, success.metadata().timestamp());
100     }
101
102     private void onApplySnapshot(ApplySnapshot message) {
103         log.info("{}: Applying snapshot on follower:  {}", context.getId(), message.getSnapshot());
104
105         context.getSnapshotManager().apply(message);
106     }
107
108     private void onGetSnapshot(ActorRef sender) {
109         log.debug("{}: onGetSnapshot", context.getId());
110
111         if (context.getPersistenceProvider().isRecoveryApplicable()) {
112             CaptureSnapshot captureSnapshot = context.getSnapshotManager().newCaptureSnapshot(
113                     context.getReplicatedLog().last(), -1, true);
114
115             ActorRef snapshotReplyActor = context.actorOf(GetSnapshotReplyActor.props(captureSnapshot,
116                     ImmutableElectionTerm.copyOf(context.getTermInformation()), sender,
117                     snapshotReplyActorTimeout, context.getId(), context.getPeerServerInfo(true)));
118
119             cohort.createSnapshot(snapshotReplyActor, Optional.empty());
120         } else {
121             Snapshot snapshot = Snapshot.create(
122                     EmptyState.INSTANCE, Collections.<ReplicatedLogEntry>emptyList(),
123                     -1, -1, -1, -1,
124                     context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(),
125                     context.getPeerServerInfo(true));
126
127             sender.tell(new GetSnapshotReply(context.getId(), snapshot), context.getActor());
128         }
129     }
130
131     @VisibleForTesting
132     void setSnapshotReplyActorTimeout(FiniteDuration snapshotReplyActorTimeout) {
133         this.snapshotReplyActorTimeout = snapshotReplyActorTimeout;
134     }
135 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.