(final @NotNull RequestVote req, final long curTime)
| 274 | } |
| 275 | |
| 276 | private void onProcess(final @NotNull RequestVote req, final long curTime) { |
| 277 | if (currentTerm < req.term) { |
| 278 | currentTerm = req.term; |
| 279 | votedFor = -1; |
| 280 | timeout = curTime + VOTE_TIMEOUT + env.random(RANDOM_TIMEOUT_MAX); |
| 281 | releaseLeader(-1); |
| 282 | if (ENABLE_TRACE) |
| 283 | env.traceInfo("update currentTerm for RequestVote.candidateId=" + req.candidateId); |
| 284 | } |
| 285 | boolean voteGranted = false; |
| 286 | if (currentTerm == req.term && (votedFor == -1 || votedFor == req.candidateId)) { |
| 287 | final long lastIndex = env.getLogCount() - 1; |
| 288 | final long lastTerm = lastIndex >= 0 ? env.getLog(lastIndex).term : -1; |
| 289 | if (lastTerm < req.lastLogTerm || lastTerm == req.lastLogTerm && lastIndex <= req.lastLogIndex) { |
| 290 | votedFor = (byte)req.candidateId; |
| 291 | voteGranted = true; |
| 292 | } else if (leaderId == -1) // 如果发现对方版本比自己低,自己还没发起投票,那么立即开始成为candidate发起新一轮投票 |
| 293 | timeout = curTime; |
| 294 | if (ENABLE_TRACE) |
| 295 | env.traceInfo("vote " + voteGranted + "(leaderId=" + leaderId + ") for " + req); |
| 296 | } else if (ENABLE_TRACE) |
| 297 | env.traceInfo("vote false(voteFor=" + votedFor + ") for " + req); |
| 298 | env.sendEvent(req.candidateId, new RequestVoteRe(currentTerm, voteGranted)); |
| 299 | } |
| 300 | |
| 301 | private void onProcess(final @NotNull RequestVoteRe res, final long curTime) { |
| 302 | if (currentTerm < res.term) { |
no test coverage detected