[JAVA-706] Aggregate and mapReduce commands sent to mongos break when setting read preference Created: 30/Nov/12  Updated: 08/Jan/13  Resolved: 30/Nov/12

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 2.10.1

Type: Bug Priority: Critical - P2
Reporter: Jeffrey Yemin Assignee: Jeffrey Yemin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends

 Description   

There is an incompatibility between mongos versions 2.0 and greater and previous version, in the way that command are handled.

In mongos 2.0 and greater, commands could be wrapped in a $query field, e.g.

 
{$query : {count : "test"} }

so that they were more in line with how regular queries work, which require the wrapping when sending special fields, like $hint or $explain. In version 1.8 and previous, this wasn't allowed.

The java driver never actually wrapped command like this, since commands didn't have any extra special fields to send. That changed with the 2.9.0 driver, when support for sending read preferences with commands was added. The way it was done was like this:

 
{count : "test", $readPreference : {mode : "primary"}} 

This works for most commands, but breaks some, including mapReduce and aggregate. To fix it, the driver should wraps the command in $query:

 
{$query : { count : "test" }, $readPreference : {mode : "primary"}} 

Unfortunately, this will break when sending the command to mongos version < 2.0, which doesn't support the wrapping. To alleviate this, we will take care to only do this wrapping if the requested read preference is something besides primary or secondaryPreferred. For primary, there is no need since that's the default, and for secondaryPreferred the driver will use the slaveOk bit instead.

Since mongos < 2.2 don't understand read preferences anyway, this looks to be an acceptable choice.



 Comments   
Comment by auto [ 08/Jan/13 ]

Author:

{u'date': u'2012-11-29T23:14:46Z', u'email': u'jeff.yemin@10gen.com', u'name': u'Jeff Yemin'}

Message: JAVA-706: Wrap command in $query field if sending to mongos and read preference is not primary or secondaryPreferred (primary is the default and
secondaryPreferred is already covered by slaveOk bit).
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/b925dee6ca6cdd64bbf0ef0bbde9b2de73f92f37

Comment by Jeffrey Yemin [ 30/Nov/12 ]

The fix is available for testing in a SNAPSHOT release: https://oss.sonatype.org/content/repositories/snapshots/org/mongodb/mongo-java-driver/2.10.1-SNAPSHOT/

Comment by auto [ 30/Nov/12 ]

Author:

{u'date': u'2012-11-29T23:14:46Z', u'email': u'jeff.yemin@10gen.com', u'name': u'Jeff Yemin'}

Message: JAVA-706: Wrap command in $query field if sending to mongos and read preference is not primary or secondaryPreferred (primary is the default and
secondaryPreferred is already covered by slaveOk bit).
Branch: 2.10.x
https://github.com/mongodb/mongo-java-driver/commit/b925dee6ca6cdd64bbf0ef0bbde9b2de73f92f37

Generated at Thu Feb 08 08:52:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.