[CSHARP-553] Shardset: Unrecognized command: $readPreference, code: 13390 Created: 03/Sep/12  Updated: 20/Mar/14  Resolved: 11/Oct/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.6
Fix Version/s: 1.6.1

Type: Bug Priority: Major - P3
Reporter: Kenny Inggs Assignee: Craig Wilson
Resolution: Done Votes: 1
Labels: driver, replicaset, sharding
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Win 8 Client, connecting to either win8 MongoDB 2.2 shardset or Ubuntu 2.2 shardset


Issue Links:
Related
is related to CSHARP-600 Unrecognized command: $query Closed

 Description   

Connecting to a 2.2 shard-set using the 1.6 driver results in the following exception:

MongoDB.Driver.MongoQueryException was unhandled
  HResult=-2146233088
  Message=QueryFailure flag was unrecognized command: $readPreference (response was {"$err" : "unrecognized command: $readPreference", "code" : 13390 }).
  Source=MongoDB.Driver
  StackTrace:
       at MongoDB.Driver.Internal.MongoReplyMessage`1.ReadFrom(BsonBuffer buffer, IBsonSerializationOptions serializationOptions) in c:\mongodb-mongo-csharp-driver-v1.6.0.4624-4-g10826e1\mongodb-mongo-csharp-driver-10826e1\Driver\Internal\MongoReplyMessage.cs:line 99
       at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument](BsonBinaryReaderSettings readerSettings, IBsonSerializationOptions serializationOptions) in c:\mongodb-mongo-csharp-driver-v1.6.0.4624-4-g10826e1\mongodb-mongo-csharp-driver-10826e1\Driver\Internal\MongoConnection.cs:line 475
       at MongoDB.Driver.MongoCursorEnumerator`1.GetReply(MongoConnection connection, MongoRequestMessage message) in c:\mongodb-mongo-csharp-driver-v1.6.0.4624-4-g10826e1\mongodb-mongo-csharp-driver-10826e1\Driver\Core\MongoCursorEnumerator.cs:line 296
       at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst() in c:\mongodb-mongo-csharp-driver-v1.6.0.4624-4-g10826e1\mongodb-mongo-csharp-driver-10826e1\Driver\Core\MongoCursorEnumerator.cs:line 253
       at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext() in c:\mongodb-mongo-csharp-driver-v1.6.0.4624-4-g10826e1\mongodb-mongo-csharp-driver-10826e1\Driver\Core\MongoCursorEnumerator.cs:line 141
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
       at MongoDB.Driver.MongoCollection.FindOneAs[TDocument](IMongoQuery query) in c:\mongodb-mongo-csharp-driver-v1.6.0.4624-4-g10826e1\mongodb-mongo-csharp-driver-10826e1\Driver\Core\MongoCollection.cs:line 557
       at MongoDB.Driver.MongoCollection`1.FindOne(IMongoQuery query) in c:\mongodb-mongo-csharp-driver-v1.6.0.4624-4-g10826e1\mongodb-mongo-csharp-driver-10826e1\Driver\Core\MongoCollection.cs:line 1734
       at MongoDB.Driver.MongoDatabase.RunCommandAs(Type commandResultType, IMongoCommand command) in c:\mongodb-mongo-csharp-driver-v1.6.0.4624-4-g10826e1\mongodb-mongo-csharp-driver-10826e1\Driver\Core\MongoDatabase.cs:line 973
       at MongoDB.Driver.MongoDatabase.RunCommandAs[TCommandResult](IMongoCommand command) in c:\mongodb-mongo-csharp-driver-v1.6.0.4624-4-g10826e1\mongodb-mongo-csharp-driver-10826e1\Driver\Core\MongoDatabase.cs:line 950
       at MongoDB.Driver.MongoDatabase.RunCommand(IMongoCommand command) in c:\mongodb-mongo-csharp-driver-v1.6.0.4624-4-g10826e1\mongodb-mongo-csharp-driver-10826e1\Driver\Core\MongoDatabase.cs:line 928
       at MongoDB.Driver.MongoCollection.Count(IMongoQuery query) in c:\mongodb-mongo-csharp-driver-v1.6.0.4624-4-g10826e1\mongodb-mongo-csharp-driver-10826e1\Driver\Core\MongoCollection.cs:line 158
       at MongoDB.Driver.MongoCollection.Count() in c:\mongodb-mongo-csharp-driver-v1.6.0.4624-4-g10826e1\mongodb-mongo-csharp-driver-10826e1\Driver\Core\MongoCollection.cs:line 143
       at MongoTest.Program.Main(String[] args) in c:\Users\kennyi\Documents\Visual Studio 2012\Projects\MongoTest\MongoTest\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       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: 

To repro, start a simple sharded replicaset:

mongod.exe --shardsvr --replSet rs_a --port 27020 --dbpath c:\data\rs_a\n1
mongod.exe --shardsvr --replSet rs_a --port 27021 --dbpath c:\data\rs_a\n2
mongod.exe --shardsvr --replSet rs_a --port 27022 --dbpath c:\data\rs_a\n3
 
mongod.exe --configsvr --port 27030 --dbpath c:\data\config\c1
mongod.exe --configsvr --port 27031 --dbpath c:\data\config\c2
mongod.exe --configsvr --port 27032 --dbpath c:\data\config\c3
 
mongos.exe --configdb my-machine:27030,my-machine:27031,my-machine:27032
 
cfg = {
    _id : "rs_a",
    members : [
        {_id : 0, host : "my-machine:27020", priority : 1},
        {_id : 1, host : "my-machine:27021", priority : 1},
        {_id : 2, host : "my-machine:27022", priority : 0}
    ]}
rs.initiate(cfg)
db.adminCommand( { addShard : "rs_a/my-machine:27020,my-machine:27021,my-machine:27022" } )

And call with the following code:

            var server = MongoServer.Create("mongodb://my-machine");
            var db = server.GetDatabase("test");
            Console.WriteLine("Found {0} transactions", db.GetCollection("test").Count());

However, if I connect to the same shard-set with the same code, but using version 1.4.2 of the driver, it works fine



 Comments   
Comment by Craig Wilson [ 11/Oct/12 ]

Created new issue, CSHARP-600, for Van Nguyen's secondary issue above.

Comment by Zarian Waheed [ 28/Sep/12 ]

Is there any work around for this? We also faced the same issue while tring to connect to the shard server through mongos. Server 2.2. C# driver 1.6.

MongoDB.Driver.MongoQueryException: QueryFailure flag was unrecognized command: $readPreference (response was

{ "$err" : "unrecognized command: $readPreference", "code" : 13390 }

).
at MongoDB.Driver.Internal.MongoReplyMessage`1.ReadFrom(BsonBuffer buffer, IBsonSerializationOptions serializationOptions)
at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument](BsonBinaryReaderSettings readerSettings, IBsonSerializationOptions serializationOptions)
at MongoDB.Driver.MongoCursorEnumerator`1.GetReply(MongoConnection connection, MongoRequestMessage message)
at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst()
at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at MongoDB.Driver.MongoDatabase.RunCommandAs(Type commandResultType, IMongoCommand command)
at MongoDB.Driver.MongoDatabase.RunCommandAs[TCommandResult](IMongoCommand command)
at MongoDB.Driver.MongoCollection.FindAndModify(IMongoQuery query, IMongoSortBy sortBy, IMongoUpdate update, IMongoFields fields, Boolean returnNew, Boolean upsert)
at MongoDB.Driver.MongoCollection.FindAndModify(IMongoQuery query, IMongoSortBy sortBy, IMongoUpdate update, Boolean returnNew, Boolean upsert)
at MongoDB.Driver.MongoCollection.FindAndModify(IMongoQuery query, IMongoSortBy sortBy, IMongoUpdate update, Boolean returnNew)

Comment by Van Nguyen [ 27/Sep/12 ]

@Craig Sorry for confusion. We're running replica sets.
Thanks

Comment by Craig Wilson [ 27/Sep/12 ]

@Van: could you clarify what you mean by 1 master set and 1 slave set? Are you running master/slaves or replica sets? I'm attempting to reproduce this issue and want to set it up identically to yours.

Comment by Michael Ballou [ 27/Sep/12 ]

I also had the same issue with the 1.6.0 driver on a shard I had setup. The latest source fixed the error for me.

Comment by Van Nguyen [ 20/Sep/12 ]

Thanks for the response

Error message

QueryFailure flag was unrecognized command: $query (response was { "$err" : "unrecognized command: $query", "code" : 13390 }).

Stacktrace

   at MongoDB.Driver.Internal.MongoReplyMessage`1.ReadFrom(BsonBuffer buffer, IBsonSerializationOptions serializationOptions) in D:\Temp\mongodb-code\mongodb-mongo-csharp-driver-98a0129\Driver\Internal\MongoReplyMessage.cs:line 99
   at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument](BsonBinaryReaderSettings readerSettings, IBsonSerializationOptions serializationOptions) in D:\Temp\mongodb-code\mongodb-mongo-csharp-driver-98a0129\Driver\Internal\MongoConnection.cs:line 475
   at MongoDB.Driver.MongoCursorEnumerator`1.GetReply(MongoConnection connection, MongoRequestMessage message) in D:\Temp\mongodb-code\mongodb-mongo-csharp-driver-98a0129\Driver\Core\MongoCursorEnumerator.cs:line 296
   at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst() in D:\Temp\mongodb-code\mongodb-mongo-csharp-driver-98a0129\Driver\Core\MongoCursorEnumerator.cs:line 253
   at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext() in D:\Temp\mongodb-code\mongodb-mongo-csharp-driver-98a0129\Driver\Core\MongoCursorEnumerator.cs:line 141
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   at MongoDB.Driver.MongoCollection.FindOneAs[TDocument](IMongoQuery query) in D:\Temp\mongodb-code\mongodb-mongo-csharp-driver-98a0129\Driver\Core\MongoCollection.cs:line 557
   at MongoDB.Driver.MongoCollection`1.FindOne(IMongoQuery query) in D:\Temp\mongodb-code\mongodb-mongo-csharp-driver-98a0129\Driver\Core\MongoCollection.cs:line 1734
   at MongoDB.Driver.MongoDatabase.RunCommandAs(Type commandResultType, IMongoCommand command) in D:\Temp\mongodb-code\mongodb-mongo-csharp-driver-98a0129\Driver\Core\MongoDatabase.cs:line 973
   at MongoDB.Driver.MongoDatabase.RunCommandAs[TCommandResult](IMongoCommand command) in D:\Temp\mongodb-code\mongodb-mongo-csharp-driver-98a0129\Driver\Core\MongoDatabase.cs:line 950
   at MongoDB.Driver.MongoCollection.Aggregate(IEnumerable`1 operations) in D:\Temp\mongodb-code\mongodb-mongo-csharp-driver-98a0129\Driver\Core\MongoCollection.cs:line 124
   at MongoDB.Driver.MongoCollection.Aggregate(BsonDocument[] operations) in D:\Temp\mongodb-code\mongodb-mongo-csharp-driver-98a0129\Driver\Core\MongoCollection.cs:line 134
   at TopSecretProject;).Service.Internal.AggregateFramework.BaseAggregateFrameworkMongoMentionService.GetAggregateResult(MentionAggregateQuery query, MongoField groupingKey, IDictionary`2 includeAggregateFields, BsonDocument[]& commands, Boolean isTimeSeries) in D:\Projects\TopSecretProject;)\Service\Internal\AggregateFramework\BaseAggregateFrameworkMongoMentionService.cs:line 50
   at TopSecretProject;).Service.Internal.AggregateFramework.AggregateFrameworkMongoMentionService.GetTopItemResults(MentionAggregateQuery query, IDictionary`2 includeAggregateFields) in D:\Projects\TopSecretProject;)\Service\Internal\AggregateFramework\AggregateFrameworkMongoMentionService.cs:line 22

Environment:

MongoDB.Driver: C# 1.6.1.20900
MongoDB.Bson: C# 1.6.1.20899
Mongos: 2.07
- 1 master set (3 shards) 2.07
- 1 slave set (3 shards) 2.2
connectionString: mongodb://mongos-version-2.0.7-ip:port/?readPreference=secondaryPreferred

We could not upgrade master & mongos to 2.2 atm so we have to try this approach as it's mentioned in the doc.
However, if I setup another mongos 2.2, there is not any error but the aggregate result is always empty. I tried to run the same aggregate commands using script against every single shard then it's good. Anyway, this could be another issue.

Cheers

Comment by Robert Stam [ 20/Sep/12 ]

Can you provide a full stack trace and a little more information about what you were trying to do when you got this exception?

We did not see this exception when we tested the fix, but perhaps you are doing something that is not covered by our unit tests. In any case, a little more information should help us reproduce it.

Thanks for trying out the fix. Sorry you ran into a problem.

Comment by Van Nguyen [ 20/Sep/12 ]

Not sure if it's fixed.
I tried to compile the driver from the source code which supposes to have this fix.
I'm trying to connect to mongos version 2.07, there is only 1 slave set which are at version 2.2
connection string: http://mongos-ip/?readPreference=secondaryPreferred

Error:

QueryFailure flag was unrecognized command: $query (response was

{ "$err" : "unrecognized command: $query", "code" : 13390 }

).

The error was below when using mongo client 1.6
Unrecognized command: $readPreference, code: 13390

Comment by auto [ 05/Sep/12 ]

Author:

{u'date': u'2012-09-03T18:42:39-07:00', u'email': u'robert@10gen.com', u'name': u'rstam'}

Message: CSHARP-553: fix unit tests that weren't passing when connected to mongos (mostly by skipping them, as they test features that mongos doesn't support or supports in a complicated way that doesn't match the simple test).
Branch: x2.0
https://github.com/mongodb/mongo-csharp-driver/commit/caa0e74e06119b33c75d5eaa6d589681c0d1490f

Comment by auto [ 05/Sep/12 ]

Author:

{u'date': u'2012-09-03T17:50:00-07:00', u'email': u'robert@10gen.com', u'name': u'rstam'}

Message: CSHARP-553: change the order of $readPreference and $query from the documented order to the order that actually works.
Branch: x2.0
https://github.com/mongodb/mongo-csharp-driver/commit/ba1f6e5bd6c306fbe94b2297a168b2fc7b65e705

Comment by auto [ 04/Sep/12 ]

Author:

{u'date': u'2012-09-03T18:42:39-07:00', u'email': u'robert@10gen.com', u'name': u'rstam'}

Message: CSHARP-553: fix unit tests that weren't passing when connected to mongos (mostly by skipping them, as they test features that mongos doesn't support or supports in a complicated way that doesn't match the simple test).
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/aa6fc73e5defbe027b60996e9960206046cf6138

Comment by auto [ 04/Sep/12 ]

Author:

{u'date': u'2012-09-03T17:50:00-07:00', u'email': u'robert@10gen.com', u'name': u'rstam'}

Message: CSHARP-553: change the order of $readPreference and $query from the documented order to the order that actually works.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/415552d3dfc0a83552242be0fe48458aa25600fb

Comment by Kenny Inggs [ 04/Sep/12 ]

Thanks Robert. We'll use 1.5 in the meantime.

Comment by Robert Stam [ 04/Sep/12 ]

In code review.

Comment by Robert Stam [ 04/Sep/12 ]

I can reproduce this. Apparently there is an error in the documentation describing how drivers should forward a read preference to mongos, and this was not caught during testing.

You don't have to go all the way back to 1.4.2 to work around this, you can use 1.5 also.

Comment by Kenny Inggs [ 03/Sep/12 ]

p.s. For what it's worth, I also tried forcing read preference in the connection string, but to no avail:

var server = MongoServer.Create("mongodb://my-machine/SafeMode=False;ReadPreference=primary");

Generated at Wed Feb 07 21:37:10 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.