def restore(self): # if "restore_sys_time" in self._scenario.tags: # utils.restore_sys_time() # # if "aft_reset_replication" in self._scenario.tags: # utils.reset_repl() # if "restore_mysql_service" in self._scenario.tags: params_dic = self.get_tag_params("{'restore_mysql_service'") if params_dic: paras = params_dic["restore_mysql_service"] else: paras = {} logger.debug("try to restore_mysql_service: {0}".format(paras)) for host_name, mysql_vars in paras.items(): logger.debug("the value of host_name is: {0}".format(host_name)) for k, v in mysql_vars.items(): logger.debug("the value of v is: {0}".format(v)) if v: mysql = ObjectFactory.create_mysql_object(host_name) mysql.start() if "restore_global_setting" in self._scenario.tags: params_dic = self.get_tag_params("{'restore_global_setting'") if params_dic: paras = params_dic["restore_global_setting"] else: paras = {} logger.debug("try to restore_global_setting of mysqls: {0}".format(paras)) for mysql, mysql_vars in paras.items(): query = "set global " for k, v in mysql_vars.items(): query = query + "{0}={1},".format(k, v) sql = query[:-1]#delete the last ',' execute_sql_in_host(mysql, {"sql": sql}) if "restore_mysql_config" in self._scenario.tags: params_dic = self.get_tag_params("{'restore_mysql_config'") if params_dic: paras = params_dic["restore_mysql_config"] else: paras = {} logger.debug("try to restore_mysql_config of mysqls: {0}".format(paras)) for host_name, mysql_vars in paras.items(): sed_str = "" for k, v in mysql_vars.items(): sed_str += "/{0}/d\n/server-id/a {0}={1}\n".format(k, v) mysql = ObjectFactory.create_mysql_object(host_name) mysql.restart(sed_str)
def execute_sql_in_host(host_name, info_dic, mode="mysql"): if mode in ["admin", "user"]: #query to dble obj = ObjectFactory.create_dble_object(host_name) query_meta = QueryMeta(info_dic, mode, obj._dble_meta) else: obj = ObjectFactory.create_mysql_object(host_name) query_meta = QueryMeta(info_dic, mode, obj._mysql_meta) pre_delegater = PreQueryPrepare(query_meta) pre_delegater.prepare() res, err, time_cost = obj.do_execute_query(query_meta) post_delegater = PostQueryCheck(res, err, time_cost, query_meta) post_delegater.check_result() return res, err
def stop_mysql(context, host_name): mysql = ObjectFactory.create_mysql_object(host_name) # this is temp for debug stop mysql fail execute_sql_in_host(host_name, {'sql': 'show processlist'}) # end debug stop mysql fail mysql.stop()
def step_impl(context, host_name, exclude_conn_ids=None): if exclude_conn_ids: exclude_ids = getattr(context, exclude_conn_ids, None) else: exclude_ids = [] mysql = ObjectFactory.create_mysql_object(host_name) mysql.kill_all_conns(exclude_ids)
def restart_mysql(context, host_name, sed_str=None): if not sed_str and context.text is not None and len(context.text) > 0: sed_str = context.text mysql = ObjectFactory.create_mysql_object(host_name) # this is temp for debug stop mysql fail execute_sql_in_host(host_name, {'sql': 'show processlist'}) # end debug stop mysql fail mysql.restart(sed_str)
def check_result(self): while True: if self._expect == "success": logger.debug("sql:{},err:{}".format(self._sql, self._real_err)) assert self._real_err is None, "expect no err, but outcomes '{0}'".format( self._real_err) break dest_host = re.search(r'dest_node:(.*)', self._expect, re.I) if dest_host: shardings_host = dest_host.group(1) if self._sql.lower().startswith("insert into"): target = self._sql.split("values")[1] else: target = self._sql mysql = ObjectFactory.create_mysql_object(shardings_host) mysql.check_query_in_general_log(target, expect_exist=True) break hasObj = re.search(r"has\{(.*?)\}", self._expect, re.I) if hasObj: assert_that( self._real_err is None, "expect query success, but failed for '{0}'".format( self._real_err)) expectRS = hasObj.group(1) logger.debug("expect resultset:{0}, real res:{1}".format( eval(expectRS), self._real_res)) self.hasResultSet(self._real_res, expectRS, True) break hasnotObj = re.search(r"hasnot\{(.*?)\}", self._expect, re.I) if hasnotObj: assert_that( self._real_err is None, "expect query success, but failed for '{0}'".format( self._real_err)) notExpectRS = hasnotObj.group(1) logger.debug("not expect resultset:{0}, real res:{1}".format( eval(notExpectRS), self._real_res)) self.hasResultSet(self._real_res, notExpectRS, False) break lengthObj = re.search(r"length\{(.*?)\}", self._expect, re.I) if lengthObj: assert_that( self._real_err is None, "expect query success, but failed for '{0}'".format( self._real_err)) expectRS = lengthObj.group(1) logger.debug( "expect resultset:{0}, length equal to real res length:{1}" .format(eval(expectRS), len(self._real_res))) assert_that(len(self._real_res), equal_to(eval(expectRS)), "sql resultset records count is not as expected") break matchObj = re.search(r"match\{(.*?)\}", self._expect, re.I) if matchObj: assert_that( self._real_err is None, "expect query success, but failed for '{0}'".format( self._real_err)) match_Obj = matchObj.group(1) logger.debug("expect match_obj:{0}".format(match_Obj)) match_Obj_Split = re.split(r'[;,\s]', match_Obj.encode('ascii')) logger.debug( "expect match_Obj_Split:{0}".format(match_Obj_Split)) self.matchResultSet(self._real_res, match_Obj_Split, len(match_Obj_Split) - 1) break isBalance = re.search(r"balance\{(.*?)\}", self._expect, re.I) if isBalance: assert_that( self._real_err is None, "expect query success, but failed for '{0}'".format( self._real_err)) bal_num = isBalance.group(1) self.balance(self._real_res, int(bal_num)) break hasString = re.search(r"hasStr\{(.*?)\}", self._expect, re.I) if hasString: assert_that( self._real_err is None, "expect query success, but failed for '{0}'".format( self._real_err)) expectRS = hasString.group(1) assert_that( str(self._real_res), contains_string(str(expectRS)), "expect containing text: {0}, resultset:{1}".format( expectRS, self._real_res)) break hasNoString = re.search(r"hasNoStr\{(.*?)\}", self._expect, re.I) if hasNoString: assert_that( self._real_err is None, "expect query success, but failed for '{0}'".format( self._real_err)) notExpectRS = hasNoString.group(1) assert str(notExpectRS) not in str( self._real_res ), "not expect containing text: {0}, resultset:{1}".format( notExpectRS, self._real_res) break executeTime = re.search(r"execute_time\{(.*?)\}", self._expect, re.I) if executeTime: expectRS = executeTime.group(1) duration = self._time_cost.seconds logger.debug( "expect duration is :{0},real duration is{1} ".format( eval(expectRS), duration)) assert_that(duration, equal_to(eval(expectRS))) break if self._expect.lower() == "error totally whack": assert_that( self._real_err, not_none(), "exec sql:{} Err is None, expect:{}".format( self._sql, self._expect)) break assert_that( self._real_err, not_none(), "exec sql:{0} success, but expect failed for:{1}".format( self._sql, self._expect)) assert_that( self._real_err[1], contains_string(self._expect), "expect query failed for: {0}, real err:{1}".format( self._expect, self._real_err)) break
def restore(self): # if "restore_sys_time" in self._scenario.tags: # utils.restore_sys_time() # # if "aft_reset_replication" in self._scenario.tags: # utils.reset_repl() # if "restore_network" in self._scenario.tags: params_dic = self.get_tag_params("{'restore_network'") logger.debug("params_dic is: {0}".format(params_dic)) if params_dic: paras = params_dic["restore_network"].split(",") # paras = paras.split(",") else: paras = "" logger.debug("try to restore_network: {0}".format(paras)) for host_name in paras: logger.debug("the value of host_name is: {0}".format(host_name)) cmd = "iptables -F" ssh = get_ssh(host_name) rc, stdout, stderr = ssh.exec_command(cmd) assert_that(len(stderr) == 0, "restore network with command:{1}, got err:{0}".format(stderr, cmd)) if "restore_view" in self._scenario.tags: params_dic = self.get_tag_params("{'restore_view'") if params_dic: paras = params_dic["restore_view"] else: paras = {} for host_name, mysql_vars in paras.items(): if host_name.find('dble') != -1: mode = "user" else: mode = "mysql" for k, v in mysql_vars.items(): list_value = filter(lambda x: x, v.split(",")) view_value = "" for value in list_value: view_value = view_value + "{0}.{1},".format(k, value) query = "drop view if exists " + view_value sql = query[:-1] # delete the last ',' # logger.debug("the sql is: {0}".format(sql)) execute_sql_in_host(host_name, {"sql": sql}, mode) if "restore_mysql_service" in self._scenario.tags: params_dic = self.get_tag_params("{'restore_mysql_service'") if params_dic: paras = params_dic["restore_mysql_service"] else: paras = {} logger.debug("try to restore_mysql_service: {0}".format(paras)) for host_name, mysql_vars in paras.items(): logger.debug("the value of host_name is: {0}".format(host_name)) for k, v in mysql_vars.items(): if v: mysql = ObjectFactory.create_mysql_object(host_name) mysql.start() if "restore_global_setting" in self._scenario.tags: params_dic = self.get_tag_params("{'restore_global_setting'") if params_dic: paras = params_dic["restore_global_setting"] else: paras = {} logger.debug("try to restore_global_setting of mysqls: {0}".format(paras)) for mysql, mysql_vars in paras.items(): query = "set global " for k, v in mysql_vars.items(): query = query + "{0}={1},".format(k, v) sql = query[:-1] # delete the last ',' execute_sql_in_host(mysql, {"sql": sql}) if "restore_mysql_config" in self._scenario.tags: params_dic = self.get_tag_params("{'restore_mysql_config'") if params_dic: paras = params_dic["restore_mysql_config"] else: paras = {} logger.debug("try to restore_mysql_config of mysqls: {0}".format(paras)) for host_name, mysql_vars in paras.items(): sed_str = "" for k, v in mysql_vars.items(): m = ['log-bin', 'binlog_format', 'relay-log'] if k in m: sed_str += "/{0}/d\n".format(k) else: sed_str += "/{0}/d\n/server-id/a {0}={1}\n".format(k, v) mysql = ObjectFactory.create_mysql_object(host_name) mysql.restart(sed_str) if "restore_xa_recover" in self._scenario.tags: params_dic = self.get_tag_params("{'restore_xa_recover'") if params_dic: paras = params_dic["restore_xa_recover"] else: paras = {} logger.debug("try to restore_xa_recover of mysqls: {0}".format(paras)) for host_name in paras: if host_name.find('dble') != -1: mode = "user" else: mode = "mysql" logger.debug("the value of host_name is: {0}, mode: {1}".format(host_name, mode)) sql = "xa recover" res, err = execute_sql_in_host(host_name, {"sql": sql}, mode) if res: for data in res: sql = "xa rollback '{0}'".format(data[3]) execute_sql_in_host(host_name, {"sql": sql}, mode) if "delete_mysql_tables" in self._scenario.tags: params_dic = self.get_tag_params("{'delete_mysql_tables'") if params_dic: paras = params_dic["delete_mysql_tables"] else: paras = {} for host_name in paras.keys(): logger.debug("try to delete_mysql_tables of mysqls: {0}".format(host_name)) ssh = get_ssh(host_name) if host_name.find("dble") != -1: mode = "user" else: mode = "mysql" logger.debug("the value of host_name is: {0}, mode: {1}".format(host_name, mode)) # in case of program exits unexpected, tables.txt Residue in /tmp rc, stdout, stderr = ssh.exec_command("find /tmp -name tables.txt") if len(stdout) > 0: ssh.exec_command("rm -rf /tmp/tables.txt") for database in paras[host_name]: generate_drop_tables_sql = "select concat('drop table if exists ',table_name,';') from information_schema.TABLES where table_schema='{0}' into outfile '/tmp/tables.txt'".format(database) execute_sql_in_host(host_name, {"sql": generate_drop_tables_sql}, mode) # MySQLDB not support source grammar,replace with ssh cmd rc, stdout, stderr = ssh.exec_command("mysql -uroot -p111111 -D{0} -e 'source /tmp/tables.txt'".format(database)) stderr = stderr.lower() assert stderr.find("error") == -1, "deletet mysql in {0}:{1} failed, err: {2}".format(host_name, database, stderr) ssh.exec_command("rm -rf /tmp/tables.txt") logger.debug("{0} tables has been delete success".format(host_name)) logger.info("all required tables has been delete success")
def restore(self): # if "restore_sys_time" in self._scenario.tags: # utils.restore_sys_time() # # if "aft_reset_replication" in self._scenario.tags: # utils.reset_repl() # if "restore_network" in self._scenario.tags: params_dic = self.get_tag_params("{'restore_network'") logger.debug("params_dic is: {0}".format(params_dic)) if params_dic: paras = params_dic["restore_network"].split(",") # paras = paras.split(",") else: paras = "" logger.debug("try to restore_network: {0}".format(paras)) for host_name in paras: logger.debug( "the value of host_name is: {0}".format(host_name)) cmd = "iptables -F" ssh = get_ssh(host_name) rc, stdout, stderr = ssh.exec_command(cmd) assert_that( len(stderr) == 0, "restore network with command:{1}, got err:{0}".format( stderr, cmd)) if "restore_view" in self._scenario.tags: params_dic = self.get_tag_params("{'restore_view'") if params_dic: paras = params_dic["restore_view"] else: paras = {} for host_name, mysql_vars in paras.items(): if host_name.find('dble') != -1: mode = "user" else: mode = "mysql" for k, v in mysql_vars.items(): list_value = filter(lambda x: x, v.split(",")) view_value = "" for value in list_value: view_value = view_value + "{0}.{1},".format(k, value) query = "drop view if exists " + view_value sql = query[:-1] #delete the last ',' # logger.debug("the sql is: {0}".format(sql)) execute_sql_in_host(host_name, {"sql": sql}, mode) if "restore_mysql_service" in self._scenario.tags: params_dic = self.get_tag_params("{'restore_mysql_service'") if params_dic: paras = params_dic["restore_mysql_service"] else: paras = {} logger.debug("try to restore_mysql_service: {0}".format(paras)) for host_name, mysql_vars in paras.items(): logger.debug( "the value of host_name is: {0}".format(host_name)) for k, v in mysql_vars.items(): if v: mysql = ObjectFactory.create_mysql_object(host_name) mysql.start() if "restore_global_setting" in self._scenario.tags: params_dic = self.get_tag_params("{'restore_global_setting'") if params_dic: paras = params_dic["restore_global_setting"] else: paras = {} logger.debug( "try to restore_global_setting of mysqls: {0}".format(paras)) for mysql, mysql_vars in paras.items(): query = "set global " for k, v in mysql_vars.items(): query = query + "{0}={1},".format(k, v) sql = query[:-1] #delete the last ',' execute_sql_in_host(mysql, {"sql": sql}) if "restore_mysql_config" in self._scenario.tags: params_dic = self.get_tag_params("{'restore_mysql_config'") if params_dic: paras = params_dic["restore_mysql_config"] else: paras = {} logger.debug( "try to restore_mysql_config of mysqls: {0}".format(paras)) for host_name, mysql_vars in paras.items(): sed_str = "" for k, v in mysql_vars.items(): sed_str += "/{0}/d\n/server-id/a {0}={1}\n".format(k, v) mysql = ObjectFactory.create_mysql_object(host_name) mysql.restart(sed_str)
def prepare(self): need_check_sharding = re.search(r'dest_node:(.*)', self._expect, re.I) if need_check_sharding: shardings_host = need_check_sharding.group(1) mysql = ObjectFactory.create_mysql_object(shardings_host) mysql.turn_on_general_log()
def step_impl(context, host_name, query, occur_times_expr=None): mysql = ObjectFactory.create_mysql_object(host_name) mysql.check_query_in_general_log(query, expect_exist=True, expect_occur_times_expr=occur_times_expr)
def step_impl(context, host_name, query): mysql = ObjectFactory.create_mysql_object(host_name) mysql.check_query_in_general_log(query, expect_exist=False)
def turn_off_general_log(context, host_name): mysql = ObjectFactory.create_mysql_object(host_name) mysql.turn_off_general_log()
def step_impl(context, host_name): mysql = ObjectFactory.create_mysql_object(host_name) mysql.turn_on_general_log()
def start_mysql(context, host_name, sed_str=None): if not sed_str and context.text is not None and len(context.text) > 0: sed_str = context.text mysql = ObjectFactory.create_mysql_object(host_name) mysql.start(sed_str)
def step_impl(context, host_name, conn_ids): conn_ids = getattr(context, conn_ids, None) assert len(conn_ids) > 0, "no conns in '{}' to kill".format(conn_ids) mysql = ObjectFactory.create_mysql_object(host_name) mysql.kill_conns(conn_ids)