From 5ddfb95ca7bea40d7ea576d72c00d92cd08f4630 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Wed, 13 Mar 2013 12:38:45 -0600 Subject: [PATCH] Fix lib/Cxn.pm that didn't merge correctly. Fix t/lib/OptionParser.t test failure. Update Cxn, OptionParser, and DSNParser in all tools. --- bin/pt-config-diff | 11 ++++--- bin/pt-deadlock-logger | 11 ++++--- bin/pt-fk-error-logger | 11 ++++--- bin/pt-kill | 11 ++++--- bin/pt-online-schema-change | 11 ++++--- bin/pt-table-checksum | 11 ++++--- bin/pt-upgrade | 63 +++++++++++++++++++++++++---------- lib/Cxn.pm | 65 +++++++++++++++++++++++++++---------- t/lib/OptionParser.t | 2 +- 9 files changed, 137 insertions(+), 59 deletions(-) diff --git a/bin/pt-config-diff b/bin/pt-config-diff index 7c76af15..4696244a 100755 --- a/bin/pt-config-diff +++ b/bin/pt-config-diff @@ -2223,15 +2223,19 @@ sub new { $dsn = $dp->copy($prev_dsn, $dsn); } + my $dsn_name = $dp->as_string($dsn, [qw(h P S)]) + || $dp->as_string($dsn, [qw(F)]) + || ''; + my $self = { dsn => $dsn, dbh => $args{dbh}, - dsn_name => $dp->as_string($dsn, [qw(h P S)]), + dsn_name => $dsn_name, hostname => '', set => $args{set}, NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, dbh_set => 0, - OptionParser => $o, + ask_pass => $o->get('ask-pass'), DSNParser => $dp, is_cluster_node => undef, parent => $args{parent}, @@ -2244,11 +2248,10 @@ sub connect { my ( $self, %opts ) = @_; my $dsn = $self->{dsn}; my $dp = $self->{DSNParser}; - my $o = $self->{OptionParser}; my $dbh = $self->{dbh}; if ( !$dbh || !$dbh->ping() ) { - if ( $o->get('ask-pass') && !$self->{asked_for_pass} ) { + if ( $self->{ask_pass} && !$self->{asked_for_pass} ) { $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $self->{asked_for_pass} = 1; } diff --git a/bin/pt-deadlock-logger b/bin/pt-deadlock-logger index 13241478..baed4824 100755 --- a/bin/pt-deadlock-logger +++ b/bin/pt-deadlock-logger @@ -2567,15 +2567,19 @@ sub new { $dsn = $dp->copy($prev_dsn, $dsn); } + my $dsn_name = $dp->as_string($dsn, [qw(h P S)]) + || $dp->as_string($dsn, [qw(F)]) + || ''; + my $self = { dsn => $dsn, dbh => $args{dbh}, - dsn_name => $dp->as_string($dsn, [qw(h P S)]), + dsn_name => $dsn_name, hostname => '', set => $args{set}, NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, dbh_set => 0, - OptionParser => $o, + ask_pass => $o->get('ask-pass'), DSNParser => $dp, is_cluster_node => undef, parent => $args{parent}, @@ -2588,11 +2592,10 @@ sub connect { my ( $self, %opts ) = @_; my $dsn = $self->{dsn}; my $dp = $self->{DSNParser}; - my $o = $self->{OptionParser}; my $dbh = $self->{dbh}; if ( !$dbh || !$dbh->ping() ) { - if ( $o->get('ask-pass') && !$self->{asked_for_pass} ) { + if ( $self->{ask_pass} && !$self->{asked_for_pass} ) { $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $self->{asked_for_pass} = 1; } diff --git a/bin/pt-fk-error-logger b/bin/pt-fk-error-logger index c2b2fe8c..b098d3b4 100755 --- a/bin/pt-fk-error-logger +++ b/bin/pt-fk-error-logger @@ -1724,15 +1724,19 @@ sub new { $dsn = $dp->copy($prev_dsn, $dsn); } + my $dsn_name = $dp->as_string($dsn, [qw(h P S)]) + || $dp->as_string($dsn, [qw(F)]) + || ''; + my $self = { dsn => $dsn, dbh => $args{dbh}, - dsn_name => $dp->as_string($dsn, [qw(h P S)]), + dsn_name => $dsn_name, hostname => '', set => $args{set}, NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, dbh_set => 0, - OptionParser => $o, + ask_pass => $o->get('ask-pass'), DSNParser => $dp, is_cluster_node => undef, parent => $args{parent}, @@ -1745,11 +1749,10 @@ sub connect { my ( $self, %opts ) = @_; my $dsn = $self->{dsn}; my $dp = $self->{DSNParser}; - my $o = $self->{OptionParser}; my $dbh = $self->{dbh}; if ( !$dbh || !$dbh->ping() ) { - if ( $o->get('ask-pass') && !$self->{asked_for_pass} ) { + if ( $self->{ask_pass} && !$self->{asked_for_pass} ) { $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $self->{asked_for_pass} = 1; } diff --git a/bin/pt-kill b/bin/pt-kill index e55e5a9f..60a4b2e9 100755 --- a/bin/pt-kill +++ b/bin/pt-kill @@ -5054,15 +5054,19 @@ sub new { $dsn = $dp->copy($prev_dsn, $dsn); } + my $dsn_name = $dp->as_string($dsn, [qw(h P S)]) + || $dp->as_string($dsn, [qw(F)]) + || ''; + my $self = { dsn => $dsn, dbh => $args{dbh}, - dsn_name => $dp->as_string($dsn, [qw(h P S)]), + dsn_name => $dsn_name, hostname => '', set => $args{set}, NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, dbh_set => 0, - OptionParser => $o, + ask_pass => $o->get('ask-pass'), DSNParser => $dp, is_cluster_node => undef, parent => $args{parent}, @@ -5075,11 +5079,10 @@ sub connect { my ( $self, %opts ) = @_; my $dsn = $self->{dsn}; my $dp = $self->{DSNParser}; - my $o = $self->{OptionParser}; my $dbh = $self->{dbh}; if ( !$dbh || !$dbh->ping() ) { - if ( $o->get('ask-pass') && !$self->{asked_for_pass} ) { + if ( $self->{ask_pass} && !$self->{asked_for_pass} ) { $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $self->{asked_for_pass} = 1; } diff --git a/bin/pt-online-schema-change b/bin/pt-online-schema-change index d0bed7f6..cd7e2871 100755 --- a/bin/pt-online-schema-change +++ b/bin/pt-online-schema-change @@ -3683,15 +3683,19 @@ sub new { $dsn = $dp->copy($prev_dsn, $dsn); } + my $dsn_name = $dp->as_string($dsn, [qw(h P S)]) + || $dp->as_string($dsn, [qw(F)]) + || ''; + my $self = { dsn => $dsn, dbh => $args{dbh}, - dsn_name => $dp->as_string($dsn, [qw(h P S)]), + dsn_name => $dsn_name, hostname => '', set => $args{set}, NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, dbh_set => 0, - OptionParser => $o, + ask_pass => $o->get('ask-pass'), DSNParser => $dp, is_cluster_node => undef, parent => $args{parent}, @@ -3704,11 +3708,10 @@ sub connect { my ( $self, %opts ) = @_; my $dsn = $self->{dsn}; my $dp = $self->{DSNParser}; - my $o = $self->{OptionParser}; my $dbh = $self->{dbh}; if ( !$dbh || !$dbh->ping() ) { - if ( $o->get('ask-pass') && !$self->{asked_for_pass} ) { + if ( $self->{ask_pass} && !$self->{asked_for_pass} ) { $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $self->{asked_for_pass} = 1; } diff --git a/bin/pt-table-checksum b/bin/pt-table-checksum index 61dc13a7..4c71ba02 100755 --- a/bin/pt-table-checksum +++ b/bin/pt-table-checksum @@ -3461,15 +3461,19 @@ sub new { $dsn = $dp->copy($prev_dsn, $dsn); } + my $dsn_name = $dp->as_string($dsn, [qw(h P S)]) + || $dp->as_string($dsn, [qw(F)]) + || ''; + my $self = { dsn => $dsn, dbh => $args{dbh}, - dsn_name => $dp->as_string($dsn, [qw(h P S)]), + dsn_name => $dsn_name, hostname => '', set => $args{set}, NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, dbh_set => 0, - OptionParser => $o, + ask_pass => $o->get('ask-pass'), DSNParser => $dp, is_cluster_node => undef, parent => $args{parent}, @@ -3482,11 +3486,10 @@ sub connect { my ( $self, %opts ) = @_; my $dsn = $self->{dsn}; my $dp = $self->{DSNParser}; - my $o = $self->{OptionParser}; my $dbh = $self->{dbh}; if ( !$dbh || !$dbh->ping() ) { - if ( $o->get('ask-pass') && !$self->{asked_for_pass} ) { + if ( $self->{ask_pass} && !$self->{asked_for_pass} ) { $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $self->{asked_for_pass} = 1; } diff --git a/bin/pt-upgrade b/bin/pt-upgrade index 1109c9b4..781aaf31 100755 --- a/bin/pt-upgrade +++ b/bin/pt-upgrade @@ -2397,23 +2397,24 @@ sub new { || ''; my $self = { - dsn => $dsn, - dbh => $args{dbh}, - dsn_name => $dsn_name, - hostname => '', - set => $args{set}, - NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, - dbh_set => 0, - ask_pass => $o->get('ask-pass'), - DSNParser => $dp, + dsn => $dsn, + dbh => $args{dbh}, + dsn_name => $dsn_name, + hostname => '', + set => $args{set}, + NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, + dbh_set => 0, + ask_pass => $o->get('ask-pass'), + DSNParser => $dp, is_cluster_node => undef, + parent => $args{parent}, }; return bless $self, $class; } sub connect { - my ( $self ) = @_; + my ( $self, %opts ) = @_; my $dsn = $self->{dsn}; my $dp = $self->{DSNParser}; @@ -2423,11 +2424,18 @@ sub connect { $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $self->{asked_for_pass} = 1; } - $dbh = $dp->get_dbh($dp->get_cxn_params($dsn), { AutoCommit => 1 }); + $dbh = $dp->get_dbh( + $dp->get_cxn_params($dsn), + { + AutoCommit => 1, + %opts, + }, + ); } - PTDEBUG && _d($dbh, 'Connected dbh to', $self->{name}); - return $self->set_dbh($dbh); + $dbh = $self->set_dbh($dbh); + PTDEBUG && _d($dbh, 'Connected dbh to', $self->{hostname},$self->{dsn_name}); + return $dbh; } sub set_dbh { @@ -2450,6 +2458,11 @@ sub set_dbh { $self->{hostname} = $hostname; } + if ( $self->{parent} ) { + PTDEBUG && _d($dbh, 'Setting InactiveDestroy=1 in parent'); + $dbh->{InactiveDestroy} = 1; + } + if ( my $set = $self->{set}) { $set->($dbh); } @@ -2459,6 +2472,13 @@ sub set_dbh { return $dbh; } +sub lost_connection { + my ($self, $e) = @_; + return 0 unless $e; + return $e =~ m/MySQL server has gone away/ + || $e =~ m/Lost connection to MySQL server/; +} + sub dbh { my ($self) = @_; return $self->{dbh}; @@ -2477,12 +2497,21 @@ sub name { sub DESTROY { my ($self) = @_; - if ( $self->{dbh} - && blessed($self->{dbh}) - && $self->{dbh}->can("disconnect") ) { - PTDEBUG && _d('Disconnecting dbh', $self->{dbh}, $self->{name}); + + PTDEBUG && _d('Destroying cxn'); + + if ( $self->{parent} ) { + PTDEBUG && _d($self->{dbh}, 'Not disconnecting dbh in parent'); + } + elsif ( $self->{dbh} + && blessed($self->{dbh}) + && $self->{dbh}->can("disconnect") ) + { + PTDEBUG && _d($self->{dbh}, 'Disconnecting dbh on', $self->{hostname}, + $self->{dsn_name}); $self->{dbh}->disconnect(); } + return; } diff --git a/lib/Cxn.pm b/lib/Cxn.pm index c8758d82..554c5126 100644 --- a/lib/Cxn.pm +++ b/lib/Cxn.pm @@ -102,23 +102,24 @@ sub new { || ''; my $self = { - dsn => $dsn, - dbh => $args{dbh}, - dsn_name => $dsn_name, - hostname => '', - set => $args{set}, - NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, - dbh_set => 0, - ask_pass => $o->get('ask-pass'), - DSNParser => $dp, + dsn => $dsn, + dbh => $args{dbh}, + dsn_name => $dsn_name, + hostname => '', + set => $args{set}, + NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, + dbh_set => 0, + ask_pass => $o->get('ask-pass'), + DSNParser => $dp, is_cluster_node => undef, + parent => $args{parent}, }; return bless $self, $class; } sub connect { - my ( $self ) = @_; + my ( $self, %opts ) = @_; my $dsn = $self->{dsn}; my $dp = $self->{DSNParser}; @@ -129,11 +130,18 @@ sub connect { $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $self->{asked_for_pass} = 1; } - $dbh = $dp->get_dbh($dp->get_cxn_params($dsn), { AutoCommit => 1 }); + $dbh = $dp->get_dbh( + $dp->get_cxn_params($dsn), + { + AutoCommit => 1, + %opts, + }, + ); } - PTDEBUG && _d($dbh, 'Connected dbh to', $self->{name}); - return $self->set_dbh($dbh); + $dbh = $self->set_dbh($dbh); + PTDEBUG && _d($dbh, 'Connected dbh to', $self->{hostname},$self->{dsn_name}); + return $dbh; } sub set_dbh { @@ -166,6 +174,11 @@ sub set_dbh { $self->{hostname} = $hostname; } + if ( $self->{parent} ) { + PTDEBUG && _d($dbh, 'Setting InactiveDestroy=1 in parent'); + $dbh->{InactiveDestroy} = 1; + } + # Call the set callback to let the caller SET any MySQL variables. if ( my $set = $self->{set}) { $set->($dbh); @@ -176,6 +189,15 @@ sub set_dbh { return $dbh; } +sub lost_connection { + my ($self, $e) = @_; + return 0 unless $e; + return $e =~ m/MySQL server has gone away/ + || $e =~ m/Lost connection to MySQL server/; + # The 1st pattern means that MySQL itself died or was stopped. + # The 2nd pattern means that our cxn was killed (KILL ). +} + # Sub: dbh # Return the cxn's dbh. sub dbh { @@ -200,12 +222,21 @@ sub name { sub DESTROY { my ($self) = @_; - if ( $self->{dbh} - && blessed($self->{dbh}) - && $self->{dbh}->can("disconnect") ) { - PTDEBUG && _d('Disconnecting dbh', $self->{dbh}, $self->{name}); + + PTDEBUG && _d('Destroying cxn'); + + if ( $self->{parent} ) { + PTDEBUG && _d($self->{dbh}, 'Not disconnecting dbh in parent'); + } + elsif ( $self->{dbh} + && blessed($self->{dbh}) + && $self->{dbh}->can("disconnect") ) + { + PTDEBUG && _d($self->{dbh}, 'Disconnecting dbh on', $self->{hostname}, + $self->{dsn_name}); $self->{dbh}->disconnect(); } + return; } diff --git a/t/lib/OptionParser.t b/t/lib/OptionParser.t index e0e5e606..2f7e7bf5 100644 --- a/t/lib/OptionParser.t +++ b/t/lib/OptionParser.t @@ -1980,7 +1980,7 @@ my %synop = $o->_parse_synopsis(); is_deeply( \%synop, { - usage => "pt-archiver [OPTION...] --source DSN --where WHERE", + usage => "pt-archiver [OPTIONS] --source DSN --where WHERE", description => "pt-archiver nibbles records from a MySQL table. The --source and --dest arguments use DSN syntax; if COPY is yes, --dest defaults to the key's value from --source.", }, "_parse_synopsis() gets usage and description"