######## suite/innodb/t/innodb-wl6445-1                     ##########
#                                                                    #
# Testcase for worklog WL#6494: InnoDB should be able to work if     #
# redo log file size mismatch
# Test resizing the InnoDB redo log. 
#                                                                    #
#                                                                    #
# Creation:                                                          #
# 2011-09-26 Implemented this test as part of WL#6494                #
#                                                                    #
######################################################################



-- source include/have_innodb_max_16k.inc

if (`SELECT @@innodb_log_file_size = 2097152`) {
  --skip Test requires innodb_log_file_size!=2M.
}

# restart with innodb-fast-shutdown=2 to on stopping it , behave as crash
let $restart_parameters = restart: --innodb-fast-shutdown=2 --innodb-log-file-size=4M;
--source include/restart_mysqld.inc

call mtr.add_suppression("The log sequence numbers [0-9]+ and [0-9]+ in ibdata files do not match the log sequence number [0-9]+ in the ib_logfiles");

let MYSQLD_DATADIR =`SELECT @@datadir`;

SELECT @@innodb_log_file_size;
SELECT @@innodb_log_files_in_group;


# Testcase 1 - set log file size > existing size and innodb_log_files_in_group > old value
--disable_warnings
DROP DATABASE IF EXISTS db_wl6494;
--enable_warnings
CREATE DATABASE db_wl6494;
USE db_wl6494;

CREATE TABLE t1(id INT PRIMARY KEY,bfield blob) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1,repeat('a',2000)),(2,repeat('b',2000)),
(3,repeat('c',2000));
START TRANSACTION;
INSERT INTO t1 VALUES (11,repeat('a',2000)),(12,repeat('b',2000)),
(13,repeat('c',2000));
SAVEPOINT A;
INSERT INTO t1 VALUES (21,repeat('a',2000)),(22,repeat('b',2000)),
(23,repeat('c',2000));
SAVEPOINT B;
SELECT id,LEFT(bfield,20) FROM t1;
ROLLBACK TO A;
COMMIT;
SELECT id,LEFT(bfield,20) FROM t1;

--source include/shutdown_mysqld.inc

# restart with different redo log size
# set log size from 2*4M to 5*4M
--exec echo "restart: --innodb-log-file-size=4M --innodb_log_files_in_group=5 --log-error=$MYSQLTEST_VARDIR/log/case1.err --log-error-verbosity=3" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
--enable_reconnect
--source include/wait_until_connected_again.inc
--disable_reconnect

let SEARCH_FILE= $MYSQLTEST_VARDIR/log/case1.err;
let SEARCH_PATTERN=  Starting crash recovery;
--source include/search_pattern.inc

# Check size of ib_logfile* when innodb_log_files_in_group is set
perl;
 my $dir = $ENV{'MYSQLD_DATADIR'};
 $file = "ib_logfile0";
 $size_in_MB = (-s "$dir/$file") / (1024 * 1024);;
 print "The size of the $file file: $size_in_MB\n";
 $file = "ib_logfile1";
 $size_in_MB = (-s "$dir/$file") / (1024 * 1024);;
 print "The size of the $file file: $size_in_MB\n";
 $file = "ib_logfile2";
 $size_in_MB = (-s "$dir/$file") / (1024 * 1024);;
 print "The size of the $file file: $size_in_MB\n";
 $file = "ib_logfile3";
 $size_in_MB = (-s "$dir/$file") / (1024 * 1024);;
 print "The size of the $file file: $size_in_MB\n";
 $file = "ib_logfile4";
 $size_in_MB = (-s "$dir/$file") / (1024 * 1024);;
 print "The size of the $file file: $size_in_MB\n";
EOF


USE db_wl6494;
SELECT id,LEFT(bfield,20) FROM t1;
--ERROR ER_DUP_ENTRY
INSERT INTO t1 VALUES (1,repeat('a',2000)),(2,repeat('b',2000)),
(3,repeat('c',2000));
INSERT INTO t1 VALUES (21,repeat('a',2000)),(22,repeat('b',2000)),
(23,repeat('c',2000));
SELECT id,LEFT(bfield,20) FROM t1;

# clenaup of test
# restart with different redo log size
DROP TABLE t1;
DROP DATABASE db_wl6494;
# set log size to 3*6M
let $restart_parameters = restart: --innodb-fast-shutdown=2 --innodb-log-file-size=6M --innodb_log_files_in_group=3;
--source include/restart_mysqld.inc

SELECT @@innodb_log_file_size;
SELECT @@innodb_log_files_in_group;

# Testcase 2 - set log file size < existing size and innodb_log_files_in_group < old value
--disable_warnings
DROP DATABASE IF EXISTS db_wl6494;
--enable_warnings
CREATE DATABASE db_wl6494;
USE db_wl6494;

CREATE TABLE t1(id INT PRIMARY KEY,bfield blob) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1,repeat('a',2000)),(2,repeat('b',2000)),
(3,repeat('c',2000));
START TRANSACTION;
INSERT INTO t1 VALUES (11,repeat('a',2000)),(12,repeat('b',2000)),
(13,repeat('c',2000));
SAVEPOINT A;
INSERT INTO t1 VALUES (21,repeat('a',2000)),(22,repeat('b',2000)),
(23,repeat('c',2000));
SAVEPOINT B;
SELECT id,LEFT(bfield,20) FROM t1;
ROLLBACK TO A;
COMMIT;
SELECT id,LEFT(bfield,20) FROM t1;

