Implement RemoveServer for leader
[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             return true;
71         } else if (message instanceof SaveSnapshotSuccess) {
72             onSaveSnapshotSuccess((SaveSnapshotSuccess) message);
73             return true;
74         } else if (message instanceof SaveSnapshotFailure) {
75             onSaveSnapshotFailure((SaveSnapshotFailure) message);
76             return true;
77         } else if (message instanceof CaptureSnapshotReply) {
78             onCaptureSnapshotReply(((CaptureSnapshotReply) message).getSnapshot());
79             return true;
80         } else if (message.equals(COMMIT_SNAPSHOT)) {
81             context.getSnapshotManager().commit(-1, currentBehavior);
82             return true;
83         } else if (message instanceof GetSnapshot) {
84             onGetSnapshot(sender);
85             return true;
86         } else {
87             return false;
88         }
89     }
90
91     private void onCaptureSnapshotReply(byte[] snapshotBytes) {
92         log.debug("{}: CaptureSnapshotReply received by actor: snapshot size {}", context.getId(), snapshotBytes.length);
93
94         context.getSnapshotManager().persist(snapshotBytes, currentBehavior, context.getTotalMemory());
95     }
96
97     private void onSaveSnapshotFailure(SaveSnapshotFailure saveSnapshotFailure) {
98         log.error("{}: SaveSnapshotFailure received for snapshot Cause:",
99                 context.getId(), saveSnapshotFailure.cause());
100
101         context.getSnapshotManager().rollback();
102     }
103
104     private void onSaveSnapshotSuccess(SaveSnapshotSuccess success) {
105         log.info("{}: SaveSnapshotSuccess received for snapshot", context.getId());
106
107         long sequenceNumber = success.metadata().sequenceNr();
108
109         context.getSnapshotManager().commit(sequenceNumber, currentBehavior);
110     }
111
112     private void onApplySnapshot(ApplySnapshot message) {
113         log.info("{}: Applying snapshot on follower with snapshotIndex: {}, snapshotTerm: {}", context.getId(),
114                 message.getSnapshot().getLastAppliedIndex(), message.getSnapshot().getLastAppliedTerm());
115
116         context.getSnapshotManager().apply(message);
117     }
118
119     private void onGetSnapshot(ActorRef sender) {
120         log.debug("{}: onGetSnapshot", context.getId());
121
122         if(context.getPersistenceProvider().isRecoveryApplicable()) {
123             CaptureSnapshot captureSnapshot = context.getSnapshotManager().newCaptureSnapshot(
124                     context.getReplicatedLog().last(), -1, false);
125
126             ActorRef snapshotReplyActor = context.actorOf(GetSnapshotReplyActor.props(captureSnapshot,
127                     ImmutableElectionTerm.copyOf(context.getTermInformation()), sender,
128                     snapshotReplyActorTimeout, context.getId(), context.getPeerServerInfo(true)));
129
130             cohort.createSnapshot(snapshotReplyActor);
131         } else {
132             Snapshot snapshot = Snapshot.create(new byte[0], Collections.<ReplicatedLogEntry>emptyList(), -1, -1, -1, -1,
133                     context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(),
134                     context.getPeerServerInfo(true));
135
136             sender.tell(new GetSnapshotReply(context.getId(), SerializationUtils.serialize(snapshot)),
137                     context.getActor());
138         }
139     }
140
141     @VisibleForTesting
142     void setSnapshotReplyActorTimeout(Duration snapshotReplyActorTimeout) {
143         this.snapshotReplyActorTimeout = snapshotReplyActorTimeout;
144     }
145 }