我使用C#驱动程序的2.4.3版连接到MongoDB版本3.4.4。我有一个3成员副本集(一个主要,一个次要,一个仲裁)。我已经在连接字符串中设置了写入问题。当主节点和辅助节点都在线时,我可以毫无问题地写入数据库。但是当我主要离线时我遇到了麻烦。我正在使用此代码:
string connectionString = "mongodb://USERNAME:PASSWORD@PRIMARY,SECONDARY/?replicaSet=MyReplicaSet&w=majority";
var client = new MongoClient(connectionString);
IMongoDatabase database = client.GetDatabase("test");
IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("people");
Console.WriteLine($"Number of documents: {collection.Count(FilterDefinition<BsonDocument>.Empty)}");
collection.InsertOne(new BsonDocument("name", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss")));
Console.WriteLine($"Number of documents: {collection.Count(FilterDefinition<BsonDocument>.Empty)}");
当我将主要离线时,辅助设备被选中,我仍然可以写入数据库(我可以在新选出的主数据库中看到数据库中的文档)。但是对collection.InsertOne的调用最终会抛出一个MongoDBConnectionException:
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.ReadBytes(Stream stream, Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBuffer()
--- End of inner exception stack trace ---
at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBuffer()
at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBuffer(Int32 responseTo, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveMessage(Int32 responseTo, IMessageEncoderSelector encoderSelector, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.AcquiredConnection.ReceiveMessage(Int32 responseTo, IMessageEncoderSelector encoderSelector, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.Execute(IConnection connection, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocol[TResult](IWireProtocol`1 protocol, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Servers.Server.ServerChannel.Command[TResult](DatabaseNamespace databaseNamespace, BsonDocument command, IElementNameValidator commandValidator, Func`1 responseHandling, Boolean slaveOk, IBsonSerializer`1 resultSerializer, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.BulkUnmixedWriteOperationBase.ExecuteProtocol(IChannelHandle channel, BsonDocument command, Func`1 responseHandling, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.BulkUnmixedWriteOperationBase.ExecuteBatch(IChannelHandle channel, BatchableSource`1 requestSource, Int32 originalIndex, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.BulkUnmixedWriteOperationBase.ExecuteBatches(IChannelHandle channel, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.BulkUnmixedWriteOperationBase.Execute(IChannelHandle channel, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.ExecuteBatch(IChannelHandle channel, Run run, Boolean isLast, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.Execute(IWriteBinding binding, CancellationToken cancellationToken)
at MongoDB.Driver.OperationExecutor.ExecuteWriteOperation[TResult](IWriteBinding binding, IWriteOperation`1 operation, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.ExecuteWriteOperation[TResult](IWriteOperation`1 operation, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.BulkWrite(IEnumerable`1 requests, BulkWriteOptions options, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionBase`1.InsertOne(TDocument document, InsertOneOptions options, CancellationToken cancellationToken)
at ReplicaSetTest.Program.Main(String[] args) in C:\Code\TFS\Published Register\Main\ReplicaSetTest\Program.cs:line 57
如果我没有将写作关注设置为大多数,那么我没有问题。我对MongoDB没有多少经验,所以我很难知道出了什么问题。任何人都可以建议我应该在哪里开始追查这个问题的原因吗?
谢谢,
大卫
答案 0 :(得分:2)
但是对collection.InsertOne的调用最终会抛出一个MongoDBConnectionException
首先,写入关注点占多数意味着客户端请求来自大多数副本集的确认(必须包括主节点)。因此,客户端应用程序将等待主节点和一个辅助节点的响应。有关此图表,请参阅Verify Write Operations to Replica Sets。
如果在确认等待期间写入主节点降级,则与主节点的客户端连接将丢失。辅助节点还会尝试清除任何已建立的连接,因为节点的状态已从SECONDARY更改为PRIMARY(另请参阅Replica Set Member States)。这将导致客户端丢失连接(流),因为在客户端期待回复但它永远不会到来。因此,你得到了:
System.IO.EndOfStreamException: Attempted to read past the end of the stream
您应该尝试捕获此异常,并根据写入的性质进行适当处理。
您可能也有兴趣知道在MongoDB v3.6中,有Retryable Writes来处理瞬态网络错误/副本集选举。
此外,我还注意到您的部署包含Primary with a Secondary and an Arbiter 而不是Primary with Two Secondaries。如果您只有2个数据承载节点,其中一个节点已关闭,则无法满足写入关注点。由于它要求副本集的多数接收写操作,但只有一个数据承载成员处于活动状态。请考虑将仲裁者转换为次要成员。