YUKI Hiroshi
null+****@clear*****
Sun Nov 30 03:56:11 JST 2014
YUKI Hiroshi 2014-11-30 03:56:11 +0900 (Sun, 30 Nov 2014) New Revision: 7cb6af5748a18dd5a6d852c9e5569cf2d9b38c84 https://github.com/droonga/express-droonga/commit/7cb6af5748a18dd5a6d852c9e5569cf2d9b38c84 Message: Join to the default cluster if the list of live nodes becomes blank Added files: bin/express-droonga-join-to-cluster Modified files: lib/droonga-protocol/connection-pool.js lib/serf/client.js package.json Added: bin/express-droonga-join-to-cluster (+30 -0) 100755 =================================================================== --- /dev/null +++ bin/express-droonga-join-to-cluster 2014-11-30 03:56:11 +0900 (f133730) @@ -0,0 +1,30 @@ +#!/usr/bin/env node +// -*- js -*- +var SerfClient = require('../lib/serf/client'); + +var options = require('../lib/serf/options'); +options = options.define() + .add('--hosts <hosts>', + 'Comma-separated list of host names.', + function(newValue, oldValue) { + return newValue.trim().split(/[, ]+/); + }) + .parse(process.argv); + +var client = new SerfClient({ + rpcAddress: options.rpcAddress, + enginePort: options.droongaEnginePort, + tag: options.tag +}); + +client.joinToCluster({ hosts: options.hosts || [] }) + .then(function(members) { + members.forEach(function(member) { + console.log(member); + }); + process.exit(0); + }) + .catch(function(error) { + console.error(error); + process.exit(1); + }); Modified: lib/droonga-protocol/connection-pool.js (+29 -1) =================================================================== --- lib/droonga-protocol/connection-pool.js 2014-11-29 02:26:13 +0900 (76cf77c) +++ lib/droonga-protocol/connection-pool.js 2014-11-30 03:56:11 +0900 (245ee14) @@ -28,6 +28,8 @@ function ConnectionPool(params) { this.hostNames = this._params.hostNames || this._params.hostName || Connection.DEFAULT_FLUENT_HOST_NAME; + + this._initialHostNames = this._hostNames.slice(0); } ConnectionPool.prototype = { @@ -153,6 +155,26 @@ ConnectionPool.prototype = { }); }).bind(this)); }, + reJoinToInitialCluster: function() { + return Q.Promise((function(resolve, reject, notify) { + // Because node-rpc has no API to disconnect from the RPC host, + // we should use it in a separate expendable process. + var commandPath = path.join(__dirname, '..', '..', 'bin', + 'express-droonga-join-to-cluster'); + var commandLine = [ + commandPath, + '--rpc-address=' + this._serf.rpcAddress, + '--droonga-engine-port=' + this._params.port, + '--tag=' + this._params.tag, + '--hosts=' + this._initialHostNames.join(',') + ].join(' '); + exec(commandLine, function(error, stdin, stdout) { + if (error) + return reject(error); + resolve(); + }); + }).bind(this)); + }, updateHostNamesFromCluster: function() { return this.getHostNamesFromCluster() @@ -187,7 +209,12 @@ ConnectionPool.prototype = { return; this._updateHostNamesTimer = setTimeout((function() { this._updateHostNamesTimer = null; - this.updateHostNamesFromCluster(); + this.updateHostNamesFromCluster() + .then((function() { + if (this._hostNames.length == 0) { + return this.reJoinToInitialCluster(); + } + }).bind(this)); }).bind(this), 500); }).bind(this)); resolve(); @@ -215,6 +242,7 @@ ConnectionPool.prototype = { startSyncHostNamesFromCluster: function() { + this._defaultHostNames = this._hostNames.slice(0); return this.updateHostNamesFromCluster() .then(this.startWatchClusterChanges.bind(this)); }, Modified: lib/serf/client.js (+19 -1) =================================================================== --- lib/serf/client.js 2014-11-29 02:26:13 +0900 (a490c0b) +++ lib/serf/client.js 2014-11-30 03:56:11 +0900 (8f7e189) @@ -82,7 +82,25 @@ Client.prototype = { member.Status == 'alive'; }, this); }).bind(this)); - } + }, + + joinToCluster: function(parameters) { + var hosts = parameters.hosts; + return this.connect().then((function() { + return Q.Promise((function tryJoin(resolve, reject, notify) { + var host = hosts.shift(); + this._client.join(host, function(error, result) { + if (error) { + if (hosts.length == 0) + return reject(error); + else + tryJoin.call(this, resolve, reject, notify); + } + resolve(result.Existing); + }); + }).bind(this)); + }).bind(this)); + }, }; module.exports = Client; Modified: package.json (+1 -0) =================================================================== --- package.json 2014-11-29 02:26:13 +0900 (a1eb2e3) +++ package.json 2014-11-30 03:56:11 +0900 (12e9724) @@ -49,6 +49,7 @@ }, "scripts": { "express-droonga-report-live-engine-hosts": "./bin/express-droonga-report-live-engine-hosts", + "express-droonga-join-to-cluster": "./bin/express-droonga-join-to-cluster", "test": "NODE_ENV=test ./node_modules/.bin/mocha --reporter list --ui tdd --timeout 5s --recursive" }, "main": "index", -------------- next part -------------- HTML����������������������������... 下載