Mike Frysinger | d03e6b5 | 2019-08-03 12:49:01 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2 |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 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 | 001c4dd | 2016-02-24 13:55:44 -0800 | [diff] [blame] | 13 | import argparse |
Fang Deng | 5517a8a | 2015-03-11 17:23:45 -0700 | [diff] [blame] | 14 | import logging |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 15 | import socket |
| 16 | import subprocess |
Fang Deng | 5517a8a | 2015-03-11 17:23:45 -0700 | [diff] [blame] | 17 | import sys |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 18 | |
| 19 | import common |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 20 | from autotest_lib.frontend import database_settings_helper |
Fang Deng | 001c4dd | 2016-02-24 13:55:44 -0800 | [diff] [blame] | 21 | from autotest_lib.server import utils |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 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() |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 26 | |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 27 | def main_without_exception_handling(): |
Fang Deng | 5517a8a | 2015-03-11 17:23:45 -0700 | [diff] [blame] | 28 | database_settings = database_settings_helper.get_default_db_config() |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 29 | command = ['mysqlcheck', |
| 30 | '-o', database_settings['NAME'], |
| 31 | '-u', database_settings['USER'], |
| 32 | '-p%s' % database_settings['PASSWORD'], |
Fang Deng | e126351 | 2015-06-07 04:37:25 -0700 | [diff] [blame] | 33 | # we want to do db optimation on each master/slave |
| 34 | # in rotation. Do not write otimize table to bin log |
| 35 | # so that it won't be picked up by slaves automatically |
| 36 | '--skip-write-binlog', |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 37 | ] |
| 38 | subprocess.check_call(command) |
| 39 | |
| 40 | |
Fang Deng | 001c4dd | 2016-02-24 13:55:44 -0800 | [diff] [blame] | 41 | def should_optimize(): |
| 42 | """Check if the server should run db_optimize. |
| 43 | |
| 44 | Only shard should optimize db. |
| 45 | |
| 46 | @returns: True if it should optimize db otherwise False. |
| 47 | """ |
| 48 | return utils.is_shard() |
| 49 | |
| 50 | |
| 51 | def parse_args(): |
| 52 | """Parse command line arguments""" |
| 53 | parser = argparse.ArgumentParser() |
| 54 | parser.add_argument('-c', '--check_server', action='store_true', |
| 55 | help='Check if the server should optimize db.') |
| 56 | return parser.parse_args() |
| 57 | |
| 58 | |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 59 | def main(): |
Fang Deng | 001c4dd | 2016-02-24 13:55:44 -0800 | [diff] [blame] | 60 | """Main.""" |
| 61 | args = parse_args() |
| 62 | |
Fang Deng | 5517a8a | 2015-03-11 17:23:45 -0700 | [diff] [blame] | 63 | logging.basicConfig(level=logging.INFO, format=LOGGING_FORMAT) |
| 64 | logging.info('Calling: %s', sys.argv) |
Fang Deng | 001c4dd | 2016-02-24 13:55:44 -0800 | [diff] [blame] | 65 | |
| 66 | if args.check_server and not should_optimize(): |
| 67 | print 'Only shard can run db optimization.' |
| 68 | return |
| 69 | |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 70 | try: |
| 71 | main_without_exception_handling() |
| 72 | except Exception as e: |
| 73 | message = 'Uncaught exception; terminating db_optimize.' |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 74 | logging.exception(message) |
| 75 | raise |
Fang Deng | 5517a8a | 2015-03-11 17:23:45 -0700 | [diff] [blame] | 76 | logging.info('db_optimize completed.') |
Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 77 | |
| 78 | |
| 79 | if __name__ == '__main__': |
| 80 | main() |