Navigation Menu

Skip to content

Commit

Permalink
Don't create new connection for an engine node while cluster structur…
Browse files Browse the repository at this point in the history
…e is changing
  • Loading branch information
piroor committed Apr 20, 2015
1 parent d10a86e commit 6356609
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/adapter/http.js
Expand Up @@ -3,6 +3,8 @@ var wrapper = require('./wrapper');
var api = require('./api');
var ConsoleLogger = require('../console-logger').ConsoleLogger;

var CONNECTION_RETRY_INTERVAL = 1000;

function createRequestResponseHandler(params) {
params = params || {};
var connectionPool = params.connectionPool;
Expand Down Expand Up @@ -95,6 +97,15 @@ function createGenericHandler(params) {
return;
}
var connection = connectionPool.get();
if (!connection) {
// When the cluster members are changing, we cannot get
// actual connection for a member, so retry later.
setTimeout(function() {
processRequest();
}, CONNECTION_RETRY_INTERVAL);
return;
}

var wrappedConnection = new wrapper.DroongaProtocolConnectionWrapper(connection, options);
try {
definition.onHandle(request, response, wrappedConnection);
Expand Down
11 changes: 11 additions & 0 deletions lib/droonga-protocol/connection-pool.js
Expand Up @@ -69,6 +69,11 @@ ConnectionPool.prototype = {
},

get: function() {
// When the cluster members are changing, we cannot get
// actual connection for a member.
if (this.updating)
return null;

var hostName = this.hostNames[this.nextIndex];

this.nextIndex++;
Expand Down Expand Up @@ -196,6 +201,10 @@ ConnectionPool.prototype = {
},

updateHostNamesFromCluster: function() {
if (this.updating)
return Q.Promise.resolve();

this.updating = true;
return this.getEnginesFromCluster()
.then((function(engines) {
this.clusterId = engines[0].Tags.cluster_id;
Expand All @@ -209,11 +218,13 @@ ConnectionPool.prototype = {
this._logger.info('List of droonga-engine hosts is successfully initialized from the cluster.');
this._logger.info('cluster id: '+this.clusterId);
this._logger.info(hostNames);
this.updating = false;
return hostNames;
}).bind(this))
.catch(function(error) {
this._logger.error('Failed to initialize the list of droonga-engine hosts from the cluster.');
this._logger.error(error);
this.updating = false;
});
},

Expand Down

0 comments on commit 6356609

Please sign in to comment.