can't force mongo to work with secondary in replicaset

40 views
Skip to first unread message

mongoUser1

unread,
Jul 18, 2012, 11:32:12 AM7/18/12
to mongod...@googlegroups.com
hi ,

i am using a replica set with 1 primary and 1 secondary .
through my java driver i am doing the following to force the mongo to query the secondary first , and i check programmatically on with instance it worked on :

         MongoOptions options = new MongoOptions();

         options.connectionsPerHost = 20;

         options.autoConnectRetry = true; 

         m = new Mongo( mongoServerList,options);
        
         m.slaveOk();
        
         m.setReadPreference(ReadPreference.SECONDARY);

         DB db = m.getDB( "auth" );
        
         BasicDBObject f= (BasicDBObject) db.eval("db.isMaster()",0);
        
         boolean isMaster = (boolean) f.get("ismaster");
        
         System.out.println("is Master : " + isMaster);

output :
is Master :true

why?.....


A. Jesse Jiryu Davis

unread,
Jul 18, 2012, 1:35:02 PM7/18/12
to mongod...@googlegroups.com
eval() is always sent to the primary. You can use the generic command() method to run commands on secondaries, although with the upcoming Java driver release that will change, and virtually all commands will be run on the primary no matter how you call them.

There are a few ways to verify that operations are run on secondaries. You can start secondaries with high verbosity (mongod -vvvvv) and tail the logs to see what operations they're performing. You can setProfilingLevel(2) and see which queries are run by looking at the system.profile collection. Or use mongosniff on the secondaries' machines to see what queries the driver sends them.

yair friedman

unread,
Jul 19, 2012, 2:19:34 AM7/19/12
to mongod...@googlegroups.com
what i really need is a way to prefer the run of my queries on the secondary instances since from what i understand - in a replica set there is only 1 primary and up to 11 secondaries (since a replica set support up to 12 instances ).
since i have a lot of load on the mongo i want the read queries to be performed on the large number of secondaries rather then on all the load on 1 primary .
should the
 m.setReadPreference(ReadPreference.SECONDARY);

do the job ?
thanks!


--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

A. Jesse Jiryu Davis

unread,
Jul 19, 2012, 9:19:13 AM7/19/12
to mongod...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages