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
#####################################################################
20
# Author: Jorgen Loland
23
# Purpose: Implementation of WL#4218: Test that transactions executed
24
# concurrently with backup are either completely restored or not
25
# restored at all. No transaction should be partially represented
28
# See further documentation in invariant.yy
31
# mysql-test/gentest/conf/backup/invariant.yy
32
# mysql-test/gentest/conf/backup/invariant.zz
33
# mysql-test/gentest/lib/GenTest/Validator/Invariant.pm
34
# mysql-test/gentest/lib/GenTest/Reporter/BackupAndRestoreInvariant.pm
36
#####################################################################
37
package GenTest::Validator::Invariant;
40
@ISA = qw(GenTest::Validator GenTest);
46
use GenTest::Constants;
48
use GenTest::Validator;
52
my $inconsistent_state = 0;
58
my ($validator, $executors, $results) = @_;
59
my $dsn = $executors->[0]->dsn();
61
# RQG does not stop the script if an error occurs.
62
# The following code preserves transactional consistency by
63
# explicitly rolling back the rest of the statements in a
64
# transaction that has failed.
65
foreach my $i (0..$#$results) {
67
# Additional rules where the transaction must rollback for the
68
# invariant script to work
70
# "ERROR 1048 (23000): Column 'x' cannot be null"
71
# '-> used to check that @val!=null (see the delete_update
72
# rule in invariant.yy)
74
if ($results->[$i]->err() == 1048) {
75
# say("Found transaction that failed due to insert of NULL value");
76
$inconsistent_state=1;
79
if ($results->[$i]->status() == STATUS_TRANSACTION_ERROR) {
80
#say("entering inconsistent state due to query".$results->[$i]->query());
81
$inconsistent_state = 1;
82
} elsif ($results->[$i]->query() =~ m{^\s*(COMMIT|START TRANSACTION|BEGIN)}sio) {
83
if ($inconsistent_state > 0) {
84
#say("leaving inconsistent state due to query ".$results->[$i]->query());
86
$inconsistent_state = 0;
89
if ($inconsistent_state == 1) {
90
#say("$$ Rollback ".$results->[$i]->query());
91
$executors->[$i]->dbh()->do("ROLLBACK /* Explicit ROLLBACK after a ".$results->[$i]->errstr()." error. */ ");
93
#say("$$ Execute: ".$results->[$i]->query()." Affected rows: ".$results->[0]->affectedRows());
96
# End transactional consistency code
98
# Start code that checks that the total amount of money across all
99
# bank accounts has not changed
100
$dbh = DBI->connect($dsn) if not defined $dbh;
101
$tables = $dbh->selectcol_arrayref("SHOW TABLES") if not defined $tables;
103
my $query="SELECT SUM(alltbls.int_not_null) as sum,
104
COUNT(alltbls.int_not_null) as count
108
foreach my $table (@$tables) {
110
$query = $query . " UNION ALL ";
112
$query = $query . "SELECT int_not_null FROM " . $table;
115
$query = $query . ") AS alltbls";
117
#say("The query: ".$query);
118
my ($sum, $count) = $dbh->selectrow_array($query);
120
if (($sum eq '') && ($count eq '')) {
121
# Server probably crashed, the SELECT returned no data
122
return STATUS_UNKNOWN_ERROR;
125
if (($sum ne '100000')) {
126
say("Invariant: Bad sum for tables: sum: $sum; count: $count; affected_rows: ".$results->[0]->affectedRows()."; query: ".$results->[0]->query());
127
return STATUS_DATABASE_CORRUPTION;