Refactor raft recovery code to a RaftActorRecoverySupport class
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActorRecoverySupport.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.persistence.RecoveryCompleted;
11 import akka.persistence.SnapshotOffer;
12 import com.google.common.base.Stopwatch;
13 import org.opendaylight.controller.cluster.DataPersistenceProvider;
14 import org.opendaylight.controller.cluster.raft.RaftActor.DeleteEntries;
15 import org.opendaylight.controller.cluster.raft.RaftActor.UpdateElectionTerm;
16 import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries;
17 import org.opendaylight.controller.cluster.raft.base.messages.ApplyLogEntries;
18 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
19 import org.slf4j.Logger;
20
21 /**
22  * Support class that handles persistence recovery for a RaftActor.
23  *
24  * @author Thomas Pantelis
25  */
26 class RaftActorRecoverySupport {
27     private final DataPersistenceProvider persistence;
28     private final RaftActorContext context;
29     private final RaftActorBehavior currentBehavior;
30     private final RaftActorRecoveryCohort cohort;
31
32     private int currentRecoveryBatchCount;
33
34     private Stopwatch recoveryTimer;
35     private final Logger log;
36
37     RaftActorRecoverySupport(DataPersistenceProvider persistence, RaftActorContext context,
38             RaftActorBehavior currentBehavior, RaftActorRecoveryCohort cohort) {
39         this.persistence = persistence;
40         this.context = context;
41         this.currentBehavior = currentBehavior;
42         this.cohort = cohort;
43         this.log = context.getLogger();
44     }
45
46     boolean handleRecoveryMessage(Object message) {
47         boolean recoveryComplete = false;
48         if(persistence.isRecoveryApplicable()) {
49             if (message instanceof SnapshotOffer) {
50                 onRecoveredSnapshot((SnapshotOffer) message);
51             } else if (message instanceof ReplicatedLogEntry) {
52                 onRecoveredJournalLogEntry((ReplicatedLogEntry) message);
53             } else if (message instanceof ApplyLogEntries) {
54                 // Handle this message for backwards compatibility with pre-Lithium versions.
55                 onRecoveredApplyLogEntries(((ApplyLogEntries) message).getToIndex());
56             } else if (message instanceof ApplyJournalEntries) {
57                 onRecoveredApplyLogEntries(((ApplyJournalEntries) message).getToIndex());
58             } else if (message instanceof DeleteEntries) {
59                 replicatedLog().removeFrom(((DeleteEntries) message).getFromIndex());
60             } else if (message instanceof UpdateElectionTerm) {
61                 context.getTermInformation().update(((UpdateElectionTerm) message).getCurrentTerm(),
62                         ((UpdateElectionTerm) message).getVotedFor());
63             } else if (message instanceof RecoveryCompleted) {
64                 onRecoveryCompletedMessage();
65                 recoveryComplete = true;
66             }
67         } else if (message instanceof RecoveryCompleted) {
68             recoveryComplete = true;
69         }
70
71         return recoveryComplete;
72     }
73
74     private ReplicatedLog replicatedLog() {
75         return context.getReplicatedLog();
76     }
77
78     private void initRecoveryTimer() {
79         if(recoveryTimer == null) {
80             recoveryTimer = Stopwatch.createStarted();
81         }
82     }
83
84     private void onRecoveredSnapshot(SnapshotOffer offer) {
85         if(log.isDebugEnabled()) {
86             log.debug("{}: SnapshotOffer called..", context.getId());
87         }
88
89         initRecoveryTimer();
90
91         Snapshot snapshot = (Snapshot) offer.snapshot();
92
93         // Create a replicated log with the snapshot information
94         // The replicated log can be used later on to retrieve this snapshot
95         // when we need to install it on a peer
96
97         context.setReplicatedLog(ReplicatedLogImpl.newInstance(snapshot, context, persistence, currentBehavior));
98         context.setLastApplied(snapshot.getLastAppliedIndex());
99         context.setCommitIndex(snapshot.getLastAppliedIndex());
100
101         Stopwatch timer = Stopwatch.createStarted();
102
103         // Apply the snapshot to the actors state
104         cohort.applyRecoverySnapshot(snapshot.getState());
105
106         timer.stop();
107         log.info("Recovery snapshot applied for {} in {}: snapshotIndex={}, snapshotTerm={}, journal-size=" +
108                 replicatedLog().size(), context.getId(), timer.toString(),
109                 replicatedLog().getSnapshotIndex(), replicatedLog().getSnapshotTerm());
110     }
111
112     private void onRecoveredJournalLogEntry(ReplicatedLogEntry logEntry) {
113         if(log.isDebugEnabled()) {
114             log.debug("{}: Received ReplicatedLogEntry for recovery: {}", context.getId(), logEntry.getIndex());
115         }
116
117         replicatedLog().append(logEntry);
118     }
119
120     private void onRecoveredApplyLogEntries(long toIndex) {
121         if(log.isDebugEnabled()) {
122             log.debug("{}: Received ApplyLogEntries for recovery, applying to state: {} to {}",
123                     context.getId(), context.getLastApplied() + 1, toIndex);
124         }
125
126         for (long i = context.getLastApplied() + 1; i <= toIndex; i++) {
127             batchRecoveredLogEntry(replicatedLog().get(i));
128         }
129
130         context.setLastApplied(toIndex);
131         context.setCommitIndex(toIndex);
132     }
133
134     private void batchRecoveredLogEntry(ReplicatedLogEntry logEntry) {
135         initRecoveryTimer();
136
137         int batchSize = context.getConfigParams().getJournalRecoveryLogBatchSize();
138         if(currentRecoveryBatchCount == 0) {
139             cohort.startLogRecoveryBatch(batchSize);
140         }
141
142         cohort.appendRecoveredLogEntry(logEntry.getData());
143
144         if(++currentRecoveryBatchCount >= batchSize) {
145             endCurrentLogRecoveryBatch();
146         }
147     }
148
149     private void endCurrentLogRecoveryBatch() {
150         cohort.applyCurrentLogRecoveryBatch();
151         currentRecoveryBatchCount = 0;
152     }
153
154     private void onRecoveryCompletedMessage() {
155         if(currentRecoveryBatchCount > 0) {
156             endCurrentLogRecoveryBatch();
157         }
158
159         String recoveryTime = "";
160         if(recoveryTimer != null) {
161             recoveryTimer.stop();
162             recoveryTime = " in " + recoveryTimer.toString();
163             recoveryTimer = null;
164         }
165
166         log.info("Recovery completed" + recoveryTime + " - Switching actor to Follower - " +
167                  "Persistence Id =  " + context.getId() +
168                  " Last index in log = {}, snapshotIndex = {}, snapshotTerm = {}, " +
169                  "journal-size = {}", replicatedLog().lastIndex(), replicatedLog().getSnapshotIndex(),
170                  replicatedLog().getSnapshotTerm(), replicatedLog().size());
171     }
172 }