Merge "Update lispflowmapping options in custom.properties"
[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                 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         if(log.isDebugEnabled()) {
123             log.debug("{}: Received ApplyLogEntries for recovery, applying to state: {} to {}",
124                     context.getId(), context.getLastApplied() + 1, toIndex);
125         }
126
127         for (long i = context.getLastApplied() + 1; i <= toIndex; i++) {
128             batchRecoveredLogEntry(replicatedLog().get(i));
129         }
130
131         context.setLastApplied(toIndex);
132         context.setCommitIndex(toIndex);
133     }
134
135     private void batchRecoveredLogEntry(ReplicatedLogEntry logEntry) {
136         initRecoveryTimer();
137
138         int batchSize = context.getConfigParams().getJournalRecoveryLogBatchSize();
139         if(currentRecoveryBatchCount == 0) {
140             cohort.startLogRecoveryBatch(batchSize);
141         }
142
143         cohort.appendRecoveredLogEntry(logEntry.getData());
144
145         if(++currentRecoveryBatchCount >= batchSize) {
146             endCurrentLogRecoveryBatch();
147         }
148     }
149
150     private void endCurrentLogRecoveryBatch() {
151         cohort.applyCurrentLogRecoveryBatch();
152         currentRecoveryBatchCount = 0;
153     }
154
155     private void onRecoveryCompletedMessage() {
156         if(currentRecoveryBatchCount > 0) {
157             endCurrentLogRecoveryBatch();
158         }
159
160         String recoveryTime = "";
161         if(recoveryTimer != null) {
162             recoveryTimer.stop();
163             recoveryTime = " in " + recoveryTimer.toString();
164             recoveryTimer = null;
165         }
166
167         log.info("Recovery completed" + recoveryTime + " - Switching actor to Follower - " +
168                  "Persistence Id =  " + context.getId() +
169                  " Last index in log = {}, snapshotIndex = {}, snapshotTerm = {}, " +
170                  "journal-size = {}", replicatedLog().lastIndex(), replicatedLog().getSnapshotIndex(),
171                  replicatedLog().getSnapshotTerm(), replicatedLog().size());
172     }
173 }