Rails Mongoid 在数据库无法访问时是否会自动重连?

lithium4010 · November 28, 2017 · 1836 hits

生产事故

之前生产环境发生了因为阿里云运维数据库导致所有 puma 进程以及 sidekiq 进程无法链接数据库,报错

Mongo::Error::NoServerAvailable: No server is available matching preference

重启应用实例后恢复正常链接。

试图复现问题

起一个rails c 连接生产数据库,执行以下脚本

loop do
  begin 
    puts Document.count
    sleep 3
  rescue Mongo::Error::NoServerAvailable => e; 
    puts e.message
  end
end 

控制台正常输出文档数目。

主机添加 iptables 规则 block 掉数据库端口的访问

sudo iptables -A OUTPUT -p tcp --dport xxxxxx -j DROP

等待发现控制台报错

No server is available matching preference ...

恢复数据库访问

sudo iptables -D OUTPUT -p tcp --dport xxxxxx -j DROP

等待后发现控制台恢复正常输出文档数目。

求助

根据上面的测试我认为 mongoid 是会自动处理数据库挂机后的重连的,那么生产事故是如何发生的?当时为何没有自动重连?

lithium4010 closed this topic. 20 Apr 11:00
You need to Sign in before reply, if you don't have an account, please Sign up first.