github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/deploy/apecloud-mysql/scripts/switchover-with-candidate.sh (about)

     1  #!/bin/bash
     2  set -ex
     3  
     4  if [ ! -z $MYSQL_ROOT_PASSWORD ]; then
     5    password_flag="-p$MYSQL_ROOT_PASSWORD"
     6  fi
     7  
     8  leader_ip_port=$KB_CONSENSUS_LEADER_POD_FQDN:13306
     9  candidate_ip_port=$KB_SWITCHOVER_CANDIDATE_FQDN:13306
    10  
    11  # get currently leader weight
    12  leader_weight_info=`mysql -h$KB_CONSENSUS_LEADER_POD_FQDN -uroot $password_flag 2>/dev/null -e "select ELECTION_WEIGHT from information_schema.wesql_cluster_global where IP_PORT='$leader_ip_port' limit 1;"`
    13  leader_weight_arr=($leader_weight_info)
    14  leader_weight=${leader_weight_arr[1]}
    15  echo "leader_weight=$leader_weight"
    16  
    17  # get candidate weight
    18  candidate_weight_info=`mysql -h$KB_CONSENSUS_LEADER_POD_FQDN -uroot $password_flag 2>/dev/null -e "select ELECTION_WEIGHT from information_schema.wesql_cluster_global where IP_PORT='$candidate_ip_port' limit 1;"`
    19  candidate_weight_arr=($candidate_weight_info)
    20  candidate_weight=${candidate_weight_arr[1]}
    21  echo "candidate_weight=$leader_weight"
    22  
    23  # if candidate weight is less than leader weight, update leader weight to candidate weight
    24  if [ $candidate_weight -lt $leader_weight ];then
    25     echo "leader weight larger than follower weight, update leader weight to current follower weight! leader:$leader_ip_port, leader weight:$leader_weight, follower:$candidate_ip_port, follower weight:$candidate_weight"
    26     mysql -h$KB_CONSENSUS_LEADER_POD_FQDN -uroot $password_flag -e "call dbms_consensus.configure_follower('$leader_ip_port', $candidate_weight, 0);" 2>&1
    27  fi
    28  
    29  # do switchover
    30  mysql -h$KB_CONSENSUS_LEADER_POD_FQDN -uroot $password_flag -e "call dbms_consensus.change_leader('$KB_SWITCHOVER_CANDIDATE_FQDN:13306');"
    31  
    32  sleep 5
    33  
    34  # check if switchover successfully
    35  role_info=`mysql -h$KB_CONSENSUS_LEADER_POD_FQDN -uroot $password_flag 2>/dev/null -e "select ROLE from information_schema.wesql_cluster_local;"`
    36  role_info_arr=($role_info)
    37  real_role=${role_info_arr[1]}
    38  if [ "$real_role" == "Follower" ];then
    39    echo "switchover successfully"
    40  else
    41    echo "switchover failed, please check!"
    42    exit 1
    43  fi