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.base.Preconditions;
15 import com.google.protobuf.ByteString;
16 import org.opendaylight.controller.cluster.raft.ClientRequestTracker;
17 import org.opendaylight.controller.cluster.raft.ClientRequestTrackerImpl;
18 import org.opendaylight.controller.cluster.raft.FollowerLogInformation;
19 import org.opendaylight.controller.cluster.raft.FollowerLogInformationImpl;
20 import org.opendaylight.controller.cluster.raft.RaftActorContext;
21 import org.opendaylight.controller.cluster.raft.RaftState;
22 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
23 import org.opendaylight.controller.cluster.raft.base.messages.Replicate;
24 import org.opendaylight.controller.cluster.raft.base.messages.SendHeartBeat;
25 import org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot;
26 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
27 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
28 import org.opendaylight.controller.cluster.raft.messages.InstallSnapshot;
29 import org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply;
30 import org.opendaylight.controller.cluster.raft.messages.RaftRPC;
31 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
32 import scala.concurrent.duration.FiniteDuration;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.List;
41 import java.util.concurrent.TimeUnit;
42 import java.util.concurrent.atomic.AtomicLong;
45 * The behavior of a RaftActor when it is in the Leader state
49 * <li> Upon election: send initial empty AppendEntries RPCs
50 * (heartbeat) to each server; repeat during idle periods to
51 * prevent election timeouts (§5.2)
52 * <li> If command received from client: append entry to local log,
53 * respond after entry applied to state machine (§5.3)
54 * <li> If last log index ≥ nextIndex for a follower: send
55 * AppendEntries RPC with log entries starting at nextIndex
57 * <li> If successful: update nextIndex and matchIndex for
59 * <li> If AppendEntries fails because of log inconsistency:
60 * decrement nextIndex and retry (§5.3)
62 * <li> If there exists an N such that N > commitIndex, a majority
63 * of matchIndex[i] ≥ N, and log[N].term == currentTerm:
64 * set commitIndex = N (§5.3, §5.4).
66 public class Leader extends AbstractRaftActorBehavior {
69 protected final Map<String, FollowerLogInformation> followerToLog =
71 protected final Map<String, FollowerToSnapshot> mapFollowerToSnapshot = new HashMap<>();
73 private final Set<String> followers;
75 private Cancellable heartbeatSchedule = null;
76 private Cancellable appendEntriesSchedule = null;
77 private Cancellable installSnapshotSchedule = null;
79 private List<ClientRequestTracker> trackerList = new ArrayList<>();
81 private final int minReplicationCount;
83 public Leader(RaftActorContext context) {
86 if (lastIndex() >= 0) {
87 context.setCommitIndex(lastIndex());
90 followers = context.getPeerAddresses().keySet();
92 for (String followerId : followers) {
93 FollowerLogInformation followerLogInformation =
94 new FollowerLogInformationImpl(followerId,
95 new AtomicLong(lastIndex()),
98 followerToLog.put(followerId, followerLogInformation);
101 context.getLogger().debug("Election:Leader has following peers:"+ followers);
103 if (followers.size() > 0) {
104 minReplicationCount = (followers.size() + 1) / 2 + 1;
106 minReplicationCount = 0;
110 // Immediately schedule a heartbeat
111 // Upon election: send initial empty AppendEntries RPCs
112 // (heartbeat) to each server; repeat during idle periods to
113 // prevent election timeouts (§5.2)
114 scheduleHeartBeat(new FiniteDuration(0, TimeUnit.SECONDS));
116 scheduleInstallSnapshotCheck(
117 new FiniteDuration(context.getConfigParams().getHeartBeatInterval().length() * 1000,
118 context.getConfigParams().getHeartBeatInterval().unit())
123 @Override protected RaftState handleAppendEntries(ActorRef sender,
124 AppendEntries appendEntries) {
126 context.getLogger().debug(appendEntries.toString());
131 @Override protected RaftState handleAppendEntriesReply(ActorRef sender,
132 AppendEntriesReply appendEntriesReply) {
134 if(! appendEntriesReply.isSuccess()) {
136 .debug(appendEntriesReply.toString());
139 // Update the FollowerLogInformation
140 String followerId = appendEntriesReply.getFollowerId();
141 FollowerLogInformation followerLogInformation =
142 followerToLog.get(followerId);
144 if(followerLogInformation == null){
145 context.getLogger().error("Unknown follower {}", followerId);
149 if (appendEntriesReply.isSuccess()) {
150 followerLogInformation
151 .setMatchIndex(appendEntriesReply.getLogLastIndex());
152 followerLogInformation
153 .setNextIndex(appendEntriesReply.getLogLastIndex() + 1);
156 // TODO: When we find that the follower is out of sync with the
157 // Leader we simply decrement that followers next index by 1.
158 // Would it be possible to do better than this? The RAFT spec
159 // does not explicitly deal with it but may be something for us to
162 followerLogInformation.decrNextIndex();
165 // Now figure out if this reply warrants a change in the commitIndex
166 // If there exists an N such that N > commitIndex, a majority
167 // of matchIndex[i] ≥ N, and log[N].term == currentTerm:
168 // set commitIndex = N (§5.3, §5.4).
169 for (long N = context.getCommitIndex() + 1; ; N++) {
170 int replicatedCount = 1;
172 for (FollowerLogInformation info : followerToLog.values()) {
173 if (info.getMatchIndex().get() >= N) {
178 if (replicatedCount >= minReplicationCount) {
179 ReplicatedLogEntry replicatedLogEntry =
180 context.getReplicatedLog().get(N);
181 if (replicatedLogEntry != null
182 && replicatedLogEntry.getTerm()
184 context.setCommitIndex(N);
191 // Apply the change to the state machine
192 if (context.getCommitIndex() > context.getLastApplied()) {
193 applyLogToStateMachine(context.getCommitIndex());
199 protected ClientRequestTracker findClientRequestTracker(long logIndex) {
200 for (ClientRequestTracker tracker : trackerList) {
201 if (tracker.getIndex() == logIndex) {
209 @Override protected RaftState handleRequestVoteReply(ActorRef sender,
210 RequestVoteReply requestVoteReply) {
214 @Override public RaftState state() {
215 return RaftState.Leader;
218 @Override public RaftState handleMessage(ActorRef sender, Object originalMessage) {
219 Preconditions.checkNotNull(sender, "sender should not be null");
221 Object message = fromSerializableMessage(originalMessage);
223 if (message instanceof RaftRPC) {
224 RaftRPC rpc = (RaftRPC) message;
225 // If RPC request or response contains term T > currentTerm:
226 // set currentTerm = T, convert to follower (§5.1)
227 // This applies to all RPC messages and responses
228 if (rpc.getTerm() > context.getTermInformation().getCurrentTerm()) {
229 context.getTermInformation().updateAndPersist(rpc.getTerm(), null);
230 return RaftState.Follower;
235 if (message instanceof SendHeartBeat) {
236 return sendHeartBeat();
237 } else if(message instanceof SendInstallSnapshot) {
238 installSnapshotIfNeeded();
239 } else if (message instanceof Replicate) {
240 replicate((Replicate) message);
241 } else if (message instanceof InstallSnapshotReply){
242 handleInstallSnapshotReply(
243 (InstallSnapshotReply) message);
246 scheduleHeartBeat(context.getConfigParams().getHeartBeatInterval());
249 return super.handleMessage(sender, message);
252 private void handleInstallSnapshotReply(InstallSnapshotReply reply) {
253 String followerId = reply.getFollowerId();
254 FollowerToSnapshot followerToSnapshot =
255 mapFollowerToSnapshot.get(followerId);
257 if (followerToSnapshot != null &&
258 followerToSnapshot.getChunkIndex() == reply.getChunkIndex()) {
260 if (reply.isSuccess()) {
261 if(followerToSnapshot.isLastChunk(reply.getChunkIndex())) {
262 //this was the last chunk reply
263 context.getLogger().debug("InstallSnapshotReply received, " +
264 "last chunk received, Chunk:{}. Follower:{} Setting nextIndex:{}",
265 reply.getChunkIndex(), followerId,
266 context.getReplicatedLog().getSnapshotIndex() + 1);
268 FollowerLogInformation followerLogInformation =
269 followerToLog.get(followerId);
270 followerLogInformation.setMatchIndex(
271 context.getReplicatedLog().getSnapshotIndex());
272 followerLogInformation.setNextIndex(
273 context.getReplicatedLog().getSnapshotIndex() + 1);
274 mapFollowerToSnapshot.remove(followerId);
275 context.getLogger().debug("followerToLog.get(followerId).getNextIndex().get()=" +
276 followerToLog.get(followerId).getNextIndex().get());
279 followerToSnapshot.markSendStatus(true);
282 context.getLogger().info("InstallSnapshotReply received, " +
283 "sending snapshot chunk failed, Will retry, Chunk:{}",
284 reply.getChunkIndex());
285 followerToSnapshot.markSendStatus(false);
289 context.getLogger().error("ERROR!!" +
290 "FollowerId in InstallSnapshotReply not known to Leader" +
291 " or Chunk Index in InstallSnapshotReply not matching {} != {}",
292 followerToSnapshot.getChunkIndex(), reply.getChunkIndex() );
296 private void replicate(Replicate replicate) {
297 long logIndex = replicate.getReplicatedLogEntry().getIndex();
299 context.getLogger().debug("Replicate message " + logIndex);
301 // Create a tracker entry we will use this later to notify the
304 new ClientRequestTrackerImpl(replicate.getClientActor(),
305 replicate.getIdentifier(),
309 if (followers.size() == 0) {
310 context.setCommitIndex(logIndex);
311 applyLogToStateMachine(logIndex);
317 private void sendAppendEntries() {
318 // Send an AppendEntries to all followers
319 for (String followerId : followers) {
320 ActorSelection followerActor = context.getPeerActorSelection(followerId);
322 if (followerActor != null) {
323 FollowerLogInformation followerLogInformation = followerToLog.get(followerId);
324 long followerNextIndex = followerLogInformation.getNextIndex().get();
325 List<ReplicatedLogEntry> entries = Collections.emptyList();
327 if (mapFollowerToSnapshot.get(followerId) != null) {
328 if (mapFollowerToSnapshot.get(followerId).canSendNextChunk()) {
329 sendSnapshotChunk(followerActor, followerId);
334 if (context.getReplicatedLog().isPresent(followerNextIndex)) {
335 // FIXME : Sending one entry at a time
336 entries = context.getReplicatedLog().getFrom(followerNextIndex, 1);
339 new AppendEntries(currentTerm(), context.getId(),
340 prevLogIndex(followerNextIndex),
341 prevLogTerm(followerNextIndex), entries,
342 context.getCommitIndex()).toSerializable(),
347 // if the followers next index is not present in the leaders log, then snapshot should be sent
348 long leaderSnapShotIndex = context.getReplicatedLog().getSnapshotIndex();
349 long leaderLastIndex = context.getReplicatedLog().lastIndex();
350 if (followerNextIndex >= 0 && leaderLastIndex >= followerNextIndex ) {
351 // if the follower is just not starting and leader's index
352 // is more than followers index
353 context.getLogger().debug("SendInstallSnapshot to follower:{}," +
354 "follower-nextIndex:{}, leader-snapshot-index:{}, " +
355 "leader-last-index:{}", followerId,
356 followerNextIndex, leaderSnapShotIndex, leaderLastIndex);
358 actor().tell(new SendInstallSnapshot(), actor());
361 new AppendEntries(currentTerm(), context.getId(),
362 prevLogIndex(followerNextIndex),
363 prevLogTerm(followerNextIndex), entries,
364 context.getCommitIndex()).toSerializable(),
375 * An installSnapshot is scheduled at a interval that is a multiple of
376 * a HEARTBEAT_INTERVAL. This is to avoid the need to check for installing
377 * snapshots at every heartbeat.
379 private void installSnapshotIfNeeded(){
380 for (String followerId : followers) {
381 ActorSelection followerActor =
382 context.getPeerActorSelection(followerId);
384 if(followerActor != null) {
385 FollowerLogInformation followerLogInformation =
386 followerToLog.get(followerId);
388 long nextIndex = followerLogInformation.getNextIndex().get();
390 if (!context.getReplicatedLog().isPresent(nextIndex) &&
391 context.getReplicatedLog().isInSnapshot(nextIndex)) {
392 sendSnapshotChunk(followerActor, followerId);
399 * Sends a snapshot chunk to a given follower
400 * InstallSnapshot should qualify as a heartbeat too.
402 private void sendSnapshotChunk(ActorSelection followerActor, String followerId) {
405 new InstallSnapshot(currentTerm(), context.getId(),
406 context.getReplicatedLog().getSnapshotIndex(),
407 context.getReplicatedLog().getSnapshotTerm(),
408 getNextSnapshotChunk(followerId,
409 context.getReplicatedLog().getSnapshot()),
410 mapFollowerToSnapshot.get(followerId).incrementChunkIndex(),
411 mapFollowerToSnapshot.get(followerId).getTotalChunks()
415 context.getLogger().info("InstallSnapshot sent to follower {}, Chunk: {}/{}",
416 followerActor.path(), mapFollowerToSnapshot.get(followerId).getChunkIndex(),
417 mapFollowerToSnapshot.get(followerId).getTotalChunks());
418 } catch (IOException e) {
419 context.getLogger().error("InstallSnapshot failed for Leader.", e);
424 * Acccepts snaphot as ByteString, enters into map for future chunks
425 * creates and return a ByteString chunk
427 private ByteString getNextSnapshotChunk(String followerId, ByteString snapshotBytes) throws IOException {
428 FollowerToSnapshot followerToSnapshot = mapFollowerToSnapshot.get(followerId);
429 if (followerToSnapshot == null) {
430 followerToSnapshot = new FollowerToSnapshot(snapshotBytes);
431 mapFollowerToSnapshot.put(followerId, followerToSnapshot);
433 ByteString nextChunk = followerToSnapshot.getNextChunk();
434 context.getLogger().debug("Leader's snapshot nextChunk size:{}", nextChunk.size());
439 private RaftState sendHeartBeat() {
440 if (followers.size() > 0) {
446 private void stopHeartBeat() {
447 if (heartbeatSchedule != null && !heartbeatSchedule.isCancelled()) {
448 heartbeatSchedule.cancel();
452 private void stopInstallSnapshotSchedule() {
453 if (installSnapshotSchedule != null && !installSnapshotSchedule.isCancelled()) {
454 installSnapshotSchedule.cancel();
458 private void scheduleHeartBeat(FiniteDuration interval) {
459 if(followers.size() == 0){
460 // Optimization - do not bother scheduling a heartbeat as there are
467 // Schedule a heartbeat. When the scheduler triggers a SendHeartbeat
468 // message is sent to itself.
469 // Scheduling the heartbeat only once here because heartbeats do not
470 // need to be sent if there are other messages being sent to the remote
473 context.getActorSystem().scheduler().scheduleOnce(
475 context.getActor(), new SendHeartBeat(),
476 context.getActorSystem().dispatcher(), context.getActor());
480 private void scheduleInstallSnapshotCheck(FiniteDuration interval) {
481 if(followers.size() == 0){
482 // Optimization - do not bother scheduling a heartbeat as there are
487 stopInstallSnapshotSchedule();
489 // Schedule a message to send append entries to followers that can
490 // accept an append entries with some data in it
491 installSnapshotSchedule =
492 context.getActorSystem().scheduler().scheduleOnce(
494 context.getActor(), new SendInstallSnapshot(),
495 context.getActorSystem().dispatcher(), context.getActor());
500 @Override public void close() throws Exception {
504 @Override public String getLeaderId() {
505 return context.getId();
509 * Encapsulates the snapshot bytestring and handles the logic of sending
512 protected class FollowerToSnapshot {
513 private ByteString snapshotBytes;
514 private int offset = 0;
515 // the next snapshot chunk is sent only if the replyReceivedForOffset matches offset
516 private int replyReceivedForOffset;
517 // if replyStatus is false, the previous chunk is attempted
518 private boolean replyStatus = false;
519 private int chunkIndex;
520 private int totalChunks;
522 public FollowerToSnapshot(ByteString snapshotBytes) {
523 this.snapshotBytes = snapshotBytes;
524 replyReceivedForOffset = -1;
526 int size = snapshotBytes.size();
527 totalChunks = ( size / context.getConfigParams().getSnapshotChunkSize()) +
528 ((size % context.getConfigParams().getSnapshotChunkSize()) > 0 ? 1 : 0);
529 context.getLogger().debug("Snapshot {} bytes, total chunks to send:{}",
533 public ByteString getSnapshotBytes() {
534 return snapshotBytes;
537 public int incrementOffset() {
539 // if prev chunk failed, we would want to sent the same chunk again
540 offset = offset + context.getConfigParams().getSnapshotChunkSize();
545 public int incrementChunkIndex() {
547 // if prev chunk failed, we would want to sent the same chunk again
548 chunkIndex = chunkIndex + 1;
553 public int getChunkIndex() {
557 public int getTotalChunks() {
561 public boolean canSendNextChunk() {
562 // we only send a false if a chunk is sent but we have not received a reply yet
563 return replyReceivedForOffset == offset;
566 public boolean isLastChunk(int chunkIndex) {
567 return totalChunks == chunkIndex;
570 public void markSendStatus(boolean success) {
572 // if the chunk sent was successful
573 replyReceivedForOffset = offset;
576 // if the chunk sent was failure
577 replyReceivedForOffset = offset;
582 public ByteString getNextChunk() {
583 int snapshotLength = getSnapshotBytes().size();
584 int start = incrementOffset();
585 int size = context.getConfigParams().getSnapshotChunkSize();
586 if (context.getConfigParams().getSnapshotChunkSize() > snapshotLength) {
587 size = snapshotLength;
589 if ((start + context.getConfigParams().getSnapshotChunkSize()) > snapshotLength) {
590 size = snapshotLength - start;
594 context.getLogger().debug("length={}, offset={},size={}",
595 snapshotLength, start, size);
596 return getSnapshotBytes().substring(start, start + size);