#!/bin/bash
|
# failover_master.sh
|
|
ARGS=$(getopt -o '' --long 'event:,initiator:,nodelist:' -- "$@")
|
eval set -- "$ARGS"
|
|
while true; do
|
case "$1" in
|
--event)
|
shift;
|
event=$1
|
shift;
|
;;
|
--initiator)
|
shift;
|
initiator=$1
|
shift;
|
;;
|
--nodelist)
|
shift;
|
nodelist=$1
|
shift;
|
;;
|
--)
|
shift;
|
break;
|
;;
|
esac
|
done
|
|
candidate=`echo "$nodelist" | awk -F':' '{print $1}'`
|
maxscale_host=`echo "$initiator" | awk -F'-' '{print $5}'`
|
maxscale_host=`echo "$maxscale_host" | awk -F':' '{print $1}'`
|
|
if [ -z $candidate ]; then
|
echo "ERROR!!! NO candidate master found when failing over $initiator! The system might be down."|wall
|
echo "ERROR!!! NO candidate master found! The system might be down."
|
exit 0
|
fi
|
|
# WORK AROUND for race condition, see https://jira.mariadb.org/browse/MXS-845
|
currently_in_maintenance=`maxadmin -pmariadb list servers|grep Maintenance|grep $maxscale_host|wc -l`
|
if [ $currently_in_maintenance = "0" ]; then
|
maxadmin -pmariadb set server $maxscale_host maintenance
|
maxadmin -pmariadb clear server $maxscale_host running
|
else
|
echo "This script is not the first one, exiting."|wall
|
exit 1
|
fi
|
|
# loosen (ACI)D to speedup any lag.
|
mysql -u maxscale -p'***' --host=$candidate -e "set global sync_binlog=0; set global innodb_flush_log_at_trx_commit=0;set global innodb_io_capacity=50000;"
|
|
while true; do
|
echo "Waiting until all transactions have been applied on candidate master $candidate..."
|
sleep 1
|
SLAVESTAT=$(mysql -umaxscale -p'***' --host=$candidate -e "show slave status\G");
|
exec_master_pos=`echo "$SLAVESTAT" | grep -w 'Exec_Master_Log_Pos:' | awk '{print $2}';`
|
read_master_pos=`echo "$SLAVESTAT" | grep -w 'Read_Master_Log_Pos:' | awk '{print $2}';`
|
if [ -n $old_read_master_pos ] && [ ! $old_read_master_pos = $read_master_pos ]; then
|
echo "ERROR!!! Old master $initiator still receives transactions after putting it into maintenance! Manual intervention required to make sure the old master is really down."
|
echo "ERROR!!! Old master $initiator still receives transactions after putting it into maintenance! Manual intervention required to make sure the old master is really down."|wall
|
exit 0
|
fi
|
old_read_master_pos=read_master_pos
|
count=`expr $read_master_pos - $exec_master_pos`
|
|
if [ $count -eq 0 ]; then
|
mysql -umaxscale -p'***' --host=$candidate -e "set global read_only=OFF; set global sync_binlog=1; set global innodb_flush_log_at_trx_commit=1;SET GLOBAL innodb_io_capacity=200"
|
break;
|
fi
|
done
|