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 com.google.common.base.Preconditions;
13 import org.opendaylight.controller.cluster.raft.FollowerLogInformationImpl;
14 import org.opendaylight.controller.cluster.raft.RaftActorContext;
15 import org.opendaylight.controller.cluster.raft.RaftReplicator;
16 import org.opendaylight.controller.cluster.raft.RaftState;
17 import org.opendaylight.controller.cluster.raft.internal.messages.SendHeartBeat;
18 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
19 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
20 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
21 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.concurrent.atomic.AtomicLong;
30 * The behavior of a RaftActor when it is in the Leader state
34 * <li> Upon election: send initial empty AppendEntries RPCs
35 * (heartbeat) to each server; repeat during idle periods to
36 * prevent election timeouts (§5.2)
37 * <li> If command received from client: append entry to local log,
38 * respond after entry applied to state machine (§5.3)
39 * <li> If last log index ≥ nextIndex for a follower: send
40 * AppendEntries RPC with log entries starting at nextIndex
42 * <li> If successful: update nextIndex and matchIndex for
44 * <li> If AppendEntries fails because of log inconsistency:
45 * decrement nextIndex and retry (§5.3)
47 * <li> If there exists an N such that N > commitIndex, a majority
48 * of matchIndex[i] ≥ N, and log[N].term == currentTerm:
49 * set commitIndex = N (§5.3, §5.4).
51 public class Leader extends AbstractRaftActorBehavior {
54 private final Map<String, ActorRef> followerToReplicator = new HashMap<>();
56 public Leader(RaftActorContext context, List<String> followers){
59 for(String follower : followers) {
61 ActorRef replicator = context.actorOf(
63 new FollowerLogInformationImpl(follower,
70 // Create a replicator for each follower
71 followerToReplicator.put(follower, replicator);
77 @Override protected RaftState handleAppendEntries(ActorRef sender,
78 AppendEntries appendEntries, RaftState suggestedState) {
79 return suggestedState;
82 @Override protected RaftState handleAppendEntriesReply(ActorRef sender,
83 AppendEntriesReply appendEntriesReply, RaftState suggestedState) {
84 return suggestedState;
87 @Override protected RaftState handleRequestVote(ActorRef sender,
88 RequestVote requestVote, RaftState suggestedState) {
89 return suggestedState;
92 @Override protected RaftState handleRequestVoteReply(ActorRef sender,
93 RequestVoteReply requestVoteReply, RaftState suggestedState) {
94 return suggestedState;
97 @Override protected RaftState state() {
98 return RaftState.Leader;
101 @Override public RaftState handleMessage(ActorRef sender, Object message) {
102 Preconditions.checkNotNull(sender, "sender should not be null");
104 if(message instanceof SendHeartBeat) {
105 sender.tell(new AppendEntries(
106 context.getTermInformation().getCurrentTerm().get() , context.getId(),
107 context.getReplicatedLog().last().getIndex(),
108 context.getReplicatedLog().last().getTerm(),
109 Collections.EMPTY_LIST, context.getCommitIndex().get()), context.getActor());
112 return super.handleMessage(sender, message);