MongoDB C#2.0 TimeoutException [英] MongoDB C# 2.0 TimeoutException
问题描述
我们最近已将Web应用程序升级到MongoDB C#Driver 2.0,并已部署到生产环境中.在一定负载下,应用程序运行良好.一旦生产服务器上的负载超过某个限制,应用程序的CPU立即下降到0,大约30秒后,多次记录此异常:
We've recently upgraded our web application to MongoDB C# Driver 2.0 and deployed to production. Below a certain load, the application runs fine. Once the load on the production server exceeds a certain limit, the CPU of the application instantly falls down to 0 and after about 30 seconds, this exception is logged several times:
System.TimeoutException message: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode = Primary, TagSets = System.Collections.Generic.List`1[MongoDB.Driver.TagSet] } }, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", Type : "Standalone", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/10.4.0.113:27017" }", EndPoint: "Unspecified/10.4.0.113:27017", State: "Disconnected", Type: "Unknown" }] }.
stack trace:
at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
at MongoDB.Driver.Core.Clusters.Cluster.<WaitForDescriptionChangedAsync>d__18.MoveNext()
--- End of stack trace
我们正在使用一个单例MongoClient对象,该对象的初始化方式如下:
We are using a singleton MongoClient object, which is initiated like this:
private static object _syncRoot = new object();
private static MongoClient _client;
private static IMongoDatabase _database;
private IMongoDatabase GetDatabase()
{
...
if (_client == null)
{
lock (_syncRoot)
{
if (_client == null)
{
_client = new MongoClient(
new MongoClientSettings
{
Server = new MongoServerAddress(host, port),
Credentials = new[] { credentials },
});
_database = _client.GetDatabase("proddb");
return _database;
}
}
}
return _database;
}
public IMongoCollection<T> GetCollection<T>(string name)
{
return GetDatabase().GetCollection<T>(name);
}
对数据库的典型调用如下:
A typical call to database looks like this:
public async Task<MongoItem> GetById(string id)
{
var collection = _connectionManager.GetCollection<MongoItem>("items");
var fdb = new FilterDefinitionBuilder<MongoItem>();
var f = fdb.Eq(mi => mi.Id, id);
return await collection.Find(f).FirstOrDefaultAsync();
}
我们如何发现原因并解决此问题?
How can we discover the reason and fix this issue?
推荐答案
此帖子可能有帮助:
我知道了. 这张JIRA票证包含详细信息.
有效地,我们对连接到
独立服务器并直接连接到副本集成员,
后者相对不常见.不幸的是,MongoLab的
单节点设置实际上是一个单节点副本集,这是
使我们不信任它.您可以通过附加来解决此问题
?connect=replicaSet
到您的连接字符串.它将迫使
驱动程序进入副本集设置模式,一切都将正常工作.
Effectively, we've made a distinction between connecting to a
standalone server and connecting directly to a replica set member,
where the latter is relatively uncommon. Unfortunately, MongoLab's
Single-Node settings are actually a single node replica set and this
causes us to not trust it. You can fix this by appending
?connect=replicaSet
to your connection string. It will force the
driver to move into replica set mode and all will work.
鉴于此,我们将重新考虑 CSHARP-1160 .太谢谢了
大量的报告,让我知道是否将?connect=replicaSet
附加到
您的连接字符串不起作用.
We are going to re-consider CSHARP-1160 in light of this. Thanks so
much for reporting and let me know if appending ?connect=replicaSet
to
your connection string doesn't work.
这篇关于MongoDB C#2.0 TimeoutException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!