Merge lp:~percona-toolkit-dev/percona-toolkit/fix-pt-heartbeat-dupe-key-bug-1004567 into lp:percona-toolkit/2.1

Proposed by Daniel Nichter
Status: Merged
Merged at revision: 320
Proposed branch: lp:~percona-toolkit-dev/percona-toolkit/fix-pt-heartbeat-dupe-key-bug-1004567
Merge into: lp:percona-toolkit/2.1
Diff against target: 173 lines (+82/-16)
2 files modified
bin/pt-heartbeat (+5/-1)
t/pt-heartbeat/basics.t (+77/-15)
To merge this branch: bzr merge lp:~percona-toolkit-dev/percona-toolkit/fix-pt-heartbeat-dupe-key-bug-1004567
Reviewer Review Type Date Requested Status
Daniel Nichter Approve
Review via email: mp+116083@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
=== modified file 'bin/pt-heartbeat'
--- bin/pt-heartbeat 2012-07-20 20:25:10 +0000
+++ bin/pt-heartbeat 2012-07-20 22:10:25 +0000
@@ -3352,10 +3352,14 @@
3352 PTDEBUG && _d($sql);3352 PTDEBUG && _d($sql);
3353 $dbh->do($sql);3353 $dbh->do($sql);
33543354
3355 $sql = "INSERT INTO $db_tbl (ts, server_id) VALUES (NOW(), $server_id)";3355 $sql = ($o->get('replace') ? "REPLACE" : "INSERT")
3356 . qq/ INTO $db_tbl (ts, server_id) VALUES (NOW(), $server_id)/;
3356 PTDEBUG && _d($sql);3357 PTDEBUG && _d($sql);
3357 # This may fail if the table already existed and already had this row.3358 # This may fail if the table already existed and already had this row.
3358 # We eval to ignore this possibility.3359 # We eval to ignore this possibility.
3360 # NOTE: This can break replication though! See:
3361 # https://bugs.launchpad.net/percona-toolkit/+bug/1004567
3362 # So --replace should be used in most cases.
3359 eval { $dbh->do($sql); };3363 eval { $dbh->do($sql); };
3360 }3364 }
33613365
33623366
=== modified file 't/pt-heartbeat/basics.t'
--- t/pt-heartbeat/basics.t 2012-06-09 18:43:33 +0000
+++ t/pt-heartbeat/basics.t 2012-07-20 22:10:25 +0000
@@ -17,16 +17,20 @@
1717
18my $dp = new DSNParser(opts=>$dsn_opts);18my $dp = new DSNParser(opts=>$dsn_opts);
19my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);19my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
20my $dbh = $sb->get_dbh_for('master');20my $master_dbh = $sb->get_dbh_for('master');
21my $slave1_dbh = $sb->get_dbh_for('slave1');
2122
22if ( !$dbh ) {23if ( !$master_dbh ) {
23 plan skip_all => 'Cannot connect to sandbox master';24 plan skip_all => 'Cannot connect to sandbox master';
24}25}
26elsif ( !$slave1_dbh ) {
27 plan skip_all => 'Cannot connect to sandbox slave1';
28}
25else {29else {
26 plan tests => 18;30 plan tests => 22;
27}31}
2832
29$sb->create_dbs($dbh, ['test']);33$sb->create_dbs($master_dbh, ['test']);
3034
31my $output;35my $output;
32my $cnf = '/tmp/12345/my.sandbox.cnf';36my $cnf = '/tmp/12345/my.sandbox.cnf';
@@ -35,10 +39,10 @@
35my $sent_file = "/tmp/pt-heartbeat-sentinel";39my $sent_file = "/tmp/pt-heartbeat-sentinel";
36my $ps_grep_cmd = "ps x | grep pt-heartbeat | grep daemonize | grep -v grep";40my $ps_grep_cmd = "ps x | grep pt-heartbeat | grep daemonize | grep -v grep";
3741
38`rm $sent_file 2>/dev/null`;42diag(`rm $sent_file 2>/dev/null`);
3943
40$dbh->do('drop table if exists test.heartbeat');44$master_dbh->do('drop table if exists test.heartbeat');
41$dbh->do(q{CREATE TABLE test.heartbeat (45$master_dbh->do(q{CREATE TABLE test.heartbeat (
42 id int NOT NULL PRIMARY KEY,46 id int NOT NULL PRIMARY KEY,
43 ts datetime NOT NULL47 ts datetime NOT NULL
44 ) ENGINE=MEMORY});48 ) ENGINE=MEMORY});
@@ -53,7 +57,7 @@
53$output = output(57$output = output(
54 sub { pt_heartbeat::main('-F', $cnf, qw(-D test --check)) },58 sub { pt_heartbeat::main('-F', $cnf, qw(-D test --check)) },
55);59);
56my $row = $dbh->selectall_hashref('select * from test.heartbeat', 'id');60my $row = $master_dbh->selectall_hashref('select * from test.heartbeat', 'id');
57is(61is(
58 $row->{1}->{id},62 $row->{1}->{id},
59 1,63 1,
@@ -62,7 +66,7 @@
6266
63# Run one instance with --replace to create the table.67# Run one instance with --replace to create the table.
64`$cmd -D test --update --replace --run-time 1s`;68`$cmd -D test --update --replace --run-time 1s`;
65ok($dbh->selectrow_array('select id from test.heartbeat'), 'Record is there');69ok($master_dbh->selectrow_array('select id from test.heartbeat'), 'Record is there');
6670
67# Check the delay and ensure it is only a single line with nothing but the71# Check the delay and ensure it is only a single line with nothing but the
68# delay (no leading whitespace or anything).72# delay (no leading whitespace or anything).
@@ -105,7 +109,7 @@
105unlike($output, qr/$cmd/, 'It is not running');109unlike($output, qr/$cmd/, 'It is not running');
106ok(-f $sent_file, 'Sentinel file is there');110ok(-f $sent_file, 'Sentinel file is there');
107unlink($sent_file);111unlink($sent_file);
108$dbh->do('drop table if exists test.heartbeat'); # This will kill it112$master_dbh->do('drop table if exists test.heartbeat'); # This will kill it
109113
110# #############################################################################114# #############################################################################
111# Issue 353: Add --create-table to mk-heartbeat115# Issue 353: Add --create-table to mk-heartbeat
@@ -114,10 +118,10 @@
114# These creates the new table format, whereas the preceding tests used the118# These creates the new table format, whereas the preceding tests used the
115# old format, so tests from here on may need --master-server-id.119# old format, so tests from here on may need --master-server-id.
116120
117$dbh->do('drop table if exists test.heartbeat');121$master_dbh->do('drop table if exists test.heartbeat');
118diag(`$cmd --update --run-time 1s --database test --table heartbeat --create-table`);122diag(`$cmd --update --run-time 1s --database test --table heartbeat --create-table`);
119$dbh->do('use test');123$master_dbh->do('use test');
120$output = $dbh->selectcol_arrayref("SHOW TABLES LIKE 'heartbeat'");124$output = $master_dbh->selectcol_arrayref("SHOW TABLES LIKE 'heartbeat'");
121is(125is(
122 $output->[0],126 $output->[0],
123 'heartbeat', 127 'heartbeat',
@@ -136,9 +140,67 @@
136);140);
137141
138# #############################################################################142# #############################################################################
143# Bug 1004567: pt-heartbeat --update --replace causes duplicate key error
144# #############################################################################
145
146# Create the heartbeat table on the master and slave.
147$master_dbh->do("DROP TABLE IF EXISTS test.heartbeat");
148$sb->wait_for_slaves();
149
150$output = output(
151 sub { pt_heartbeat::main("F=/tmp/12345/my.sandbox.cnf",
152 qw(-D test --update --replace --create-table --run-time 1))
153 }
154);
155
156$row = $master_dbh->selectrow_arrayref('SELECT server_id FROM test.heartbeat');
157is(
158 $row->[0],
159 '12345',
160 'Heartbeat on master'
161);
162
163$sb->wait_for_slaves();
164
165$row = $slave1_dbh->selectrow_arrayref('SELECT server_id FROM test.heartbeat');
166is(
167 $row->[0],
168 '12345',
169 'Heartbeat on slave1'
170);
171
172# Drop the heartbeat table only on the master.
173$master_dbh->do("SET SQL_LOG_BIN=0");
174$master_dbh->do("DROP TABLE test.heartbeat");
175$master_dbh->do("SET SQL_LOG_BIN=1");
176
177# Re-create the heartbeat table on the master.
178$output = output(
179 sub { pt_heartbeat::main("F=/tmp/12345/my.sandbox.cnf",
180 qw(-D test --update --replace --create-table --run-time 1))
181 }
182);
183
184$row = $master_dbh->selectrow_arrayref('SELECT server_id FROM test.heartbeat');
185is(
186 $row->[0],
187 '12345',
188 'New heartbeat on master'
189);
190
191$sb->wait_for_slaves();
192
193$row = $slave1_dbh->selectrow_hashref("SHOW SLAVE STATUS");
194is(
195 $row->{last_error},
196 '',
197 "No slave error"
198);
199
200# #############################################################################
139# Done.201# Done.
140# #############################################################################202# #############################################################################
141`rm $pid_file $sent_file 2>/dev/null`;203diag(`rm $pid_file $sent_file 2>/dev/null`);
142$sb->wipe_clean($dbh);204$sb->wipe_clean($master_dbh);
143ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");205ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
144exit;206exit;

Subscribers

People subscribed via source and target branches