[SERVER-22289] Mongo.setReadPref(“secondary”) doesn't work when connected to primary node Created: 25/Jan/16  Updated: 29/Jan/16  Resolved: 25/Jan/16

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 3.0.8, 3.2.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Robert Aistleitner Assignee: Kelsey Schubert
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

As stated in the description..

Participants:

 Description   

I'm not sure if I got this wrong, but I expect Mongo.setReadPref("secondary") to force reads to get redirected to the secondary nodes of a replica set. If I'm connected to the master of the set via mongo-shell, the shell should know about primary/secondary nodes. Problem is that if I query something (which is of course a read) the master handles my query, which is not what I want it to do (because I do some reporting stuff).

I also tried to set the readPref for single find(...) calls (with db.collection.find(

{email: "example@mail.com"}

).readPref("secondary")), which didn't work neither.

If I connect to the secondary right away (when starting the mongo shell), this works as expected. The problem still exists, since I don't want to care to which server I connect (in case of primary/secondary node changes). Mongo drivers in python e.g. do support this.



 Comments   
Comment by Kelsey Schubert [ 29/Jan/16 ]

Hi robsi,

I wanted to correct my previous comment and provide some additional information. The mongo shell respects read preference, but you need to use a replica set connection instead of a direct connection

I suggest you use the following command which creates a replica set connection:

mongo --host replsetname/seedhost:port script.js

where the script sets the readPref to secondary.

Additionally, as of 3.2.0, SERVER-6233 means you may also use the modern URI style connection string:

mongo mongodb://seedhost1:port,seedhost2:port,.../dbname?replicaSet=replsetname

I believe that the first command should fulfill your needs. It is possible that during failover the read pref of "secondary" may be directed to the new primary if the connection if the connection was established while it was a secondary.

Kind regards,
Thomas

Comment by Kelsey Schubert [ 25/Jan/16 ]

Hi robsi,

I connected to the replicaset using

mongo --host host1:27017

When this parameter is used the mongo shell will connect to the primary and if a step down occurs it will reconnect to the new primary after an election occurs.

I executed

db.getMongo().setReadPref("secondary")

and observed that the queries were being sent to the secondaries.

If you follow these steps and the issue persists please comment and we will reopen this ticket.

Thank you,
Thomas

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