38

Having some trouble understanding the right approach here.

I have a connection to a mongodb replica set with three members (standard master-slave-slave). Everything is working fine with the connection when the master remains consistent.

pymongo.Connection(['host1:27017','host2:27018','host3:27019']).database_test

For some reason, when the replica set primary steps down, this starts to throw an autoreconnect exception that doesn't go away even after a new primary is elected.

Now I am aware that this exception needs to be caught and handled, most likely by waiting for the new primary to be elected. The problem I am having seems to be that it doesn't care at all once the new primary has been chosen. This "master has changed" exception just keeps coming up.

Printing the connection with __dict__ shows all three hosts.

I've tried passing the replicaset kwarg to the connection, but this comes up as an unexpected argument.

Is there a reason why this kind of connection wouldn't just start querying against the new primary?

EDIT:

This same problem is apparently now manifesting on the deployment server. The autoreconnect exception is thrown if the master changes at all and never goes away even after a new primary is elected.

Pymongo is version 2.2 and mongodb version 2.0.2. Changing the manner in which the connection is defined in the pymongo code (mongouri vs. list of hosts) has no effect. The only way to revive the service is to rs.stepDown() the other hosts until the original master is primary once more.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
DeaconDesperado
  • 9,977
  • 9
  • 47
  • 77
  • What version of pymongo are you using? There was a bug in 1.9 or 1.10, I can't remember which, where it could exhibit this behavior. It was definitely fixed as of 1.11 and 2.0+ – dcrosta Dec 14 '11 at 15:17
  • What version of MongoDB? 2.0.x has an issue where if you stepdown the primary it doesn't close it's connections. This will cause the problem you're having with PyMongo. https://jira.mongodb.org/browse/SERVER-4405 – Bernie Hackett Dec 14 '11 at 15:28
  • Mongo is version 2.0.0. I think this may be part of the problem. I should mention though that just now I specified the replicaSet using a mongouri rather than kwargs or driver properties, and this seemed to make it failover. Not sure if the bug will make it manifest again though. – DeaconDesperado Dec 14 '11 at 15:34
  • This definitely sounds like the pymongo is still trying to use the same connection even after the master has gone. When I had problems like this with PHP and Mongo, I would close the database connection, wait briefly, and then create a new one. – slashingweapon Oct 06 '12 at 00:54
  • Can you provide us with your exception handling and retry code please? The [doc](http://api.mongodb.org/python/2.2/examples/replica_set.html) says (the driver) _will attempt to automatically reconnect on subsequent operations_, so I want to make sure you do try operating again instead of just waiting for the error to disappear. And another (completely different) question, do you get the same behaviour connecting with `Connection("mongodb://host1:27017,host2:27018,host3:27019")`? – Alain BECKER Oct 06 '12 at 20:13
  • 8
    Does this problem still happen with the current version of MongoDB and PyMongo? The original question is 10 months old and the versions have moved on somewhat since then :) – Stennie Oct 08 '12 at 11:59

1 Answers1

2

The behavior you describe is a bug. The best possible course of action is to make sure there is a bug logged for it and link to it from your question. Since the question is almost a year old, I am expecting the bug to be closed (check jira.mongodb.org/browse/SERVER-4405 to see if it applies).

If you upgrade to MongoDB 2.2 or later, the problem should go away.

iwein
  • 25,788
  • 10
  • 70
  • 111