github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/deploy/apecloud-mysql/scripts/pre-stop.sh (about) 1 #!/bin/bash 2 3 set_my_weight_to_zero() { 4 if [ ! -z $MYSQL_ROOT_PASSWORD ]; then 5 password_flag="-p$MYSQL_ROOT_PASSWORD" 6 fi 7 8 if [ "$KB_POD_NAME" = "$leader" ]; then 9 echo "self is leader, before scale in, need to set my election weight to 0." >> /data/mysql/.kb_pre_stop.log 10 host=$(eval echo \$KB_"$idx"_HOSTNAME) 11 echo "set weight to 0. mysql -uroot $password_flag -e \"call dbms_consensus.configure_follower('$host:13306',0 ,false);\" 2>&1" >> /data/mysql/.kb_pre_stop.log 12 mysql -uroot $password_flag -e "call dbms_consensus.configure_follower('$host:13306',0 ,false);" 2>&1 13 fi 14 } 15 16 switchover() { 17 if [ ! -z $MYSQL_ROOT_PASSWORD ]; then 18 password_flag="-p$MYSQL_ROOT_PASSWORD" 19 fi 20 #new_leader_host=$KB_0_HOSTNAME 21 if [ "$KB_POD_NAME" = "$leader" ]; then 22 echo "self is leader, need to switchover" >> /data/mysql/.kb_pre_stop.log 23 echo "try to get global cluster info" >> /data/mysql/.kb_pre_stop.log 24 global_info=`mysql -uroot $password_flag 2>/dev/null -e "select IP_PORT from information_schema.wesql_cluster_global order by MATCH_INDEX desc;"` 25 echo "all nodes: $global_info" >> /data/mysql/.kb_pre_stop.log 26 global_info_arr=($global_info) 27 echo "all nodes array: ${global_info_arr[0]},${global_info_arr[1]},${global_info_arr[2]},${global_info_arr[3]}" >> /data/mysql/.kb_pre_stop.log 28 echo "array size: ${#global_info_arr[@]}, the first one is not real address,just the field name IP_PORT" >> /data/mysql/.kb_pre_stop.log 29 30 host=$(eval echo \$KB_"$idx"_HOSTNAME) 31 host_ip_port=$host:13306 32 try_times=10 33 for((i=1;i<${#global_info_arr[@]};i++)) do 34 if [ "$host_ip_port" == "${global_info_arr[i]}" ];then 35 echo "do not transfer to leader, leader:${global_info_arr[i]}" >> /data/mysql/.kb_pre_stop.log; 36 else 37 echo "try to transfer to:${global_info_arr[i]}" >> /data/mysql/.kb_pre_stop.log; 38 echo "mysql -uroot $password_flag -e \"call dbms_consensus.change_leader('${global_info_arr[i]}');\" 2>&1" >> /data/mysql/.kb_pre_stop.log 39 mysql -uroot $password_flag -e "call dbms_consensus.change_leader('${global_info_arr[i]}');" 2>&1 40 sleep 1 41 role_info=`mysql -uroot $password_flag 2>/dev/null -e "select ROLE from information_schema.wesql_cluster_local;"` 42 role_info_arr=($role_info) 43 real_role=${role_info_arr[1]} 44 echo "this node's current role info:$real_role" >> /data/mysql/.kb_pre_stop.log 45 if [ "$real_role" == "Follower" ];then 46 echo "transfer successfully" >> /data/mysql/.kb_pre_stop.log 47 new_leader_host_and_port=${global_info_arr[i]} 48 # get rid of port 49 new_leader_host=${new_leader_host_and_port%%:*} 50 echo "new_leader_host=$new_leader_host" >> /data/mysql/.kb_pre_stop.log 51 leader=`echo "$new_leader_host" | cut -d "." -f 1` 52 echo "leader_host: $leader" >> /data/mysql/.kb_pre_stop.log 53 idx=${KB_POD_NAME##*-} 54 break 55 fi 56 fi 57 ((try_times--)) 58 if [ $try_times -le 0 ];then 59 echo "try too many times" >> /data/mysql/.kb_pre_stop.log 60 break 61 fi 62 done 63 fi 64 } 65 leader=`cat /etc/annotations/leader` 66 idx=${KB_POD_NAME##*-} 67 current_component_replicas=`cat /etc/annotations/component-replicas` 68 echo "current replicas: $current_component_replicas" >> /data/mysql/.kb_pre_stop.log 69 if [ ! $idx -lt $current_component_replicas ] && [ $current_component_replicas -ne 0 ]; then 70 # if idx greater than or equal to current_component_replicas means the cluster's scaling in 71 # put .restore on pvc for next scaling out, if pvc not deleted 72 touch /data/mysql/data/.restore; sync 73 # set wegiht to 0 and switch leader before leader scaling in itself 74 set_my_weight_to_zero 75 switchover 76 elif [ $current_component_replicas -eq 0 ]; then 77 # stop, do nothing. 78 echo "stop, do nothing" >> /data/mysql/.kb_pre_stop.log 79 else 80 # restart, switchover first. 81 echo "Also try to switchover just before restart" >> /data/mysql/.kb_pre_stop.log 82 switchover 83 echo "no need to drop followers" >> /data/mysql/.kb_pre_stop.log 84 fi