I've been testing out the official MongoDB C# Driver with a replica set of 3 instances. I've created a simple app which accesses the replica set in a loop.
My question is: Is it possible to make the C# driver to re-run the query automatically when I close the primary server, without it throwing the EndOfStreamException like it does now?
Here's my initialization code for the MongoServerSettings:
var settings = new MongoServerSettings()
{
ConnectionMode = ConnectionMode.ReplicaSet,
ReplicaSetName = "mongors",
ReadPreference = new ReadPreference(ReadPreferenceMode.PrimaryPreferred),
SafeMode = SafeMode.True,
DefaultCredentials = new MongoCredentials("user", "password"),
Servers = new[] { new MongoServerAddress("server.net", 27020),
new MongoServerAddress("server.net", 27019),
new MongoServerAddress("server.net", 27018)}
};
And here's the code where I query the server:
while (true)
{
var server = MongoServer.Create(settings);
var db = server.GetDatabase("db");
var collection = db.GetCollection<TaggedAction>("actions");
var query = Query.EQ("_id", id);
var entity = collection.FindOne(query);
Console.WriteLine(DateTime.Now +" " + entity.ActionName);
Thread.Sleep(2500);
}
If I shut down the primary server, the client throws the following exception:
System.IO.EndOfStreamException was unhandled
HResult=-2147024858
Message=Attempted to read past the end of the stream.
Source=MongoDB.Bson
StackTrace:
at MongoDB.Bson.IO.BsonBuffer.LoadFrom(Stream stream, Int32 count) in C:\work\rstam\mongo-csharp-driver\Bson\IO\BsonBuffer.cs: line 314
at MongoDB.Bson.IO.BsonBuffer.LoadFrom(Stream stream) in C:\work\rstam\mongo-csharp-driver\Bson\IO\BsonBuffer.cs: line 281
at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage(BsonBinaryReaderSettings readerSettings, IBsonSerializationOptions serializationOptions) in C:\work\rstam\mongo-csharp-driver\Driver\Internal\MongoConnection.cs: line 478
at MongoDB.Driver.MongoCursorEnumerator`1.GetReply(MongoConnection connection, MongoRequestMessage message) in C:\work\rstam\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs: line 296
at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst() in C:\work\rstam\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs: line 253
at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext() in C:\work\rstam\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs: line 141
at System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source)
at MongoDB.Driver.MongoCollection.FindOneAs(IMongoQuery query) in C:\work\rstam\mongo-csharp-driver\Driver\Core\MongoCollection.cs: line 557
at MongoDB.Driver.MongoCollection`1.FindOne(IMongoQuery query) in C:\work\rstam\mongo-csharp-driver\Driver\Core\MongoCollection.cs: line 1734
at ConsoleApplication16.Program.Main(String[] args) in Program.cs: line 53
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
If I just swallow this exception and continue with the loop, everything works. So it can sort out the issue and switch to an another server. But it would be great if the driver could automatically handle this so that in no point it throws the exception. Is it possible?
IAsyncCursor is equivalent to IEnumerator (the result of IEnumerable.GetEnumerator ) while IAsyncCursorSource is to IEnumerable . The difference is that these support async (and get a batch each iteration and not just a single item).
BsonDocument is the default type used for documents. It handles dynamic documents of any complexity. For instance, the document { a: 1, b: [{ c: 1 }] } can be built as follows: var doc = new BsonDocument { { "a", 1 }, { "b", new BsonArray { new BsonDocument("c", 1) }} };
In many cases, it would be impossible to fail over because the cursor that is executing only exists on the primary server. The secondaries don't know anything about it and therefore could not continue it.
In your case, you happen you know you want to continue, but it would presumptuous of us to take your needs and apply them to all situations. Where you want to just continue the loop, others may not.
Other than that point, some drivers do retry queries. The .NET driver does not because we feel we cannot always ascertain the correct behavior and therefore leave it up to the application to decide.
In the case of PrimaryPreferred, there is a reason that you want reads to come from the Primary -- because they are up-to-date. If we silently fall back to a secondary then, depending on how far back the secondary is, there is a chance that your query actually returns results from before the last successful query to the Primary. This is not a good experience and therefore we simply don't do it and recommend that you catch these errors and handle the retries yourself.
We are looking to get some of these errors wrapped in MongoDB specific exceptions so you don't have to guess at things like an EndOfStream exception (https://jira.mongodb.org/browse/CSHARP-474). In addition, if you would like to see this feature, please file a jira and we'll look into how we can do this in a predictable manner -- perhaps using a user supplied strategy for handling retries (IRetryStrategy or something).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With