Merge "Do not retain NetconfConnectModule reference"
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / AbstractLeader.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.raft.behaviors;
10
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;
23 import java.util.Map;
24 import java.util.Set;
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;
46
47 /**
48  * The behavior of a RaftActor when it is in the Leader state
49  * <p/>
50  * Leaders:
51  * <ul>
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
59  * <ul>
60  * <li> If successful: update nextIndex and matchIndex for
61  * follower (§5.3)
62  * <li> If AppendEntries fails because of log inconsistency:
63  * decrement nextIndex and retry (§5.3)
64  * </ul>
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).
68  */
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<>();
72
73     protected final Set<String> followers;
74
75     private Cancellable heartbeatSchedule = null;
76
77     private List<ClientRequestTracker> trackerList = new ArrayList<>();
78
79     protected final int minReplicationCount;
80
81     protected final int minIsolatedLeaderPeerCount;
82
83     private Optional<ByteString> snapshot;
84
85     public AbstractLeader(RaftActorContext context) {
86         super(context);
87
88         followers = context.getPeerAddresses().keySet();
89
90         for (String followerId : followers) {
91             FollowerLogInformation followerLogInformation =
92                 new FollowerLogInformationImpl(followerId,
93                     new AtomicLong(context.getCommitIndex()),
94                     new AtomicLong(-1),
95                     context.getConfigParams().getElectionTimeOutInterval());
96
97             followerToLog.put(followerId, followerLogInformation);
98         }
99
100         leaderId = context.getId();
101
102         if(LOG.isDebugEnabled()) {
103             LOG.debug("Election:Leader has following peers: {}", followers);
104         }
105
106         minReplicationCount = getMajorityVoteCount(followers.size());
107
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
110         // for e.g
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;
115
116         snapshot = Optional.absent();
117
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));
123     }
124
125     private Optional<ByteString> getSnapshot() {
126         return snapshot;
127     }
128
129     @VisibleForTesting
130     void setSnapshot(Optional<ByteString> snapshot) {
131         this.snapshot = snapshot;
132     }
133
134     @Override
135     protected RaftActorBehavior handleAppendEntries(ActorRef sender,
136         AppendEntries appendEntries) {
137
138         if(LOG.isDebugEnabled()) {
139             LOG.debug(appendEntries.toString());
140         }
141
142         return this;
143     }
144
145     @Override
146     protected RaftActorBehavior handleAppendEntriesReply(ActorRef sender,
147         AppendEntriesReply appendEntriesReply) {
148
149         if(! appendEntriesReply.isSuccess()) {
150             if(LOG.isDebugEnabled()) {
151                 LOG.debug(appendEntriesReply.toString());
152             }
153         }
154
155         // Update the FollowerLogInformation
156         String followerId = appendEntriesReply.getFollowerId();
157         FollowerLogInformation followerLogInformation =
158             followerToLog.get(followerId);
159
160         if(followerLogInformation == null){
161             LOG.error("Unknown follower {}", followerId);
162             return this;
163         }
164
165         followerLogInformation.markFollowerActive();
166
167         if (appendEntriesReply.isSuccess()) {
168             followerLogInformation
169                 .setMatchIndex(appendEntriesReply.getLogLastIndex());
170             followerLogInformation
171                 .setNextIndex(appendEntriesReply.getLogLastIndex() + 1);
172         } else {
173
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
178             // think about
179
180             followerLogInformation.decrNextIndex();
181         }
182
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;
189
190             for (FollowerLogInformation info : followerToLog.values()) {
191                 if (info.getMatchIndex().get() >= N) {
192                     replicatedCount++;
193                 }
194             }
195
196             if (replicatedCount >= minReplicationCount) {
197                 ReplicatedLogEntry replicatedLogEntry = context.getReplicatedLog().get(N);
198                 if (replicatedLogEntry != null &&
199                     replicatedLogEntry.getTerm() == currentTerm()) {
200                     context.setCommitIndex(N);
201                 }
202             } else {
203                 break;
204             }
205         }
206
207         // Apply the change to the state machine
208         if (context.getCommitIndex() > context.getLastApplied()) {
209             applyLogToStateMachine(context.getCommitIndex());
210         }
211
212         return this;
213     }
214
215     protected ClientRequestTracker removeClientRequestTracker(long logIndex) {
216
217         ClientRequestTracker toRemove = findClientRequestTracker(logIndex);
218         if(toRemove != null) {
219             trackerList.remove(toRemove);
220         }
221
222         return toRemove;
223     }
224
225     protected ClientRequestTracker findClientRequestTracker(long logIndex) {
226         for (ClientRequestTracker tracker : trackerList) {
227             if (tracker.getIndex() == logIndex) {
228                 return tracker;
229             }
230         }
231         return null;
232     }
233
234     @Override
235     protected RaftActorBehavior handleRequestVoteReply(ActorRef sender,
236         RequestVoteReply requestVoteReply) {
237         return this;
238     }
239
240     @Override
241     public RaftState state() {
242         return RaftState.Leader;
243     }
244
245     @Override
246     public RaftActorBehavior handleMessage(ActorRef sender, Object originalMessage) {
247         Preconditions.checkNotNull(sender, "sender should not be null");
248
249         Object message = fromSerializableMessage(originalMessage);
250
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);
258
259                 return switchBehavior(new Follower(context));
260             }
261         }
262
263         try {
264             if (message instanceof SendHeartBeat) {
265                 sendHeartBeat();
266                 return this;
267
268             } else if(message instanceof InitiateInstallSnapshot) {
269                 installSnapshotIfNeeded();
270
271             } else if(message instanceof SendInstallSnapshot) {
272                 // received from RaftActor
273                 setSnapshot(Optional.of(((SendInstallSnapshot) message).getSnapshot()));
274                 sendInstallSnapshot();
275
276             } else if (message instanceof Replicate) {
277                 replicate((Replicate) message);
278
279             } else if (message instanceof InstallSnapshotReply){
280                 handleInstallSnapshotReply((InstallSnapshotReply) message);
281
282             }
283         } finally {
284             scheduleHeartBeat(context.getConfigParams().getHeartBeatInterval());
285         }
286
287         return super.handleMessage(sender, message);
288     }
289
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();
295
296         if (followerToSnapshot != null &&
297             followerToSnapshot.getChunkIndex() == reply.getChunkIndex()) {
298
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
307                         );
308                     }
309
310                     followerLogInformation.setMatchIndex(
311                         context.getReplicatedLog().getSnapshotIndex());
312                     followerLogInformation.setNextIndex(
313                         context.getReplicatedLog().getSnapshotIndex() + 1);
314                     mapFollowerToSnapshot.remove(followerId);
315
316                     if(LOG.isDebugEnabled()) {
317                         LOG.debug("followerToLog.get(followerId).getNextIndex().get()=" +
318                             followerToLog.get(followerId).getNextIndex().get());
319                     }
320
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());
325                     }
326
327                 } else {
328                     followerToSnapshot.markSendStatus(true);
329                 }
330             } else {
331                 LOG.info("InstallSnapshotReply received, " +
332                         "sending snapshot chunk failed, Will retry, Chunk:{}",
333                     reply.getChunkIndex()
334                 );
335                 followerToSnapshot.markSendStatus(false);
336             }
337
338         } else {
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()
343             );
344         }
345     }
346
347     private void replicate(Replicate replicate) {
348         long logIndex = replicate.getReplicatedLogEntry().getIndex();
349
350         if(LOG.isDebugEnabled()) {
351             LOG.debug("Replicate message {}", logIndex);
352         }
353
354         // Create a tracker entry we will use this later to notify the
355         // client actor
356         trackerList.add(
357             new ClientRequestTrackerImpl(replicate.getClientActor(),
358                 replicate.getIdentifier(),
359                 logIndex)
360         );
361
362         if (followers.size() == 0) {
363             context.setCommitIndex(logIndex);
364             applyLogToStateMachine(logIndex);
365         } else {
366             sendAppendEntries();
367         }
368     }
369
370     private void sendAppendEntries() {
371         // Send an AppendEntries to all followers
372         for (String followerId : followers) {
373             ActorSelection followerActor = context.getPeerActorSelection(followerId);
374
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;
380
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);
385                     } else {
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());
389                     }
390
391                 } else {
392                     long leaderLastIndex = context.getReplicatedLog().lastIndex();
393                     long leaderSnapShotIndex = context.getReplicatedLog().getSnapshotIndex();
394
395                     if (isFollowerActive &&
396                         context.getReplicatedLog().isPresent(followerNextIndex)) {
397                         // FIXME : Sending one entry at a time
398                         entries = context.getReplicatedLog().getFrom(followerNextIndex, 1);
399
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
405
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
411                             );
412                         }
413                         actor().tell(new InitiateInstallSnapshot(), actor());
414
415                         // we would want to sent AE as the capture snapshot might take time
416                         entries =  Collections.<ReplicatedLogEntry>emptyList();
417
418                     } else {
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();
422                     }
423
424                     sendAppendEntriesToFollower(followerActor, followerNextIndex, entries);
425
426                 }
427             }
428         }
429     }
430
431     private void sendAppendEntriesToFollower(ActorSelection followerActor, long followerNextIndex,
432         List<ReplicatedLogEntry> entries) {
433         followerActor.tell(
434             new AppendEntries(currentTerm(), context.getId(),
435                 prevLogIndex(followerNextIndex),
436                 prevLogTerm(followerNextIndex), entries,
437                 context.getCommitIndex()).toSerializable(),
438             actor()
439         );
440     }
441
442     /**
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.
446      *
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.
456      *
457      */
458     private void installSnapshotIfNeeded() {
459         for (String followerId : followers) {
460             ActorSelection followerActor =
461                 context.getPeerActorSelection(followerId);
462
463             if(followerActor != null) {
464                 FollowerLogInformation followerLogInformation =
465                     followerToLog.get(followerId);
466
467                 long nextIndex = followerLogInformation.getNextIndex().get();
468
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);
476
477                     } else {
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
482                         break;
483                     }
484
485                 }
486             }
487         }
488     }
489
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;
497
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();
504         }
505
506         boolean isInstallSnapshotInitiated = true;
507         actor().tell(new CaptureSnapshot(lastIndex(), lastTerm(),
508                 lastAppliedIndex, lastAppliedTerm, isInstallSnapshotInitiated),
509             actor());
510     }
511
512
513     private void sendInstallSnapshot() {
514         for (String followerId : followers) {
515             ActorSelection followerActor = context.getPeerActorSelection(followerId);
516
517             if(followerActor != null) {
518                 FollowerLogInformation followerLogInformation = followerToLog.get(followerId);
519                 long nextIndex = followerLogInformation.getNextIndex().get();
520
521                 if (!context.getReplicatedLog().isPresent(nextIndex) &&
522                     context.getReplicatedLog().isInSnapshot(nextIndex)) {
523                     sendSnapshotChunk(followerActor, followerId);
524                 }
525             }
526         }
527     }
528
529     /**
530      *  Sends a snapshot chunk to a given follower
531      *  InstallSnapshot should qualify as a heartbeat too.
532      */
533     private void sendSnapshotChunk(ActorSelection followerActor, String followerId) {
534         try {
535             if (snapshot.isPresent()) {
536                 followerActor.tell(
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()
543                     ).toSerializable(),
544                     actor()
545                 );
546                 LOG.info("InstallSnapshot sent to follower {}, Chunk: {}/{}",
547                     followerActor.path(), mapFollowerToSnapshot.get(followerId).getChunkIndex(),
548                     mapFollowerToSnapshot.get(followerId).getTotalChunks());
549             }
550         } catch (IOException e) {
551             LOG.error(e, "InstallSnapshot failed for Leader.");
552         }
553     }
554
555     /**
556      * Acccepts snaphot as ByteString, enters into map for future chunks
557      * creates and return a ByteString chunk
558      */
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);
564         }
565         ByteString nextChunk = followerToSnapshot.getNextChunk();
566         if (LOG.isDebugEnabled()) {
567             LOG.debug("Leader's snapshot nextChunk size:{}", nextChunk.size());
568         }
569         return nextChunk;
570     }
571
572     private void sendHeartBeat() {
573         if (followers.size() > 0) {
574             sendAppendEntries();
575         }
576     }
577
578     private void stopHeartBeat() {
579         if (heartbeatSchedule != null && !heartbeatSchedule.isCancelled()) {
580             heartbeatSchedule.cancel();
581         }
582     }
583
584     private void scheduleHeartBeat(FiniteDuration interval) {
585         if(followers.size() == 0){
586             // Optimization - do not bother scheduling a heartbeat as there are
587             // no followers
588             return;
589         }
590
591         stopHeartBeat();
592
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
597         // actor.
598         heartbeatSchedule = context.getActorSystem().scheduler().scheduleOnce(
599             interval, context.getActor(), new SendHeartBeat(),
600             context.getActorSystem().dispatcher(), context.getActor());
601     }
602
603     @Override
604     public void close() throws Exception {
605         stopHeartBeat();
606     }
607
608     @Override
609     public String getLeaderId() {
610         return context.getId();
611     }
612
613     protected boolean isLeaderIsolated() {
614         int minPresent = minIsolatedLeaderPeerCount;
615         for (FollowerLogInformation followerLogInformation : followerToLog.values()) {
616             if (followerLogInformation.isFollowerActive()) {
617                 --minPresent;
618                 if (minPresent == 0) {
619                     break;
620                 }
621             }
622         }
623         return (minPresent != 0);
624     }
625
626     /**
627      * Encapsulates the snapshot bytestring and handles the logic of sending
628      * snapshot chunks
629      */
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;
639
640         public FollowerToSnapshot(ByteString snapshotBytes) {
641             this.snapshotBytes = snapshotBytes;
642             replyReceivedForOffset = -1;
643             chunkIndex = 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:{}",
649                     size, totalChunks);
650             }
651         }
652
653         public ByteString getSnapshotBytes() {
654             return snapshotBytes;
655         }
656
657         public int incrementOffset() {
658             if(replyStatus) {
659                 // if prev chunk failed, we would want to sent the same chunk again
660                 offset = offset + context.getConfigParams().getSnapshotChunkSize();
661             }
662             return offset;
663         }
664
665         public int incrementChunkIndex() {
666             if (replyStatus) {
667                 // if prev chunk failed, we would want to sent the same chunk again
668                 chunkIndex =  chunkIndex + 1;
669             }
670             return chunkIndex;
671         }
672
673         public int getChunkIndex() {
674             return chunkIndex;
675         }
676
677         public int getTotalChunks() {
678             return totalChunks;
679         }
680
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;
684         }
685
686         public boolean isLastChunk(int chunkIndex) {
687             return totalChunks == chunkIndex;
688         }
689
690         public void markSendStatus(boolean success) {
691             if (success) {
692                 // if the chunk sent was successful
693                 replyReceivedForOffset = offset;
694                 replyStatus = true;
695             } else {
696                 // if the chunk sent was failure
697                 replyReceivedForOffset = offset;
698                 replyStatus = false;
699             }
700         }
701
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;
708             } else {
709                 if ((start + context.getConfigParams().getSnapshotChunkSize()) > snapshotLength) {
710                     size = snapshotLength - start;
711                 }
712             }
713
714             if(LOG.isDebugEnabled()) {
715                 LOG.debug("length={}, offset={},size={}",
716                     snapshotLength, start, size);
717             }
718             return getSnapshotBytes().substring(start, start + size);
719
720         }
721     }
722
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 + "},");
729
730         }
731         return "[" + sb.toString() + "]";
732     }
733
734     @VisibleForTesting
735     void markFollowerActive(String followerId) {
736         followerToLog.get(followerId).markFollowerActive();
737     }
738 }