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.DataPersistenceProvider;
14 import org.opendaylight.controller.cluster.raft.RaftActor.UpdateElectionTerm;
15 import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries;
16 import org.opendaylight.controller.cluster.raft.base.messages.ApplyLogEntries;
17 import org.opendaylight.controller.cluster.raft.base.messages.DeleteEntries;
18 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
19 import org.slf4j.Logger;
22 * Support class that handles persistence recovery for a RaftActor.
24 * @author Thomas Pantelis
26 class RaftActorRecoverySupport {
27 private final DataPersistenceProvider persistence;
28 private final RaftActorContext context;
29 private final RaftActorBehavior currentBehavior;
30 private final RaftActorRecoveryCohort cohort;
32 private int currentRecoveryBatchCount;
34 private Stopwatch recoveryTimer;
35 private final Logger log;
37 RaftActorRecoverySupport(DataPersistenceProvider persistence, RaftActorContext context,
38 RaftActorBehavior currentBehavior, RaftActorRecoveryCohort cohort) {
39 this.persistence = persistence;
40 this.context = context;
41 this.currentBehavior = currentBehavior;
43 this.log = context.getLogger();
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 org.opendaylight.controller.cluster.raft.RaftActor.DeleteEntries) {
61 // Handle this message for backwards compatibility with pre-Lithium versions.
62 replicatedLog().removeFrom(((org.opendaylight.controller.cluster.raft.RaftActor.DeleteEntries) message).getFromIndex());
63 } else if (message instanceof UpdateElectionTerm) {
64 context.getTermInformation().update(((UpdateElectionTerm) message).getCurrentTerm(),
65 ((UpdateElectionTerm) message).getVotedFor());
66 } else if (message instanceof RecoveryCompleted) {
67 onRecoveryCompletedMessage();
68 recoveryComplete = true;
70 } else if (message instanceof RecoveryCompleted) {
71 recoveryComplete = true;
74 return recoveryComplete;
77 private ReplicatedLog replicatedLog() {
78 return context.getReplicatedLog();
81 private void initRecoveryTimer() {
82 if(recoveryTimer == null) {
83 recoveryTimer = Stopwatch.createStarted();
87 private void onRecoveredSnapshot(SnapshotOffer offer) {
88 if(log.isDebugEnabled()) {
89 log.debug("{}: SnapshotOffer called..", context.getId());
94 Snapshot snapshot = (Snapshot) offer.snapshot();
96 // Create a replicated log with the snapshot information
97 // The replicated log can be used later on to retrieve this snapshot
98 // when we need to install it on a peer
100 context.setReplicatedLog(ReplicatedLogImpl.newInstance(snapshot, context, persistence, currentBehavior));
101 context.setLastApplied(snapshot.getLastAppliedIndex());
102 context.setCommitIndex(snapshot.getLastAppliedIndex());
104 Stopwatch timer = Stopwatch.createStarted();
106 // Apply the snapshot to the actors state
107 cohort.applyRecoverySnapshot(snapshot.getState());
110 log.info("Recovery snapshot applied for {} in {}: snapshotIndex={}, snapshotTerm={}, journal-size={}",
111 context.getId(), timer.toString(), replicatedLog().getSnapshotIndex(),
112 replicatedLog().getSnapshotTerm(), replicatedLog().size());
115 private void onRecoveredJournalLogEntry(ReplicatedLogEntry logEntry) {
116 if(log.isDebugEnabled()) {
117 log.debug("{}: Received ReplicatedLogEntry for recovery: index: {}, size: {}", context.getId(),
118 logEntry.getIndex(), logEntry.size());
121 replicatedLog().append(logEntry);
124 private void onRecoveredApplyLogEntries(long toIndex) {
125 long lastUnappliedIndex = context.getLastApplied() + 1;
127 if(log.isDebugEnabled()) {
128 log.debug("{}: Received apply journal entries for recovery, applying to state: {} to {}",
129 context.getId(), lastUnappliedIndex, toIndex);
132 long lastApplied = lastUnappliedIndex - 1;
133 for (long i = lastUnappliedIndex; i <= toIndex; i++) {
134 ReplicatedLogEntry logEntry = replicatedLog().get(i);
135 if(logEntry != null) {
137 batchRecoveredLogEntry(logEntry);
139 // Shouldn't happen but cover it anyway.
140 log.error("Log entry not found for index {}", i);
145 context.setLastApplied(lastApplied);
146 context.setCommitIndex(lastApplied);
149 private void batchRecoveredLogEntry(ReplicatedLogEntry logEntry) {
152 int batchSize = context.getConfigParams().getJournalRecoveryLogBatchSize();
153 if(currentRecoveryBatchCount == 0) {
154 cohort.startLogRecoveryBatch(batchSize);
157 cohort.appendRecoveredLogEntry(logEntry.getData());
159 if(++currentRecoveryBatchCount >= batchSize) {
160 endCurrentLogRecoveryBatch();
164 private void endCurrentLogRecoveryBatch() {
165 cohort.applyCurrentLogRecoveryBatch();
166 currentRecoveryBatchCount = 0;
169 private void onRecoveryCompletedMessage() {
170 if(currentRecoveryBatchCount > 0) {
171 endCurrentLogRecoveryBatch();
174 String recoveryTime = "";
175 if(recoveryTimer != null) {
176 recoveryTimer.stop();
177 recoveryTime = " in " + recoveryTimer.toString();
178 recoveryTimer = null;
181 log.info("Recovery completed" + recoveryTime + " - Switching actor to Follower - " +
182 "Persistence Id = " + context.getId() +
183 " Last index in log = {}, snapshotIndex = {}, snapshotTerm = {}, " +
184 "journal-size = {}", replicatedLog().lastIndex(), replicatedLog().getSnapshotIndex(),
185 replicatedLog().getSnapshotTerm(), replicatedLog().size());