* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.controller.cluster.raft.behaviors;
import akka.actor.ActorRef;
import org.opendaylight.controller.cluster.raft.PeerInfo;
import org.opendaylight.controller.cluster.raft.RaftActorContext;
import org.opendaylight.controller.cluster.raft.RaftState;
+import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
+import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
import org.opendaylight.controller.cluster.raft.messages.RaftRPC;
import org.opendaylight.controller.cluster.raft.messages.RequestVote;
import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
+import scala.concurrent.duration.FiniteDuration;
/**
* The behavior of a RaftActor when it is in the Candidate raft state.
* <li> If election timeout elapses: start new election
* </ul>
*/
-public class Candidate extends AbstractRaftActorBehavior {
+public final class Candidate extends AbstractRaftActorBehavior {
private int voteCount;
private final Collection<String> votingPeers = new ArrayList<>();
- public Candidate(RaftActorContext context) {
+ public Candidate(final RaftActorContext context) {
super(context, RaftState.Candidate);
for (PeerInfo peer: context.getPeers()) {
}
@Override
- public final String getLeaderId() {
+ public String getLeaderId() {
return null;
}
@Override
- public final short getLeaderPayloadVersion() {
+ public short getLeaderPayloadVersion() {
return -1;
}
@Override
- protected RaftActorBehavior handleAppendEntries(ActorRef sender, AppendEntries appendEntries) {
+ protected RaftActorBehavior handleAppendEntries(final ActorRef sender, final AppendEntries appendEntries) {
log.debug("{}: handleAppendEntries: {}", logName(), appendEntries);
}
@Override
- protected RaftActorBehavior handleAppendEntriesReply(ActorRef sender, AppendEntriesReply appendEntriesReply) {
+ protected RaftActorBehavior handleAppendEntriesReply(final ActorRef sender,
+ final AppendEntriesReply appendEntriesReply) {
return this;
}
@Override
- protected RaftActorBehavior handleRequestVoteReply(ActorRef sender, RequestVoteReply requestVoteReply) {
+ protected RaftActorBehavior handleRequestVoteReply(final ActorRef sender, final RequestVoteReply requestVoteReply) {
log.debug("{}: handleRequestVoteReply: {}, current voteCount: {}", logName(), requestVoteReply, voteCount);
if (requestVoteReply.isVoteGranted()) {
}
@Override
- public RaftActorBehavior handleMessage(ActorRef sender, Object message) {
+ protected FiniteDuration electionDuration() {
+ return super.electionDuration().$div(context.getConfigParams().getCandidateElectionTimeoutDivisor());
+ }
+
+
+ @Override
+ ApplyState getApplyStateFor(final ReplicatedLogEntry entry) {
+ throw new IllegalStateException("A candidate should never attempt to apply " + entry);
+ }
+
+ @Override
+ public RaftActorBehavior handleMessage(final ActorRef sender, final Object message) {
if (message instanceof ElectionTimeout) {
log.debug("{}: Received ElectionTimeout", logName());
return super.handleMessage(sender, message);
}
-
private void startNewTerm() {
-
-
// set voteCount back to 1 (that is voting for self)
voteCount = 1;