Skip to content

Commit

Permalink
fix: Add comprehensive debug logging for node status updates
Browse files Browse the repository at this point in the history
This commit adds extensive debug logging to trace and prevent empty role issues in node status updates. Key changes include:

- Enhanced logging in `update_node_status()` to capture call stack and input parameters
- Added role validation to default to 'slave' if no role is provided
- Improved JSON generation logging in `generate_node_status_json()`
- Multiple layers of role validation to prevent empty or null roles
- Detailed logging of the JSON being written to etcd

The changes will help diagnose and prevent issues with role configuration in the MySQL cluster management system.
  • Loading branch information
pcfreak30 committed Dec 18, 2024
1 parent f8b97ca commit e54b71d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
25 changes: 21 additions & 4 deletions lib/etcd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,39 @@ generate_node_status_json() {
local role=$2
local additional_fields=${3:-"{}"}

jq -n \
# Debug logging for JSON generation
log_info "=== Generate Node Status JSON Debug ==="
log_info "Input status='$status' role='$role'"
log_info "HOST='$HOST' PORT='${MYSQL_EXTERNAL_PORT}'"

# Ensure role has a valid value
role="${role:-slave}"
if [ -z "$role" ] || [ "$role" = "null" ]; then
log_warn "Empty or null role detected in generate_node_status_json, forcing to 'slave'"
role="slave"
fi

local json=$(jq -n \
--arg status "$status" \
--arg role "${role:-slave}" \
--arg role "$role" \
--arg host "$HOST" \
--arg port "${MYSQL_EXTERNAL_PORT}" \
--arg last_seen "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
--arg gtid "$(get_gtid_position)" \
--argjson extra "$additional_fields" \
'{
status: $status,
role: ($role | if . == "" then "slave" else . end),
role: ($role | if . == "" or . == "null" or . == null then "slave" else . end),
host: $host,
port: $port,
last_seen: $last_seen,
gtid_position: $gtid
} * $extra'
} * $extra')

log_info "Generated JSON: $json"
log_info "========================="

echo "$json"
}

# Update node status in etcd with lease
Expand Down
22 changes: 21 additions & 1 deletion lib/mysql-role.sh
Original file line number Diff line number Diff line change
Expand Up @@ -700,19 +700,39 @@ update_node_status() {
local status=$2
local new_role=$3

# Debug logging
log_info "=== Node Status Update Debug ==="
log_info "Called with: node_id='$node_id' status='$status' new_role='$new_role'"
log_info "CURRENT_ROLE='$CURRENT_ROLE'"
log_info "Call stack:"
local frame=0
while caller $frame; do
((frame++))
done 2>/dev/null
log_info "========================="

if [ -z "$LEASE_ID" ]; then
log_error "No lease ID available for status update"
return 1
fi

# Validate role before update
if [ -z "$new_role" ]; then
log_warn "Empty role provided to update_node_status, using CURRENT_ROLE='${CURRENT_ROLE:-slave}'"
new_role="${CURRENT_ROLE:-slave}"
fi

log_info "Updating node status: $status (role: $new_role)"

# Generate health status JSON
local health_json=$(generate_health_status_json "$HEALTH_STATUS_DETAILS" "0" "0")

# Generate full node status JSON
# Generate full node status JSON with role validation
local status_json=$(generate_node_status_json "$status" "$new_role" "$health_json")

# Debug log the actual JSON being written
log_info "Writing status JSON to etcd: $status_json"

# Update status in etcd
update_node_status_etcd "$node_id" "$status_json"
}
Expand Down

0 comments on commit e54b71d

Please sign in to comment.