尽管长时间超时,PHP仍会丢失mongoDB游标 [英] PHP loses mongoDB cursor despite long timeouts

查看:62
本文介绍了尽管长时间超时,PHP仍会丢失mongoDB游标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样运行一个长的mongoDB查询:

I'm running a long mongoDB query like this:

foreach($xyz->find(...)->timeout(24 * 60 * 60 * 1000)->maxTimeMS(24 * 60 * 60 * 1000) as $document) {
    ...
}

尽管客户端和服务器有24小时超时,但几分钟后脚本仍会以MongoCursorException退出:

But despite those 24 hour timeouts for the client and the server, the script exits with a MongoCursorException after a few minutes:

localhost:27017:在集合xyz上找不到光标

localhost:27017: could not find cursor over collection xyz

我在使用v1.6.10 mongoDB驱动程序的PHP 5.4.该数据库为mongoDB 3.0.4. PHP将连接到mongos实例,集合xyz被分片.

I'm on PHP 5.4 with the v1.6.10 mongoDB driver. The DB is mongoDB 3.0.4. PHP will connect to a mongos instance, the collection xyz is sharded.

您知道什么可能导致此异常吗?

Any idea what may cause this exception?

推荐答案

似乎还有更多的超时(PHP尚未完全支持).其中之一是 maxIdleTimeMS :

It appears that there are more timeouts which are not all supported by PHP (yet). One of them is maxIdleTimeMS:

在连接被删除和关闭之前,连接在池中可以保持空闲状态的最大毫秒数.

The maximum number of milliseconds that a connection can remain idle in the pool before being removed and closed.

在我的情况下发生的是,由于数据库有时很忙,因此这个长时间运行的查询处于空闲状态太长时间了,因此它的游标在服务器端丢失了.

What happened in my case was that, due to the database being busy at times, this long running query was idle for too long so its cursor got dropped server-side.

设置此参数会有所帮助,但是由于它不可用,因此我发现解决问题的方法是减小

Setting this parameter would've helped but since it's not available, I've found that what solved the problem was to decrease the batch size:

$cursor->batchSize(-20)

在此示例中,每批最多在服务器和客户端之间传输20个文档.这样可以减少连接闲置时间过长的可能性.当然,确切的值取决于数据库的负载,处理每个文档所需的时间等.

In this example, at most 20 documents are transferred between the server and the client for each batch. This reduces the probability that the connection is idle for too long. Of course, the exact value depends on the load of your database, how long you need to process each document etc.

这篇关于尽管长时间超时,PHP仍会丢失mongoDB游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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