# restart with different redo log size
--source include/shutdown_mysqld.inc
# set log size from 3*6M to 2*4M
--exec echo "restart: --innodb-log-file-size=4M --innodb_log_files_in_group=2 --log-error=$MYSQLTEST_VARDIR/log/case2.err --log-error-verbosity=3" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
--enable_reconnect
--source include/wait_until_connected_again.inc
--disable_reconnect

SELECT @@innodb_log_file_size;
SELECT @@innodb_log_files_in_group;


let SEARCH_FILE= $MYSQLTEST_VARDIR/log/case2.err;
let SEARCH_PATTERN=  Starting crash recovery;
--source include/search_pattern.inc


USE db_wl6494;
SELECT id,LEFT(bfield,20) FROM t1;
--ERROR ER_DUP_ENTRY
INSERT INTO t1 VALUES (1,repeat('a',2000)),(2,repeat('b',2000)),
(3,repeat('c',2000));
INSERT INTO t1 VALUES (21,repeat('a',2000)),(22,repeat('b',2000)),
(23,repeat('c',2000));
SELECT id,LEFT(bfield,20) FROM t1;

# end of testcase 2.  Do not drop table/database as it is used in foloowing case

# testcase misc
let MYSQLD_DATADIR= `select @@datadir`;
let SEARCH_FILE= $MYSQLTEST_VARDIR/log/my_restart.err;
let $args=--loose-console --core-file > $SEARCH_FILE 2>&1;

# Stop the server
SET GLOBAL innodb_fast_shutdown=0;
--source include/shutdown_mysqld.inc

--echo "test misc 1"
--error 1
--exec $MYSQLD_CMD $args --innodb-log-group-home-dir=foo\;bar
let SEARCH_PATTERN= syntax error in innodb_log_group_home_dir;
--source include/search_pattern.inc
--remove_file $SEARCH_FILE

--echo "test misc 3"
--error 1,42
--exec $MYSQLD_CMD $args --innodb-read-only
let SEARCH_PATTERN=  Cannot resize log files in read-only mode;
--source include/search_pattern.inc
--remove_file $SEARCH_FILE

--echo "test misc 4"
# We should have perfectly synced files here. 
# Rename the log files, and trigger an error in recovery. 
#--move_file $MYSQLD_DATADIR/ib_logfile101 $MYSQLD_DATADIR/ib_logfile0
--move_file $MYSQLD_DATADIR/ib_logfile1 $MYSQLD_DATADIR/ib_logfile1_hidden
--error 1,42
--exec $MYSQLD_CMD $args
let SEARCH_PATTERN=  Only one log file found;
--source include/search_pattern.inc
--remove_file $SEARCH_FILE
#--move_file $MYSQLD_DATADIR/ib_logfile0 $MYSQLD_DATADIR/ib_logfile101
--move_file $MYSQLD_DATADIR/ib_logfile1_hidden $MYSQLD_DATADIR/ib_logfile1

--echo "test misc 5"
# make copy of ib_logfile0 before editing
--move_file $MYSQLD_DATADIR/ib_logfile0 $MYSQLD_DATADIR/ib_logfile0_hidden
perl;
die unless open(FILE, ">$ENV{MYSQLD_DATADIR}/ib_logfile0");
print FILE "garbage";
close(FILE);
EOF
--error 1,42
--exec $MYSQLD_CMD $args
let SEARCH_PATTERN=  .*ib_logfile0 size 7 is not a multiple of innodb_page_size;
--source include/search_pattern.inc
--remove_file $SEARCH_FILE
--remove_file $MYSQLD_DATADIR/ib_logfile0
--move_file $MYSQLD_DATADIR/ib_logfile0_hidden $MYSQLD_DATADIR/ib_logfile0


--echo "test misc 6"
--move_file $MYSQLD_DATADIR/ib_logfile1 $MYSQLD_DATADIR/ib_logfile1_hidden
perl;
die unless open(FILE, ">$ENV{MYSQLD_DATADIR}/ib_logfile1");
print FILE "junkfill" x 131072;
close(FILE);
EOF

--error 1,42
--exec $MYSQLD_CMD $args
let SEARCH_PATTERN=  .*ib_logfile1 is of different size;
--source include/search_pattern.inc
--remove_file $SEARCH_FILE
--remove_file $MYSQLD_DATADIR/ib_logfile1
--move_file $MYSQLD_DATADIR/ib_logfile0 $MYSQLD_DATADIR/ib_logfile0_hidden

# restart server
let $restart_parameters = restart;
--source include/start_mysqld.inc
USE db_wl6494;
SELECT id,LEFT(bfield,20) FROM t1;
--ERROR ER_DUP_ENTRY
INSERT INTO t1 VALUES (1,repeat('a',2000)),(2,repeat('b',2000)),
(3,repeat('c',2000));

# cleanup and set original values
DROP TABLE t1;
DROP DATABASE db_wl6494;
--remove_file $MYSQLTEST_VARDIR/log/case1.err
--remove_file $MYSQLTEST_VARDIR/log/case2.err
--remove_file $MYSQLD_DATADIR/ib_logfile0_hidden
--remove_file $MYSQLD_DATADIR/ib_logfile1_hidden
