Compare commits

..

No commits in common. "713e10d22341fa25a4e031d4aa04ca28b1af33ba" and "b8701b4aa7c06086c1e91cd8887bee9f5d487534" have entirely different histories.

1 changed files with 9 additions and 16 deletions

View File

@ -200,25 +200,18 @@ class TinyRaft extends EventEmitter
this.votes[msg.leader] = this.votes[msg.leader] || []; this.votes[msg.leader] = this.votes[msg.leader] || [];
this.votes[msg.leader].push(from); this.votes[msg.leader].push(from);
const n = this.votes[msg.leader].length; const n = this.votes[msg.leader].length;
if (n == 1 + (0 | this.nodes.length/2)) if (n == 1 + (0 | this.nodes.length/2) && msg.leader == this.nodeId)
{ {
if (msg.leader == this.nodeId) this.leader = msg.leader;
this.state = LEADER;
this._nextTerm(this.leadershipTimeout > 0 ? this.leadershipTimeout : -1);
this.followers = this.votes[this.nodeId];
for (const follower of this.followers)
{ {
this.leader = msg.leader; // Send a heartbeat to confirm leadership
this.state = LEADER; this.send(follower, { type: PING, term: this.term });
this._nextTerm(this.leadershipTimeout > 0 ? this.leadershipTimeout : -1);
this.followers = this.votes[this.nodeId];
for (const follower of this.followers)
{
// Send a heartbeat to confirm leadership
this.send(follower, { type: PING, term: this.term });
}
this.emit('change', { state: this.state, term: this.term, leader: this.nodeId, followers: this.votes[this.nodeId] });
}
else
{
this._nextTerm(0);
} }
this.emit('change', { state: this.state, term: this.term, leader: this.nodeId, followers: this.votes[this.nodeId] });
} }
else if (n > this.nodes.length/2 && this.state == LEADER && msg.leader == this.nodeId) else if (n > this.nodes.length/2 && this.state == LEADER && msg.leader == this.nodeId)
{ {