Skip to content

Commit

Permalink
fix: adapt wesql script for specify instance offline (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuriwuyun authored Jun 20, 2024
1 parent f09ab1a commit 10087aa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 43 deletions.
9 changes: 1 addition & 8 deletions addons/apecloud-mysql/scripts/pre-stop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,7 @@ 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
# put .restore on pvc for next scaling out, if pvc not deleted
touch /data/mysql/data/.restore; sync
# set wegiht to 0 and switch leader before leader scaling in itself
set_my_weight_to_zero
switchover
elif [ $current_component_replicas -eq 0 ]; then
if [ $current_component_replicas -eq 0 ]; then
# stop, do nothing.
echo "stop, do nothing" >> /data/mysql/.kb_pre_stop.log
else
Expand Down
69 changes: 35 additions & 34 deletions addons/apecloud-mysql/scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,42 @@

set -ex

get_replica_host() {
local id=$1

if [ -z "${REPLICATION_ENDPOINT}" ]; then
host="KB_${id}_HOSTNAME"
echo "${!host}"
else
endpoints=$(eval echo "${REPLICATION_ENDPOINT}" | tr ',' '\n')
for endpoint in ${endpoints}; do
host=$(eval echo "${endpoint}" | cut -d ':' -f 1)
ip=$(eval echo "${endpoint}" | cut -d ':' -f 2)
hostId=$(eval echo "${host}" | grep -oE "[0-9]+\$")
if [ "${id}" = "${hostId}" ]; then
echo "${ip}"
break
fi
done
get_hostname_suffix() {
IFS='.' read -ra fields <<< "$KB_POD_FQDN"
if [ "${#fields[@]}" -gt "2" ]; then
echo "${fields[1]}"
fi
}

get_cluster_members() {
local cluster_members=""
IFS=',' read -ra PODS <<< "$KB_POD_LIST"
for pod in "${PODS[@]}"; do
hostname=${pod}.${hostname_suffix}
cluster_members="${cluster_members};${hostname}:${MYSQL_CONSENSUS_PORT:-13306}"
done
echo "${cluster_members#;}"
}

get_pod_index() {
local pod_name="${1:?missing pod name}"
local pod_index=0

IFS=',' read -ra PODS <<< "$KB_POD_LIST"
for pod in "${PODS[@]}"; do
if [ "$pod" = "$pod_name" ]; then
break
fi
((pod_index++))
done

echo "$pod_index"
}

generate_cluster_info() {
local pod_name="${KB_POD_NAME:?missing pod name}"
local cluster_members=""
local hostname_suffix=$(get_hostname_suffix)

export MYSQL_PORT=${MYSQL_PORT:-3306}
export MYSQL_CONSENSUS_PORT=${MYSQL_CONSENSUS_PORT:-13306}
Expand All @@ -41,29 +54,17 @@ generate_cluster_info() {
fi
echo "KB_MYSQL_CLUSTER_UID=${KB_MYSQL_CLUSTER_UID}"

for ((i = 0; i < KB_REPLICA_COUNT; i++)); do
if [ $i -gt 0 ]; then
cluster_members="${cluster_members};"
fi

host="$(get_replica_host ${i})"
echo "${host:?missing member hostname}"
cluster_members="${cluster_members}${host}:${MYSQL_CONSENSUS_PORT:-13306}"

# compatiable with old version images
export KB_MYSQL_${i}_HOSTNAME=${host}
done
export KB_MYSQL_CLUSTER_MEMBERS="${cluster_members}"
export KB_MYSQL_CLUSTER_MEMBERS=`get_cluster_members`
echo "${KB_MYSQL_CLUSTER_MEMBERS:?missing cluster members}"

export KB_MYSQL_CLUSTER_MEMBER_INDEX=${pod_name##*-};
local pod_host=$(get_replica_host ${KB_MYSQL_CLUSTER_MEMBER_INDEX})
export KB_MYSQL_CLUSTER_MEMBER_INDEX=`get_pod_index $pod_name`
local pod_host=${pod_name}.${hostname_suffix}
export KB_MYSQL_CLUSTER_MEMBER_HOST=${pod_host:?missing current member hostname}

if [ -n "$KB_LEADER" ]; then
echo "KB_LEADER=${KB_LEADER}"

local leader_index=${KB_LEADER##*-}
local leader_host=$(get_replica_host ${leader_index})
local leader_host=$KB_LEADER.${hostname_suffix}
export KB_MYSQL_CLUSTER_LEADER_HOST=${leader_host:?missing leader hostname}

# compatiable with old version images
Expand Down
2 changes: 1 addition & 1 deletion addons/apecloud-mysql/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ wesqlscale:
cell: "zone1"
image:
# if the value of wesqlscale.image.registry is not specified using `--set`, it will be set to the value of 'image.registry' by default
registry: ""
registry: infracreate-registry.cn-zhangjiakou.cr.aliyuncs.com
repository: apecloud/apecloud-mysql-scale
tag: "0.2.7"
pullPolicy: IfNotPresent
Expand Down

0 comments on commit 10087aa

Please sign in to comment.