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;
14 import org.opendaylight.controller.cluster.raft.RaftActorContext;
15 import org.opendaylight.controller.cluster.raft.RaftState;
16 import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
17 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
18 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
19 import org.opendaylight.controller.cluster.raft.messages.RaftRPC;
20 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
21 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
24 * The behavior of a RaftActor when it is in the CandidateState
28 * <li> On conversion to candidate, start election:
30 * <li> Increment currentTerm
32 * <li> Reset election timer
33 * <li> Send RequestVote RPCs to all other servers
35 * <li> If votes received from majority of servers: become leader
36 * <li> If AppendEntries RPC received from new leader: convert to
38 * <li> If election timeout elapses: start new election
41 public class Candidate extends AbstractRaftActorBehavior {
43 private int voteCount;
45 private final int votesRequired;
47 private final Set<String> peers;
49 public Candidate(RaftActorContext context) {
50 super(context, RaftState.Candidate);
52 peers = context.getPeerAddresses().keySet();
54 if(LOG.isDebugEnabled()) {
55 LOG.debug("{}: Election: Candidate has following peers: {}", logName(), peers);
58 votesRequired = getMajorityVoteCount(peers.size());
61 scheduleElection(electionDuration());
64 @Override protected RaftActorBehavior handleAppendEntries(ActorRef sender,
65 AppendEntries appendEntries) {
67 if(LOG.isDebugEnabled()) {
68 LOG.debug("{}: handleAppendEntries: {}", logName(), appendEntries);
74 @Override protected RaftActorBehavior handleAppendEntriesReply(ActorRef sender,
75 AppendEntriesReply appendEntriesReply) {
80 @Override protected RaftActorBehavior handleRequestVoteReply(ActorRef sender,
81 RequestVoteReply requestVoteReply) {
83 LOG.debug("{}: handleRequestVoteReply: {}, current voteCount: {}", logName(), requestVoteReply,
86 if (requestVoteReply.isVoteGranted()) {
90 if (voteCount >= votesRequired) {
91 return switchBehavior(new Leader(context));
98 public RaftActorBehavior handleMessage(ActorRef sender, Object originalMessage) {
100 Object message = fromSerializableMessage(originalMessage);
102 if (message instanceof RaftRPC) {
104 RaftRPC rpc = (RaftRPC) message;
106 if(LOG.isDebugEnabled()) {
107 LOG.debug("{}: RaftRPC message received {}, my term is {}", logName(), rpc,
108 context.getTermInformation().getCurrentTerm());
111 // If RPC request or response contains term T > currentTerm:
112 // set currentTerm = T, convert to follower (§5.1)
113 // This applies to all RPC messages and responses
114 if (rpc.getTerm() > context.getTermInformation().getCurrentTerm()) {
115 context.getTermInformation().updateAndPersist(rpc.getTerm(), null);
117 return switchBehavior(new Follower(context));
121 if (message instanceof ElectionTimeout) {
122 LOG.debug("{}: Received ElectionTimeout", logName());
124 if (votesRequired == 0) {
125 // If there are no peers then we should be a Leader
126 // We wait for the election timeout to occur before declare
127 // ourselves the leader. This gives enough time for a leader
128 // who we do not know about (as a peer)
129 // to send a message to the candidate
131 return switchBehavior(new Leader(context));
134 scheduleElection(electionDuration());
138 return super.handleMessage(sender, message);
142 private void startNewTerm() {
145 // set voteCount back to 1 (that is voting for self)
148 // Increment the election term and vote for self
149 long currentTerm = context.getTermInformation().getCurrentTerm();
150 long newTerm = currentTerm + 1;
151 context.getTermInformation().updateAndPersist(newTerm, context.getId());
153 LOG.debug("{}: Starting new term {}", logName(), newTerm);
155 // Request for a vote
156 // TODO: Retry request for vote if replies do not arrive in a reasonable
157 // amount of time TBD
158 for (String peerId : peers) {
159 ActorSelection peerActor = context.getPeerActorSelection(peerId);
160 if(peerActor != null) {
161 RequestVote requestVote = new RequestVote(
162 context.getTermInformation().getCurrentTerm(),
164 context.getReplicatedLog().lastIndex(),
165 context.getReplicatedLog().lastTerm());
167 LOG.debug("{}: Sending {} to peer {}", logName(), requestVote, peerId);
169 peerActor.tell(requestVote, context.getActor());
174 @Override public void close() throws Exception {