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