[JAVA-812] setting slaveOK() does not result in a secondary read preference through mongos Created: 23/Apr/13  Updated: 31/Mar/15  Resolved: 22/Aug/13

Status: Closed
Project: Java Driver
Component/s: Cluster Management, Connection Management
Affects Version/s: 2.11.1
Fix Version/s: 2.11.3, 3.0.0

Type: Bug Priority: Major - P3
Reporter: Andre de Frere Assignee: Jeffrey Yemin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related

 Description   

Although slaveOk() is deprecated, if it is in use the expectation is it should behave the same as setting ReadPreference.secondaryPreferred();

Instead the queries are seen with { $readPreference:

{ mode: "primary" }

} explicitly set when going through a mongos

Under 2.6 the following java sample will correctly result in a query to a Secondary. The same code in 2.7 or above will result with the primary $readPreference set.

import com.mongodb.*;
import java.net.UnknownHostException;
 
public class TestSlaveOK{
	public static void main(String[] args){
		Mongo m = null;
		DB db = null;
		DBCollection coll = null;
		
		try{
		
		MongoOptions mongoOptions = new MongoOptions();
		mongoOptions.slaveOk = true;
 
		m = new Mongo("localhost:27017", mongoOptions);
		db = m.getDB("test");
		}
		
		catch(UnknownHostException uhe){
		System.out.println("UnknownHostException:" + uhe);
		}
		coll = db.getCollection("test");
		DBObject query = new BasicDBObject("x", "100");
		DBObject obj = coll.findOne(query, null);	
	}
}

Looking at the highest verbosity logs (or capturing the query in another way) under 2.11.1 will show the query executed with { $readPreference:

{ mode: "primary" }

} on both the mongos and the Primary node:

Tue Apr 23 16:37:37 [conn28] shard query: test.test  { $query: { x: "100" }, $readPreference: { mode: "primary" } }



 Comments   
Comment by Jeffrey Yemin [ 31/Mar/15 ]

Closing all resolved 3.0.0 issues, as 3.0.0 has been tagged and released.

Comment by auto [ 12/Sep/13 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-812: Only set $readPreference on a query if the read preference is not primary.
This lets the slaveOK bit have precedence if it's set.
Branch: 2.11.x
https://github.com/mongodb/mongo-java-driver/commit/4f049873ff9923a57247cf7580d5cc0fb4381948

Comment by auto [ 22/Aug/13 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-812: Only set $readPreference on a query if the read preference is not primary.
This lets the slaveOK bit have precedence if it's set.
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/04bcf29716fd941d0eac9788fdfe763f302540b8

Comment by Jeffrey Yemin [ 24/Apr/13 ]

The driver is setting both the slaveok bit in query options and {$readPreference :

{"mode" : "primary}

}, and as of mongos 2.2, the read preference wins and queries are routed to shard primaries.

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