From 53071c5e3216a54fe9fb9004f3df274a7f4761a1 Mon Sep 17 00:00:00 2001 From: Dan VerWeire Date: Thu, 29 Sep 2011 15:22:56 -0400 Subject: [PATCH] First commit --- README.md | 126 +++++++++++++++++++++++++++++++ index.js | 1 + lib/broadcast.js | 127 ++++++++++++++++++++++++++++++++ lib/discover.js | 188 +++++++++++++++++++++++++++++++++++++++++++++++ test/test.js | 38 ++++++++++ 5 files changed, 480 insertions(+) create mode 100644 README.md create mode 100644 index.js create mode 100644 lib/broadcast.js create mode 100644 lib/discover.js create mode 100644 test/test.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..7bdbc71 --- /dev/null +++ b/README.md @@ -0,0 +1,126 @@ +node-discover +------------- + +Automatically discover your nodejs instances using UDP broadcast with support for automatic single master + + +Why? +---- + +So, you have a whole bunch of node processes running but you have no way within each process to +determine where the other processes are or what they can do. This module aims to make discovery of new +processes as simple as possible. Additionally, what if you want one process to be in charge of a cluster +of processes? This module also has automatic master process selection. + + +Example +------- + + var Discover = require('../lib/discover.js'); + + var d = new Discover(); + + d.on("promotion", function () { + /* + * Launch things this master process should do. + * + * For example: + * - Monitior your redis servers and handle failover by issuing slaveof commands then notify + * other node instances to use the new master + * - Make sure there are a certain number of nodes in the cluster and launch new ones if there + * are not enough + * - whatever + * + */ + + console.log("I was promoted to a master."); + }); + + d.on("demotion", function () { + /* + * End all master specific functions or whatever you might like. + * + */ + + console.log("I was demoted from being a master."); + }); + + d.on("added", function (obj) { + console.log("A new node has been added."); + }); + + d.on("removed", function (obj) { + console.log("A node has been removed."); + }); + + d.on("master", function (obj) { + /* + * A new master process has been selected + * + * Things we might want to do: + * - Review what the new master is advertising use its services + * - Kill all connections to the old master + */ + + console.log("A new master is in control"); + }); + + +API +--- + +Constructor +----------- + +new Discover({ + helloInterval : How often to broadcast a hello packet in milliseconds; Default: 1000 + checkInterval : How often to to check for missing nodes in milliseconds; Default: 2000 + nodeTimeout : Consider a node dead if not seen in this many milliseconds; Default: 2000 + masterTimeout : Consider a master node dead if not seen in this many milliseconds; Default: 2000 + address : Address to bind to; Default: '0.0.0.0' + port : Port to bind to and broadcast to: Default: 12345 + destination : Destination ip address; Default: '255.255.255.255' + key : Encryption key if your broadcast packets should be encrypted; Default: null (that means no encryption); +}); + +Attributes +---------- + +nodes + +Methods +------- + +promote +demote +join --not implemented +leave --not implemented +advertise +start +stop +eachNode(fn) + +Events +------ + +promotion +demotion +added +removed +master + + + + + +LICENSE + +(MIT License) + +Copyright (c) 2011 Dan VerWeire dverweire@gmail.com + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..e1893d9 --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require('./lib/discover.js'); diff --git a/lib/broadcast.js b/lib/broadcast.js new file mode 100644 index 0000000..7b14262 --- /dev/null +++ b/lib/broadcast.js @@ -0,0 +1,127 @@ +var dgram = require('dgram'), + crypto = require('crypto'); + +var procUuid = uuid(); + +var Broadcast = module.exports = function (options) { + var self = this, options = options || {}; + + self.address = options.address || '0.0.0.0'; + self.port = options.port || 12345; + self.destination = options.destination || "255.255.255.255"; + self.key = options.key || null; + + self.socket = dgram.createSocket('udp4'); + self.socket.setBroadcast(true); + self.socket.bind(self.port, self.address); + + self.instanceUuid = uuid(); + self.processUuid = procUuid; + + self.socket.on("message", function ( data, rinfo ) { + self.decode(data, function (err, obj) { + if (err) { + self.emit("error", err); + } + else if (obj.pid == procUuid) { + return false; + } + else if (obj.event && obj.data) { + + self.emit(obj.event, obj.data, obj, rinfo); + } + else { + self.emit("message", obj) + } + }); + }); + + self.on("error", function (err) { /* do nothing */}); +}; + +Broadcast.prototype = new process.EventEmitter(); + +Broadcast.prototype.send = function (event) { + var self = this; + + var obj = { event : event, pid : procUuid, iid : self.instanceUuid }; + + if (arguments.length == 2) { + obj.data = arguments[1]; + } + else { + //TODO: splice the arguments array and remove the first element setting data to the result array + } + + self.encode(obj, function (err, contents) { + if (err) { + return false; + } + + var msg = new Buffer(contents); + self.socket.send(msg, 0, msg.length, self.port, self.destination); + }); +}; + +Broadcast.prototype.encode = function (data, callback) { + var self = this; + + try { + return callback(null, (self.key) ? encrypt(JSON.stringify(data),self.key) : JSON.stringify(data)); + } + catch (e) { + return callback(e, null); + } +}; + +Broadcast.prototype.decode = function (data, callback) { + var self = this; + + try { + if (self.key) { + return callback(null, JSON.parse(decrypt(data.toString(),self.key))); + } + else { + return callback(null, JSON.parse(data)); + } + } + catch (e) { + return callback(e, null); + } +}; + + +//TODO: this may need to be improved +function uuid() { + var str = require('os').hostname() + ":" + process.pid + ":" + (+new Date) + ":" + (Math.floor(Math.random() * 100000000000)) + "" + (Math.floor(Math.random() * 100000000000)) + + return md5(str); +} + +function md5 (str) { + var hash = crypto.createHash('md5'); + + hash.update(str); + + return hash.digest('hex'); +}; + +function encrypt (str, key) { + var buf = []; + var cipher = crypto.createCipher('aes256', key); + + buf.push(cipher.update(str, 'utf8', 'binary')); + buf.push(cipher.final('binary')); + + return buf.join(''); +}; + +function decrypt (str, key) { + var buf = []; + var decipher = crypto.createDecipher('aes256', key); + + buf.push(decipher.update(str, 'binary', 'utf8')); + buf.push(decipher.final('utf8')); + + return buf.join(''); +}; \ No newline at end of file diff --git a/lib/discover.js b/lib/discover.js new file mode 100644 index 0000000..ef6fc5a --- /dev/null +++ b/lib/discover.js @@ -0,0 +1,188 @@ +/* + * + * Node Discover + * + * Attributes + * Nodes + * + * Methods + * Promote + * Demote + * Join + * Leave + * Advertise + * Start + * Stop + * EachNode(fn) + * + * Events + * Promotion + * Demotion + * Added + * Removed + * Master + * + * + * checkInterval should be greater than hello interval or you're just wasting cpu + * nodeTimeout must be greater than checkInterval + * masterTimeout must be greater than nodeTimeout + * + */ + +var Broadcast = require('./broadcast.js'); + +var Discover = module.exports = function(options) { + var self = this, options = options || {}; + + var settings = self.settings = { + helloInterval : options.helloInterval || 1000, + checkInterval : options.checkInterval || 2000, + nodeTimeout : options.nodeTimeout || 2000, + masterTimeout : options.masterTimeout || 2000, + address : options.address || '0.0.0.0', + port : options.port || 12345, + destination : options.destination || '255.255.255.255', + key : options.key || null + }; + + if (!(settings.nodeTimeout >= settings.checkInterval)) { + throw new Error("nodeTimeout must be greater than or equal to checkInterval."); + } + + if (!(settings.masterTimeout >= settings.nodeTimeout)) { + throw new Error("masterTimeout must be greater than or equal to nodeTimeout."); + } + + self.broadcast = new Broadcast({ + address : settings.address, + port : settings.port, + destination : settings.destination, + key : settings.key + }); + + self.me = { + isMaster : false, + isMasterEligible : true + }; + + self.nodes = {}; + + self.broadcast.on("hello", function (data, obj, rinfo) { + data.lastSeen = +new Date(); + data.address = rinfo.address; + data.port = rinfo.port; + data.id = obj.pid; + + var isNew = !self.nodes[obj.pid] + + self.nodes[obj.pid] = data; + + if (isNew) { + //new node found + + self.emit("added", data, obj, rinfo) + } + + if (data.isMaster) { + //if we have this node and it was not previously a master then it is a new master node + if (!(!isNew && self.nodes[obj.pid].isMaster)) { + //this is a new master + + if (self.me.isMaster) { + self.demote(); + } + + self.emit("master", data, obj, rinfo); + } + + } + }); + + var checkId, helloId; + + self.start = function () { + checkId = setInterval(function () { + var node = null, foundMaster = false; + + for (var processUuid in self.nodes) { + node = self.nodes[processUuid]; + + if ( +new Date() - node.lastSeen > settings.nodeTimeout ) { + //we haven't seen the node recently + + //Become master + if ( node.isMaster && +new Date() - node.lastSeen > settings.masterTimeout && self.me.isMasterEligible) { + //master is lost, become the master + self.promote(); + } + + //delete the node from our nodes list + delete self.nodes[processUuid] + + self.emit("removed", node); + } + else if (node.isMaster) { + foundMaster = true; + } + } + + if (!self.me.isMaster && !foundMaster && self.me.isMasterEligible) { + //no masters found out of all our nodes, become one. + self.promote(); + } + }, settings.checkInterval); + + //send hello every helloInterval + helloId = setInterval(function () { + self.broadcast.send("hello", self.me) + }, settings.helloInterval); + }; + + self.stop = function () { + clearInterval(checkId); + clearInterval(helloId); + }; + + self.start(); +}; + +Discover.prototype = new process.EventEmitter(); + +Discover.prototype.promote = function () { + var self = this; + + self.me.isMasterEligible = true; + self.me.isMaster = true; + self.hello(); + self.emit("promotion", true); +}; + +Discover.prototype.demote = function (permanent) { + var self = this; + + self.me.isMasterEligible = !permanent; + self.me.isMaster = false; + self.hello(); + self.emit("demotion", true); +}; + +Discover.prototype.hello = function () { + var self = this; + + self.broadcast.send("hello", self.me); +}; + +Discover.prototype.advertise = function (obj) { + var self = this; + + self.me.advertisement = obj; +}; + +Discover.prototype.eachNode = function (fn) { + var self = this; + + for ( var uuid in self.nodes ) { + fn(self.nodes[uuid]); + } +}; + diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..2140bd4 --- /dev/null +++ b/test/test.js @@ -0,0 +1,38 @@ +var Discover = require('../lib/discover.js'); + +var c = new Discover(); + +c.on("promotion", function () { + console.log("I was promoted."); + + c.advertise({ + RedisMonitor : { + protocol : 'tcp', + port : 5555 + } + }); +}); + +c.on("demotion", function () { + console.log("I was demoted."); + + c.advertise(null); +}); + +c.on("added", function (obj) { + console.log("Node added; here are all the nodes:"); + c.eachNode(function (node) { + console.log(node); + }); +}); + +c.on("removed", function (obj) { + console.log("Node removed; here are all the nodes:"); + c.eachNode(function (node) { + console.log(node); + }); +}); + +c.on("master", function (obj) { + console.log("New master."); +});