-->

MongoDB的C#2.0 TimeoutException异常MongoDB的C#2.0 Time

2019-05-12 10:38发布

我们最近升级我们的Web应用程序MongoDB的C#2.0驱动程序并将其部署到生产环境。 低于一定的负载,应用程序运行正常。 一旦生产服务器上的负载超过一定限度时,应用程序的CPU瞬间下降到0,约30秒后,这个记录异常几次:

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对象,它开始是这样的:

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);
}

到数据库中的典型调用如下:

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();
}

我们怎样才能找出原因和解决这个问题?

Answer 1:

这个职位可能有帮助:

我想到了。 这JIRA票有细节。

实际上,我们已经取得了连接到一个独立的服务器,直接连接到副本集成员,后者是比较少见的区别。 不幸的是,MongoLab的单节点的设置实际上是一个节点副本集,这使我们不信任它。 您可以通过附加解决这个问题?connect=replicaSet到您的连接字符串。 这将迫使司机进入副本集模式,一切都将正常工作。

我们要重新考虑CSHARP-1160在这个光。 感谢这么多的报告,让我知道,如果追加?connect=replicaSet到连接字符串不工作。



Answer 2:

I was experiencing the same issue using driver v2.2.4. After upgrading to v2.3.0, the issue seems to have been resolved



Answer 3:

这个问题涉及到CSHARP-1435 , CSHARP-1515和CSHARP-1538错误报告,并且极有可能这已被固定在最近的C#MongoDB的驱动程序。

这个问题可能与读取单批返回的文件数超过一个契合。 资源

所以,可能的解决方案是:

  • 确保您的MongoDB是启动和运行。 资源
  • 检查MongoDB的日志,任何其他细节。
  • 如果要连接到的组mongod过程(参见: 复制 ),加上?connect=replicaSet你的连接字符串。 例如:

     mongodb://db1.example.net:27017,db2.example.net:2500/?replicaSet=test&connectTimeoutMS=300000 

    例如ConnectionStrings.config文件:

     <connectionStrings> <add name="MongoDB" connectionString="mongodb://10.0.80.231:27017,10.0.108.31:27017/?replicaSet=groupname&amp;connectTimeoutMS=600000&amp;socketTimeoutMS=600000" /> <add name="RedisCache" connectionString="www-redis.foo.cache.amazonaws.com:6379" /> <add name="SqlConnection" connectionString="server=api.foo.eu-west-1.rds.amazonaws.com;database=foo;uid=sqlvpc;pwd=somepass;" providerName="System.Data.SqlClient" /> </connectionStrings> 

    相关阅读: CSHARP-1160 , 如何为包括连接字符串符号?

    如果以上都不行,请检查: C#的MongoDB驱动程序忽略超时选项 。

  • 尝试升级MongoDB.Driver按照上面的错误报告。

  • 尝试增加连接插座超时。

    无论是通过追加?connectTimeoutMS=60000&socketTimeoutMS=60000到您的连接字符串。 请参阅: MongoDB的连接字符串选项 。

    在你的代码或者改变设置(例如connecttimeoutmaxpoolsizewaitQueueSizewaitQueueTimeout )。 看到这里例子 。

  • 请检查连接字符串,无论您是包括数据库正常。 资源
  • 在快速调用的MongoDB的情况下,增加每个查询之间的延迟。 资源

也请检查MongoDB的兼容性MongoDB的C#/。NET的驱动程序。 :



Answer 4:

我有,当我使用MongoLab免费的(2.6版本),沙箱和超时问题就走了,当我开始使用付费集群同样的问题。

我要说,我认为这个问题是,只有MongoDB的3.0或更高版本支持(因为我发现了一些文档说的多,我发誓,我经由MongoLab 3.0升级过程中去),但是当我去寻找文档,它现在说2.6的支持和我的付出MongoLab DB还是说,这是2.6.9版本。

我想我必须要疯了,但至少我的代码现在工作!



文章来源: MongoDB C# 2.0 TimeoutException