2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft;
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;
21 * Support class that handles persistence recovery for a RaftActor.
23 * @author Thomas Pantelis
25 class RaftActorRecoverySupport {
26 private final RaftActorContext context;
27 private final RaftActorBehavior currentBehavior;
28 private final RaftActorRecoveryCohort cohort;
30 private int currentRecoveryBatchCount;
32 private Stopwatch recoveryTimer;
33 private final Logger log;
35 RaftActorRecoverySupport(RaftActorContext context, RaftActorBehavior currentBehavior,
36 RaftActorRecoveryCohort cohort) {
37 this.context = context;
38 this.currentBehavior = currentBehavior;
40 this.log = context.getLogger();
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;
72 } else if (message instanceof RecoveryCompleted) {
73 recoveryComplete = true;
76 return recoveryComplete;
79 private ReplicatedLog replicatedLog() {
80 return context.getReplicatedLog();
83 private void initRecoveryTimer() {
84 if(recoveryTimer == null) {
85 recoveryTimer = Stopwatch.createStarted();
89 private void onRecoveredSnapshot(SnapshotOffer offer) {
90 if(log.isDebugEnabled()) {
91 log.debug("{}: SnapshotOffer called..", context.getId());
96 Snapshot snapshot = (Snapshot) offer.snapshot();
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
102 context.setReplicatedLog(ReplicatedLogImpl.newInstance(snapshot, context, currentBehavior));
103 context.setLastApplied(snapshot.getLastAppliedIndex());
104 context.setCommitIndex(snapshot.getLastAppliedIndex());
106 Stopwatch timer = Stopwatch.createStarted();
108 // Apply the snapshot to the actors state
109 cohort.applyRecoverySnapshot(snapshot.getState());
112 log.info("Recovery snapshot applied for {} in {}: snapshotIndex={}, snapshotTerm={}, journal-size={}",
113 context.getId(), timer.toString(), replicatedLog().getSnapshotIndex(),
114 replicatedLog().getSnapshotTerm(), replicatedLog().size());
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());
123 replicatedLog().append(logEntry);
126 private void onRecoveredApplyLogEntries(long toIndex) {
127 long lastUnappliedIndex = context.getLastApplied() + 1;
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);
136 long lastApplied = lastUnappliedIndex - 1;
137 for (long i = lastUnappliedIndex; i <= toIndex; i++) {
138 ReplicatedLogEntry logEntry = replicatedLog().get(i);
139 if(logEntry != null) {
141 batchRecoveredLogEntry(logEntry);
143 // Shouldn't happen but cover it anyway.
144 log.error("Log entry not found for index {}", i);
149 context.setLastApplied(lastApplied);
150 context.setCommitIndex(lastApplied);
153 private void batchRecoveredLogEntry(ReplicatedLogEntry logEntry) {
156 int batchSize = context.getConfigParams().getJournalRecoveryLogBatchSize();
157 if(currentRecoveryBatchCount == 0) {
158 cohort.startLogRecoveryBatch(batchSize);
161 cohort.appendRecoveredLogEntry(logEntry.getData());
163 if(++currentRecoveryBatchCount >= batchSize) {
164 endCurrentLogRecoveryBatch();
168 private void endCurrentLogRecoveryBatch() {
169 cohort.applyCurrentLogRecoveryBatch();
170 currentRecoveryBatchCount = 0;
173 private void onRecoveryCompletedMessage() {
174 if(currentRecoveryBatchCount > 0) {
175 endCurrentLogRecoveryBatch();
178 String recoveryTime = "";
179 if(recoveryTimer != null) {
180 recoveryTimer.stop();
181 recoveryTime = " in " + recoveryTimer.toString();
182 recoveryTimer = null;
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());