Skip to content

Commit

Permalink
Client: Max execute retries #7
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgebay committed Jul 18, 2013
1 parent 42c5ae2 commit 232ef4d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ var hosts = ['host1:9042', 'host2:9042', 'host3', 'host4'];
var cqlClient = new Client({hosts: hosts, keyspace: 'Keyspace1'});
```
Client() accepts an object with these slots:

hosts : String list in host:port format. Port is optional (defaults to 9042).
keyspace : Name of keyspace to use.
username : User for authentication (optional).
password : Password for authentication (optional).
version : Currently only '3.0.0' is supported (optional).
staleTime : Time in milliseconds before trying to reconnect(optional).

Queries are performed using the `execute()`. For example:
```
hosts : String list in host:port format.
Port is optional (defaults to 9042).
keyspace : Name of keyspace to use (optional).
username : User for authentication (optional).
password : Password for authentication (optional).
version : Currently only '3.0.0' is supported (optional).
staleTime : Time in milliseconds before trying to reconnect(optional).
maxExecuteRetries : Maximum amount of times an execute can be retried
using another connection, in case the server is unhealthy (optional).
```
Queries are performed using the `execute()` method. For example:
```javascript
// Reading
cqlClient.execute('SELECT key, email, last_name FROM user_profiles WHERE key=?', ['jbay'],
Expand Down Expand Up @@ -94,7 +97,7 @@ con.open(function(err) {
Instances of `Client()` and `Connection()` are `EventEmitter`'s and emit `log` events:
```javascript
var Connection = require('node-cassandra-cql').Connection;
var con = new Connection({host:'host1', port:9042, keyspace:'Keyspace1', username:'user', password:'password'});
var con = new Connection({host:'host1', port:9042, keyspace:'Keyspace1'});
con.on('log', function(level, message) {
console.log('log event: %s -- %j', level, message);
});
Expand Down
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Client.prototype.getAConnection = function (callback) {
Executes a query in an available connection.
@param {function} callback, executes callback(err, result) when finished
*/
Client.prototype.execute = function (query, args, consistency, callback) {
Client.prototype.execute = function (query, args, consistency, callback, retryCount) {
if(typeof callback === 'undefined') {
if (typeof consistency === 'undefined') {
//only the query and the callback was specified
Expand Down Expand Up @@ -202,9 +202,16 @@ Client.prototype.execute = function (query, args, consistency, callback) {
if (self.isServerUnhealthy(err)) {
//if its a fatal error, set the connection to unhealthy
self.setUnhealthy(c);
self.emit('log', 'error', 'There was an error executing a query, retrying execute (will get another connection)', err);
//retry, it will get another connection
self.execute.call(self, query, args, consistency, callback, retryCount);
retryCount = (!retryCount) ? 0 : retryCount;
if (retryCount === self.options.maxExecuteRetries) {
callback(err, result, retryCount);
}
else {
//retry, it will get another connection
self.emit('log', 'error', 'There was an error executing a query, retrying execute (will get another connection)', err);
retryCount++;
self.execute.call(self, query, args, consistency, callback, retryCount);
}
}
else {
//If the result is OK or there is error (syntax error or an unauthorized for example), callback
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Jorge Bay <jorgebaygondra@gmail.com>",
"name": "node-cassandra-cql",
"version": "0.1.7",
"version": "0.1.8",
"description": "Node.js driver for Cassandra CQL3 binary protocol",
"keywords": [
"cassandra",
Expand Down
23 changes: 20 additions & 3 deletions test/clientTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,37 @@ module.exports = {
callback(null);
}
});
}/*,
},
function (callback) {
//no query params
client.execute('SELECT * FROM system.schema_keyspaces', function(err){
client.execute('SELECT * FROM system.schema_keyspaces', function(err) {
callback(err);
});
}*/
}
],
//all finished
function(err){
test.ok(err === null, err);
test.done();
});
},
'max execute retries': function (test) {
//Only 1 retry
client.options.maxExecuteRetries = 1;
var isServerUnhealthyOriginal = client.isServerUnhealthy;

//Change the behaviour so every err is a "server error"
client.isServerUnhealthy = function (err) {
return true;
};

client.execute('WILL FAIL AND EXECUTE THE METHOD FROM ABOVE', function (err, result, retryCount){
test.ok(err, 'The execution must fail');
test.equal(retryCount, client.options.maxExecuteRetries, 'It must retry executing the times specified');
client.isServerUnhealthy = isServerUnhealthyOriginal;
test.done();
});
},
'shutdown': function(test) {
client.shutdown(function(){
test.done();
Expand Down

0 comments on commit 232ef4d

Please sign in to comment.