Merge lp:~percona-toolkit-dev/percona-toolkit/fix-pt-osc-drop-bug-1188002 into lp:percona-toolkit/2.2

Proposed by Daniel Nichter
Status: Merged
Approved by: Daniel Nichter
Approved revision: 582
Merged at revision: 582
Proposed branch: lp:~percona-toolkit-dev/percona-toolkit/fix-pt-osc-drop-bug-1188002
Merge into: lp:percona-toolkit/2.2
Diff against target: 202 lines (+136/-9)
3 files modified
bin/pt-online-schema-change (+20/-9)
t/pt-online-schema-change/cleanup.t (+80/-0)
t/pt-online-schema-change/samples/plugins/make_drop_trigger_fail.pm (+36/-0)
To merge this branch: bzr merge lp:~percona-toolkit-dev/percona-toolkit/fix-pt-osc-drop-bug-1188002
Reviewer Review Type Date Requested Status
Daniel Nichter Approve
Review via email: mp+168183@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Daniel Nichter (daniel-nichter) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/pt-online-schema-change'
2--- bin/pt-online-schema-change 2013-04-19 23:26:48 +0000
3+++ bin/pt-online-schema-change 2013-06-07 20:57:30 +0000
4@@ -7710,16 +7710,18 @@
5 my $exit_status = 0;
6 my $oktorun = 1;
7 my @drop_trigger_sqls;
8+my @triggers_not_dropped;
9
10 $OUTPUT_AUTOFLUSH = 1;
11
12 sub main {
13- local @ARGV = @_;
14+ local @ARGV = @_;
15
16 # Reset global vars else tests will fail.
17- $exit_status = 0;
18- $oktorun = 1;
19- @drop_trigger_sqls = ();
20+ $exit_status = 0;
21+ $oktorun = 1;
22+ @drop_trigger_sqls = ();
23+ @triggers_not_dropped = ();
24
25 my %stats = (
26 INSERT => 0,
27@@ -8419,6 +8421,16 @@
28 . "--no-drop-new-table was specified. To drop the new table, "
29 . "execute:\n$sql\n";
30 }
31+ elsif ( @triggers_not_dropped ) {
32+ # https://bugs.launchpad.net/percona-toolkit/+bug/1188002
33+ print "Not dropping the new table $new_tbl->{name} because "
34+ . "dropping these triggers failed:\n"
35+ . join("\n", map { " $_" } @triggers_not_dropped)
36+ . "\nThese triggers must be dropped before dropping "
37+ . "$new_tbl->{name}, else writing to $orig_tbl->{name} will "
38+ . "cause MySQL error 1146 (42S02): \"Table $new_tbl->{name} "
39+ . " doesn't exist\".\n";
40+ }
41 else {
42 print "Dropping new table...\n";
43 print $sql, "\n" if $o->get('print');
44@@ -10025,8 +10037,7 @@
45 else {
46 print "Dropping triggers...\n";
47 }
48-
49- my @not_dropped;
50+
51 foreach my $sql ( @drop_trigger_sqls ) {
52 print $sql, "\n" if $o->get('print');
53 if ( $o->get('execute') ) {
54@@ -10044,19 +10055,19 @@
55 };
56 if ( $EVAL_ERROR ) {
57 warn "Error dropping trigger: $EVAL_ERROR\n";
58- push @not_dropped, $sql;
59+ push @triggers_not_dropped, $sql;
60 $exit_status = 1;
61 }
62 }
63 }
64
65 if ( $o->get('execute') ) {
66- if ( !@not_dropped ) {
67+ if ( !@triggers_not_dropped ) {
68 print "Dropped triggers OK.\n";
69 }
70 else {
71 warn "To try dropping the triggers again, execute:\n"
72- . join("\n", @not_dropped) . "\n";
73+ . join("\n", @triggers_not_dropped) . "\n";
74 }
75 }
76
77
78=== added file 't/pt-online-schema-change/cleanup.t'
79--- t/pt-online-schema-change/cleanup.t 1970-01-01 00:00:00 +0000
80+++ t/pt-online-schema-change/cleanup.t 2013-06-07 20:57:30 +0000
81@@ -0,0 +1,80 @@
82+#!/usr/bin/env perl
83+
84+BEGIN {
85+ die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
86+ unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
87+ unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
88+};
89+
90+use strict;
91+use warnings FATAL => 'all';
92+use English qw(-no_match_vars);
93+use Test::More;
94+
95+use PerconaTest;
96+use Sandbox;
97+require "$trunk/bin/pt-online-schema-change";
98+require VersionParser;
99+
100+use Time::HiRes qw(sleep);
101+use Data::Dumper;
102+$Data::Dumper::Indent = 1;
103+$Data::Dumper::Sortkeys = 1;
104+$Data::Dumper::Quotekeys = 0;
105+
106+my $dp = new DSNParser(opts=>$dsn_opts);
107+my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
108+my $dbh1 = $sb->get_dbh_for('master');
109+my $dbh2 = $sb->get_dbh_for('master');
110+
111+if ( !$dbh1 || !$dbh2 ) {
112+ plan skip_all => 'Cannot connect to sandbox master';
113+}
114+elsif ( $sandbox_version lt '5.5' ) {
115+ plan skip_all => "Metadata locks require MySQL 5.5 and newer";
116+}
117+
118+my $output;
119+my $master_dsn = $sb->dsn_for('master');
120+my $sample = "t/pt-online-schema-change/samples";
121+my $plugin = "$trunk/$sample/plugins";
122+my $exit;
123+my $rows;
124+
125+# Loads pt_osc.t with cols id (pk), c (unique index),, d.
126+$sb->load_file('master', "$sample/basic_no_fks_innodb.sql");
127+
128+# #############################################################################
129+# Meta-block on create_triggers.
130+# #############################################################################
131+
132+($output) = full_output(
133+ sub { pt_online_schema_change::main(
134+ "$master_dsn,D=pt_osc,t=t",
135+ qw(--statistics --execute --tries drop_triggers:1:0.01),
136+ qw(--set-vars lock_wait_timeout=1 --print --no-swap-tables),
137+ '--plugin', "$plugin/make_drop_trigger_fail.pm",
138+ )},
139+ stderr => 1,
140+);
141+
142+my $triggers = $dbh1->selectall_arrayref("SHOW TRIGGERS FROM pt_osc");
143+is(
144+ @$triggers,
145+ 3,
146+ "Bug 1188002: triggers not dropped"
147+) or diag(Dumper($triggers));
148+
149+my $tables = $dbh1->selectall_arrayref("SHOW TABLES FROM pt_osc");
150+is_deeply(
151+ $tables,
152+ [ ['_t_new'], ['t'] ],
153+ "Bug 1188002: new table not dropped"
154+) or diag(Dumper($tables));
155+
156+# #############################################################################
157+# Done.
158+# #############################################################################
159+$sb->wipe_clean($dbh1);
160+ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
161+done_testing;
162
163=== added file 't/pt-online-schema-change/samples/plugins/make_drop_trigger_fail.pm'
164--- t/pt-online-schema-change/samples/plugins/make_drop_trigger_fail.pm 1970-01-01 00:00:00 +0000
165+++ t/pt-online-schema-change/samples/plugins/make_drop_trigger_fail.pm 2013-06-07 20:57:30 +0000
166@@ -0,0 +1,36 @@
167+package pt_online_schema_change_plugin;
168+
169+use strict;
170+use warnings FATAL => 'all';
171+use English qw(-no_match_vars);
172+use constant PTDEBUG => $ENV{PTDEBUG} || 0;
173+
174+sub new {
175+ my ($class, %args) = @_;
176+ my $self = { %args };
177+ return bless $self, $class;
178+}
179+
180+sub init {
181+ my ($self, %args) = @_;
182+ print "PLUGIN: init()\n";
183+ $self->{orig_tbl} = $args{orig_tbl};
184+}
185+
186+sub before_drop_triggers {
187+ my ($self, %args) = @_;
188+ print "PLUGIN: before_drop_triggers()\n";
189+
190+ my $dbh = $self->{aux_cxn}->dbh;
191+ my $orig_tbl = $self->{orig_tbl};
192+
193+ # Start a trx and get a metadata lock on the table being altered.
194+ $dbh->do('SET autocommit=0');
195+ $dbh->{AutoCommit} = 0;
196+ $dbh->do("START TRANSACTION");
197+ $dbh->do("SELECT * FROM " . $orig_tbl->{name});
198+
199+ return;
200+}
201+
202+1;

Subscribers

People subscribed via source and target branches