From f717a6969d62f42379ba5712d41d8e09b79f0e10 Mon Sep 17 00:00:00 2001 From: abarre Date: Tue, 27 Nov 2012 12:04:16 +0100 Subject: [PATCH 1/6] Update the proxy events connection_interrupted and session_expired are not emitted by node-zookeeper. They have been replaced by not_connected and error. --- docs/index.restdown | 4 ++-- lib/client.js | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/docs/index.restdown b/docs/index.restdown index b5d1955..41a476c 100644 --- a/docs/index.restdown +++ b/docs/index.restdown @@ -124,9 +124,9 @@ connecting to ZK via once(). If the client driver has an unexpected error, it is sent here. Specifically, if the client has lost its session to ZK. -### Event: 'connection_interrupted' +### Event: 'not_connected' - function onConnectionInterrupted() { } + function onNotConnected() { } If the underlying native client loses connection to ZK, but not its session, it is sent here. Notably, for most applications, this should be a no-op, since diff --git a/lib/client.js b/lib/client.js index 126034e..1984a8e 100644 --- a/lib/client.js +++ b/lib/client.js @@ -19,8 +19,7 @@ var sprintf = util.format; var PROXY_EVENTS = [ 'connect', 'not_connected', - 'close', - 'session_expired' + 'close' ]; @@ -189,8 +188,7 @@ ZKClient.prototype.close = function close() { }); this.removeAllListeners('connect'); - this.removeAllListeners('connection_interrupted'); - this.removeAllListeners('session_expired'); + this.removeAllListeners('not_connected'); this.zk.once('close', function () { self.watchers.forEach(function (w) { From 6092988cdb64cf1a91d0afa1245677ba7fe1570c Mon Sep 17 00:00:00 2001 From: abarre Date: Tue, 27 Nov 2012 14:41:41 +0100 Subject: [PATCH 2/6] Clear the list of watchers. For debugging, we verify that the number of watchers is stables after a few start/stop. --- lib/client.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/client.js b/lib/client.js index 1984a8e..14ca1be 100644 --- a/lib/client.js +++ b/lib/client.js @@ -194,6 +194,7 @@ ZKClient.prototype.close = function close() { self.watchers.forEach(function (w) { w.stop(); }); + self.watchers = []; self.zk.removeAllListeners('error'); self.removeAllListeners('error'); From 0c9d70bd19554068534f6d9aea305d32f8ce467f Mon Sep 17 00:00:00 2001 From: abarre Date: Thu, 29 Nov 2012 09:13:34 +0100 Subject: [PATCH 3/6] Don't call register for unexpected events. If we call register, we double the number of watchers. --- lib/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 14ca1be..e5c425c 100644 --- a/lib/client.js +++ b/lib/client.js @@ -758,7 +758,7 @@ ZKClient.prototype.watch = function watch(p, options, callback) { path: _p }, 'watch: notification fired'); - if (!done) { + if (!done && event != "session" && event != "unknown") { emitter.emit(event); register(); } From 5dfe2c7a01c9241c0da2f724d6a5c634e64a588b Mon Sep 17 00:00:00 2001 From: abarre Date: Fri, 14 Dec 2012 17:27:02 +0100 Subject: [PATCH 4/6] Don't emit a global event error for a data parsing. --- lib/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index e5c425c..f004b22 100644 --- a/lib/client.js +++ b/lib/client.js @@ -737,7 +737,7 @@ ZKClient.prototype.watch = function watch(p, options, callback) { } catch (e) { log.error({err: e}, 'error while parsing data', e); - return (self.emit('error', e)); + return (emitter.emit('error', e)); } log.trace({ From d1bbb774f32759b81c3a5c6ce2311410df3c948c Mon Sep 17 00:00:00 2001 From: abarre Date: Mon, 23 Sep 2013 13:40:17 +0200 Subject: [PATCH 5/6] Update zookeeper for node 0.10 --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 57251e4..7defc53 100644 --- a/package.json +++ b/package.json @@ -17,10 +17,10 @@ "main": "./lib/index.js", "dependencies": { "assert-plus": "0.1.2", - "bunyan": "0.16.5", + "bunyan": "0.21.1", "node-uuid": "1.4.0", - "vasync": "1.3.2", - "zookeeper": "git://github.com/yunong/node-zookeeper.git#3a0545d" + "vasync": "1.3.3", + "zookeeper": "git://github.com/yunong/node-zookeeper.git#ef48f8c24b42d6c1b1c3dbab82278666bed37b70" }, "devDependencies": { "cover": "0.2.8", From ec1ac08affbaff59d9ea52783345fcbd057fc6d6 Mon Sep 17 00:00:00 2001 From: abarre Date: Fri, 27 Sep 2013 09:26:55 +0200 Subject: [PATCH 6/6] use once on callbacks and cleanup double callback cases in stat --- lib/client.js | 27 +++++++++++++++++++++++++-- package.json | 1 + 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/client.js b/lib/client.js index f004b22..6811223 100644 --- a/lib/client.js +++ b/lib/client.js @@ -6,6 +6,7 @@ var util = require('util'); var assert = require('assert-plus'); var vasync = require('vasync'); +var once = require('once'); var ZK = require('zookeeper'); var ZKError = require('./error').ZKError; @@ -313,6 +314,8 @@ ZKClient.prototype.mkdirp = function mkdirp(p, callback) { assert.string(p, 'path'); assert.func(callback, 'callback'); + callback = once(callback); + var dirs = path.normalize(p).split('/').slice(1); var log = this.log; var self = this; @@ -327,6 +330,7 @@ ZKClient.prototype.mkdirp = function mkdirp(p, callback) { tasks.push(function checkIfExists(_, cb) { log.trace('mkdirp: checking %s', dir); self.stat(dir, function (err, _stat) { + cb = once(cb); if (err && err.code !== ZK.ZNONODE) { cb(); } @@ -337,6 +341,7 @@ ZKClient.prototype.mkdirp = function mkdirp(p, callback) { }); tasks.push(function mkdirIfNotExists(_, cb) { + cb = once(cb); if (exists) { cb(); } else { @@ -367,12 +372,15 @@ ZKClient.prototype.put = function put(p, object, options, callback) { assert.object(options, 'options'); assert.func(callback, 'callback'); + callback = once(callback); + var exists; var log = this.log; var _p = path.normalize(p); var self = this; var tasks = [ function checkIfExists(_, cb) { + cb = once(cb); log.trace('put: checking %s', _p); self.stat(_p, function (err, _stat) { if (err && err.code !== ZK.ZNONODE) { @@ -385,6 +393,7 @@ ZKClient.prototype.put = function put(p, object, options, callback) { }, function putIfNotExists(_, cb) { + cb = once(cb); if (exists) { cb(); } else { @@ -394,7 +403,7 @@ ZKClient.prototype.put = function put(p, object, options, callback) { }, function set(_, cb) { - self.update(_p, object, cb); + self.update(_p, object, once(cb)); } ]; @@ -423,6 +432,8 @@ ZKClient.prototype.readdir = function readdir(p, callback) { var _p = path.normalize(p); var zk = this.zk; + callback = once(callback); + log.trace({path: p}, 'readdir: entered'); zk.a_get_children(_p, false, function (rc, msg, nodes) { if (rc !== 0) { @@ -441,6 +452,8 @@ ZKClient.prototype.rmr = function rmr(p, callback) { assert.string(p, 'path'); assert.func(callback, 'callback'); + callback = once(callback); + var _done = false; var inflight = 0; var log = this.log; @@ -488,7 +501,7 @@ ZKClient.prototype.rmr = function rmr(p, callback) { nodes.forEach(function (n) { tasks.push(function (_, cb) { - self.unlink(n, cb); + self.unlink(n, once(cb)); }); }); @@ -504,6 +517,8 @@ ZKClient.prototype.stat = function stat(p, callback) { assert.string(p, 'path'); assert.func(callback, 'callback'); + callback = once(callback); + var log = this.log; var zk = this.zk; @@ -541,6 +556,8 @@ ZKClient.prototype.unlink = function unlink(p, options, callback) { assert.object(options, 'options'); assert.func(callback, 'callback'); + callback = once(callback); + var log = this.log; var _p = path.normalize(p); var zk = this.zk; @@ -577,12 +594,15 @@ ZKClient.prototype.update = function update(p, object, options, callback) { assert.object(options, 'options'); assert.func(callback, 'callback'); + callback = once(callback); + var data = JSON.stringify(object); var log = this.log; var _p = path.normalize(p); var self = this; var tasks = [ function getVersion(_, cb) { + cb = once(cb); if (version !== undefined) return (cb()); self.stat(_p, function (err, _stat) { @@ -596,6 +616,7 @@ ZKClient.prototype.update = function update(p, object, options, callback) { return (undefined); }, function write(_, cb) { + cb = once(cb); zk.a_set(_p, data, version, function (rc, msg) { if (rc !== 0) { cb(new ZKError(rc, msg)); @@ -633,6 +654,8 @@ ZKClient.prototype.watch = function watch(p, options, callback) { assert.object(options, 'options'); assert.func(callback, 'callback'); + callback = once(callback); + var log = this.log; var _p = path.normalize(p); var self = this; diff --git a/package.json b/package.json index 7defc53..a41ce6d 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "assert-plus": "0.1.2", "bunyan": "0.21.1", "node-uuid": "1.4.0", + "once": "1.1.1", "vasync": "1.3.3", "zookeeper": "git://github.com/yunong/node-zookeeper.git#ef48f8c24b42d6c1b1c3dbab82278666bed37b70" },