Read preference needs mongos when used with Java driver?

248 views
Skip to first unread message

Christian Bayer

unread,
Oct 14, 2014, 11:42:01 AM10/14/14
to mongod...@googlegroups.com
I'm trying to use the read preference setting but couldn't get it to work on my setup consisting of an arbiter, a primary and a secondary. I use a Java client and set the read preference on a cursor. I was surprised that it wasn't working so I checked the code of the Java driver v2.12.1:

 private void _check() {
 [...]
       if (_collection.getDB().getMongo().isMongosConnection()) {
            builder.addReadPreference(_readPref);
        }

Why do you require the read preference to be on a mongos connection? That should work just fine unsharded.

Jeff Yemin

unread,
Oct 14, 2014, 12:25:34 PM10/14/14
to mongod...@googlegroups.com
That code is misleading.  The effect of adding the read preference to the QueryOpBuilder is to add a $readPreference field to the query specification, which is only necessary when sending the query to a mongos.  When the driver is connected to a replica set, that's not necessary since the driver is responsible for routing.

How are you setting the read preference?  Can you provide some evidence to show that it's not working?

For reference, here's a test demonstrating the expected behavior.


Jeff

Christian Bayer

unread,
Oct 14, 2014, 12:43:15 PM10/14/14
to mongod...@googlegroups.com

Hey Jeff,

 

thanks for your quick reply! Here is my code snippet:

 

private DBCursor cursor( DB db, String id ) {

DBCollection col = db.getCollection( id );

BasicDBObject query = new BasicDBObject();

query.put( "uuid", new BasicDBObject( "$in", uuids ) );

return col.find( query ).setReadPreference( ReadPreference.secondary() );

}

 

I'm executing this against a single MongoDB instance that I bring up in my unit tests, no secondary. With a read preference of secondary, I expect this unit test to fail (according to http://docs.mongodb.org/manual/reference/read-preference/#secondary), but it works fine.

 

Christian

 

 

 

On Tuesday 14 October 2014 09:25:34 Jeff Yemin wrote:

> That code is misleading. The effect of adding the read preference to the

> QueryOpBuilder is to add a $readPreference field to the query

> specification, which is only necessary when sending the query to a mongos.

> When the driver is connected to a replica set, that's not necessary since

> the driver is responsible for routing.

>

> How are you setting the read preference? Can you provide some evidence to

> show that it's not working?

>

> For reference, here's a test

> <https://github.com/mongodb/mongo-java-driver/blob/2.12.x/src/test/com/mongo

> db/SecondaryReadTest.java#L75-104> demonstrating the expected behavior.

Jeff Yemin

unread,
Oct 14, 2014, 12:53:09 PM10/14/14
to mongod...@googlegroups.com
Your original post mentioned a primary, secondary, and arbiter.  In your unit test are you starting a standalone MongoDB server (no --replicaset)?   Can you provide the code snippet where you create the MongoClient instance?  If you are starting a standalone, a secondary read preference will actually work, since the driver considers a standalone to match any read preference.

Secondly, the code below doesn't actually send anything to the server, as it doesn't iterate the DBCursor, so make sure that somewhere in the test the DBCursor is iterated.

Jeff

Christian Bayer

unread,
Oct 14, 2014, 1:05:47 PM10/14/14
to mongod...@googlegroups.com
I know, in reality both my unit test setup as well as the MongoClient creation are much more complex to put it here in full. For the unit test, my setup is like the following:

String mongoUrl = "mongodb://127.0.0.1:27017";
MongoClient mongo = new MongoClient( new MongoClientURI( uri ) );

Your comment about the standalone solves the mystery, we haven't actually deployed this to production yet. So it should work just with our setup? I'll verify with a clustered MongoDB unittest then.

Christian

Jeff Yemin

unread,
Oct 14, 2014, 1:30:01 PM10/14/14
to mongod...@googlegroups.com
Yes, it should work as expected.


Jeff
Reply all
Reply to author
Forward
0 new messages