diff --git a/bin/pt-table-sync b/bin/pt-table-sync index 982d3257..61c44432 100755 --- a/bin/pt-table-sync +++ b/bin/pt-table-sync @@ -8035,6 +8035,16 @@ sub sync_via_replication { lock_server(src => $src, dst => $dst, %args); foreach my $diff ( @$diffs ) { + # Clear the tbl_struct if this is a new table. The tbl_struct + # is fetched and parsed in ok_to_sync() if not set. We only + # need to set it once per table to avoid doing this for every + # diff in the same table. + # https://bugs.launchpad.net/percona-toolkit/+bug/1003014 + if ( ($src->{db} || '') ne $diff->{db} + || ($src->{tbl} || '') ne $diff->{tbl} ) { + PTDEBUG && _d('New table:', $diff->{db}, $diff->{tbl}); + $src->{tbl_struct} = undef; + } $src->{db} = $dst->{db} = $diff->{db}; $src->{tbl} = $dst->{tbl} = $diff->{tbl}; @@ -8103,6 +8113,14 @@ sub sync_via_replication { lock_server(src => $src, dst => $dst, %args); foreach my $diff ( @$diffs ) { + # Clear the tbl_struct if this is a new table. + # See the same code block above. + if ( ($src->{db} || '') ne $diff->{db} + || ($src->{tbl} || '') ne $diff->{tbl} ) { + PTDEBUG && _d('New table:', + $diff->{db}, $diff->{tbl}); + $src->{tbl_struct} = undef; + } $src->{db} = $dst->{db} = $diff->{db}; $src->{tbl} = $dst->{tbl} = $diff->{tbl}; diff --git a/t/pt-table-sync/bugs.t b/t/pt-table-sync/bugs.t new file mode 100644 index 00000000..2c0dceff --- /dev/null +++ b/t/pt-table-sync/bugs.t @@ -0,0 +1,156 @@ +#!/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 Data::Dumper; + +use PerconaTest; +use Sandbox; +require "$trunk/bin/pt-table-sync"; + +my $output; +my $dp = new DSNParser(opts=>$dsn_opts); +my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp); +my $master_dbh = $sb->get_dbh_for('master'); +my $slave_dbh = $sb->get_dbh_for('slave1'); + +if ( !$master_dbh ) { + plan skip_all => 'Cannot connect to sandbox master'; +} +elsif ( !$slave_dbh ) { + plan skip_all => 'Cannot connect to sandbox slave'; +} +else { + plan tests => 8; +} + +my $sample = "t/pt-table-sync/samples"; +my $master_dsn = "h=127.1,P=12345,u=msandbox,p=msandbox"; +my $slave_dsn = "h=127.1,P=12346,u=msandbox,p=msandbox"; + +# ############################################################################# +# +# ############################################################################# + +$sb->load_file('master', "$sample/wrong-tbl-struct-bug-1003014.sql"); +PerconaTest::wait_for_table($slave_dbh, "test.zzz", "id=111"); + +# Make a diff in each table. +$slave_dbh->do("DELETE FROM test.aaa WHERE STOP_ARCHIVE IN (5,6,7)"); +$slave_dbh->do("UPDATE test.zzz SET c='x' WHERE id IN (44,45,46)"); + +$output = `$trunk/bin/pt-table-checksum $master_dsn --lock-wait-timeout 3 --max-load '' -d test --chunk-size 10 2>&1`; + +is( + PerconaTest::count_checksum_results($output, 'diffs'), + 2, + "Bug 1003014 (wrong tbl_struct): 2 diffs" +) or print STDERR $output; + +my $checksums = [ + [qw( test aaa 1 )], + [qw( test zzz 1 )], + [qw( test zzz 2 )], + [qw( test zzz 3 )], + [qw( test zzz 4 )], + [qw( test zzz 5 )], + [qw( test zzz 6 )], + [qw( test zzz 7 )], + [qw( test zzz 8 )], + [qw( test zzz 9 )], + [qw( test zzz 10 )], + [qw( test zzz 11 )], + [qw( test zzz 12 )], + [qw( test zzz 13 )], + [qw( test zzz 14 )], +]; + +my $rows = $master_dbh->selectall_arrayref("SELECT db, tbl, chunk FROM percona.checksums ORDER BY db, tbl, chunk"); +is_deeply( + $rows, + $checksums, + "Bug 1003014 (wrong tbl_struct): checksums" +); + +my $exit_status; +$output = output( + sub { $exit_status = pt_table_sync::main($slave_dsn, + qw(--replicate percona.checksums --sync-to-master --print --execute), + "--tables", "test.aaa,test.zzz") }, + stderr => 1, +); + +is( + $exit_status, + 2, # rows synced OK; 3=error (1) & rows synced (2) + "Bug 1003014 (wrong tbl_struct): 0 exit" +) or diag($output); + +$rows = $slave_dbh->selectall_arrayref("SELECT c FROM test.zzz WHERE id IN (44,45,46)"); +is_deeply( + $rows, + [ ['a'], ['a'], ['a'] ], + "Bug 1003014 (wrong tbl_struct): synced rows" +); + +# ######################################################################### +# Repeat the whole process without --sync-to-master so the second code path +# in sync_via_replication() is tested. +# ######################################################################### + +$sb->wipe_clean($master_dbh); + +$sb->load_file('master', "$sample/wrong-tbl-struct-bug-1003014.sql"); +PerconaTest::wait_for_table($slave_dbh, "test.zzz", "id=111"); + +$slave_dbh->do("DELETE FROM test.aaa WHERE STOP_ARCHIVE IN (5,6,7)"); +$slave_dbh->do("UPDATE test.zzz SET c='x' WHERE id IN (44,45,46)"); + +$output = `$trunk/bin/pt-table-checksum $master_dsn --lock-wait-timeout 3 --max-load '' -d test --chunk-size 10 2>&1`; + +is( + PerconaTest::count_checksum_results($output, 'diffs'), + 2, + "Bug 1003014 (wrong tbl_struct): 2 diffs (just replicate)" +) or print STDERR $output; + +$rows = $master_dbh->selectall_arrayref("SELECT db, tbl, chunk FROM percona.checksums ORDER BY db, tbl, chunk"); +is_deeply( + $rows, + $checksums, + "Bug 1003014 (wrong tbl_struct): checksums (just replicate)" +); + +$output = output( + sub { $exit_status = pt_table_sync::main($master_dsn, + qw(--replicate percona.checksums --print --execute), + "--tables", "test.aaa,test.zzz") }, + stderr => 1, +); + +is( + $exit_status, + 2, # rows synced OK; 3=error (1) & rows synced (2) + "Bug 1003014 (wrong tbl_struct): 0 exit (just replicate)" +) or diag($output); + +$rows = $slave_dbh->selectall_arrayref("SELECT c FROM test.zzz WHERE id IN (44,45,46)"); +is_deeply( + $rows, + [ ['a'], ['a'], ['a'] ], + "Bug 1003014 (wrong tbl_struct): synced rows (just replicate)" +); + +# ############################################################################# +# Done. +# ############################################################################# +$sb->wipe_clean($master_dbh); +exit; diff --git a/t/pt-table-sync/samples/wrong-tbl-struct-bug-1003014.sql b/t/pt-table-sync/samples/wrong-tbl-struct-bug-1003014.sql new file mode 100644 index 00000000..8fe2f00f --- /dev/null +++ b/t/pt-table-sync/samples/wrong-tbl-struct-bug-1003014.sql @@ -0,0 +1,35 @@ +drop database if exists test; +create database test; +use test; + +-- +-- Checksum aaa in one chunk, make it differ, sync it first. +-- +create table aaa ( + `STOP_ARCHIVE` int(11) NOT NULL default '1', + UNIQUE KEY `STOP_ARCHIVE` (`STOP_ARCHIVE`) +) ENGINE=MyISAM; + +insert into aaa values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10); + +-- +-- Checksum zzz in chunks, make a chunk differ, sync it 2nd. +-- +create table `zzz` ( + id int not null primary key, + c varchar(16) not null +); + +insert into zzz values + (1,'a'),(2,'a'),(3,'a'),(4,'a'),(5,'a'),(6,'a'),(7,'a'),(8,'a'),(9,'a'),(10,'a'), + (11,'a'),(12,'a'),(13,'a'),(14,'a'),(15,'a'),(16,'a'),(17,'a'),(18,'a'),(19,'a'),(20,'a'), + (21,'a'),(22,'a'),(23,'a'),(24,'a'),(25,'a'),(26,'a'),(27,'a'),(28,'a'),(29,'a'),(30,'a'), + (31,'a'),(32,'a'),(33,'a'),(34,'a'),(35,'a'),(36,'a'),(37,'a'),(38,'a'),(39,'a'),(40,'a'), + (41,'a'),(42,'a'),(43,'a'),(44,'a'),(45,'a'),(46,'a'),(47,'a'),(48,'a'),(49,'a'),(50,'a'), + (51,'a'),(52,'a'),(53,'a'),(54,'a'),(55,'a'),(56,'a'),(57,'a'),(58,'a'),(59,'a'),(60,'a'), + (61,'a'),(62,'a'),(63,'a'),(64,'a'),(65,'a'),(66,'a'),(67,'a'),(68,'a'),(69,'a'),(70,'a'), + (71,'a'),(72,'a'),(73,'a'),(74,'a'),(75,'a'),(76,'a'),(77,'a'),(78,'a'),(79,'a'),(80,'a'), + (81,'a'),(82,'a'),(83,'a'),(84,'a'),(85,'a'),(86,'a'),(87,'a'),(88,'a'),(89,'a'),(90,'a'), + (91,'a'),(92,'a'),(93,'a'),(94,'a'),(95,'a'),(96,'a'),(97,'a'),(98,'a'),(99,'a'),(100,'a'), + (101,'a'),(102,'a'),(103,'a'),(104,'a'),(105,'a'),(106,'a'),(107,'a'),(108,'a'),(109,'a'),(110,'a'), + (111, 'a');