Skip to content

Commit

Permalink
refactor: Centralize MySQL recovery logic and remove redundant calls
Browse files Browse the repository at this point in the history
  • Loading branch information
pcfreak30 committed Dec 30, 2024
1 parent 1a6b05d commit 4f62c2b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
7 changes: 2 additions & 5 deletions lib/cluster-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 9 additions & 4 deletions lib/mysql-init-checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}}"
Expand Down Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion lib/mysql-startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions lib/standalone-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,21 @@ 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"
exit 1
;;
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"
Expand Down

0 comments on commit 4f62c2b

Please sign in to comment.