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.ActorSelection;
13 import akka.actor.Cancellable;
14 import com.google.common.annotations.VisibleForTesting;
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
17 import com.google.protobuf.ByteString;
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.atomic.AtomicLong;
27 import org.opendaylight.controller.cluster.raft.ClientRequestTracker;
28 import org.opendaylight.controller.cluster.raft.ClientRequestTrackerImpl;
29 import org.opendaylight.controller.cluster.raft.FollowerLogInformation;
30 import org.opendaylight.controller.cluster.raft.FollowerLogInformationImpl;
31 import org.opendaylight.controller.cluster.raft.RaftActorContext;
32 import org.opendaylight.controller.cluster.raft.RaftState;
33 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
34 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot;
35 import org.opendaylight.controller.cluster.raft.base.messages.InitiateInstallSnapshot;
36 import org.opendaylight.controller.cluster.raft.base.messages.Replicate;
37 import org.opendaylight.controller.cluster.raft.base.messages.SendHeartBeat;
38 import org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot;
39 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
40 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
41 import org.opendaylight.controller.cluster.raft.messages.InstallSnapshot;
42 import org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply;
43 import org.opendaylight.controller.cluster.raft.messages.RaftRPC;
44 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
45 import scala.concurrent.duration.FiniteDuration;
48 * The behavior of a RaftActor when it is in the Leader state
52 * <li> Upon election: send initial empty AppendEntries RPCs
53 * (heartbeat) to each server; repeat during idle periods to
54 * prevent election timeouts (§5.2)
55 * <li> If command received from client: append entry to local log,
56 * respond after entry applied to state machine (§5.3)
57 * <li> If last log index ≥ nextIndex for a follower: send
58 * AppendEntries RPC with log entries starting at nextIndex
60 * <li> If successful: update nextIndex and matchIndex for
62 * <li> If AppendEntries fails because of log inconsistency:
63 * decrement nextIndex and retry (§5.3)
65 * <li> If there exists an N such that N > commitIndex, a majority
66 * of matchIndex[i] ≥ N, and log[N].term == currentTerm:
67 * set commitIndex = N (§5.3, §5.4).
69 public abstract class AbstractLeader extends AbstractRaftActorBehavior {
70 protected final Map<String, FollowerLogInformation> followerToLog = new HashMap<>();
71 protected final Map<String, FollowerToSnapshot> mapFollowerToSnapshot = new HashMap<>();
73 protected final Set<String> followers;
75 private Cancellable heartbeatSchedule = null;
77 private List<ClientRequestTracker> trackerList = new ArrayList<>();
79 protected final int minReplicationCount;
81 protected final int minIsolatedLeaderPeerCount;
83 private Optional<ByteString> snapshot;
85 public AbstractLeader(RaftActorContext context) {
88 followers = context.getPeerAddresses().keySet();
90 for (String followerId : followers) {
91 FollowerLogInformation followerLogInformation =
92 new FollowerLogInformationImpl(followerId,
93 new AtomicLong(context.getCommitIndex()),
95 context.getConfigParams().getElectionTimeOutInterval());
97 followerToLog.put(followerId, followerLogInformation);
100 leaderId = context.getId();
102 if(LOG.isDebugEnabled()) {
103 LOG.debug("Election:Leader has following peers: {}", followers);
106 minReplicationCount = getMajorityVoteCount(followers.size());
108 // the isolated Leader peer count will be 1 less than the majority vote count.
109 // this is because the vote count has the self vote counted in it
111 // 0 peers = 1 votesRequired , minIsolatedLeaderPeerCount = 0
112 // 2 peers = 2 votesRequired , minIsolatedLeaderPeerCount = 1
113 // 4 peers = 3 votesRequired, minIsolatedLeaderPeerCount = 2
114 minIsolatedLeaderPeerCount = minReplicationCount > 0 ? (minReplicationCount - 1) : 0;
116 snapshot = Optional.absent();
118 // Immediately schedule a heartbeat
119 // Upon election: send initial empty AppendEntries RPCs
120 // (heartbeat) to each server; repeat during idle periods to
121 // prevent election timeouts (§5.2)
122 scheduleHeartBeat(new FiniteDuration(0, TimeUnit.SECONDS));
125 private Optional<ByteString> getSnapshot() {
130 void setSnapshot(Optional<ByteString> snapshot) {
131 this.snapshot = snapshot;
135 protected RaftActorBehavior handleAppendEntries(ActorRef sender,
136 AppendEntries appendEntries) {
138 if(LOG.isDebugEnabled()) {
139 LOG.debug(appendEntries.toString());
146 protected RaftActorBehavior handleAppendEntriesReply(ActorRef sender,
147 AppendEntriesReply appendEntriesReply) {
149 if(! appendEntriesReply.isSuccess()) {
150 if(LOG.isDebugEnabled()) {
151 LOG.debug(appendEntriesReply.toString());
155 // Update the FollowerLogInformation
156 String followerId = appendEntriesReply.getFollowerId();
157 FollowerLogInformation followerLogInformation =
158 followerToLog.get(followerId);
160 if(followerLogInformation == null){
161 LOG.error("Unknown follower {}", followerId);
165 followerLogInformation.markFollowerActive();
167 if (appendEntriesReply.isSuccess()) {
168 followerLogInformation
169 .setMatchIndex(appendEntriesReply.getLogLastIndex());
170 followerLogInformation
171 .setNextIndex(appendEntriesReply.getLogLastIndex() + 1);
174 // TODO: When we find that the follower is out of sync with the
175 // Leader we simply decrement that followers next index by 1.
176 // Would it be possible to do better than this? The RAFT spec
177 // does not explicitly deal with it but may be something for us to
180 followerLogInformation.decrNextIndex();
183 // Now figure out if this reply warrants a change in the commitIndex
184 // If there exists an N such that N > commitIndex, a majority
185 // of matchIndex[i] ≥ N, and log[N].term == currentTerm:
186 // set commitIndex = N (§5.3, §5.4).
187 for (long N = context.getCommitIndex() + 1; ; N++) {
188 int replicatedCount = 1;
190 for (FollowerLogInformation info : followerToLog.values()) {
191 if (info.getMatchIndex().get() >= N) {
196 if (replicatedCount >= minReplicationCount) {
197 ReplicatedLogEntry replicatedLogEntry = context.getReplicatedLog().get(N);
198 if (replicatedLogEntry != null &&
199 replicatedLogEntry.getTerm() == currentTerm()) {
200 context.setCommitIndex(N);
207 // Apply the change to the state machine
208 if (context.getCommitIndex() > context.getLastApplied()) {
209 applyLogToStateMachine(context.getCommitIndex());
215 protected ClientRequestTracker removeClientRequestTracker(long logIndex) {
217 ClientRequestTracker toRemove = findClientRequestTracker(logIndex);
218 if(toRemove != null) {
219 trackerList.remove(toRemove);
225 protected ClientRequestTracker findClientRequestTracker(long logIndex) {
226 for (ClientRequestTracker tracker : trackerList) {
227 if (tracker.getIndex() == logIndex) {
235 protected RaftActorBehavior handleRequestVoteReply(ActorRef sender,
236 RequestVoteReply requestVoteReply) {
241 public RaftState state() {
242 return RaftState.Leader;
246 public RaftActorBehavior handleMessage(ActorRef sender, Object originalMessage) {
247 Preconditions.checkNotNull(sender, "sender should not be null");
249 Object message = fromSerializableMessage(originalMessage);
251 if (message instanceof RaftRPC) {
252 RaftRPC rpc = (RaftRPC) message;
253 // If RPC request or response contains term T > currentTerm:
254 // set currentTerm = T, convert to follower (§5.1)
255 // This applies to all RPC messages and responses
256 if (rpc.getTerm() > context.getTermInformation().getCurrentTerm()) {
257 context.getTermInformation().updateAndPersist(rpc.getTerm(), null);
259 return switchBehavior(new Follower(context));
264 if (message instanceof SendHeartBeat) {
268 } else if(message instanceof InitiateInstallSnapshot) {
269 installSnapshotIfNeeded();
271 } else if(message instanceof SendInstallSnapshot) {
272 // received from RaftActor
273 setSnapshot(Optional.of(((SendInstallSnapshot) message).getSnapshot()));
274 sendInstallSnapshot();
276 } else if (message instanceof Replicate) {
277 replicate((Replicate) message);
279 } else if (message instanceof InstallSnapshotReply){
280 handleInstallSnapshotReply((InstallSnapshotReply) message);
284 scheduleHeartBeat(context.getConfigParams().getHeartBeatInterval());
287 return super.handleMessage(sender, message);
290 private void handleInstallSnapshotReply(InstallSnapshotReply reply) {
291 String followerId = reply.getFollowerId();
292 FollowerToSnapshot followerToSnapshot = mapFollowerToSnapshot.get(followerId);
293 FollowerLogInformation followerLogInformation = followerToLog.get(followerId);
294 followerLogInformation.markFollowerActive();
296 if (followerToSnapshot != null &&
297 followerToSnapshot.getChunkIndex() == reply.getChunkIndex()) {
299 if (reply.isSuccess()) {
300 if(followerToSnapshot.isLastChunk(reply.getChunkIndex())) {
301 //this was the last chunk reply
302 if(LOG.isDebugEnabled()) {
303 LOG.debug("InstallSnapshotReply received, " +
304 "last chunk received, Chunk:{}. Follower:{} Setting nextIndex:{}",
305 reply.getChunkIndex(), followerId,
306 context.getReplicatedLog().getSnapshotIndex() + 1
310 followerLogInformation.setMatchIndex(
311 context.getReplicatedLog().getSnapshotIndex());
312 followerLogInformation.setNextIndex(
313 context.getReplicatedLog().getSnapshotIndex() + 1);
314 mapFollowerToSnapshot.remove(followerId);
316 if(LOG.isDebugEnabled()) {
317 LOG.debug("followerToLog.get(followerId).getNextIndex().get()=" +
318 followerToLog.get(followerId).getNextIndex().get());
321 if (mapFollowerToSnapshot.isEmpty()) {
322 // once there are no pending followers receiving snapshots
323 // we can remove snapshot from the memory
324 setSnapshot(Optional.<ByteString>absent());
328 followerToSnapshot.markSendStatus(true);
331 LOG.info("InstallSnapshotReply received, " +
332 "sending snapshot chunk failed, Will retry, Chunk:{}",
333 reply.getChunkIndex()
335 followerToSnapshot.markSendStatus(false);
339 LOG.error("ERROR!!" +
340 "FollowerId in InstallSnapshotReply not known to Leader" +
341 " or Chunk Index in InstallSnapshotReply not matching {} != {}",
342 followerToSnapshot.getChunkIndex(), reply.getChunkIndex()
347 private void replicate(Replicate replicate) {
348 long logIndex = replicate.getReplicatedLogEntry().getIndex();
350 if(LOG.isDebugEnabled()) {
351 LOG.debug("Replicate message {}", logIndex);
354 // Create a tracker entry we will use this later to notify the
357 new ClientRequestTrackerImpl(replicate.getClientActor(),
358 replicate.getIdentifier(),
362 if (followers.size() == 0) {
363 context.setCommitIndex(logIndex);
364 applyLogToStateMachine(logIndex);
370 private void sendAppendEntries() {
371 // Send an AppendEntries to all followers
372 for (String followerId : followers) {
373 ActorSelection followerActor = context.getPeerActorSelection(followerId);
375 if (followerActor != null) {
376 FollowerLogInformation followerLogInformation = followerToLog.get(followerId);
377 long followerNextIndex = followerLogInformation.getNextIndex().get();
378 boolean isFollowerActive = followerLogInformation.isFollowerActive();
379 List<ReplicatedLogEntry> entries = null;
381 if (mapFollowerToSnapshot.get(followerId) != null) {
382 // if install snapshot is in process , then sent next chunk if possible
383 if (isFollowerActive && mapFollowerToSnapshot.get(followerId).canSendNextChunk()) {
384 sendSnapshotChunk(followerActor, followerId);
386 // we send a heartbeat even if we have not received a reply for the last chunk
387 sendAppendEntriesToFollower(followerActor, followerNextIndex,
388 Collections.<ReplicatedLogEntry>emptyList());
392 long leaderLastIndex = context.getReplicatedLog().lastIndex();
393 long leaderSnapShotIndex = context.getReplicatedLog().getSnapshotIndex();
395 if (isFollowerActive &&
396 context.getReplicatedLog().isPresent(followerNextIndex)) {
397 // FIXME : Sending one entry at a time
398 entries = context.getReplicatedLog().getFrom(followerNextIndex, 1);
400 } else if (isFollowerActive && followerNextIndex >= 0 &&
401 leaderLastIndex >= followerNextIndex ) {
402 // if the followers next index is not present in the leaders log, and
403 // if the follower is just not starting and if leader's index is more than followers index
404 // then snapshot should be sent
406 if(LOG.isDebugEnabled()) {
407 LOG.debug("InitiateInstallSnapshot to follower:{}," +
408 "follower-nextIndex:{}, leader-snapshot-index:{}, " +
409 "leader-last-index:{}", followerId,
410 followerNextIndex, leaderSnapShotIndex, leaderLastIndex
413 actor().tell(new InitiateInstallSnapshot(), actor());
415 // we would want to sent AE as the capture snapshot might take time
416 entries = Collections.<ReplicatedLogEntry>emptyList();
419 //we send an AppendEntries, even if the follower is inactive
420 // in-order to update the followers timestamp, in case it becomes active again
421 entries = Collections.<ReplicatedLogEntry>emptyList();
424 sendAppendEntriesToFollower(followerActor, followerNextIndex, entries);
431 private void sendAppendEntriesToFollower(ActorSelection followerActor, long followerNextIndex,
432 List<ReplicatedLogEntry> entries) {
434 new AppendEntries(currentTerm(), context.getId(),
435 prevLogIndex(followerNextIndex),
436 prevLogTerm(followerNextIndex), entries,
437 context.getCommitIndex()).toSerializable(),
443 * An installSnapshot is scheduled at a interval that is a multiple of
444 * a HEARTBEAT_INTERVAL. This is to avoid the need to check for installing
445 * snapshots at every heartbeat.
447 * Install Snapshot works as follows
448 * 1. Leader sends a InitiateInstallSnapshot message to self
449 * 2. Leader then initiates the capture snapshot by sending a CaptureSnapshot message to actor
450 * 3. RaftActor on receipt of the CaptureSnapshotReply (from Shard), stores the received snapshot in the replicated log
451 * and makes a call to Leader's handleMessage , with SendInstallSnapshot message.
452 * 4. Leader , picks the snapshot from im-mem ReplicatedLog and sends it in chunks to the Follower
453 * 5. On complete, Follower sends back a InstallSnapshotReply.
454 * 6. On receipt of the InstallSnapshotReply for the last chunk, Leader marks the install complete for that follower
455 * and replenishes the memory by deleting the snapshot in Replicated log.
458 private void installSnapshotIfNeeded() {
459 for (String followerId : followers) {
460 ActorSelection followerActor =
461 context.getPeerActorSelection(followerId);
463 if(followerActor != null) {
464 FollowerLogInformation followerLogInformation =
465 followerToLog.get(followerId);
467 long nextIndex = followerLogInformation.getNextIndex().get();
469 if (!context.getReplicatedLog().isPresent(nextIndex) &&
470 context.getReplicatedLog().isInSnapshot(nextIndex)) {
471 LOG.info("{} follower needs a snapshot install", followerId);
472 if (snapshot.isPresent()) {
473 // if a snapshot is present in the memory, most likely another install is in progress
474 // no need to capture snapshot
475 sendSnapshotChunk(followerActor, followerId);
478 initiateCaptureSnapshot();
479 //we just need 1 follower who would need snapshot to be installed.
480 // when we have the snapshot captured, we would again check (in SendInstallSnapshot)
481 // who needs an install and send to all who need
490 // on every install snapshot, we try to capture the snapshot.
491 // Once a capture is going on, another one issued will get ignored by RaftActor.
492 private void initiateCaptureSnapshot() {
493 LOG.info("Initiating Snapshot Capture to Install Snapshot, Leader:{}", getLeaderId());
494 ReplicatedLogEntry lastAppliedEntry = context.getReplicatedLog().get(context.getLastApplied());
495 long lastAppliedIndex = -1;
496 long lastAppliedTerm = -1;
498 if (lastAppliedEntry != null) {
499 lastAppliedIndex = lastAppliedEntry.getIndex();
500 lastAppliedTerm = lastAppliedEntry.getTerm();
501 } else if (context.getReplicatedLog().getSnapshotIndex() > -1) {
502 lastAppliedIndex = context.getReplicatedLog().getSnapshotIndex();
503 lastAppliedTerm = context.getReplicatedLog().getSnapshotTerm();
506 boolean isInstallSnapshotInitiated = true;
507 actor().tell(new CaptureSnapshot(lastIndex(), lastTerm(),
508 lastAppliedIndex, lastAppliedTerm, isInstallSnapshotInitiated),
513 private void sendInstallSnapshot() {
514 for (String followerId : followers) {
515 ActorSelection followerActor = context.getPeerActorSelection(followerId);
517 if(followerActor != null) {
518 FollowerLogInformation followerLogInformation = followerToLog.get(followerId);
519 long nextIndex = followerLogInformation.getNextIndex().get();
521 if (!context.getReplicatedLog().isPresent(nextIndex) &&
522 context.getReplicatedLog().isInSnapshot(nextIndex)) {
523 sendSnapshotChunk(followerActor, followerId);
530 * Sends a snapshot chunk to a given follower
531 * InstallSnapshot should qualify as a heartbeat too.
533 private void sendSnapshotChunk(ActorSelection followerActor, String followerId) {
535 if (snapshot.isPresent()) {
537 new InstallSnapshot(currentTerm(), context.getId(),
538 context.getReplicatedLog().getSnapshotIndex(),
539 context.getReplicatedLog().getSnapshotTerm(),
540 getNextSnapshotChunk(followerId,snapshot.get()),
541 mapFollowerToSnapshot.get(followerId).incrementChunkIndex(),
542 mapFollowerToSnapshot.get(followerId).getTotalChunks()
546 LOG.info("InstallSnapshot sent to follower {}, Chunk: {}/{}",
547 followerActor.path(), mapFollowerToSnapshot.get(followerId).getChunkIndex(),
548 mapFollowerToSnapshot.get(followerId).getTotalChunks());
550 } catch (IOException e) {
551 LOG.error(e, "InstallSnapshot failed for Leader.");
556 * Acccepts snaphot as ByteString, enters into map for future chunks
557 * creates and return a ByteString chunk
559 private ByteString getNextSnapshotChunk(String followerId, ByteString snapshotBytes) throws IOException {
560 FollowerToSnapshot followerToSnapshot = mapFollowerToSnapshot.get(followerId);
561 if (followerToSnapshot == null) {
562 followerToSnapshot = new FollowerToSnapshot(snapshotBytes);
563 mapFollowerToSnapshot.put(followerId, followerToSnapshot);
565 ByteString nextChunk = followerToSnapshot.getNextChunk();
566 if (LOG.isDebugEnabled()) {
567 LOG.debug("Leader's snapshot nextChunk size:{}", nextChunk.size());
572 private void sendHeartBeat() {
573 if (followers.size() > 0) {
578 private void stopHeartBeat() {
579 if (heartbeatSchedule != null && !heartbeatSchedule.isCancelled()) {
580 heartbeatSchedule.cancel();
584 private void scheduleHeartBeat(FiniteDuration interval) {
585 if(followers.size() == 0){
586 // Optimization - do not bother scheduling a heartbeat as there are
593 // Schedule a heartbeat. When the scheduler triggers a SendHeartbeat
594 // message is sent to itself.
595 // Scheduling the heartbeat only once here because heartbeats do not
596 // need to be sent if there are other messages being sent to the remote
598 heartbeatSchedule = context.getActorSystem().scheduler().scheduleOnce(
599 interval, context.getActor(), new SendHeartBeat(),
600 context.getActorSystem().dispatcher(), context.getActor());
604 public void close() throws Exception {
609 public String getLeaderId() {
610 return context.getId();
613 protected boolean isLeaderIsolated() {
614 int minPresent = minIsolatedLeaderPeerCount;
615 for (FollowerLogInformation followerLogInformation : followerToLog.values()) {
616 if (followerLogInformation.isFollowerActive()) {
618 if (minPresent == 0) {
623 return (minPresent != 0);
627 * Encapsulates the snapshot bytestring and handles the logic of sending
630 protected class FollowerToSnapshot {
631 private ByteString snapshotBytes;
632 private int offset = 0;
633 // the next snapshot chunk is sent only if the replyReceivedForOffset matches offset
634 private int replyReceivedForOffset;
635 // if replyStatus is false, the previous chunk is attempted
636 private boolean replyStatus = false;
637 private int chunkIndex;
638 private int totalChunks;
640 public FollowerToSnapshot(ByteString snapshotBytes) {
641 this.snapshotBytes = snapshotBytes;
642 replyReceivedForOffset = -1;
644 int size = snapshotBytes.size();
645 totalChunks = ( size / context.getConfigParams().getSnapshotChunkSize()) +
646 ((size % context.getConfigParams().getSnapshotChunkSize()) > 0 ? 1 : 0);
647 if(LOG.isDebugEnabled()) {
648 LOG.debug("Snapshot {} bytes, total chunks to send:{}",
653 public ByteString getSnapshotBytes() {
654 return snapshotBytes;
657 public int incrementOffset() {
659 // if prev chunk failed, we would want to sent the same chunk again
660 offset = offset + context.getConfigParams().getSnapshotChunkSize();
665 public int incrementChunkIndex() {
667 // if prev chunk failed, we would want to sent the same chunk again
668 chunkIndex = chunkIndex + 1;
673 public int getChunkIndex() {
677 public int getTotalChunks() {
681 public boolean canSendNextChunk() {
682 // we only send a false if a chunk is sent but we have not received a reply yet
683 return replyReceivedForOffset == offset;
686 public boolean isLastChunk(int chunkIndex) {
687 return totalChunks == chunkIndex;
690 public void markSendStatus(boolean success) {
692 // if the chunk sent was successful
693 replyReceivedForOffset = offset;
696 // if the chunk sent was failure
697 replyReceivedForOffset = offset;
702 public ByteString getNextChunk() {
703 int snapshotLength = getSnapshotBytes().size();
704 int start = incrementOffset();
705 int size = context.getConfigParams().getSnapshotChunkSize();
706 if (context.getConfigParams().getSnapshotChunkSize() > snapshotLength) {
707 size = snapshotLength;
709 if ((start + context.getConfigParams().getSnapshotChunkSize()) > snapshotLength) {
710 size = snapshotLength - start;
714 if(LOG.isDebugEnabled()) {
715 LOG.debug("length={}, offset={},size={}",
716 snapshotLength, start, size);
718 return getSnapshotBytes().substring(start, start + size);
723 // called from example-actor for printing the follower-states
724 public String printFollowerStates() {
725 StringBuilder sb = new StringBuilder();
726 for(FollowerLogInformation followerLogInformation : followerToLog.values()) {
727 boolean isFollowerActive = followerLogInformation.isFollowerActive();
728 sb.append("{"+followerLogInformation.getId() + " state:" + isFollowerActive + "},");
731 return "[" + sb.toString() + "]";
735 void markFollowerActive(String followerId) {
736 followerToLog.get(followerId).markFollowerActive();