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;
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSelection;
13 import akka.event.Logging;
14 import akka.event.LoggingAdapter;
15 import akka.japi.Procedure;
16 import akka.persistence.RecoveryCompleted;
17 import akka.persistence.SaveSnapshotFailure;
18 import akka.persistence.SaveSnapshotSuccess;
19 import akka.persistence.SnapshotOffer;
20 import akka.persistence.SnapshotSelectionCriteria;
21 import akka.persistence.UntypedPersistentActor;
22 import com.google.common.base.Optional;
23 import com.google.protobuf.ByteString;
24 import org.opendaylight.controller.cluster.raft.base.messages.ApplyLogEntries;
25 import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot;
26 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
27 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot;
28 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
29 import org.opendaylight.controller.cluster.raft.base.messages.Replicate;
30 import org.opendaylight.controller.cluster.raft.base.messages.SendHeartBeat;
31 import org.opendaylight.controller.cluster.raft.behaviors.Candidate;
32 import org.opendaylight.controller.cluster.raft.behaviors.Follower;
33 import org.opendaylight.controller.cluster.raft.behaviors.Leader;
34 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
35 import org.opendaylight.controller.cluster.raft.client.messages.AddRaftPeer;
36 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
37 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
38 import org.opendaylight.controller.cluster.raft.client.messages.RemoveRaftPeer;
39 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
40 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
41 import org.opendaylight.controller.protobuff.messages.cluster.raft.AppendEntriesMessages;
43 import java.io.Serializable;
47 * RaftActor encapsulates a state machine that needs to be kept synchronized
48 * in a cluster. It implements the RAFT algorithm as described in the paper
49 * <a href='https://ramcloud.stanford.edu/wiki/download/attachments/11370504/raft.pdf'>
50 * In Search of an Understandable Consensus Algorithm</a>
52 * RaftActor has 3 states and each state has a certain behavior associated
53 * with it. A Raft actor can behave as,
56 * <li> A Follower (or) </li>
57 * <li> A Candidate </li>
61 * A RaftActor MUST be a Leader in order to accept requests from clients to
62 * change the state of it's encapsulated state machine. Once a RaftActor becomes
63 * a Leader it is also responsible for ensuring that all followers ultimately
64 * have the same log and therefore the same state machine as itself.
67 * The current behavior of a RaftActor determines how election for leadership
68 * is initiated and how peer RaftActors react to request for votes.
71 * Each RaftActor also needs to know the current election term. It uses this
72 * information for a couple of things. One is to simply figure out who it
73 * voted for in the last election. Another is to figure out if the message
74 * it received to update it's state is stale.
77 * The RaftActor uses akka-persistence to store it's replicated log.
78 * Furthermore through it's behaviors a Raft Actor determines
81 * <li> when a log entry should be persisted </li>
82 * <li> when a log entry should be applied to the state machine (and) </li>
83 * <li> when a snapshot should be saved </li>
86 public abstract class RaftActor extends UntypedPersistentActor {
87 protected final LoggingAdapter LOG =
88 Logging.getLogger(getContext().system(), this);
91 * The current state determines the current behavior of a RaftActor
92 * A Raft Actor always starts off in the Follower State
94 private RaftActorBehavior currentBehavior;
97 * This context should NOT be passed directly to any other actor it is
98 * only to be consumed by the RaftActorBehaviors
100 protected RaftActorContext context;
103 * The in-memory journal
105 private ReplicatedLogImpl replicatedLog = new ReplicatedLogImpl();
107 private CaptureSnapshot captureSnapshot = null;
109 private volatile boolean hasSnapshotCaptureInitiated = false;
111 public RaftActor(String id, Map<String, String> peerAddresses) {
112 this(id, peerAddresses, Optional.<ConfigParams>absent());
115 public RaftActor(String id, Map<String, String> peerAddresses,
116 Optional<ConfigParams> configParams) {
118 context = new RaftActorContextImpl(this.getSelf(),
119 this.getContext(), id, new ElectionTermImpl(),
120 -1, -1, replicatedLog, peerAddresses,
121 (configParams.isPresent() ? configParams.get(): new DefaultConfigParamsImpl()),
125 @Override public void onReceiveRecover(Object message) {
126 if (message instanceof SnapshotOffer) {
127 LOG.info("SnapshotOffer called..");
128 SnapshotOffer offer = (SnapshotOffer) message;
129 Snapshot snapshot = (Snapshot) offer.snapshot();
131 // Create a replicated log with the snapshot information
132 // The replicated log can be used later on to retrieve this snapshot
133 // when we need to install it on a peer
134 replicatedLog = new ReplicatedLogImpl(snapshot);
136 context.setReplicatedLog(replicatedLog);
137 context.setLastApplied(snapshot.getLastAppliedIndex());
138 context.setCommitIndex(snapshot.getLastAppliedIndex());
140 LOG.info("Applied snapshot to replicatedLog. " +
141 "snapshotIndex={}, snapshotTerm={}, journal-size={}",
142 replicatedLog.snapshotIndex, replicatedLog.snapshotTerm,
146 // Apply the snapshot to the actors state
147 applySnapshot(ByteString.copyFrom(snapshot.getState()));
149 } else if (message instanceof ReplicatedLogEntry) {
150 ReplicatedLogEntry logEntry = (ReplicatedLogEntry) message;
151 if(LOG.isDebugEnabled()) {
152 LOG.debug("Received ReplicatedLogEntry for recovery:{}", logEntry.getIndex());
154 replicatedLog.append(logEntry);
156 } else if (message instanceof ApplyLogEntries) {
157 ApplyLogEntries ale = (ApplyLogEntries) message;
159 if(LOG.isDebugEnabled()) {
160 LOG.debug("Received ApplyLogEntries for recovery, applying to state:{} to {}",
161 context.getLastApplied() + 1, ale.getToIndex());
164 for (long i = context.getLastApplied() + 1; i <= ale.getToIndex(); i++) {
165 applyState(null, "recovery", replicatedLog.get(i).getData());
167 context.setLastApplied(ale.getToIndex());
168 context.setCommitIndex(ale.getToIndex());
170 } else if (message instanceof DeleteEntries) {
171 replicatedLog.removeFrom(((DeleteEntries) message).getFromIndex());
173 } else if (message instanceof UpdateElectionTerm) {
174 context.getTermInformation().update(((UpdateElectionTerm) message).getCurrentTerm(),
175 ((UpdateElectionTerm) message).getVotedFor());
177 } else if (message instanceof RecoveryCompleted) {
179 "RecoveryCompleted - Switching actor to Follower - " +
180 "Persistence Id = " + persistenceId() +
181 " Last index in log:{}, snapshotIndex={}, snapshotTerm={}, " +
183 replicatedLog.lastIndex(), replicatedLog.snapshotIndex,
184 replicatedLog.snapshotTerm, replicatedLog.size());
185 currentBehavior = switchBehavior(RaftState.Follower);
190 @Override public void onReceiveCommand(Object message) {
191 if (message instanceof ApplyState){
192 ApplyState applyState = (ApplyState) message;
194 if(LOG.isDebugEnabled()) {
195 LOG.debug("Applying state for log index {} data {}",
196 applyState.getReplicatedLogEntry().getIndex(),
197 applyState.getReplicatedLogEntry().getData());
200 applyState(applyState.getClientActor(), applyState.getIdentifier(),
201 applyState.getReplicatedLogEntry().getData());
203 } else if (message instanceof ApplyLogEntries){
204 ApplyLogEntries ale = (ApplyLogEntries) message;
205 if(LOG.isDebugEnabled()) {
206 LOG.debug("Persisting ApplyLogEntries with index={}", ale.getToIndex());
208 persist(new ApplyLogEntries(ale.getToIndex()), new Procedure<ApplyLogEntries>() {
210 public void apply(ApplyLogEntries param) throws Exception {
214 } else if(message instanceof ApplySnapshot ) {
215 Snapshot snapshot = ((ApplySnapshot) message).getSnapshot();
217 if(LOG.isDebugEnabled()) {
218 LOG.debug("ApplySnapshot called on Follower Actor " +
219 "snapshotIndex:{}, snapshotTerm:{}", snapshot.getLastAppliedIndex(),
220 snapshot.getLastAppliedTerm()
223 applySnapshot(ByteString.copyFrom(snapshot.getState()));
225 //clears the followers log, sets the snapshot index to ensure adjusted-index works
226 replicatedLog = new ReplicatedLogImpl(snapshot);
227 context.setReplicatedLog(replicatedLog);
228 context.setLastApplied(snapshot.getLastAppliedIndex());
230 } else if (message instanceof FindLeader) {
232 new FindLeaderReply(getLeaderAddress()),
236 } else if (message instanceof SaveSnapshotSuccess) {
237 SaveSnapshotSuccess success = (SaveSnapshotSuccess) message;
238 LOG.info("SaveSnapshotSuccess received for snapshot");
240 context.getReplicatedLog().snapshotCommit();
242 // TODO: Not sure if we want to be this aggressive with trimming stuff
243 trimPersistentData(success.metadata().sequenceNr());
245 } else if (message instanceof SaveSnapshotFailure) {
246 SaveSnapshotFailure saveSnapshotFailure = (SaveSnapshotFailure) message;
248 LOG.info("saveSnapshotFailure.metadata():{}", saveSnapshotFailure.metadata().toString());
249 LOG.error(saveSnapshotFailure.cause(), "SaveSnapshotFailure received for snapshot Cause:");
251 context.getReplicatedLog().snapshotRollback();
253 LOG.info("Replicated Log rollbacked. Snapshot will be attempted in the next cycle." +
254 "snapshotIndex:{}, snapshotTerm:{}, log-size:{}",
255 context.getReplicatedLog().getSnapshotIndex(),
256 context.getReplicatedLog().getSnapshotTerm(),
257 context.getReplicatedLog().size());
259 } else if (message instanceof AddRaftPeer){
261 // FIXME : Do not add raft peers like this.
262 // When adding a new Peer we have to ensure that the a majority of
263 // the peers know about the new Peer. Doing it this way may cause
264 // a situation where multiple Leaders may emerge
265 AddRaftPeer arp = (AddRaftPeer)message;
266 context.addToPeers(arp.getName(), arp.getAddress());
268 } else if (message instanceof RemoveRaftPeer){
270 RemoveRaftPeer rrp = (RemoveRaftPeer)message;
271 context.removePeer(rrp.getName());
273 } else if (message instanceof CaptureSnapshot) {
274 LOG.info("CaptureSnapshot received by actor");
275 CaptureSnapshot cs = (CaptureSnapshot)message;
276 captureSnapshot = cs;
279 } else if (message instanceof CaptureSnapshotReply){
280 LOG.info("CaptureSnapshotReply received by actor");
281 CaptureSnapshotReply csr = (CaptureSnapshotReply) message;
283 ByteString stateInBytes = csr.getSnapshot();
284 LOG.info("CaptureSnapshotReply stateInBytes size:{}", stateInBytes.size());
285 handleCaptureSnapshotReply(stateInBytes);
288 if (!(message instanceof AppendEntriesMessages.AppendEntries)
289 && !(message instanceof AppendEntriesReply) && !(message instanceof SendHeartBeat)) {
290 if(LOG.isDebugEnabled()) {
291 LOG.debug("onReceiveCommand: message:" + message.getClass());
296 currentBehavior.handleMessage(getSender(), message);
297 RaftActorBehavior oldBehavior = currentBehavior;
298 currentBehavior = switchBehavior(state);
299 if(oldBehavior != currentBehavior){
303 onLeaderChanged(oldBehavior.getLeaderId(), currentBehavior.getLeaderId());
307 public java.util.Set<String> getPeers() {
308 return context.getPeerAddresses().keySet();
311 protected String getReplicatedLogState() {
312 return "snapshotIndex=" + context.getReplicatedLog().getSnapshotIndex()
313 + ", snapshotTerm=" + context.getReplicatedLog().getSnapshotTerm()
314 + ", im-mem journal size=" + context.getReplicatedLog().size();
319 * When a derived RaftActor needs to persist something it must call
326 protected void persistData(ActorRef clientActor, String identifier,
329 ReplicatedLogEntry replicatedLogEntry = new ReplicatedLogImplEntry(
330 context.getReplicatedLog().lastIndex() + 1,
331 context.getTermInformation().getCurrentTerm(), data);
333 if(LOG.isDebugEnabled()) {
334 LOG.debug("Persist data {}", replicatedLogEntry);
338 .appendAndPersist(clientActor, identifier, replicatedLogEntry);
341 protected String getId() {
342 return context.getId();
346 * Derived actors can call the isLeader method to check if the current
347 * RaftActor is the Leader or not
349 * @return true it this RaftActor is a Leader false otherwise
351 protected boolean isLeader() {
352 return context.getId().equals(currentBehavior.getLeaderId());
356 * Derived actor can call getLeader if they need a reference to the Leader.
357 * This would be useful for example in forwarding a request to an actor
358 * which is the leader
360 * @return A reference to the leader if known, null otherwise
362 protected ActorSelection getLeader(){
363 String leaderAddress = getLeaderAddress();
365 if(leaderAddress == null){
369 return context.actorSelection(leaderAddress);
374 * @return the current leader's id
376 protected String getLeaderId(){
377 return currentBehavior.getLeaderId();
380 protected RaftState getRaftState() {
381 return currentBehavior.state();
384 protected ReplicatedLogEntry getLastLogEntry() {
385 return replicatedLog.last();
388 protected Long getCurrentTerm(){
389 return context.getTermInformation().getCurrentTerm();
392 protected Long getCommitIndex(){
393 return context.getCommitIndex();
396 protected Long getLastApplied(){
397 return context.getLastApplied();
401 * setPeerAddress sets the address of a known peer at a later time.
403 * This is to account for situations where a we know that a peer
404 * exists but we do not know an address up-front. This may also be used in
405 * situations where a known peer starts off in a different location and we
406 * need to change it's address
408 * Note that if the peerId does not match the list of peers passed to
409 * this actor during construction an IllegalStateException will be thrown.
414 protected void setPeerAddress(String peerId, String peerAddress){
415 context.setPeerAddress(peerId, peerAddress);
421 * The applyState method will be called by the RaftActor when some data
422 * needs to be applied to the actor's state
424 * @param clientActor A reference to the client who sent this message. This
425 * is the same reference that was passed to persistData
426 * by the derived actor. clientActor may be null when
427 * the RaftActor is behaving as a follower or during
429 * @param identifier The identifier of the persisted data. This is also
430 * the same identifier that was passed to persistData by
431 * the derived actor. identifier may be null when
432 * the RaftActor is behaving as a follower or during
434 * @param data A piece of data that was persisted by the persistData call.
435 * This should NEVER be null.
437 protected abstract void applyState(ActorRef clientActor, String identifier,
441 * This method will be called by the RaftActor when a snapshot needs to be
442 * created. The derived actor should respond with its current state.
444 * During recovery the state that is returned by the derived actor will
445 * be passed back to it by calling the applySnapshot method
447 * @return The current state of the actor
449 protected abstract void createSnapshot();
452 * This method will be called by the RaftActor during recovery to
453 * reconstruct the state of the actor.
455 * This method may also be called at any other point during normal
456 * operations when the derived actor is out of sync with it's peers
457 * and the only way to bring it in sync is by applying a snapshot
459 * @param snapshot A snapshot of the state of the actor
461 protected abstract void applySnapshot(ByteString snapshot);
464 * This method will be called by the RaftActor when the state of the
465 * RaftActor changes. The derived actor can then use methods like
466 * isLeader or getLeader to do something useful
468 protected abstract void onStateChanged();
470 protected void onLeaderChanged(String oldLeader, String newLeader){};
472 private RaftActorBehavior switchBehavior(RaftState state) {
473 if (currentBehavior != null) {
474 if (currentBehavior.state() == state) {
475 return currentBehavior;
477 LOG.info("Switching from state " + currentBehavior.state() + " to "
481 currentBehavior.close();
482 } catch (Exception e) {
484 "Failed to close behavior : " + currentBehavior.state());
488 LOG.info("Switching behavior to " + state);
490 RaftActorBehavior behavior = null;
491 if (state == RaftState.Candidate) {
492 behavior = new Candidate(context);
493 } else if (state == RaftState.Follower) {
494 behavior = new Follower(context);
496 behavior = new Leader(context);
504 private void trimPersistentData(long sequenceNumber) {
505 // Trim akka snapshots
506 // FIXME : Not sure how exactly the SnapshotSelectionCriteria is applied
507 // For now guessing that it is ANDed.
508 deleteSnapshots(new SnapshotSelectionCriteria(
509 sequenceNumber - context.getConfigParams().getSnapshotBatchCount(), 43200000));
512 deleteMessages(sequenceNumber);
515 private String getLeaderAddress(){
517 return getSelf().path().toString();
519 String leaderId = currentBehavior.getLeaderId();
520 if (leaderId == null) {
523 String peerAddress = context.getPeerAddress(leaderId);
524 if(LOG.isDebugEnabled()) {
525 LOG.debug("getLeaderAddress leaderId = " + leaderId + " peerAddress = "
532 private void handleCaptureSnapshotReply(ByteString stateInBytes) {
533 // create a snapshot object from the state provided and save it
534 // when snapshot is saved async, SaveSnapshotSuccess is raised.
536 Snapshot sn = Snapshot.create(stateInBytes.toByteArray(),
537 context.getReplicatedLog().getFrom(captureSnapshot.getLastAppliedIndex() + 1),
538 captureSnapshot.getLastIndex(), captureSnapshot.getLastTerm(),
539 captureSnapshot.getLastAppliedIndex(), captureSnapshot.getLastAppliedTerm());
543 LOG.info("Persisting of snapshot done:{}", sn.getLogMessage());
545 //be greedy and remove entries from in-mem journal which are in the snapshot
546 // and update snapshotIndex and snapshotTerm without waiting for the success,
548 context.getReplicatedLog().snapshotPreCommit(stateInBytes,
549 captureSnapshot.getLastAppliedIndex(),
550 captureSnapshot.getLastAppliedTerm());
552 LOG.info("Removed in-memory snapshotted entries, adjusted snaphsotIndex:{} " +
553 "and term:{}", captureSnapshot.getLastAppliedIndex(),
554 captureSnapshot.getLastAppliedTerm());
556 captureSnapshot = null;
557 hasSnapshotCaptureInitiated = false;
561 private class ReplicatedLogImpl extends AbstractReplicatedLogImpl {
563 public ReplicatedLogImpl(Snapshot snapshot) {
564 super(ByteString.copyFrom(snapshot.getState()),
565 snapshot.getLastAppliedIndex(), snapshot.getLastAppliedTerm(),
566 snapshot.getUnAppliedEntries());
569 public ReplicatedLogImpl() {
573 @Override public void removeFromAndPersist(long logEntryIndex) {
574 int adjustedIndex = adjustedIndex(logEntryIndex);
576 if (adjustedIndex < 0) {
580 // FIXME: Maybe this should be done after the command is saved
581 journal.subList(adjustedIndex , journal.size()).clear();
583 persist(new DeleteEntries(adjustedIndex), new Procedure<DeleteEntries>(){
585 @Override public void apply(DeleteEntries param)
587 //FIXME : Doing nothing for now
592 @Override public void appendAndPersist(
593 final ReplicatedLogEntry replicatedLogEntry) {
594 appendAndPersist(null, null, replicatedLogEntry);
597 public void appendAndPersist(final ActorRef clientActor,
598 final String identifier,
599 final ReplicatedLogEntry replicatedLogEntry) {
600 context.getLogger().debug(
601 "Append log entry and persist {} ", replicatedLogEntry);
602 // FIXME : By adding the replicated log entry to the in-memory journal we are not truly ensuring durability of the logs
603 journal.add(replicatedLogEntry);
605 // When persisting events with persist it is guaranteed that the
606 // persistent actor will not receive further commands between the
607 // persist call and the execution(s) of the associated event
608 // handler. This also holds for multiple persist calls in context
609 // of a single command.
610 persist(replicatedLogEntry,
611 new Procedure<ReplicatedLogEntry>() {
612 public void apply(ReplicatedLogEntry evt) throws Exception {
613 // when a snaphsot is being taken, captureSnapshot != null
614 if (hasSnapshotCaptureInitiated == false &&
615 journal.size() % context.getConfigParams().getSnapshotBatchCount() == 0) {
617 LOG.info("Initiating Snapshot Capture..");
618 long lastAppliedIndex = -1;
619 long lastAppliedTerm = -1;
621 ReplicatedLogEntry lastAppliedEntry = get(context.getLastApplied());
622 if (lastAppliedEntry != null) {
623 lastAppliedIndex = lastAppliedEntry.getIndex();
624 lastAppliedTerm = lastAppliedEntry.getTerm();
627 if(LOG.isDebugEnabled()) {
628 LOG.debug("Snapshot Capture logSize: {}", journal.size());
629 LOG.debug("Snapshot Capture lastApplied:{} ",
630 context.getLastApplied());
631 LOG.debug("Snapshot Capture lastAppliedIndex:{}", lastAppliedIndex);
632 LOG.debug("Snapshot Capture lastAppliedTerm:{}", lastAppliedTerm);
635 // send a CaptureSnapshot to self to make the expensive operation async.
636 getSelf().tell(new CaptureSnapshot(
637 lastIndex(), lastTerm(), lastAppliedIndex, lastAppliedTerm),
639 hasSnapshotCaptureInitiated = true;
641 // Send message for replication
642 if (clientActor != null) {
643 currentBehavior.handleMessage(getSelf(),
644 new Replicate(clientActor, identifier,
655 private static class DeleteEntries implements Serializable {
656 private final int fromIndex;
659 public DeleteEntries(int fromIndex) {
660 this.fromIndex = fromIndex;
663 public int getFromIndex() {
669 private class ElectionTermImpl implements ElectionTerm {
671 * Identifier of the actor whose election term information this is
673 private long currentTerm = 0;
674 private String votedFor = null;
676 public long getCurrentTerm() {
680 public String getVotedFor() {
684 @Override public void update(long currentTerm, String votedFor) {
685 if(LOG.isDebugEnabled()) {
686 LOG.debug("Set currentTerm={}, votedFor={}", currentTerm, votedFor);
688 this.currentTerm = currentTerm;
689 this.votedFor = votedFor;
693 public void updateAndPersist(long currentTerm, String votedFor){
694 update(currentTerm, votedFor);
695 // FIXME : Maybe first persist then update the state
696 persist(new UpdateElectionTerm(this.currentTerm, this.votedFor), new Procedure<UpdateElectionTerm>(){
698 @Override public void apply(UpdateElectionTerm param)
706 private static class UpdateElectionTerm implements Serializable {
707 private final long currentTerm;
708 private final String votedFor;
710 public UpdateElectionTerm(long currentTerm, String votedFor) {
711 this.currentTerm = currentTerm;
712 this.votedFor = votedFor;
715 public long getCurrentTerm() {
719 public String getVotedFor() {