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 org.opendaylight.controller.cluster.raft.RaftActorContext;
13 import org.opendaylight.controller.cluster.raft.RaftState;
14 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
15 import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot;
16 import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
17 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
18 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
19 import org.opendaylight.controller.cluster.raft.messages.InstallSnapshot;
20 import org.opendaylight.controller.cluster.raft.messages.RaftRPC;
21 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
24 * The behavior of a RaftActor in the Follower state
27 * <li> Respond to RPCs from candidates and leaders
28 * <li> If election timeout elapses without receiving AppendEntries
29 * RPC from current leader or granting vote to candidate:
30 * convert to candidate
33 public class Follower extends AbstractRaftActorBehavior {
34 public Follower(RaftActorContext context) {
37 scheduleElection(electionDuration());
40 @Override protected RaftState handleAppendEntries(ActorRef sender,
41 AppendEntries appendEntries) {
43 if(appendEntries.getEntries() != null && appendEntries.getEntries().size() > 0) {
45 .debug(appendEntries.toString());
48 // TODO : Refactor this method into a bunch of smaller methods
49 // to make it easier to read. Before refactoring ensure tests
50 // cover the code properly
52 // 1. Reply false if term < currentTerm (§5.1)
53 // This is handled in the appendEntries method of the base class
55 // If we got here then we do appear to be talking to the leader
56 leaderId = appendEntries.getLeaderId();
58 // 2. Reply false if log doesn’t contain an entry at prevLogIndex
59 // whose term matches prevLogTerm (§5.3)
61 ReplicatedLogEntry previousEntry = context.getReplicatedLog()
62 .get(appendEntries.getPrevLogIndex());
65 boolean outOfSync = true;
67 // First check if the logs are in sync or not
69 && appendEntries.getPrevLogIndex() != -1) {
71 // The follower's log is out of sync because the leader does have
72 // an entry at prevLogIndex and this follower has no entries in
75 context.getLogger().debug(
76 "The followers log is empty and the senders prevLogIndex is {}",
77 appendEntries.getPrevLogIndex());
79 } else if (lastIndex() > -1
80 && appendEntries.getPrevLogIndex() != -1
81 && previousEntry == null) {
83 // The follower's log is out of sync because the Leader's
84 // prevLogIndex entry was not found in it's log
86 context.getLogger().debug(
87 "The log is not empty but the prevLogIndex {} was not found in it",
88 appendEntries.getPrevLogIndex());
90 } else if (lastIndex() > -1
91 && previousEntry != null
92 && previousEntry.getTerm()!= appendEntries.getPrevLogTerm()) {
94 // The follower's log is out of sync because the Leader's
95 // prevLogIndex entry does exist in the follower's log but it has
96 // a different term in it
98 context.getLogger().debug(
99 "Cannot append entries because previous entry term {} is not equal to append entries prevLogTerm {}"
100 , previousEntry.getTerm()
101 , appendEntries.getPrevLogTerm());
107 // We found that the log was out of sync so just send a negative
110 new AppendEntriesReply(context.getId(), currentTerm(), false,
111 lastIndex(), lastTerm()), actor()
116 if (appendEntries.getEntries() != null
117 && appendEntries.getEntries().size() > 0) {
118 context.getLogger().debug(
119 "Number of entries to be appended = " + appendEntries
123 // 3. If an existing entry conflicts with a new one (same index
124 // but different terms), delete the existing entry and all that
126 int addEntriesFrom = 0;
127 if (context.getReplicatedLog().size() > 0) {
129 // Find the entry up until which the one that is not in the follower's log
130 for (int i = 0;i < appendEntries.getEntries().size(); i++, addEntriesFrom++) {
131 ReplicatedLogEntry matchEntry = appendEntries.getEntries().get(i);
132 ReplicatedLogEntry newEntry = context.getReplicatedLog().get(matchEntry.getIndex());
134 if (newEntry == null) {
135 //newEntry not found in the log
139 if (newEntry.getTerm() == matchEntry
144 context.getLogger().debug(
145 "Removing entries from log starting at "
146 + matchEntry.getIndex()
149 // Entries do not match so remove all subsequent entries
150 context.getReplicatedLog()
151 .removeFromAndPersist(matchEntry.getIndex());
156 context.getLogger().debug(
157 "After cleanup entries to be added from = " + (addEntriesFrom
161 // 4. Append any new entries not already in the log
162 for (int i = addEntriesFrom;
163 i < appendEntries.getEntries().size(); i++) {
165 context.getLogger().info(
166 "Append entry to log " + appendEntries.getEntries().get(
170 context.getReplicatedLog()
171 .appendAndPersist(appendEntries.getEntries().get(i));
174 context.getLogger().debug(
175 "Log size is now " + context.getReplicatedLog().size());
179 // 5. If leaderCommit > commitIndex, set commitIndex =
180 // min(leaderCommit, index of last new entry)
182 long prevCommitIndex = context.getCommitIndex();
184 context.setCommitIndex(Math.min(appendEntries.getLeaderCommit(),
185 context.getReplicatedLog().lastIndex()));
187 if (prevCommitIndex != context.getCommitIndex()) {
189 .debug("Commit index set to " + context.getCommitIndex());
192 // If commitIndex > lastApplied: increment lastApplied, apply
193 // log[lastApplied] to state machine (§5.3)
194 if (appendEntries.getLeaderCommit() > context.getLastApplied()) {
195 applyLogToStateMachine(appendEntries.getLeaderCommit());
198 sender.tell(new AppendEntriesReply(context.getId(), currentTerm(), true,
199 lastIndex(), lastTerm()), actor());
204 @Override protected RaftState handleAppendEntriesReply(ActorRef sender,
205 AppendEntriesReply appendEntriesReply) {
209 @Override protected RaftState handleRequestVoteReply(ActorRef sender,
210 RequestVoteReply requestVoteReply) {
214 @Override public RaftState state() {
215 return RaftState.Follower;
218 @Override public RaftState handleMessage(ActorRef sender, Object originalMessage) {
220 Object message = fromSerializableMessage(originalMessage);
222 if (message instanceof RaftRPC) {
223 RaftRPC rpc = (RaftRPC) message;
224 // If RPC request or response contains term T > currentTerm:
225 // set currentTerm = T, convert to follower (§5.1)
226 // This applies to all RPC messages and responses
227 if (rpc.getTerm() > context.getTermInformation().getCurrentTerm()) {
228 context.getTermInformation().updateAndPersist(rpc.getTerm(), null);
232 if (message instanceof ElectionTimeout) {
233 return RaftState.Candidate;
235 } else if (message instanceof InstallSnapshot) {
236 InstallSnapshot installSnapshot = (InstallSnapshot) message;
237 actor().tell(new ApplySnapshot(installSnapshot.getData()), actor());
240 scheduleElection(electionDuration());
242 return super.handleMessage(sender, message);
245 @Override public void close() throws Exception {