Bug 3161: Create new UpdateElectionTerm 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.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             log.debug("{}: Received apply journal entries for recovery, applying to state: {} to {}",
131                     context.getId(), lastUnappliedIndex, toIndex);
132         }
133
134         long lastApplied = lastUnappliedIndex - 1;
135         for (long i = lastUnappliedIndex; i <= toIndex; i++) {
136             ReplicatedLogEntry logEntry = replicatedLog().get(i);
137             if(logEntry != null) {
138                 lastApplied++;
139                 batchRecoveredLogEntry(logEntry);
140             } else {
141                 // Shouldn't happen but cover it anyway.
142                 log.error("Log entry not found for index {}", i);
143                 break;
144             }
145         }
146
147         context.setLastApplied(lastApplied);
148         context.setCommitIndex(lastApplied);
149     }
150
151     private void batchRecoveredLogEntry(ReplicatedLogEntry logEntry) {
152         initRecoveryTimer();
153
154         int batchSize = context.getConfigParams().getJournalRecoveryLogBatchSize();
155         if(currentRecoveryBatchCount == 0) {
156             cohort.startLogRecoveryBatch(batchSize);
157         }
158
159         cohort.appendRecoveredLogEntry(logEntry.getData());
160
161         if(++currentRecoveryBatchCount >= batchSize) {
162             endCurrentLogRecoveryBatch();
163         }
164     }
165
166     private void endCurrentLogRecoveryBatch() {
167         cohort.applyCurrentLogRecoveryBatch();
168         currentRecoveryBatchCount = 0;
169     }
170
171     private void onRecoveryCompletedMessage() {
172         if(currentRecoveryBatchCount > 0) {
173             endCurrentLogRecoveryBatch();
174         }
175
176         String recoveryTime = "";
177         if(recoveryTimer != null) {
178             recoveryTimer.stop();
179             recoveryTime = " in " + recoveryTimer.toString();
180             recoveryTimer = null;
181         }
182
183         log.info("Recovery completed" + recoveryTime + " - Switching actor to Follower - " +
184                  "Persistence Id =  " + context.getId() +
185                  " Last index in log = {}, snapshotIndex = {}, snapshotTerm = {}, " +
186                  "journal-size = {}", replicatedLog().lastIndex(), replicatedLog().getSnapshotIndex(),
187                  replicatedLog().getSnapshotTerm(), replicatedLog().size());
188     }
189 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.