5
votes

I have a site that has been running perfectly fine for months, and now, all of a sudden, nothing works. The site is done with MVC 5 and is use MongoDB for storage.

My best guess is that the MongoDB host is messing around, and that's causing the problem, but...

What do I do to avoid this problem in the future? There has to be some setting I can change?

Here's the error and the stack trace:

QueryFailure flag was not master and slaveOk=false (response was { "$err" : "not master and slaveOk=false", "code" : 13435 }).

at MongoDB.Driver.Internal.MongoReplyMessage1.ReadHeaderFrom(BsonBuffer buffer) at MongoDB.Driver.Internal.MongoReplyMessage1.ReadFrom(BsonBuffer buffer) at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument](BsonBinaryReaderSettings readerSettings, IBsonSerializer serializer, IBsonSerializationOptions serializationOptions) at MongoDB.Driver.Operations.QueryOperation1.GetFirstBatch(IConnectionProvider connectionProvider) at MongoDB.Driver.Operations.QueryOperation1.Execute(IConnectionProvider connectionProvider) at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.OrderedEnumerable1.<GetEnumerator>d__0.MoveNext() at System.Linq.Enumerable.<TakeIterator>d__3a1.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at ScaleModellingCentral.Web.UI.Controllers.HomeController.Read() at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.b__32(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.b__1c() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult)

1
Did you solve it? Same issue for me on MongoHq.Justin

1 Answers

4
votes

It was a hosting issue that triggered the problem.

My primary server/shard had issues, so, as expected, the secondary became the new primary and then everything failed.

The quick fix was to postfix the connect string with "?slaveOk=true". That fixed reading. Writing still failed.

So the real issue here was that I had only entered the (initial) primary server in the connect string, so the (C#) driver didn't know there was two shards.

Instead if I had written the correct connect string, the site would have had a few failed requests, and then the driver would have switched to the new primary instead of go on using the failing, now secondary, shard.

        <add name="MongoDB" connectionString="mongodb://<username>:<password>@<server1>:<port1>,<server2>:<port2>/<dbname>" />