Skip to content

Commit

Permalink
Add size/performance data to optimization output
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelley Reynolds committed Jan 13, 2015
1 parent ea47cb1 commit c7b66ea
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 deletions ginnoptimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,30 @@
import sys
import time
from colorama import init, Fore
from datetime import datetime

hostname, username, password = ['', '', '']


def sizeof_fmt(num, suffix='B'):
"""@todo: Docstring for sizeof_fmt
:num: size in bytes
:type num: int
:suffix: str
:type suffix: str
:returns:
:rtype: return a human-readable string
"""
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)


def print_color(mtype, message=''):
"""@todo: Docstring for print_text.
Expand All @@ -33,13 +53,13 @@ def print_color(mtype, message=''):

init(autoreset=True)
if (mtype == 'ok'):
print(Fore.GREEN + 'OK')
print(Fore.GREEN + 'OK' + Fore.RESET + message)
elif (mtype == '+'):
print('[+] ' + message + '...'),
elif (mtype == 'fail'):
print(Fore.RED + "\n[!]" + message)
elif (mtype == 'sub'):
print(' -> ' + message + '...'),
print((' -> ' + message).ljust(65, '.')),
elif (mtype == 'subsub'):
print("\n -> " + message + '...'),
elif (mtype == 'up'):
Expand Down Expand Up @@ -106,7 +126,7 @@ def get_sorted_tables_by_size(dbname):

print_color('+', "Getting list of all tables in " + dbname + " database")
tables_list = sql_query([
'SELECT TABLE_NAME FROM information_schema.TABLES \
'SELECT TABLE_NAME, (data_length + index_length) AS size FROM information_schema.TABLES \
WHERE table_schema = "' + dbname + '"\
ORDER BY (data_length + index_length);'],
True)
Expand Down Expand Up @@ -158,7 +178,21 @@ def optimize_rsu(dbname, tables_list, fcpmax):
"""

def launch_sql_queries(table):
def print_formatted_results(optimize_start, table_size):
"""
Print OK along with some optimization performance data
:optimize_start: time of optimization start
:type optimize_start: datetime
:table_size: size of table/partition
:type table_size: int
"""

time_spent = (datetime.now() - optimize_start).total_seconds()
print_color('ok', ' (' + '{:.1f}'.format(time_spent) + 's; ' + sizeof_fmt(table_size/time_spent) + '/s)')

def launch_sql_queries(table, size):
"""
Launch SQL optimize on a table
If fail during optimize, will simply go to the next one after warning
Expand All @@ -177,14 +211,16 @@ def launch_sql_queries(table):
partitions = ptables[0][3].split(',')

# Launching query
print_color('sub', 'optimizing ' + table + ' in progress')
print_color('sub', 'optimizing ' + table + ' (' + sizeof_fmt(size) + ') in progress')
if len(partitions) == 1:
start_time = datetime.now()
sql_query(['SET wsrep_on=OFF;',
'optimize table ' + dbname + '.' + table + ';'],
False, False)
print_color('ok')
print_formatted_results(start_time, size)
else:
for partition in partitions:
start_time = datetime.now()
print_color('subsub', 'partition ' + partition +
' in progress')
print('ALTER ONLINE TABLE ' + dbname + '.' + table +
Expand All @@ -193,17 +229,16 @@ def launch_sql_queries(table):
'ALTER ONLINE TABLE ' + dbname + '.' + table +
' REBUILD PARTITION ' + partition + ';'],
False, False)
print_color('ok')
print_formatted_results(start_time, size)
get_wsrep_fcp(fcpmax)

# Optimize each tables
enable_rsu()
print_color('+', 'Starting optimization on ' + dbname + ' database')
print ''
for row in tables_list:
for table in row:
get_wsrep_fcp(fcpmax)
launch_sql_queries(table)
get_wsrep_fcp(fcpmax)
launch_sql_queries(row[0], row[1])
restore_toi()


Expand Down Expand Up @@ -318,7 +353,7 @@ def check_and_set_param(query, param_name, value, set_param):

# Optional but required checks
check_and_set_param('SHOW GLOBAL VARIABLES LIKE "wsrep_desync";',
'wsrep_desync', 'OFF', 'SET GLOBAL wsrep_desync=OFF;')
'wsrep_desync', 'OFF', 'SET GLOBAL wsrep_desync=0;')
check_and_set_param('SHOW GLOBAL VARIABLES LIKE "wsrep_OSU_method";',
'wsrep_OSU_method', 'TOI',
'SET GLOBAL wsrep_OSU_method="TOI";')
Expand Down

0 comments on commit c7b66ea

Please sign in to comment.