Closed wankdanker closed 4 years ago
I can see the race condition here, but I'm questioning why wouldn't that have still been a problem with previous code. I thought I kept the overall flow the same?
I guess the leadership election shouldn't start until checkInterval + 1000(leadershipDelay) has passed.
I can see the race condition here, but I'm questioning why wouldn't that have still been a problem with previous code. I thought I kept the overall flow the same?
All of the logic for checking leadership was in the check()
function. That function was only called by setInterval()
every checkInterval
ms. That changes that were introduced called the check
function on other occasions besides the interval (like helloReceived).
Anyway, node-discover@1.1.2 is on npm which fixes this.
Thanks for your help!
If you run
node examples/master.js
in three separate terminals, you'll see that the last node added becomes a master and then gets demoted shortly after.This happens because the
BasicLeadershipElection.prototype.check
is being called on eachBasicLeadershipElection.prototype.helloReceived
. So, if the first packet the newest node sees is from not a master it will promote itself. There should be a timeout so that the new node can have some time to learn about all the nodes in the network before determining if it should be promoted.related: #34, #35