# Test resizing the InnoDB redo log.

--source include/have_innodb.inc
--source include/have_debug.inc

# Valgrind would complain about memory leaks when we crash on purpose.
--source include/not_valgrind.inc
# Embedded server does not support crashing
--source include/not_embedded.inc
# Avoid CrashReporter popup on Mac
--source include/not_crashrep.inc

if (`SELECT @@innodb_log_file_size = 1048576`) {
  --skip Test requires innodb_log_file_size>1M.
}

call mtr.add_suppression("InnoDB: Resizing redo log");
call mtr.add_suppression("InnoDB: Starting to delete and rewrite log files");
call mtr.add_suppression("InnoDB: New log files created");
call mtr.add_suppression("InnoDB: 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");

CREATE TABLE t1(a INT PRIMARY KEY) ENGINE=InnoDB;

SET DEBUG='+d,crash_commit_before';
# Write file to make mysql-test-run.pl restart the server
--exec echo "restart: --innodb-log-file-size=6M" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
--error 2013
INSERT INTO t1 VALUES (42);

--enable_reconnect
--source include/wait_until_connected_again.inc
--disable_reconnect

SELECT * FROM t1;

INSERT INTO t1 VALUES (42);

SET DEBUG='+d,crash_commit_before';
# Write file to make mysql-test-run.pl restart the server
--exec echo "restart: --innodb-log-files-in-group=3 --innodb-log-file-size=5M" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
--error 2013
DELETE FROM t1;

--enable_reconnect
--source include/wait_until_connected_again.inc
--disable_reconnect

SELECT * FROM t1;

INSERT INTO t1 VALUES (123);

let MYSQLD_DATADIR= `select @@datadir`;
let SEARCH_FILE= $MYSQLTEST_VARDIR/log/my_restart.err;
let $args=--loose-console > $SEARCH_FILE 2>&1;
let crash=$args --innodb-force-recovery-crash;

SET DEBUG='+d,crash_commit_before';
# Write file to make mysql-test-run.pl restart the server
--exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
--error 2013
DELETE FROM t1;

--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_in_file.inc
--remove_file $SEARCH_FILE

--error 1
--exec $MYSQLD_CMD $args --innodb-mirrored-log-groups=2
let SEARCH_PATTERN= innodb_mirrored_log_groups is an unimplemented feature and the variable will be completely removed in a future version. Using values other than 1 is not supported.;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE

# On Windows, DBUG_SUICIDE() does exit(3) instead of SIGKILL (128+9).
--echo --innodb-force-recovery-crash=1
--error 3
--exec $MYSQLD_CMD $crash=1
let SEARCH_PATTERN= InnoDB: Starting an apply batch of log records;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE
--echo --innodb-force-recovery-crash=2
--error 3
--exec $MYSQLD_CMD $crash=2
let SEARCH_PATTERN= InnoDB: Starting an apply batch of log records;
--source include/search_pattern_in_file.inc
--echo --innodb-force-recovery-crash=3
--error 3
--exec $MYSQLD_CMD $crash=3
let SEARCH_PATTERN= InnoDB: Starting an apply batch of log records;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE

--error 1
--exec $MYSQLD_CMD $args --innodb-read-only
let SEARCH_PATTERN= InnoDB: Recovery skipped, --innodb-read-only set!;
--source include/search_pattern_in_file.inc
let SEARCH_PATTERN= InnoDB: Can.t initiate database recovery, running in read-only-mode;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE

--echo --innodb-force-recovery-crash=4
--error 3
--exec $MYSQLD_CMD $crash=4
let SEARCH_PATTERN= InnoDB: Starting an apply batch of log records;
--source include/search_pattern_in_file.inc
let SEARCH_PATTERN= InnoDB: Resizing redo log from 3\*[0-9]+ to 2\*[0-9]+ pages;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE
--echo --innodb-force-recovery-crash=5
--error 3
--exec $MYSQLD_CMD $crash=5
let SEARCH_PATTERN= InnoDB: Starting an apply batch of log records;
--source include/search_pattern_in_file.inc
let SEARCH_PATTERN= InnoDB: Resizing redo log from 3\*[0-9]+ to 2\*[0-9]+ pages;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE

--error 1
--exec $MYSQLD_CMD $args --innodb-read-only
let SEARCH_PATTERN= InnoDB: Can.t initiate database recovery, running in read-only-mode;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE

--echo --innodb-force-recovery-crash=6
--error 3
--exec $MYSQLD_CMD $crash=6
let SEARCH_PATTERN= InnoDB: Starting an apply batch of log records;
--source include/search_pattern_in_file.inc
let SEARCH_PATTERN= InnoDB: Resizing redo log from 3\*[0-9]+ to 2\*[0-9]+ pages;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE
--echo --innodb-force-recovery-crash=7
--error 3
--exec $MYSQLD_CMD $crash=7
# this crashes right after deleting all log files
--remove_file $SEARCH_FILE

--error 1
--exec $MYSQLD_CMD $args --innodb-read-only
let SEARCH_PATTERN= InnoDB: Cannot create log files in read-only mode;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE

--echo --innodb-force-recovery-crash=8
--error 3
--exec $MYSQLD_CMD $crash=8
let SEARCH_PATTERN= InnoDB: Setting log file .*ib_logfile[0-9]+ size to;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE
--echo --innodb-force-recovery-crash=9
--error 3
--exec $MYSQLD_CMD $crash=9
let SEARCH_PATTERN= InnoDB: Setting log file .*ib_logfile[0-9]+ size to;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE

# 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
--exec $MYSQLD_CMD $args
let SEARCH_PATTERN= InnoDB: Only one log file found;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE
--move_file $MYSQLD_DATADIR/ib_logfile0 $MYSQLD_DATADIR/ib_logfile101

perl;
die unless open(FILE, ">$ENV{MYSQLD_DATADIR}/ib_logfile0");
print FILE "garbage";
close(FILE);
EOF
--error 1
--exec $MYSQLD_CMD $args
let SEARCH_PATTERN= InnoDB: Log file .*ib_logfile0 size 7 is not a multiple of innodb_page_size;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE
--remove_file $MYSQLD_DATADIR/ib_logfile0
--move_file $MYSQLD_DATADIR/ib_logfile101 $MYSQLD_DATADIR/ib_logfile0

perl;
die unless open(FILE, ">$ENV{MYSQLD_DATADIR}/ib_logfile1");
print FILE "junkfill" x 131072;
close(FILE);
EOF

--error 1
--exec $MYSQLD_CMD $args
let SEARCH_PATTERN= InnoDB: Log file .*ib_logfile1 is of different size 1048576 bytes than other log files;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE
--remove_file $MYSQLD_DATADIR/ib_logfile1
--move_file $MYSQLD_DATADIR/ib_logfile0 $MYSQLD_DATADIR/ib_logfile101
--move_file $MYSQLD_DATADIR/ib_logfile1_hidden $MYSQLD_DATADIR/ib_logfile1

--echo --innodb-force-recovery-crash=10
--error 3
--exec $MYSQLD_CMD $crash=10
let SEARCH_PATTERN= InnoDB: Setting log file .*ib_logfile[0-9]+ size to;
--source include/search_pattern_in_file.inc
let SEARCH_PATTERN= InnoDB: Renaming log file .*ib_logfile101 to .*ib_logfile0;
--source include/search_pattern_in_file.inc
--remove_file $SEARCH_FILE

--exec echo "restart" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect

--enable_reconnect
--source include/wait_until_connected_again.inc
--disable_reconnect

SELECT * FROM t1;
DROP TABLE t1;
