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