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.RaftRPC;
17 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
18 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
21 * Abstract class that represents the behavior of a RaftActor
25 * <li> If commitIndex > lastApplied: increment lastApplied, apply
26 * log[lastApplied] to state machine (§5.3)
27 * <li> If RPC request or response contains term T > currentTerm:
28 * set currentTerm = T, convert to follower (§5.1)
30 public abstract class AbstractRaftActorBehavior implements RaftActorBehavior {
33 * Information about the RaftActor whose behavior this class represents
35 protected final RaftActorContext context;
38 protected AbstractRaftActorBehavior(RaftActorContext context) {
39 this.context = context;
43 * Derived classes should not directly handle AppendEntries messages it
44 * should let the base class handle it first. Once the base class handles
45 * the AppendEntries message and does the common actions that are applicable
46 * in all RaftState's it will delegate the handling of the AppendEntries
47 * message to the derived class to do more state specific handling by calling
50 * @param sender The actor that sent this message
51 * @param appendEntries The AppendEntries message
52 * @param suggestedState The state that the RaftActor should be in based
53 * on the base class's processing of the AppendEntries
57 protected abstract RaftState handleAppendEntries(ActorRef sender,
58 AppendEntries appendEntries, RaftState suggestedState);
61 * Derived classes should not directly handle AppendEntriesReply messages it
62 * should let the base class handle it first. Once the base class handles
63 * the AppendEntriesReply message and does the common actions that are
64 * applicable in all RaftState's it will delegate the handling of the
65 * AppendEntriesReply message to the derived class to do more state specific
66 * handling by calling this method
68 * @param sender The actor that sent this message
69 * @param appendEntriesReply The AppendEntriesReply message
70 * @param suggestedState The state that the RaftActor should be in based
71 * on the base class's processing of the
72 * AppendEntriesReply message
76 protected abstract RaftState handleAppendEntriesReply(ActorRef sender,
77 AppendEntriesReply appendEntriesReply, RaftState suggestedState);
80 * Derived classes should not directly handle RequestVote messages it
81 * should let the base class handle it first. Once the base class handles
82 * the RequestVote message and does the common actions that are applicable
83 * in all RaftState's it will delegate the handling of the RequestVote
84 * message to the derived class to do more state specific handling by calling
87 * @param sender The actor that sent this message
88 * @param requestVote The RequestVote message
89 * @param suggestedState The state that the RaftActor should be in based
90 * on the base class's processing of the RequestVote
94 protected abstract RaftState handleRequestVote(ActorRef sender,
95 RequestVote requestVote, RaftState suggestedState);
98 * Derived classes should not directly handle RequestVoteReply messages it
99 * should let the base class handle it first. Once the base class handles
100 * the RequestVoteReply message and does the common actions that are
101 * applicable in all RaftState's it will delegate the handling of the
102 * RequestVoteReply message to the derived class to do more state specific
103 * handling by calling this method
105 * @param sender The actor that sent this message
106 * @param requestVoteReply The RequestVoteReply message
107 * @param suggestedState The state that the RaftActor should be in based
108 * on the base class's processing of the RequestVote
113 protected abstract RaftState handleRequestVoteReply(ActorRef sender,
114 RequestVoteReply requestVoteReply, RaftState suggestedState);
117 * @return The derived class should return the state that corresponds to
120 protected abstract RaftState state();
123 public RaftState handleMessage(ActorRef sender, Object message) {
124 RaftState raftState = state();
125 if (message instanceof RaftRPC) {
126 raftState = applyTerm((RaftRPC) message);
128 if (message instanceof AppendEntries) {
129 AppendEntries appendEntries = (AppendEntries) message;
130 if (appendEntries.getLeaderCommit() > context.getLastApplied()
132 applyLogToStateMachine(appendEntries.getLeaderCommit());
134 raftState = handleAppendEntries(sender, appendEntries, raftState);
135 } else if (message instanceof AppendEntriesReply) {
137 handleAppendEntriesReply(sender, (AppendEntriesReply) message,
139 } else if (message instanceof RequestVote) {
141 handleRequestVote(sender, (RequestVote) message, raftState);
142 } else if (message instanceof RequestVoteReply) {
144 handleRequestVoteReply(sender, (RequestVoteReply) message,
150 private RaftState applyTerm(RaftRPC rpc) {
151 if (rpc.getTerm() > context.getTermInformation().getCurrentTerm()
153 context.getTermInformation().update(rpc.getTerm(), null);
154 return RaftState.Follower;
159 private void applyLogToStateMachine(long index) {
160 context.getLastApplied().set(index);