1
# Copyright (C) 2008-2009 Sun Microsystems, Inc. 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
# We need to revisit the grammar we use / how we validate consistency
19
# if we need to keep using this reporter
21
package GenTest::Reporter::DrizzleRecoveryConsistency;
24
@ISA = qw(GenTest::Reporter);
29
use GenTest::Constants;
30
use GenTest::Reporter;
34
my $dbh = DBI->connect($reporter->dsn(), undef, undef, {PrintError => 0});
36
if (time() > $reporter->testEnd() - 20) {
37
say("Sending shutdown() call to server.");
38
$dbh->selectrow_array('SELECT shutdown()');
40
return STATUS_SERVER_KILLED;
49
my $dbh = DBI->connect($reporter->dsn(), undef, undef, {PrintError => 0});
50
my $basedir = $reporter->serverVariable('basedir');
52
my $binary = $basedir.'/drizzled/drizzled' ;
54
if (-e $basedir.'/var')
56
$datadir = $basedir.'/var/';
60
$datadir = $basedir.'tests/var/master-data';
62
$datadir =~ s{[\\/]$}{}sgio;
63
my $recovery_datadir = $datadir.'_recovery';
64
my $port = $reporter->serverVariable('mysql_protocol_port');
66
say("Sending shutdown() call to server.");
67
$dbh->selectrow_array('SELECT shutdown()');
71
system("cp -r $datadir $recovery_datadir");
73
say("Attempting database recovery...");
75
my @drizzled_options = (
78
'--datadir="'.$recovery_datadir.'"',
79
'--basedir="'.$basedir.'"',
80
'--plugin-add=shutdown_function',
81
'--mysql-protocol.port='.$port,
85
my $drizzled_command = $binary.' '.join(' ', @drizzled_options).' 2>&1';
86
say("Executing $drizzled_command .");
88
open(DRIZZLED, "$drizzled_command|");
89
my $recovery_status = STATUS_OK;
91
$_ =~ s{[\r\n]}{}siog;
93
if ($_ =~ m{exception}sio) {
94
$recovery_status = STATUS_DATABASE_CORRUPTION;
95
} elsif ($_ =~ m{ready for connections}sio) {
96
say("Server Recovery was apparently successfull.") if $recovery_status == STATUS_OK ;
98
} elsif ($_ =~ m{got signal}sio) {
99
$recovery_status = STATUS_DATABASE_CORRUPTION;
104
say("Checking database consistency...");
105
my $dbh = DBI->connect($reporter->dsn());
107
my $tables = $dbh->selectcol_arrayref("SHOW TABLES");
109
foreach my $table (@$tables) {
110
my $average = $dbh->selectrow_array("
111
SELECT (SUM(`col_int_key`) + SUM(`col_int`)) / COUNT(*)
115
if ($average ne '200.0000') {
116
say("Bad average on table: $table; average: $average");
117
$recovery_status = STATUS_DATABASE_CORRUPTION;
119
say("Average is $average");
123
say("Shutting down the recovered server...");
125
if (not defined $dbh) {
126
$recovery_status = STATUS_DATABASE_CORRUPTION;
128
say("Sending shutdown() call to server.");
129
$dbh->selectrow_array('SELECT shutdown()');
134
if ($recovery_status > STATUS_OK) {
135
say("Recovery has failed.");
138
return $recovery_status;
142
return REPORTER_TYPE_CRASH | REPORTER_TYPE_DEADLOCK | REPORTER_TYPE_SUCCESS | REPORTER_TYPE_PERIODIC | REPORTER_TYPE_SERVER_KILLED;