22
33from math import ceil
44
5- from Common import DB
6- from Sharding import Sharding
7-
85
96class Replset :
107 def __init__ (self , db , user = None , password = None , authdb = 'admin' , max_lag_secs = 5 ):
@@ -31,18 +28,18 @@ def __init__(self, db, user=None, password=None, authdb='admin', max_lag_secs=5)
3128 def close (self ):
3229 pass
3330
34- def get_rs_status (self , force = False ):
31+ def get_rs_status (self , force = False , retry = True ):
3532 try :
3633 if force or not self .rs_status :
37- self .rs_status = self .db .admin_command ('replSetGetStatus' )
34+ self .rs_status = self .db .admin_command ('replSetGetStatus' , retry )
3835 return self .rs_status
3936 except Exception , e :
4037 raise Exception , "Error getting replica set status! Error: %s" % e , None
4138
42- def get_rs_config (self ):
39+ def get_rs_config (self , retry = True ):
4340 try :
4441 if self .db .server_version () >= tuple ("3.0.0" .split ("." )):
45- output = self .db .admin_command ('replSetGetConfig' )
42+ output = self .db .admin_command ('replSetGetConfig' , retry )
4643 return output ['config' ]
4744 else :
4845 return self .connection ['local' ].system .replset .find_one ()
@@ -133,76 +130,3 @@ def primary_optime(self):
133130 rs_primary = self .find_primary (True )
134131 if 'optime' in rs_primary :
135132 return rs_primary ['optime' ]
136-
137-
138- class ReplsetSharded :
139- def __init__ (self , sharding , db , user = None , password = None , authdb = 'admin' , max_lag_secs = 5 ):
140- self .sharding = sharding
141- self .db = db
142- self .user = user
143- self .password = password
144- self .authdb = authdb
145- self .max_lag_secs = max_lag_secs
146-
147- self .replsets = {}
148- self .replset_conns = {}
149-
150- # Check Sharding class:
151- if not self .sharding .__class__ .__name__ == "Sharding" :
152- raise Exception , "'sharding' field is an instance of %s, not 'Sharding'!" % self .sharding .__class__ .__name__ , None
153-
154- # Get a DB connection
155- try :
156- if self .db .__class__ .__name__ == "DB" :
157- self .connection = self .db .connection ()
158- if not self .connection .is_mongos :
159- raise Exception , 'MongoDB connection is not to a mongos!' , None
160- else :
161- raise Exception , "'db' field is an instance of %s, not 'DB'!" % self .db .__class__ .__name__ , None
162- except Exception , e :
163- logging .fatal ("Could not get DB connection! Error: %s" % e )
164- raise e
165-
166- def get_replset_connection (self , host , port , force = False ):
167- conn_name = "%s-%i" % (host , port )
168- if force or not conn_name in self .replset_conns :
169- try :
170- self .replset_conns [conn_name ] = DB (host , port , self .user , self .password , self .authdb )
171- except Exception , e :
172- logging .fatal ("Could not get DB connection to %s:%i! Error: %s" % (host , port , e ))
173- raise e
174- return self .replset_conns [conn_name ]
175-
176- def get_replsets (self , force = False ):
177- for shard in self .sharding .shards ():
178- shard_name , members = shard ['host' ].split ('/' )
179- host , port = members .split (',' )[0 ].split (":" )
180- port = int (port )
181- if force or not shard_name in self .replsets :
182- try :
183- rs_db = self .get_replset_connection (host , port )
184- self .replsets [shard_name ] = Replset (rs_db , self .user , self .password , self .authdb , self .max_lag_secs )
185- except Exception , e :
186- logging .fatal ("Could not get Replset class object for replset %s! Error: %s" % (rs_name , e ))
187- raise e
188- return self .replsets
189-
190- def find_secondaries (self ):
191- shard_secondaries = {}
192- for rs_name in self .get_replsets ():
193- replset = self .replsets [rs_name ]
194- shard_secondaries [rs_name ] = replset .find_secondary ()
195- return shard_secondaries
196-
197- def primary_optimes (self ):
198- primary_optimes = {}
199- for rs_name in self .get_replsets ():
200- replset = self .replsets [rs_name ]
201- primary_optimes [rs_name ] = replset .primary_optime ()
202- return primary_optimes
203-
204- def close (self ):
205- for rs_name in self .replsets :
206- self .replsets [rs_name ].close ()
207- for conn_name in self .replset_conns :
208- self .replset_conns [conn_name ].close ()
0 commit comments