Test that pt-kill reconnects using new util/kill-mysql-process. Fix optional DSN by adding magical options rule.

This commit is contained in:
Daniel Nichter
2012-07-12 16:49:15 -06:00
parent dab2e5c692
commit b1c6bba43a
4 changed files with 85 additions and 12 deletions

View File

@@ -4086,6 +4086,7 @@ sub main {
eval {
$dbh = $cxn->connect();
$proc_sth = $dbh->prepare('SHOW FULL PROCESSLIST');
msg('Reconnected to ' . $cxn->name());
};
return 1 unless $EVAL_ERROR; # try again
}
@@ -4102,6 +4103,7 @@ sub main {
my $kill_sth = $dbh->prepare($kill_sql);
$kill = sub {
my ($id) = @_;
PTDEBUG && _d('Killing process', $id);
return $retry->retry(
try => sub {
return $kill_sth->execute($id);
@@ -4116,6 +4118,7 @@ sub main {
eval {
$dbh = $cxn->connect();
$kill_sth = $dbh->prepare($kill_sql);
msg('Reconnected to ' . $cxn->name());
};
return 1 unless $EVAL_ERROR; # try again
}
@@ -4371,16 +4374,6 @@ sub main {
# Subroutines.
# ############################################################################
sub get_cxn {
my ( $dp, $dsn, $ac ) = @_;
if ( $o->get('ask-pass') ) {
$dsn->{p} = OptionParser::prompt_noecho("Enter password: ");
}
my $dbh = $dp->get_dbh($dp->get_cxn_params($dsn), {AutoCommit => $ac});
$dbh->{InactiveDestroy} = 1; # Because of forking.
return $dbh;
}
# Forks and detaches from parent to execute the given command;
# does not block parent.
sub exec_cmd {
@@ -4604,6 +4597,9 @@ L<"--kill"> and L<"--kill-query"> are mutually exclusive.
L<"--daemonize"> and L<"--test-matching"> are mutually exclusive.
This tool accepts additional command-line arguments. Refer to the
L<"SYNOPSIS"> and usage information for details.
=over
=item --ask-pass
@@ -4626,6 +4622,12 @@ type: Array
Read this comma-separated list of config files; if specified, this must be the
first option on the command line.
=item --database
short form: -D; type: string
The database to use for the connection.
=item --daemonize
Fork to the background and detach from the shell. POSIX operating systems

View File

@@ -4,6 +4,7 @@ BEGIN {
die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
$ENV{PERCONA_TOOLKIT_TEST_USE_DSN_NAMES} = 1;
};
use strict;
@@ -25,7 +26,7 @@ if ( !$master_dbh ) {
plan skip_all => 'Cannot connect to sandbox master';
}
else {
plan tests => 4;
plan tests => 6;
}
my $output;
@@ -79,6 +80,32 @@ like(
'--verbose'
);
# #############################################################################
# Reconnect if cxn lost.
# #############################################################################
$master_dbh->do("CREATE DATABASE IF NOT EXISTS pt_kill_test");
system(qq($trunk/util/kill-mysql-process db=pt_kill_test wait=2 &));
$output = output(
sub { pt_kill::main('-F', $cnf, qw(-D pt_kill_test),
qw(--run-time 4 --interval 1 --print --verbose)) },
stderr => 1,
);
like(
$output,
qr/Reconnected/,
"kill-mysql-process says it reconnected"
);
my $n_checks =()= $output =~ m/Checking processlist/g;
is(
$n_checks,
4,
"pt-kill still checked the processlist 4 times"
) or diag($output);
# #############################################################################
# Done.
# #############################################################################

View File

@@ -41,7 +41,7 @@ my $cnf='/tmp/12345/my.sandbox.cnf';
# Shell out to a sleep(10) query and try to capture the query.
# Backticks don't work here.
system("/tmp/12345/use -h127.1 -P12345 -umsandbox -pmsandbox -e 'select sleep(4)' >/dev/null&");
system("/tmp/12345/use -h127.1 -P12345 -umsandbox -pmsandbox -e 'select sleep(4)' >/dev/null 2>&1 &");
sleep 0.5;
my $rows = $dbh->selectall_hashref('show processlist', 'id');
my $pid;

44
util/kill-mysql-process Executable file
View File

@@ -0,0 +1,44 @@
#!/usr/bin/env perl
# This script helps test that tools reconnect to MySQL. Its meant to be ran
# in the background like system(qq($trunk/util/kill-mysql-process DB)) where
# DB is the name of special "tracer" database used to isolate the test in
# the process list. So, do something like CREATE DATABASE pt_kill_test, then
# run the test with D=pt_kill_test (presuming pt_kill_test is unique to
# the test). This script will then kill any and all processes that are using
# the pt_kill_test db.
#
# Exits 0 if the tracer db is observed and procs are killed, else exits 1.
use strict;
use warnings FATAL => 'all';
use Time::HiRes qw(sleep time);
if ( !@ARGV || @ARGV < 1 || @ARGV > 3 ) {
print STDERR "Usage: kill-mysql-process OPTION=VALUE\n";
print STDERR "Options: db, wait, runtime, interval\n";
exit 1;
}
my %opt = map { my ($op, $val) = split '=', $_; $op => $val; } @ARGV;
$opt{db} ||= 'tracer_db';
$opt{runtime} ||= 5.0;
$opt{interval} ||= 0.2;
sleep $opt{wait} if $opt{wait};
my $t_start = time;
while ( time - $t_start < $opt{runtime} ) {
my $procs = `/tmp/12345/use -ss -e "show processlist" | grep $opt{db} | cut -f1`;
if ( $procs && $procs =~ /\d/ ) {
foreach my $proc ( split "\n", $procs ) {
chomp $proc;
`/tmp/12345/use -e "KILL $proc"`;
}
exit 0;
}
sleep $opt{interval};
}
exit 1;