We have a mongodb replica set with 1 master and 2 slaves. All slaves can be become masters.
At one point the client, a Windows Service using .NET 4.0, stopped working. When I checked the logs, there was a large number of these exceptions:
MongoDB.Driver.MongoConnectionException: Unable to connect to a member of the replica set matching the read preference Primary\r\n at MongoDB.Driver.Internal.MultipleInstanceMongoServerProxy.ThrowConnectionException(ReadPreference readPreference) in c:\\projects\\mongo-csharp-driver\\Driver\\Internal
MultipleInstanceMongoServerProxy.cs:line 509\r\n at MongoDB.Driver.Internal.MultipleInstanceMongoServerProxy.Connect(TimeSpan timeout, ReadPreference readPreference) in c:\\projects\\mongo-csharp-driver\\Driver\\Internal
MultipleInstanceMongoServerProxy.cs:line 224\r\n at MongoDB.Driver.Internal.MultipleInstanceMongoServerProxy.ChooseServerInstance(ReadPreference readPreference) in c:\\projects\\mongo-csharp-driver\\Driver\\Internal
MultipleInstanceMongoServerProxy.cs:line 168\r\n at MongoDB.Driver.Internal.DiscoveringMongoServerProxy.ChooseServerInstance(ReadPreference readPreference) in c:\\projects\\mongo-csharp-driver\\Driver\\Internal
DiscoveringMongoServerProxy.cs:line 126\r\n at MongoDB.Driver.MongoServer.AcquireConnection(MongoDatabase database, ReadPreference readPreference) in c:\\projects\\mongo-csharp-driver\\Driver\\Core
MongoServer.cs:line 1051\r\n at MongoDB.Driver.MongoCursorEnumerator`1.AcquireConnection() in c:\\projects\\mongo-csharp-driver\\Driver\\Core
MongoCursorEnumerator.cs:line 299\r\n at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst() in c:\\projects\\mongo-csharp-driver\\Driver\\Core
MongoCursorEnumerator.cs:line 312\r\n at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext() in c:\\projects\\mongo-csharp-driver\\Driver\\Core
MongoCursorEnumerator.cs:line 230\r\n at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)\r\n at MongoDB.Driver.MongoCollection.FindOneAs[TDocument](IMongoQuery query) in c:\\projects\\mongo-csharp-driver\\Driver\\Core
MongoCollection.cs:line 570\r\n at MongoDB.Driver.MongoCollection.RunCommandAs(Type commandResultType, IMongoCommand command) in c:\\projects\\mongo-csharp-driver\\Driver\\Core
MongoCollection.cs:line 1658\r\n at MongoDB.Driver.MongoCollection.RunCommandAs(Type commandResultType, IMongoCommand command) in c:\\projects\\mongo-csharp-driver\\Driver\\Core
MongoCollection.cs:line 1680\r\n at MongoDB.Driver.MongoCollection.RunCommandAs[TCommandResult](IMongoCommand command) in c:\\projects\\mongo-csharp-driver\\Driver\\Core
MongoCollection.cs:line 1650\r\n at MongoDB.Driver.MongoCollection.FindAndModify(IMongoQuery query, IMongoSortBy sortBy, IMongoUpdate update, IMongoFields fields, Boolean returnNew, Boolean upsert) in c:\\projects\\mongo-csharp-driver\\Driver\\Core
MongoCollection.cs:line 461\r\n at MongoDB.Driver.MongoCollection.FindAndModify(IMongoQuery query, IMongoSortBy sortBy, IMongoUpdate update, Boolean returnNew, Boolean upsert) in c:\\projects\\mongo-csharp-driver\\Driver\\Core
MongoCollection.cs:line 440\r\n at MongoDB.Driver.MongoCollection.FindAndModify(IMongoQuery query, IMongoSortBy sortBy, IMongoUpdate update, Boolean returnNew) in c:\\projects\\mongo-csharp-driver\\Driver\\Core
MongoCollection.cs:line 421\r\n at MongoDB.Driver.MongoCollection.FindAndModify(IMongoQuery query, IMongoSortBy sortBy, IMongoUpdate update) in c:\\projects\\mongo-csharp-driver\\Driver\\Core
MongoCollection.cs:line 404\r\n
...
(Note that the actual read preference of the collection is ReadPreferenceMode.Secondary, but that has not caused any problems with FindAndModify until now)
We were not able to see anything wrong with the replica set itself, so we simply restarted the client. The exceptions immediately went away and the client continued to work perfectly fine after that.
I cannot determine what caused the client to begin throwing those exceptions. It might have been some temporary network failure. The real problem is that the driver was not able to recover from that failure by itself. I would expect the driver to be more robust and to be able to resume normal operation without the need for a restart.
Is there a code workaround for this issue? Would it help if we set the Read preference mode to SecodaryPreferred or to Primary?
- is duplicated by
-
CSHARP-754 Driver could not reconnect to the server after socket exception, had to recycle application pool.
- Closed
- is related to
-
CSHARP-678 An existing connection was forcibly closed by the remote host
- Closed