Try to dynamically change option, restart if it fails

parent 8ba9c0af
# -*- cperl -*-
# Copyright (C) 2004-2006 MySQL AB
#
# 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
package My::Options;
#
# Utility functions to work with list of options
#
use strict;
sub same($$) {
my $l1= shift;
my $l2= shift;
return compare($l1,$l2) == 0;
}
sub compare ($$) {
my $l1= shift;
my $l2= shift;
my @l1= @$l1;
my @l2= @$l2;
return -1 if @l1 < @l2;
return 1 if @l1 > @l2;
while ( @l1 ) # Same length
{
my $e1= shift @l1;
my $e2= shift @l2;
my $cmp= ($e1 cmp $e2);
return $cmp if $cmp != 0;
}
return 0; # They are the same
}
sub _split_option {
my ($option)= @_;
if ($option=~ /^--(.*)=(.*)$/){
return ($1, $2);
}
elsif ($option=~ /^--(.*)$/){
return ($1, undef)
}
elsif ($option=~ /^(.*)=(.*)$/){
return ($1, $2)
}
elsif ($option=~ /^-O$/){
return (undef, undef);
}
die "Unknown option format '$option'";
}
sub _build_option {
my ($name, $value)= @_;
if ($name =~ /^O, /){
return "-".$name."=".$value;
}
elsif ($value){
return "--".$name."=".$value;
}
return "--".$name;
}
#
# Compare two list of options and return what would need
# to be done to get the server running with the new settings
#
sub diff {
my ($from_opts, $to_opts)= @_;
my %from;
foreach my $from (@$from_opts)
{
my ($opt, $value)= _split_option($from);
next unless defined($opt);
$from{$opt}= $value;
}
#print "from: ", %from, "\n";
my %to;
foreach my $to (@$to_opts)
{
my ($opt, $value)= _split_option($to);
next unless defined($opt);
$to{$opt}= $value;
}
#print "to: ", %to, "\n";
# Remove the ones that are in both lists
foreach my $name (keys %from){
if (exists $to{$name} and $to{$name} eq $from{$name}){
#print "removing '$name' from both lists\n";
delete $to{$name};
delete $from{$name};
}
}
#print "from: ", %from, "\n";
#print "to: ", %to, "\n";
# Add all keys in "to" to result
my @result;
foreach my $name (keys %to){
push(@result, _build_option($name, $to{$name}));
}
# Add all keys in "from" that are not in "to"
# to result as "set to default"
foreach my $name (keys %from){
if (not exists $to{$name}) {
push(@result, _build_option($name, "default"));
}
}
return @result;
}
sub is_set {
my ($opts, $set_opts)= @_;
foreach my $opt (@$opts){
my ($opt_name1, $value1)= _split_option($opt);
foreach my $set_opt (@$set_opts){
my ($opt_name2, $value2)= _split_option($set_opt);
if ($opt_name1 eq $opt_name2){
# Option already set
return 1;
}
}
}
return 0;
}
sub toSQL {
my (@options)= @_;
my @sql;
foreach my $option (@options) {
my ($name, $value)= _split_option($option);
#print "name: $name\n";
if ($name =~ /^O, (.*)/){
push(@sql, "SET GLOBAL $1=$value");
} else {
my $sql_name= $name;
$sql_name=~ s/-/_/g;
push(@sql, "SET GLOBAL $sql_name=$value");
}
}
return join("; ", @sql);
}
sub toStr {
my $name= shift;
return "$name: ",
"['", join("', '", @_), "']\n";
}
1;
......@@ -31,7 +31,7 @@ our $skip_rpl;
our $do_test;
our $skip_test;
our $opt_skip_combination;
our $binlog_format;;
our $binlog_format;
our $enable_disabled;
our $default_storage_engine;
our $opt_with_ndbcluster_only;
......@@ -83,8 +83,9 @@ sub init_pattern {
#
##############################################################################
sub collect_test_cases ($) {
sub collect_test_cases ($$) {
my $suites= shift; # Semicolon separated list of test suites
my $opt_cases= shift;
my $cases= []; # Array of hash(one hash for each testcase)
$do_test_reg= init_pattern($do_test, "--do-test");
......@@ -92,15 +93,15 @@ sub collect_test_cases ($) {
foreach my $suite (split(",", $suites))
{
push(@$cases, collect_one_suite($suite));
push(@$cases, collect_one_suite($suite, $opt_cases));
}
if ( @::opt_cases )
if ( @$opt_cases )
{
# A list of tests was specified on the command line
# Check that the tests specified was found
# in at least one suite
foreach my $test_name_spec ( @::opt_cases )
foreach my $test_name_spec ( @$opt_cases )
{
my $found= 0;
my ($sname, $tname, $extension)= split_testname($test_name_spec);
......@@ -236,6 +237,7 @@ sub split_testname {
sub collect_one_suite($)
{
my $suite= shift; # Test suite name
my $opt_cases= shift;
my @cases; # Array of hash
mtr_verbose("Collecting: $suite");
......@@ -304,10 +306,10 @@ sub collect_one_suite($)
$suite_opts= opts_from_file($suite_opt_file);
}
if ( @::opt_cases )
if ( @$opt_cases )
{
# Collect in specified order
foreach my $test_name_spec ( @::opt_cases )
foreach my $test_name_spec ( @$opt_cases )
{
my ($sname, $tname, $extension)= split_testname($test_name_spec);
......@@ -428,9 +430,15 @@ sub collect_one_suite($)
{
foreach my $test (@cases)
{
#print $test->{name}, " ", $comb, "\n";
my $new_test= {};
# Skip this combination if the values it provides
# already are set in master_opt or slave_opt
if (My::Options::is_set($test->{master_opt}, $comb->{comb_opt}) &&
My::Options::is_set($test->{slave_opt}, $comb->{comb_opt}) ){
next;
}
# Copy test options
my $new_test= {};
while (my ($key, $value) = each(%$test)) {
if (ref $value eq "ARRAY") {
push(@{$new_test->{$key}}, @$value);
......@@ -450,6 +458,18 @@ sub collect_one_suite($)
push(@new_cases, $new_test);
}
}
# Add the plain test if it was not already added
# as part of a combination
my %added;
foreach my $new_test (@new_cases){
$added{$new_test->{name}}= 1;
}
foreach my $test (@cases){
push(@new_cases, $test) unless $added{$test->{name}};
}
#print_testcases(@new_cases);
@cases= @new_cases;
#print_testcases(@cases);
......@@ -481,6 +501,7 @@ sub optimize_cases {
# --mysqld=--binlog-format=x, skip all test that does not
# support it
# =======================================================
#print "binlog_format: $binlog_format\n";
if (defined $binlog_format )
{
# =======================================================
......@@ -488,6 +509,8 @@ sub optimize_cases {
# =======================================================
if ( defined $tinfo->{'binlog_formats'} )
{
#print "binlog_formats: ". join(", ", @{$tinfo->{binlog_formats}})."\n";
# The test supports different binlog formats
# check if the selected one is ok
my $supported=
......@@ -513,23 +536,18 @@ sub optimize_cases {
mtr_match_prefix($opt, "--binlog-format=") || $test_binlog_format;
}
if (defined $test_binlog_format)
if (defined $test_binlog_format and
defined $tinfo->{binlog_formats} )
{
if ( defined $tinfo->{binlog_formats} )
my $supported=
grep { $_ eq $test_binlog_format } @{$tinfo->{'binlog_formats'}};
if ( !$supported )
{
my $supported=
grep { $_ eq $test_binlog_format } @{$tinfo->{'binlog_formats'}};
if ( !$supported )
{
$tinfo->{'skip'}= 1;
$tinfo->{'comment'}=
"Doesn't support --binlog-format='$test_binlog_format'";
next;
}
$tinfo->{'skip'}= 1;
$tinfo->{'comment'}=
"Doesn't support --binlog-format='$test_binlog_format'";
next;
}
# Save binlog format for dynamic switching
$tinfo->{binlog_format_switch}= $test_binlog_format;
}
}
}
......@@ -882,6 +900,12 @@ sub collect_one_test_case {
$tinfo->{extra_template_path}= $defaults_extra_file;
}
# ----------------------------------------------------------------------
# Append mysqld extra options to both master and slave
# ----------------------------------------------------------------------
push(@{$tinfo->{'master_opt'}}, @::opt_extra_mysqld_opt);
push(@{$tinfo->{'slave_opt'}}, @::opt_extra_mysqld_opt);
return $tinfo;
}
......
......@@ -29,8 +29,6 @@ sub mtr_script_exists(@);
sub mtr_file_exists(@);
sub mtr_exe_exists(@);
sub mtr_exe_maybe_exists(@);
sub mtr_same_opts($$);
sub mtr_cmp_opts($$);
##############################################################################
#
......@@ -189,35 +187,6 @@ sub mtr_exe_exists (@) {
}
sub mtr_same_opts ($$) {
my $l1= shift;
my $l2= shift;
return mtr_cmp_opts($l1,$l2) == 0;
}
sub mtr_cmp_opts ($$) {
my $l1= shift;
my $l2= shift;
my @l1= @$l1;
my @l2= @$l2;
return -1 if @l1 < @l2;
return 1 if @l1 > @l2;
while ( @l1 ) # Same length
{
my $e1= shift @l1;
my $e2= shift @l2;
my $cmp= ($e1 cmp $e2);
return $cmp if $cmp != 0;
}
return 0; # They are the same
}
sub mtr_milli_sleep {
die "usage: mtr_milli_sleep(milliseconds)" unless @_ == 1;
my ($millis)= @_;
......@@ -225,33 +194,4 @@ sub mtr_milli_sleep {
select(undef, undef, undef, ($millis/1000));
}
#
# Compare two arrays and put all unequal elements into a new one
#
sub mtr_diff_opts ($$) {
my $l1= shift;
my $l2= shift;
my $found;
my @result;
foreach my $e1 (@$l1)
{
$found= undef;
foreach my $e2 (@$l2)
{
$found= 1 unless ($e1 ne $e2);
}
push(@result, $e1) unless (defined $found);
}
foreach my $e2 (@$l2)
{
$found= undef;
foreach my $e1 (@$l1)
{
$found= 1 unless ($e1 ne $e2);
}
push(@result, $e2) unless (defined $found);
}
return @result;
}
1;
......@@ -68,7 +68,9 @@ sub mtr_report_test_skipped ($) {
if ( $tinfo->{skip_detected_by_test} )
{
mtr_report("[ skip.] $tinfo->{'comment'}");
} else {
}
else
{
mtr_report("[ skip ] $tinfo->{'comment'}");
}
}
......
# -*- cperl -*-
use Test::More qw(no_plan);
use strict;
use_ok("My::Options");
my @tests=
(
[
['--binlog-format=row', '--loose-skip-innodb', '--binlog-format=ms'],
['--binlog-format=row', '--loose-skip-innodb', '--binlog-format=statement'],
['--binlog-format=statement']
],
[
['--binlog-format=row', '--loose-skip-innodb', '--binlog-format=statement'],
['--binlog-format=row', '--loose-skip-innodb', '--binlog-format=mixed'],
['--binlog-format=mixed']
],
[
['--binlog-format=row', '--loose-skip-innodb', '--binlog-format=mixed'],
['--binlog-format=row', '--loose-skip-innodb', '--binlog-format=statement'],
['--binlog-format=statement']
],
[
['--binlog-format=mixed', '--loose-skip-innodb', '--binlog-format=row'],
['--binlog-format=statement', '--loose-skip-innodb', '--binlog-format=row'],
[ ]
],
[
['--binlog-format=row'],
[ ],
['--binlog-format=default']
],
[
[ ],
['--binlog-format=row'],
['--binlog-format=row']
],
[
[ ],
['-O', 'max_binlog_size=1' ],
['--max_binlog_size=1' ]
],
[
['-O', 'max_binlog_size=1' ],
['-O', 'max_binlog_size=1' ],
[ ],
],
[
['-O', 'max_binlog_size=1' ],
[ ],
['--max_binlog_size=default' ]
],
[
[ ],
['-O', 'max_binlog_size=1', '--binlog-format=row' ],
['--max_binlog_size=1', '--binlog-format=row' ]
],
[
['--binlog-format=statement' ],
['-O', 'max_binlog_size=1', '--binlog-format=row' ],
['--max_binlog_size=1', '--binlog-format=row']
],
[
[ '--binlog-format=statement' ],
['-O', 'max_binlog_size=1', '--binlog-format=statement' ],
['--max_binlog_size=1' ]
],
[
[ '--binlog-format=statement' ],
['-O', 'max_binlog_size=1', '--binlog-format=statement' ],
['--max_binlog_size=1' ]
],
[
[ '--binlog-format=statement' ],
['--relay-log=/path/to/a/relay-log', '--binlog-format=row'],
['--relay-log=/path/to/a/relay-log', '--binlog-format=row' ]
],
[
[ '--binlog-format=statement' ],
['--relay-log=/path/to/a/relay-log', '-O', 'max_binlog_size=1'],
['--max_binlog_size=1', '--relay-log=/path/to/a/relay-log', '--binlog-format=default' ]
],
);
my $test_no= 0;
foreach my $test (@tests){
print "test", $test_no++, "\n";
foreach my $opts (@$test){
print My::Options::toStr("", @$opts);
}
my $from= $test->[0];
my $to= $test->[1];
my @result= My::Options::diff($from, $to);
ok(My::Options::same(\@result, $test->[2]));
if (!My::Options::same(\@result, $test->[2])){
print "failed\n";
print My::Options::toStr("result", @result);
print My::Options::toStr("expect", @{$test->[2]});
}
print My::Options::toSQL(@result), "\n";
print "\n";
}
......@@ -48,6 +48,7 @@ use File::Copy;
use File::Temp qw / tempdir /;
use My::SafeProcess;
use My::ConfigFactory;
use My::Options;
use mtr_cases;
our $is_win32_perl= ($^O eq "MSWin32"); # ActiveState Win32 Perl
......@@ -230,7 +231,7 @@ sub main {
# Figure out which tests we are going to run
mtr_report("Collecting tests...");
my $tests= collect_test_cases($opt_suites);
my $tests= collect_test_cases($opt_suites, \@opt_cases);
initialize_servers();
......@@ -2075,28 +2076,29 @@ sub run_testcase_check_skip_test($)
}
sub dynamic_binlog_format_switch {
my ($tinfo, $mysqld)= @_;
sub run_query {
my ($tinfo, $mysqld, $query)= @_;
my $sql= "include/set_binlog_format_".$tinfo->{binlog_format_switch}.".sql";
my $args;
mtr_init_args(\$args);
mtr_add_arg($args, "--defaults-file=%s", $path_config_file);
mtr_add_arg($args, "--defaults-group-suffix=%s", $mysqld->after('mysqld'));
mtr_add_arg($args, "-e %s", $query);
my $res= My::SafeProcess->run
(
name => "switch binlog format ".$mysqld->name(),
name => "run_query -> ".$mysqld->name(),
path => $exe_mysql,
args => \$args,
input => $sql,
output => '/dev/null',
error => '/dev/null'
);
if ($res != 0)
{
mtr_error("Failed to switch binlog format");
}
return $res
}
sub do_before_run_mysqltest($)
{
my $tinfo= shift;
......@@ -2570,7 +2572,7 @@ sub mysqld_stop {
sub mysqld_arguments ($$$) {
my $args= shift;
my $mysqld= shift;
my $extra_opt= shift;
my $extra_opts= shift;
my $prefix= ""; # If mysqltest server arg
if ( $opt_embedded_server )
......@@ -2585,7 +2587,7 @@ sub mysqld_arguments ($$$) {
# to start unless we specify what user to run as, see BUG#30630
my $euid= $>;
if (!$is_win32 and $euid == 0 and
(grep(/^--user/, @$extra_opt, @opt_extra_mysqld_opt)) == 0) {
(grep(/^--user/, @$extra_opts)) == 0) {
mtr_add_arg($args, "%s--user=root", $prefix);
}
......@@ -2606,8 +2608,7 @@ sub mysqld_arguments ($$$) {
}
# Check if "extra_opt" contains skip-log-bin
my $skip_binlog= grep(/^(--|--loose-)skip-log-bin/,
@$extra_opt, @opt_extra_mysqld_opt);
my $skip_binlog= grep(/^(--|--loose-)skip-log-bin/, @$extra_opts);
if ( $opt_debug )
{
......@@ -2622,7 +2623,7 @@ sub mysqld_arguments ($$$) {
}
my $found_skip_core= 0;
foreach my $arg ( @opt_extra_mysqld_opt, @$extra_opt )
foreach my $arg ( @$extra_opts )
{
# Allow --skip-core-file to be set in <testname>-[master|slave].opt file
if ($arg eq "--skip-core-file")
......@@ -2651,9 +2652,12 @@ sub mysqld_arguments ($$$) {
}
sub mysqld_start ($$) {
my $mysqld= shift;
my $extra_opt= shift;
my $extra_opts= shift;
mtr_verbose(My::Options::toStr("mysqld_start", @$extra_opts));
my $exe= $exe_mysqld;
my $wait_for_pid_file= 1;
......@@ -2669,7 +2673,7 @@ sub mysqld_start ($$) {
valgrind_arguments($args, \$exe);
}
mysqld_arguments($args,$mysqld,$extra_opt);
mysqld_arguments($args,$mysqld,$extra_opts);
if ( $opt_gdb || $opt_manual_gdb )
{
......@@ -2738,7 +2742,7 @@ sub mysqld_start ($$) {
}
# Remember options used when starting
$mysqld->{'started_opts'}= $extra_opt;
$mysqld->{'started_opts'}= $extra_opts;
return;
}
......@@ -2758,7 +2762,6 @@ sub stop_all_servers () {
}
# Find out if server should be restarted for this test
sub server_need_restart {
my ($tinfo, $server)= @_;
......@@ -2811,35 +2814,34 @@ sub server_need_restart {
}
}
my $is_mysqld= grep ($server eq $_, mysqlds());
my $is_mysqld= grep ($server eq $_, mysqlds());
if ($is_mysqld)
{
# Check that running process was started with same options
# as the current test requires
my $extra_opt= get_extra_opt($server, $tinfo);
my $extra_opts= get_extra_opts($server, $tinfo);
my $started_opts= $server->{'started_opts'};
if (defined $started_opts and $extra_opt and
! mtr_same_opts($started_opts, $extra_opt) )
if (!My::Options::same($started_opts, $extra_opts) )
{
# TODO Use a list to find all options that can be set dynamically
# Check if diff is binlog format only
# and the next test has $binlog_format_switch set
my @diff_opts= mtr_diff_opts($started_opts, $extra_opt);
if (@diff_opts == 2 and
$diff_opts[0] =~/^--binlog-format=/ and
$diff_opts[1] =~/^--binlog-format=/ and
defined $tinfo->{binlog_format_switch})
{
mtr_verbose("Using dynamic switch of binlog format from ",
$diff_opts[0],"to", $diff_opts[1]);
dynamic_binlog_format_switch($tinfo, $server);
mtr_verbose(My::Options::toStr("started_opts", @$started_opts));
mtr_verbose(My::Options::toStr("extra_opts", @$extra_opts));
# Get diff and check if dynamic switch is possible
my @diff_opts= My::Options::diff($started_opts, $extra_opts);
mtr_verbose(My::Options::toStr("diff_opts", @diff_opts));
my $query= My::Options::toSQL(@diff_opts);
mtr_verbose("Attempting dynamic switch '$query'");
if (run_query($tinfo, $server, $query)){
mtr_verbose("Restart: Dynamic switch failed");
return 1;
}
else
{
mtr_verbose("Restart: running with different options '" .
join(" ", @{$extra_opt}) . "' != '" .
join(" ", @{$extra_opts}) . "' != '" .
join(" ", @{$server->{'started_opts'}}) . "'" );
return 1;
}
......@@ -2890,7 +2892,7 @@ sub started { return grep(defined $_, map($_->{proc}, @_)); }
sub stopped { return grep(!defined $_, map($_->{proc}, @_)); }
sub get_extra_opt {
sub get_extra_opts {
my ($mysqld, $tinfo)= @_;
return
......@@ -3026,8 +3028,8 @@ sub start_servers($) {
return 1;
}
my $extra_opt= get_extra_opt($mysqld, $tinfo);
mysqld_start($mysqld,$extra_opt);
my $extra_opts= get_extra_opts($mysqld, $tinfo);
mysqld_start($mysqld,$extra_opts);
# Save this test case information, so next can examine it
$mysqld->{'started_tinfo'}= $tinfo;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment