def check_slave_is_down(connection=None): """Raise an StandardError in case the slave is not correctly down.""" if connection is None: connection = get_connection_for_dump_on_slave() res = run_sql("SHOW SLAVE STATUS", with_dict=True, connection=connection) if res[0]['Slave_SQL_Running'] != 'No': raise StandardError("Slave_SQL_Running is not set to 'No'")
def attach_slave(connection=None): """Attach the slave.""" if connection is None: connection = get_connection_for_dump_on_slave() connection.ping(1) run_sql("START SLAVE", connection=connection) check_slave_is_up(connection)
def detach_slave(connection=None): """Detach the slave.""" if connection is None: connection = get_connection_for_dump_on_slave() connection.ping(1) run_sql("STOP SLAVE SQL_THREAD", connection=connection) check_slave_is_down(connection)
def check_slave_is_in_consistent_state(connection=None): """ Check if the slave is already aware that dbdump task is running. dbdump being a monotask, guarantee that no other task is currently running and it's hence safe to detach the slave and start the actual dump. """ if connection is None: connection = get_connection_for_dump_on_slave() i = 0 ## Let's take the current status of dbdump (e.g. RUNNING, ABOUT TO STOP, etc.)... current_status = run_sql("SELECT status FROM schTASK WHERE id=%s", (task_get_task_param('task_id'), ))[0][0] while True: if i == 10: ## Timeout!! raise StandardError("The slave seems not to pick up with the master") ## ...and let's see if it matches with what the slave sees. if run_sql("SELECT status FROM schTASK WHERE id=%s AND status=%s", (task_get_task_param('task_id'), current_status), connection=connection): ## Bingo! return time.sleep(3) i += 1
def attach_slave(connection=None): """Attach the slave.""" if connection is None: connection = get_connection_for_dump_on_slave() run_sql("START SLAVE", connection=connection) check_slave_is_up(connection)
def detach_slave(connection=None): """Detach the slave.""" if connection is None: connection = get_connection_for_dump_on_slave() run_sql("STOP SLAVE SQL_THREAD", connection=connection) check_slave_is_down(connection)
def _dbdump_run_task_core(): """ Run DB dumper core stuff. Note: do not use task_can_sleep() stuff here because we don't want other tasks to interrupt us while we are dumping the DB content. """ # read params: host = CFG_DATABASE_HOST port = CFG_DATABASE_PORT connection = None try: if task_get_option('slave') and not task_get_option('dump_on_slave_helper_mode'): connection = get_connection_for_dump_on_slave() write_message("Dump on slave requested") write_message("... checking if slave is well up...") check_slave_is_up(connection) write_message("... checking if slave is in consistent state...") check_slave_is_in_consistent_state(connection) write_message("... detaching slave database...") detach_slave(connection) write_message("... scheduling dump on slave helper...") helper_arguments = [] if task_get_option("number"): helper_arguments += ["--number", str(task_get_option("number"))] if task_get_option("output"): helper_arguments += ["--output", str(task_get_option("output"))] if task_get_option("params"): helper_arguments += ["--params", str(task_get_option("params"))] if task_get_option("ignore_tables"): helper_arguments += ["--ignore-tables", str(task_get_option("ignore_tables"))] if task_get_option("compress"): helper_arguments += ["--compress"] if task_get_option("slave"): helper_arguments += ["--slave", str(task_get_option("slave"))] helper_arguments += ['-N', 'slavehelper', '--dump-on-slave-helper'] task_id = task_low_level_submission('dbdump', task_get_task_param('user'), '-P4', *helper_arguments) write_message("Slave scheduled with ID %s" % task_id) task_update_progress("DONE") return True elif task_get_option('dump_on_slave_helper_mode'): write_message("Dumping on slave mode") connection = get_connection_for_dump_on_slave() write_message("... checking if slave is well down...") check_slave_is_down(connection) host = CFG_DATABASE_SLAVE task_update_progress("Reading parameters") write_message("Reading parameters started") output_dir = task_get_option('output', CFG_LOGDIR) output_num = task_get_option('number', 5) params = task_get_option('params', None) compress = task_get_option('compress', False) slave = task_get_option('slave', False) ignore_tables = task_get_option('ignore_tables', None) if ignore_tables: ignore_tables = get_table_names(ignore_tables) else: ignore_tables = None output_file_suffix = task_get_task_param('task_starting_time') output_file_suffix = output_file_suffix.replace(' ', '_') + '.sql' if compress: output_file_suffix = "%s.gz" % (output_file_suffix,) write_message("Reading parameters ended") # make dump: task_update_progress("Dumping database") write_message("Database dump started") if slave: output_file_prefix = 'slave-%s-dbdump-' % (CFG_DATABASE_NAME,) else: output_file_prefix = '%s-dbdump-' % (CFG_DATABASE_NAME,) output_file = output_file_prefix + output_file_suffix dump_path = output_dir + os.sep + output_file dump_database(dump_path, \ host=host, port=port, params=params, \ compress=compress, \ ignore_tables=ignore_tables) write_message("Database dump ended") finally: if connection and task_get_option('dump_on_slave_helper_mode'): write_message("Reattaching slave") attach_slave(connection) # prune old dump files: task_update_progress("Pruning old dump files") write_message("Pruning old dump files started") _delete_old_dumps(output_dir, output_file_prefix, output_num) write_message("Pruning old dump files ended") # we are done: task_update_progress("Done.") return True