mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Not reconnecting to replica set

Open lauramorillo opened this issue 10 years ago • 6 comments

Hello,

I am trying to figure out how to reconnect to a replica set when the connection is lost. You can find an example here.

You will need to initialize first the replica set with this config:

config = {
    "_id" : "rs198765",
    "members" : [
      {
        "_id" : 0,
        "host" : "mongo1:27017"
      },
      {
        "_id" : 1,
        "host" : "mongo2:27017"
      },
      {
        "_id" : 2,
        "host" : "mongo3:27017"
      }
    ]
  }

I start everything in the docker-compose and once everything is running I stop all the databases and I see the disconnect event being executed. After that, if I start again the database instances, but my code is never connecting again.

Am I doing something wrong or missing any configuration param?

Thank you!

lauramorillo avatar Dec 05 '16 18:12 lauramorillo

Try listening to the 'reconnected' and 'connected' events too

vkarpov15 avatar Dec 06 '16 22:12 vkarpov15

Thanks for the advice @vkarpov15. I added those to the test code and tried it again. The 'connected' event is executed when I start the app and it connects successfully but none of them are executed after stopping the database images and starting them again.

lauramorillo avatar Dec 07 '16 10:12 lauramorillo

How long are you shutting the database images down for? Might be that reconnectTries somehow isn't getting passed down correctly

vkarpov15 avatar Dec 08 '16 20:12 vkarpov15

@vkarpov15 I'm actually looking into some issues I'm having. This is work made following an issue I added to the mongo driver: https://jira.mongodb.org/browse/NODE-893.

What I'm seeing is that replica set never emits a 'reconnect' nor 'open' events when connecting back. It just emits a 'connect' event. What's currently happening is that the replica set emits a 'close' event, that makes a 'buffer' variable to switch to true, further queries are buffered, and that never changes again.

There seems to be some code setup to listen for the 'open' event:

https://github.com/Automattic/mongoose/blob/master/lib/drivers/node-mongodb-native/connection.js#L171.

And then emit a 'reconnect' event. It seems that we no longer should need to listen to the 'open' event, and start listening to the 'connect' event instead (or maybe listen for both?). The driver code I'm talking about is around https://github.com/christkv/mongodb-core/blob/2.0/lib/topologies/replset.js#L622.

Looking forward for you giving me your thoughts about this, thanks..!!

gfzabarino avatar Dec 08 '16 23:12 gfzabarino

@vkarpov15 I actually debugged the mongo core driver and saw that the ReplSet is not firing connect / reconnect events after having fired the close event, which is the responsible of having Mongoose to switch to buffer mode. See a detailed explanation in https://jira.mongodb.org/browse/NODE-893?focusedCommentId=1453800&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-1453800

gfzabarino avatar Dec 09 '16 14:12 gfzabarino

This should be solved by https://github.com/Automattic/mongoose/pull/4792.

gfzabarino avatar Dec 22 '16 17:12 gfzabarino