Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 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 | |
Fang Deng | 5517a8a | 2015-03-11 17:23:45 -0700 | [diff] [blame] | 13 | import logging |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 14 | import socket |
| 15 | import subprocess |
Fang Deng | 5517a8a | 2015-03-11 17:23:45 -0700 | [diff] [blame] | 16 | import sys |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 17 | |
| 18 | import common |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 19 | from autotest_lib.client.common_lib.cros.graphite import autotest_stats |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 20 | from autotest_lib.frontend import database_settings_helper |
| 21 | from autotest_lib.scheduler import email_manager |
| 22 | |
Fang Deng | 5517a8a | 2015-03-11 17:23:45 -0700 | [diff] [blame] | 23 | # Format Appears as: [Date] [Time] - [Msg Level] - [Message] |
| 24 | LOGGING_FORMAT = '%(asctime)s - %(levelname)s - %(message)s' |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 25 | STATS_KEY = 'db_optimize.%s' % socket.gethostname() |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 26 | timer = autotest_stats.Timer(STATS_KEY) |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 27 | |
| 28 | @timer.decorate |
| 29 | def main_without_exception_handling(): |
Fang Deng | 5517a8a | 2015-03-11 17:23:45 -0700 | [diff] [blame] | 30 | database_settings = database_settings_helper.get_default_db_config() |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 31 | command = ['mysqlcheck', |
| 32 | '-o', database_settings['NAME'], |
| 33 | '-u', database_settings['USER'], |
| 34 | '-p%s' % database_settings['PASSWORD'], |
Fang Deng | e126351 | 2015-06-07 04:37:25 -0700 | [diff] [blame] | 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', |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 39 | ] |
| 40 | subprocess.check_call(command) |
| 41 | |
| 42 | |
| 43 | def main(): |
Fang Deng | 5517a8a | 2015-03-11 17:23:45 -0700 | [diff] [blame] | 44 | logging.basicConfig(level=logging.INFO, format=LOGGING_FORMAT) |
| 45 | logging.info('Calling: %s', sys.argv) |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 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() |
Fang Deng | 5517a8a | 2015-03-11 17:23:45 -0700 | [diff] [blame] | 55 | logging.info('db_optimize completed.') |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 56 | |
| 57 | |
| 58 | if __name__ == '__main__': |
| 59 | main() |