1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| private void handleVoteTimeout() { this.writeLock.lock(); if (this.state == State.STATE_CANDIDATE) { LOG.debug("Node {} term {} retry elect.", getNodeId(), this.currTerm); electSelf(); } else { this.writeLock.unlock(); } }
private void electSelf() { long oldTerm; try { LOG.info("Node {} start vote and grant vote self, term={}.", getNodeId(), this.currTerm); if (!this.conf.contains(this.serverId)) { LOG.warn("Node {} can't do electSelf as it is not in {}.", getNodeId(), this.conf); return; } if (this.state == State.STATE_FOLLOWER) { LOG.debug("Node {} stop election timer, term={}.", getNodeId(), this.currTerm); this.electionTimer.stop(); } resetLeaderId(PeerId.emptyPeer(), new Status(RaftError.ERAFTTIMEDOUT, "A follower's leader_id is reset to NULL as it begins to request_vote.")); this.state = State.STATE_CANDIDATE; this.currTerm++; this.votedId = this.serverId.copy(); LOG.debug("Node {} start vote timer, term={} .", getNodeId(), this.currTerm); this.voteTimer.start(); this.voteCtx.init(this.conf.getConf(), this.conf.isStable() ? null : this.conf.getOldConf()); oldTerm = this.currTerm; } finally { this.writeLock.unlock(); }
final LogId lastLogId = this.logManager.getLastLogId(true);
this.writeLock.lock(); try { if (oldTerm != this.currTerm) { LOG.warn("Node {} raise term {} when getLastLogId.", getNodeId(), this.currTerm); return; } for (final PeerId peer : this.conf.listPeers()) { if (peer.equals(this.serverId)) { continue; } if (!this.rpcService.connect(peer.getEndpoint())) { LOG.warn("Node {} channel init failed, address={}.", getNodeId(), peer.getEndpoint()); continue; } final OnRequestVoteRpcDone done = new OnRequestVoteRpcDone(peer, this.currTerm, this); done.request = RequestVoteRequest.newBuilder() .setPreVote(false) .setGroupId(this.groupId) .setServerId(this.serverId.toString()) .setPeerId(peer.toString()) .setTerm(this.currTerm) .setLastLogIndex(lastLogId.getIndex()) .setLastLogTerm(lastLogId.getTerm()) .build(); this.rpcService.requestVote(peer.getEndpoint(), done.request, done); } this.metaStorage.setTermAndVotedFor(this.currTerm, this.serverId); this.voteCtx.grant(this.serverId); if (this.voteCtx.isGranted()) { becomeLeader(); } } finally { this.writeLock.unlock(); } }
public void handleRequestVoteResponse(final PeerId peerId, final long term, final RequestVoteResponse response) { this.writeLock.lock(); try { if (this.state != State.STATE_CANDIDATE) { LOG.warn("Node {} received invalid RequestVoteResponse from {}, state not in STATE_CANDIDATE but {}.", getNodeId(), peerId, this.state); return; } if (term != this.currTerm) { LOG.warn("Node {} received stale RequestVoteResponse from {}, term={}, currTerm={}.", getNodeId(), peerId, term, this.currTerm); return; } if (response.getTerm() > this.currTerm) { LOG.warn("Node {} received invalid RequestVoteResponse from {}, term={}, expect={}.", getNodeId(), peerId, response.getTerm(), this.currTerm); stepDown(response.getTerm(), false, new Status(RaftError.EHIGHERTERMRESPONSE, "Raft node receives higher term request_vote_response.")); return; } if (response.getGranted()) { this.voteCtx.grant(peerId); if (this.voteCtx.isGranted()) { becomeLeader(); } } } finally { this.writeLock.unlock(); } }
private void becomeLeader() { Requires.requireTrue(this.state == State.STATE_CANDIDATE, "Illegal state: " + this.state); LOG.info("Node {} become leader of group, term={}, conf={}, oldConf={}.", getNodeId(), this.currTerm, this.conf.getConf(), this.conf.getOldConf()); stopVoteTimer(); this.state = State.STATE_LEADER; this.leaderId = this.serverId.copy(); this.replicatorGroup.resetTerm(this.currTerm); for (final PeerId peer : this.conf.listPeers()) { if (peer.equals(this.serverId)) { continue; } LOG.debug("Node {} add replicator, term={}, peer={}.", getNodeId(), this.currTerm, peer); if (!this.replicatorGroup.addReplicator(peer)) { LOG.error("Fail to add replicator, peer={}.", peer); } } this.ballotBox.resetPendingIndex(this.logManager.getLastLogIndex() + 1); if (this.confCtx.isBusy()) { throw new IllegalStateException(); } this.confCtx.flush(this.conf.getConf(), this.conf.getOldConf()); this.stepDownTimer.start(); }
|