Environment:
- host: Windows 7
- mongod: v2.2 (mongodb-win32-x86_64-2008plus-2.2.0)
- mongo setup: 3 mongod processes running on the same host in a replica set, 2 real and 1 arbiter
- csharp driver: v.1.6.0.4624
I create a MongoServer by providing a MongoServerSetings with the following properties:
- SafeMode = SafeMode.True
- Servers = a collecttion of all 3 mongods' MongoServerAddress-es
- ReadPreference = PrimaryPrefered
I have a function that asks the MongoServer for a collection (MongoCollection) of a database, first does a read on the collection and then attempts to do a write. The read succeeds but the write fails with the following stack trace (wrapped as JSON doc as Mongo in this case is backing a web service - please excuse the ugliness):
===
{"responseStatus":{"errorCode":"MongoSafeModeException","message":"Safemode detected an error 'not master'. (Response was { \"err\" : \"not master\", \"code\" : 10056, \"n\" : 0, \"lastOp\" : { \"$timestamp\" : NumberLong(0) }, \"connectionId\" : 1569, \"ok\" : 1.0 }).","stackTrace":"[ById: 10/25/2012 8:53:40 PM]:\n[REQUEST: {id:1}]MongoDB.Driver.MongoSafeModeException: Safemode detected an error 'not master'. (Response was { \"err\" : \"not master\", \"code\" : 10056, \"n\" : 0, \"lastOp\" : { \"$timestamp\" : NumberLong(0) }, \"connectionId\" : 1569, \"ok\" : 1.0 }).\r\n at MongoDB.Driver.Internal.MongoConnection.SendMessage(MongoRequestMessage message, SafeMode safeMode, String databaseName) in C:\\work\\rstam\\mongo-csharp-driver\\Driver\\Internal\\MongoConnection.cs:line 549\r\n at MongoDB.Driver.MongoCollection.Remove(IMongoQuery query, RemoveFlags flags, SafeMode safeMode) in C:\\work\\rstam\\mongo-csharp-driver\\Driver\\Core\\MongoCollection.cs:line 1312\r\n at MongoDB.Driver.MongoCollection.Remove(IMongoQuery query) in C:\\work\\rstam\\mongo-csharp-driver\\Driver\\Core\\MongoCollection.cs:line 1272
===
What's
Guessing here the collection when initially obtained was backed by a connection to the slave replica and that's why the read succeeded but the write did not. If that's the case how would one use the driver for such scenarios?
Moreover, going with my guess I changed the read preference to Primary (just Primary). Then even more weirdness happened. I got an exception even before the write was attempted, but when the function was trying to do the read:
===
{"responseStatus":{"errorCode":"MongoQueryException","message":"QueryFailure flag was not master and slaveOk=false (response was { \"$err\" : \"not master and slaveOk=false\", \"code\" : 13435 }).","stackTrace":"[ById: 10/25/2012 9:05:30 PM]:\n[REQUEST: {id:1}]MongoDB.Driver.MongoQueryException: QueryFailure flag was not master and slaveOk=false (response was { \"$err\" : \"not master and slaveOk=false\", \"code\" : 13435 }).\r\n at MongoDB.Driver.Internal.MongoReplyMessage`1.ReadFrom(BsonBuffer buffer, IBsonSerializationOptions serializationOptions) in C:\\work\\rstam\\mongo-csharp-driver\\Driver\\Internal\\MongoReplyMessage.cs:line 99\r\n at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument](BsonBinaryReaderSettings readerSettings, IBsonSerializationOptions serializationOptions) in C:\\work\\rstam\\mongo-csharp-driver\\Driver\\Internal\\MongoConnection.cs:line 475\r\n at MongoDB.Driver.MongoCursorEnumerator`1.GetReply(MongoConnection connection, MongoRequestMessage message) in C:\\work\\rstam\\mongo-csharp-driver\\Driver\\Core\\MongoCursorEnumerator.cs:line 296\r\n at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst() in C:\\work\\rstam\\mongo-csharp-driver\\Driver\\Core\\MongoCursorEnumerator.cs:line 253\r\n at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext() in C:\\work\\rstam\\mongo-csharp-driver\\Driver\\Core\\MongoCursorEnumerator.cs:line 141
===
I see the error in the mongod's log (this is the SECONDARY mongod's log):
===
Thu Oct 25 22:05:30 [conn1665] assertion 13435 not master and slaveOk=false ns:mdm.policies query:{ ids: "1" }
Thu Oct 25 22:05:30 [conn1665] problem detected during query over mdm.policies : { $err: "not master and slaveOk=false", code: 13435 }
===
FWIW I don't see the first error I discussed captured in one shape or another in the mongod's logs - I checked in the logs for all 3 of my mongod processes.
I assumed that setting the ReadPreference to Primary would let the driver know that all connections need to be asked of the primary mongod. I guess that's not what happened here for me?
Ideas/Suggestions/Recs would be greatly appreciated.
Thanks, -nik