-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Unable to set readPreference #4831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Ok, so basically, the readPreference parse forces PRIMARY when none is provided instead of using the one provided at database initialization level. Are you willing to provide a fix for that? It seems reasonable to change it so readPreference is 'undefined' if not provided. |
Thanks for the quick response. It looks like this is a result of the change implemented here: #4449 |
I believe the readPreference should not be passed on a query basis if it is not provided, as it overrides the default driver behavior. I can have a look too :) |
Thanks for looking at this, I may have made a mistake here, but, I've manually put in your change(excluding the spec files) by commenting out the line) and it doesn't resolve my issue. Reads go to the Primary again. If I do the following:
Then it works as I would expect. |
Uhm... interesting, but what bothers me is that we should not touch this prop, the driver should do it’s job and use the provided readPreference unless specified. I perhaps suspect that passing readPreference: undefined, makes the driver use the primary. What if your ‘delete’ readPreference if undefined, jnstesd of you ur workaround? |
Apologies for the delay in replying. Sorry, I'm not sure I understand what you want me to do? If you can point me to the section of code that parses and creates the structure for the mongodb URI, I can look deeper as my above solution only works if you add the databaseOptions parameter at the top level. Many thanks. I"m not sure why my URI readPreference at the URI level is not adhered to when using your submitted fix. |
I believe the issue lies in passing undefined. And the driver sets internally as PRIMARY, not the connection parameter. My suggestion would be to not pass the readPreference if it’s un defined at all. Do you follow? |
Understood. Thanks for clarification. Let me test again on my side and update you. |
If I understand correctly it appears that if you omit the readPreference entirely it is defaulting to PRIMARY. I've tested this by simply commenting out in MongoStorageAdaptor as follows:
with debug logging, I still see all requests going to the PRIMARY despite the database object clearly showing readPreference is nearest: Db { |
That would mean that at large the driver option is not respected when initializing it. Which is odd.. |
@rossdraper have a look at this particular commit 87a7c96 Everything looks fine now. |
* fix(package): update mongodb to version 3.1.0 * chore(package): update lockfile https://npm.im/greenkeeper-lockfile * starting mongo 3.1.0, read preferences are passed again * Adds test confirming #4831 is properly functional now
* fix(package): update mongodb to version 3.1.0 * chore(package): update lockfile https://npm.im/greenkeeper-lockfile * starting mongo 3.1.0, read preferences are passed again * Adds test confirming #4831 is properly functional now
* fix(package): update mongodb to version 3.1.0 * chore(package): update lockfile https://npm.im/greenkeeper-lockfile * starting mongo 3.1.0, read preferences are passed again * Adds test confirming #4831 is properly functional now
* fix(package): update mongodb to version 3.1.0 * chore(package): update lockfile https://npm.im/greenkeeper-lockfile * starting mongo 3.1.0, read preferences are passed again * Adds test confirming parse-community#4831 is properly functional now
Issue Description
When using parse-server 2.7.2 with a mongodb geographically seperated replicaset, using readPreference=nearest on the URI(or in databaseOptions) is not working. When setting mongoclient log level to debug I can see:
showing in the logs for queries and they are going to the primary IP.
Steps to reproduce
Connect to any replicaset using the normal uri string:
mongodb://dbuser:[email protected]:27017,10.1.0.1:27017,10.2.0.1:27017/dbname?replicaSet=RS01&w=1&readPreference=nearest&connectTimeoutMS=30000&maxPoolSize=300',
Expected Results
Running mongostat I expect to see queries logged on the nearest db and in the debug logs for the parse server/mongo client I expect to see queries passed to the nearest server. This also happens for secondary or secondaryPreferred.
Actual Outcome
The readPreference is reset to "PRIMARY".
Environment Setup
Server
Database
Logs/Trace
I have added some console log statements to parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js
I can see that the value "nearest" is correctly used in the constructor, however it is getting reset to "PRIMARY" because calls to _parseReadPreference are all having a undefined value for readPreference passed in. As such they hit lines 628-631 which is marked as being added due to testing and it gets reset to "PRIMARY'.
My knowledge of the source code is not good enough to trace this back further, but it seems that this is a option to support per query read preferences instead of using the global setting from the URI. If I set this to be:
then things work as expected.
The text was updated successfully, but these errors were encountered: