Merge "Cleanup: Remove passing around of DataPersistenceProvider"
[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.raft.RaftActor.UpdateElectionTerm;
14 import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries;
15 import org.opendaylight.controller.cluster.raft.base.messages.ApplyLogEntries;
16 import org.opendaylight.controller.cluster.raft.base.messages.DeleteEntries;
17 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
18 import org.slf4j.Logger;
19
20 /**
21  * Support class that handles persistence recovery for a RaftActor.
22  *
23  * @author Thomas Pantelis
24  */
25 class RaftActorRecoverySupport {
26     private final RaftActorContext context;
27     private final RaftActorBehavior currentBehavior;
28     private final RaftActorRecoveryCohort cohort;
29
30     private int currentRecoveryBatchCount;
31
32     private Stopwatch recoveryTimer;
33     private final Logger log;
34
35     RaftActorRecoverySupport(RaftActorContext context, RaftActorBehavior currentBehavior,
36             RaftActorRecoveryCohort cohort) {
37         this.context = context;
38         this.currentBehavior = currentBehavior;
39         this.cohort = cohort;
40         this.log = context.getLogger();
41     }
42
43     boolean handleRecoveryMessage(Object message) {
44         boolean recoveryComplete = false;
45         if(context.getPersistenceProvider().isRecoveryApplicable()) {
46             if (message instanceof SnapshotOffer) {
47                 onRecoveredSnapshot((SnapshotOffer) message);
48             } else if (message instanceof ReplicatedLogEntry) {
49                 onRecoveredJournalLogEntry((ReplicatedLogEntry) message);
50             } else if (message instanceof ApplyLogEntries) {
51                 // Handle this message for backwards compatibility with pre-Lithium versions.
52                 onRecoveredApplyLogEntries(((ApplyLogEntries) message).getToIndex());
53             } else if (message instanceof ApplyJournalEntries) {
54                 onRecoveredApplyLogEntries(((ApplyJournalEntries) message).getToIndex());
55             } else if (message instanceof DeleteEntries) {
56                 replicatedLog().removeFrom(((DeleteEntries) message).getFromIndex());
57             } else if (message instanceof org.opendaylight.controller.cluster.raft.RaftActor.DeleteEntries) {
58                 // Handle this message for backwards compatibility with pre-Lithium versions.
59                 replicatedLog().removeFrom(((org.opendaylight.controller.cluster.raft.RaftActor.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, 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                 context.getId(), timer.toString(), replicatedLog().getSnapshotIndex(),
109                 replicatedLog().getSnapshotTerm(), replicatedLog().size());
110     }
111
112     private void onRecoveredJournalLogEntry(ReplicatedLogEntry logEntry) {
113         if(log.isDebugEnabled()) {
114             log.debug("{}: Received ReplicatedLogEntry for recovery: index: {}, size: {}", context.getId(),
115                     logEntry.getIndex(), logEntry.size());
116         }
117
118         replicatedLog().append(logEntry);
119     }
120
121     private void onRecoveredApplyLogEntries(long toIndex) {
122         long lastUnappliedIndex = context.getLastApplied() + 1;
123
124         if(log.isDebugEnabled()) {
125             log.debug("{}: Received apply journal entries for recovery, applying to state: {} to {}",
126                     context.getId(), lastUnappliedIndex, toIndex);
127         }
128
129         long lastApplied = lastUnappliedIndex - 1;
130         for (long i = lastUnappliedIndex; i <= toIndex; i++) {
131             ReplicatedLogEntry logEntry = replicatedLog().get(i);
132             if(logEntry != null) {
133                 lastApplied++;
134                 batchRecoveredLogEntry(logEntry);
135             } else {
136                 // Shouldn't happen but cover it anyway.
137                 log.error("Log entry not found for index {}", i);
138                 break;
139             }
140         }
141
142         context.setLastApplied(lastApplied);
143         context.setCommitIndex(lastApplied);
144     }
145
146     private void batchRecoveredLogEntry(ReplicatedLogEntry logEntry) {
147         initRecoveryTimer();
148
149         int batchSize = context.getConfigParams().getJournalRecoveryLogBatchSize();
150         if(currentRecoveryBatchCount == 0) {
151             cohort.startLogRecoveryBatch(batchSize);
152         }
153
154         cohort.appendRecoveredLogEntry(logEntry.getData());
155
156         if(++currentRecoveryBatchCount >= batchSize) {
157             endCurrentLogRecoveryBatch();
158         }
159     }
160
161     private void endCurrentLogRecoveryBatch() {
162         cohort.applyCurrentLogRecoveryBatch();
163         currentRecoveryBatchCount = 0;
164     }
165
166     private void onRecoveryCompletedMessage() {
167         if(currentRecoveryBatchCount > 0) {
168             endCurrentLogRecoveryBatch();
169         }
170
171         String recoveryTime = "";
172         if(recoveryTimer != null) {
173             recoveryTimer.stop();
174             recoveryTime = " in " + recoveryTimer.toString();
175             recoveryTimer = null;
176         }
177
178         log.info("Recovery completed" + recoveryTime + " - Switching actor to Follower - " +
179                  "Persistence Id =  " + context.getId() +
180                  " Last index in log = {}, snapshotIndex = {}, snapshotTerm = {}, " +
181                  "journal-size = {}", replicatedLog().lastIndex(), replicatedLog().getSnapshotIndex(),
182                  replicatedLog().getSnapshotTerm(), replicatedLog().size());
183     }
184 }