Eduardo de Luna
Eduardo de Luna

Reputation: 491

Aerospike works ok for 1 - 4 hours before defaulting to a connection error

I have been working with the Aerospike Node.js client for a while now, and I've noticed that if I run the server for more than two hours, if I make a connection call to an Aerospike database I get the following error:

ERROR(23159) [connect.cc:69] [Connect] - Connecting to Cluster Failed

Before this happens, execution goes smoothly and without a hitch. I'm using Aerospike coupled with Express to store and retrieve user content. This is one of the endpoints I have written

app.get("/api/content/:id",function(req,res){
    aero.client(aeroSec.generateConfig()).connect(function(err, db){
        if(err.code != aero.status.AEROSPIKE_OK)  return res.status(403).send();
        var options = {};
        options.filters = [filter.equal("secindex", req.params.id)];
        var query = db.query("ns","posts",options);
        var data;
        var count = 0;
        var s = query.execute();
        s.on("data",function(rec){
           data = rec;
        });
        s.on("error",function(err){
            data = err;    
        });
        s.on("end",function(){
           db.close();
           if(data == undefined) res.status(404).send("NOTHING FOUND");
           else res.status(200).send(data);
        });   
    });
  });

In the interest of full disclosure, I am not querying an index that doesn't exist. A search in the Aerospike forums suggested that I comment out db.close() out of my endpoints, which only slows down the problem and doesn't eliminate it. The error started happening when I connected to a Aerospike database hosted on another server, so I decided to query a local server instead only to find the same error after two hours of normal operation.

Does anyone know what to do? I really like Aerospike as a database, but if these problems persist I will have no other choice but to go to another NoSQL database.

Upvotes: 2

Views: 200

Answers (1)

Gayathri
Gayathri

Reputation: 121

The code snippet shows that application creates as many client objects as number of requests received. The common usage is, a single client object is created and this is shared for all communications to Aerospike server. Another clarification is, client.connect() is a synchronous API call. And there is a sample web application in node.js using express and Aerospike. Here is the link. Please refer here for more clarifications.

The above code can be refactored like this

 var aerospike = require('aerospike');
 var express   = require('express');
 var app       = express();
 var aerospikeConfig = {
      // add a node in the cluster.
      hosts: [{addr: "192.168.0.1", port: 3000 }] 
 }  
 var client = aerospike.client(aerospikeConfig);
 // client.connect() is a synchronous API call.
 client.connect(function(err, client) {
    if(err.code != aerospike.status.AEROSPIKE_OK) {
        // application should exit or re-attempt the connect call.
        // If connect call fails, connection with Aerospike server is
        // is not established. Subsequent aerospike API calls will fail.
        return err; 
    }
 });

app.get("/api/content/:id",function(req,res){
    var options = {};
    options.filters = [filter.equal("secindex", req.params.id)];
    var query = client.query("ns","posts",options);
    var data;
    var count = 0;
    var s = query.execute();
    s.on("data",function(rec){
       data = rec;
    });
    s.on("error",function(err){
        data = err;    
    });
    s.on("end",function(){
       client.close();
       if(data == undefined) res.status(404).send("NOTHING FOUND");
       else res.status(200).send(data);
    });   
});

Thanks

Upvotes: 4

Related Questions