@@ -16,6 +16,7 @@ def __init__(self, db, user=None, password=None, authdb='admin', balancer_wait_s
1616 self .balancer_sleep = balancer_sleep
1717
1818 self .config_server = None
19+ self .config_db = None
1920 self ._balancer_state_start = None
2021
2122 # Get a DB connection
@@ -31,6 +32,8 @@ def __init__(self, db, user=None, password=None, authdb='admin', balancer_wait_s
3132 raise e
3233
3334 def close (self ):
35+ if self .config_db :
36+ self .config_db .close ()
3437 return self .restore_balancer_state ()
3538
3639 def get_start_state (self ):
@@ -98,8 +101,8 @@ def stop_balancer(self):
98101 logging .fatal ("Could not stop balancer: %s:%i!" % (self .host , self .port ))
99102 raise Exception , "Could not stop balancer: %s:%i" % (self .host , self .port ), None
100103
101- def get_configserver (self , force = False ):
102- if force or not self . config_server :
104+ def get_configdb_hosts (self ):
105+ try :
103106 cmdlineopts = self .db .admin_command ("getCmdLineOpts" )
104107 config_string = None
105108 if cmdlineopts .get ('parsed' ).get ('configdb' ):
@@ -109,26 +112,33 @@ def get_configserver(self, force=False):
109112 if config_string :
110113 # noinspection PyBroadException
111114 try :
112- config_list = config_string .split ("," )
115+ return config_string .split (',' )
113116 except Exception :
114- config_list = [config_string ]
117+ return [config_string ]
118+ else :
119+ logging .fatal ("Unable to locate config servers for %s:%i!" % (self .host , self .port ))
120+ raise Exception , "Unable to locate config servers for %s:%i!" % (self .host , self .port ), None
121+ except Exception , e :
122+ raise e
115123
116- db = None
124+ def get_config_server (self , force = False ):
125+ if force or not self .config_server :
126+ configdb_hosts = self .get_configdb_hosts ()
127+ try :
128+ config_host , config_port = configdb_hosts [0 ].split (":" )
129+ logging .info ("Found sharding config server: %s" % (config_host ))
130+
131+ self .config_db = DB (config_host , config_port , self .user , self .password , self .authdb )
132+ rs = Replset (self .config_db , self .user , self .password , self .authdb )
117133 try :
118- config_host , config_port = config_list [0 ].split (":" )
119- db = DB (config_host , config_port , self .user , self .password , self .authdb )
120- rs = Replset (db , self .user , self .password , self .authdb )
121- try :
122- self .config_server = rs .find_secondary (True , True )
123- except Exception :
124- self .config_server = {'host' : config_list [0 ], 'replSet' : None }
125- except Exception , e :
126- raise e
134+ rs_status = rs .get_rs_status (False , True )
135+ self .config_server = rs
136+ except Exception :
137+ self .config_server = {'host' : configdb_hosts [0 ]}
127138 finally :
128- if db :
129- db .close ()
130-
131- logging .debug ("Found sharding config server: %s" % (self .config_server ['host' ]))
139+ return self .config_server
140+ except Exception , e :
141+ raise e
132142 else :
133143 logging .fatal ("Unable to locate config servers for %s:%i!" % (self .host , self .port ))
134144 raise Exception , "Unable to locate config servers for %s:%i!" % (self .host , self .port ), None
0 commit comments