我可以设置Mongoid查询超时吗?Mongoid不杀长时间查询

时间:2022-03-07 21:13:03

Mongoid don't have timeout option.
http://mongoid.org/en/mongoid/docs/installation.html

Mongoid没有超时选项。http://mongoid.org/en/mongoid/docs/installation.html

I want Mongoid to kill long time queries. How can I set Mongoid query timeout?

我想让Mongoid消除长时间的查询。如何设置Mongoid查询超时?

If I do nothing, Mongoid wait a long time like the below.

如果我什么都不做,蒙古人就等了很长时间。

mongo > db.currentOp()

{
    "opid" : 34973,
    "active" : true,
    "secs_running" : 1317, // <- too long!
    "op" : "query",
    "ns" : "db_name.collection_name",
    "query" : {
    "$msg" : "query not recording (too large)"
  },
    "client" : "123.456.789.123:46529",
    "desc" : "conn42",
    "threadId" : "0x7ff5fb95c700",
    "connectionId" : 42,
    "locks" : {
    "^db_name" : "R"
  },
    "waitingForLock" : true,
    "numYields" : 431282,
    "lockStats" : {
    "timeLockedMicros" : {
     "r" : NumberLong(514304267),
     "w" : NumberLong(0)
  },
    "timeAcquiringMicros" : {
     "r" : NumberLong(1315865170),
     "w" : NumberLong(0)
    }
  }
 }

3 个解决方案

#1


1  

Actually, all queries have a timeout by default. You can set a no_timeout option to tell the query to never timeout.Take a look here

实际上,所有查询默认都有一个超时。您可以设置no_timeout选项来告诉查询永不超时。看一看这里

You can configure the timeout period using a initializer, like this

可以使用初始化器配置超时周期,如下所示

Mongoid.configure do |config|
  config.master = Mongo::Connection.new(
    "localhost", 27017, :op_timeout => 3, :connect_timeout => 3
  ).db("mongoid_database")
end

#2


1  

The default query timeout is typically 60 seconds for Mongoids but due to Rubys threading there tend to be issues when it comes to shutting down processes properly.

对于mongoid,默认的查询超时时间通常为60秒,但由于Rubys线程的存在,在正确地关闭进程时往往会出现问题。

Hopefully none of your requests will be putting that strain on the Mongrels but if you keep running into this issue I would consider some optimization changes to your application or possibly consider adopting God or Monit. If you aren't into that and want to change your request handing I might recommend Unicorn if you are already on nginx (github has made this transition and you can read about it more here)

希望您的任何请求都不会对Mongrels造成压力,但是如果您继续运行这个问题,我将考虑对您的应用程序进行一些优化更改,或者考虑采用上帝或Monit。如果你不喜欢,想改变你的请求如果你已经在nginx上了,我可能会推荐独角兽。

#3


0  

Try this solution:

试试这个解决方案:

ModelName.all.no_timeout.each do |m|
  "do something with model"
end

https://*.com/a/19987744/706022

https://*.com/a/19987744/706022

#1


1  

Actually, all queries have a timeout by default. You can set a no_timeout option to tell the query to never timeout.Take a look here

实际上,所有查询默认都有一个超时。您可以设置no_timeout选项来告诉查询永不超时。看一看这里

You can configure the timeout period using a initializer, like this

可以使用初始化器配置超时周期,如下所示

Mongoid.configure do |config|
  config.master = Mongo::Connection.new(
    "localhost", 27017, :op_timeout => 3, :connect_timeout => 3
  ).db("mongoid_database")
end

#2


1  

The default query timeout is typically 60 seconds for Mongoids but due to Rubys threading there tend to be issues when it comes to shutting down processes properly.

对于mongoid,默认的查询超时时间通常为60秒,但由于Rubys线程的存在,在正确地关闭进程时往往会出现问题。

Hopefully none of your requests will be putting that strain on the Mongrels but if you keep running into this issue I would consider some optimization changes to your application or possibly consider adopting God or Monit. If you aren't into that and want to change your request handing I might recommend Unicorn if you are already on nginx (github has made this transition and you can read about it more here)

希望您的任何请求都不会对Mongrels造成压力,但是如果您继续运行这个问题,我将考虑对您的应用程序进行一些优化更改,或者考虑采用上帝或Monit。如果你不喜欢,想改变你的请求如果你已经在nginx上了,我可能会推荐独角兽。

#3


0  

Try this solution:

试试这个解决方案:

ModelName.all.no_timeout.each do |m|
  "do something with model"
end

https://*.com/a/19987744/706022

https://*.com/a/19987744/706022