gpt4 book ai didi

多个应用服务器上的 MongoDB 连接失败

转载 作者:IT老高 更新时间:2023-10-28 13:22:50 28 4
gpt4 key购买 nike

我们有用于 golang 的带有 mgo 驱动程序的 mongodb。除了应用程序(golang 二进制文件)之外,还有两个应用程序服务器连接到运行的 mongodb。 Mongodb 作为副本集运行,每个服务器根据副本的当前状态连接两个主要或辅助。

我们在其中一台mongo服务器上遇到SocketException处理请求,关闭客户端连接:9001套接字异常(这导致我们的应用程序与mongodb的连接死亡。之后,副本集继续正常工作,但我们的第二台服务器(没有发生错误)连接也断开了。

在golang日志中表现为:

读取 tcp 10.10.0.5:37698-\u003e10.10.0.7:27017: i/o 超时

为什么会这样?如何预防?

据我了解,mgo 通过 url 连接到整个副本(它通过单个实例的 url 检测整个拓扑)但是为什么在其中一台服务器上的连接死亡杀死了它在第二个?

编辑:

  1. 使用的完整包路径"gopkg.in/mgo.v2"
  2. 很遗憾,这里无法共享 mongo 文件。但除了 socketexecption mongo 日志不包含任何有用的东西。有迹象表明存在某种程度的锁争用,在某些时候锁获取时间相当长,但除此之外没有任何变化
  3. MongoDB 有时会做一些繁重的索引,但最近并没有出现任何异常的峰值,所以它没有超出正常范围

最佳答案

首先,您正在使用的 mgo 驱动程序:gopkg.in/mgo.v2 由 Gustavo Niemeyer 开发(托管在 https://github.com/go-mgo/mgo )不再维护。

改为使用社区支持的 fork github.com/globalsign/mgo .这个继续得到修补和发展。

其变更日志包括:“改进的连接处理”,这似乎与您的问题直接相关。

详情请看这里https://github.com/globalsign/mgo/pull/5指向原始拉取请求 https://github.com/go-mgo/mgo/pull/437 :

If mongoServer fail to dial server, it will close all sockets that are alive, whether they're currently use or not. There are two cons:

  • Inflight requests will be interrupt rudely.

  • All sockets closed at the same time, and likely to dial server at the same time. Any occasional fail in the massive dial requests (high concurrency scenario) will make all sockets closed again, and repeat...(It happened in our production environment)

So I think sockets currently in use should closed after idle.

请注意,github.com/globalsign/mgo 具有向后兼容的 API,它基本上只是添加了一些新的东西/功能(除了修复和补丁),这意味着您应该能够只需更改导入路径,一切都应该正常工作,无需进一步更改。

关于多个应用服务器上的 MongoDB 连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52726305/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com