From aacdd9db177555dce4fb88643276450fd09c30d3 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Wed, 24 Oct 2012 11:17:34 -0600 Subject: [PATCH 1/7] Rewrite find_my_cnf_file() so if a port is given only that port's cnf is returned, else nothing. --- bin/pt-mysql-summary | 25 ++++++------ lib/bash/collect_mysql_info.sh | 29 +++++++------- t/lib/bash/collect_mysql_info.sh | 40 ++++++++++++++++---- t/pt-mysql-summary/samples/ps-mysqld-005.txt | 4 ++ 4 files changed, 63 insertions(+), 35 deletions(-) create mode 100644 t/pt-mysql-summary/samples/ps-mysqld-005.txt diff --git a/bin/pt-mysql-summary b/bin/pt-mysql-summary index 799d4463..17cff681 100755 --- a/bin/pt-mysql-summary +++ b/bin/pt-mysql-summary @@ -793,22 +793,21 @@ find_my_cnf_file() { local port="${2:-""}" local cnf_file="" - if test -n "$port" && grep -- "/mysqld.*--port=$port" "${file}" >/dev/null 2>&1 ; then - cnf_file="$(grep -- "/mysqld.*--port=$port" "${file}" \ - | awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }' \ - | head -n1)" + + if [ "$port" ]; then + cnf_file="$(grep --max-count 1 "/mysqld.*--port=$port" "$file" \ + | awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }')" else - cnf_file="$(grep '/mysqld' "${file}" \ - | awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }' \ - | head -n1)" + cnf_file="$(grep --max-count 1 '/mysqld' "$file" \ + | awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }')" fi - if [ ! -n "${cnf_file}" ]; then - cnf_file="/etc/my.cnf"; - if [ ! -e "${cnf_file}" ]; then - cnf_file="/etc/mysql/my.cnf"; - fi - if [ ! -e "${cnf_file}" ]; then + if [ -z "$cnf_file" ]; then + if [ -e "/etc/my.cnf" ]; then + cnf_file="/etc/my.cnf" + elif [ -e "/etc/mysql/my.cnf" ]; then + cnf_file="/etc/mysql/my.cnf" + elif [ -e "/var/db/mysql/my.cnf" ]; then cnf_file="/var/db/mysql/my.cnf"; fi fi diff --git a/lib/bash/collect_mysql_info.sh b/lib/bash/collect_mysql_info.sh index f4dd7337..ff79deb6 100644 --- a/lib/bash/collect_mysql_info.sh +++ b/lib/bash/collect_mysql_info.sh @@ -60,23 +60,24 @@ find_my_cnf_file() { local port="${2:-""}" local cnf_file="" - if test -n "$port" && grep -- "/mysqld.*--port=$port" "${file}" >/dev/null 2>&1 ; then - cnf_file="$(grep -- "/mysqld.*--port=$port" "${file}" \ - | awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }' \ - | head -n1)" + + if [ "$port" ]; then + # Find the cnf file for the specific port. + cnf_file="$(grep --max-count 1 "/mysqld.*--port=$port" "$file" \ + | awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }')" else - cnf_file="$(grep '/mysqld' "${file}" \ - | awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }' \ - | head -n1)" + # Find the cnf file for the first mysqld instance. + cnf_file="$(grep --max-count 1 '/mysqld' "$file" \ + | awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }')" fi - if [ ! -n "${cnf_file}" ]; then - # "Cannot autodetect config file, trying common locations" - cnf_file="/etc/my.cnf"; - if [ ! -e "${cnf_file}" ]; then - cnf_file="/etc/mysql/my.cnf"; - fi - if [ ! -e "${cnf_file}" ]; then + if [ -z "$cnf_file" ]; then + # Cannot autodetect config file, try common locations. + if [ -e "/etc/my.cnf" ]; then + cnf_file="/etc/my.cnf" + elif [ -e "/etc/mysql/my.cnf" ]; then + cnf_file="/etc/mysql/my.cnf" + elif [ -e "/var/db/mysql/my.cnf" ]; then cnf_file="/var/db/mysql/my.cnf"; fi fi diff --git a/t/lib/bash/collect_mysql_info.sh b/t/lib/bash/collect_mysql_info.sh index 6bdfc5c2..c2226d6f 100644 --- a/t/lib/bash/collect_mysql_info.sh +++ b/t/lib/bash/collect_mysql_info.sh @@ -79,26 +79,50 @@ is \ "collect_internal_vars works" # find_my_cnf_file + +# We know the port is 12345 (2nd to last test), but the sandbox is started +# with just --defaults-file, no --port, so find_my_cnf_file isn't going to +# be able to get the specific cnf file, and the test machine shouldn't have +# any of the default files (/etc/my.cnf, etc.). cnf_file=$(find_my_cnf_file "$p/mysqld-instances" ${port}); -is \ - "$cnf_file" \ - "/tmp/12345/my.sandbox.cnf" \ - "find_my_cnf_file gets the correct file" +is "$cnf_file" "" "find_my_cnf_file gets the correct file" [ $? -ne 0 ] && diag "$p/mysqld-instances" +# ps-mysqld-001.txt has several instances: +# port 3306 cnf - +# port 12345 cnf /tmp/12345/my.sandbox.cnf +# port 12346 cnf /tmp/12346/my.sandbox.cnf + res=$(find_my_cnf_file "$samples/ps-mysqld-001.txt") -is "$res" "/tmp/12345/my.sandbox.cnf" "ps-mysqld-001.txt" +is "$res" "" "ps-mysqld-001.txt no port" + +res=$(find_my_cnf_file "$samples/ps-mysqld-001.txt" 3306) +is "$res" "" "ps-mysqld-001.txt port but no cnf" + +res=$(find_my_cnf_file "$samples/ps-mysqld-001.txt" 999) +is "$res" "" "ps-mysqld-001.txt nonexistent port" res=$(find_my_cnf_file "$samples/ps-mysqld-001.txt" 12346) -is "$res" "/tmp/12346/my.sandbox.cnf" "ps-mysqld-001.txt with port" +is "$res" "/tmp/12346/my.sandbox.cnf" "ps-mysqld-001.txt port 12346" + +res=$(find_my_cnf_file "$samples/ps-mysqld-001.txt" 12345) +is "$res" "/tmp/12345/my.sandbox.cnf" "ps-mysqld-001.txt port 12345" + +# ps-mysqld-004.txt has 1 instance without --port using +# --defaults-file=/var/lib/mysql/my.cnf res=$(find_my_cnf_file "$samples/ps-mysqld-004.txt") -is "$res" "/var/lib/mysql/my.cnf" "ps-mysqld-004.txt" +is "$res" "/var/lib/mysql/my.cnf" "ps-mysqld-004.txt no port" res=$(find_my_cnf_file "$samples/ps-mysqld-004.txt" 12345) -is "$res" "/var/lib/mysql/my.cnf" "ps-mysqld-004.txt with port" +is "$res" "" "ps-mysqld-004.txt port 12345" +# ps-mysqld-005.txt has the 3 sandbox instances, but 12347 +# is first, which was causing bug 1070916. + +res=$(find_my_cnf_file "$samples/ps-mysqld-005.txt" 12345) +is "$res" "" "ps-mysqld-005.txt port 12345 (bug 1070916)" # collect_mysql_databases $CMD_MYSQL $EXT_ARGV -ss -e 'SHOW DATABASES' > "$PT_TMPDIR/mysql_collect_databases" 2>/dev/null diff --git a/t/pt-mysql-summary/samples/ps-mysqld-005.txt b/t/pt-mysql-summary/samples/ps-mysqld-005.txt new file mode 100644 index 00000000..2f41faf5 --- /dev/null +++ b/t/pt-mysql-summary/samples/ps-mysqld-005.txt @@ -0,0 +1,4 @@ + PID TTY STAT TIME COMMAND + 1427 ? Sl 0:13 /home/jenkins/mysql-bin/mysql-5.5.24-i386-barebones/bin/mysqld --defaults-file=/tmp/12347/my.sandbox.cnf + 20928 ? Sl 0:07 /home/jenkins/mysql-bin/mysql-5.5.24-i386-barebones/bin/mysqld --defaults-file=/tmp/12345/my.sandbox.cnf + 29930 ? Sl 0:00 /home/jenkins/mysql-bin/mysql-5.5.24-i386-barebones/bin/mysqld --defaults-file=/tmp/12346/my.sandbox.cnf From a94605c825f7501b552e198c1a5040e9244f2267 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Wed, 24 Oct 2012 12:08:30 -0600 Subject: [PATCH 2/7] Use sys default cnf file since test machines have different ones. --- t/lib/bash/collect_mysql_info.sh | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/t/lib/bash/collect_mysql_info.sh b/t/lib/bash/collect_mysql_info.sh index c2226d6f..d021460c 100644 --- a/t/lib/bash/collect_mysql_info.sh +++ b/t/lib/bash/collect_mysql_info.sh @@ -80,13 +80,24 @@ is \ # find_my_cnf_file +# Test machines may have one of these, and find_my_cnf_file will use +# the same if the specific port-based cnf file isn't found. +if [ -e "/etc/my.cnf" ]; then + sys_cnf_file="/etc/my.cnf" +elif [ -e "/etc/mysql/my.cnf" ]; then + sys_cnf_file="/etc/mysql/my.cnf" +elif [ -e "/var/db/mysql/my.cnf" ]; then + sys_cnf_file="/var/db/mysql/my.cnf"; +else + sys_cnf_file="" +fi + # We know the port is 12345 (2nd to last test), but the sandbox is started # with just --defaults-file, no --port, so find_my_cnf_file isn't going to -# be able to get the specific cnf file, and the test machine shouldn't have -# any of the default files (/etc/my.cnf, etc.). +# be able to get the specific cnf file. cnf_file=$(find_my_cnf_file "$p/mysqld-instances" ${port}); -is "$cnf_file" "" "find_my_cnf_file gets the correct file" +is "$cnf_file" "$sys_cnf_file" "find_my_cnf_file gets the correct file" [ $? -ne 0 ] && diag "$p/mysqld-instances" # ps-mysqld-001.txt has several instances: @@ -95,13 +106,13 @@ is "$cnf_file" "" "find_my_cnf_file gets the correct file" # port 12346 cnf /tmp/12346/my.sandbox.cnf res=$(find_my_cnf_file "$samples/ps-mysqld-001.txt") -is "$res" "" "ps-mysqld-001.txt no port" +is "$res" "$sys_cnf_file" "ps-mysqld-001.txt no port" res=$(find_my_cnf_file "$samples/ps-mysqld-001.txt" 3306) -is "$res" "" "ps-mysqld-001.txt port but no cnf" +is "$res" "$sys_cnf_file" "ps-mysqld-001.txt port but no cnf" res=$(find_my_cnf_file "$samples/ps-mysqld-001.txt" 999) -is "$res" "" "ps-mysqld-001.txt nonexistent port" +is "$res" "$sys_cnf_file" "ps-mysqld-001.txt nonexistent port" res=$(find_my_cnf_file "$samples/ps-mysqld-001.txt" 12346) is "$res" "/tmp/12346/my.sandbox.cnf" "ps-mysqld-001.txt port 12346" @@ -116,13 +127,13 @@ res=$(find_my_cnf_file "$samples/ps-mysqld-004.txt") is "$res" "/var/lib/mysql/my.cnf" "ps-mysqld-004.txt no port" res=$(find_my_cnf_file "$samples/ps-mysqld-004.txt" 12345) -is "$res" "" "ps-mysqld-004.txt port 12345" +is "$res" "$sys_cnf_file" "ps-mysqld-004.txt port 12345" # ps-mysqld-005.txt has the 3 sandbox instances, but 12347 # is first, which was causing bug 1070916. res=$(find_my_cnf_file "$samples/ps-mysqld-005.txt" 12345) -is "$res" "" "ps-mysqld-005.txt port 12345 (bug 1070916)" +is "$res" "$sys_cnf_file" "ps-mysqld-005.txt port 12345 (bug 1070916)" # collect_mysql_databases $CMD_MYSQL $EXT_ARGV -ss -e 'SHOW DATABASES' > "$PT_TMPDIR/mysql_collect_databases" 2>/dev/null From b6072ab0114ff21bd04f375f703e2c2a8029d4df Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Wed, 24 Oct 2012 12:43:26 -0600 Subject: [PATCH 3/7] Only cat cnf file > mysql-config-file if it exists. --- lib/bash/collect_mysql_info.sh | 2 +- t/lib/bash/collect_mysql_info.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/bash/collect_mysql_info.sh b/lib/bash/collect_mysql_info.sh index ff79deb6..ba35831a 100644 --- a/lib/bash/collect_mysql_info.sh +++ b/lib/bash/collect_mysql_info.sh @@ -234,7 +234,7 @@ collect_mysql_info () { local port="$(get_var port "$dir/mysql-variables")" local cnf_file="$(find_my_cnf_file "$dir/mysqld-instances" ${port})" - cat "$cnf_file" > "$dir/mysql-config-file" + [ -e "$cnf_file" ] && cat "$cnf_file" > "$dir/mysql-config-file" local pid_file="$(get_var "pid_file" "$dir/mysql-variables")" local pid_file_exists="" diff --git a/t/lib/bash/collect_mysql_info.sh b/t/lib/bash/collect_mysql_info.sh index d021460c..390d051c 100644 --- a/t/lib/bash/collect_mysql_info.sh +++ b/t/lib/bash/collect_mysql_info.sh @@ -28,7 +28,7 @@ wait file_count=$(ls "$p" | wc -l) -is $file_count 14 "Creates the correct number of files (without --databases)" +is $file_count 13 "Creates the correct number of files (without --databases)" awk '{print $1}' "$p/mysqld-instances" > "$PT_TMPDIR/collect_mysqld_instances1.test" pids="$(_pidof mysqld)" From bb44887615f74001988916935454ec491598d26f Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Wed, 24 Oct 2012 12:45:10 -0600 Subject: [PATCH 4/7] Update collect_mysql_info in pt-mysql-summary. --- bin/pt-mysql-summary | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/pt-mysql-summary b/bin/pt-mysql-summary index 17cff681..35336559 100755 --- a/bin/pt-mysql-summary +++ b/bin/pt-mysql-summary @@ -953,7 +953,7 @@ collect_mysql_info () { local port="$(get_var port "$dir/mysql-variables")" local cnf_file="$(find_my_cnf_file "$dir/mysqld-instances" ${port})" - cat "$cnf_file" > "$dir/mysql-config-file" + [ -e "$cnf_file" ] && cat "$cnf_file" > "$dir/mysql-config-file" local pid_file="$(get_var "pid_file" "$dir/mysql-variables")" local pid_file_exists="" From 70b666add97f237314cd0326f76fbbb5c97e6c12 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Wed, 24 Oct 2012 12:46:22 -0600 Subject: [PATCH 5/7] Update pt-mysql-summary.t. --- t/pt-mysql-summary/pt-mysql-summary.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/pt-mysql-summary/pt-mysql-summary.t b/t/pt-mysql-summary/pt-mysql-summary.t index fb274639..7d7da788 100644 --- a/t/pt-mysql-summary/pt-mysql-summary.t +++ b/t/pt-mysql-summary/pt-mysql-summary.t @@ -36,7 +36,7 @@ my @files = glob("$dir/*"); is( scalar @files, - 15, + 14, "And leaves all files in there" ); From 9b920bc5a5814c366d4bcae4d8d29051f8b8b6f6 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Wed, 24 Oct 2012 13:03:45 -0600 Subject: [PATCH 6/7] Conditionalize the number of files expected: 14 or 15 depending on if the box has a default my.cnf somewhere. --- t/lib/bash/collect_mysql_info.sh | 33 ++++++++++++++++----------- t/pt-mysql-summary/pt-mysql-summary.t | 11 +++++---- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/t/lib/bash/collect_mysql_info.sh b/t/lib/bash/collect_mysql_info.sh index 390d051c..7a8c8a26 100644 --- a/t/lib/bash/collect_mysql_info.sh +++ b/t/lib/bash/collect_mysql_info.sh @@ -18,6 +18,19 @@ samples="$PERCONA_TOOLKIT_BRANCH/t/pt-mysql-summary/samples" mkdir "$p" +# This is mostly for the find_my_cnf_file tests. +# Test machines may have one of these, and find_my_cnf_file will use +# the same if the specific port-based cnf file isn't found. +if [ -e "/etc/my.cnf" ]; then + sys_cnf_file="/etc/my.cnf" +elif [ -e "/etc/mysql/my.cnf" ]; then + sys_cnf_file="/etc/mysql/my.cnf" +elif [ -e "/var/db/mysql/my.cnf" ]; then + sys_cnf_file="/var/db/mysql/my.cnf"; +else + sys_cnf_file="" +fi + parse_options "$BIN_DIR/pt-mysql-summary" --sleep 1 -- --defaults-file=/tmp/12345/my.sandbox.cnf CMD_MYSQL="$(_which mysql)" @@ -28,7 +41,13 @@ wait file_count=$(ls "$p" | wc -l) -is $file_count 13 "Creates the correct number of files (without --databases)" +if [ "$sys_cnf_file" ]; then + n_files=14 +else + n_files=13 +fi + +is $file_count $n_files "Creates the correct number of files (without --databases)" awk '{print $1}' "$p/mysqld-instances" > "$PT_TMPDIR/collect_mysqld_instances1.test" pids="$(_pidof mysqld)" @@ -80,18 +99,6 @@ is \ # find_my_cnf_file -# Test machines may have one of these, and find_my_cnf_file will use -# the same if the specific port-based cnf file isn't found. -if [ -e "/etc/my.cnf" ]; then - sys_cnf_file="/etc/my.cnf" -elif [ -e "/etc/mysql/my.cnf" ]; then - sys_cnf_file="/etc/mysql/my.cnf" -elif [ -e "/var/db/mysql/my.cnf" ]; then - sys_cnf_file="/var/db/mysql/my.cnf"; -else - sys_cnf_file="" -fi - # We know the port is 12345 (2nd to last test), but the sandbox is started # with just --defaults-file, no --port, so find_my_cnf_file isn't going to # be able to get the specific cnf file. diff --git a/t/pt-mysql-summary/pt-mysql-summary.t b/t/pt-mysql-summary/pt-mysql-summary.t index 7d7da788..8898d2d9 100644 --- a/t/pt-mysql-summary/pt-mysql-summary.t +++ b/t/pt-mysql-summary/pt-mysql-summary.t @@ -32,13 +32,14 @@ ok( "Using --save-samples doesn't mistakenly delete the target dir" ); +# If the box has a default my.cnf (e.g. /etc/my.cnf) there +# should be 15 files, else 14. my @files = glob("$dir/*"); - -is( - scalar @files, - 14, +my $n_files = scalar @files; +ok( + $n_files == 15 || $n_files == 14, "And leaves all files in there" -); +) or diag($n_files, `ls -l $dir`); undef($dir); From 33f75e069d994b4b632a3558f9213aac3998764e Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Wed, 24 Oct 2012 13:36:32 -0600 Subject: [PATCH 7/7] Fix some test plans. --- t/lib/bash/collect_mysql_info.sh | 2 +- t/lib/bash/report_mysql_info.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/t/lib/bash/collect_mysql_info.sh b/t/lib/bash/collect_mysql_info.sh index 7a8c8a26..c25af7e6 100644 --- a/t/lib/bash/collect_mysql_info.sh +++ b/t/lib/bash/collect_mysql_info.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -plan 20 +plan 24 PT_TMPDIR="$TEST_PT_TMPDIR" PATH="$PATH:$PERCONA_TOOLKIT_SANDBOX/bin" diff --git a/t/lib/bash/report_mysql_info.sh b/t/lib/bash/report_mysql_info.sh index 8ffd9d88..b753120d 100644 --- a/t/lib/bash/report_mysql_info.sh +++ b/t/lib/bash/report_mysql_info.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -plan 32 +plan 33 . "$LIB_DIR/alt_cmds.sh" . "$LIB_DIR/log_warn_die.sh"