~elenst/randgen/rocksdb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/perl

# Copyright (c) 2008, 2011 Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2014, SkySQL Ab.
# Use is subject to license terms.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA

use strict;
use lib 'lib';
use lib "$ENV{RQG_HOME}/lib";
#use List::Util 'shuffle';
use Cwd;
use File::Path 'remove_tree';
use GenTest;
use GenTest::Constants;
use Getopt::Long;
use File::Basename;

Getopt::Long::Configure("pass_through");

if (defined $ENV{RQG_HOME}) {
	if (osWindows()) {
		$ENV{RQG_HOME} = $ENV{RQG_HOME}.'\\';
	} else {
		$ENV{RQG_HOME} = $ENV{RQG_HOME}.'/';
	}
} else {
	$ENV{RQG_HOME} = dirname(Cwd::abs_path($0));
}

if ( osWindows() )
{
	require Win32::API;
	my $errfunc = Win32::API->new('kernel32', 'SetErrorMode', 'I', 'I');
	my $initial_mode = $errfunc->Call(2);
	$errfunc->Call($initial_mode | 2);
};

$| = 1;
my $ctrl_c = 0;
	
$SIG{INT} = sub { $ctrl_c = 1 };
$SIG{TERM} = sub { exit(0) };
$SIG{CHLD} = "IGNORE" if osWindows();

my ($vardir, $vardir1, $vardir2, $trials, $force, $old, $exit_status, @exit_status);

my $max_result = 0;

my $opt_result = GetOptions(
	'vardir=s' => \$vardir,
	'vardir1=s' => \$vardir1,
	'vardir2=s' => \$vardir2,
	'trials=i' => \$trials,
	'force' => \$force,
	'old' => \$old,
	'exit_status=s' => \$exit_status,
	'exit-status=s' => \$exit_status,
);

$trials = 1 unless defined $trials;

push @ARGV, "--vardir=$vardir" if defined $vardir;
push @ARGV, "--vardir1=$vardir1" if defined $vardir1;
push @ARGV, "--vardir2=$vardir2" if defined $vardir2;

my $comb_str = join(' ', @ARGV);		

if ($exit_status) {
	@exit_status = split(',', $exit_status);
}

foreach my $trial_id (1..$trials) {

	say("##########################################################");
	say("Running trial ".$trial_id."/".$trials);
	my $runall = $old?"runall.pl":"runall-new.pl";

	my $command = "perl ".
		(defined $ENV{RQG_HOME} ? $ENV{RQG_HOME}."/" : "" ).
		"$runall $comb_str ";

	$command =~ s{[\t\r\n]}{ }sgio;
	$command =~ s{"}{\\"}sgio;

	unless (osWindows())
	{
		$command = 'bash -c "set -o pipefail; '.$command.'"';
	}

	say("$command");

	my $result = system($command) >> 8;
	my $result_name = status2text($result);
	say("Trial $trial_id ended with exit status $result_name ($result)");
	unless (check_for_desired_status($result_name)) {
		say("Exit status $result_name is not on the list of desired status codes ($exit_status), it will be ignored");
		next;
	}
	exit($result) if not defined $force;

	$max_result = $result if $result > $max_result;

	if ($result > 0) {
		# Storing vardirs for the failure
		foreach my $v ($vardir,$vardir1,$vardir2) {
			next unless defined $v;
			# Remove trailing slashes
			$v =~ s/[\/\\]+$//;
			my $from = $v;
			my $to = $v.'_trial'.$trial_id;
			say("Copying $from to $to");
			remove_tree($to) if (-e $to);
			remove_tree($to.'_slave') if (-e $to.'_slave');
			if (osWindows()) {
				system("xcopy \"$from\" \"$to\" /E /I /Q");
				system("xcopy \"$from"."_slave\" \"$to"."_slave\" /E /I /Q") if -e $from.'_slave';
				open(OUT, ">$to/command");
				print OUT $command;
				close(OUT);
			} elsif ($command =~ m{--mem}) {
				system("cp -r /dev/shm/var $to");
				open(OUT, ">$to/command");
				print OUT $command;
				close(OUT);
			} else {
				system("cp -r $from $to");
				system("cp -r $from"."_slave $to"."_slave") if -e $from.'_slave';
				open(OUT, ">$to/command");
				print OUT $command;
				close(OUT);
			}
		}
	}
}

say("$0 will exit with exit status ".status2text($max_result)."($max_result)");
exit($max_result);

sub check_for_desired_status {
	my $resname = shift;
	if (!$exit_status) {
		# No desired codes, anything except for STATUS_OIK will do
		return $resname ne 'STATUS_OK';
	}
	else {
		foreach (@exit_status) {
			return 1 if $resname eq $_;
		}
		return 0;
	}
}