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 java.util.Random;
14 import java.util.concurrent.TimeUnit;
15 import org.opendaylight.controller.cluster.raft.ClientRequestTracker;
16 import org.opendaylight.controller.cluster.raft.RaftActorContext;
17 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
18 import org.opendaylight.controller.cluster.raft.SerializationUtils;
19 import org.opendaylight.controller.cluster.raft.base.messages.ApplyLogEntries;
20 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
21 import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
22 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
23 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
24 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
25 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
26 import org.slf4j.Logger;
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 Logger LOG;
54 private Cancellable electionCancel = null;
59 protected String leaderId = null;
61 private long replicatedToAllIndex = -1;
63 protected AbstractRaftActorBehavior(RaftActorContext context) {
64 this.context = context;
65 this.LOG = context.getLogger();
69 public void setReplicatedToAllIndex(long replicatedToAllIndex) {
70 this.replicatedToAllIndex = replicatedToAllIndex;
74 public long getReplicatedToAllIndex() {
75 return replicatedToAllIndex;
79 * Derived classes should not directly handle AppendEntries messages it
80 * should let the base class handle it first. Once the base class handles
81 * the AppendEntries message and does the common actions that are applicable
82 * in all RaftState's it will delegate the handling of the AppendEntries
83 * message to the derived class to do more state specific handling by calling
86 * @param sender The actor that sent this message
87 * @param appendEntries The AppendEntries message
90 protected abstract RaftActorBehavior handleAppendEntries(ActorRef sender,
91 AppendEntries appendEntries);
95 * appendEntries first processes the AppendEntries message and then
96 * delegates handling to a specific behavior
99 * @param appendEntries
102 protected RaftActorBehavior appendEntries(ActorRef sender,
103 AppendEntries appendEntries) {
105 // 1. Reply false if term < currentTerm (§5.1)
106 if (appendEntries.getTerm() < currentTerm()) {
107 if(LOG.isDebugEnabled()) {
108 LOG.debug("{}: Cannot append entries because sender term {} is less than {}",
109 context.getId(), appendEntries.getTerm(), currentTerm());
113 new AppendEntriesReply(context.getId(), currentTerm(), false,
114 lastIndex(), lastTerm()), actor()
120 return handleAppendEntries(sender, appendEntries);
124 * Derived classes should not directly handle AppendEntriesReply messages it
125 * should let the base class handle it first. Once the base class handles
126 * the AppendEntriesReply message and does the common actions that are
127 * applicable in all RaftState's it will delegate the handling of the
128 * AppendEntriesReply message to the derived class to do more state specific
129 * handling by calling this method
131 * @param sender The actor that sent this message
132 * @param appendEntriesReply The AppendEntriesReply message
135 protected abstract RaftActorBehavior handleAppendEntriesReply(ActorRef sender,
136 AppendEntriesReply appendEntriesReply);
139 * requestVote handles the RequestVote message. This logic is common
146 protected RaftActorBehavior requestVote(ActorRef sender,
147 RequestVote requestVote) {
149 if(LOG.isDebugEnabled()) {
150 LOG.debug("{}: Received {}", context.getId(), requestVote);
153 boolean grantVote = false;
155 // Reply false if term < currentTerm (§5.1)
156 if (requestVote.getTerm() < currentTerm()) {
159 // If votedFor is null or candidateId, and candidate’s log is at
160 // least as up-to-date as receiver’s log, grant vote (§5.2, §5.4)
161 } else if (votedFor() == null || votedFor()
162 .equals(requestVote.getCandidateId())) {
164 boolean candidateLatest = false;
167 // Raft determines which of two logs is more up-to-date
168 // by comparing the index and term of the last entries in the
169 // logs. If the logs have last entries with different terms, then
170 // the log with the later term is more up-to-date. If the logs
171 // end with the same term, then whichever log is longer is
173 if (requestVote.getLastLogTerm() > lastTerm()) {
174 candidateLatest = true;
175 } else if ((requestVote.getLastLogTerm() == lastTerm())
176 && requestVote.getLastLogIndex() >= lastIndex()) {
177 candidateLatest = true;
180 if (candidateLatest) {
182 context.getTermInformation().updateAndPersist(requestVote.getTerm(),
183 requestVote.getCandidateId());
187 sender.tell(new RequestVoteReply(currentTerm(), grantVote), actor());
193 * Derived classes should not directly handle RequestVoteReply messages it
194 * should let the base class handle it first. Once the base class handles
195 * the RequestVoteReply message and does the common actions that are
196 * applicable in all RaftState's it will delegate the handling of the
197 * RequestVoteReply message to the derived class to do more state specific
198 * handling by calling this method
200 * @param sender The actor that sent this message
201 * @param requestVoteReply The RequestVoteReply message
204 protected abstract RaftActorBehavior handleRequestVoteReply(ActorRef sender,
205 RequestVoteReply requestVoteReply);
208 * Creates a random election duration
212 protected FiniteDuration electionDuration() {
213 long variance = new Random().nextInt(context.getConfigParams().getElectionTimeVariance());
214 return context.getConfigParams().getElectionTimeOutInterval().$plus(
215 new FiniteDuration(variance, TimeUnit.MILLISECONDS));
219 * stop the scheduled election
221 protected void stopElection() {
222 if (electionCancel != null && !electionCancel.isCancelled()) {
223 electionCancel.cancel();
228 * schedule a new election
232 protected void scheduleElection(FiniteDuration interval) {
235 // Schedule an election. When the scheduler triggers an ElectionTimeout
236 // message is sent to itself
238 context.getActorSystem().scheduler().scheduleOnce(interval,
239 context.getActor(), new ElectionTimeout(),
240 context.getActorSystem().dispatcher(), context.getActor());
244 * Get the current term
247 protected long currentTerm() {
248 return context.getTermInformation().getCurrentTerm();
252 * Get the candidate for whom we voted in the current term
255 protected String votedFor() {
256 return context.getTermInformation().getVotedFor();
260 * Get the actor associated with this behavior
263 protected ActorRef actor() {
264 return context.getActor();
268 * Get the term from the last entry in the log
272 protected long lastTerm() {
273 return context.getReplicatedLog().lastTerm();
277 * Get the index from the last entry in the log
281 protected long lastIndex() {
282 return context.getReplicatedLog().lastIndex();
286 * Find the client request tracker for a specific logIndex
291 protected ClientRequestTracker findClientRequestTracker(long logIndex) {
296 * Find the client request tracker for a specific logIndex
301 protected ClientRequestTracker removeClientRequestTracker(long logIndex) {
307 * Find the log index from the previous to last entry in the log
311 protected long prevLogIndex(long index){
312 ReplicatedLogEntry prevEntry =
313 context.getReplicatedLog().get(index - 1);
314 if (prevEntry != null) {
315 return prevEntry.getIndex();
321 * Find the log term from the previous to last entry in the log
324 protected long prevLogTerm(long index){
325 ReplicatedLogEntry prevEntry =
326 context.getReplicatedLog().get(index - 1);
327 if (prevEntry != null) {
328 return prevEntry.getTerm();
334 * Apply the provided index to the state machine
336 * @param index a log index that is known to be committed
338 protected void applyLogToStateMachine(final long index) {
339 long newLastApplied = context.getLastApplied();
340 // Now maybe we apply to the state machine
341 for (long i = context.getLastApplied() + 1;
342 i < index + 1; i++) {
343 ActorRef clientActor = null;
344 String identifier = null;
345 ClientRequestTracker tracker = removeClientRequestTracker(i);
347 if (tracker != null) {
348 clientActor = tracker.getClientActor();
349 identifier = tracker.getIdentifier();
351 ReplicatedLogEntry replicatedLogEntry =
352 context.getReplicatedLog().get(i);
354 if (replicatedLogEntry != null) {
355 // Send a local message to the local RaftActor (it's derived class to be
356 // specific to apply the log to it's index)
357 actor().tell(new ApplyState(clientActor, identifier,
358 replicatedLogEntry), actor());
361 //if one index is not present in the log, no point in looping
362 // around as the rest wont be present either
364 "{}: Missing index {} from log. Cannot apply state. Ignoring {} to {}",
365 context.getId(), i, i, index);
369 if(LOG.isDebugEnabled()) {
370 LOG.debug("{}: Setting last applied to {}", context.getId(), newLastApplied);
372 context.setLastApplied(newLastApplied);
374 // send a message to persist a ApplyLogEntries marker message into akka's persistent journal
375 // will be used during recovery
376 //in case if the above code throws an error and this message is not sent, it would be fine
377 // as the append entries received later would initiate add this message to the journal
378 actor().tell(new ApplyLogEntries((int) context.getLastApplied()), actor());
381 protected Object fromSerializableMessage(Object serializable){
382 return SerializationUtils.fromSerializable(serializable);
386 public RaftActorBehavior handleMessage(ActorRef sender, Object message) {
387 if (message instanceof AppendEntries) {
388 return appendEntries(sender, (AppendEntries) message);
389 } else if (message instanceof AppendEntriesReply) {
390 return handleAppendEntriesReply(sender, (AppendEntriesReply) message);
391 } else if (message instanceof RequestVote) {
392 return requestVote(sender, (RequestVote) message);
393 } else if (message instanceof RequestVoteReply) {
394 return handleRequestVoteReply(sender, (RequestVoteReply) message);
399 @Override public String getLeaderId() {
403 protected RaftActorBehavior switchBehavior(RaftActorBehavior behavior) {
404 LOG.info("{} :- Switching from behavior {} to {}", context.getId(), this.state(), behavior.state());
407 } catch (Exception e) {
408 LOG.error("{}: Failed to close behavior : {}", context.getId(), this.state(), e);
414 protected int getMajorityVoteCount(int numPeers) {
415 // Votes are required from a majority of the peers including self.
416 // The numMajority field therefore stores a calculated value
417 // of the number of votes required for this candidate to win an
418 // election based on it's known peers.
419 // If a peer was added during normal operation and raft replicas
420 // came to know about them then the new peer would also need to be
421 // taken into consideration when calculating this value.
422 // Here are some examples for what the numMajority would be for n
424 // 0 peers = 1 numMajority -: (0 + 1) / 2 + 1 = 1
425 // 2 peers = 2 numMajority -: (2 + 1) / 2 + 1 = 2
426 // 4 peers = 3 numMajority -: (4 + 1) / 2 + 1 = 3
431 numMajority = (numPeers + self) / 2 + 1;
439 * Performs a snapshot with no capture on the replicated log.
440 * It clears the log from the supplied index or last-applied-1 which ever is minimum.
442 * @param snapshotCapturedIndex
444 protected void performSnapshotWithoutCapture(final long snapshotCapturedIndex) {
445 // we would want to keep the lastApplied as its used while capturing snapshots
446 long lastApplied = context.getLastApplied();
447 long tempMin = Math.min(snapshotCapturedIndex, (lastApplied > -1 ? lastApplied - 1 : -1));
449 if (tempMin > -1 && context.getReplicatedLog().isPresent(tempMin)) {
450 //use the term of the temp-min, since we check for isPresent, entry will not be null
451 ReplicatedLogEntry entry = context.getReplicatedLog().get(tempMin);
452 context.getReplicatedLog().snapshotPreCommit(tempMin, entry.getTerm());
453 context.getReplicatedLog().snapshotCommit();
454 setReplicatedToAllIndex(tempMin);