Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: modify the register to sentinel #1060

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ Describe 'register_to_sentinel.sh'
return 0
}

get_master_addr_by_name(){
output=""
echo "$output"
return 0
}

When call register_to_sentinel $sentinel_host $master_name $redis_primary_host $redis_primary_port
The status should be success
The output should include "host:redis-redis-sentinel-0.redis-redis-sentinel-headless.default.svc.cluster.local, port:26379 Command:SENTINEL monitor redis-redis redis-redis-0.redis-redis.default.svc.cluster.local 6379 2"
Expand Down
39 changes: 36 additions & 3 deletions addons/redis/scripts/redis-register-to-sentinel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,26 @@ execute_sentinel_sub_command() {
fi
}

get_master_addr_by_name(){
local sentinel_host=$1
local sentinel_port=$2
local command=$3
local output
output=$(redis-cli -h "$sentinel_host" -p "$sentinel_port" -a "$SENTINEL_PASSWORD" $command)
local status=$?
if [ $status -ne 0 ]; then
echo "Command failed with status $status." >&2
return 1
fi
if echo "$output" | grep -E '^[a-zA-Z0-9-]+\.[a-zA-Z0-9-]+\.default\.svc$' > /dev/null || is_empty "$output"; then
echo "$output"
return 0
else
echo "Command failed with $output" >&2
return 1
fi
}

# usage: register_to_sentinel <sentinel_host> <master_name> <redis_primary_host> <redis_primary_port>
# redis sentinel configuration refer: https://redis.io/docs/management/sentinel/#configuring-sentinel
register_to_sentinel() {
Expand All @@ -164,9 +184,22 @@ register_to_sentinel() {
call_func_with_retry 3 5 check_connectivity "$sentinel_host" "$sentinel_port" "$SENTINEL_PASSWORD" || exit 1
call_func_with_retry 3 5 check_connectivity "$redis_primary_host" "$redis_primary_port" "$REDIS_DEFAULT_PASSWORD" || exit 1

# Register and configure the Redis primary to redis sentinel
sentinel_commands=("monitor" "down-after-milliseconds" "failover-timeout" "parallel-syncs" "auth-user" "auth-pass")
for cmd in "${sentinel_commands[@]}"
# Check if Sentinel is already monitoring the Redis primary
if ! master_addr=$(call_func_with_retry 3 5 get_master_addr_by_name "$sentinel_host" "$sentinel_port" "SENTINEL get-master-addr-by-name $master_name"); then
DWJ-Squirtle marked this conversation as resolved.
Show resolved Hide resolved
echo "Failed to get master address after maximum retries."
exit 1
fi
if is_empty "$master_addr"; then
DWJ-Squirtle marked this conversation as resolved.
Show resolved Hide resolved
echo "Sentinel is not monitoring $master_name. Registering it..."
# Register the Redis primary with Sentinel
sentinel_monitor_cmd="SENTINEL monitor $master_name $redis_primary_host $redis_primary_port 2"
call_func_with_retry 3 5 execute_sentinel_sub_command "$sentinel_host" "$sentinel_port" "$sentinel_monitor_cmd"
else
echo "Sentinel is already monitoring $master_name at $master_addr. Skipping monitor registration."
fi
#configure the Redis primary with Sentinel
sentinel_configure_commands=("down-after-milliseconds" "failover-timeout" "parallel-syncs" "auth-user" "auth-pass")
for cmd in "${sentinel_configure_commands[@]}"
do
sentinel_cli_cmd=$(construct_sentinel_sub_command "$cmd" "$master_name" "$redis_primary_host" "$redis_primary_port")
call_func_with_retry 3 5 execute_sentinel_sub_command "$sentinel_host" "$sentinel_port" "$sentinel_cli_cmd"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call_func_with_retry xxx || exit 1 ?

What do you think? @Y-Rookie

Expand Down
Loading