blob: bf946aba2ba24bf96da76492f0340fe595efb9c5 [file] [log] [blame]
Jakob Juelicha2cb2c12014-10-09 15:11:35 -07001#!/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
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 Deng5517a8a2015-03-11 17:23:45 -070013import logging
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070014import socket
15import subprocess
Fang Deng5517a8a2015-03-11 17:23:45 -070016import sys
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070017
18import common
Gabe Black1e1c41b2015-02-04 23:55:15 -080019from autotest_lib.client.common_lib.cros.graphite import autotest_stats
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070020from autotest_lib.frontend import database_settings_helper
21from autotest_lib.scheduler import email_manager
22
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()
Gabe Black1e1c41b2015-02-04 23:55:15 -080026timer = autotest_stats.Timer(STATS_KEY)
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070027
28@timer.decorate
29def main_without_exception_handling():
Fang Deng5517a8a2015-03-11 17:23:45 -070030 database_settings = database_settings_helper.get_default_db_config()
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070031 command = ['mysqlcheck',
32 '-o', database_settings['NAME'],
33 '-u', database_settings['USER'],
34 '-p%s' % database_settings['PASSWORD'],
Fang Denge1263512015-06-07 04:37:25 -070035 # 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 Juelicha2cb2c12014-10-09 15:11:35 -070039 ]
40 subprocess.check_call(command)
41
42
43def main():
Fang Deng5517a8a2015-03-11 17:23:45 -070044 logging.basicConfig(level=logging.INFO, format=LOGGING_FORMAT)
45 logging.info('Calling: %s', sys.argv)
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070046 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 Deng5517a8a2015-03-11 17:23:45 -070055 logging.info('db_optimize completed.')
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070056
57
58if __name__ == '__main__':
59 main()