From 7e464dc3f8e1e8d4bd7ae8c17034ff70a514cec3 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Wed, 1 Aug 2012 09:40:05 -0600 Subject: [PATCH] Wait 1 minute to start and stop sandboxes. Report how long start/stop took. Clean up code. --- sandbox/servers/start | 72 ++++++++++++++++++++++++------------------- sandbox/servers/stop | 62 ++++++++++++++++++++++++------------- sandbox/start-sandbox | 25 +++++++-------- sandbox/test-env | 2 +- 4 files changed, 94 insertions(+), 67 deletions(-) diff --git a/sandbox/servers/start b/sandbox/servers/start index 7e849ea1..5137733e 100755 --- a/sandbox/servers/start +++ b/sandbox/servers/start @@ -1,61 +1,69 @@ #!/bin/sh +start_ts=$(date +%s) + PIDFILE="/tmp/PORT/data/mysql_sandboxPORT.pid" SOCKETFILE="/tmp/PORT/mysql_sandboxPORT.sock" BASEDIR="PERCONA_TOOLKIT_SANDBOX" sandbox_is_alive() { - local pid=`cat /tmp/PORT/data/mysql_sandboxPORT.pid 2>/dev/null` - if [ -z "$pid" ]; then - return 0 - fi - kill -0 $pid - local ps_alive=$? + # First, all these files must exist. + [ -f $PIDFILE -a -S $SOCKETFILE -a -s "/tmp/PORT/data/ibdata1" ] || return 1 + # And that PID file must have a PID. + local pid=$(cat /tmp/PORT/data/mysql_sandboxPORT.pid 2>/dev/null) + [ "$pid" ] || return 1 + + # Second, MySQL is truly alive when it respond to a ping. + # It's not enough that the mysqld process is running because + # InnoDB can take time to create ibdata1, etc. So MySQL is + # only alive when it responds to queries. $BASEDIR/bin/mysqladmin --defaults-file="/tmp/PORT/my.sandbox.cnf" ping >/dev/null 2>&1 - local mysql_alive=$? + [ $? -eq 0 ] || return 1 - if [ $ps_alive -eq 0 ] && [ $mysql_alive -eq 0 ]; then - return 1 # sandbox is alive - else - return 0 - fi + return 0 } -if [ -f "$PIDFILE" ] || [ -S "$SOCKETFILE" ]; then - sandbox_is_alive - if [ $? -eq 1 ]; then +_seq() { + local i="$1" + awk "BEGIN { for(i=1; i<=$i; i++) print i; }" +} + +# ############################################################################# +# Script starts here +# ############################################################################# + +# If there's a PID or socket file, MySQL may already be alive. +if [ -f "$PIDFILE" -o -S "$SOCKETFILE" ]; then + if sandbox_is_alive; then echo "MySQL test server on port PORT is running." exit 0 fi # Sandbox exists but is not running. Clear it and then start it. /tmp/PORT/stop >/dev/null 2>&1 - rm -rf $PIDFILE - rm -rf $SOCKETFILE fi -PWD=`pwd` +echo -n "Starting MySQL test server on port PORT... " + +# Start MySQL. +cwd=$PWD cd $BASEDIR -$BASEDIR/bin/mysqld_safe --defaults-file=/tmp/PORT/my.sandbox.cnf > /dev/null 2>&1 & +$BASEDIR/bin/mysqld --defaults-file=/tmp/PORT/my.sandbox.cnf > /dev/null 2>&1 & cd $PWD -echo -n "Starting MySQL test server on port PORT... " -for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do - if [ -f $PIDFILE -a -S $SOCKETFILE -a -s "/tmp/PORT/data/ibdata1" ]; then - break - fi - sleep 1 -done - -for i in 1 2 3 4 5; do - sandbox_is_alive - if [ $? -eq 1 ]; then - echo "OK" +# Wait for MySQL to actually be up, i.e. to respond to queries. +for i in $(_seq 60); do + if sandbox_is_alive; then + end_ts=$(date +%s) + t=$((end_ts - start_ts)) + echo "OK (${t}s)" exit 0 fi sleep 1 done -echo "FAILED" +end_ts=$(date +%s) +t=$((end_ts - start_ts)) +echo "FAILED (${t}s)" exit 1 diff --git a/sandbox/servers/stop b/sandbox/servers/stop index 83e1df6e..51910255 100755 --- a/sandbox/servers/stop +++ b/sandbox/servers/stop @@ -1,40 +1,58 @@ #!/bin/sh +start_ts=$(date +%s) + PIDFILE="/tmp/PORT/data/mysql_sandboxPORT.pid" +SOCKETFILE="/tmp/PORT/mysql_sandboxPORT.sock" BASEDIR="PERCONA_TOOLKIT_SANDBOX" sandbox_is_alive() { - local pid=`cat /tmp/PORT/data/mysql_sandboxPORT.pid 2>/dev/null` - if [ -z "$pid" ]; then - return 0 - fi - kill -0 $pid - local ps_alive=$? + # First, all these files must exist. + [ -f $PIDFILE -a -S $SOCKETFILE -a -s "/tmp/PORT/data/ibdata1" ] || return 1 + # And that PID file must have a PID. + local pid=$(cat /tmp/PORT/data/mysql_sandboxPORT.pid 2>/dev/null) + [ "$pid" ] || return 1 + + # Second, MySQL is truly alive when it respond to a ping. + # It's not enough that the mysqld process is running because + # InnoDB can take time to create ibdata1, etc. So MySQL is + # only alive when it responds to queries. $BASEDIR/bin/mysqladmin --defaults-file="/tmp/PORT/my.sandbox.cnf" ping >/dev/null 2>&1 - local mysql_alive=$? + [ $? -eq 0 ] || return 1 - if [ $ps_alive -eq 0 ] && [ $mysql_alive -eq 0 ]; then - return 1 # sandbox is alive - else - return 0 - fi + return 0 } -exit_status=0 +_seq() { + local i="$1" + awk "BEGIN { for(i=1; i<=$i; i++) print i; }" +} + +# ############################################################################# +# Script starts here +# ############################################################################# echo -n "Stopping MySQL test server on port PORT... " -sandbox_is_alive -if [ $? -eq 1 ]; then +if sandbox_is_alive; then $BASEDIR/bin/mysqladmin --defaults-file=/tmp/PORT/my.sandbox.cnf shutdown - exit_status=$? fi -if [ $exit_status -eq 0 ]; then - echo "OK" -else - echo "FAILED" -fi +for i in $(_seq 60); do + if sandbox_is_alive; then + sleep 1 + continue + fi + rm -rf $PIDFILE 2>/dev/null + rm -rf $SOCKETFILE 2>/dev/null + end_ts=$(date +%s) + t=$((end_ts - start_ts)) + echo "OK (${t}s)" + exit 0 +done -exit $exit_status +end_ts=$(date +%s) +t=$((end_ts - start_ts)) +echo "FAILED (${t}s)" +exit 1 diff --git a/sandbox/start-sandbox b/sandbox/start-sandbox index d116c39c..df1b5353 100755 --- a/sandbox/start-sandbox +++ b/sandbox/start-sandbox @@ -1,8 +1,13 @@ #!/bin/sh -# This script makes Maatkit sandbox servers for mk-test-env. -# It's a "low level" script that is not usually called directly. -# Exit 0 means everything was successful, else exit 1 on problems. +# This script starts a Percona Toolkit sandbox sever. sandbox/test-env +# uses it, and many tests use it to create special, temporary sandbox +# servers. The actual startup is done by /tmp/PORT/start, which this +# script calls after doing a bunch of sanity checks. +# +# Exit 0 if the sandbox server started ok, else 1 and debug_sandbox() +# is caleld to print some info to STDERR about what state the sandbox +# server might have been in. die() { echo $1 >&2 @@ -10,16 +15,12 @@ die() { } debug_sandbox() { + set -x local port="$1" - echo - echo "MySQL processes:" >&2 ps x | grep mysql >&2 - echo if [ -d "/tmp/$port" ]; then ls -lh /tmp/$port/* >&2 - echo cat /tmp/$port/data/mysqld.log >&2 - echo tail -n 100 /tmp/$port/data/genlog >&2 else echo "/tmp/$port does not exist" >&2 @@ -28,8 +29,8 @@ debug_sandbox() { make_sandbox() { # Make the sandbox dir and extract the base files. - rm -rf /tmp/$port || die "can't rm /tmp/$port" - mkdir /tmp/$port || die "mkdir /tmp/$port failed" + rm -rf /tmp/$port || die "Failed to rm /tmp/$port" + mkdir /tmp/$port || die "Failed to mkdir /tmp/$port" cp $PERCONA_TOOLKIT_BRANCH/sandbox/servers/$version/my.sandbox.cnf /tmp/$port tar xzf $PERCONA_TOOLKIT_BRANCH/sandbox/servers/$version/data.tar.gz -C /tmp/$port @@ -84,7 +85,7 @@ make_sandbox() { /tmp/$port/use -e 'SHOW /*!40100 ENGINE*/ INNODB STATUS' | grep 'INNODB MONITOR OUTPUT' >/dev/null 2>&1 # grep exits 0 if lines are found if [ $? -ne 0 ]; then - echo "Sandbox $type $port doesn't have InnoDB" >&2 + echo "Sandbox $type $port doesn't have InnoDB." >&2 debug_sandbox $port exit 1 fi @@ -146,7 +147,7 @@ fi cd $PERCONA_TOOLKIT_BRANCH/sandbox -# This script is usually called by mk-test-env which discovers and +# This script is usually called by test-env which discovers and # sets PERCONA_TOOLKIT_SANDBOX. If this script is called directly, # then the caller is reponsible for setting PERCONA_TOOLKIT_SANDBOX. # PERCONA_TOOLKIT_SANDBOX points to a base directory containing the diff --git a/sandbox/test-env b/sandbox/test-env index 95e5b900..3643a848 100755 --- a/sandbox/test-env +++ b/sandbox/test-env @@ -345,7 +345,7 @@ case $opt in fi ;; kill) - # This is a blunt approach for killing the entire mk test env + # This is a blunt approach for killing the entire test env # when a polite stop fails. It uses kill -9 as a last resort. for port in 12349 12348 12347 12346 12345 2903 2902 2901 2900; do kill_sandbox $port