~ubuntu-branches/ubuntu/saucy/drizzle/saucy

« back to all changes in this revision

Viewing changes to tests/kewpie/randgen/lib/GenTest/Reporter/QueryTimeout.pm

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.2.11) (2.1.16 sid)
  • Revision ID: package-import@ubuntu.com-20120619104649-9ij634mxm4x8pp4l
Tags: 1:7.1.36-stable-1ubuntu1
* Merge from Debian unstable. (LP: #987575)
  Remaining changes:
  - Added upstart script.
* debian/drizzle.upstart: dropped logger since upstart logs job
  output now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2008,2011 Oracle and/or its affiliates. All rights reserved.
 
2
# Use is subject to license terms.
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; version 2 of the License.
 
7
#
 
8
# This program is distributed in the hope that it will be useful, but
 
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
11
# General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 
16
# USA
 
17
 
 
18
package GenTest::Reporter::QueryTimeout;
 
19
 
 
20
require Exporter;
 
21
@ISA = qw(GenTest::Reporter);
 
22
 
 
23
use strict;
 
24
use DBI;
 
25
use GenTest;
 
26
use GenTest::Constants;
 
27
use GenTest::Reporter;
 
28
 
 
29
use constant PROCESSLIST_CONNECTION_ID          => 0;
 
30
use constant PROCESSLIST_PROCESS_TIME           => 5;
 
31
use constant PROCESSLIST_PROCESS_STATE          => 6;
 
32
use constant PROCESSLIST_PROCESS_INFO           => 7;
 
33
 
 
34
# Default minimum lifetime for a query before it is killed
 
35
use constant DEFAULT_QUERY_LIFETIME_THRESHOLD   => 20;  # Seconds
 
36
 
 
37
# The query lifetime threshold is configurable via properties.
 
38
# We check this once and store the value (or the default) in this variable.
 
39
my $q_l_t;
 
40
 
 
41
sub monitor {
 
42
        my $reporter = shift;
 
43
 
 
44
        my $dsn = $reporter->dsn();
 
45
        my $dbh = DBI->connect($dsn);
 
46
 
 
47
        if (defined GenTest::Executor::MySQL::errorType($DBI::err)) {
 
48
                return GenTest::Executor::MySQL::errorType($DBI::err);
 
49
        } elsif (not defined $dbh) {
 
50
                return STATUS_UNKNOWN_ERROR;
 
51
        }
 
52
 
 
53
        my $processlist = $dbh->selectall_arrayref("SHOW FULL PROCESSLIST");
 
54
 
 
55
        if (not defined $q_l_t) {
 
56
                # We only check the querytimeout option the first time the reporter runs
 
57
                $q_l_t = DEFAULT_QUERY_LIFETIME_THRESHOLD;
 
58
                $q_l_t = $reporter->properties->querytimeout 
 
59
                        if defined $reporter->properties->querytimeout;
 
60
                say("QueryTimeout Reporter will use query timeout threshold of $q_l_t seconds");
 
61
        }
 
62
 
 
63
        foreach my $process (@$processlist) {
 
64
                if ($process->[PROCESSLIST_PROCESS_INFO] ne '') {
 
65
                        if ($process->[PROCESSLIST_PROCESS_TIME] > $q_l_t + 100) {
 
66
                                # Query survived QUERY_LIFETIME + 100 seconds.
 
67
                                # If QUERY_LIFETIME_THRESHOLD is 20, and reporter interval is
 
68
                                # 10 seconds, this means query survived more than 120 seconds and
 
69
                                # 10 attempted KILL QUERY attempts. This looks like a mysqld issue.
 
70
                                # Hence, we now try killing the whole thread instead.
 
71
                                say("Query: ".$process->[PROCESSLIST_PROCESS_INFO]." took more than ".($q_l_t + 100). " seconds ($q_l_t + 100). Killing thread.");
 
72
                                $dbh->do("KILL ".$process->[PROCESSLIST_CONNECTION_ID]);
 
73
                        }elsif ($process->[PROCESSLIST_PROCESS_TIME] > $q_l_t) {
 
74
                                say("Query: ".$process->[PROCESSLIST_PROCESS_INFO]." is taking more than ".($q_l_t). " seconds. Killing query.");
 
75
                                $dbh->do("KILL QUERY ".$process->[PROCESSLIST_CONNECTION_ID]);
 
76
                        }
 
77
                }
 
78
        }
 
79
 
 
80
        return STATUS_OK;
 
81
}
 
82
 
 
83
sub type {
 
84
        return REPORTER_TYPE_PERIODIC;
 
85
}
 
86
 
 
87
1;