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.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;
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 UpdateElectionTerm) {
61 context.getTermInformation().update(((UpdateElectionTerm) message).getCurrentTerm(),
62 ((UpdateElectionTerm) message).getVotedFor());
63 } else if (message instanceof RecoveryCompleted) {
64 onRecoveryCompletedMessage();
65 recoveryComplete = true;
67 } else if (message instanceof RecoveryCompleted) {
68 recoveryComplete = true;
71 return recoveryComplete;
74 private ReplicatedLog replicatedLog() {
75 return context.getReplicatedLog();
78 private void initRecoveryTimer() {
79 if(recoveryTimer == null) {
80 recoveryTimer = Stopwatch.createStarted();
84 private void onRecoveredSnapshot(SnapshotOffer offer) {
85 if(log.isDebugEnabled()) {
86 log.debug("{}: SnapshotOffer called..", context.getId());
91 Snapshot snapshot = (Snapshot) offer.snapshot();
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
97 context.setReplicatedLog(ReplicatedLogImpl.newInstance(snapshot, context, currentBehavior));
98 context.setLastApplied(snapshot.getLastAppliedIndex());
99 context.setCommitIndex(snapshot.getLastAppliedIndex());
101 Stopwatch timer = Stopwatch.createStarted();
103 // Apply the snapshot to the actors state
104 cohort.applyRecoverySnapshot(snapshot.getState());
107 log.info("Recovery snapshot applied for {} in {}: snapshotIndex={}, snapshotTerm={}, journal-size={}",
108 context.getId(), timer.toString(), replicatedLog().getSnapshotIndex(),
109 replicatedLog().getSnapshotTerm(), replicatedLog().size());
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());
118 replicatedLog().append(logEntry);
121 private void onRecoveredApplyLogEntries(long toIndex) {
122 long lastUnappliedIndex = context.getLastApplied() + 1;
124 if(log.isDebugEnabled()) {
125 log.debug("{}: Received apply journal entries for recovery, applying to state: {} to {}",
126 context.getId(), lastUnappliedIndex, toIndex);
129 long lastApplied = lastUnappliedIndex - 1;
130 for (long i = lastUnappliedIndex; i <= toIndex; i++) {
131 ReplicatedLogEntry logEntry = replicatedLog().get(i);
132 if(logEntry != null) {
134 batchRecoveredLogEntry(logEntry);
136 // Shouldn't happen but cover it anyway.
137 log.error("Log entry not found for index {}", i);
142 context.setLastApplied(lastApplied);
143 context.setCommitIndex(lastApplied);
146 private void batchRecoveredLogEntry(ReplicatedLogEntry logEntry) {
149 int batchSize = context.getConfigParams().getJournalRecoveryLogBatchSize();
150 if(currentRecoveryBatchCount == 0) {
151 cohort.startLogRecoveryBatch(batchSize);
154 cohort.appendRecoveredLogEntry(logEntry.getData());
156 if(++currentRecoveryBatchCount >= batchSize) {
157 endCurrentLogRecoveryBatch();
161 private void endCurrentLogRecoveryBatch() {
162 cohort.applyCurrentLogRecoveryBatch();
163 currentRecoveryBatchCount = 0;
166 private void onRecoveryCompletedMessage() {
167 if(currentRecoveryBatchCount > 0) {
168 endCurrentLogRecoveryBatch();
171 String recoveryTime = "";
172 if(recoveryTimer != null) {
173 recoveryTimer.stop();
174 recoveryTime = " in " + recoveryTimer.toString();
175 recoveryTimer = null;
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());