Permalink
Browse files

add support for elasticsearch cluster discovering.

-> configure one node and discover others using /_cluster/nodes
  • Loading branch information...
1 parent e8db8a6 commit 5dd6fc3c0c73f2a7dad5539e6ed5c0024cced229 nicoo committed Sep 1, 2011
Showing with 68 additions and 24 deletions.
  1. +25 −19 lib/elasticsearchclient/calls/cluster.js
  2. +43 −5 lib/elasticsearchclient/elasticSearchClient.js
@@ -25,13 +25,16 @@ ElasticSearchClient.prototype.state = function(options) {
return this.createCall({path: path, method: 'GET'}, this.clientOptions);
}
-ElasticSearchClient.prototype.nodesInfo = function(nodes, options) {
+
+ElasticSearchClient.prototype.nodesInfo = function(nodes, options){
var path = '/_cluster/nodes'
- for(i=0;i<nodes;i++){
- if(i==0)
- path+='/'+nodes[i]
- else
- path+=','+nodes[i]
+ if(nodes instanceof Array){
+ for(n in nodes){
+ if(n==0)
+ path+='/'+nodes[n]
+ else
+ path+=','+nodes[n]
+ }
}
var qs = '';
if (options) {
@@ -43,16 +46,17 @@ ElasticSearchClient.prototype.nodesInfo = function(nodes, options) {
return this.createCall({path: path, method: 'GET'}, this.clientOptions);
}
-
-
ElasticSearchClient.prototype.nodesStats = function(nodes, options) {
var path = '/_cluster/nodes'
- for(i=0;i<nodes;i++){
- if(i==0)
- path+='/'+nodes[i]
- else
- path+=','+nodes[i]
+ if(nodes instanceof Array){
+ for(n in nodes){
+ if(n==0)
+ path+='/'+nodes[n]
+ else
+ path+=','+nodes[n]
+ }
}
+
path+='/stats'
var qs = '';
if (options) {
@@ -66,11 +70,13 @@ ElasticSearchClient.prototype.nodesStats = function(nodes, options) {
ElasticSearchClient.prototype.nodesShutdown = function(nodes, options) {
var path = '/_cluster/nodes'
- for(i=0;i<nodes;i++){
- if(i==0)
- path+='/'+nodes[i]
- else
- path+=','+nodes[i]
+ if(nodes instanceof Array){
+ for(n in nodes){
+ if(n==0)
+ path+='/'+nodes[n]
+ else
+ path+=','+nodes[n]
+ }
}
path+='/_shutdown'
var qs = '';
@@ -81,4 +87,4 @@ ElasticSearchClient.prototype.nodesShutdown = function(nodes, options) {
path += "?" + qs;
}
return this.createCall({path: path, method: 'POST'}, this.clientOptions);
-}
+}
@@ -13,12 +13,50 @@ function ElasticSearchClient(options) {
this.clientOptions = options || {}
}
-
ElasticSearchClient.prototype.createCall = function(params, options) {
+ return new ElasticSearchCall(params, this.nextHost())
+}
+
+ElasticSearchClient.prototype.nextHost = function() {
//If options.hosts round robin the hosts
- if (options.hosts) {
- var nextHost = options.hosts.shift()
- options.hosts.push(nextHost)
+ if (this.clientOptions.hosts) {
+ var nextHost = this.clientOptions.hosts.shift()
+ this.clientOptions.hosts.push(nextHost)
}
- return new ElasticSearchCall(params, nextHost)
+ return nextHost;
+}
+
+var inetRE = /inet\[\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\:(\d{1,5})\]/
+
+/**
+ * Discover nodes of the elasticsearch cluster.
+ * Call this function to automatically add all nodes of the
+ * cluster in the hosts list.
+ */
+ElasticSearchClient.prototype.discoverNodes = function(){
+ var self = this;
+ var query = this.nodesInfo();
+ var hosts = [];
+ query.on('data', function(data){
+ var obj = JSON.parse(data);
+ var nodes = obj.nodes;
+ for(n in nodes){
+ node = nodes[n];
+ if(!"http_address" in node){
+ continue;
+ }
+
+ address = inetRE.exec(node.http_address);
+ if(!address){
+ continue;
+ }
+
+ hosts.push({ host : address[1], port : address[2] });
+ }
+
+ if(hosts.length > 0){
+ self.clientOptions.hosts = hosts;
+ }
+ });
+ query.exec();
}

0 comments on commit 5dd6fc3

Please sign in to comment.