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