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 org.opendaylight.controller.cluster.raft.RaftActorContext;
13 import org.opendaylight.controller.cluster.raft.RaftState;
14 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
15 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
16 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
17 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
19 import java.util.List;
22 * The behavior of a RaftActor when it is in the CandidateState
26 * <li> On conversion to candidate, start election:
28 * <li> Increment currentTerm
30 * <li> Reset election timer
31 * <li> Send RequestVote RPCs to all other servers
33 * <li> If votes received from majority of servers: become leader
34 * <li> If AppendEntries RPC received from new leader: convert to
36 * <li> If election timeout elapses: start new election
39 public class Candidate extends AbstractRaftActorBehavior {
40 private final List<String> peers;
42 public Candidate(RaftActorContext context, List<String> peers) {
47 @Override protected RaftState handleAppendEntries(ActorRef sender,
48 AppendEntries appendEntries, RaftState suggestedState) {
49 return suggestedState;
52 @Override protected RaftState handleAppendEntriesReply(ActorRef sender,
53 AppendEntriesReply appendEntriesReply, RaftState suggestedState) {
54 return suggestedState;
57 @Override protected RaftState handleRequestVote(ActorRef sender,
58 RequestVote requestVote, RaftState suggestedState) {
59 return suggestedState;
62 @Override protected RaftState handleRequestVoteReply(ActorRef sender,
63 RequestVoteReply requestVoteReply, RaftState suggestedState) {
64 return suggestedState;
67 @Override protected RaftState state() {
68 return RaftState.Candidate;
72 public RaftState handleMessage(ActorRef sender, Object message) {
73 return super.handleMessage(sender, message);