From 4f62c2bef3f4fdfbda88d4b1be24bb508bd60591 Mon Sep 17 00:00:00 2001 From: "Derrick Hammer (aider)" Date: Mon, 30 Dec 2024 08:04:44 -0500 Subject: [PATCH] refactor: Centralize MySQL recovery logic and remove redundant calls --- lib/cluster-start.sh | 7 ++----- lib/mysql-init-checks.sh | 13 +++++++++---- lib/mysql-startup.sh | 21 ++++++++++++++++++++- lib/standalone-start.sh | 13 ++++++++----- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/lib/cluster-start.sh b/lib/cluster-start.sh index 6e76848..e1da6a7 100755 --- a/lib/cluster-start.sh +++ b/lib/cluster-start.sh @@ -49,11 +49,8 @@ case $state_code in 0) log_info "Fresh installation needed" ;; 1) log_info "Valid installation detected" ;; 2) - log_warn "Recovery needed - attempting repair" - if ! perform_recovery 0; then - log_error "Recovery failed" - exit 1 - fi + log_error "Recovery needed - startup aborted" + exit 1 ;; *) log_error "Unknown database state" diff --git a/lib/mysql-init-checks.sh b/lib/mysql-init-checks.sh index 5fa5560..d68b878 100644 --- a/lib/mysql-init-checks.sh +++ b/lib/mysql-init-checks.sh @@ -7,11 +7,11 @@ declare -g MYSQL_INIT_CHECKS_SOURCED=1 source "${LIB_PATH}/core/logging.sh" source "${LIB_PATH}/core/constants.sh" -# Detect MySQL installation state and return appropriate code +# Detect MySQL installation state and handle recovery if needed # Returns: # 0 = Fresh Install Needed # 1 = Valid Installation -# 2 = Recovery Needed +# 2 = Recovery Failed # 3 = Error State detect_mysql_state() { local data_dir="${1:-${DATA_DIR}}" @@ -72,8 +72,13 @@ detect_mysql_state() { [ -f "${data_dir}/ib_logfile1" ] || \ [ -f "${data_dir}/aria_log_control" ] || \ [ -f "${data_dir}/#innodb_temp/temp_*.ibt" ]; then - log_warn "Found crash recovery files - recovery needed" - return 2 + log_warn "Found crash recovery files - attempting recovery" + if ! perform_recovery 0; then + log_error "Recovery failed" + return 2 + fi + # After successful recovery, treat as valid installation + return 1 fi # 4. Analyze Installation Type diff --git a/lib/mysql-startup.sh b/lib/mysql-startup.sh index 9225414..9a3f448 100644 --- a/lib/mysql-startup.sh +++ b/lib/mysql-startup.sh @@ -30,8 +30,27 @@ init_mysql() { detect_mysql_state state_code=$? + case $state_code in + 0) # Fresh install needed + log_info "MySQL data directory needs initialization..." + # Proceed with initialization below + ;; + 1) # Valid installation + log_info "Using existing MySQL installation" + return 0 # Skip initialization for valid installations + ;; + 2) # Recovery needed + log_error "Recovery needed - initialization aborted" + return 1 + ;; + *) # Unknown state + log_error "Unknown MySQL state detected" + return 1 + ;; + esac + + # Only proceed with initialization for fresh installs if [ $state_code -eq 0 ]; then - log_info "MySQL data directory needs initialization..." # Ensure directories exist and are writable for dir in "$DATA_DIR" "$RUN_DIR" "$LOG_DIR"; do diff --git a/lib/standalone-start.sh b/lib/standalone-start.sh index f397168..d406ef7 100755 --- a/lib/standalone-start.sh +++ b/lib/standalone-start.sh @@ -35,11 +35,8 @@ case $state_code in 0) log_info "Fresh installation needed" ;; 1) log_info "Valid installation detected" ;; 2) - log_warn "Recovery needed - attempting repair" - if ! perform_recovery 0; then - log_error "Recovery failed" - exit 1 - fi + log_error "Recovery needed - startup aborted" + exit 1 ;; *) log_error "Unknown database state" @@ -47,6 +44,12 @@ case $state_code in ;; esac +# Only proceed with startup for valid states (0 or 1) +if [ $state_code -ne 0 ] && [ $state_code -ne 1 ]; then + log_error "Invalid database state for startup: $state_code" + exit 1 +fi + # Start MySQL in standalone mode (server_id=1 for standalone) if ! start_mysql "$ROLE" 1 "$HOST" "${MYSQL_ARGS[@]}"; then log_error "Failed to start MySQL server"