1
# Copyright (c) 2008,2011 Oracle and/or its affiliates. All rights reserved.
2
# Use is subject to license terms.
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.
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.
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
18
package GenTest::Reporter::QueryTimeout;
21
@ISA = qw(GenTest::Reporter);
26
use GenTest::Constants;
27
use GenTest::Reporter;
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;
34
# Default minimum lifetime for a query before it is killed
35
use constant DEFAULT_QUERY_LIFETIME_THRESHOLD => 20; # Seconds
37
# The query lifetime threshold is configurable via properties.
38
# We check this once and store the value (or the default) in this variable.
44
my $dsn = $reporter->dsn();
45
my $dbh = DBI->connect($dsn);
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;
53
my $processlist = $dbh->selectall_arrayref("SHOW FULL PROCESSLIST");
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");
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]);
84
return REPORTER_TYPE_PERIODIC;