1#!/usr/bin/env perl
2
3BEGIN {
4   die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
5      unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
6   unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
7};
8
9use strict;
10use warnings FATAL => 'all';
11use English qw(-no_match_vars);
12use Test::More;
13
14use PerconaTest;
15use Sandbox;
16use SqlModes;
17use threads;
18use Time::HiRes qw( usleep );
19use constant PTDEBUG => $ENV{PTDEBUG} || 0;
20
21require "$trunk/bin/pt-table-checksum";
22
23my $dp  = new DSNParser(opts=>$dsn_opts);
24my $sb  = new Sandbox(basedir => '/tmp', DSNParser => $dp);
25my $dbh = $sb->get_dbh_for('master');
26
27if ( !$dbh ) {
28   plan skip_all => 'Cannot connect to sandbox master';
29}
30else {
31   plan tests => 2;
32}
33
34my $num_rows = 1_000_000;
35my $table = 't1';
36
37$dbh->do("DROP DATABASE IF EXISTS test");
38$dbh->do("CREATE DATABASE IF NOT EXISTS test");
39$dbh->do("CREATE TABLE `test`.`$table` (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(5)) Engine=InnoDB");
40
41diag(`util/mysql_random_data_load --host=127.0.0.1 --port=12345 --user=msandbox --password=msandbox test $table $num_rows`);
42
43sub start_thread {
44   my ($dsn_opts, $initial_sleep_time) = @_;
45   my $dp = new DSNParser(opts=>$dsn_opts);
46   my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
47   my $master_dbh = $sb->get_dbh_for('master');
48   my $slave_dbh = $sb->get_dbh_for('slave1');
49   diag("Sleeping");
50   sleep($initial_sleep_time);
51   diag("Woke up");
52   $slave_dbh->do("STOP SLAVE IO_THREAD FOR CHANNEL ''");
53   $slave_dbh->do("STOP SLAVE");
54   $master_dbh->do("TRUNCATE TABLE test.$table");
55   # PTDEBUG && diag("Exit thread")
56   sleep(2);
57   $slave_dbh->do("START SLAVE");
58   diag("Exit thread")
59}
60# This is not a realiable sleep value. It works for a i7, hybrid HDD
61my $initial_sleep_time = 17;
62my $thr = threads->create('start_thread', $dsn_opts, $initial_sleep_time);
63threads->yield();
64
65diag("Starting checksum");
66# The sandbox servers run with lock_wait_timeout=3 and it's not dynamic
67# so we need to specify --set-vars innodb_lock_wait_timeout=3 else the tool will die.
68# And --max-load "" prevents waiting for status variables.
69my $master_dsn = 'h=127.1,P=12345,u=msandbox,p=msandbox';
70my @args       = ($master_dsn, qw(--no-check-binlog-format --chunk-size 10));
71my $output;
72
73$output = output(
74   sub { pt_table_checksum::main(@args) },
75   stderr => 1,
76);
77
78diag($output);
79unlike(
80   $output,
81   qr/Can't use an undefined value as an ARRAY/,
82   "Truncating tables while checksum is running"
83);
84
85$thr->join();
86
87# #############################################################################
88# Done.
89# #############################################################################
90$sb->wipe_clean($dbh);
91ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
92exit;
93