Properly handle RequestVote in all states
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / Follower.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.raft.behaviors;
10
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.internal.messages.ElectionTimeout;
15 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
16 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
17 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
18
19 /**
20  * The behavior of a RaftActor in the Follower state
21  *
22  * <ul>
23  * <li> Respond to RPCs from candidates and leaders
24  * <li> If election timeout elapses without receiving AppendEntries
25  * RPC from current leader or granting vote to candidate:
26  * convert to candidate
27  * </ul>
28  *
29  */
30 public class Follower extends AbstractRaftActorBehavior {
31     public Follower(RaftActorContext context) {
32         super(context);
33
34         scheduleElection(electionDuration());
35     }
36
37     @Override protected RaftState handleAppendEntries(ActorRef sender,
38         AppendEntries appendEntries, RaftState suggestedState) {
39         return suggestedState;
40     }
41
42     @Override protected RaftState handleAppendEntriesReply(ActorRef sender,
43         AppendEntriesReply appendEntriesReply, RaftState suggestedState) {
44         return suggestedState;
45     }
46
47     @Override protected RaftState handleRequestVoteReply(ActorRef sender,
48         RequestVoteReply requestVoteReply, RaftState suggestedState) {
49         return suggestedState;
50     }
51
52     @Override protected RaftState state() {
53         return RaftState.Follower;
54     }
55
56     @Override public RaftState handleMessage(ActorRef sender, Object message) {
57         if(message instanceof ElectionTimeout){
58             return RaftState.Candidate;
59         }
60
61         scheduleElection(electionDuration());
62
63         return super.handleMessage(sender, message);
64     }
65 }