#!/usr/bin/env bash # Verification for: show-and-detect-deadlocks # Self-contained. Builds its own scratch schema (hw_rw_deadlock_), fires two real MySQL # connections that take row locks in opposite order, confirms one gets ER_LOCK_DEADLOCK (1213), # and confirms SHOW ENGINE INNODB STATUS actually recorded a LATEST DETECTED DEADLOCK block. # Also reports (does not assert exact values for) innodb_deadlock_detect and # innodb_lock_wait_timeout, since those are legitimately server-specific. # # Rows are cleared at the end; the empty schema is left behind because this environment blocks # DROP DATABASE/DROP TABLE outright. The exact manual cleanup command is printed at the end - # never run automatically, per this project's standing policy on destructive SQL. # # Override the client with: MYSQL_CMD="mysql -u root -p'...' -h 127.0.0.1" ./verify-...sh set -u PASS=0 FAIL=0 SKIP=0 CONTENT=0 pass() { echo "PASS: $1"; PASS=$((PASS+1)); } fail() { echo "FAIL: $1"; FAIL=$((FAIL+1)); } skip() { echo "SKIP: $1"; SKIP=$((SKIP+1)); } content_pass() { echo "CONTENT: $1"; CONTENT=$((CONTENT+1)); } # Resolve the article path to an ABSOLUTE directory before any cd. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ARTICLE="$SCRIPT_DIR/show-and-detect-deadlocks.html" if [ -f "$ARTICLE" ]; then for needle in "ERROR 1213" "LATEST DETECTED DEADLOCK" "innodb_deadlock_detect" "innodb_lock_wait_timeout"; do if grep -qF "$needle" "$ARTICLE"; then content_pass "article mentions '$needle'" else fail "article is missing expected content: '$needle'" fi done else skip "article HTML not found next to script - content checks skipped" fi MYSQL_CMD="${MYSQL_CMD:-mysql -u root}" if ! command -v mysql >/dev/null 2>&1; then skip "mysql client not found on PATH - executed checks skipped" echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP CONTENT=$CONTENT" [ "$FAIL" -eq 0 ] exit $? fi if ! $MYSQL_CMD -e "SELECT 1;" >/dev/null 2>&1; then skip "cannot connect to MySQL with '$MYSQL_CMD' - executed checks skipped" echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP CONTENT=$CONTENT" [ "$FAIL" -eq 0 ] exit $? fi DB="hw_rw_deadlock_$$" WORKDIR="$(mktemp -d 2>/dev/null || echo /tmp/hw_w20_deadlock_$$)" mkdir -p "$WORKDIR" cleanup() { # Clear rows only - DROP DATABASE/TABLE is blocked in this environment by design. $MYSQL_CMD -e "DELETE FROM $DB.accounts WHERE id IN (1,2);" >/dev/null 2>&1 rm -rf "$WORKDIR" } trap cleanup EXIT if ! $MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS $DB;" >/dev/null 2>&1; then skip "could not create scratch database - executed checks skipped" echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP CONTENT=$CONTENT" [ "$FAIL" -eq 0 ] exit $? fi $MYSQL_CMD -e "CREATE TABLE IF NOT EXISTS $DB.accounts (id INT PRIMARY KEY, balance INT) ENGINE=InnoDB;" >/dev/null 2>&1 $MYSQL_CMD -e "INSERT INTO $DB.accounts (id, balance) VALUES (1,100),(2,200) ON DUPLICATE KEY UPDATE balance=VALUES(balance);" >/dev/null 2>&1 cat > "$WORKDIR/a.sql" < "$WORKDIR/b.sql" < "$WORKDIR/a.out" 2>&1 ) & sleep 0.6 ( $MYSQL_CMD < "$WORKDIR/b.sql" > "$WORKDIR/b.out" 2>&1 ) & wait if grep -q "ERROR 1213" "$WORKDIR/a.out" "$WORKDIR/b.out" 2>/dev/null; then pass "one of the two concurrent transactions failed with ERROR 1213 (deadlock)" else fail "neither transaction reported ERROR 1213 - deadlock was not reproduced this run" fi $MYSQL_CMD -e "SHOW ENGINE INNODB STATUS" > "$WORKDIR/status.txt" 2>&1 if grep -q "LATEST DETECTED DEADLOCK" "$WORKDIR/status.txt" 2>/dev/null; then pass "SHOW ENGINE INNODB STATUS recorded a LATEST DETECTED DEADLOCK section" else fail "SHOW ENGINE INNODB STATUS has no LATEST DETECTED DEADLOCK section" fi if grep -q "WE ROLL BACK TRANSACTION" "$WORKDIR/status.txt" 2>/dev/null; then pass "InnoDB's status output names the rolled-back victim transaction" else fail "InnoDB's status output does not show a rollback victim line" fi DETECT="$($MYSQL_CMD -N -e "SHOW VARIABLES LIKE 'innodb_deadlock_detect';" 2>/dev/null | awk '{print $2}')" TIMEOUT="$($MYSQL_CMD -N -e "SHOW VARIABLES LIKE 'innodb_lock_wait_timeout';" 2>/dev/null | awk '{print $2}')" if [ -n "$DETECT" ] && [ -n "$TIMEOUT" ]; then pass "read live server settings: innodb_deadlock_detect=$DETECT innodb_lock_wait_timeout=$TIMEOUT" else fail "could not read innodb_deadlock_detect / innodb_lock_wait_timeout" fi echo "manual cleanup (not run automatically - destructive SQL is not executed by this script):" echo " DROP DATABASE $DB;" echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP CONTENT=$CONTENT" [ "$FAIL" -eq 0 ]