Confusing Behavior of client.info('statistics') on Cluster
See original GitHub issueThis might be caused by my lack of understanding of JS/Node.js in general but when I tried to do something like this (assuming client being successfully connected already):
var i = 0;
client.info('statistics', function(err, response, host) {
console.log(err, i++);
});
The output would be:
{ code: 0, message: null, func: null, file: null, line: null } 0
{ code: 0, message: null, func: null, file: null, line: null } 1
{ code: 0, message: null, func: null, file: null, line: null } 2
{ code: 0, message: null, func: null, file: null, line: null } 3
{ code: 0, message: null, func: null, file: null, line: null } 4
{ code: 0, message: null, func: null, file: null, line: null } 5
{ code: 0, message: null, func: null, file: null, line: null } 6
{ code: 0, message: null, func: null, file: null, line: null } 7
Which indicates to me that this anonymous callback function gets invoked N times, where N being the number of nodes of given cluster in the client connection.
I know there might be techniques to work around this behavior to have a streamlined workflow to collect all statistics of all the nodes within given cluster synchronously (?), but wouldn’t it be easier to have this request (I haven’t tested other requests through client.info
API) behave somewhat similar to client.batchGet
where the callback would get an array of results
and the callback being effectively just executed once?
I’m happy to elaborate more on this issue. My specific use case is to have a client regularly checks a given Aerospike cluster’s health, and maybe there’s a better and easier way to do it? Regardless, in my opinion the current behavior of client.info('statistics')
is a little bit weird and due to the async nature of js/node.js, it’s really hard to collect info from a cluster of nodes.
To give an example of this undesirable behavior, I’m using async
, and client.info('statistics')
is one of the tasks (I’m using async.parallel
if this is relevant):
var jobs = [];
// some other jobs
jobs.push(function(callback) {
client.info('statistics', function(err, response, host) {
callback(err);
});
});
And async
would complain to me as Error: Callback was already called.
(expected as my observation in the first example).
Issue Analytics
- State:
- Created 9 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
The info method works as designed, i.e. it returns the requested info per each cluster node through the callback method as soon it is received by the client. Note that the method also has an additional, 2nd callback, which is called once all the nodes have responded:
See the API docs for the full method signature.
But I get how this behavior be a bit unexpected and that for many use cases it would be more convenient to have all the info returned in a single callback. I’ll consider this a feature request for a future update. A pull request would be welcome as well. 😃
I’ve added a new
Client#infoAll
method, which queries all the cluster nodes and returns the results in a single callback method call. This will be included in the next release, which is tentatively scheduled for next week.