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 akka.actor.Cancellable;
13 import akka.event.LoggingAdapter;
14 import java.util.Random;
15 import java.util.concurrent.TimeUnit;
16 import org.opendaylight.controller.cluster.raft.ClientRequestTracker;
17 import org.opendaylight.controller.cluster.raft.RaftActorContext;
18 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
19 import org.opendaylight.controller.cluster.raft.SerializationUtils;
20 import org.opendaylight.controller.cluster.raft.base.messages.ApplyLogEntries;
21 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
22 import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
23 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
24 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
25 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
26 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
27 import scala.concurrent.duration.FiniteDuration;
30 * Abstract class that represents the behavior of a RaftActor
34 * <li> If commitIndex > lastApplied: increment lastApplied, apply
35 * log[lastApplied] to state machine (§5.3)
36 * <li> If RPC request or response contains term T > currentTerm:
37 * set currentTerm = T, convert to follower (§5.1)
39 public abstract class AbstractRaftActorBehavior implements RaftActorBehavior {
42 * Information about the RaftActor whose behavior this class represents
44 protected final RaftActorContext context;
49 protected final LoggingAdapter LOG;
54 private Cancellable electionCancel = null;
59 protected String leaderId = null;
62 protected AbstractRaftActorBehavior(RaftActorContext context) {
63 this.context = context;
64 this.LOG = context.getLogger();
68 * Derived classes should not directly handle AppendEntries messages it
69 * should let the base class handle it first. Once the base class handles
70 * the AppendEntries message and does the common actions that are applicable
71 * in all RaftState's it will delegate the handling of the AppendEntries
72 * message to the derived class to do more state specific handling by calling
75 * @param sender The actor that sent this message
76 * @param appendEntries The AppendEntries message
79 protected abstract RaftActorBehavior handleAppendEntries(ActorRef sender,
80 AppendEntries appendEntries);
84 * appendEntries first processes the AppendEntries message and then
85 * delegates handling to a specific behavior
88 * @param appendEntries
91 protected RaftActorBehavior appendEntries(ActorRef sender,
92 AppendEntries appendEntries) {
94 // 1. Reply false if term < currentTerm (§5.1)
95 if (appendEntries.getTerm() < currentTerm()) {
96 if(LOG.isDebugEnabled()) {
97 LOG.debug("Cannot append entries because sender term {} is less than {}",
98 appendEntries.getTerm(), currentTerm());
102 new AppendEntriesReply(context.getId(), currentTerm(), false,
103 lastIndex(), lastTerm()), actor()
109 return handleAppendEntries(sender, appendEntries);
113 * Derived classes should not directly handle AppendEntriesReply messages it
114 * should let the base class handle it first. Once the base class handles
115 * the AppendEntriesReply message and does the common actions that are
116 * applicable in all RaftState's it will delegate the handling of the
117 * AppendEntriesReply message to the derived class to do more state specific
118 * handling by calling this method
120 * @param sender The actor that sent this message
121 * @param appendEntriesReply The AppendEntriesReply message
124 protected abstract RaftActorBehavior handleAppendEntriesReply(ActorRef sender,
125 AppendEntriesReply appendEntriesReply);
128 * requestVote handles the RequestVote message. This logic is common
135 protected RaftActorBehavior requestVote(ActorRef sender,
136 RequestVote requestVote) {
138 if(LOG.isDebugEnabled()) {
139 LOG.debug(requestVote.toString());
142 boolean grantVote = false;
144 // Reply false if term < currentTerm (§5.1)
145 if (requestVote.getTerm() < currentTerm()) {
148 // If votedFor is null or candidateId, and candidate’s log is at
149 // least as up-to-date as receiver’s log, grant vote (§5.2, §5.4)
150 } else if (votedFor() == null || votedFor()
151 .equals(requestVote.getCandidateId())) {
153 boolean candidateLatest = false;
156 // Raft determines which of two logs is more up-to-date
157 // by comparing the index and term of the last entries in the
158 // logs. If the logs have last entries with different terms, then
159 // the log with the later term is more up-to-date. If the logs
160 // end with the same term, then whichever log is longer is
162 if (requestVote.getLastLogTerm() > lastTerm()) {
163 candidateLatest = true;
164 } else if ((requestVote.getLastLogTerm() == lastTerm())
165 && requestVote.getLastLogIndex() >= lastIndex()) {
166 candidateLatest = true;
169 if (candidateLatest) {
171 context.getTermInformation().updateAndPersist(requestVote.getTerm(),
172 requestVote.getCandidateId());
176 sender.tell(new RequestVoteReply(currentTerm(), grantVote), actor());
182 * Derived classes should not directly handle RequestVoteReply messages it
183 * should let the base class handle it first. Once the base class handles
184 * the RequestVoteReply message and does the common actions that are
185 * applicable in all RaftState's it will delegate the handling of the
186 * RequestVoteReply message to the derived class to do more state specific
187 * handling by calling this method
189 * @param sender The actor that sent this message
190 * @param requestVoteReply The RequestVoteReply message
193 protected abstract RaftActorBehavior handleRequestVoteReply(ActorRef sender,
194 RequestVoteReply requestVoteReply);
197 * Creates a random election duration
201 protected FiniteDuration electionDuration() {
202 long variance = new Random().nextInt(context.getConfigParams().getElectionTimeVariance());
203 return context.getConfigParams().getElectionTimeOutInterval().$plus(
204 new FiniteDuration(variance, TimeUnit.MILLISECONDS));
208 * stop the scheduled election
210 protected void stopElection() {
211 if (electionCancel != null && !electionCancel.isCancelled()) {
212 electionCancel.cancel();
217 * schedule a new election
221 protected void scheduleElection(FiniteDuration interval) {
224 // Schedule an election. When the scheduler triggers an ElectionTimeout
225 // message is sent to itself
227 context.getActorSystem().scheduler().scheduleOnce(interval,
228 context.getActor(), new ElectionTimeout(),
229 context.getActorSystem().dispatcher(), context.getActor());
233 * Get the current term
236 protected long currentTerm() {
237 return context.getTermInformation().getCurrentTerm();
241 * Get the candidate for whom we voted in the current term
244 protected String votedFor() {
245 return context.getTermInformation().getVotedFor();
249 * Get the actor associated with this behavior
252 protected ActorRef actor() {
253 return context.getActor();
257 * Get the term from the last entry in the log
261 protected long lastTerm() {
262 return context.getReplicatedLog().lastTerm();
266 * Get the index from the last entry in the log
270 protected long lastIndex() {
271 return context.getReplicatedLog().lastIndex();
275 * Find the client request tracker for a specific logIndex
280 protected ClientRequestTracker findClientRequestTracker(long logIndex) {
285 * Find the client request tracker for a specific logIndex
290 protected ClientRequestTracker removeClientRequestTracker(long logIndex) {
296 * Find the log index from the previous to last entry in the log
300 protected long prevLogIndex(long index){
301 ReplicatedLogEntry prevEntry =
302 context.getReplicatedLog().get(index - 1);
303 if (prevEntry != null) {
304 return prevEntry.getIndex();
310 * Find the log term from the previous to last entry in the log
313 protected long prevLogTerm(long index){
314 ReplicatedLogEntry prevEntry =
315 context.getReplicatedLog().get(index - 1);
316 if (prevEntry != null) {
317 return prevEntry.getTerm();
323 * Apply the provided index to the state machine
325 * @param index a log index that is known to be committed
327 protected void applyLogToStateMachine(final long index) {
328 long newLastApplied = context.getLastApplied();
329 // Now maybe we apply to the state machine
330 for (long i = context.getLastApplied() + 1;
331 i < index + 1; i++) {
332 ActorRef clientActor = null;
333 String identifier = null;
334 ClientRequestTracker tracker = removeClientRequestTracker(i);
336 if (tracker != null) {
337 clientActor = tracker.getClientActor();
338 identifier = tracker.getIdentifier();
340 ReplicatedLogEntry replicatedLogEntry =
341 context.getReplicatedLog().get(i);
343 if (replicatedLogEntry != null) {
344 // Send a local message to the local RaftActor (it's derived class to be
345 // specific to apply the log to it's index)
346 actor().tell(new ApplyState(clientActor, identifier,
347 replicatedLogEntry), actor());
350 //if one index is not present in the log, no point in looping
351 // around as the rest wont be present either
353 "Missing index {} from log. Cannot apply state. Ignoring {} to {}", i, i, index);
357 if(LOG.isDebugEnabled()) {
358 LOG.debug("Setting last applied to {}", newLastApplied);
360 context.setLastApplied(newLastApplied);
362 // send a message to persist a ApplyLogEntries marker message into akka's persistent journal
363 // will be used during recovery
364 //in case if the above code throws an error and this message is not sent, it would be fine
365 // as the append entries received later would initiate add this message to the journal
366 actor().tell(new ApplyLogEntries((int) context.getLastApplied()), actor());
369 protected Object fromSerializableMessage(Object serializable){
370 return SerializationUtils.fromSerializable(serializable);
374 public RaftActorBehavior handleMessage(ActorRef sender, Object message) {
375 if (message instanceof AppendEntries) {
376 return appendEntries(sender, (AppendEntries) message);
377 } else if (message instanceof AppendEntriesReply) {
378 return handleAppendEntriesReply(sender, (AppendEntriesReply) message);
379 } else if (message instanceof RequestVote) {
380 return requestVote(sender, (RequestVote) message);
381 } else if (message instanceof RequestVoteReply) {
382 return handleRequestVoteReply(sender, (RequestVoteReply) message);
387 @Override public String getLeaderId() {
391 protected RaftActorBehavior switchBehavior(RaftActorBehavior behavior) {
392 LOG.info("{} :- Switching from behavior {} to {}", context.getId(), this.state(), behavior.state());
395 } catch (Exception e) {
396 LOG.error(e, "Failed to close behavior : {}", this.state());
402 protected int getMajorityVoteCount(int numPeers) {
403 // Votes are required from a majority of the peers including self.
404 // The numMajority field therefore stores a calculated value
405 // of the number of votes required for this candidate to win an
406 // election based on it's known peers.
407 // If a peer was added during normal operation and raft replicas
408 // came to know about them then the new peer would also need to be
409 // taken into consideration when calculating this value.
410 // Here are some examples for what the numMajority would be for n
412 // 0 peers = 1 numMajority -: (0 + 1) / 2 + 1 = 1
413 // 2 peers = 2 numMajority -: (2 + 1) / 2 + 1 = 2
414 // 4 peers = 3 numMajority -: (4 + 1) / 2 + 1 = 3
419 numMajority = (numPeers + self) / 2 + 1;