问题
My NodeJS application hangs on the “find” query of the MongoDB when accessed by the 200 or more users simultaneously.
To demonstrate/reproduce the issue I have built following small POC which has one route and it connects to the MongoDB and fetches the data. When I create around 500 simultaneous request using LoadUIweb, sometimes, the “findone” function of collection never returns.
var express = require('express');
var mongodb = require('mongodb');
var config = require('../Config/Config');
var logger = require('../Config/LogManager');
var app = express();
var MONGODB_URI = config.PCMMongoDB;
var db;
var coll;
// Initialize connection once
mongodb.MongoClient.connect(MONGODB_URI, function(err, database) {
if(err) throw err;
db = database;
coll = db.collection('FacilitySettings');
app.listen(3000);
console.log('Listening on port 3000');
});
// Reuse database/collection object
app.get('/', function(req, res) {
logger.loggerinfo.info("Got the request");
//var result = new Array();
coll.find({}, function(err, docs) {
logger.loggerinfo.info("Got the Response", err);
docs.each(function(err, doc) {
if(err){
logger.loggerinfo.info("Got the error while iterating", err);
res.end();
}
if(doc) {
logger.loggerinfo.info("Iterated the Response");
res.write(JSON.stringify(doc) + "\n");
//result.push(doc);
}
else {
logger.loggerinfo.info("Returned the Response");
res.end();
}
});
});
});
The “FacilitySettings” collection has around 100 documents.
When the server hangs, it prints “Got the Request” and “Got the Response” but it never prints “Iterated the Response” and “returned the Response”. It doesn’t seem that the Nodes server itself is hanged because it accepts the new request. It seems that the result from the MongoDB is never returned. When I check the last executed queries on the MongoDB, it seems that the query never reaches the MongoDB. I am using following versions:
MongoDB: 3.2.3,
Nodes: 5.2.0,
MongoDB driver: 2.1.7
Has anyone encountered this issue before? What are the tools available, for MongoDB or for Nodes, to trace the cause? Who is the culprit here? Is it MongoDB or is it Node’s MongoDB native driver?
回答1:
Posting this just incase anyone else comes across same issue.
I updated the MongoDB driver (version 2.1.13) and issue seems to be resolved now. Then I took a diif between the version I was using, v 2.1.7 and 2.1.13 and I found that there were few changes made to cursor class, noticeable around the exhaust condition. I am not sure if this is a relevant change or there is something else that has been fixed which resolved my case.
来源:https://stackoverflow.com/questions/36264331/nodejs-server-hangs-on-find-query-of-mongodb-on-simultanious-requests