尝试写入 Mongo 副本集时导致超时的原因是什么?
What is causing a timeout when trying to write to a Mongo replica set?
我正在 运行 编写一个 C# 脚本来对分片集群执行写操作。
每当我尝试对数据库执行写操作时,都会收到以
开头的错误
使用 CompositeServerSelector 选择服务器 30000 毫秒后发生超时
有谁知道我需要做什么才能成功写作?该集群是一个 3 节点副本集,具有一个主节点和两个辅助节点。
详情如下:
我正在使用 Nuget 包:Mongo.Db.Core、Mongodb.Core.Driver 和 MongoDb.Bson,所有版本号均为 2.11.2。如果我在控制台中 运行 db.version() returns 4.2.9
我准备了如下所示的连接字符串:
MongoUrlBuilder bldr = new MongoUrlBuilder();
List<MongoServerAddress> servers = new List<MongoServerAddress>();
servers.Add(new MongoServerAddress("the-domain0.azure.mongodb.net", 27017));
servers.Add(new MongoServerAddress("the-domain1.azure.mongodb.net", 27017));
servers.Add(new MongoServerAddress("the-domain2.azure.mongodb.net", 27017));
bldr.Username = "the_user";
bldr.Password = "the_password";
bldr.Servers = servers;
bldr.DatabaseName = "the_db_name";
bldr.ConnectionMode = ConnectionMode.ReplicaSet;
_mongoConnectionString = bldr.ToMongoUrl().ToString();
然后我将它传递给客户端构造函数,如下所示:
var client = new MongoClient(_mongoConnectionString);
IMongoDatabase database = client.GetDatabase("the_db_name");
_the_collection_name = database.GetCollection<CollType>(_collname);
一切运行都很好,直到我需要执行此语句(更新插入)
var updateResult = _the_collection_name.UpdateOne(filter, update, new UpdateOptions { IsUpsert = true });
错误的主要方面如下:
System.TimeoutException: 'A timeout occured after 30000ms selecting a
server using CompositeServerSelector{ Selectors =
MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector,
LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000
} }. Client view of cluster state is { ClusterId : "1", ConnectionMode
: "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers :
[{ ServerId: "{ ClusterId : 1, EndPoint :
"Unspecified/the_domain.azure.mongodb.net:27017"
}", EndPoint:
"Unspecified/the_domain.azure.mongodb.net:27017",
ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: ,
TopologyVersion: , Type: "Unknown", HeartbeatException:
"MongoDB.Driver.MongoConnectionException: An exception occurred while
opening a connection to the server.
这是完整的堆栈跟踪:
System.TimeoutException: 'A timeout occured after 30000ms selecting a
server using CompositeServerSelector{ Selectors =
MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector,
LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000
} }. Client view of cluster state is { ClusterId : "1", ConnectionMode
: "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers :
[{ ServerId: "{ ClusterId : 1, EndPoint :
"Unspecified/the-domain0.azure.mongodb.net:27017"
}", EndPoint:
"Unspecified/the-domain0.azure.mongodb.net:27017",
ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: ,
TopologyVersion: , Type: "Unknown", HeartbeatException:
"MongoDB.Driver.MongoConnectionException: An exception occurred while
opening a connection to the server. --->
MongoDB.Driver.MongoConnectionException: An exception occurred while
receiving a message from the server. --->
System.IO.EndOfStreamException: Attempted to read past the end of the
stream. at
MongoDB.Driver.Core.Misc.StreamExtensionMethods.ReadBytesAsync(Stream
stream, Byte[] buffer, Int32 offset, Int32 count, TimeSpan timeout,
CancellationToken cancellationToken) at
MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBufferAsync(CancellationToken
cancellationToken) --- End of inner exception stack trace --- at
MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveMessageAsync(Int32
responseTo, IMessageEncoderSelector encoderSelector,
MessageEncoderSettings messageEncoderSettings, CancellationToken
cancellationToken) at
MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.IsMasterHelper.GetResultAsync(IConnection connection, CommandWireProtocol
1 isMasterProtocol, CancellationToken
cancellationToken) at
MongoDB.Driver.Core.Connections.ConnectionInitializer.InitializeConnectionAsync(IConnection
connection, CancellationToken cancellationToken) at
MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken
cancellationToken) --- End of inner exception stack trace --- at
MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken
cancellationToken) at
MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnectionAsync(CancellationToken
cancellationToken) at
MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken
cancellationToken)", LastHeartbeatTimestamp:
"2020-09-11T18:25:25.7998117Z", LastUpdateTimestamp:
"2020-09-11T18:25:25.7998118Z" }, { ServerId: "{ ClusterId : 1,
EndPoint :
"Unspecified/the-domain1.azure.mongodb.net:27017"
}", EndPoint:
"Unspecified/the-domain1.azure.mongodb.net:27017",
ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: ,
TopologyVersion: , Type: "Unknown", HeartbeatException:
"MongoDB.Driver.MongoConnectionException: An exception occurred while
opening a connection to the server. --->
MongoDB.Driver.MongoConnectionException: An exception occurred while
receiving a message from the server. --->
System.IO.EndOfStreamException: Attempted to read past the end of the
stream. at
MongoDB.Driver.Core.Misc.StreamExtensionMethods.ReadBytesAsync(Stream
stream, Byte[] buffer, Int32 offset, Int32 count, TimeSpan timeout,
CancellationToken cancellationToken) at
MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBufferAsync(CancellationToken
cancellationToken) --- End of inner exception stack trace --- at
MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveMessageAsync(Int32
responseTo, IMessageEncoderSelector encoderSelector,
MessageEncoderSettings messageEncoderSettings, CancellationToken
cancellationToken) at
MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.IsMasterHelper.GetResultAsync(IConnection connection, CommandWireProtocol
1 isMasterProtocol, CancellationToken
cancellationToken) at
MongoDB.Driver.Core.Connections.ConnectionInitializer.InitializeConnectionAsync(IConnection
connection, CancellationToken cancellationToken) at
MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken
cancellationToken) --- End of inner exception stack trace --- at
MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken
cancellationToken) at
MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnectionAsync(CancellationToken
cancellationToken) at
MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken
cancellationToken)", LastHeartbeatTimestamp:
"2020-09-11T18:25:25.7920423Z", LastUpdateTimestamp:
"2020-09-11T18:25:25.7920425Z" }, { ServerId: "{ ClusterId : 1,
EndPoint :
"Unspecified/the-domain2.azure.mongodb.net:27017"
}", EndPoint:
"Unspecified/the-domain2.azure.mongodb.net:27017",
ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: ,
TopologyVersion: , Type: "Unknown", HeartbeatException:
"MongoDB.Driver.MongoConnectionException: An exception occurred while
opening a connection to the server. --->
MongoDB.Driver.MongoConnectionException: An exception occurred while
receiving a message from the server. --->
System.IO.EndOfStreamException: Attempted to read past the end of the
stream. at
MongoDB.Driver.Core.Misc.StreamExtensionMethods.ReadBytesAsync(Stream
stream, Byte[] buffer, Int32 offset, Int32 count, TimeSpan timeout,
CancellationToken cancellationToken) at
MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBufferAsync(CancellationToken
cancellationToken) --- End of inner exception stack trace --- at
MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveMessageAsync(Int32
responseTo, IMessageEncoderSelector encoderSelector,
MessageEncoderSettings messageEncoderSettings, CancellationToken
cancellationToken) at
MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.IsMasterHelper.GetResultAsync(IConnection connection, CommandWireProtocol
1 isMasterProtocol, CancellationToken
cancellationToken) at
MongoDB.Driver.Core.Connections.ConnectionInitializer.InitializeConnectionAsync(IConnection
connection, CancellationToken cancellationToken) at
MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken
cancellationToken) --- End of inner exception stack trace --- at
MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken
cancellationToken) at
MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnectionAsync(CancellationToken
cancellationToken) at
MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken
cancellationToken)", LastHeartbeatTimestamp:
"2020-09-11T18:25:25.5936678Z", LastUpdateTimestamp:
"2020-09-11T18:25:25.5936680Z" }] }.'
您应该添加 UseTls 并更改 DatabaseName,如下所示:
bldr.DatabaseName = "admin";
bldr.UseTls = true;
我正在 运行 编写一个 C# 脚本来对分片集群执行写操作。 每当我尝试对数据库执行写操作时,都会收到以
开头的错误使用 CompositeServerSelector 选择服务器 30000 毫秒后发生超时
有谁知道我需要做什么才能成功写作?该集群是一个 3 节点副本集,具有一个主节点和两个辅助节点。
详情如下: 我正在使用 Nuget 包:Mongo.Db.Core、Mongodb.Core.Driver 和 MongoDb.Bson,所有版本号均为 2.11.2。如果我在控制台中 运行 db.version() returns 4.2.9
我准备了如下所示的连接字符串:
MongoUrlBuilder bldr = new MongoUrlBuilder();
List<MongoServerAddress> servers = new List<MongoServerAddress>();
servers.Add(new MongoServerAddress("the-domain0.azure.mongodb.net", 27017));
servers.Add(new MongoServerAddress("the-domain1.azure.mongodb.net", 27017));
servers.Add(new MongoServerAddress("the-domain2.azure.mongodb.net", 27017));
bldr.Username = "the_user";
bldr.Password = "the_password";
bldr.Servers = servers;
bldr.DatabaseName = "the_db_name";
bldr.ConnectionMode = ConnectionMode.ReplicaSet;
_mongoConnectionString = bldr.ToMongoUrl().ToString();
然后我将它传递给客户端构造函数,如下所示:
var client = new MongoClient(_mongoConnectionString);
IMongoDatabase database = client.GetDatabase("the_db_name");
_the_collection_name = database.GetCollection<CollType>(_collname);
一切运行都很好,直到我需要执行此语句(更新插入)
var updateResult = _the_collection_name.UpdateOne(filter, update, new UpdateOptions { IsUpsert = true });
错误的主要方面如下:
System.TimeoutException: 'A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/the_domain.azure.mongodb.net:27017" }", EndPoint: "Unspecified/the_domain.azure.mongodb.net:27017", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
这是完整的堆栈跟踪:
System.TimeoutException: 'A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/the-domain0.azure.mongodb.net:27017" }", EndPoint: "Unspecified/the-domain0.azure.mongodb.net:27017", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> MongoDB.Driver.MongoConnectionException: An exception occurred while receiving a message from the server. ---> System.IO.EndOfStreamException: Attempted to read past the end of the stream. at MongoDB.Driver.Core.Misc.StreamExtensionMethods.ReadBytesAsync(Stream stream, Byte[] buffer, Int32 offset, Int32 count, TimeSpan timeout, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBufferAsync(CancellationToken cancellationToken) --- End of inner exception stack trace --- at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveMessageAsync(Int32 responseTo, IMessageEncoderSelector encoderSelector, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken) at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol
1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.IsMasterHelper.GetResultAsync(IConnection connection, CommandWireProtocol
1 isMasterProtocol, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.ConnectionInitializer.InitializeConnectionAsync(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken) --- End of inner exception stack trace --- at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken) at MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnectionAsync(CancellationToken cancellationToken) at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)", LastHeartbeatTimestamp: "2020-09-11T18:25:25.7998117Z", LastUpdateTimestamp: "2020-09-11T18:25:25.7998118Z" }, { ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/the-domain1.azure.mongodb.net:27017" }", EndPoint: "Unspecified/the-domain1.azure.mongodb.net:27017", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> MongoDB.Driver.MongoConnectionException: An exception occurred while receiving a message from the server. ---> System.IO.EndOfStreamException: Attempted to read past the end of the stream. at MongoDB.Driver.Core.Misc.StreamExtensionMethods.ReadBytesAsync(Stream stream, Byte[] buffer, Int32 offset, Int32 count, TimeSpan timeout, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBufferAsync(CancellationToken cancellationToken) --- End of inner exception stack trace --- at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveMessageAsync(Int32 responseTo, IMessageEncoderSelector encoderSelector, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken) at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.IsMasterHelper.GetResultAsync(IConnection connection, CommandWireProtocol
1 isMasterProtocol, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.ConnectionInitializer.InitializeConnectionAsync(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken) --- End of inner exception stack trace --- at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken) at MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnectionAsync(CancellationToken cancellationToken) at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)", LastHeartbeatTimestamp: "2020-09-11T18:25:25.7920423Z", LastUpdateTimestamp: "2020-09-11T18:25:25.7920425Z" }, { ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/the-domain2.azure.mongodb.net:27017" }", EndPoint: "Unspecified/the-domain2.azure.mongodb.net:27017", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> MongoDB.Driver.MongoConnectionException: An exception occurred while receiving a message from the server. ---> System.IO.EndOfStreamException: Attempted to read past the end of the stream. at MongoDB.Driver.Core.Misc.StreamExtensionMethods.ReadBytesAsync(Stream stream, Byte[] buffer, Int32 offset, Int32 count, TimeSpan timeout, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBufferAsync(CancellationToken cancellationToken) --- End of inner exception stack trace --- at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveMessageAsync(Int32 responseTo, IMessageEncoderSelector encoderSelector, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken) at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.IsMasterHelper.GetResultAsync(IConnection connection, CommandWireProtocol
1 isMasterProtocol, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.ConnectionInitializer.InitializeConnectionAsync(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken) --- End of inner exception stack trace --- at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken) at MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnectionAsync(CancellationToken cancellationToken) at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)", LastHeartbeatTimestamp: "2020-09-11T18:25:25.5936678Z", LastUpdateTimestamp: "2020-09-11T18:25:25.5936680Z" }] }.'
您应该添加 UseTls 并更改 DatabaseName,如下所示:
bldr.DatabaseName = "admin";
bldr.UseTls = true;