2 * Copyright (c) 2014 Cisco 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
9 package org.opendaylight.controller.cluster.raft.behaviors;
11 import akka.actor.ActorRef;
12 import com.google.common.annotations.VisibleForTesting;
13 import java.util.ArrayList;
14 import org.opendaylight.controller.cluster.raft.RaftActorContext;
15 import org.opendaylight.controller.cluster.raft.RaftState;
16 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
17 import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload;
18 import org.opendaylight.controller.cluster.raft.Snapshot;
19 import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot;
20 import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
21 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
22 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
23 import org.opendaylight.controller.cluster.raft.messages.InstallSnapshot;
24 import org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply;
25 import org.opendaylight.controller.cluster.raft.messages.RaftRPC;
26 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
27 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
30 * The behavior of a RaftActor in the Follower state
33 * <li> Respond to RPCs from candidates and leaders
34 * <li> If election timeout elapses without receiving AppendEntries
35 * RPC from current leader or granting vote to candidate:
36 * convert to candidate
39 public class Follower extends AbstractRaftActorBehavior {
41 private SnapshotTracker snapshotTracker = null;
43 private final SyncStatusTracker initialSyncStatusTracker;
45 private static final int SYNC_THRESHOLD = 10;
47 public Follower(RaftActorContext context) {
51 public Follower(RaftActorContext context, String initialLeaderId) {
52 super(context, RaftState.Follower);
53 leaderId = initialLeaderId;
55 initialSyncStatusTracker = new SyncStatusTracker(context.getActor(), getId(), SYNC_THRESHOLD);
57 if(canStartElection()) {
58 if (context.getPeerIds().isEmpty() && getLeaderId() == null) {
59 actor().tell(ELECTION_TIMEOUT, actor());
61 scheduleElection(electionDuration());
67 private boolean isLogEntryPresent(long index){
68 if(index == context.getReplicatedLog().getSnapshotIndex()){
72 ReplicatedLogEntry previousEntry = context.getReplicatedLog()
75 return previousEntry != null;
79 private long getLogEntryTerm(long index){
80 if(index == context.getReplicatedLog().getSnapshotIndex()){
81 return context.getReplicatedLog().getSnapshotTerm();
84 ReplicatedLogEntry previousEntry = context.getReplicatedLog()
87 if(previousEntry != null){
88 return previousEntry.getTerm();
94 private void updateInitialSyncStatus(long currentLeaderCommit, String leaderId){
95 initialSyncStatusTracker.update(leaderId, currentLeaderCommit, context.getCommitIndex());
98 @Override protected RaftActorBehavior handleAppendEntries(ActorRef sender,
99 AppendEntries appendEntries) {
101 int numLogEntries = appendEntries.getEntries() != null ? appendEntries.getEntries().size() : 0;
102 if(LOG.isTraceEnabled()) {
103 LOG.trace("{}: handleAppendEntries: {}", logName(), appendEntries);
104 } else if(LOG.isDebugEnabled() && numLogEntries > 0) {
105 LOG.debug("{}: handleAppendEntries: {}", logName(), appendEntries);
108 // TODO : Refactor this method into a bunch of smaller methods
109 // to make it easier to read. Before refactoring ensure tests
110 // cover the code properly
112 if (snapshotTracker != null || context.getSnapshotManager().isApplying()) {
113 // if snapshot install is in progress, follower should just acknowledge append entries with a reply.
114 AppendEntriesReply reply = new AppendEntriesReply(context.getId(), currentTerm(), true,
115 lastIndex(), lastTerm(), context.getPayloadVersion());
117 if(LOG.isDebugEnabled()) {
118 LOG.debug("{}: snapshot install is in progress, replying immediately with {}", logName(), reply);
120 sender.tell(reply, actor());
125 // If we got here then we do appear to be talking to the leader
126 leaderId = appendEntries.getLeaderId();
128 setLeaderPayloadVersion(appendEntries.getPayloadVersion());
130 updateInitialSyncStatus(appendEntries.getLeaderCommit(), appendEntries.getLeaderId());
131 // First check if the logs are in sync or not
132 long lastIndex = lastIndex();
134 if (isOutOfSync(appendEntries)) {
135 // We found that the log was out of sync so just send a negative
138 LOG.debug("{}: Follower is out-of-sync, so sending negative reply, lastIndex: {}, lastTerm: {}",
139 logName(), lastIndex, lastTerm());
141 sender.tell(new AppendEntriesReply(context.getId(), currentTerm(), false, lastIndex,
142 lastTerm(), context.getPayloadVersion()), actor());
146 if (appendEntries.getEntries() != null && appendEntries.getEntries().size() > 0) {
148 LOG.debug("{}: Number of entries to be appended = {}", logName(),
149 appendEntries.getEntries().size());
151 // 3. If an existing entry conflicts with a new one (same index
152 // but different terms), delete the existing entry and all that
154 int addEntriesFrom = 0;
155 if (context.getReplicatedLog().size() > 0) {
157 // Find the entry up until the one that is not in the follower's log
158 for (int i = 0;i < appendEntries.getEntries().size(); i++, addEntriesFrom++) {
159 ReplicatedLogEntry matchEntry = appendEntries.getEntries().get(i);
160 ReplicatedLogEntry newEntry = context.getReplicatedLog().get(matchEntry.getIndex());
162 if (newEntry == null) {
163 //newEntry not found in the log
167 if (newEntry.getTerm() == matchEntry.getTerm()) {
171 if(!context.getRaftPolicy().applyModificationToStateBeforeConsensus()) {
173 LOG.debug("{}: Removing entries from log starting at {}", logName(),
174 matchEntry.getIndex());
176 // Entries do not match so remove all subsequent entries
177 context.getReplicatedLog().removeFromAndPersist(matchEntry.getIndex());
180 sender.tell(new AppendEntriesReply(context.getId(), currentTerm(), false, lastIndex,
181 lastTerm(), context.getPayloadVersion(), true), actor());
187 lastIndex = lastIndex();
188 LOG.debug("{}: After cleanup entries to be added from = {}", logName(),
189 (addEntriesFrom + lastIndex));
191 // 4. Append any new entries not already in the log
192 for (int i = addEntriesFrom; i < appendEntries.getEntries().size(); i++) {
193 ReplicatedLogEntry entry = appendEntries.getEntries().get(i);
195 LOG.debug("{}: Append entry to log {}", logName(), entry.getData());
197 context.getReplicatedLog().appendAndPersist(entry);
199 if(entry.getData() instanceof ServerConfigurationPayload) {
200 context.updatePeerIds((ServerConfigurationPayload)entry.getData());
204 LOG.debug("{}: Log size is now {}", logName(), context.getReplicatedLog().size());
207 // 5. If leaderCommit > commitIndex, set commitIndex =
208 // min(leaderCommit, index of last new entry)
210 lastIndex = lastIndex();
211 long prevCommitIndex = context.getCommitIndex();
213 context.setCommitIndex(Math.min(appendEntries.getLeaderCommit(), lastIndex));
215 if (prevCommitIndex != context.getCommitIndex()) {
216 LOG.debug("{}: Commit index set to {}", logName(), context.getCommitIndex());
219 // If commitIndex > lastApplied: increment lastApplied, apply
220 // log[lastApplied] to state machine (ยง5.3)
221 // check if there are any entries to be applied. last-applied can be equal to last-index
222 if (appendEntries.getLeaderCommit() > context.getLastApplied() &&
223 context.getLastApplied() < lastIndex) {
224 if(LOG.isDebugEnabled()) {
225 LOG.debug("{}: applyLogToStateMachine, " +
226 "appendEntries.getLeaderCommit(): {}," +
227 "context.getLastApplied(): {}, lastIndex(): {}", logName(),
228 appendEntries.getLeaderCommit(), context.getLastApplied(), lastIndex);
231 applyLogToStateMachine(appendEntries.getLeaderCommit());
234 AppendEntriesReply reply = new AppendEntriesReply(context.getId(), currentTerm(), true,
235 lastIndex, lastTerm(), context.getPayloadVersion());
237 if(LOG.isTraceEnabled()) {
238 LOG.trace("{}: handleAppendEntries returning : {}", logName(), reply);
239 } else if(LOG.isDebugEnabled() && numLogEntries > 0) {
240 LOG.debug("{}: handleAppendEntries returning : {}", logName(), reply);
243 sender.tell(reply, actor());
245 if (!context.getSnapshotManager().isCapturing()) {
246 super.performSnapshotWithoutCapture(appendEntries.getReplicatedToAllIndex());
252 private boolean isOutOfSync(AppendEntries appendEntries) {
254 long prevLogTerm = getLogEntryTerm(appendEntries.getPrevLogIndex());
255 boolean prevEntryPresent = isLogEntryPresent(appendEntries.getPrevLogIndex());
256 long lastIndex = lastIndex();
257 int numLogEntries = appendEntries.getEntries() != null ? appendEntries.getEntries().size() : 0;
258 boolean outOfSync = true;
260 if (lastIndex == -1 && appendEntries.getPrevLogIndex() != -1) {
262 // The follower's log is out of sync because the leader does have
263 // an entry at prevLogIndex and this follower has no entries in
266 LOG.debug("{}: The followers log is empty and the senders prevLogIndex is {}",
267 logName(), appendEntries.getPrevLogIndex());
268 } else if (lastIndex > -1 && appendEntries.getPrevLogIndex() != -1 && !prevEntryPresent) {
270 // The follower's log is out of sync because the Leader's
271 // prevLogIndex entry was not found in it's log
273 LOG.debug("{}: The log is not empty but the prevLogIndex {} was not found in it",
274 logName(), appendEntries.getPrevLogIndex());
275 } else if (lastIndex > -1 && prevEntryPresent && prevLogTerm != appendEntries.getPrevLogTerm()) {
277 // The follower's log is out of sync because the Leader's
278 // prevLogIndex entry does exist in the follower's log but it has
279 // a different term in it
282 "{}: Cannot append entries because previous entry term {} is not equal to append entries prevLogTerm {}",
283 logName(), prevLogTerm, appendEntries.getPrevLogTerm());
284 } else if(appendEntries.getPrevLogIndex() == -1 && appendEntries.getPrevLogTerm() == -1
285 && appendEntries.getReplicatedToAllIndex() != -1
286 && !isLogEntryPresent(appendEntries.getReplicatedToAllIndex())
287 && !context.getReplicatedLog().isInSnapshot(appendEntries.getReplicatedToAllIndex())) {
288 // This append entry comes from a leader who has it's log aggressively trimmed and so does not have
289 // the previous entry in it's in-memory journal
292 "{}: Cannot append entries because the replicatedToAllIndex {} does not appear to be in the in-memory journal",
293 logName(), appendEntries.getReplicatedToAllIndex());
294 } else if(appendEntries.getPrevLogIndex() == -1 && appendEntries.getPrevLogTerm() == -1
295 && appendEntries.getReplicatedToAllIndex() != -1 && numLogEntries > 0
296 && !isLogEntryPresent(appendEntries.getEntries().get(0).getIndex() - 1)
297 && !context.getReplicatedLog().isInSnapshot(appendEntries.getEntries().get(0).getIndex() - 1)) {
299 "{}: Cannot append entries because the calculated previousIndex {} was not found in the in-memory journal",
300 logName(), appendEntries.getEntries().get(0).getIndex() - 1);
307 @Override protected RaftActorBehavior handleAppendEntriesReply(ActorRef sender,
308 AppendEntriesReply appendEntriesReply) {
312 @Override protected RaftActorBehavior handleRequestVoteReply(ActorRef sender,
313 RequestVoteReply requestVoteReply) {
317 @Override public RaftActorBehavior handleMessage(ActorRef sender, Object originalMessage) {
319 Object message = fromSerializableMessage(originalMessage);
321 if (message instanceof RaftRPC) {
322 RaftRPC rpc = (RaftRPC) message;
323 // If RPC request or response contains term T > currentTerm:
324 // set currentTerm = T, convert to follower (ยง5.1)
325 // This applies to all RPC messages and responses
326 if (rpc.getTerm() > context.getTermInformation().getCurrentTerm()) {
327 LOG.debug("{}: Term {} in \"{}\" message is greater than follower's term {} - updating term",
328 logName(), rpc.getTerm(), rpc, context.getTermInformation().getCurrentTerm());
330 context.getTermInformation().updateAndPersist(rpc.getTerm(), null);
334 if (message instanceof ElectionTimeout) {
335 if(canStartElection()) {
336 LOG.debug("{}: Received ElectionTimeout - switching to Candidate", logName());
337 return internalSwitchBehavior(RaftState.Candidate);
342 } else if (message instanceof InstallSnapshot) {
343 InstallSnapshot installSnapshot = (InstallSnapshot) message;
344 handleInstallSnapshot(sender, installSnapshot);
347 if(message instanceof RaftRPC && (!(message instanceof RequestVote) || (canGrantVote((RequestVote) message)))){
348 scheduleElection(electionDuration());
351 return super.handleMessage(sender, message);
354 private void handleInstallSnapshot(final ActorRef sender, InstallSnapshot installSnapshot) {
356 LOG.debug("{}: InstallSnapshot received from leader {}, datasize: {} , Chunk: {}/{}",
357 logName(), installSnapshot.getLeaderId(), installSnapshot.getData().size(),
358 installSnapshot.getChunkIndex(), installSnapshot.getTotalChunks());
360 leaderId = installSnapshot.getLeaderId();
362 if(snapshotTracker == null){
363 snapshotTracker = new SnapshotTracker(LOG, installSnapshot.getTotalChunks());
366 updateInitialSyncStatus(installSnapshot.getLastIncludedIndex(), installSnapshot.getLeaderId());
369 final InstallSnapshotReply reply = new InstallSnapshotReply(
370 currentTerm(), context.getId(), installSnapshot.getChunkIndex(), true);
372 if(snapshotTracker.addChunk(installSnapshot.getChunkIndex(), installSnapshot.getData(),
373 installSnapshot.getLastChunkHashCode())){
374 Snapshot snapshot = Snapshot.create(snapshotTracker.getSnapshot(),
375 new ArrayList<ReplicatedLogEntry>(),
376 installSnapshot.getLastIncludedIndex(),
377 installSnapshot.getLastIncludedTerm(),
378 installSnapshot.getLastIncludedIndex(),
379 installSnapshot.getLastIncludedTerm(),
380 context.getTermInformation().getCurrentTerm(),
381 context.getTermInformation().getVotedFor(),
382 context.getPeerServerInfo(true));
384 ApplySnapshot.Callback applySnapshotCallback = new ApplySnapshot.Callback() {
386 public void onSuccess() {
387 LOG.debug("{}: handleInstallSnapshot returning: {}", logName(), reply);
389 sender.tell(reply, actor());
393 public void onFailure() {
394 sender.tell(new InstallSnapshotReply(currentTerm(), context.getId(), -1, false), actor());
398 actor().tell(new ApplySnapshot(snapshot, applySnapshotCallback), actor());
400 snapshotTracker = null;
402 LOG.debug("{}: handleInstallSnapshot returning: {}", logName(), reply);
404 sender.tell(reply, actor());
406 } catch (SnapshotTracker.InvalidChunkException e) {
407 LOG.debug("{}: Exception in InstallSnapshot of follower", logName(), e);
409 sender.tell(new InstallSnapshotReply(currentTerm(), context.getId(),
410 -1, false), actor());
411 snapshotTracker = null;
413 } catch (Exception e){
414 LOG.error("{}: Exception in InstallSnapshot of follower", logName(), e);
416 //send reply with success as false. The chunk will be sent again on failure
417 sender.tell(new InstallSnapshotReply(currentTerm(), context.getId(),
418 installSnapshot.getChunkIndex(), false), actor());
424 public void close() throws Exception {
429 SnapshotTracker getSnapshotTracker(){
430 return snapshotTracker;