~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to tests/randgen/lib/GenTest/Validator/Invariant.pm

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.1.6)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20120619104649-e2l0ggd4oz3um0f4
Tags: upstream-7.1.36-stable
ImportĀ upstreamĀ versionĀ 7.1.36-stable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008-2009 Sun Microsystems, Inc. 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
#####################################################################
 
19
#
 
20
# Author: Jorgen Loland
 
21
# Date: April 2009
 
22
#
 
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
 
26
# after restore.
 
27
#
 
28
# See further documentation in invariant.yy
 
29
#
 
30
# Associated files: 
 
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
 
35
#
 
36
#####################################################################
 
37
package GenTest::Validator::Invariant;
 
38
 
 
39
require Exporter;
 
40
@ISA = qw(GenTest::Validator GenTest);
 
41
 
 
42
use strict;
 
43
 
 
44
use DBI;
 
45
use GenTest;
 
46
use GenTest::Constants;
 
47
use GenTest::Result;
 
48
use GenTest::Validator;
 
49
 
 
50
my $tables;
 
51
my $dbh;
 
52
my $inconsistent_state = 0;
 
53
 
 
54
my $total_sum;
 
55
my $total_count;
 
56
 
 
57
sub validate {
 
58
  my ($validator, $executors, $results) = @_;
 
59
  my $dsn = $executors->[0]->dsn();
 
60
 
 
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) {
 
66
 
 
67
    # Additional rules where the transaction must rollback for the
 
68
    # invariant script to work
 
69
    #
 
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)
 
73
 
 
74
    if ($results->[$i]->err() == 1048) {
 
75
      # say("Found transaction that failed due to insert of NULL value");
 
76
      $inconsistent_state=1;
 
77
    }
 
78
 
 
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());
 
85
      }
 
86
      $inconsistent_state = 0;
 
87
    }
 
88
 
 
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. */ ");
 
92
    } else {
 
93
      #say("$$ Execute: ".$results->[$i]->query()." Affected rows: ".$results->[0]->affectedRows());
 
94
    }
 
95
  }
 
96
  # End transactional consistency code
 
97
 
 
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;
 
102
 
 
103
  my $query="SELECT SUM(alltbls.int_not_null) as sum, 
 
104
                    COUNT(alltbls.int_not_null) as count 
 
105
             FROM (";
 
106
 
 
107
  my $tblno=0;
 
108
  foreach my $table (@$tables) {
 
109
    if ($tblno>0) {
 
110
      $query = $query . " UNION ALL ";
 
111
    }
 
112
    $query = $query . "SELECT int_not_null FROM " . $table;
 
113
    $tblno++;
 
114
  }
 
115
  $query = $query . ") AS alltbls";
 
116
 
 
117
  #say("The query: ".$query);
 
118
  my ($sum, $count) = $dbh->selectrow_array($query);
 
119
 
 
120
  if (($sum eq '') && ($count eq '')) {
 
121
    # Server probably crashed, the SELECT returned no data
 
122
    return STATUS_UNKNOWN_ERROR;
 
123
  }
 
124
 
 
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;
 
128
  }
 
129
 
 
130
  return STATUS_OK;
 
131
}
 
132
 
 
133
1;