Merge "Bug 1392: Change ReadTransaction#read to return CheckedFuture"
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / Follower.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.raft.behaviors;
10
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;
22
23 /**
24  * The behavior of a RaftActor in the Follower state
25  * <p/>
26  * <ul>
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
31  * </ul>
32  */
33 public class Follower extends AbstractRaftActorBehavior {
34     public Follower(RaftActorContext context) {
35         super(context);
36
37         scheduleElection(electionDuration());
38     }
39
40     @Override protected RaftState handleAppendEntries(ActorRef sender,
41         AppendEntries appendEntries) {
42
43         if(appendEntries.getEntries() != null && appendEntries.getEntries().size() > 0) {
44             context.getLogger()
45                 .info("Follower: Received {}", appendEntries.toString());
46         }
47
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
51
52         // 1. Reply false if term < currentTerm (§5.1)
53         // This is handled in the appendEntries method of the base class
54
55         // If we got here then we do appear to be talking to the leader
56         leaderId = appendEntries.getLeaderId();
57
58         // 2. Reply false if log doesn’t contain an entry at prevLogIndex
59         // whose term matches prevLogTerm (§5.3)
60
61         ReplicatedLogEntry previousEntry = context.getReplicatedLog()
62             .get(appendEntries.getPrevLogIndex());
63
64
65         boolean outOfSync = true;
66
67         // First check if the logs are in sync or not
68         if (lastIndex() == -1
69             && appendEntries.getPrevLogIndex() != -1) {
70
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
73             // it's log.
74
75             context.getLogger().debug(
76                 "The followers log is empty and the senders prevLogIndex is {}",
77                 appendEntries.getPrevLogIndex());
78
79         } else if (lastIndex() > -1
80             && appendEntries.getPrevLogIndex() != -1
81             && previousEntry == null) {
82
83             // The follower's log is out of sync because the Leader's
84             // prevLogIndex entry was not found in it's log
85
86             context.getLogger().debug(
87                 "The log is not empty but the prevLogIndex {} was not found in it",
88                 appendEntries.getPrevLogIndex());
89
90         } else if (lastIndex() > -1
91             && previousEntry != null
92             && previousEntry.getTerm()!= appendEntries.getPrevLogTerm()) {
93
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
97
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());
102         } else {
103             outOfSync = false;
104         }
105
106         if (outOfSync) {
107             // We found that the log was out of sync so just send a negative
108             // reply and return
109             sender.tell(
110                 new AppendEntriesReply(context.getId(), currentTerm(), false,
111                     lastIndex(), lastTerm()), actor()
112             );
113             return state();
114         }
115
116         if (appendEntries.getEntries() != null
117             && appendEntries.getEntries().size() > 0) {
118             context.getLogger().debug(
119                 "Number of entries to be appended = " + appendEntries
120                     .getEntries().size()
121             );
122
123             // 3. If an existing entry conflicts with a new one (same index
124             // but different terms), delete the existing entry and all that
125             // follow it (§5.3)
126             int addEntriesFrom = 0;
127             if (context.getReplicatedLog().size() > 0) {
128
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());
133
134                     if (newEntry == null) {
135                         //newEntry not found in the log
136                         break;
137                     }
138
139                     if (newEntry.getTerm() == matchEntry
140                         .getTerm()) {
141                         continue;
142                     }
143
144                     context.getLogger().debug(
145                         "Removing entries from log starting at "
146                             + matchEntry.getIndex()
147                     );
148
149                     // Entries do not match so remove all subsequent entries
150                     context.getReplicatedLog()
151                         .removeFromAndPersist(matchEntry.getIndex());
152                     break;
153                 }
154             }
155
156             context.getLogger().debug(
157                 "After cleanup entries to be added from = " + (addEntriesFrom
158                     + lastIndex())
159             );
160
161             // 4. Append any new entries not already in the log
162             for (int i = addEntriesFrom;
163                  i < appendEntries.getEntries().size(); i++) {
164
165                 context.getLogger().info(
166                     "Append entry to log " + appendEntries.getEntries().get(
167                         i).getData()
168                         .toString()
169                 );
170                 context.getReplicatedLog()
171                     .appendAndPersist(appendEntries.getEntries().get(i));
172             }
173
174             context.getLogger().debug(
175                 "Log size is now " + context.getReplicatedLog().size());
176         }
177
178
179         // 5. If leaderCommit > commitIndex, set commitIndex =
180         // min(leaderCommit, index of last new entry)
181
182         long prevCommitIndex = context.getCommitIndex();
183
184         context.setCommitIndex(Math.min(appendEntries.getLeaderCommit(),
185             context.getReplicatedLog().lastIndex()));
186
187         if (prevCommitIndex != context.getCommitIndex()) {
188             context.getLogger()
189                 .debug("Commit index set to " + context.getCommitIndex());
190         }
191
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());
196         }
197
198         sender.tell(new AppendEntriesReply(context.getId(), currentTerm(), true,
199             lastIndex(), lastTerm()), actor());
200
201         return state();
202     }
203
204     @Override protected RaftState handleAppendEntriesReply(ActorRef sender,
205         AppendEntriesReply appendEntriesReply) {
206         return state();
207     }
208
209     @Override protected RaftState handleRequestVoteReply(ActorRef sender,
210         RequestVoteReply requestVoteReply) {
211         return state();
212     }
213
214     @Override public RaftState state() {
215         return RaftState.Follower;
216     }
217
218     @Override public RaftState handleMessage(ActorRef sender, Object originalMessage) {
219
220         Object message = fromSerializableMessage(originalMessage);
221
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);
229             }
230         }
231
232         if (message instanceof ElectionTimeout) {
233             return RaftState.Candidate;
234
235         } else if (message instanceof InstallSnapshot) {
236             InstallSnapshot installSnapshot = (InstallSnapshot) message;
237             actor().tell(new ApplySnapshot(installSnapshot.getData()), actor());
238         }
239
240         scheduleElection(electionDuration());
241
242         return super.handleMessage(sender, message);
243     }
244
245     @Override public void close() throws Exception {
246         stopElection();
247     }
248 }