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'], |
| 35 | ] |
| 36 | subprocess.check_call(command) |
| 37 | |
| 38 | |
| 39 | def main(): |
Fang Deng | 5517a8a | 2015-03-11 17:23:45 -0700 | [diff] [blame^] | 40 | logging.basicConfig(level=logging.INFO, format=LOGGING_FORMAT) |
| 41 | logging.info('Calling: %s', sys.argv) |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 42 | try: |
| 43 | main_without_exception_handling() |
| 44 | except Exception as e: |
| 45 | message = 'Uncaught exception; terminating db_optimize.' |
| 46 | email_manager.manager.log_stacktrace(message) |
| 47 | logging.exception(message) |
| 48 | raise |
| 49 | finally: |
| 50 | email_manager.manager.send_queued_emails() |
Fang Deng | 5517a8a | 2015-03-11 17:23:45 -0700 | [diff] [blame^] | 51 | logging.info('db_optimize completed.') |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 52 | |
| 53 | |
| 54 | if __name__ == '__main__': |
| 55 | main() |