Question on replica set and driver behavior

15 views
Skip to first unread message

Davis Ford

unread,
Dec 8, 2016, 10:54:49 AM12/8/16
to mgo-users
Hi Gustavo,

I know this has been discussed in some previous posts, and I did some digging, but I just want to make sure I understand the driver behavior correctly.

First and foremost, let's say I have a replica set with 3 members:

* primary P1
* secondary S1
* secondary S2

What would the behavior be if say, S2 is currently down and has been removed from the replica set and we create the following session:

session, err := mgo.Dial("mongodb://P1:27017,S1:27017,S2:27017/mydatabase")

One thing I noticed, as I'm trying to upgrade my mongo nodes, in my production app: I took one of the secondaries offline, and removed it from the replica set on the primary by issuing rs.remove('S2').  This seemed to have caused a replica set re-configuration, and my production app started to return EOF for all requests to the current session.

My understanding from reading other posts on this forum, is that this could get fixed dynamically if we simply do a session.Refresh() -- is that true?  We are using 

session.SetMode(mgo.Strong, true)



Gustavo Niemeyer

unread,
Dec 8, 2016, 12:14:18 PM12/8/16
to mgo-...@googlegroups.com
Yes, session.Refresh would do it, but see the response to your other message. You don't want that being done behind the back of concurrent code, since they'd ignore actual errors they'd better deal with.

Alternatively, a better approach when you're dealing with http requests is to create a new session by copying your master session inside the handler:

session := master.Copy()
defer session.Close()

This way you don't need to worry about its lifetime. If it errors out, you can just return as usual in Go and it'll get closed at the proper point. The new session will pick a new underlying socket from the pool or reconnect if necessary.





--
You received this message because you are subscribed to the Google Groups "mgo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mgo-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Davis Ford

unread,
Dec 8, 2016, 12:33:06 PM12/8/16
to mgo-users
Thanks for the feedback.  I am indeed copying the session for http requests exactly as you'd expect.

But please see my response to your response in the other thread, which has some additional context / questions.

The local code I have which may execute a query, does indeed handle errors locally.  For example....

session, err := mgo.Dial('url')

func someHandler(session) {
   cs := session.Copy()
   defer cs.Close()
   // execute query 
   result, err := cs. [do some query ]
   if err != nil {
      // returns an error to the client as expected
   }
   // returns some response
}

What I'm trying to figure out is how to detect a situation where the replica set is reconfiguring or has reconfigured.  This morning, I had to remove one secondary member from my replica set.  I did this by executing the command:


rs.remove('url-of-secondary')

on my PRIMARY database.  This threw the mgo session into an unusable state....causing all operations to return the error EOF.  I want to recover from this.
To unsubscribe from this group and stop receiving emails from it, send an email to mgo-users+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages