blob: 6b4dce2f1b71b277a705cca492fab4c21ff2112e [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
21from autotest_lib.scheduler import email_manager
Fang Deng001c4dd2016-02-24 13:55:44 -080022from autotest_lib.server import utils
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070023
Fang Deng5517a8a2015-03-11 17:23:45 -070024# Format Appears as: [Date] [Time] - [Msg Level] - [Message]
25LOGGING_FORMAT = '%(asctime)s - %(levelname)s - %(message)s'
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070026STATS_KEY = 'db_optimize.%s' % socket.gethostname()
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070027
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070028def main_without_exception_handling():
Fang Deng5517a8a2015-03-11 17:23:45 -070029 database_settings = database_settings_helper.get_default_db_config()
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070030 command = ['mysqlcheck',
31 '-o', database_settings['NAME'],
32 '-u', database_settings['USER'],
33 '-p%s' % database_settings['PASSWORD'],
Fang Denge1263512015-06-07 04:37:25 -070034 # we want to do db optimation on each master/slave
35 # in rotation. Do not write otimize table to bin log
36 # so that it won't be picked up by slaves automatically
37 '--skip-write-binlog',
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070038 ]
39 subprocess.check_call(command)
40
41
Fang Deng001c4dd2016-02-24 13:55:44 -080042def should_optimize():
43 """Check if the server should run db_optimize.
44
45 Only shard should optimize db.
46
47 @returns: True if it should optimize db otherwise False.
48 """
49 return utils.is_shard()
50
51
52def parse_args():
53 """Parse command line arguments"""
54 parser = argparse.ArgumentParser()
55 parser.add_argument('-c', '--check_server', action='store_true',
56 help='Check if the server should optimize db.')
57 return parser.parse_args()
58
59
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070060def main():
Fang Deng001c4dd2016-02-24 13:55:44 -080061 """Main."""
62 args = parse_args()
63
Fang Deng5517a8a2015-03-11 17:23:45 -070064 logging.basicConfig(level=logging.INFO, format=LOGGING_FORMAT)
65 logging.info('Calling: %s', sys.argv)
Fang Deng001c4dd2016-02-24 13:55:44 -080066
67 if args.check_server and not should_optimize():
68 print 'Only shard can run db optimization.'
69 return
70
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070071 try:
72 main_without_exception_handling()
73 except Exception as e:
74 message = 'Uncaught exception; terminating db_optimize.'
75 email_manager.manager.log_stacktrace(message)
76 logging.exception(message)
77 raise
78 finally:
79 email_manager.manager.send_queued_emails()
Fang Deng5517a8a2015-03-11 17:23:45 -070080 logging.info('db_optimize completed.')
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070081
82
83if __name__ == '__main__':
84 main()