Home | History | Annotate | Download | only in contrib
      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 
     13 import argparse
     14 import logging
     15 import socket
     16 import subprocess
     17 import sys
     18 
     19 import common
     20 from autotest_lib.frontend import database_settings_helper
     21 from autotest_lib.scheduler import email_manager
     22 from autotest_lib.server import utils
     23 
     24 # Format Appears as: [Date] [Time] - [Msg Level] - [Message]
     25 LOGGING_FORMAT = '%(asctime)s - %(levelname)s - %(message)s'
     26 STATS_KEY = 'db_optimize.%s' % socket.gethostname()
     27 
     28 def main_without_exception_handling():
     29     database_settings = database_settings_helper.get_default_db_config()
     30     command = ['mysqlcheck',
     31                '-o', database_settings['NAME'],
     32                '-u', database_settings['USER'],
     33                '-p%s' % database_settings['PASSWORD'],
     34                # 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',
     38                ]
     39     subprocess.check_call(command)
     40 
     41 
     42 def 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 
     52 def 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 
     60 def main():
     61     """Main."""
     62     args = parse_args()
     63 
     64     logging.basicConfig(level=logging.INFO, format=LOGGING_FORMAT)
     65     logging.info('Calling: %s', sys.argv)
     66 
     67     if args.check_server and not should_optimize():
     68         print 'Only shard can run db optimization.'
     69         return
     70 
     71     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()
     80     logging.info('db_optimize completed.')
     81 
     82 
     83 if __name__ == '__main__':
     84     main()
     85