Merge pt-fke-logger-2.2.

This commit is contained in:
Daniel Nichter
2013-02-27 16:41:28 -07:00
36 changed files with 1856 additions and 823 deletions

View File

@@ -9,7 +9,7 @@ BEGIN {
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 19;
use Test::More;
use Sandbox;
use OptionParser;
@@ -251,9 +251,65 @@ is_deeply(
@ARGV = ();
$o->get_opts();
# #############################################################################
# The parent of a forked Cxn should not disconnect the dbh in DESTORY
# because the child still has access to it.
# #############################################################################
my $sync_file = "/tmp/pt-cxn-sync.$PID";
my $outfile = "/tmp/pt-cxn-outfile.$PID";
my $pid;
{
my $parent_cxn = make_cxn(
dsn_string => 'h=127.1,P=12345,u=msandbox,p=msandbox',
parent => 1,
);
$parent_cxn->connect();
$pid = fork();
if ( defined($pid) && $pid == 0 ) {
# I am the child.
# Wait for the parent to leave this code block which will cause
# the $parent_cxn to be destroyed.
PerconaTest::wait_for_files($sync_file);
$parent_cxn->{parent} = 0;
eval {
$parent_cxn->dbh->do("SELECT 123 INTO OUTFILE '$outfile'");
$parent_cxn->dbh->disconnect();
};
warn $EVAL_ERROR if $EVAL_ERROR;
exit;
}
}
# Let the child know that we (the parent) have left that ^ code block,
# so our copy of $parent_cxn has been destroyed, but hopefully the child's
# copy is still alive, i.e. has an active/not-disconnected dbh.
diag(`touch $sync_file`);
# Wait for the child.
waitpid($pid, 0);
ok(
-f $outfile,
"Child created outfile"
);
my $output = `cat $outfile 2>/dev/null`;
is(
$output,
"123\n",
"Child executed query"
);
unlink $sync_file if -f $sync_file;
unlink $outfile if -f $outfile;
# #############################################################################
# Done.
# #############################################################################
$master_dbh->disconnect() if $master_dbh;
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
exit;
done_testing;

View File

@@ -11,6 +11,8 @@ use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More;
$ENV{PERCONA_TOOLKIT_TEST_USE_DSN_NAMES} = 1;
use PerconaTest;
use Sandbox;
require "$trunk/bin/pt-deadlock-logger";
@@ -25,8 +27,8 @@ if ( !$dbh1 || !$dbh2 ) {
}
my $output;
my $cnf = "/tmp/12345/my.sandbox.cnf";
my $cmd = "$trunk/bin/pt-deadlock-logger -F $cnf h=127.1";
my $dsn = $sb->dsn_for('master');
my @args = ($dsn, qw(--iterations 1));
$dbh1->commit;
$dbh2->commit;
@@ -90,20 +92,29 @@ make_deadlock();
$output = $dbh1->selectrow_hashref('show /*!40101 engine*/ innodb status')->{status};
like($output, qr/WE ROLL BACK/, 'There was a deadlock');
$output = `$cmd --print`;
$output = output(
sub {
pt_deadlock_logger::main(@args);
}
);
like(
$output,
qr/127\.1.+msandbox.+GEN_CLUST_INDEX/,
'Deadlock logger prints the output'
);
$output = `$cmd`;
like(
$output,
qr/127\.1.+msandbox.+GEN_CLUST_INDEX/,
'--print is implicit'
$output = output(
sub {
pt_deadlock_logger::main(@args, qw(--quiet));
}
);
is(
$output,
"",
"No output with --quiet"
);
# #############################################################################
# Issue 943: mk-deadlock-logger reports the same deadlock with --interval
@@ -112,55 +123,59 @@ like(
# The deadlock from above won't be re-printed so even after running for
# 3 seconds and checking multiple times only the single, 3 line deadlock
# should be reported.
chomp($output = `$cmd --run-time 3 | wc -l`);
$output = output(
sub {
pt_deadlock_logger::main(@args, qw(--run-time 3));
}
);
$output =~ s/^\s+//;
my @lines = split("\n", $output);
is(
$output,
scalar @lines,
3,
"Doesn't re-print same deadlock (issue 943)"
);
) or diag($output);
# #############################################################################
# Check that deadlocks from previous test were stored in table.
# #############################################################################
`$cmd --dest D=test,t=deadlocks --create-dest-table`;
$output = output(
sub {
pt_deadlock_logger::main(@args, '--dest', 'D=test,t=deadlocks',
qw(--create-dest-table))
}
);
my $res = $dbh1->selectall_arrayref('SELECT * FROM test.deadlocks');
ok(
scalar @$res,
'Deadlocks recorded in --dest table'
);
'Deadlock saved in --dest table'
) or diag($output);
# #############################################################################
# Check that --dest suppress --print output unless --print is explicit.
# In 2.1, --dest suppressed output (--print). In 2.2, output is only
# suppressed by --quiet.
# #############################################################################
$output = 'foo';
$dbh1->do('TRUNCATE TABLE test.deadlocks');
$output = `$cmd --dest D=test,t=deadlocks`;
is(
$output,
'',
'No output with --dest'
);
$res = $dbh1->selectall_arrayref('SELECT * FROM test.deadlocks');
ok(
scalar @$res,
'Deadlocks still recorded in table'
);
$output = '';
$dbh1->do('TRUNCATE TABLE test.deadlocks');
$output = `$trunk/bin/pt-deadlock-logger --print --dest D=test,t=deadlocks --host 127.1 --port 12345 --user msandbox --password msandbox`;
like(
$output = output(
sub {
pt_deadlock_logger::main(@args, '--dest', 'D=test,t=deadlocks',
qw(--quiet))
}
);
is(
$output,
qr/127\.1.+msandbox.+GEN_CLUST_INDEX/,
'Prints output with --dest and explicit --print'
"",
"No output with --dest and --quiet"
);
$res = $dbh1->selectall_arrayref('SELECT * FROM test.deadlocks');
ok(
scalar @$res,
'Deadlocks recorded in table again'
"... deadlock still saved in the table"
);
# #############################################################################
@@ -180,9 +195,7 @@ SKIP: {
make_deadlock();
$output = output(
sub { pt_deadlock_logger::main("F=/tmp/12345/my.sandbox.cnf",
qw(--print) );
}
sub { pt_deadlock_logger::main(@args) }
);
like(
@@ -200,4 +213,3 @@ $dbh2->commit;
$sb->wipe_clean($dbh1);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
done_testing;
exit;

View File

@@ -22,9 +22,6 @@ my $dbh1 = $sb->get_dbh_for('master');
if ( !$dbh1 ) {
plan skip_all => 'Cannot connect to sandbox master';
}
else {
plan tests => 4;
}
my $output;
my $cnf = "/tmp/12345/my.sandbox.cnf";
@@ -39,7 +36,7 @@ $sb->create_dbs($dbh1, ['test']);
# The clear-deadlocks table comes and goes quickly so we can really
# only search the debug output for evidence that it was created.
$output = `PTDEBUG=1 $trunk/bin/pt-deadlock-logger F=$cnf,D=test --clear-deadlocks test.make_deadlock 2>&1`;
$output = `PTDEBUG=1 $trunk/bin/pt-deadlock-logger F=$cnf,D=test --clear-deadlocks test.make_deadlock --iterations 1 2>&1`;
like(
$output,
qr/INSERT INTO test.make_deadlock/,
@@ -67,4 +64,4 @@ like(
# #############################################################################
$sb->wipe_clean($dbh1);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
exit;
done_testing;

View File

@@ -22,15 +22,10 @@ my $dbh1 = $sb->get_dbh_for('master');
if ( !$dbh1 ) {
plan skip_all => 'Cannot connect to sandbox master';
}
else {
plan tests => 3;
}
my $output;
my $cnf = "/tmp/12345/my.sandbox.cnf";
my $cmd = "$trunk/bin/pt-deadlock-logger -F $cnf h=127.1";
my $dsn = $sb->dsn_for('master');
$sb->wipe_clean($dbh1);
$sb->create_dbs($dbh1, ['test']);
# #############################################################################
@@ -42,17 +37,25 @@ is_deeply(
'Deadlocks table does not exit (issue 386)'
);
`$cmd --dest D=test,t=issue_386 --run-time 1s --interval 1s --create-dest-table`;
$output = output(
sub {
pt_deadlock_logger::main($dsn,
'--dest', 'D=test,t=issue_386',
qw(--iterations 1 --create-dest-table)
)
},
stderr => 1,
);
is_deeply(
$dbh1->selectall_arrayref(q{show tables from `test` like 'issue_386'}),
[['issue_386']],
'Deadlocks table created with --create-dest-table (issue 386)'
);
) or diag($output);
# #############################################################################
# Done.
# #############################################################################
$sb->wipe_clean($dbh1);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
exit;
done_testing;

View File

@@ -21,7 +21,7 @@ my $output;
$output = `$trunk/bin/pt-deadlock-logger --dest D=test,t=deadlocks 2>&1`;
like(
$output,
qr/Missing or invalid source host/,
qr/No DSN was specified/,
'Requires source host'
);

View File

@@ -17,69 +17,96 @@ require "$trunk/bin/pt-deadlock-logger";
my $dp = new DSNParser(opts=>$dsn_opts);
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $dbh1 = $sb->get_dbh_for('master');
my $dbh = $sb->get_dbh_for('master');
if ( !$dbh1 ) {
if ( !$dbh ) {
plan skip_all => 'Cannot connect to sandbox master';
}
else {
plan tests => 10;
}
my $output;
my $cnf = "/tmp/12345/my.sandbox.cnf";
my $cmd = "$trunk/bin/pt-deadlock-logger -F $cnf h=127.1";
my $dsn = $sb->dsn_for('master');
my @args = ($dsn, qw(--iterations 1));
$sb->wipe_clean($dbh1);
$sb->create_dbs($dbh1, ['test']);
$sb->wipe_clean($dbh);
$sb->create_dbs($dbh, ['test']);
# #############################################################################
# Issue 248: Add --user, --pass, --host, etc to all tools
# #############################################################################
# Test that source DSN inherits from --user, etc.
$output = `$trunk/bin/pt-deadlock-logger h=127.1,D=test,u=msandbox,p=msandbox --clear-deadlocks test.make_deadlock --port 12345 2>&1`;
$output = output(
sub {
pt_deadlock_logger::main(
"h=127.1,D=test,u=msandbox,p=msandbox",
qw(--clear-deadlocks test.make_deadlock --port 12345),
qw(--iterations 1)
)
}
);
unlike(
$output,
qr/failed/,
'Source DSN inherits from standard connection options (issue 248)'
);
# #########################################################################
# #############################################################################
# Issue 391: Add --pid option to all scripts
# #########################################################################
`touch /tmp/mk-script.pid`;
$output = `$cmd --clear-deadlocks test.make_deadlock --port 12345 --pid /tmp/mk-script.pid 2>&1`;
# #############################################################################
my $pid_file = "/tmp/pt-deadlock-logger-test.pid.$PID";
diag(`touch $pid_file`);
$output = output(
sub {
pt_deadlock_logger::main(@args, '--pid', $pid_file)
},
stderr => 1,
);
like(
$output,
qr{PID file /tmp/mk-script.pid already exists},
qr{PID file $pid_file already exists},
'Dies if PID file already exists (--pid without --daemonize) (issue 391)'
);
`rm -rf /tmp/mk-script.pid`;
unlink $pid_file if -f $pid_file;
# #############################################################################
# Check daemonization
# #############################################################################
my $deadlocks_tbl = load_file('t/pt-deadlock-logger/deadlocks_tbl.sql');
$dbh1->do('USE test');
$dbh1->do('DROP TABLE IF EXISTS deadlocks');
$dbh1->do("$deadlocks_tbl");
$dbh->do('USE test');
$dbh->do('DROP TABLE IF EXISTS deadlocks');
$sb->load_file('master', 't/pt-deadlock-logger/samples/deadlocks_tbl.sql', 'test');
my $pid_file = '/tmp/mk-deadlock-logger.pid';
unlink $pid_file
and diag("Unlinked existing $pid_file");
`$cmd --dest D=test,t=deadlocks --daemonize --run-time 6s --interval 1s --pid $pid_file 1>/dev/null 2>/dev/null`;
$output = `ps -eaf | grep '$cmd \-\-dest '`;
like($output, qr/\Q$cmd/, 'It lives daemonized');
$output = `$trunk/bin/pt-deadlock-logger $dsn --dest D=test,t=deadlocks --daemonize --run-time 10 --interval 1 --pid $pid_file 1>/dev/null 2>/dev/null`;
PerconaTest::wait_for_files($pid_file);
ok(-f $pid_file, 'PID file created');
my ($pid) = $output =~ /\s+(\d+)\s+/;
$output = `ps x | grep 'pt-deadlock-logger $dsn' | grep -v grep`;
like(
$output,
qr/\Qpt-deadlock-logger $dsn/,
'It lives daemonized'
) or diag($output);
my ($pid) = $output =~ /(\d+)/;
ok(
-f $pid_file,
'PID file created'
) or diag($output);
chomp($output = slurp_file($pid_file));
is($output, $pid, 'PID file has correct PID');
is(
$output,
$pid,
'PID file has correct PID'
);
# Kill it
kill 2, $pid;
PerconaTest::wait_until(sub { !kill 0, $pid });
ok(! -f $pid_file, 'PID file removed');
@@ -90,17 +117,25 @@ ok(
'PID file already exists'
);
$output = `$cmd --dest D=test,t=deadlocks --daemonize --run-time 1s --interval 1s --pid $pid_file 2>&1`;
$output = output(
sub {
pt_deadlock_logger::main(@args, '--pid', $pid_file,
qw(--daemonize))
},
stderr => 1,
);
like(
$output,
qr/PID file .+ already exists/,
qr/PID file $pid_file already exists/,
'Does not run if PID file already exists'
);
$output = `ps -eaf | grep 'pt-deadlock-logger \-\-dest '`;
unlike(
$output = `ps x | grep 'pt-deadlock-logger $dsn' | grep -v grep`;
is(
$output,
qr/$cmd/,
"",
'It does not lived daemonized'
);
@@ -109,6 +144,6 @@ unlink $pid_file;
# #############################################################################
# Done.
# #############################################################################
$sb->wipe_clean($dbh1);
$sb->wipe_clean($dbh);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
exit;
done_testing;

View File

@@ -27,8 +27,9 @@ if ( !$dbh ) {
$sb->create_dbs($dbh, [qw(test)]);
my $output;
my $cnf = '/tmp/12345/my.sandbox.cnf';
my $cmd = "$trunk/bin/pt-fk-error-logger -F $cnf ";
my $cnf = '/tmp/12345/my.sandbox.cnf';
my $cmd = "$trunk/bin/pt-fk-error-logger -F $cnf ";
my @args = qw(--iterations 1);
$sb->load_file('master', 't/pt-fk-error-logger/samples/fke_tbl.sql', 'test');
@@ -39,8 +40,45 @@ $sb->load_file('master', 't/pt-fk-error-logger/samples/fke_tbl.sql', 'test');
# First, create a foreign key error.
`/tmp/12345/use -D test < $trunk/t/pt-fk-error-logger/samples/fke.sql 1>/dev/null 2>/dev/null`;
# Then get and save that fke.
output(sub { pt_fk_error_logger::main('h=127.1,P=12345,u=msandbox,p=msandbox', '--dest', 'h=127.1,P=12345,D=test,t=foreign_key_errors'); } );
$output = output(
sub {
pt_fk_error_logger::main(@args, 'h=127.1,P=12345,u=msandbox,p=msandbox'),
}
);
like(
$output,
qr/Foreign key constraint fails/,
"Prints fk error by default"
);
$output = output(
sub {
pt_fk_error_logger::main(@args, 'h=127.1,P=12345,u=msandbox,p=msandbox',
qw(--quiet))
}
);
is(
$output,
"",
"No output with --quiet"
);
# #############################################################################
# --dest
# #############################################################################
$output = output(
sub {
pt_fk_error_logger::main(@args,
'h=127.1,P=12345,u=msandbox,p=msandbox',
'--dest', 'h=127.1,P=12345,D=test,t=foreign_key_errors',
)
}
);
sleep 0.1;
# And then test that it was actually saved.
@@ -61,7 +99,7 @@ like(
# Check again to make sure that the same fke isn't saved twice.
my $first_ts = $fke->[0]->[0];
output(sub { pt_fk_error_logger::main('h=127.1,P=12345,u=msandbox,p=msandbox', '--dest', 'h=127.1,P=12345,D=test,t=foreign_key_errors'); } );
output(sub { pt_fk_error_logger::main(@args, 'h=127.1,P=12345,u=msandbox,p=msandbox', '--dest', 'h=127.1,P=12345,D=test,t=foreign_key_errors'); } );
sleep 0.1;
$fke = $dbh->selectall_arrayref('SELECT * FROM test.foreign_key_errors');
is(
@@ -82,7 +120,7 @@ $dbh->do('INSERT INTO child VALUES (1, 2)');
eval {
$dbh->do('DELETE FROM parent WHERE id = 2'); # Causes foreign key error.
};
output( sub { pt_fk_error_logger::main('h=127.1,P=12345,u=msandbox,p=msandbox', '--dest', 'h=127.1,P=12345,D=test,t=foreign_key_errors'); } );
output( sub { pt_fk_error_logger::main(@args, 'h=127.1,P=12345,u=msandbox,p=msandbox', '--dest', 'h=127.1,P=12345,D=test,t=foreign_key_errors'); } );
sleep 0.1;
$fke = $dbh->selectall_arrayref('SELECT * FROM test.foreign_key_errors');
like(
@@ -99,11 +137,14 @@ is(
# ##########################################################################
# Test printing the errors.
# ##########################################################################
$dbh->do('USE test');
eval {
$dbh->do('DELETE FROM parent WHERE id = 2'); # Causes foreign key error.
};
$output = output(sub { pt_fk_error_logger::main('h=127.1,P=12345,u=msandbox,p=msandbox'); });
$output = output(sub { pt_fk_error_logger::main(@args, 'h=127.1,P=12345,u=msandbox,p=msandbox'); });
like(
$output,
qr/DELETE FROM parent WHERE id = 2/,
@@ -127,7 +168,7 @@ $sb->load_file('master1', 't/pt-fk-error-logger/samples/fke_tbl.sql', 'test');
$output = output(
sub {
pt_fk_error_logger::main('h=127.1,P=12348,u=msandbox,p=msandbox',
pt_fk_error_logger::main(@args, 'h=127.1,P=12348,u=msandbox,p=msandbox',
'--dest', 'h=127.1,P=12348,D=test,t=foreign_key_errors')
},
stderr => 1,
@@ -141,6 +182,23 @@ is(
diag(`$trunk/sandbox/stop-sandbox 12348 >/dev/null`);
# #############################################################################
# Test --pid
# #############################################################################
my $pid_file = "/tmp/pt-fk-error-log-test-$PID.pid";
diag(`touch $pid_file`);
$output = `$trunk/bin/pt-fk-error-logger h=127.1,P=12345,u=msandbox,p=msandbox --pid $pid_file --iterations 1 2>&1`;
like(
$output,
qr{PID file $pid_file already exists},
'Dies if PID file already exists (--pid without --daemonize) (issue 391)'
);
unlink $pid_file;
# #############################################################################
# Done.
# #############################################################################

View File

@@ -9,7 +9,7 @@ BEGIN {
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 10;
use Test::More;
use PerconaTest;
require "$trunk/bin/pt-fk-error-logger";
@@ -70,4 +70,4 @@ test_get_fk_error(
# #############################################################################
# Done.
# #############################################################################
exit;
done_testing;

View File

@@ -1,32 +0,0 @@
#!/usr/bin/env perl
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";
};
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 1;
use PerconaTest;
require "$trunk/bin/pt-fk-error-logger";
# #########################################################################
# Issue 391: Add --pid option to all scripts
# #########################################################################
`touch /tmp/mk-script.pid`;
my $output = `$trunk/bin/pt-fk-error-logger h=127.1,P=12345,u=msandbox,p=msandbox --print --pid /tmp/mk-script.pid 2>&1`;
like(
$output,
qr{PID file /tmp/mk-script.pid already exists},
'Dies if PID file already exists (--pid without --daemonize) (issue 391)'
);
`rm -rf /tmp/mk-script.pid`;
# #############################################################################
# Done.
# #############################################################################
exit;