@@ -143,7 +143,8 @@ def __init__(self, sharding, db, user, password, authdb, max_lag_secs):
143143 self .authdb = authdb
144144 self .max_lag_secs = max_lag_secs
145145
146- self .replset = None
146+ self .replsets = {}
147+ self .replset_conns = {}
147148
148149 # Check Sharding class:
149150 if not self .sharding .__class__ .__name__ == "Sharding" :
@@ -161,22 +162,41 @@ def __init__(self, sharding, db, user, password, authdb, max_lag_secs):
161162 logging .fatal ("Could not get DB connection! Error: %s" % e )
162163 raise e
163164
165+ def get_replset_connection (self , host , port , force = False ):
166+ conn_name = "%s-%i" % (host , port )
167+ if force or not conn_name in self .replset_conns :
168+ try :
169+ self .replset_conns [conn_name ] = DB (host , port , self .user , self .password , self .authdb )
170+ except Exception , e :
171+ logging .fatal ("Could not get DB connection to %s:%i! Error: %s" % (host , port , e ))
172+ raise e
173+ return self .replset_conns [conn_name ]
174+
175+ def get_replsets (self , force = False ):
176+ for shard in self .sharding .shards ():
177+ shard_name , members = shard ['host' ].split ('/' )
178+ host , port = members .split (',' )[0 ].split (":" )
179+ port = int (port )
180+ if force or not shard_name in self .replsets :
181+ try :
182+ rs_db = self .get_replset_connection (host , port )
183+ self .replsets [shard_name ] = Replset (rs_db , self .user , self .password , self .authdb , self .max_lag_secs )
184+ except Exception , e :
185+ logging .fatal ("Could not get Replset class object for replset %s! Error: %s" % (rs_name , e ))
186+ raise e
187+ print self .replsets
188+ return self .replsets
189+
164190 def find_secondaries (self ):
165191 shard_secondaries = {}
166- if self .sharding :
167- for shard in self .sharding .shards ():
168- shard_name , members = shard ['host' ].split ('/' )
169- host , port = members .split (',' )[0 ].split (":" )
170-
171- replset_db = DB (host , port , self .user , self .password , self .authdb )
172- self .replset = Replset (replset_db , self .user , self .password , self .authdb , self .max_lag_secs )
173- secondary = self .replset .find_secondary ()
174- shard_secondaries [shard_name ] = secondary
175-
176- self .replset .close ()
177- replset_db .close ()
192+ for rs_name in self .get_replsets ():
193+ replset = self .replsets [rs_name ]
194+ secondary = replset .find_secondary ()
195+ shard_secondaries [rs_name ] = secondary
178196 return shard_secondaries
179197
180198 def close (self ):
181- if self .replset :
182- self .replset .close ()
199+ for conn_name in self .replset_conns :
200+ self .replset_conns [conn_name ].close ()
201+ for rs_name in self .replsets :
202+ self .replsets [rs_name ].close ()
0 commit comments