blob: ecaac73e5d647c0a80df972e071cd708e19d9614 [file] [log] [blame]
Mike Frysingerd03e6b52019-08-03 12:49:01 -04001#!/usr/bin/python2
Jakob Juelicha2cb2c12014-10-09 15:11:35 -07002# 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
8This script might have notable impact on the mysql performance as it locks
9tables and rebuilds indexes. So be careful when running it on production
10systems.
11"""
12
Fang Deng001c4dd2016-02-24 13:55:44 -080013import argparse
Fang Deng5517a8a2015-03-11 17:23:45 -070014import logging
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070015import socket
16import subprocess
Fang Deng5517a8a2015-03-11 17:23:45 -070017import sys
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070018
19import common
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070020from autotest_lib.frontend import database_settings_helper
Fang Deng001c4dd2016-02-24 13:55:44 -080021from autotest_lib.server import utils
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070022
Fang Deng5517a8a2015-03-11 17:23:45 -070023# Format Appears as: [Date] [Time] - [Msg Level] - [Message]
24LOGGING_FORMAT = '%(asctime)s - %(levelname)s - %(message)s'
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070025STATS_KEY = 'db_optimize.%s' % socket.gethostname()
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070026
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070027def main_without_exception_handling():
Fang Deng5517a8a2015-03-11 17:23:45 -070028 database_settings = database_settings_helper.get_default_db_config()
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070029 command = ['mysqlcheck',
30 '-o', database_settings['NAME'],
31 '-u', database_settings['USER'],
32 '-p%s' % database_settings['PASSWORD'],
Fang Denge1263512015-06-07 04:37:25 -070033 # 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 Juelicha2cb2c12014-10-09 15:11:35 -070037 ]
38 subprocess.check_call(command)
39
40
Fang Deng001c4dd2016-02-24 13:55:44 -080041def 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
51def 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 Juelicha2cb2c12014-10-09 15:11:35 -070059def main():
Fang Deng001c4dd2016-02-24 13:55:44 -080060 """Main."""
61 args = parse_args()
62
Fang Deng5517a8a2015-03-11 17:23:45 -070063 logging.basicConfig(level=logging.INFO, format=LOGGING_FORMAT)
64 logging.info('Calling: %s', sys.argv)
Fang Deng001c4dd2016-02-24 13:55:44 -080065
66 if args.check_server and not should_optimize():
67 print 'Only shard can run db optimization.'
68 return
69
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070070 try:
71 main_without_exception_handling()
72 except Exception as e:
73 message = 'Uncaught exception; terminating db_optimize.'
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070074 logging.exception(message)
75 raise
Fang Deng5517a8a2015-03-11 17:23:45 -070076 logging.info('db_optimize completed.')
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070077
78
79if __name__ == '__main__':
80 main()