Add forked Maatkit tools in bin/ and their tests in t/.

This commit is contained in:
Daniel Nichter
2011-06-24 16:02:05 -06:00
parent 6c501128e6
commit b4b6376be1
679 changed files with 177876 additions and 3 deletions

View File

@@ -0,0 +1,164 @@
#!/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;
use MaatkitTest;
use Sandbox;
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', { PrintError => 0, RaiseError => 1, AutoCommit => 0 });
my $dbh2 = $sb->get_dbh_for('master', { PrintError => 0, RaiseError => 1, AutoCommit => 0 });
if ( !$dbh1 || !$dbh2 ) {
plan skip_all => 'Cannot connect to sandbox master';
}
else {
plan tests => 9;
}
my $output;
my $cnf = "/tmp/12345/my.sandbox.cnf";
my $cmd = "$trunk/bin/pt-deadlock-logger -F $cnf h=127.1";
$sb->wipe_clean($dbh1);
$sb->create_dbs($dbh1, ['test']);
# Set up the table for creating a deadlock.
$dbh1->do("create table test.dl(a int) engine=innodb");
$dbh1->do("insert into test.dl(a) values(0), (1)");
$dbh1->commit;
$dbh1->{InactiveDestroy} = 1;
$dbh2->{InactiveDestroy} = 1;
# Fork off two children to deadlock against each other.
my %children;
foreach my $child ( 0..1 ) {
my $pid = fork();
if ( defined($pid) && $pid == 0 ) { # I am a child
eval {
my $dbh = ($dbh1, $dbh2)[$child];
my @stmts = (
"set transaction isolation level serializable",
"begin",
"select * from test.dl where a = $child",
"update test.dl set a = $child where a <> $child",
);
foreach my $stmt (@stmts[0..2]) {
$dbh->do($stmt);
}
sleep(1 + $child);
$dbh->do($stmts[-1]);
};
if ( $EVAL_ERROR ) {
if ( $EVAL_ERROR !~ m/Deadlock found/ ) {
die $EVAL_ERROR;
}
}
exit(0);
}
elsif ( !defined($pid) ) {
die("Unable to fork for clearing deadlocks!\n");
}
# I already exited if I'm a child, so I'm the parent.
$children{$child} = $pid;
}
# Wait for the children to exit.
foreach my $child ( keys %children ) {
my $pid = waitpid($children{$child}, 0);
}
# Test that there is a deadlock
$output = $dbh1->selectrow_hashref('show innodb status')->{status};
like($output, qr/WE ROLL BACK/, 'There was a deadlock');
$output = `$cmd --print`;
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'
);
# #############################################################################
# Issue 943: mk-deadlock-logger reports the same deadlock with --interval
# #############################################################################
# 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 =~ s/^\s+//;
is(
$output,
3,
"Doesn't re-print same deadlock (issue 943)"
);
# #############################################################################
# Check that deadlocks from previous test were stored in table.
# #############################################################################
`$cmd --dest D=test,t=deadlocks --create-dest-table`;
my $res = $dbh1->selectall_arrayref('SELECT * FROM test.deadlocks');
ok(
scalar @$res,
'Deadlocks recorded in --dest table'
);
# #############################################################################
# Check that --dest suppress --print output unless --print is explicit.
# #############################################################################
$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,
qr/127\.1.+msandbox.+GEN_CLUST_INDEX/,
'Prints output with --dest and explicit --print'
);
$res = $dbh1->selectall_arrayref('SELECT * FROM test.deadlocks');
ok(
scalar @$res,
'Deadlocks recorded in table again'
);
# #############################################################################
# Done.
# #############################################################################
$sb->wipe_clean($dbh1);
exit;

View File

@@ -0,0 +1,69 @@
#!/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;
use MaatkitTest;
use Sandbox;
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');
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";
$sb->wipe_clean($dbh1);
$sb->create_dbs($dbh1, ['test']);
# #############################################################################
# Test --clear-deadlocks
# #############################################################################
# The clear-deadlocks table comes and goes quickly so we can really
# only search the debug output for evidence that it was created.
$output = `MKDEBUG=1 $trunk/bin/pt-deadlock-logger F=$cnf,D=test --clear-deadlocks test.make_deadlock 2>&1`;
like(
$output,
qr/INSERT INTO test.make_deadlock/,
'Create --clear-deadlocks table (output)'
);
like(
$output,
qr/CREATE TABLE test.make_deadlock/,
'Create --clear-deadlocks table (debug)'
);
# #############################################################################
# Issue 942: mk-deadlock-logger --clear-deadlocks doesn't work with --interval
# #############################################################################
$output = `MKDEBUG=1 $trunk/bin/pt-deadlock-logger F=$cnf,D=test --clear-deadlocks test.make_deadlock2 --interval 1 --run-time 1 2>&1`;
like(
$output,
qr/CREATE TABLE test.make_deadlock2/,
'--clear-deadlocks with --interval (isue 942)'
);
# #############################################################################
# Done.
# #############################################################################
$sb->wipe_clean($dbh1);
exit;

