@@ -38,56 +38,74 @@ def close(self):
3838 self .config_db .close ()
3939 return self .restore_balancer_state ()
4040
41+ def is_gte_34 (self ):
42+ return self .db .server_version () >= tuple ("3.4.0" .split ("." ))
43+
4144 def get_start_state (self ):
4245 self ._balancer_state_start = self .get_balancer_state ()
4346 logging .info ("Began with balancer state running: %s" % str (self ._balancer_state_start ))
4447 return self ._balancer_state_start
4548
4649 def shards (self ):
4750 try :
48- if self .db .is_configsvr () and self .db .server_version () < tuple ("3.4.0" .split ("." )):
49- return self .connection ['config' ].shards .find ()
50- else :
51+ if self .is_gte_34 ():
5152 listShards = self .db .admin_command ("listShards" )
5253 if 'shards' in listShards :
5354 return listShards ['shards' ]
55+ elif self .db .is_configsvr ():
56+ return self .connection ['config' ].shards .find ()
5457 except Exception , e :
5558 raise DBOperationError (e )
5659
5760 def check_balancer_running (self ):
5861 try :
59- config = self .connection ['config' ]
60- lock = config ['locks' ].find_one ({'_id' : 'balancer' })
61- if 'state' in lock and int (lock ['state' ]) == 0 :
62- return False
62+ if self .is_gte_34 ():
63+ balancerState = self .db .admin_command ("balancerStatus" )
64+ if 'inBalancerRound' in balancerState :
65+ return balancerState ['inBalancerRound' ]
66+ else :
67+ config = self .connection ['config' ]
68+ lock = config ['locks' ].find_one ({'_id' : 'balancer' })
69+ if 'state' in lock and int (lock ['state' ]) == 0 :
70+ return False
6371 return True
6472 except Exception , e :
6573 raise DBOperationError (e )
6674
6775 def get_balancer_state (self ):
6876 try :
69- config = self .connection ['config' ]
70- state = config ['settings' ].find_one ({'_id' : 'balancer' })
71-
72- if not state :
73- return True
74- elif 'stopped' in state and state .get ('stopped' ) is True :
75- return False
77+ if self .is_gte_34 ():
78+ balancerState = self .db .admin_command ("balancerStatus" )
79+ if 'mode' in balancerState and balancerState ['mode' ] == 'off' :
80+ return False
81+ return True
7682 else :
77- return True
83+ config = self .connection ['config' ]
84+ state = config ['settings' ].find_one ({'_id' : 'balancer' })
85+ if not state :
86+ return True
87+ elif 'stopped' in state and state .get ('stopped' ) is True :
88+ return False
89+ return True
7890 except Exception , e :
7991 raise DBOperationError (e )
8092
8193 def set_balancer (self , value ):
8294 try :
83- if value is True :
84- set_value = False
85- elif value is False :
86- set_value = True
95+ if self .is_gte_34 ():
96+ if value is True :
97+ self .db .admin_command ("balancerStart" )
98+ else :
99+ self .db .admin_command ("balancerStop" )
87100 else :
88- set_value = True
89- config = self .connection ['config' ]
90- config ['settings' ].update_one ({'_id' : 'balancer' }, {'$set' : {'stopped' : set_value }})
101+ if value is True :
102+ set_value = False
103+ elif value is False :
104+ set_value = True
105+ else :
106+ set_value = True
107+ config = self .connection ['config' ]
108+ config ['settings' ].update_one ({'_id' : 'balancer' }, {'$set' : {'stopped' : set_value }})
91109 except Exception , e :
92110 logging .fatal ("Failed to set balancer state! Error: %s" % e )
93111 raise DBOperationError (e )
0 commit comments