mirror of
https://github.com/percona/percona-toolkit.git
synced 2026-04-26 01:02:25 +08:00
Update KeySize in pt-duplicate-key-checker to fix bug. Update all other modules too.
This commit is contained in:
+199
-130
@@ -23,7 +23,7 @@ BEGIN {
|
||||
Daemon
|
||||
Schema
|
||||
SchemaIterator
|
||||
HTTPMicro
|
||||
HTTP::Micro
|
||||
VersionCheck
|
||||
));
|
||||
}
|
||||
@@ -133,6 +133,8 @@ sub quote_val {
|
||||
return $val if $val =~ m/^0x[0-9a-fA-F]+$/ # quote hex data
|
||||
&& !$args{is_char}; # unless is_char is true
|
||||
|
||||
return $val if $args{is_float};
|
||||
|
||||
$val =~ s/(['\\])/\\$1/g;
|
||||
return "'$val'";
|
||||
}
|
||||
@@ -2203,7 +2205,7 @@ sub get_key_size {
|
||||
foreach my $col ( @cols ) {
|
||||
push @where_cols, "$col=1";
|
||||
}
|
||||
if ( scalar @cols == 1 ) {
|
||||
if ( scalar(@cols) == 1 && !$args{only_eq} ) {
|
||||
push @where_cols, "$cols[0]<>1";
|
||||
}
|
||||
$sql .= join(' OR ', @where_cols);
|
||||
@@ -2227,6 +2229,21 @@ sub get_key_size {
|
||||
PTDEBUG && _d('MySQL chose key:', $chosen_key, 'len:', $key_len,
|
||||
'rows:', $rows);
|
||||
|
||||
if ( $chosen_key && $key_len eq '0' ) {
|
||||
if ( $args{recurse} ) {
|
||||
$self->{error} = "key_len = 0 in EXPLAIN:\n"
|
||||
. _explain_to_text($explain);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
return $self->get_key_size(
|
||||
%args,
|
||||
only_eq => 1,
|
||||
recurse => 1,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
my $key_size = 0;
|
||||
if ( $key_len && $rows ) {
|
||||
if ( $chosen_key =~ m/,/ && $key_len =~ m/,/ ) {
|
||||
@@ -2687,59 +2704,79 @@ package Daemon;
|
||||
use strict;
|
||||
use warnings FATAL => 'all';
|
||||
use English qw(-no_match_vars);
|
||||
|
||||
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
|
||||
|
||||
use POSIX qw(setsid);
|
||||
use Fcntl qw(:DEFAULT);
|
||||
|
||||
sub new {
|
||||
my ($class, %args) = @_;
|
||||
foreach my $arg ( qw(o) ) {
|
||||
die "I need a $arg argument" unless $args{$arg};
|
||||
}
|
||||
my $o = $args{o};
|
||||
my $self = {
|
||||
o => $o,
|
||||
log_file => $o->has('log') ? $o->get('log') : undef,
|
||||
PID_file => $o->has('pid') ? $o->get('pid') : undef,
|
||||
log_file => $args{log_file},
|
||||
pid_file => $args{pid_file},
|
||||
daemonize => $args{daemonize},
|
||||
force_log_file => $args{force_log_file},
|
||||
parent_exit => $args{parent_exit},
|
||||
pid_file_owner => 0,
|
||||
};
|
||||
|
||||
check_PID_file(undef, $self->{PID_file});
|
||||
|
||||
PTDEBUG && _d('Daemonized child will log to', $self->{log_file});
|
||||
return bless $self, $class;
|
||||
}
|
||||
|
||||
sub daemonize {
|
||||
sub run {
|
||||
my ($self) = @_;
|
||||
|
||||
PTDEBUG && _d('About to fork and daemonize');
|
||||
defined (my $pid = fork()) or die "Cannot fork: $OS_ERROR";
|
||||
if ( $pid ) {
|
||||
PTDEBUG && _d('Parent PID', $PID, 'exiting after forking child PID',$pid);
|
||||
exit;
|
||||
my $daemonize = $self->{daemonize};
|
||||
my $pid_file = $self->{pid_file};
|
||||
my $log_file = $self->{log_file};
|
||||
my $force_log_file = $self->{force_log_file};
|
||||
my $parent_exit = $self->{parent_exit};
|
||||
|
||||
PTDEBUG && _d('Starting daemon');
|
||||
|
||||
if ( $pid_file ) {
|
||||
eval {
|
||||
$self->_make_pid_file(
|
||||
pid => $PID, # parent's pid
|
||||
pid_file => $pid_file,
|
||||
);
|
||||
};
|
||||
die "$EVAL_ERROR\n" if $EVAL_ERROR;
|
||||
if ( !$daemonize ) {
|
||||
$self->{pid_file_owner} = $PID; # parent's pid
|
||||
}
|
||||
}
|
||||
|
||||
PTDEBUG && _d('Daemonizing child PID', $PID);
|
||||
$self->{PID_owner} = $PID;
|
||||
$self->{child} = 1;
|
||||
if ( $daemonize ) {
|
||||
defined (my $child_pid = fork()) or die "Cannot fork: $OS_ERROR";
|
||||
if ( $child_pid ) {
|
||||
PTDEBUG && _d('Forked child', $child_pid);
|
||||
$parent_exit->($child_pid) if $parent_exit;
|
||||
exit 0;
|
||||
}
|
||||
|
||||
POSIX::setsid() or die "Cannot start a new session: $OS_ERROR";
|
||||
chdir '/' or die "Cannot chdir to /: $OS_ERROR";
|
||||
|
||||
$self->_make_PID_file();
|
||||
|
||||
$OUTPUT_AUTOFLUSH = 1;
|
||||
if ( $pid_file ) {
|
||||
$self->_update_pid_file(
|
||||
pid => $PID, # child's pid
|
||||
pid_file => $pid_file,
|
||||
);
|
||||
$self->{pid_file_owner} = $PID;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $daemonize || $force_log_file ) {
|
||||
PTDEBUG && _d('Redirecting STDIN to /dev/null');
|
||||
close STDIN;
|
||||
open STDIN, '/dev/null'
|
||||
or die "Cannot reopen STDIN to /dev/null: $OS_ERROR";
|
||||
|
||||
if ( $self->{log_file} ) {
|
||||
PTDEBUG && _d('Redirecting STDOUT and STDERR to', $self->{log_file});
|
||||
if ( $log_file ) {
|
||||
PTDEBUG && _d('Redirecting STDOUT and STDERR to', $log_file);
|
||||
close STDOUT;
|
||||
open STDOUT, '>>', $self->{log_file}
|
||||
or die "Cannot open log file $self->{log_file}: $OS_ERROR";
|
||||
open STDOUT, '>>', $log_file
|
||||
or die "Cannot open log file $log_file: $OS_ERROR";
|
||||
|
||||
close STDERR;
|
||||
open STDERR, ">&STDOUT"
|
||||
@@ -2762,82 +2799,119 @@ sub daemonize {
|
||||
}
|
||||
}
|
||||
|
||||
$OUTPUT_AUTOFLUSH = 1;
|
||||
}
|
||||
|
||||
PTDEBUG && _d('Daemon running');
|
||||
return;
|
||||
}
|
||||
|
||||
sub check_PID_file {
|
||||
my ( $self, $file ) = @_;
|
||||
my $PID_file = $self ? $self->{PID_file} : $file;
|
||||
PTDEBUG && _d('Checking PID file', $PID_file);
|
||||
if ( $PID_file && -f $PID_file ) {
|
||||
my $pid;
|
||||
eval {
|
||||
chomp($pid = (slurp_file($PID_file) || ''));
|
||||
sub _make_pid_file {
|
||||
my ($self, %args) = @_;
|
||||
my @required_args = qw(pid pid_file);
|
||||
foreach my $arg ( @required_args ) {
|
||||
die "I need a $arg argument" unless $args{$arg};
|
||||
};
|
||||
if ( $EVAL_ERROR ) {
|
||||
die "The PID file $PID_file already exists but it cannot be read: "
|
||||
. $EVAL_ERROR;
|
||||
my $pid = $args{pid};
|
||||
my $pid_file = $args{pid_file};
|
||||
|
||||
eval {
|
||||
sysopen(PID_FH, $pid_file, O_RDWR|O_CREAT|O_EXCL) or die $OS_ERROR;
|
||||
print PID_FH $PID, "\n";
|
||||
close PID_FH;
|
||||
};
|
||||
if ( my $e = $EVAL_ERROR ) {
|
||||
if ( $e =~ m/file exists/i ) {
|
||||
my $old_pid = $self->_check_pid_file(
|
||||
pid_file => $pid_file,
|
||||
pid => $PID,
|
||||
);
|
||||
if ( $old_pid ) {
|
||||
warn "Overwriting PID file $pid_file because PID $old_pid "
|
||||
. "is not running.\n";
|
||||
}
|
||||
PTDEBUG && _d('PID file exists; it contains PID', $pid);
|
||||
if ( $pid ) {
|
||||
my $pid_is_alive = kill 0, $pid;
|
||||
$self->_update_pid_file(
|
||||
pid => $PID,
|
||||
pid_file => $pid_file
|
||||
);
|
||||
}
|
||||
else {
|
||||
die "Error creating PID file $pid_file: $e\n";
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub _check_pid_file {
|
||||
my ($self, %args) = @_;
|
||||
my @required_args = qw(pid_file pid);
|
||||
foreach my $arg ( @required_args ) {
|
||||
die "I need a $arg argument" unless $args{$arg};
|
||||
};
|
||||
my $pid_file = $args{pid_file};
|
||||
my $pid = $args{pid};
|
||||
|
||||
PTDEBUG && _d('Checking if PID in', $pid_file, 'is running');
|
||||
|
||||
if ( ! -f $pid_file ) {
|
||||
PTDEBUG && _d('PID file', $pid_file, 'does not exist');
|
||||
return;
|
||||
}
|
||||
|
||||
open my $fh, '<', $pid_file
|
||||
or die "Error opening $pid_file: $OS_ERROR";
|
||||
my $existing_pid = do { local $/; <$fh> };
|
||||
chomp($existing_pid) if $existing_pid;
|
||||
close $fh
|
||||
or die "Error closing $pid_file: $OS_ERROR";
|
||||
|
||||
if ( $existing_pid ) {
|
||||
if ( $existing_pid == $pid ) {
|
||||
warn "The current PID $pid already holds the PID file $pid_file\n";
|
||||
return;
|
||||
}
|
||||
else {
|
||||
PTDEBUG && _d('Checking if PID', $existing_pid, 'is running');
|
||||
my $pid_is_alive = kill 0, $existing_pid;
|
||||
if ( $pid_is_alive ) {
|
||||
die "The PID file $PID_file already exists "
|
||||
. " and the PID that it contains, $pid, is running";
|
||||
die "PID file $pid_file exists and PID $existing_pid is running\n";
|
||||
}
|
||||
else {
|
||||
warn "Overwriting PID file $PID_file because the PID that it "
|
||||
. "contains, $pid, is not running";
|
||||
}
|
||||
}
|
||||
else {
|
||||
die "The PID file $PID_file already exists but it does not "
|
||||
. "contain a PID";
|
||||
die "PID file $pid_file exists but it is empty. Remove the file "
|
||||
. "if the process is no longer running.\n";
|
||||
}
|
||||
|
||||
return $existing_pid;
|
||||
}
|
||||
else {
|
||||
PTDEBUG && _d('No PID file');
|
||||
}
|
||||
|
||||
sub _update_pid_file {
|
||||
my ($self, %args) = @_;
|
||||
my @required_args = qw(pid pid_file);
|
||||
foreach my $arg ( @required_args ) {
|
||||
die "I need a $arg argument" unless $args{$arg};
|
||||
};
|
||||
my $pid = $args{pid};
|
||||
my $pid_file = $args{pid_file};
|
||||
|
||||
open my $fh, '>', $pid_file
|
||||
or die "Cannot open $pid_file: $OS_ERROR";
|
||||
print { $fh } $pid, "\n"
|
||||
or die "Cannot print to $pid_file: $OS_ERROR";
|
||||
close $fh
|
||||
or warn "Cannot close $pid_file: $OS_ERROR";
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub make_PID_file {
|
||||
my ( $self ) = @_;
|
||||
if ( exists $self->{child} ) {
|
||||
die "Do not call Daemon::make_PID_file() for daemonized scripts";
|
||||
}
|
||||
$self->_make_PID_file();
|
||||
$self->{PID_owner} = $PID;
|
||||
return;
|
||||
}
|
||||
|
||||
sub _make_PID_file {
|
||||
my ( $self ) = @_;
|
||||
|
||||
my $PID_file = $self->{PID_file};
|
||||
if ( !$PID_file ) {
|
||||
PTDEBUG && _d('No PID file to create');
|
||||
return;
|
||||
}
|
||||
|
||||
$self->check_PID_file();
|
||||
|
||||
open my $PID_FH, '>', $PID_file
|
||||
or die "Cannot open PID file $PID_file: $OS_ERROR";
|
||||
print $PID_FH $PID
|
||||
or die "Cannot print to PID file $PID_file: $OS_ERROR";
|
||||
close $PID_FH
|
||||
or die "Cannot close PID file $PID_file: $OS_ERROR";
|
||||
|
||||
PTDEBUG && _d('Created PID file:', $self->{PID_file});
|
||||
return;
|
||||
}
|
||||
|
||||
sub _remove_PID_file {
|
||||
my ( $self ) = @_;
|
||||
if ( $self->{PID_file} && -f $self->{PID_file} ) {
|
||||
unlink $self->{PID_file}
|
||||
or warn "Cannot remove PID file $self->{PID_file}: $OS_ERROR";
|
||||
sub remove_pid_file {
|
||||
my ($self, $pid_file) = @_;
|
||||
$pid_file ||= $self->{pid_file};
|
||||
if ( $pid_file && -f $pid_file ) {
|
||||
unlink $self->{pid_file}
|
||||
or warn "Cannot remove PID file $pid_file: $OS_ERROR";
|
||||
PTDEBUG && _d('Removed PID file');
|
||||
}
|
||||
else {
|
||||
@@ -2849,16 +2923,11 @@ sub _remove_PID_file {
|
||||
sub DESTROY {
|
||||
my ($self) = @_;
|
||||
|
||||
$self->_remove_PID_file() if ($self->{PID_owner} || 0) == $PID;
|
||||
|
||||
return;
|
||||
if ( $self->{pid_file_owner} == $PID ) {
|
||||
$self->remove_pid_file();
|
||||
}
|
||||
|
||||
sub slurp_file {
|
||||
my ($file) = @_;
|
||||
return unless $file;
|
||||
open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR";
|
||||
return do { local $/; <$fh> };
|
||||
return;
|
||||
}
|
||||
|
||||
sub _d {
|
||||
@@ -3543,25 +3612,23 @@ sub _d {
|
||||
# ###########################################################################
|
||||
|
||||
# ###########################################################################
|
||||
# HTTPMicro package
|
||||
# HTTP::Micro package
|
||||
# This package is a copy without comments from the original. The original
|
||||
# with comments and its test file can be found in the Bazaar repository at,
|
||||
# lib/HTTPMicro.pm
|
||||
# t/lib/HTTPMicro.t
|
||||
# lib/HTTP/Micro.pm
|
||||
# t/lib/HTTP/Micro.t
|
||||
# See https://launchpad.net/percona-toolkit for more information.
|
||||
# ###########################################################################
|
||||
{
|
||||
package HTTP::Micro;
|
||||
|
||||
our $VERSION = '0.01';
|
||||
|
||||
package HTTPMicro;
|
||||
BEGIN {
|
||||
$HTTPMicro::VERSION = '0.001';
|
||||
}
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use warnings FATAL => 'all';
|
||||
use English qw(-no_match_vars);
|
||||
use Carp ();
|
||||
|
||||
|
||||
my @attributes;
|
||||
BEGIN {
|
||||
@attributes = qw(agent timeout);
|
||||
@@ -3632,7 +3699,7 @@ sub _request {
|
||||
headers => {},
|
||||
};
|
||||
|
||||
my $handle = HTTPMicro::Handle->new(timeout => $self->{timeout});
|
||||
my $handle = HTTP::Micro::Handle->new(timeout => $self->{timeout});
|
||||
|
||||
$handle->connect($scheme, $host, $port);
|
||||
|
||||
@@ -3697,14 +3764,18 @@ sub _split_url {
|
||||
return ($scheme, $host, $port, $path_query);
|
||||
}
|
||||
|
||||
package
|
||||
HTTPMicro::Handle; # hide from PAUSE/indexers
|
||||
use strict;
|
||||
use warnings;
|
||||
} # HTTP::Micro
|
||||
|
||||
use Carp qw[croak];
|
||||
use Errno qw[EINTR EPIPE];
|
||||
use IO::Socket qw[SOCK_STREAM];
|
||||
{
|
||||
package HTTP::Micro::Handle;
|
||||
|
||||
use strict;
|
||||
use warnings FATAL => 'all';
|
||||
use English qw(-no_match_vars);
|
||||
|
||||
use Carp qw(croak);
|
||||
use Errno qw(EINTR EPIPE);
|
||||
use IO::Socket qw(SOCK_STREAM);
|
||||
|
||||
sub BUFSIZE () { 32768 }
|
||||
|
||||
@@ -3747,7 +3818,7 @@ sub connect {
|
||||
croak(qq/Unsupported URL scheme '$scheme'\n/);
|
||||
}
|
||||
|
||||
$self->{fh} = 'IO::Socket::INET'->new(
|
||||
$self->{fh} = IO::Socket::INET->new(
|
||||
PeerHost => $host,
|
||||
PeerPort => $port,
|
||||
Proto => 'tcp',
|
||||
@@ -4011,6 +4082,7 @@ sub can_write {
|
||||
my $self = shift;
|
||||
return $self->_do_timeout('write', @_)
|
||||
}
|
||||
} # HTTP::Micro::Handle
|
||||
|
||||
my $prog = <<'EOP';
|
||||
BEGIN {
|
||||
@@ -4031,6 +4103,7 @@ BEGIN {
|
||||
}
|
||||
}
|
||||
{
|
||||
use Carp qw(croak);
|
||||
my %dispatcher = (
|
||||
issuer => sub { Net::SSLeay::X509_NAME_oneline( Net::SSLeay::X509_get_issuer_name( shift )) },
|
||||
subject => sub { Net::SSLeay::X509_NAME_oneline( Net::SSLeay::X509_get_subject_name( shift )) },
|
||||
@@ -4186,9 +4259,8 @@ if ( $INC{"IO/Socket/SSL.pm"} ) {
|
||||
}
|
||||
|
||||
1;
|
||||
}
|
||||
# ###########################################################################
|
||||
# End HTTPMicro package
|
||||
# End HTTP::Micro package
|
||||
# ###########################################################################
|
||||
|
||||
# ###########################################################################
|
||||
@@ -4222,7 +4294,7 @@ use FindBin qw();
|
||||
|
||||
eval {
|
||||
require Percona::Toolkit;
|
||||
require HTTPMicro;
|
||||
require HTTP::Micro;
|
||||
};
|
||||
|
||||
{
|
||||
@@ -4453,7 +4525,7 @@ sub pingback {
|
||||
my $url = $args{url};
|
||||
my $instances = $args{instances};
|
||||
|
||||
my $ua = $args{ua} || HTTPMicro->new( timeout => 3 );
|
||||
my $ua = $args{ua} || HTTP::Micro->new( timeout => 3 );
|
||||
|
||||
my $response = $ua->request('GET', $url);
|
||||
PTDEBUG && _d('Server response:', Dumper($response));
|
||||
@@ -4832,14 +4904,11 @@ sub main {
|
||||
# ########################################################################
|
||||
# If --pid, check it first since we'll die if it already exits.
|
||||
# ########################################################################
|
||||
my $daemon;
|
||||
if ( $o->get('pid') ) {
|
||||
# We're not daemoninzing, it just handles PID stuff. Keep $daemon
|
||||
# in the the scope of main() because when it's destroyed it automatically
|
||||
# removes the PID file.
|
||||
$daemon = new Daemon(o=>$o);
|
||||
$daemon->make_PID_file();
|
||||
}
|
||||
my $daemon = Daemon->new(
|
||||
daemonize => 0,
|
||||
pid_file => $o->get('pid'),
|
||||
);
|
||||
$daemon->run();
|
||||
|
||||
# #######################################################################
|
||||
# Connect to MySQL.
|
||||
|
||||
@@ -18,20 +18,25 @@ require "$trunk/bin/pt-duplicate-key-checker";
|
||||
my $output;
|
||||
my $cnf = "/tmp/12345/my.sandbox.cnf";
|
||||
my $cmd = "$trunk/bin/pt-duplicate-key-checker -F $cnf -h 127.1";
|
||||
my $pid_file = "/tmp/pt-dupe-key-test.pid";
|
||||
|
||||
diag(`rm -f $pid_file >/dev/null`);
|
||||
|
||||
# #########################################################################
|
||||
# Issue 391: Add --pid option to all scripts
|
||||
# #########################################################################
|
||||
`touch /tmp/mk-script.pid`;
|
||||
$output = `$cmd -d issue_295 --pid /tmp/mk-script.pid 2>&1`;
|
||||
|
||||
diag(`touch $pid_file`);
|
||||
|
||||
$output = `$cmd -d issue_295 --pid $pid_file 2>&1`;
|
||||
like(
|
||||
$output,
|
||||
qr{PID file /tmp/mk-script.pid already exists},
|
||||
qr{PID file $pid_file exists},
|
||||
'Dies if PID file already exists (issue 391)'
|
||||
);
|
||||
`rm -rf /tmp/mk-script.pid`;
|
||||
|
||||
# #############################################################################
|
||||
# Done.
|
||||
# #############################################################################
|
||||
diag(`rm -f $pid_file >/dev/null`);
|
||||
exit;
|
||||
|
||||
Reference in New Issue
Block a user