~percona-toolkit-dev/percona-toolkit/fix-password-comma-bug-886077

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# This program is copyright 2011 Percona Inc.
# Feedback and improvements are welcome.
#
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, version 2; OR the Perl Artistic License.  On UNIX and similar
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
# licenses.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307  USA.
# ###########################################################################
# CopyRowsInsertSelect package
# ###########################################################################
{
# Package: CopyRowsInsertSelect
# CopyRowsInsertSelect implements the copy rows phase of an online schema
# change.
package CopyRowsInsertSelect;

use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use constant PTDEBUG => $ENV{PTDEBUG} || 0;

# Sub: new
#
# Parameters:
#   %args - Arguments
#
# Returns:
#   CopyRowsInsertSelect object
sub new {
   my ( $class, %args ) = @_;
   my @required_args = qw(Retry);
   foreach my $arg ( @required_args ) {
      die "I need a $arg argument" unless $args{$arg};
   }

   my $self = {
      %args,
   };

   return bless $self, $class;
}

sub copy {
   my ( $self, %args ) = @_;
   my @required_args = qw(dbh msg from_table to_table chunks columns);
   foreach my $arg ( @required_args ) {
      die "I need a $arg argument" unless $args{$arg};
   }
   my ($dbh, $msg, $from_table, $to_table, $chunks) = @args{@required_args};
   my $pr       = $args{Progress};
   my $sleep    = $args{sleep};
   my $columns  = join(', ', @{$args{columns}});
   my $n_chunks = @$chunks - 1;

   for my $chunkno ( 0..$n_chunks ) {
      if ( !$chunks->[$chunkno] ) {
         warn "Chunk number ", ($chunkno + 1), "is undefined";
         next;
      }

      my $sql = "INSERT IGNORE INTO $to_table ($columns) "
              . "SELECT $columns FROM $from_table "
              . "WHERE ($chunks->[$chunkno])"
              . ($args{where}        ? " AND ($args{where})"  : "")
              . ($args{engine_flags} ? " $args{engine_flags}" : "");

      # Most times we always msg($sql), but there may be a lot of chunks
      # so only do this if we're printing (i.e. not executing).
      if ( $args{print} ) {
         $msg->($sql);
      }
      else {
         PTDEBUG && _d($dbh, $sql);
         my $error;
         $self->{Retry}->retry(
            wait  => sub { sleep 1; },
            tries => 3,
            try   => sub {
               $dbh->do($sql);
               return;
            },
            fail => sub {
               my (%args) = @_;
               my $error = $args{error};
               PTDEBUG && _d($error);
               if ( $error =~ m/Lock wait timeout exceeded/ ) {
                  $msg->("Lock wait timeout exceeded; retrying $sql");
                  return 1; # call wait, call try
               }
               return 0; # call final_fail
            },
            final_fail => sub {
               my (%args) = @_;
               die $args{error};
            },
         );
      }

      # Update Progress (if there is one) with the chunkno just finished.
      $pr->update(sub { return $chunkno + 1; }) if $pr;

      # Sleep if there's a callback and this isn't the last chunk.
      $sleep->($chunkno + 1) if $sleep && $chunkno < $n_chunks;
   }

   return;
}

sub cleanup {
   my ( $self, %args ) = @_;
   # Nothing to cleanup, but caller is still going to call us.
   return;
}

sub _d {
   my ($package, undef, $line) = caller 0;
   @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
        map { defined $_ ? $_ : 'undef' }
        @_;
   print STDERR "# $package:$line $PID ", join(' ', @_), "\n";
}

1;
}
# ###########################################################################
# End CopyRowsInsertSelect package
# ###########################################################################