0.0.1 • Published 11 years ago
etcd-leader v0.0.1
etcd-leader
Under development, not yet suitable for production use.
Leader election module built on a robust election algorithm, and extremely thoroughly tested.
Usage
Expects a configured node-etcd client to be provided. Note that this package does not depend on node-etcd. It is compatible with the ^4.0.0 version of node-etcd.
var Etcd = require("node-etcd");
var etcdLeader = require("etcd-leader");
var etcd = new Etcd("localhost", 4001);
// First parameter is etcd key to use for election.
// Second parameter is name of this node.
// Third parameter is the expiry window for master election.
var election = etcdLeader(etcd, "/master", "foo", 10).start();
election.on("elected", function() {
console.log("I am the MASTER.");
});
election.on("unelected", function() {
console.log("I am no longer the MASTER.");
});
election.on("leader", function(node) {
console.log("Leader is now " + node);
});Algorithm
The leader election algorithm here is based on top of the atomic in-order insertion strategy documented by ZooKeeper.
How do we do this specifically in etcd?
- POST to
/leader/keyto perform atomic insertion of this node's membership. - GET
/leader/key?sorted=trueto read back out the current membership info. - If our membership is lowest sorted entry, this node is elected master.
- Otherwise, use etcd to watch the entry sorted immediately before this node, waiting for it to drop off. If it does, start from step 2.
- Perdiodically (TTL divided by 2) refresh our membership key to ensure it stays active.
Walk me through it
Let's assume we have initialised etcd-leader to use /master as the master key, and we have three nodes, with node names foo, bar and quux respectively.
foostarts up first, it issues aPOSTto/master(with a TTL of 10 seconds). It gets a createdIndex of "5".foobegins refreshing its value every 5 seconds.fooenumerates/masterto find lowest sorted createdIndex node. Discovers that it's itself.foois now master.barstarts up next, it also issues aPOSTto/master. It gets a createdIndex of "7".barbegins refreshing its value every 5 seconds.barenumerates/master, sees thatfoois the lowest createdIndex. Starts watching that node, waiting for it to disappear.quuxstarts up, issues the POST and gets a createdIndex of "9".quuxbegins refreshing its value every 5 seconds.quuxenumerates/master, sees thatfoois the lowest createdIndex, and thatbaris the node that immediately preceeds it.quuxstarts watchingbar's node for changes, waiting for it to disappear.
0.0.1
11 years ago