Skip to content

Commit

Permalink
chore: support new switchover action (#1338)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjc7373 authored Dec 31, 2024
1 parent af17f8a commit b4cab1e
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 5 deletions.
11 changes: 10 additions & 1 deletion addons/apecloud-mysql/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,16 @@ lifecycleActions:
- /bin/sh
- -c
- |
/tools/syncerctl switchover --primary "$KB_LEADER_POD_NAME" ${KB_SWITCHOVER_CANDIDATE_NAME:+--candidate "$KB_SWITCHOVER_CANDIDATE_NAME"}
if [ -z "$KB_SWITCHOVER_ROLE" ]; then
echo "role can't be empty"
exit 1
fi

if [ "$KB_SWITCHOVER_ROLE" != "leader" ]; then
exit 0
fi

/tools/syncerctl switchover --primary "$KB_SWITCHOVER_CURRENT_NAME" ${KB_SWITCHOVER_CANDIDATE_NAME:+--candidate "$KB_SWITCHOVER_CANDIDATE_NAME"}
accountProvision:
exec:
container: mysql
Expand Down
9 changes: 9 additions & 0 deletions addons/apecloud-postgresql/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,15 @@ lifecycleActions:
- /bin/sh
- -c
- |
if [ -z "$KB_SWITCHOVER_ROLE" ]; then
echo "role can't be empty"
exit 1
fi

if [ "$KB_SWITCHOVER_ROLE" != "leader" ]; then
exit 0
fi

/tools/syncerctl switchover --primary "$POSTGRES_LEADER_POD_NAME" ${KB_SWITCHOVER_CANDIDATE_NAME:+--candidate "$KB_SWITCHOVER_CANDIDATE_NAME"}
memberLeave:
exec:
Expand Down
5 changes: 5 additions & 0 deletions addons/etcd/scripts/switchover.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ switchover_without_candidate() {
}

switchover() {
if [[ $LEADER_POD_FQDN != "$KB_SWITCHOVER_CURRENT_FQDN" ]]; then
echo "switchover action not triggered for leader pod. Exiting."
exit 0
fi

if is_empty "$KB_SWITCHOVER_CANDIDATE_FQDN"; then
switchover_without_candidate
else
Expand Down
11 changes: 10 additions & 1 deletion addons/mongodb/templates/componentdefinition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,16 @@ spec:
- /bin/sh
- -c
- |
/tools/syncerctl switchover --primary "$KB_LEADER_POD_NAME" ${KB_SWITCHOVER_CANDIDATE_NAME:+--candidate "$KB_SWITCHOVER_CANDIDATE_NAME"}
if [ -z "$KB_SWITCHOVER_ROLE" ]; then
echo "role can't be empty"
exit 1
fi
if [ "$KB_SWITCHOVER_ROLE" != "primary" ]; then
exit 0
fi
/tools/syncerctl switchover --primary "$KB_SWITCHOVER_CURRENT_NAME" ${KB_SWITCHOVER_CANDIDATE_NAME:+--candidate "$KB_SWITCHOVER_CANDIDATE_NAME"}
runtime:
initContainers:
- command:
Expand Down
11 changes: 10 additions & 1 deletion addons/mysql/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,16 @@ lifecycleActions:
- /bin/sh
- -c
- |
/tools/syncerctl switchover --primary "$KB_LEADER_POD_NAME" ${KB_SWITCHOVER_CANDIDATE_NAME:+--candidate "$KB_SWITCHOVER_CANDIDATE_NAME"}
if [ -z "$KB_SWITCHOVER_ROLE" ]; then
echo "role can't be empty"
exit 1
fi

if [ "$KB_SWITCHOVER_ROLE" != "primary" ]; then
exit 0
fi

/tools/syncerctl switchover --primary "$KB_SWITCHOVER_CURRENT_NAME" ${KB_SWITCHOVER_CANDIDATE_NAME:+--candidate "$KB_SWITCHOVER_CANDIDATE_NAME"}
roles:
- name: primary
serviceable: true
Expand Down
5 changes: 5 additions & 0 deletions addons/orioledb/scripts/switchover.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ switchover() {
exit 1
fi

if [[ $POSTGRES_PRIMARY_POD_NAME != "$KB_SWITCHOVER_CURRENT_NAME" ]]; then
echo "switchover action not triggered for primary pod. Exiting."
exit 0
fi

current_pod_fqdn=$(get_target_pod_fqdn_from_pod_fqdn_vars "$POSTGRES_POD_FQDN_LIST" "$CURRENT_POD_NAME")
if is_empty "$current_pod_fqdn"; then
echo "Error: Failed to get current pod: $CURRENT_POD_NAME fqdn from postgres pod fqdn list: $POSTGRES_POD_FQDN_LIST. Exiting."
Expand Down
19 changes: 19 additions & 0 deletions addons/postgresql/scripts-ut-spec/switchover_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Describe "PostgreSQL Switchover Script Tests"
Context "when the current pod FQDN is not found"
setup() {
CURRENT_POD_NAME="pod1"
KB_SWITCHOVER_CURRENT_NAME="pod1"
POSTGRES_PRIMARY_POD_NAME="pod1"
POSTGRES_POD_NAME_LIST="pod2,pod3"
POSTGRES_POD_FQDN_LIST="pod2.example.com,pod3.example.com"
Expand All @@ -85,10 +86,27 @@ Describe "PostgreSQL Switchover Script Tests"
End
End

Context "when switchover action is called for a non-primary pod"
setup() {
CURRENT_POD_NAME="pod1"
POSTGRES_PRIMARY_POD_NAME="pod2"
KB_SWITCHOVER_CURRENT_NAME="pod1"
POSTGRES_POD_NAME_LIST="pod1,pod2"
POSTGRES_POD_FQDN_LIST="pod1.example.com,pod2.example.com"
}
Before 'setup'

It "exits without error"
When run switchover
The output should include "switchover action not triggered for primary pod. Exiting"
End
End

Context "when KB_SWITCHOVER_CANDIDATE_NAME is set"
setup() {
CURRENT_POD_NAME="pod1"
POSTGRES_PRIMARY_POD_NAME="pod1"
KB_SWITCHOVER_CURRENT_NAME="pod1"
POSTGRES_POD_NAME_LIST="pod1,pod2"
POSTGRES_POD_FQDN_LIST="pod1.example.com,pod2.example.com"
KB_SWITCHOVER_CANDIDATE_NAME="pod2"
Expand All @@ -108,6 +126,7 @@ Describe "PostgreSQL Switchover Script Tests"
setup() {
CURRENT_POD_NAME="pod1"
POSTGRES_PRIMARY_POD_NAME="pod1"
KB_SWITCHOVER_CURRENT_NAME="pod1"
POSTGRES_POD_NAME_LIST="pod1,pod2"
POSTGRES_POD_FQDN_LIST="pod1.example.com,pod2.example.com"
unset KB_SWITCHOVER_CANDIDATE_NAME
Expand Down
5 changes: 5 additions & 0 deletions addons/postgresql/scripts/switchover.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ switchover() {
exit 1
fi

if [[ $POSTGRES_PRIMARY_POD_NAME != "$KB_SWITCHOVER_CURRENT_NAME" ]]; then
echo "switchover action not triggered for primary pod. Exiting."
exit 0
fi

current_pod_fqdn=$(get_target_pod_fqdn_from_pod_fqdn_vars "$POSTGRES_POD_FQDN_LIST" "$CURRENT_POD_NAME")
if is_empty "$current_pod_fqdn"; then
echo "Error: Failed to get current pod: $CURRENT_POD_NAME fqdn from postgres pod fqdn list: $POSTGRES_POD_FQDN_LIST. Exiting."
Expand Down
10 changes: 10 additions & 0 deletions addons/redis/scripts/redis-switchover.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ check_environment_exist() {
echo "Error: Required environment variable REDIS_COMPONENT_NAME: $REDIS_COMPONENT_NAME is not set."
exit 1
fi

if is_empty "$KB_SWITCHOVER_ROLE"; then
echo "role can't be empty"
exit 1
fi

if [ "$KB_SWITCHOVER_ROLE" != "primary" ]; then
echo "swtichover not triggered for primary, nothing to do"
exit 0
fi
}

check_connectivity() {
Expand Down
23 changes: 23 additions & 0 deletions addons/tidb/scripts/pd_switchover.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

set -exo pipefail

if [[ -z $KB_SWITCHOVER_ROLE ]]; then
echo "role can't be empty"
exit 1
fi

if [[ $KB_SWITCHOVER_ROLE != "leader" ]]; then
exit 0
fi

if [[ -n $KB_SWITCHOVER_CANDIDATE_NAME ]]; then
result=$(/pd-ctl member leader transfer "$KB_SWITCHOVER_CANDIDATE_NAME")
else
result=$(/pd-ctl member leader resign)
fi

if [[ $result != "Success!" ]]; then
echo "switchover failed"
exit 1
fi
8 changes: 6 additions & 2 deletions addons/tidb/scripts/tikv_member_leave.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

set -exo pipefail

/pd-ctl -u "$PD_ADDRESS" store delete addr "$KB_LEAVE_MEMBER_POD_FQDN:20160"
output=$(/pd-ctl -u "$PD_ADDRESS" store delete addr "$KB_LEAVE_MEMBER_POD_FQDN:20160")
if [[ $output != "Success!" ]]; then
echo "leave member $KB_LEAVE_MEMBER_POD_FQDN failed"
exit 1
fi

until [ $(/pd-ctl -u "$PD_ADDRESS" store | jq "any(.stores[]; select(.store.address == \"$KB_LEAVE_MEMBER_POD_FQDN:20160\"))") == "false" ]
until [[ $(/pd-ctl -u "$PD_ADDRESS" store | jq "any(.stores[]; select(.store.address == \"$KB_LEAVE_MEMBER_POD_FQDN:20160\"))") == "false" ]]
do
echo "waiting for tikv node to become tombstone"
sleep 10
Expand Down
9 changes: 9 additions & 0 deletions addons/tidb/templates/componentdefinition-pd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ spec:
echo $KB_LEAVE_MEMBER_POD_NAME
echo $ADDRESS
/pd-ctl -u "$ADDRESS:2379" member delete name $KB_LEAVE_MEMBER_POD_NAME
switchover:
exec:
targetPodSelector: Role
matchingKey: leader
command:
- bash
- -c
- |
{{- .Files.Get "scripts/pd_switchover.sh" | nindent 14 }}
exporter:
containerName: pd
scrapePort: "2379"
Expand Down
3 changes: 3 additions & 0 deletions addons/tidb/templates/componentversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ spec:
pd: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.pd.repository }}:v7.5.2
roleProbe: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.pd.repository }}:v7.5.2
memberLeave: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.pd.repository }}:v7.5.2
switchover: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.pd.repository }}:v7.5.2
- name: "7.1.5"
serviceVersion: "7.1.5"
images:
pd: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.pd.repository }}:v7.1.5
roleProbe: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.pd.repository }}:v7.1.5
memberLeave: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.pd.repository }}:v7.1.5
switchover: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.pd.repository }}:v7.1.5
- name: "6.5.10"
serviceVersion: "6.5.10"
images:
pd: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.pd.repository }}:v6.5.10
roleProbe: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.pd.repository }}:v6.5.10
memberLeave: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.pd.repository }}:v6.5.10
switchover: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.pd.repository }}:v6.5.10
compatibilityRules:
- compDefs:
- {{ include "tidb.pd7.cmpdRegexpPattern" . }}
Expand Down
5 changes: 5 additions & 0 deletions addons/vanilla-postgresql/scripts/switchover.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#!/bin/sh

if [ "$POSTGRES_PRIMARY_POD_NAME" != "$KB_SWITCHOVER_CURRENT_NAME" ]; then
echo "switchover action not triggered for primary pod. Exiting."
exit 0
fi

/tools/syncerctl switchover --primary "$POSTGRES_PRIMARY_POD_NAME" ${KB_SWITCHOVER_CANDIDATE_NAME:+--candidate "$KB_SWITCHOVER_CANDIDATE_NAME"}

0 comments on commit b4cab1e

Please sign in to comment.