Skip to content
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

Make autoIndex and autoCreate default to false if connection's read preference is 'secondary' or 'secondaryPreferred' #9374

Closed
chumager opened this issue Aug 30, 2020 · 9 comments
Labels
developer-experience This issue improves error messages, debugging, or reporting enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@chumager
Copy link

Do you want to request a feature or report a bug?
Help
What is the current behavior?
when I set the readPreference to secondayPreferred in my app I get a mongo error:

You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:                                                                
MongoError: not master                                                                                                                                        
    at MessageStream.messageHandler (/home/jcmunoz/.yarn/berry/cache/mongodb-npm-3.6.0-1deea15e1a-3.zip/node_modules/mongodb/lib/cmap/connection.js:266:20)   
    at MessageStream.emit (events.js:314:20)                                                                                                                  
    at processIncomingData (/home/jcmunoz/.yarn/berry/cache/mongodb-npm-3.6.0-1deea15e1a-3.zip/node_modules/mongodb/lib/cmap/message_stream.js:144:12)        
    at MessageStream._write (/home/jcmunoz/.yarn/berry/cache/mongodb-npm-3.6.0-1deea15e1a-3.zip/node_modules/mongodb/lib/cmap/message_stream.js:42:5)         
    at writeOrBuffer (_stream_writable.js:352:12)                                                                                                             
    at MessageStream.Writable.write (_stream_writable.js:303:10)                                                                                              
    at TLSSocket.ondata (_stream_readable.js:713:22)                                                                                                          
    at TLSSocket.emit (events.js:314:20)                                                                                                                      
    at addChunk (_stream_readable.js:303:12)                                                                                                                  
    at readableAddChunk (_stream_readable.js:279:9)                                                                                                           
    at TLSSocket.Readable.push (_stream_readable.js:218:10)                                                                                                   
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)                                                                                          

I tested it like in #9309 and it works fine, but as the stack doesn't provide any useful info I can't trace the error.
is there a way to increase the stack trace so I can figure out where is my problem?

If I avoid the readPreference config I works ok, and I made a little plugin in the pre /find/ hook to set the readPreference in the query and it also works fine, so obviously it's not a mongoose problem, but I can't trace it.

It should come from another module or an error from myself, but it's weird because the driver should take care of doing insert or updates in the primary and querying in secondary.
If the current behavior is a bug, please provide the steps to reproduce.

too much code involve...
What is the expected behavior?
To be able to trace the error so I can fix it.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node: 14.8.0
mongoose: 5.10.2
mongodb atlas 4.2.8

@vkarpov15
Copy link
Collaborator

How are you setting the readPreference?

Also, can you make sure you didn't typo in your app, not "secondayPreferred"?

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Aug 31, 2020
@chumager
Copy link
Author

chumager commented Sep 1, 2020

hi, in the URL: mongodb+srv://user:pass@srv/database?retryWrites=true&w=majority&readPreference=secondaryPreferred&maxStalenessSeconds=120

As my app has a lot of async functions, the logs get printed without any related sequence, and I don't write anything to the DB unless an endpoint send an insert or update.

It happens between the schema definition and the model creation, but AFAIK I could write into de DB without problems even if I set secondaryPreferred, the configuration name is readPreference so I don't know why it could happens.

Regards.

@vkarpov15
Copy link
Collaborator

Do your models define indexes? For example, do you use unique: true or index: true in your schema definitions?

@chumager
Copy link
Author

Hi, yes indeed I've lots of unique and other indexes.

Regards.

@vkarpov15
Copy link
Collaborator

I'd disable autoIndex on this connection, mongoose.connect('mongodb+srv://user:pass@srv/database?retryWrites=true&w=majority&readPreference=secondaryPreferred&maxStalenessSeconds=120', { autoIndex: false })

That will prevent Mongoose from trying to build indexes on this connection. MongoDB won't let you build indexes when you're connected to a secondary, so that's the most likely cause of this error.

For a future release, we should consider making autoIndex default to false if the read preference is secondary or secondaryPreferred, to avoid errors like this.

@vkarpov15 vkarpov15 changed the title mongo error when connecting with secondaryPreferred. Make autoIndex and autoCreate default to false if connection's read preference is 'secondary' or 'secondaryPreferred' Sep 13, 2020
@vkarpov15 vkarpov15 added developer-experience This issue improves error messages, debugging, or reporting and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Sep 13, 2020
@vkarpov15 vkarpov15 added this to the 5.x Unprioritized milestone Sep 13, 2020
@vkarpov15 vkarpov15 added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label Sep 13, 2020
@chumager
Copy link
Author

Thanks for the advice, but in this case, how do I build an index if I'm connected with secondary or secondaryPreferred?

Please tell me if I'm wrong but AFAIK this setting is only for querying. Do you know if this behavior is from mongo driver or is from mongoose?

Regards.

@vkarpov15
Copy link
Collaborator

The MongoDB server doesn't allow you to build indexes or do anything other than read-only operations when you're connected to a secondary. This error is from the MongoDB server, not the MongoDB node driver or Mongoose.

@vkarpov15 vkarpov15 reopened this Sep 22, 2020
@Rossh87
Copy link
Contributor

Rossh87 commented Feb 12, 2021

@vkarpov15 Interested in a PR for this?

@vkarpov15
Copy link
Collaborator

@Rossh87 yes, please base the PR against the 6.0 branch.

@vkarpov15 vkarpov15 modified the milestones: 5.x Unprioritized, 6.0 Feb 15, 2021
Rossh87 added a commit to Rossh87/mongoose that referenced this issue Mar 4, 2021
…ry connections

MongoDB will not allow index creation on a non-primary replica.
If user-specified connection explicitly sets index creation  options,
and specifies secondary read preference, throw.  Otherwise, default
index options to false.
Rossh87 added a commit to Rossh87/mongoose that referenced this issue Mar 4, 2021
…ry connections

MongoDB will not allow index creation on a non-primary replica.
If user-specified connection explicitly sets index creation  options,
and specifies secondary read preference, throw.  Otherwise, default
index options to false.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer-experience This issue improves error messages, debugging, or reporting enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

3 participants