11# -*- coding: utf-8 -*-
2+ import logging
23from time import sleep
34from dbaas_credentials .models import CredentialType
45from util import get_credentials_for
78CHANGE_MASTER_ATTEMPS = 30
89CHANGE_MASTER_SECONDS = 15
910
11+ LOG = logging .getLogger (__name__ )
12+
1013
1114class HostStatus (object ):
1215 @staticmethod
@@ -159,33 +162,47 @@ class ChangeMasterTemporaryInstance(ChangeMaster):
159162
160163 @property
161164 def is_valid (self ):
162- if self .instance .temporary or self .check_master_is_temporary ():
165+ master_temporary = self .check_master_is_temporary ()
166+ # so executa para a VM tepmoraria, e se a Master nao eh temporaria
167+ if not self .instance .temporary or master_temporary :
163168 return False
164- return super (ChangeMasterTemporaryInstance , self ).is_valid
165169
166- def check_master_is_temporary (self ):
170+ return True
171+
172+ def check_master_is_temporary (self , wait_seconds = 0 ):
173+ LOG .info ("Checking master is temporary instance" )
174+ LOG .debug ("Willl sleep for %s seconds before checking" , wait_seconds )
175+ sleep (wait_seconds )
176+
167177 master = self .driver .get_master_instance ()
168- if master .temporary :
169- return True
170- return False
178+ LOG .info ("Master instance is %s" , master )
179+ LOG .info ("Master is temporary? %s" , master .temporary )
180+
181+ if master is None or not master .temporary :
182+ return False
183+
184+ return True
171185
172186 def change_master (self ):
173187 error = None
174188
175189 for _ in range (CHANGE_MASTER_ATTEMPS ):
176- if self .is_slave :
177- return
190+ error = None
178191 try :
179- self .driver .check_replication_and_switch (self .target_instance )
180- if not self .check_master_is_temporary ():
192+ LOG .info ("Trying to change master. Attempt %s" , _ )
193+ self .driver .check_replication_and_switch_with_stepdown_time (self .target_instance , stepdown_time = 300 )
194+ master_is_temporary = self .check_master_is_temporary (wait_seconds = 60 )
195+
196+ if not master_is_temporary :
181197 raise Exception ('Master is not the temporary instance' )
198+
199+ return
182200 except Exception as e :
183201 error = e
184202 sleep (CHANGE_MASTER_SECONDS )
185- else :
186- return
187203
188- raise error
204+ if error is not None :
205+ raise error
189206
190207 def do (self ):
191208 if not self .is_valid :
@@ -206,19 +223,18 @@ def change_master(self):
206223 error = None
207224
208225 for _ in range (CHANGE_MASTER_ATTEMPS ):
209- if self .is_slave :
210- return
211226 try :
212227 self .driver .check_replication_and_switch (self .target_instance )
213- if self .check_master_is_temporary ():
228+ if self .check_master_is_temporary (wait_seconds = 60 ):
214229 raise Exception ('Master is the temporary instance' )
230+
231+ return
215232 except Exception as e :
216233 error = e
217234 sleep (CHANGE_MASTER_SECONDS )
218- else :
219- return
220235
221- raise error
236+ if error is not None :
237+ raise error
222238
223239
224240class ChangeMasterDatabaseMigrate (ChangeMaster ):
0 commit comments