(RequestVote r)
| 659 | } |
| 660 | |
| 661 | @SuppressWarnings("SameReturnValue") |
| 662 | private long processRequestVote(RequestVote r) throws Exception { |
| 663 | lock(); |
| 664 | try { |
| 665 | // 不管任何状态重置下一次时间,使得每个node从大概一个时刻开始。 |
| 666 | nextVoteTime = System.currentTimeMillis() + raftConfig.getElectionTimeout(); |
| 667 | |
| 668 | if (logSequence.trySetTerm(r.Argument.getTerm()) == LogSequence.SetTermResult.Newer) |
| 669 | convertStateTo(RaftState.Follower); // new term found. |
| 670 | // else continue process |
| 671 | |
| 672 | // RequestVote RPC |
| 673 | // Receiver implementation: |
| 674 | // 1.Reply false if term < currentTerm(§5.1) |
| 675 | // 2.If votedFor is null or candidateId, and candidate's log is at |
| 676 | // least as up - to - date as receiver's log, grant vote(§5.2, §5.4) |
| 677 | |
| 678 | r.Result.setTerm(logSequence.getTerm()); |
| 679 | r.Result.setVoteGranted(r.Argument.getTerm() == logSequence.getTerm() && |
| 680 | logSequence.canVoteFor(r.Argument.getCandidateId()) && isLastLogUpToDate(r.Argument)); |
| 681 | |
| 682 | if (r.Result.getVoteGranted()) |
| 683 | logSequence.setVoteFor(r.Argument.getCandidateId()); |
| 684 | logger.info("{}: VoteFor={} Rpc={}", getName(), logSequence.getVoteFor(), r); |
| 685 | r.SendResultCode(0); |
| 686 | |
| 687 | return Procedure.Success; |
| 688 | } finally { |
| 689 | unlock(); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | @SuppressWarnings("SameReturnValue") |
| 694 | private static long processLeaderIs(LeaderIs r) { |
nothing calls this directly
no test coverage detected