1 #!/usr/bin/python 2 # Copyright (c) 2014 The Chromium OS Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 """This script will run optimize table for chromeos_autotest_db 7 8 This script might have notable impact on the mysql performance as it locks 9 tables and rebuilds indexes. So be careful when running it on production 10 systems. 11 """ 12 13 import logging 14 import socket 15 import subprocess 16 import sys 17 18 import common 19 from autotest_lib.client.common_lib.cros.graphite import autotest_stats 20 from autotest_lib.frontend import database_settings_helper 21 from autotest_lib.scheduler import email_manager 22 23 # Format Appears as: [Date] [Time] - [Msg Level] - [Message] 24 LOGGING_FORMAT = '%(asctime)s - %(levelname)s - %(message)s' 25 STATS_KEY = 'db_optimize.%s' % socket.gethostname() 26 timer = autotest_stats.Timer(STATS_KEY) 27 28 @timer.decorate 29 def main_without_exception_handling(): 30 database_settings = database_settings_helper.get_default_db_config() 31 command = ['mysqlcheck', 32 '-o', database_settings['NAME'], 33 '-u', database_settings['USER'], 34 '-p%s' % database_settings['PASSWORD'], 35 # we want to do db optimation on each master/slave 36 # in rotation. Do not write otimize table to bin log 37 # so that it won't be picked up by slaves automatically 38 '--skip-write-binlog', 39 ] 40 subprocess.check_call(command) 41 42 43 def main(): 44 logging.basicConfig(level=logging.INFO, format=LOGGING_FORMAT) 45 logging.info('Calling: %s', sys.argv) 46 try: 47 main_without_exception_handling() 48 except Exception as e: 49 message = 'Uncaught exception; terminating db_optimize.' 50 email_manager.manager.log_stacktrace(message) 51 logging.exception(message) 52 raise 53 finally: 54 email_manager.manager.send_queued_emails() 55 logging.info('db_optimize completed.') 56 57 58 if __name__ == '__main__': 59 main() 60