Bug:3260-Recovery misses flows installed on single node
[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.base.messages.ApplyJournalEntries;
14 import org.opendaylight.controller.cluster.raft.base.messages.ApplyLogEntries;
15 import org.opendaylight.controller.cluster.raft.base.messages.DeleteEntries;
16 import org.opendaylight.controller.cluster.raft.base.messages.UpdateElectionTerm;
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 org.opendaylight.controller.cluster.raft.RaftActor.UpdateElectionTerm) {
61                 // Handle this message for backwards compatibility with pre-Lithium versions.
62                 org.opendaylight.controller.cluster.raft.RaftActor.UpdateElectionTerm update =
63                         (org.opendaylight.controller.cluster.raft.RaftActor.UpdateElectionTerm)message;
64                 context.getTermInformation().update(update.getCurrentTerm(), update.getVotedFor());
65             } else if (message instanceof UpdateElectionTerm) {
66                 context.getTermInformation().update(((UpdateElectionTerm) message).getCurrentTerm(),
67                         ((UpdateElectionTerm) message).getVotedFor());
68             } else if (message instanceof RecoveryCompleted) {
69                 onRecoveryCompletedMessage();
70                 recoveryComplete = true;
71             }
72         } else if (message instanceof RecoveryCompleted) {
73             recoveryComplete = true;
74         }
75
76         return recoveryComplete;
77     }
78
79     private ReplicatedLog replicatedLog() {
80         return context.getReplicatedLog();
81     }
82
83     private void initRecoveryTimer() {
84         if(recoveryTimer == null) {
85             recoveryTimer = Stopwatch.createStarted();
86         }
87     }
88
89     private void onRecoveredSnapshot(SnapshotOffer offer) {
90         if(log.isDebugEnabled()) {
91             log.debug("{}: SnapshotOffer called..", context.getId());
92         }
93
94         initRecoveryTimer();
95
96         Snapshot snapshot = (Snapshot) offer.snapshot();
97
98         // Create a replicated log with the snapshot information
99         // The replicated log can be used later on to retrieve this snapshot
100         // when we need to install it on a peer
101
102         context.setReplicatedLog(ReplicatedLogImpl.newInstance(snapshot, context, currentBehavior));
103         context.setLastApplied(snapshot.getLastAppliedIndex());
104         context.setCommitIndex(snapshot.getLastAppliedIndex());
105
106         Stopwatch timer = Stopwatch.createStarted();
107
108         // Apply the snapshot to the actors state
109         cohort.applyRecoverySnapshot(snapshot.getState());
110
111         timer.stop();
112         log.info("Recovery snapshot applied for {} in {}: snapshotIndex={}, snapshotTerm={}, journal-size={}",
113                 context.getId(), timer.toString(), replicatedLog().getSnapshotIndex(),
114                 replicatedLog().getSnapshotTerm(), replicatedLog().size());
115     }
116
117     private void onRecoveredJournalLogEntry(ReplicatedLogEntry logEntry) {
118         if(log.isDebugEnabled()) {
119             log.debug("{}: Received ReplicatedLogEntry for recovery: index: {}, size: {}", context.getId(),
120                     logEntry.getIndex(), logEntry.size());
121         }
122
123         replicatedLog().append(logEntry);
124     }
125
126     private void onRecoveredApplyLogEntries(long toIndex) {
127         long lastUnappliedIndex = context.getLastApplied() + 1;
128
129         if(log.isDebugEnabled()) {
130             // it can happen that lastUnappliedIndex > toIndex, if the AJE is in the persistent journal
131             // but the entry itself has made it to that state and recovered via the snapshot
132             log.debug("{}: Received apply journal entries for recovery, applying to state: {} to {}",
133                     context.getId(), lastUnappliedIndex, toIndex);
134         }
135
136         long lastApplied = lastUnappliedIndex - 1;
137         for (long i = lastUnappliedIndex; i <= toIndex; i++) {
138             ReplicatedLogEntry logEntry = replicatedLog().get(i);
139             if(logEntry != null) {
140                 lastApplied++;
141                 batchRecoveredLogEntry(logEntry);
142             } else {
143                 // Shouldn't happen but cover it anyway.
144                 log.error("Log entry not found for index {}", i);
145                 break;
146             }
147         }
148
149         context.setLastApplied(lastApplied);
150         context.setCommitIndex(lastApplied);
151     }
152
153     private void batchRecoveredLogEntry(ReplicatedLogEntry logEntry) {
154         initRecoveryTimer();
155
156         int batchSize = context.getConfigParams().getJournalRecoveryLogBatchSize();
157         if(currentRecoveryBatchCount == 0) {
158             cohort.startLogRecoveryBatch(batchSize);
159         }
160
161         cohort.appendRecoveredLogEntry(logEntry.getData());
162
163         if(++currentRecoveryBatchCount >= batchSize) {
164             endCurrentLogRecoveryBatch();
165         }
166     }
167
168     private void endCurrentLogRecoveryBatch() {
169         cohort.applyCurrentLogRecoveryBatch();
170         currentRecoveryBatchCount = 0;
171     }
172
173     private void onRecoveryCompletedMessage() {
174         if(currentRecoveryBatchCount > 0) {
175             endCurrentLogRecoveryBatch();
176         }
177
178         String recoveryTime = "";
179         if(recoveryTimer != null) {
180             recoveryTimer.stop();
181             recoveryTime = " in " + recoveryTimer.toString();
182             recoveryTimer = null;
183         }
184
185         log.info("Recovery completed" + recoveryTime + " - Switching actor to Follower - " +
186                  "Persistence Id =  " + context.getId() +
187                  " Last index in log = {}, snapshotIndex = {}, snapshotTerm = {}, " +
188                  "journal-size = {}", replicatedLog().lastIndex(), replicatedLog().getSnapshotIndex(),
189                  replicatedLog().getSnapshotTerm(), replicatedLog().size());
190     }
191 }