Skip to content

Commit

Permalink
fix: leader should switchover first before scaling in (#3301)
Browse files Browse the repository at this point in the history
  • Loading branch information
lynnleelhl authored May 18, 2023
1 parent 9cff6ca commit b5abe41
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions deploy/apecloud-mysql/templates/scripts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,7 @@ data:
cluster_info="$cluster_info;";
fi;
tmp_host=$(eval echo \$KB_MYSQL_"$i"_HOSTNAME)
# setup pod weight, prefer pod 0 to be leader
if [ $i -eq 0 ]; then
cluster_info="$cluster_info$tmp_host:13306#9N";
else
cluster_info="$cluster_info$tmp_host:13306#1N";
fi
cluster_info="$cluster_info$tmp_host:13306";
done;
cluster_info="$cluster_info@$(($idx+1))";
echo "cluster_info=$cluster_info";
Expand Down Expand Up @@ -156,7 +151,6 @@ data:
pre-stop.sh: |
#!/bin/bash
drop_followers() {
leader=`cat /etc/annotations/leader`
echo "leader=$leader" >> /data/mysql/.kb_pre_stop.log
echo "KB_POD_NAME=$KB_POD_NAME" >> /data/mysql/.kb_pre_stop.log
if [ -z "$leader" -o "$KB_POD_NAME" = "$leader" ]; then
Expand All @@ -178,11 +172,34 @@ data:
echo "mysql $host_flag -uroot $password_flag -e \"call dbms_consensus.drop_learner('$host:13306');\" 2>&1 " >> /data/mysql/.kb_pre_stop.log
mysql $host_flag -uroot $password_flag -e "call dbms_consensus.drop_learner('$host:13306');" 2>&1
}
switchover() {
if [ ! -z $MYSQL_ROOT_PASSWORD ]; then
password_flag="-p$MYSQL_ROOT_PASSWORD"
fi
new_leader_host=$KB_MYSQL_0_HOSTNAME
if [ "$KB_POD_NAME" = "$leader" ]; then
echo "self is leader, need to switchover" >> /data/mysql/.kb_pre_stop.log
echo "mysql -uroot $password_flag -e \"call dbms_consensus.change_leader('$new_leader_host:13306');\" 2>&1" >> /data/mysql/.kb_pre_stop.log
mysql -uroot $password_flag -e "call dbms_consensus.change_leader('$new_leader_host:13306');" 2>&1
sleep 1
role_info=`mysql -uroot $password_flag -e "select * from information_schema.wesql_cluster_local;" 2>&1`
echo "role_info=$role_info" >> /data/mysql/.kb_pre_stop.log
is_follower=`echo $role_info | grep "Follower"`
if [ ! -z "$is_follower" ]; then
echo "new_leader=$new_leader_host" >> /data/mysql/.kb_pre_stop.log
leader=`echo "$new_leader_host" | cut -d "." -f 1`
idx=${KB_POD_NAME##*-}
fi
fi
}
leader=`cat /etc/annotations/leader`
idx=${KB_POD_NAME##*-}
current_component_replicas=`cat /etc/annotations/component-replicas`
echo "current replicas: $current_component_replicas" >> /data/mysql/.kb_pre_stop.log
if [ ! $idx -lt $current_component_replicas ] && [ $current_component_replicas -ne 0 ]; then
# if idx greater than or equal to current_component_replicas means the cluster's scaling in
# switch leader before leader scaling in itself
switchover
# only scaling in need to drop followers
drop_followers
else
Expand Down

0 comments on commit b5abe41

Please sign in to comment.