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 org.opendaylight.controller.cluster.raft.FollowerLogInformation;
16 import org.opendaylight.controller.cluster.raft.FollowerLogInformationImpl;
17 import org.opendaylight.controller.cluster.raft.RaftActorContext;
18 import org.opendaylight.controller.cluster.raft.RaftState;
19 import org.opendaylight.controller.cluster.raft.internal.messages.SendHeartBeat;
20 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
21 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
22 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
23 import scala.concurrent.duration.FiniteDuration;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.atomic.AtomicLong;
33 * The behavior of a RaftActor when it is in the Leader state
37 * <li> Upon election: send initial empty AppendEntries RPCs
38 * (heartbeat) to each server; repeat during idle periods to
39 * prevent election timeouts (§5.2)
40 * <li> If command received from client: append entry to local log,
41 * respond after entry applied to state machine (§5.3)
42 * <li> If last log index ≥ nextIndex for a follower: send
43 * AppendEntries RPC with log entries starting at nextIndex
45 * <li> If successful: update nextIndex and matchIndex for
47 * <li> If AppendEntries fails because of log inconsistency:
48 * decrement nextIndex and retry (§5.3)
50 * <li> If there exists an N such that N > commitIndex, a majority
51 * of matchIndex[i] ≥ N, and log[N].term == currentTerm:
52 * set commitIndex = N (§5.3, §5.4).
54 public class Leader extends AbstractRaftActorBehavior {
57 private final Map<String, FollowerLogInformation> followerToLog =
60 private final Map<String, ActorSelection> followerToActor = new HashMap<>();
62 private Cancellable heartbeatCancel = null;
64 public Leader(RaftActorContext context, List<String> followerPaths) {
67 for (String followerPath : followerPaths) {
68 FollowerLogInformation followerLogInformation =
69 new FollowerLogInformationImpl(followerPath,
73 followerToActor.put(followerPath,
74 context.actorSelection(followerLogInformation.getId()));
75 followerToLog.put(followerPath, followerLogInformation);
79 // Immediately schedule a heartbeat
80 // Upon election: send initial empty AppendEntries RPCs
81 // (heartbeat) to each server; repeat during idle periods to
82 // prevent election timeouts (§5.2)
83 scheduleHeartBeat(new FiniteDuration(0, TimeUnit.SECONDS));
88 @Override protected RaftState handleAppendEntries(ActorRef sender,
89 AppendEntries appendEntries, RaftState suggestedState) {
90 return suggestedState;
93 @Override protected RaftState handleAppendEntriesReply(ActorRef sender,
94 AppendEntriesReply appendEntriesReply, RaftState suggestedState) {
95 return suggestedState;
98 @Override protected RaftState handleRequestVoteReply(ActorRef sender,
99 RequestVoteReply requestVoteReply, RaftState suggestedState) {
100 return suggestedState;
103 @Override protected RaftState state() {
104 return RaftState.Leader;
107 @Override public RaftState handleMessage(ActorRef sender, Object message) {
108 Preconditions.checkNotNull(sender, "sender should not be null");
110 scheduleHeartBeat(HEART_BEAT_INTERVAL);
112 if (message instanceof SendHeartBeat) {
113 for (ActorSelection follower : followerToActor.values()) {
114 follower.tell(new AppendEntries(
115 context.getTermInformation().getCurrentTerm(),
117 context.getReplicatedLog().last().getIndex(),
118 context.getReplicatedLog().last().getTerm(),
119 Collections.EMPTY_LIST, context.getCommitIndex()),
124 return super.handleMessage(sender, message);
127 private void scheduleHeartBeat(FiniteDuration interval) {
128 if (heartbeatCancel != null && !heartbeatCancel.isCancelled()) {
129 heartbeatCancel.cancel();
132 // Schedule a heartbeat. When the scheduler triggers a SendHeartbeat
133 // message is sent to itself.
134 // Scheduling the heartbeat only once here because heartbeats do not
135 // need to be sent if there are other messages being sent to the remote
138 context.getActorSystem().scheduler().scheduleOnce(interval,
139 context.getActor(), new SendHeartBeat(),
140 context.getActorSystem().dispatcher(), context.getActor());