View File

@@ -0,0 +1,57 @@
#!/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;
use MaatkitTest;
use Sandbox;
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');
if ( !$dbh1 ) {
plan skip_all => 'Cannot connect to sandbox master';
}
else {
plan tests => 2;
}
my $output;
my $cnf = "/tmp/12345/my.sandbox.cnf";
my $cmd = "$trunk/bin/pt-deadlock-logger -F $cnf h=127.1";
$sb->wipe_clean($dbh1);
$sb->create_dbs($dbh1, ['test']);
# #############################################################################
# Issue 386: Make mk-deadlock-logger auto-create the --dest table
# #############################################################################
is_deeply(
$dbh1->selectall_arrayref('show tables from `test` like "issue_386"'),
[],
'Deadlocks table does not exit (issue 386)'
);
`$cmd --dest D=test,t=issue_386 --run-time 1s --interval 1s --create-dest-table`;
is_deeply(
$dbh1->selectall_arrayref('show tables from `test` like "issue_386"'),
[['issue_386']],
'Deadlocks table created with --create-dest-table (issue 386)'
);
# #############################################################################
# Done.
# #############################################################################
$sb->wipe_clean($dbh1);
exit;

View File

@@ -0,0 +1,19 @@
CREATE TABLE deadlocks (
server char(20) NOT NULL,
ts datetime NOT NULL,
thread int unsigned NOT NULL,
txn_id bigint unsigned NOT NULL,
txn_time smallint unsigned NOT NULL,
user char(16) NOT NULL,
hostname char(20) NOT NULL,
ip char(15) NOT NULL, -- alternatively, ip int unsigned NOT NULL
db char(64) NOT NULL,
tbl char(64) NOT NULL,
idx char(64) NOT NULL,
lock_type char(16) NOT NULL,
lock_mode char(1) NOT NULL,
wait_hold char(1) NOT NULL,
victim tinyint unsigned NOT NULL,
query text NOT NULL,
PRIMARY KEY (server,ts,thread)
) ENGINE=InnoDB;

View File

@@ -0,0 +1,45 @@
#!/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 => 3;
use MaatkitTest;
my $output;
# #############################################################################
# Sanity tests.
# #############################################################################
$output = `$trunk/bin/pt-deadlock-logger --dest D=test,t=deadlocks 2>&1`;
like(
$output,
qr/Missing or invalid source host/,
'Requires source host'
);
$output = `$trunk/bin/pt-deadlock-logger h=127.1 --dest t=deadlocks 2>&1`;
like(
$output,
qr/requires a 'D'/,
'Dest DSN requires D',
);
$output = `$trunk/bin/pt-deadlock-logger --dest D=test 2>&1`;
like(
$output,
qr/requires a 't'/,
'Dest DSN requires t'
);
# #############################################################################
# Done.
# #############################################################################
exit;

View File

@@ -0,0 +1,108 @@
#!/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;
use MaatkitTest;
use Sandbox;
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');
if ( !$dbh1 ) {
plan skip_all => 'Cannot connect to sandbox master';
}
else {
plan tests => 9;
}
my $output;
my $cnf = "/tmp/12345/my.sandbox.cnf";
my $cmd = "$trunk/bin/pt-deadlock-logger -F $cnf h=127.1";
$sb->wipe_clean($dbh1);
$sb->create_dbs($dbh1, ['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`;
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`;
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`;
# #############################################################################
# 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");
`$cmd --dest D=test,t=deadlocks --daemonize --run-time 1s --interval 1s --pid /tmp/mk-deadlock-logger.pid 1>/dev/null 2>/dev/null`;
$output = `ps -eaf | grep '$cmd \-\-dest '`;
like($output, qr/$cmd/, 'It lives daemonized');
ok(-f '/tmp/mk-deadlock-logger.pid', 'PID file created');
my ($pid) = $output =~ /\s+(\d+)\s+/;
$output = `cat /tmp/mk-deadlock-logger.pid`;
is($output, $pid, 'PID file has correct PID');
# Kill it
sleep 2;
ok(! -f '/tmp/mk-deadlock-logger.pid', 'PID file removed');
# Check that it won't run if the PID file already exists (issue 383).
diag(`touch /tmp/mk-deadlock-logger.pid`);
ok(
-f '/tmp/mk-deadlock-logger.pid',
'PID file already exists'
);
$output = `$cmd --dest D=test,t=deadlocks --daemonize --run-time 1s --interval 1s --pid /tmp/mk-deadlock-logger.pid 2>&1`;
like(
$output,
qr/PID file .+ already exists/,
'Does not run if PID file already exists'
);
$output = `ps -eaf | grep 'mk-deadlock-logger \-\-dest '`;
unlike(
$output,
qr/$cmd/,
'It does not lived daemonized'
);
diag(`rm -rf /tmp/mk-deadlock-logger.pid`);
# #############################################################################
# Done.
# #############################################################################
$sb->wipe_clean($dbh1);
exit;