[JAVA-570] distinct and count not go to secondary even we have specified the ReadPreference Created: 21/May/12  Updated: 29/Aug/12  Resolved: 02/Jul/12

Status: Closed
Project: Java Driver
Component/s: Connection Management
Affects Version/s: 2.7.2
Fix Version/s: 2.9.0

Type: Bug Priority: Major - P3
Reporter: Xiaofeng Wu Assignee: Steve Briskin (Inactive)
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to JAVA-497 ReadPrefs ignored for commands Closed

 Description   

count/distict implementation of DBCollection do not take ReadPreference, so eventually it goes to Primary instead of Secondary even we have specified secondary preference.

Here's the code caused this problem:
public long getCount(DBObject query, DBObject fields, long limit, long skip )
throws MongoException {

BasicDBObject cmd = new BasicDBObject();
cmd.put("count", getName());
cmd.put("query", query);
if (fields != null)

{ cmd.put("fields", fields); }

if ( limit > 0 )
cmd.put( "limit" , limit );
if ( skip > 0 )
cmd.put( "skip" , skip );

CommandResult res = _db.command(cmd,getOptions());

Here's when calling "_db.command(cmd,getOptions())" but not passing read preference, it works when I changed it to:
CommandResult res = _db.command(cmd,getOptions(),this.getReadPreference());

same issue for "distinct"



 Comments   
Comment by Jeffrey Yemin [ 29/Aug/12 ]

Closing as part of 2.9.0 release process.

Comment by auto [ 10/Jul/12 ]

Author:

{u'date': u'2012-06-25T07:49:44-07:00', u'name': u'Steve Briskin', u'email': u'steve.briskin@10gen.com'}

Message: merge with branch JAVA-570
Branch: ReadPrefs
https://github.com/mongodb/mongo-java-driver/commit/f7210ea648bc1c4b284e7ce7ac6da91c9a99c9a7

Comment by auto [ 10/Jul/12 ]

Author:

{u'date': u'2012-06-19T11:56:06-07:00', u'email': u'steve.briskin@10gen.com', u'name': u'Steve Briskin'}

Message: JAVA-570: using read preference for count(), distinct(), and group()
Branch: ReadPrefs
https://github.com/mongodb/mongo-java-driver/commit/b745c8d05414d81b5dd16cff30061d2d641897a1

Comment by auto [ 25/Jun/12 ]

Author:

{u'date': u'2012-06-25T07:49:44-07:00', u'name': u'Steve Briskin', u'email': u'steve.briskin@10gen.com'}

Message: merge with branch JAVA-570
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/f7210ea648bc1c4b284e7ce7ac6da91c9a99c9a7

Comment by auto [ 25/Jun/12 ]

Author:

{u'date': u'2012-06-19T11:56:06-07:00', u'name': u'Steve Briskin', u'email': u'steve.briskin@10gen.com'}

Message: JAVA-570: using read preference for count(), distinct(), and group()
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/b745c8d05414d81b5dd16cff30061d2d641897a1

Comment by Jeffrey Yemin [ 21/May/12 ]

Please use slaveOk (even though it's deprecated) until we get this fixed.

Comment by Jeffrey Yemin [ 21/May/12 ]

This is related to JAVA-497, but not totally duplicated by it. The only case that this works as expected is when you set slaveOk on the collection:

 
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
 
import java.net.UnknownHostException;
import java.util.Arrays;
 
public class ReplicaSetCountTest {
    public static void main(String[] args) throws UnknownHostException {
        Mongo m = new Mongo(Arrays.asList(new ServerAddress("127.0.0.1", 27017)));
 
        DBCollection c = m.getDB("test").getCollection("slaveOk");
 
        c.find().slaveOk().count();  // goes to primary
 
        c.find().setReadPreference(ReadPreference.SECONDARY).count();  // goes to primary
 
        c.slaveOk();
        c.find().count();  // goes to secondary
 
        c.resetOptions();
        c.setReadPreference(ReadPreference.SECONDARY);
        c.find().count();  // goes to primary
 
    }
}

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