From e1b7a6a1fd5d6b05194e953fdfa7febc15ab5f17 Mon Sep 17 00:00:00 2001 From: Carlos Salguero Date: Thu, 24 Dec 2020 16:23:42 -0300 Subject: [PATCH] PT-1857 heartbeat can't reconnect (#475) * PT-1857 pt-heartbeat reconnect * PT-1857 Updated changelog --- Changelog | 2 + bin/pt-heartbeat | 1 + t/pt-heartbeat/pt-1857.t | 95 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 t/pt-heartbeat/pt-1857.t diff --git a/Changelog b/Changelog index 7fa2b31f..fc3fec61 100644 --- a/Changelog +++ b/Changelog @@ -1,5 +1,7 @@ Changelog for Percona Toolkit + * Fixed bug PT-1857: pt-heartbeat cannot reconnect. + * Fixed bug PT-169 : pt-online-schema-change FKs error handling. * Fixed bug PT-1892: pt-summary says sshd not running * Fixed bug PT-1881: pt-upgrade fails when query including format strings and SQL errors is given (Thanks Nayuta Yanagisawa) diff --git a/bin/pt-heartbeat b/bin/pt-heartbeat index ea2efcb7..19a22f4d 100755 --- a/bin/pt-heartbeat +++ b/bin/pt-heartbeat @@ -5947,6 +5947,7 @@ sub main { $dbh->{InactiveDestroy} = 1; # Don't disconnect on fork $dbh->{FetchHashKeyName} = 'NAME_lc'; + $dbh->{mysql_auto_reconnect} = 1; $dbh->do("USE `$db`"); # ######################################################################## diff --git a/t/pt-heartbeat/pt-1857.t b/t/pt-heartbeat/pt-1857.t new file mode 100644 index 00000000..a309df50 --- /dev/null +++ b/t/pt-heartbeat/pt-1857.t @@ -0,0 +1,95 @@ +#!/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 threads ('yield'); + +use English qw(-no_match_vars); +use Test::More; + +use Data::Dumper; +use PerconaTest; +use Sandbox; +use SqlModes; +use File::Temp qw/ tempfile /; + +plan tests => 2; + +require "$trunk/bin/pt-heartbeat"; + +my $dp = new DSNParser(opts=>$dsn_opts); +my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp); +my $master_dbh = $sb->get_dbh_for('master'); +my $master_dsn = 'h=127.1,P=12345,u=msandbox,p=msandbox'; + +my $slave1_dbh = $sb->get_dbh_for('slave1'); +my $slave1_dsn = 'h=127.1,P=12346,u=unprivileged,p=password'; + +if ( !$master_dbh ) { + plan skip_all => 'Cannot connect to sandbox master'; +} + +sub start_thread { + my ($dsn_opts, $sleep) = @_; + + my $dp = new DSNParser(opts=>$dsn_opts); + my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp); + my $dbh= $sb->get_dbh_for('slave1'); + my $rows = $dbh->selectall_arrayref('SHOW PROCESSLIST', { Slice => {} }); + for my $row (@$rows) { + if ($row->{user} eq 'unprivileged') { + $dbh->do("kill $row->{id}"); + } + } +} + +my $create_table_sql = <<__EOQ; + CREATE TABLE IF NOT EXISTS sakila.heartbeat ( + ts varchar(26) NOT NULL, + server_id int unsigned NOT NULL PRIMARY KEY, + file varchar(255) DEFAULT NULL, -- SHOW MASTER STATUS + position bigint unsigned DEFAULT NULL, -- SHOW MASTER STATUS + relay_master_log_file varchar(255) DEFAULT NULL, -- SHOW SLAVE STATUS + exec_master_log_pos bigint unsigned DEFAULT NULL -- SHOW SLAVE STATUS + ); +__EOQ + +$sb->do_as_root('master', "$create_table_sql"); +if ($sandbox_version ge '8.0') { + $sb->do_as_root('slave1', 'CREATE USER "unprivileged"@"localhost" IDENTIFIED WITH mysql_native_password BY "password"'); +} else { + $sb->do_as_root('slave1', 'CREATE USER "unprivileged"@"localhost" IDENTIFIED BY "password"'); +} +$sb->do_as_root('slave1', 'GRANT SELECT, INSERT, UPDATE, REPLICATION CLIENT ON *.* TO "unprivileged"@"localhost"'); +$sb->do_as_root('slave1', "FLUSH TABLES WITH READ LOCK;"); +$sb->do_as_root('slave1', "SET GLOBAL read_only = 1;"); + +my $thread = threads->create('start_thread', $dsn_opts, 4); +$thread->detach(); +threads->yield(); + +my $output = `PTDEBUG=1 $trunk/bin/pt-heartbeat --database=sakila --table heartbeat --read-only-interval 2 --check-read-only --run-time 5 --update $slave1_dsn 2>&1`; + +unlike ( + $output, + qr/Lost connection to MySQL/, + 'PT-1508 --read-only-interval', +); + +$master_dbh->do("DROP DATABASE IF EXISTS test"); + +# ############################################################################# +# Done. +# ############################################################################# +$sb->do_as_root('master', 'DROP TABLE IF EXISTS sakila.heartbeat'); +$sb->do_as_root('slave1', 'DROP USER "unprivileged"@"localhost"'); + +$sb->wipe_clean($master_dbh); +ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox"); +done_testing;