File: EventExecutor.pm

package info (click to toggle)
percona-toolkit 3.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 106,720 kB
  • sloc: perl: 257,236; sql: 23,577; sh: 21,388; javascript: 6,322; makefile: 398; python: 62; awk: 38; sed: 1
file content (115 lines) | stat: -rw-r--r-- 3,364 bytes parent folder | download | duplicates (4)
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
# This program is copyright 2013 Percona Ireland Ltd.
# 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.
# ###########################################################################
# EventExecutor package
# ###########################################################################
{
package EventExecutor;

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

use Time::HiRes qw(time);
use Data::Dumper;

use Lmo;

has 'default_database' => (
   is       => 'rw',
   isa      => 'Maybe[Str]',
   required => 0,
);

##
# Private
##

has 'stats' => (
   is       => 'ro',
   isa      => 'HashRef',
   required => 0,
   default  => sub { return {} },
);

sub exec_event {
   my ($self, %args) = @_;
   my @required_args = qw(host event);
   foreach my $arg ( @required_args ) {
      die "I need a $arg argument" unless $args{$arg};
   }
   my $host  = $args{host};
   my $event = $args{event};

   my $results = {
      query_time => undef,
      sth        => undef,
      warnings   => undef,
      error      => undef,
   };

   eval {
      my $db = $event->{db} || $event->{Schema} || $self->default_database;
      if ( $db && (!$host->{current_db} || $host->{current_db} ne $db) ) {
         PTDEBUG && _d('New current db:', $db);
         $host->dbh->do("USE `$db`");
         $host->{current_db} = $db;
      }
      my $sth = $host->dbh->prepare($event->{arg});
      my $t0 = time;
      $sth->execute();
      my $t1 = time - $t0;
      $results->{query_time} = sprintf('%.6f', $t1);
      $results->{sth}        = $sth;
      $results->{warnings}   = $self->get_warnings(dbh => $host->dbh);
   };
   if ( my $e = $EVAL_ERROR ) {
      PTDEBUG && _d($e);
      chomp($e);
      $e =~ s/ at \S+ line \d+, \S+ line \d+\.$//;
      $results->{error} = $e;
   }
   PTDEBUG && _d('Result on', $host->name, Dumper($results));
   return $results;
}

sub get_warnings {
   my ($self, %args) = @_;
   my @required_args = qw(dbh);
   foreach my $arg ( @required_args ) {
      die "I need a $arg argument" unless $args{$arg};
   }
   my $dbh = $args{dbh};
   my $warnings = $dbh->selectall_hashref('SHOW WARNINGS', 'code');
   return $warnings;
}

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";
}

no Lmo;
1;
}
# ###########################################################################
# End EventExecutor package
# ###########################################################################