#!/bin/sh CACHE_NODES=auto SCRIPT_DIR=`dirname $0` disable_gcache_purging() { echo "$CACHE_NODES" \ | while read CACHE_NODE ; do echo -n "`date +%Y%m%d-%H%M%S` Disabling purging galera cache... " mysql -h$CACHE_NODE "$DB" -e "SET GLOBAL wsrep_provider_options='gcache.freeze_purge_at_seqno = now'" if [ "$?" -eq "0" ] ; then echo "done" else echo "failed" exit 1 fi done } enable_cluster_purging() { echo "$CACHE_NODES" \ | while read CACHE_NODE ; do echo -n "`date +%Y%m%d-%H%M%S` Enabling purging galera cache... " mysql -h$CACHE_NODE mysql -e "SET GLOBAL wsrep_provider_options='gcache.freeze_purge_at_seqno = -1'" if [ "$?" -eq "0" ] ; then echo "done" else echo "failed" exit 1 fi done } wait_node_synced() { echo -n "`date +%Y%m%d-%H%M%S` Checking cluster state..." while true ; do state=`mysql mysql -ABNe "SHOW GLOBAL STATUS WHERE Variable_name = 'wsrep_local_state_comment'"` if [ "$?" -ne "0" ] ; then echo " failed" sleep 1 continue fi state=`echo -n "$state" | cut -f2` if [ "$state" != "Synced" ] ; then echo -n "." sleep 1 continue fi break done echo " synced" } quit() { mysql "$DB" -ABNe " SET sql_log_bin = 1; SET wsrep_on = 1; SET GLOBAL wsrep_desync = 0;" >/dev/null 2>&1 wait_node_synced enable_cluster_purging exit } wait_cluster_ready() { echo "$CACHE_NODES" \ | while read CACHE_NODE ; do echo -n "`date +%Y%m%d-%H%M%S` Checking node $CACHE_NODE... " WSREP_STATE=`mysql -h $CACHE_NODE -Ne 'SELECT variable_value FROM performance_schema.global_status WHERE variable_name = "wsrep_ready";'` if [ "$?" -ne "0" ] ; then echo "failed" exit 1 fi if [ "$WSREP_STATE" != "ON" ] ; then echo "not synced" exit 1 fi echo "synced" done } wait_cluster_queue() { echo "$CACHE_NODES" \ | while read CACHE_NODE ; do echo -n "`date +%Y%m%d-%H%M%S` Waiting node $CACHE_NODE queue..." while true ; do CURRENT_QUEUE=`mysql -h $CACHE_NODE -Ne 'SELECT variable_value FROM performance_schema.global_status WHERE variable_name = "wsrep_local_recv_queue";'` if [ "$?" -ne "0" ] ; then echo " failed" exit 1 fi if [ "$CURRENT_QUEUE" -eq "0" ] ; then break fi echo -n "." sleep 30 done echo " done" done } analyze_tables() { mysql information_schema -ABNe "SELECT table_schema, table_name FROM tables WHERE table_type = 'BASE TABLE' AND engine = 'InnoDB';" \ | while read DB TABLE ; do echo -n "`date +%Y%m%d-%H%M%S` Analyzing table $DB.$TABLE... " grep -sq "^$DB.$TABLE$" "$SCRIPT_DIR/tables_analyzed.txt" if [ "$?" -eq "0" ] ; then echo "already done" continue fi mysql "$DB" -ABNe " SET GLOBAL wsrep_desync = 1; SET wsrep_on = 0; SET sql_log_bin = 0; ANALYZE TABLE \`$TABLE\`; SET sql_log_bin = 1; SET wsrep_on = 1; SET GLOBAL wsrep_desync = 0;" >/dev/null 2>&1 if [ "$?" -eq "0" ] ; then echo "done" echo "$DB.$TABLE" >> "$SCRIPT_DIR/tables_analyzed.txt" else echo "failed" exit 1 fi wait_node_synced done } alter_tables() { mysql information_schema -ABNe "SELECT table_schema, table_name FROM tables WHERE table_type = 'BASE TABLE' AND engine = 'InnoDB' AND data_free > 0 ORDER BY data_free / (data_length + index_length) DESC;" \ | while read DB TABLE ; do echo -n "`date +%Y%m%d-%H%M%S` Altering table $DB.$TABLE... " grep -sq "^$DB.$TABLE$" "$SCRIPT_DIR/tables_altered.txt" if [ "$?" -eq "0" ] ; then echo "already done" continue fi mysql "$DB" -ABNe " SET GLOBAL wsrep_desync = 1; SET wsrep_on = 0; SET sql_log_bin = 0; ALTER TABLE \`$TABLE\` ENGINE=InnoDB; SET sql_log_bin = 1; SET wsrep_on = 1; SET GLOBAL wsrep_desync = 0;" >/dev/null 2>&1 if [ "$?" -eq "0" ] ; then echo "done" echo "$DB.$TABLE" >> "$SCRIPT_DIR/tables_altered.txt" else echo "failed" mysql "$DB" -ABNe " SET wsrep_on = 1; SET GLOBAL wsrep_desync = 0; SET sql_log_bin = 1;" >/dev/null 2>&1 #exit 1 fi wait_node_synced done } if [ "$CACHE_NODES" = "auto" ] ; then CURRENT_NODE=`mysql mysql -BNe "SHOW GLOBAL VARIABLES WHERE Variable_name = 'wsrep_node_address'" | sed -e 's/^wsrep_node_address[ \t]*//g'` ALL_NODES=`mysql mysql -BNe "SHOW GLOBAL VARIABLES WHERE Variable_name = 'wsrep_cluster_address'" | sed -e 's;^wsrep_cluster_address.*gcomm://;;g; s/,/\n/g'` CACHE_NODES=`echo "$ALL_NODES" | grep -v "^$CURRENT_NODE$"` fi trap quit INT QUIT wait_cluster_ready wait_cluster_queue disable_gcache_purging analyze_tables alter_tables enable_cluster_purging