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.RaftState;
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.ApplyJournalEntries;
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 org.slf4j.Logger;
28 import scala.concurrent.duration.FiniteDuration;
31 * Abstract class that represents the behavior of a RaftActor
35 * <li> If commitIndex > lastApplied: increment lastApplied, apply
36 * log[lastApplied] to state machine (§5.3)
37 * <li> If RPC request or response contains term T > currentTerm:
38 * set currentTerm = T, convert to follower (§5.1)
40 public abstract class AbstractRaftActorBehavior implements RaftActorBehavior {
43 * Information about the RaftActor whose behavior this class represents
45 protected final RaftActorContext context;
50 protected final Logger LOG;
55 private Cancellable electionCancel = null;
60 protected String leaderId = null;
62 private long replicatedToAllIndex = -1;
64 private final String logName;
66 private final RaftState state;
68 protected AbstractRaftActorBehavior(RaftActorContext context, RaftState state) {
69 this.context = context;
71 this.LOG = context.getLogger();
73 logName = String.format("%s (%s)", context.getId(), state);
77 public RaftState state() {
81 public String logName() {
86 public void setReplicatedToAllIndex(long replicatedToAllIndex) {
87 this.replicatedToAllIndex = replicatedToAllIndex;
91 public long getReplicatedToAllIndex() {
92 return replicatedToAllIndex;
96 * Derived classes should not directly handle AppendEntries messages it
97 * should let the base class handle it first. Once the base class handles
98 * the AppendEntries message and does the common actions that are applicable
99 * in all RaftState's it will delegate the handling of the AppendEntries
100 * message to the derived class to do more state specific handling by calling
103 * @param sender The actor that sent this message
104 * @param appendEntries The AppendEntries message
107 protected abstract RaftActorBehavior handleAppendEntries(ActorRef sender,
108 AppendEntries appendEntries);
112 * appendEntries first processes the AppendEntries message and then
113 * delegates handling to a specific behavior
116 * @param appendEntries
119 protected RaftActorBehavior appendEntries(ActorRef sender,
120 AppendEntries appendEntries) {
122 // 1. Reply false if term < currentTerm (§5.1)
123 if (appendEntries.getTerm() < currentTerm()) {
124 if(LOG.isDebugEnabled()) {
125 LOG.debug("{}: Cannot append entries because sender term {} is less than {}",
126 logName(), appendEntries.getTerm(), currentTerm());
130 new AppendEntriesReply(context.getId(), currentTerm(), false,
131 lastIndex(), lastTerm()), actor()
137 return handleAppendEntries(sender, appendEntries);
141 * Derived classes should not directly handle AppendEntriesReply messages it
142 * should let the base class handle it first. Once the base class handles
143 * the AppendEntriesReply message and does the common actions that are
144 * applicable in all RaftState's it will delegate the handling of the
145 * AppendEntriesReply message to the derived class to do more state specific
146 * handling by calling this method
148 * @param sender The actor that sent this message
149 * @param appendEntriesReply The AppendEntriesReply message
152 protected abstract RaftActorBehavior handleAppendEntriesReply(ActorRef sender,
153 AppendEntriesReply appendEntriesReply);
156 * requestVote handles the RequestVote message. This logic is common
163 protected RaftActorBehavior requestVote(ActorRef sender, RequestVote requestVote) {
165 LOG.debug("{}: In requestVote: {}", logName(), requestVote);
167 boolean grantVote = false;
169 // Reply false if term < currentTerm (§5.1)
170 if (requestVote.getTerm() < currentTerm()) {
173 // If votedFor is null or candidateId, and candidate’s log is at
174 // least as up-to-date as receiver’s log, grant vote (§5.2, §5.4)
175 } else if (votedFor() == null || votedFor()
176 .equals(requestVote.getCandidateId())) {
178 boolean candidateLatest = false;
181 // Raft determines which of two logs is more up-to-date
182 // by comparing the index and term of the last entries in the
183 // logs. If the logs have last entries with different terms, then
184 // the log with the later term is more up-to-date. If the logs
185 // end with the same term, then whichever log is longer is
187 if (requestVote.getLastLogTerm() > lastTerm()) {
188 candidateLatest = true;
189 } else if ((requestVote.getLastLogTerm() == lastTerm())
190 && requestVote.getLastLogIndex() >= lastIndex()) {
191 candidateLatest = true;
194 if (candidateLatest) {
196 context.getTermInformation().updateAndPersist(requestVote.getTerm(),
197 requestVote.getCandidateId());
201 RequestVoteReply reply = new RequestVoteReply(currentTerm(), grantVote);
203 LOG.debug("{}: requestVote returning: {}", logName(), reply);
205 sender.tell(reply, actor());
211 * Derived classes should not directly handle RequestVoteReply messages it
212 * should let the base class handle it first. Once the base class handles
213 * the RequestVoteReply message and does the common actions that are
214 * applicable in all RaftState's it will delegate the handling of the
215 * RequestVoteReply message to the derived class to do more state specific
216 * handling by calling this method
218 * @param sender The actor that sent this message
219 * @param requestVoteReply The RequestVoteReply message
222 protected abstract RaftActorBehavior handleRequestVoteReply(ActorRef sender,
223 RequestVoteReply requestVoteReply);
226 * Creates a random election duration
230 protected FiniteDuration electionDuration() {
231 long variance = new Random().nextInt(context.getConfigParams().getElectionTimeVariance());
232 return context.getConfigParams().getElectionTimeOutInterval().$plus(
233 new FiniteDuration(variance, TimeUnit.MILLISECONDS));
237 * stop the scheduled election
239 protected void stopElection() {
240 if (electionCancel != null && !electionCancel.isCancelled()) {
241 electionCancel.cancel();
246 * schedule a new election
250 protected void scheduleElection(FiniteDuration interval) {
253 // Schedule an election. When the scheduler triggers an ElectionTimeout
254 // message is sent to itself
256 context.getActorSystem().scheduler().scheduleOnce(interval,
257 context.getActor(), new ElectionTimeout(),
258 context.getActorSystem().dispatcher(), context.getActor());
262 * Get the current term
265 protected long currentTerm() {
266 return context.getTermInformation().getCurrentTerm();
270 * Get the candidate for whom we voted in the current term
273 protected String votedFor() {
274 return context.getTermInformation().getVotedFor();
278 * Get the actor associated with this behavior
281 protected ActorRef actor() {
282 return context.getActor();
286 * Get the term from the last entry in the log
290 protected long lastTerm() {
291 return context.getReplicatedLog().lastTerm();
295 * Get the index from the last entry in the log
299 protected long lastIndex() {
300 return context.getReplicatedLog().lastIndex();
304 * Find the client request tracker for a specific logIndex
309 protected ClientRequestTracker findClientRequestTracker(long logIndex) {
314 * Find the client request tracker for a specific logIndex
319 protected ClientRequestTracker removeClientRequestTracker(long logIndex) {
325 * Find the log index from the previous to last entry in the log
329 protected long prevLogIndex(long index){
330 ReplicatedLogEntry prevEntry =
331 context.getReplicatedLog().get(index - 1);
332 if (prevEntry != null) {
333 return prevEntry.getIndex();
339 * Find the log term from the previous to last entry in the log
342 protected long prevLogTerm(long index){
343 ReplicatedLogEntry prevEntry =
344 context.getReplicatedLog().get(index - 1);
345 if (prevEntry != null) {
346 return prevEntry.getTerm();
352 * Apply the provided index to the state machine
354 * @param index a log index that is known to be committed
356 protected void applyLogToStateMachine(final long index) {
357 long newLastApplied = context.getLastApplied();
358 // Now maybe we apply to the state machine
359 for (long i = context.getLastApplied() + 1;
360 i < index + 1; i++) {
361 ActorRef clientActor = null;
362 String identifier = null;
363 ClientRequestTracker tracker = removeClientRequestTracker(i);
365 if (tracker != null) {
366 clientActor = tracker.getClientActor();
367 identifier = tracker.getIdentifier();
369 ReplicatedLogEntry replicatedLogEntry =
370 context.getReplicatedLog().get(i);
372 if (replicatedLogEntry != null) {
373 // Send a local message to the local RaftActor (it's derived class to be
374 // specific to apply the log to it's index)
375 actor().tell(new ApplyState(clientActor, identifier,
376 replicatedLogEntry), actor());
379 //if one index is not present in the log, no point in looping
380 // around as the rest wont be present either
382 "{}: Missing index {} from log. Cannot apply state. Ignoring {} to {}",
383 logName(), i, i, index);
387 if(LOG.isDebugEnabled()) {
388 LOG.debug("{}: Setting last applied to {}", logName(), newLastApplied);
390 context.setLastApplied(newLastApplied);
392 // send a message to persist a ApplyLogEntries marker message into akka's persistent journal
393 // will be used during recovery
394 //in case if the above code throws an error and this message is not sent, it would be fine
395 // as the append entries received later would initiate add this message to the journal
396 actor().tell(new ApplyJournalEntries(context.getLastApplied()), actor());
399 protected Object fromSerializableMessage(Object serializable){
400 return SerializationUtils.fromSerializable(serializable);
404 public RaftActorBehavior handleMessage(ActorRef sender, Object message) {
405 if (message instanceof AppendEntries) {
406 return appendEntries(sender, (AppendEntries) message);
407 } else if (message instanceof AppendEntriesReply) {
408 return handleAppendEntriesReply(sender, (AppendEntriesReply) message);
409 } else if (message instanceof RequestVote) {
410 return requestVote(sender, (RequestVote) message);
411 } else if (message instanceof RequestVoteReply) {
412 return handleRequestVoteReply(sender, (RequestVoteReply) message);
417 @Override public String getLeaderId() {
421 protected RaftActorBehavior switchBehavior(RaftActorBehavior behavior) {
422 LOG.info("{} :- Switching from behavior {} to {}", logName(), this.state(), behavior.state());
425 } catch (Exception e) {
426 LOG.error("{}: Failed to close behavior : {}", logName(), this.state(), e);
432 protected int getMajorityVoteCount(int numPeers) {
433 // Votes are required from a majority of the peers including self.
434 // The numMajority field therefore stores a calculated value
435 // of the number of votes required for this candidate to win an
436 // election based on it's known peers.
437 // If a peer was added during normal operation and raft replicas
438 // came to know about them then the new peer would also need to be
439 // taken into consideration when calculating this value.
440 // Here are some examples for what the numMajority would be for n
442 // 0 peers = 1 numMajority -: (0 + 1) / 2 + 1 = 1
443 // 2 peers = 2 numMajority -: (2 + 1) / 2 + 1 = 2
444 // 4 peers = 3 numMajority -: (4 + 1) / 2 + 1 = 3
449 numMajority = (numPeers + self) / 2 + 1;
457 * Performs a snapshot with no capture on the replicated log.
458 * It clears the log from the supplied index or last-applied-1 which ever is minimum.
460 * @param snapshotCapturedIndex
462 protected void performSnapshotWithoutCapture(final long snapshotCapturedIndex) {
463 // we would want to keep the lastApplied as its used while capturing snapshots
464 long lastApplied = context.getLastApplied();
465 long tempMin = Math.min(snapshotCapturedIndex, (lastApplied > -1 ? lastApplied - 1 : -1));
467 if (tempMin > -1 && context.getReplicatedLog().isPresent(tempMin)) {
468 LOG.debug("{}: fakeSnapshot purging log to {} for term {}", logName(), tempMin,
469 context.getTermInformation().getCurrentTerm());
471 //use the term of the temp-min, since we check for isPresent, entry will not be null
472 ReplicatedLogEntry entry = context.getReplicatedLog().get(tempMin);
473 context.getReplicatedLog().snapshotPreCommit(tempMin, entry.getTerm());
474 context.getReplicatedLog().snapshotCommit();
475 setReplicatedToAllIndex(tempMin);