From 3ed9a40e02e48873f5b095e1c0873e968e99c6ac Mon Sep 17 00:00:00 2001 From: TianyuZhang1214 Date: Mon, 25 Nov 2024 16:36:19 +0800 Subject: [PATCH] to #119 fix: bugs in replica_setup process --- devtools/replica-setup-mysql/checker.sh | 20 +++---- devtools/replica-setup-mysql/replica_setup.sh | 6 +- docker/entrypoint.sh | 60 ++++++------------- 3 files changed, 31 insertions(+), 55 deletions(-) diff --git a/devtools/replica-setup-mysql/checker.sh b/devtools/replica-setup-mysql/checker.sh index abc0f2bf..d71f691d 100644 --- a/devtools/replica-setup-mysql/checker.sh +++ b/devtools/replica-setup-mysql/checker.sh @@ -13,7 +13,7 @@ check_server_params() { echo "Checking MySQL server parameters..." # Retrieve the required MySQL server variables using mysqlsh - result=$(mysqlsh --uri="$DSN" --sql -e " + result=$(mysqlsh --uri="$SOURCE_DSN" --sql -e " SHOW VARIABLES WHERE variable_name IN ('binlog_format', 'enforce_gtid_consistency', 'gtid_mode', 'gtid_strict_mode', 'log_bin'); ") @@ -62,10 +62,10 @@ check_server_params() { # Function to check MySQL current user privileges check_user_privileges() { - echo "Checking privileges for the current user '$SOURCE_HOST'..." + echo "Checking privileges for the current user '$SOURCE_USER'..." # Check the user grants for the currently authenticated user using mysqlsh - result=$(mysqlsh --host="$SOURCE_HOST" --port="$SOURCE_HOST" --user="$SOURCE_HOST" --password="$SOURCE_PASSWORD" --sql -e " + result=$(mysqlsh --host="$SOURCE_HOST" --port="$SOURCE_PORT" --user="$SOURCE_USER" --password="$SOURCE_PASSWORD" --sql -e " SHOW GRANTS FOR CURRENT_USER(); ") @@ -73,11 +73,11 @@ check_user_privileges() { # Check if the required privileges are granted or if GRANT ALL is present if echo "$result" | grep -q -E "GRANT (SELECT|RELOAD|REPLICATION CLIENT|REPLICATION SLAVE|SHOW VIEW|EVENT)"; then - echo "Current user '$SOURCE_HOST' has all required privileges." + echo "Current user '$SOURCE_USER' has all required privileges." elif echo "$result" | grep -q "GRANT ALL"; then - echo "Current user '$SOURCE_HOST' has 'GRANT ALL' privileges." + echo "Current user '$SOURCE_USER' has 'GRANT ALL' privileges." else - echo "Error: Current user '$SOURCE_HOST' is missing some required privileges." + echo "Error: Current user '$SOURCE_USER' is missing some required privileges." return 1 fi @@ -98,7 +98,7 @@ check_mysql_config() { # Function to check if source MySQL server is empty check_if_source_mysql_is_empty() { # Run the query using mysqlsh and capture the output - OUTPUT=$(mysqlsh --uri "$DSN" --sql -e "SHOW DATABASES;" 2>/dev/null) + OUTPUT=$(mysqlsh --uri "$SOURCE_DSN" --sql -e "SHOW DATABASES;" 2>/dev/null) check_command "retrieving database list" @@ -117,11 +117,11 @@ check_if_myduck_has_replica() { REPLICA_STATUS=$(mysqlsh --sql --host=$MYDUCK_HOST --port=$MYDUCK_PORT --user=root --password='' -e "SHOW REPLICA STATUS\G") check_command "retrieving replica status" - SOURCE_HOST=$(echo "$REPLICA_STATUS" | awk '/Source_Host/ {print $2}') + EXISTED_SOURCE_HOST=$(echo "$REPLICA_STATUS" | awk '/Source_Host/ {print $2}') # Check if Source_Host is not null or empty - if [[ -n "$SOURCE_HOST" ]]; then - echo "Replication has already been started. Source Host: $SOURCE_HOST" + if [[ -n "$EXISTED_SOURCE_HOST" ]]; then + echo "Replication has already been started. Source Host: $EXISTED_SOURCE_HOST" return 1 else return 0 diff --git a/devtools/replica-setup-mysql/replica_setup.sh b/devtools/replica-setup-mysql/replica_setup.sh index ab4a2be7..1dba31b8 100644 --- a/devtools/replica-setup-mysql/replica_setup.sh +++ b/devtools/replica-setup-mysql/replica_setup.sh @@ -62,14 +62,14 @@ while [[ $# -gt 0 ]]; do esac done -source checker.sh - # Check if all parameters are set -if [[ -z "$SOURCE_PORT" || -z "$SOURCE_PORT" || -z "$SOURCE_PORT" ]]; then +if [[ -z "$SOURCE_HOST" || -z "$SOURCE_PORT" || -z "$SOURCE_USER" ]]; then echo "Error: Missing required MySQL connection variables: SOURCE_HOST, SOURCE_PORT, SOURCE_USER." usage fi +source checker.sh + # Step 1: Check if mysqlsh exists, if not, install it if ! command -v mysqlsh &> /dev/null; then echo "mysqlsh not found, attempting to install..." diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 98b2ae2a..417d206e 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -33,11 +33,11 @@ parse_dsn() { # Extract credentials and host/port/dbname if [[ "$dsn" =~ ^([^:@]+)(:([^@]*))?@([^:/]+)(:([0-9]+))?(/(.+))?$ ]]; then - SOURCE_USER="${BASH_REMATCH[1]}" - SOURCE_PASSWORD="${BASH_REMATCH[3]}" - SOURCE_HOST="${BASH_REMATCH[4]}" - SOURCE_PORT="${BASH_REMATCH[6]}" - SOURCE_DATABASE="${BASH_REMATCH[8]}" + export SOURCE_USER="${BASH_REMATCH[1]}" + export SOURCE_PASSWORD="${BASH_REMATCH[3]}" + export SOURCE_HOST="${BASH_REMATCH[4]}" + export SOURCE_PORT="${BASH_REMATCH[6]}" + export SOURCE_DATABASE="${BASH_REMATCH[8]}" else echo "Error: Failed to parse DSN" return 1 @@ -46,9 +46,9 @@ parse_dsn() { # Handle empty SOURCE_DATABASE if [[ -z "$SOURCE_DATABASE" ]]; then if [[ "$SOURCE_TYPE" == "POSTGRES" ]]; then - SOURCE_DATABASE="postgres" + export SOURCE_DATABASE="postgres" elif [[ "$SOURCE_TYPE" == "MYSQL" ]]; then - SOURCE_DATABASE="mysql" + export SOURCE_DATABASE="mysql" fi fi @@ -61,18 +61,6 @@ parse_dsn() { echo "SOURCE_DATABASE=$SOURCE_DATABASE" } -if [ -n "$PGSQL_PRIMARY_DSN" ]; then - export PGSQL_PRIMARY_DSN_ARG="-pg-primary-dsn $PGSQL_PRIMARY_DSN" -fi - -if [ -n "$PGSQL_SLOT_NAME" ]; then - export PGSQL_SLOT_NAME_ARG="-pg-slot-name $PGSQL_SLOT_NAME" -fi - -if [ -n "$LOG_LEVEL" ]; then - export LOG_LEVEL="-loglevel $LOG_LEVEL" -fi - # Function to run replica setup run_replica_setup() { case "$SOURCE_TYPE" in @@ -91,13 +79,13 @@ run_replica_setup() { } ;; *) - echo "Error: Invalid SOURCE_TYPE value. Valid options are: MYSQL, POSTGRES." + echo "Error: Invalid SOURCE_TYPE value: ${SOURCE_TYPE}. Valid options are: MYSQL, POSTGRES." exit 1 ;; esac # Run replica_setup.sh and check for errors - if bash replica_setup.sh; then + if source replica_setup.sh; then echo "Replica setup completed." else echo "Error: Replica setup failed." @@ -107,13 +95,13 @@ run_replica_setup() { run_server_in_background() { cd "$DATA_PATH" || { echo "Error: Could not change directory to ${DATA_PATH}"; exit 1; } - nohup myduckserver $PGSQL_PRIMARY_DSN_ARG $PGSQL_SLOT_NAME_ARG $LOG_LEVEL >> "${LOG_PATH}"/server.log 2>&1 & + nohup myduckserver $LOG_LEVEL >> "${LOG_PATH}"/server.log 2>&1 & echo "$!" > "${PID_FILE}" } run_server_in_foreground() { cd "$DATA_PATH" || { echo "Error: Could not change directory to ${DATA_PATH}"; exit 1; } - myduckserver $PGSQL_PRIMARY_DSN_ARG $PGSQL_SLOT_NAME_ARG $LOG_LEVEL + myduckserver $LOG_LEVEL } wait_for_my_duck_server_ready() { @@ -163,6 +151,10 @@ check_process_alive() { # Handle the setup_mode setup() { + if [ -n "$LOG_LEVEL" ]; then + export LOG_LEVEL="-loglevel $LOG_LEVEL" + fi + parse_dsn # Ensure required directories exist mkdir -p "${DATA_PATH}" "${LOG_PATH}" @@ -171,28 +163,12 @@ setup() { echo "Starting MyDuck Server in SERVER mode..." run_server_in_foreground ;; - "REPLICA") echo "Starting MyDuck Server and running replica setup in REPLICA mode..." - - case "$SOURCE_TYPE" in - MYSQL) - run_server_in_background - wait_for_my_duck_server_ready - run_replica_setup - ;; - POSTGRES) - run_replica_setup - run_server_in_background - wait_for_my_duck_server_ready - ;; - *) - echo "Error: Invalid SOURCE_TYPE value. Valid options are: MYSQL, POSTGRES." - exit 1 - ;; - esac + run_server_in_background + wait_for_my_duck_server_ready + run_replica_setup ;; - *) echo "Error: Invalid SETUP_MODE value. Valid options are: SERVER, REPLICA." exit 1