X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Fbehaviors%2FCandidate.java;h=569f6b3d245fe5a6c1021f0537f08b3e1ec36f6e;hb=d0f46920468c8e4b67c68bd9058572b2d10d75f1;hp=7c111d28de818e225eb4b1ab8e10d4f962198c72;hpb=08221ab20d1632f7c1995d5e1038411a89bc4d4a;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Candidate.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Candidate.java index 7c111d28de..569f6b3d24 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Candidate.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Candidate.java @@ -5,7 +5,6 @@ * 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; @@ -15,12 +14,15 @@ import java.util.Collection; 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. @@ -41,7 +43,7 @@ import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply; *
  • If election timeout elapses: start new election * */ -public class Candidate extends AbstractRaftActorBehavior { +public final class Candidate extends AbstractRaftActorBehavior { private int voteCount; @@ -49,7 +51,7 @@ public class Candidate extends AbstractRaftActorBehavior { private final Collection votingPeers = new ArrayList<>(); - public Candidate(RaftActorContext context) { + public Candidate(final RaftActorContext context) { super(context, RaftState.Candidate); for (PeerInfo peer: context.getPeers()) { @@ -72,17 +74,17 @@ public class Candidate extends AbstractRaftActorBehavior { } @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); @@ -98,12 +100,13 @@ public class Candidate extends AbstractRaftActorBehavior { } @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()) { @@ -124,7 +127,18 @@ public class Candidate extends AbstractRaftActorBehavior { } @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()); @@ -172,10 +186,7 @@ public class Candidate extends AbstractRaftActorBehavior { return super.handleMessage(sender, message); } - private void startNewTerm() { - - // set voteCount back to 1 (that is voting for self) voteCount = 1;