73 lines
1.8 KiB
Bash
Raw Normal View History

#!/bin/sh
# Run Valkey command
vcli() {
valkey-cli -h 127.0.0.1 -p "${VALKEY_PORT}" "$@"
return $?
}
# Run Sentinel command
vcli-sentinel() {
valkey-cli -h "$VALKEY_SERVICE" -p "$VALKEY_SENTINEL_PORT" sentinel "$@"
return $?
}
getFailOverStatus() {
# Check if the failover is finished
local failoverStatus
primaryInfo=$(vcli-sentinel get-primary-addr-by-name "mymaster")
primaryHost=$(echo ${primaryInfo} | awk -F ' ' '{print $1}')
currentHost=$(hostname -f)
if [[ "${primaryHost}" != "${currentHost}" ]]; then
echo "I'm not the primary, failover finished"
return 0
else
echo "Failover in progress"
return 1
fi
}
getRole() {
# Get the role of the current node
VALKEY_ROLE=$(vcli role | head -1)
echo "${VALKEY_ROLE}"
}
isPrimary() {
# Check if the current node is the primary
role=$(getRole)
if [ "${role}" = "master" ]; then
echo "I'm the master"
else
echo "I'm not the master, I'm a ${role}"
fi
}
if isPrimary && ! getFailOverStatus; then
echo "I'm the primary and you are stopping me, pausing client connections"
#Pausing write connections to avoid data loss"
vcli CLIENT PAUSE "22000" WRITE
echo "Starting failover"
vcli-sentinel failover "mymaster"
echo "Waiting for sentinel to complete failover for up to 120s"
tmout=120
while true ; do
echo "I'm the primary pod and you are stopping me. Starting sentinel failover"
getFailOverStatus
if [ $? -eq 0 ]; then
echo "Primary has been successfully failed over to a different pod."
break
fi
echo "Waiting for failover to finish..."
sleep 1
tmout=$((tmout - 1))
if [ "${tmout}" -le 0 ]; then
echo "Failover timed out"
exit 1
fi
done
fi
exit 0