X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Fbehaviors%2FCandidate.java;h=52ed26758ee26b6f9949fa1202f1a44628b82364;hp=72e8e8c2a70b50ed5022b392918bbadc58697eba;hb=bad1f8b8f3c1780cd37ec8a817ef4b0f23901654;hpb=6f55265c68cbd7e231b48cfb1e6a2313d6680285 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 72e8e8c2a7..52ed26758e 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 @@ -70,11 +70,20 @@ public class Candidate extends AbstractRaftActorBehavior { } else { scheduleElection(electionDuration()); } + } + @Override + public final String getLeaderId() { + return null; + } + @Override + public final short getLeaderPayloadVersion() { + return -1; } - @Override protected RaftActorBehavior handleAppendEntries(ActorRef sender, + @Override + protected RaftActorBehavior handleAppendEntries(ActorRef sender, AppendEntries appendEntries) { if(LOG.isDebugEnabled()) { @@ -92,33 +101,51 @@ public class Candidate extends AbstractRaftActorBehavior { return this; } - @Override protected RaftActorBehavior handleAppendEntriesReply(ActorRef sender, - AppendEntriesReply appendEntriesReply) { - + @Override + protected RaftActorBehavior handleAppendEntriesReply(ActorRef sender, AppendEntriesReply appendEntriesReply) { return this; } - @Override protected RaftActorBehavior handleRequestVoteReply(ActorRef sender, - RequestVoteReply requestVoteReply) { - - LOG.debug("{}: handleRequestVoteReply: {}, current voteCount: {}", logName(), requestVoteReply, - voteCount); + @Override + protected RaftActorBehavior handleRequestVoteReply(ActorRef sender, RequestVoteReply requestVoteReply) { + LOG.debug("{}: handleRequestVoteReply: {}, current voteCount: {}", logName(), requestVoteReply, voteCount); if (requestVoteReply.isVoteGranted()) { voteCount++; } if (voteCount >= votesRequired) { - return internalSwitchBehavior(RaftState.Leader); + if(context.getCommitIndex() < context.getReplicatedLog().lastIndex()) { + LOG.debug("{}: Connmit index {} is behind last index {}", logName(), context.getCommitIndex(), + context.getReplicatedLog().lastIndex()); + return internalSwitchBehavior(RaftState.PreLeader); + } else { + return internalSwitchBehavior(RaftState.Leader); + } } return this; } @Override - public RaftActorBehavior handleMessage(ActorRef sender, Object originalMessage) { + public RaftActorBehavior handleMessage(ActorRef sender, Object message) { + if (message instanceof ElectionTimeout) { + LOG.debug("{}: Received ElectionTimeout", logName()); - Object message = fromSerializableMessage(originalMessage); + if (votesRequired == 0) { + // If there are no peers then we should be a Leader + // We wait for the election timeout to occur before declare + // ourselves the leader. This gives enough time for a leader + // who we do not know about (as a peer) + // to send a message to the candidate + + return internalSwitchBehavior(RaftState.Leader); + } + + startNewTerm(); + scheduleElection(electionDuration()); + return this; + } if (message instanceof RaftRPC) { @@ -135,25 +162,14 @@ public class Candidate extends AbstractRaftActorBehavior { if (rpc.getTerm() > context.getTermInformation().getCurrentTerm()) { context.getTermInformation().updateAndPersist(rpc.getTerm(), null); - return internalSwitchBehavior(RaftState.Follower); - } - } - - if (message instanceof ElectionTimeout) { - LOG.debug("{}: Received ElectionTimeout", logName()); - - if (votesRequired == 0) { - // If there are no peers then we should be a Leader - // We wait for the election timeout to occur before declare - // ourselves the leader. This gives enough time for a leader - // who we do not know about (as a peer) - // to send a message to the candidate + // The raft paper does not say whether or not a Candidate can/should process a RequestVote in + // this case but doing so gains quicker convergence when the sender's log is more up-to-date. + if (message instanceof RequestVote) { + super.handleMessage(sender, message); + } - return internalSwitchBehavior(RaftState.Leader); + return internalSwitchBehavior(RaftState.Follower); } - startNewTerm(); - scheduleElection(electionDuration()); - return this; } return super.handleMessage(sender, message);