NodeJS服务器在同时请求时挂起MongoDB的查找查询 [英] NodeJS Server Hangs on Find Query of MongoDB on simultanious requests

查看:113
本文介绍了NodeJS服务器在同时请求时挂起MongoDB的查找查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当同时有200个或更多用户访问时,我的NodeJS应用程序挂在MongoDB的查找"查询上.

My NodeJS application hangs on the "find" query of the MongoDB when accessed by the 200 or more users simultaneously.

为了演示/重现该问题,我遵循了一条具有一条路由的小型POC,该POC连接到MongoDB并获取数据.当我使用LoadUIweb创建大约500个并发请求时,有时集合的"findone"功能永远不会返回.

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();
      }
    });
  });
});

"FacilitySettings"集合包含大约100个文档.

The "FacilitySettings" collection has around 100 documents.

服务器挂起时,它会打印得到请求"和得到响应",但从不打印迭代响应"和返回响应".似乎节点服务器本身未挂起,因为它接受了新请求.看来MongoDB的结果永远不会返回. 当我检查MongoDB上最后执行的查询时,查询似乎从未到达MongoDB. 我正在使用以下版本:

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,

  • MongoDB: 3.2.3,

节点:5.2.0,

MongoDB驱动程序:2.1.7

MongoDB driver: 2.1.7

以前有人遇到过此问题吗? MongoDB或Node有什么可用的工具来跟踪原因?谁是罪魁祸首?是MongoDB还是Node的MongoDB本机驱动程序?

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?

推荐答案

发布此信息只是为了防止其他人遇到同一问题.

Posting this just incase anyone else comes across same issue.

我更新了MongoDB驱动程序(版本2.1.13),该问题现在似乎已解决.然后我在所使用的版本2.1.7和2.1.13之间进行了区分,发现对游标类所做的更改很少,在耗尽状态下很明显.我不确定这是否是相关更改,或者是否有其他解决方案可以解决我的问题.

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.

这篇关于NodeJS服务器在同时请求时挂起MongoDB的查找查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