8b6871174180d8d372bcc2eb075ebef045ba6c3d
[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.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;
25
26 /**
27  * Handles snapshot related messages for a RaftActor.
28  *
29  * @author Thomas Pantelis
30  */
31 class RaftActorSnapshotMessageSupport {
32     static final String COMMIT_SNAPSHOT = "commit_snapshot";
33
34     private final RaftActorContext context;
35     private final RaftActorSnapshotCohort cohort;
36     private final Logger log;
37
38     private final Procedure<Void> createSnapshotProcedure = new Procedure<Void>() {
39         @Override
40         public void apply(Void notUsed) {
41             cohort.createSnapshot(context.getActor());
42         }
43     };
44
45     private final Procedure<byte[]> applySnapshotProcedure = new Procedure<byte[]>() {
46         @Override
47         public void apply(byte[] state) {
48             cohort.applySnapshot(state);
49         }
50     };
51
52     private Duration snapshotReplyActorTimeout = Duration.create(30, TimeUnit.SECONDS);
53
54     RaftActorSnapshotMessageSupport(final RaftActorContext context, final RaftActorSnapshotCohort cohort) {
55         this.context = context;
56         this.cohort = cohort;
57         this.log = context.getLogger();
58
59         context.getSnapshotManager().setCreateSnapshotCallable(createSnapshotProcedure);
60         context.getSnapshotManager().setApplySnapshotProcedure(applySnapshotProcedure);
61     }
62
63     boolean handleSnapshotMessage(Object message, ActorRef sender) {
64         if (message instanceof ApplySnapshot ) {
65             onApplySnapshot((ApplySnapshot) message);
66         } else if (message instanceof SaveSnapshotSuccess) {
67             onSaveSnapshotSuccess((SaveSnapshotSuccess) message);
68         } else if (message instanceof SaveSnapshotFailure) {
69             onSaveSnapshotFailure((SaveSnapshotFailure) message);
70         } else if (message instanceof CaptureSnapshotReply) {
71             onCaptureSnapshotReply(((CaptureSnapshotReply) message).getSnapshot());
72         } else if (message.equals(COMMIT_SNAPSHOT)) {
73             context.getSnapshotManager().commit(-1);
74         } else if (message instanceof GetSnapshot) {
75             onGetSnapshot(sender);
76         } else {
77             return false;
78         }
79
80         return true;
81     }
82
83     private void onCaptureSnapshotReply(byte[] snapshotBytes) {
84         log.debug("{}: CaptureSnapshotReply received by actor: snapshot size {}", context.getId(), snapshotBytes.length);
85
86         context.getSnapshotManager().persist(snapshotBytes, context.getTotalMemory());
87     }
88
89     private void onSaveSnapshotFailure(SaveSnapshotFailure saveSnapshotFailure) {
90         log.error("{}: SaveSnapshotFailure received for snapshot Cause:",
91                 context.getId(), saveSnapshotFailure.cause());
92
93         context.getSnapshotManager().rollback();
94     }
95
96     private void onSaveSnapshotSuccess(SaveSnapshotSuccess success) {
97         log.info("{}: SaveSnapshotSuccess received for snapshot", context.getId());
98
99         long sequenceNumber = success.metadata().sequenceNr();
100
101         context.getSnapshotManager().commit(sequenceNumber);
102     }
103
104     private void onApplySnapshot(ApplySnapshot message) {
105         log.info("{}: Applying snapshot on follower with snapshotIndex: {}, snapshotTerm: {}", context.getId(),
106                 message.getSnapshot().getLastAppliedIndex(), message.getSnapshot().getLastAppliedTerm());
107
108         context.getSnapshotManager().apply(message);
109     }
110
111     private void onGetSnapshot(ActorRef sender) {
112         log.debug("{}: onGetSnapshot", context.getId());
113
114         if(context.getPersistenceProvider().isRecoveryApplicable()) {
115             CaptureSnapshot captureSnapshot = context.getSnapshotManager().newCaptureSnapshot(
116                     context.getReplicatedLog().last(), -1, false);
117
118             ActorRef snapshotReplyActor = context.actorOf(GetSnapshotReplyActor.props(captureSnapshot,
119                     ImmutableElectionTerm.copyOf(context.getTermInformation()), sender,
120                     snapshotReplyActorTimeout, context.getId(), context.getPeerServerInfo(true)));
121
122             cohort.createSnapshot(snapshotReplyActor);
123         } else {
124             Snapshot snapshot = Snapshot.create(new byte[0], Collections.<ReplicatedLogEntry>emptyList(), -1, -1, -1, -1,
125                     context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(),
126                     context.getPeerServerInfo(true));
127
128             sender.tell(new GetSnapshotReply(context.getId(), SerializationUtils.serialize(snapshot)),
129                     context.getActor());
130         }
131     }
132
133     @VisibleForTesting
134     void setSnapshotReplyActorTimeout(Duration snapshotReplyActorTimeout) {
135         this.snapshotReplyActorTimeout = snapshotReplyActorTimeout;
136     }
137 }