# This mtr test checks how InnoDB handles corrupted redo log files, # if these files were created using the current redo log format. # --------------------------- # # For older redo formats, recovery reports that redo log isn't clean, # and clean shutdown must be performed using older version of MySQL. # However, scenarios for older redo formats are NOT part of this test. # They are located in log_5_7_8.test, log_5_7_9.test, ... # disabling checkpoints: --source include/have_debug.inc # According to perl's documentation, 'Q' in (un)pack() is supported only if perl # has been compiled with support for 64bit integers. # The log_get_newest_checkpoint(), which is used in this test, uses # log_file_get_checkpoint_lsn(), which uses unpack('Q>'). # The log_file_get_start_lsn(), which is used in this test, uses unpack('Q>'). --source include/have_perl_64bit_int.inc --source ../include/redo_log_error_patterns.inc let $tmp_dir = $MYSQLTEST_VARDIR/tmp; let $saved_datadir = $tmp_dir/saved_datadir; let CORRUPTION_INFO_FILE = $tmp_dir/corruption.inc; let MYSQLD_DATADIR = $tmp_dir/log_corruption; let MYSQLD_ERROR_LOG = $tmp_dir/my_restart.err; let SEARCH_FILE = $MYSQLD_ERROR_LOG; let $INNODB_PAGE_SIZE = `select @@innodb_page_size`; --echo # Initialize new data directory... --let $MYSQLD_EXTRA_ARGS= --innodb_page_size=$INNODB_PAGE_SIZE --source include/initialize_datadir.inc --echo # Restart MySQL using the new data directory... let $restart_parameters = restart: --datadir=$MYSQLD_DATADIR --log-error=$MYSQLD_ERROR_LOG; --replace_result $MYSQLD_ERROR_LOG my_restart.err $MYSQLD_DATADIR tmp/log_corruption --source include/restart_mysqld.inc --echo # Prepare schema used in the tests. --source include/ib_log_spammer_init.inc --echo # Ensure there is a lot of free space in the redo log. SET GLOBAL innodb_log_checkpoint_now = ON; --echo # Disable checkpointing. SET GLOBAL innodb_checkpoint_disabled = ON; --echo # Create connection which generates spam to the redo log. --connect(C1,localhost,root,,test) --send CALL log_spammer() --connection default --echo # Create at least few redo records... CREATE TABLE t (a INT) ENGINE=InnoDB; INSERT INTO t(a) VALUES(42); --echo # Kill MySQL... --source include/kill_mysqld.inc let $restart_parameters = restart:; --echo # Remove unused redo files and prepare saved_datadir... --remove_files_wildcard $MYSQLD_DATADIR/#innodb_redo #ib_redo*_tmp --force-cpdir $MYSQLD_DATADIR $saved_datadir --remove_file $SEARCH_FILE --echo ############################################################################################ --echo # Case 1. Corrupt a mini-transaction after the checkpoint by changing type of redo record. --echo # Start recovery and observe information about the corrupted redo record (invalid --echo # type of redo record, equal to 0). --echo ############################################################################################ # Create an empty file, because if perl script failed to locate mtr to corrupt, # the file would not be created. Still it would need be to included later, and # only then the test executor could discover, that the mtr hasn't been corrupted # (and skip further execution of the test). write_file $CORRUPTION_INFO_FILE; EOF perl; require 'include/ib_redo_log_files.inc'; # Find file with the newest checkpoint (and the related checkpoint_lsn)... my ($max_checkpoint_file, $max_checkpoint_lsn) = log_get_newest_checkpoint(); $max_checkpoint_file ne '' or die "Did not find checkpoint!\n"; # Read start_lsn of the file, to compute offset for checkpoint_lsn... my $file_start_lsn = log_file_get_start_lsn($max_checkpoint_file); # Compute the checkpoint offset... my $checkpoint_offset = log_file_get_offset_for_lsn($file_start_lsn, $max_checkpoint_lsn); # Start at the beginning of the next log block and search for the first block, # which has some mtr starting inside (field first_rec_group != 0). my $block_offset = log_file_get_block_offset($checkpoint_offset) + 512; open my $fh, '+<:raw', $max_checkpoint_file or die "open failed: $!\n"; # Search within ~0.5M - skip the test execution if failed to find the block. for (1..1024) { if ($block_offset >= -s $max_checkpoint_file) { last; } seek $fh, $block_offset + 6, 0; read $fh, my $packed_first_rec_group, 2 or die "read failed: $!\n"; my $first_rec_group = unpack('S>', $packed_first_rec_group); if ($first_rec_group != 0) { # Found log block with mtr starting inside ($first_rec_group bytes # after the block beginning). Save type of the first redo record: seek $fh, $block_offset + $first_rec_group, 0; read $fh, my $packed_corrupted_byte, 1 or die "read failed: $!\n"; my $corrupted_byte = unpack('C', $packed_corrupted_byte); # Overwrite type of the first redo record with 0 (it's invalid type): seek $fh, $block_offset + $first_rec_group, 0; syswrite($fh, pack('C', 0), 1) == 1 or die "write failed: $!\n"; # This is the only way I could find to pass information from this perl # script to the mtr test execution.... my $corrupted_offset = $block_offset + $first_rec_group; my $lsn = log_file_get_lsn_for_offset($file_start_lsn, $corrupted_offset); my $scanned_lsn = log_file_get_lsn_for_offset($file_start_lsn, $block_offset); my $corrupted_block = log_file_get_block_no($lsn); my $file_id = log_file_get_id($max_checkpoint_file); open my $out_fh, '>', $ENV{CORRUPTION_INFO_FILE} or die "open failed: $!\n"; # Be warned, that whatever is printed, should be escaped. # For safety, all printed variables are integers. print $out_fh "let RECOVERED_LSN = $lsn;\n" and print $out_fh "let SCANNED_LSN = $scanned_lsn;\n" and print $out_fh "let CORRUPTED_LOG_FILE_ID = $file_id;\n" and print $out_fh "let CORRUPTED_BLOCK_OFFSET = $block_offset;\n" and print $out_fh "let CORRUPTED_OFFSET = $corrupted_offset;\n" and print $out_fh "let CORRUPTED_REC_TYPE = $corrupted_byte;\n" and print $out_fh "let CORRUPTED_LOG_BLOCK = $corrupted_block;\n" or die "print failed: $!\n"; close $out_fh or die "close failed: $!\n"; last; } $block_offset += 512; } close $fh or die "close failed: $!\n"; EOF let RECOVERED_LSN = 0; --source $CORRUPTION_INFO_FILE if ($RECOVERED_LSN == 0) { --skip Failed to locate mini-transaction to corrupt } --echo # Start MySQL... --error 1,42 --exec $MYSQLD_CMD $MYSQLD_ARGS --innodb_page_size=$INNODB_PAGE_SIZE --$UNKNOWN_PARAM --innodb-log-checksums=OFF --echo # Verify... let SEARCH_PATTERN = $PATTERN_CORRUPT_LOG_RECORD; # Expected: found --source include/search_pattern.inc let SEARCH_PATTERN = Log record type 0, page .*:.*. Log parsing proceeded successfully up to $RECOVERED_LSN; # Expected: found --replace_result $RECOVERED_LSN RECOVERED_LSN --source include/search_pattern.inc let SEARCH_PATTERN = $PATTERN_APPLY_BATCH_COMPLETED; # Expected: not found --source include/search_pattern.inc let SEARCH_PATTERN = $PATTERN_UNKNOWN_PARAM; # Expected: not found --source include/search_pattern.inc --echo ############################################################################################ --echo # Case 2. Verify that recovery could be re-executed from previous checkpoint and go further --echo # after the corrupted log block is fixed. --echo ############################################################################################ --remove_file $SEARCH_FILE # The CORRUPTION_INFO_FILE script has CORRUPTED_LOG_FILE_ID instead of # a full path to the redo log file. This is to avoid troubles in case # the path contained characters that would need to be escaped. let CORRUPTED_LOG_FILE = $MYSQLD_DATADIR/#innodb_redo/#ib_redo$CORRUPTED_LOG_FILE_ID; --echo # Fix the corrupted log record type... perl; open my $fh, "+<:raw", $ENV{CORRUPTED_LOG_FILE} or die "open failed: $!\n"; seek $fh, $ENV{CORRUPTED_OFFSET}, 0; syswrite($fh, pack('C', $ENV{CORRUPTED_REC_TYPE}), 1) == 1 or die "syswrite failed\n"; close $fh or die "close failed: $!\n"; EOF --echo # Start MySQL... --error 1,42 --exec $MYSQLD_CMD $MYSQLD_ARGS --innodb_page_size=$INNODB_PAGE_SIZE --$UNKNOWN_PARAM --echo # Verify... let SEARCH_PATTERN = $PATTERN_DATABASE_NOT_CLEAN; # Expected: found --source include/search_pattern.inc let SEARCH_PATTERN = $PATTERN_STARTING_CRASH_RECOVERY; # Expected: found --source include/search_pattern.inc let SEARCH_PATTERN = $PATTERN_APPLYING_BATCH; # Expected: found --source include/search_pattern.inc let SEARCH_PATTERN = $PATTERN_APPLY_BATCH_COMPLETED; # Expected: found --source include/search_pattern.inc let SEARCH_PATTERN = $PATTERN_UNKNOWN_PARAM; # Expected: found --source include/search_pattern.inc --echo # Remove the data directory... --force-rmdir $MYSQLD_DATADIR --echo ############################################################################################ --echo # Case 3. Check how recovery stops on log block with invalid checksum. --echo ############################################################################################ --remove_file $SEARCH_FILE --echo # Restore the data directory from before recovery... --force-cpdir $saved_datadir $MYSQLD_DATADIR --echo # Corrupt checksum... perl; open my $fh, "+<:raw", $ENV{CORRUPTED_LOG_FILE} or die "open $ENV{CORRUPTED_LOG_FILE} failed: $!\n"; my $offset = $ENV{CORRUPTED_BLOCK_OFFSET} + 508; seek $fh, $offset, 0; read $fh, my $packed_checksum, 4 or die "read failed: $!\n"; seek $fh, $offset, 0; syswrite($fh, pack('L>', unpack('L>', $packed_checksum) ^ 1), 4) == 4 or die "write failed: $!\n"; close $fh or die "close failed: $!\n"; EOF --echo # Start MySQL... --error 3,42 --exec $MYSQLD_CMD $MYSQLD_ARGS --innodb_page_size=$INNODB_PAGE_SIZE --$UNKNOWN_PARAM --innodb-log-checksums=ON --innodb_force_recovery_crash=3 --echo # Verify... let SEARCH_PATTERN = Log block $CORRUPTED_LOG_BLOCK at lsn $SCANNED_LSN has valid header, but checksum field contains .*, should be .*; # Expected: found --replace_result $SCANNED_LSN SCANNED_LSN $CORRUPTED_LOG_BLOCK CORRUPTED_LOG_BLOCK --source include/search_pattern.inc let SEARCH_PATTERN = $PATTERN_RECOVERY_UP_TO $SCANNED_LSN; # Expected: found --replace_result $SCANNED_LSN SCANNED_LSN --source include/search_pattern.inc let SEARCH_PATTERN = $PATTERN_UNKNOWN_PARAM; # Expected: not found --source include/search_pattern.inc --force-rmdir $MYSQLD_DATADIR --echo ############################################################################################ --echo # Case 4. Check how recovery stops on log block with invalid block_no. --echo ############################################################################################ --remove_file $SEARCH_FILE --echo # Restore the data directory from before recovery... --force-cpdir $saved_datadir $MYSQLD_DATADIR --echo # Corrupt the block_no... perl; open my $fh, "+<:raw", $ENV{CORRUPTED_LOG_FILE} or die "open failed: $!\n"; seek $fh, $ENV{CORRUPTED_BLOCK_OFFSET}, 0; read $fh, my $packed_block_no, 4 or die "read failed: $!\n"; my $block_no = unpack('L>', $packed_block_no) + 42; seek $fh, $ENV{CORRUPTED_BLOCK_OFFSET}, 0; syswrite($fh, pack('L>', $block_no), 4) == 4 or die "write failed: $!\n"; close $fh or die "close failed: $!\n"; EOF --echo # Start MySQL... --error 3,42 --exec $MYSQLD_CMD $MYSQLD_ARGS --innodb_page_size=$INNODB_PAGE_SIZE --$UNKNOWN_PARAM --innodb-log-checksums=OFF --innodb_force_recovery_crash=3 --echo # Verify... let SEARCH_PATTERN = $PATTERN_DATABASE_NOT_CLEAN; # Expected: found --source include/search_pattern.inc let SEARCH_PATTERN = $PATTERN_STARTING_CRASH_RECOVERY; # Expected: found --source include/search_pattern.inc let SEARCH_PATTERN = $PATTERN_RECOVERY_UP_TO $SCANNED_LSN; # Expected: found --replace_result $SCANNED_LSN SCANNED_LSN --source include/search_pattern.inc let SEARCH_PATTERN = $PATTERN_APPLYING_BATCH; # Expected: found --source include/search_pattern.inc let SEARCH_PATTERN = $PATTERN_APPLY_BATCH_COMPLETED; # Expected: found --source include/search_pattern.inc let SEARCH_PATTERN = $PATTERN_UNKNOWN_PARAM; # Expected: not found --source include/search_pattern.inc --force-rmdir $MYSQLD_DATADIR --echo ############################################################################################ --echo # Cleanup... --force-rmdir $saved_datadir --remove_file $CORRUPTION_INFO_FILE --remove_file $SEARCH_FILE let SEARCH_FILE=; let SEARCH_PATTERN=; let MYSQLD_ERROR_LOG=; let MYSQLD_DATADIR=; let RECOVERED_LSN=; let SCANNED_LSN=; let CORRUPTED_LOG_FILE_ID=; let CORRUPTED_LOG_FILE=; let CORRUPTED_BLOCK_OFFSET=; let CORRUPTED_OFFSET=; let CORRUPTED_REC_TYPE=; let CORRUPTED_LOG_BLOCK=; let CORRUPTION_INFO_FILE=; --echo # Start MySQL... --source include/start_mysqld.inc