Debian Codegraph @

mysql-test/suite/innodb/t/log_corruption.test

raw
1 # This mtr test checks how InnoDB handles corrupted redo log files,
2 # if these files were created using the current redo log format.
3 # ---------------------------
4 #
5 # For older redo formats, recovery reports that redo log isn't clean,
6 # and clean shutdown must be performed using older version of MySQL.
7 # However, scenarios for older redo formats are NOT part of this test.
8 # They are located in log_5_7_8.test, log_5_7_9.test, ...
9
10 # disabling checkpoints:
11 --source include/have_debug.inc
12
13 # According to perl's documentation, 'Q' in (un)pack() is supported only if perl
14 # has been compiled with support for 64bit integers.
15 # The log_get_newest_checkpoint(), which is used in this test, uses
16 # log_file_get_checkpoint_lsn(), which uses unpack('Q>').
17 # The log_file_get_start_lsn(), which is used in this test, uses unpack('Q>').
18 --source include/have_perl_64bit_int.inc
19
20 --source ../include/redo_log_error_patterns.inc
21
22 let $tmp_dir = $MYSQLTEST_VARDIR/tmp;
23 let $saved_datadir = $tmp_dir/saved_datadir;
24
25 let CORRUPTION_INFO_FILE = $tmp_dir/corruption.inc;
26 let MYSQLD_DATADIR = $tmp_dir/log_corruption;
27 let MYSQLD_ERROR_LOG = $tmp_dir/my_restart.err;
28 let SEARCH_FILE = $MYSQLD_ERROR_LOG;
29 let $INNODB_PAGE_SIZE = `select @@innodb_page_size`;
30
31 --echo # Initialize new data directory...
32 --let $MYSQLD_EXTRA_ARGS= --innodb_page_size=$INNODB_PAGE_SIZE
33 --source include/initialize_datadir.inc
34
35 --echo # Restart MySQL using the new data directory...
36 let $restart_parameters = restart: --datadir=$MYSQLD_DATADIR --log-error=$MYSQLD_ERROR_LOG;
37 --replace_result $MYSQLD_ERROR_LOG my_restart.err $MYSQLD_DATADIR tmp/log_corruption
38 --source include/restart_mysqld.inc
39
40 --echo # Prepare schema used in the tests.
41 --source include/ib_log_spammer_init.inc
42
43 --echo # Ensure there is a lot of free space in the redo log.
44 SET GLOBAL innodb_log_checkpoint_now = ON;
45
46 --echo # Disable checkpointing.
47 SET GLOBAL innodb_checkpoint_disabled = ON;
48
49 --echo # Create connection which generates spam to the redo log.
50 --connect(C1,localhost,root,,test)
51 --send CALL log_spammer()
52 --connection default
53
54 --echo # Create at least few redo records...
55 CREATE TABLE t (a INT) ENGINE=InnoDB;
56 INSERT INTO t(a) VALUES(42);
57
58 --echo # Kill MySQL...
59 --source include/kill_mysqld.inc
60 let $restart_parameters = restart:;
61
62 --echo # Remove unused redo files and prepare saved_datadir...
63 --remove_files_wildcard $MYSQLD_DATADIR/#innodb_redo #ib_redo*_tmp
64
65 --force-cpdir $MYSQLD_DATADIR $saved_datadir
66
67 --remove_file $SEARCH_FILE
68
69 --echo ############################################################################################
70 --echo # Case 1. Corrupt a mini-transaction after the checkpoint by changing type of redo record.
71 --echo # Start recovery and observe information about the corrupted redo record (invalid
72 --echo # type of redo record, equal to 0).
73 --echo ############################################################################################
74
75 # Create an empty file, because if perl script failed to locate mtr to corrupt,
76 # the file would not be created. Still it would need be to included later, and
77 # only then the test executor could discover, that the mtr hasn't been corrupted
78 # (and skip further execution of the test).
79
80 write_file $CORRUPTION_INFO_FILE;
81 EOF
82
83 perl;
84 require 'include/ib_redo_log_files.inc';
85
86 # Find file with the newest checkpoint (and the related checkpoint_lsn)...
87 my ($max_checkpoint_file, $max_checkpoint_lsn) = log_get_newest_checkpoint();
88 $max_checkpoint_file ne '' or die "Did not find checkpoint!\n";
89
90 # Read start_lsn of the file, to compute offset for checkpoint_lsn...
91 my $file_start_lsn = log_file_get_start_lsn($max_checkpoint_file);
92
93 # Compute the checkpoint offset...
94 my $checkpoint_offset = log_file_get_offset_for_lsn($file_start_lsn, $max_checkpoint_lsn);
95
96 # Start at the beginning of the next log block and search for the first block,
97 # which has some mtr starting inside (field first_rec_group != 0).
98 my $block_offset = log_file_get_block_offset($checkpoint_offset) + 512;
99
100 open my $fh, '+<:raw', $max_checkpoint_file or die "open failed: $!\n";
101 # Search within ~0.5M - skip the test execution if failed to find the block.
102 for (1..1024) {
103 if ($block_offset >= -s $max_checkpoint_file) {
104 last;
105 }
106 seek $fh, $block_offset + 6, 0;
107 read $fh, my $packed_first_rec_group, 2 or die "read failed: $!\n";
108 my $first_rec_group = unpack('S>', $packed_first_rec_group);
109 if ($first_rec_group != 0) {
110 # Found log block with mtr starting inside ($first_rec_group bytes
111 # after the block beginning). Save type of the first redo record:
112 seek $fh, $block_offset + $first_rec_group, 0;
113 read $fh, my $packed_corrupted_byte, 1 or die "read failed: $!\n";
114 my $corrupted_byte = unpack('C', $packed_corrupted_byte);
115 # Overwrite type of the first redo record with 0 (it's invalid type):
116 seek $fh, $block_offset + $first_rec_group, 0;
117 syswrite($fh, pack('C', 0), 1) == 1 or die "write failed: $!\n";
118
119 # This is the only way I could find to pass information from this perl
120 # script to the mtr test execution....
121 my $corrupted_offset = $block_offset + $first_rec_group;
122 my $lsn = log_file_get_lsn_for_offset($file_start_lsn, $corrupted_offset);
123 my $scanned_lsn = log_file_get_lsn_for_offset($file_start_lsn, $block_offset);
124 my $corrupted_block = log_file_get_block_no($lsn);
125 my $file_id = log_file_get_id($max_checkpoint_file);
126 open my $out_fh, '>', $ENV{CORRUPTION_INFO_FILE} or die "open failed: $!\n";
127 # Be warned, that whatever is printed, should be escaped.
128 # For safety, all printed variables are integers.
129 print $out_fh "let RECOVERED_LSN = $lsn;\n"
130 and print $out_fh "let SCANNED_LSN = $scanned_lsn;\n"
131 and print $out_fh "let CORRUPTED_LOG_FILE_ID = $file_id;\n"
132 and print $out_fh "let CORRUPTED_BLOCK_OFFSET = $block_offset;\n"
133 and print $out_fh "let CORRUPTED_OFFSET = $corrupted_offset;\n"
134 and print $out_fh "let CORRUPTED_REC_TYPE = $corrupted_byte;\n"
135 and print $out_fh "let CORRUPTED_LOG_BLOCK = $corrupted_block;\n"
136 or die "print failed: $!\n";
137 close $out_fh or die "close failed: $!\n";
138 last;
139 }
140 $block_offset += 512;
141 }
142 close $fh or die "close failed: $!\n";
143 EOF
144
145 let RECOVERED_LSN = 0;
146 --source $CORRUPTION_INFO_FILE
147 if ($RECOVERED_LSN == 0) {
148 --skip Failed to locate mini-transaction to corrupt
149 }
150
151 --echo # Start MySQL...
152 --error 1,42
153 --exec $MYSQLD_CMD $MYSQLD_ARGS --innodb_page_size=$INNODB_PAGE_SIZE --$UNKNOWN_PARAM --innodb-log-checksums=OFF
154
155 --echo # Verify...
156
157 let SEARCH_PATTERN = $PATTERN_CORRUPT_LOG_RECORD;
158 # Expected: found
159 --source include/search_pattern.inc
160
161 let SEARCH_PATTERN = Log record type 0, page .*:.*. Log parsing proceeded successfully up to $RECOVERED_LSN;
162 # Expected: found
163 --replace_result $RECOVERED_LSN RECOVERED_LSN
164 --source include/search_pattern.inc
165
166 let SEARCH_PATTERN = $PATTERN_APPLY_BATCH_COMPLETED;
167 # Expected: not found
168 --source include/search_pattern.inc
169
170 let SEARCH_PATTERN = $PATTERN_UNKNOWN_PARAM;
171 # Expected: not found
172 --source include/search_pattern.inc
173
174 --echo ############################################################################################
175 --echo # Case 2. Verify that recovery could be re-executed from previous checkpoint and go further
176 --echo # after the corrupted log block is fixed.
177 --echo ############################################################################################
178
179 --remove_file $SEARCH_FILE
180
181 # The CORRUPTION_INFO_FILE script has CORRUPTED_LOG_FILE_ID instead of
182 # a full path to the redo log file. This is to avoid troubles in case
183 # the path contained characters that would need to be escaped.
184
185 let CORRUPTED_LOG_FILE = $MYSQLD_DATADIR/#innodb_redo/#ib_redo$CORRUPTED_LOG_FILE_ID;
186
187 --echo # Fix the corrupted log record type...
188 perl;
189 open my $fh, "+<:raw", $ENV{CORRUPTED_LOG_FILE} or die "open failed: $!\n";
190 seek $fh, $ENV{CORRUPTED_OFFSET}, 0;
191 syswrite($fh, pack('C', $ENV{CORRUPTED_REC_TYPE}), 1) == 1 or die "syswrite failed\n";
192 close $fh or die "close failed: $!\n";
193 EOF
194
195 --echo # Start MySQL...
196 --error 1,42
197 --exec $MYSQLD_CMD $MYSQLD_ARGS --innodb_page_size=$INNODB_PAGE_SIZE --$UNKNOWN_PARAM
198
199 --echo # Verify...
200
201 let SEARCH_PATTERN = $PATTERN_DATABASE_NOT_CLEAN;
202 # Expected: found
203 --source include/search_pattern.inc
204
205 let SEARCH_PATTERN = $PATTERN_STARTING_CRASH_RECOVERY;
206 # Expected: found
207 --source include/search_pattern.inc
208
209 let SEARCH_PATTERN = $PATTERN_APPLYING_BATCH;
210 # Expected: found
211 --source include/search_pattern.inc
212
213 let SEARCH_PATTERN = $PATTERN_APPLY_BATCH_COMPLETED;
214 # Expected: found
215 --source include/search_pattern.inc
216
217 let SEARCH_PATTERN = $PATTERN_UNKNOWN_PARAM;
218 # Expected: found
219 --source include/search_pattern.inc
220
221 --echo # Remove the data directory...
222 --force-rmdir $MYSQLD_DATADIR
223
224 --echo ############################################################################################
225 --echo # Case 3. Check how recovery stops on log block with invalid checksum.
226 --echo ############################################################################################
227
228 --remove_file $SEARCH_FILE
229
230 --echo # Restore the data directory from before recovery...
231 --force-cpdir $saved_datadir $MYSQLD_DATADIR
232
233 --echo # Corrupt checksum...
234 perl;
235 open my $fh, "+<:raw", $ENV{CORRUPTED_LOG_FILE} or die "open $ENV{CORRUPTED_LOG_FILE} failed: $!\n";
236 my $offset = $ENV{CORRUPTED_BLOCK_OFFSET} + 508;
237 seek $fh, $offset, 0;
238 read $fh, my $packed_checksum, 4 or die "read failed: $!\n";
239 seek $fh, $offset, 0;
240 syswrite($fh, pack('L>', unpack('L>', $packed_checksum) ^ 1), 4) == 4 or die "write failed: $!\n";
241 close $fh or die "close failed: $!\n";
242 EOF
243
244 --echo # Start MySQL...
245 --error 3,42
246 --exec $MYSQLD_CMD $MYSQLD_ARGS --innodb_page_size=$INNODB_PAGE_SIZE --$UNKNOWN_PARAM --innodb-log-checksums=ON --innodb_force_recovery_crash=3
247
248 --echo # Verify...
249 let SEARCH_PATTERN = Log block $CORRUPTED_LOG_BLOCK at lsn $SCANNED_LSN has valid header, but checksum field contains .*, should be .*;
250 # Expected: found
251 --replace_result $SCANNED_LSN SCANNED_LSN $CORRUPTED_LOG_BLOCK CORRUPTED_LOG_BLOCK
252 --source include/search_pattern.inc
253
254 let SEARCH_PATTERN = $PATTERN_RECOVERY_UP_TO $SCANNED_LSN;
255 # Expected: found
256 --replace_result $SCANNED_LSN SCANNED_LSN
257 --source include/search_pattern.inc
258
259 let SEARCH_PATTERN = $PATTERN_UNKNOWN_PARAM;
260 # Expected: not found
261 --source include/search_pattern.inc
262
263 --force-rmdir $MYSQLD_DATADIR
264
265 --echo ############################################################################################
266 --echo # Case 4. Check how recovery stops on log block with invalid block_no.
267 --echo ############################################################################################
268
269 --remove_file $SEARCH_FILE
270
271 --echo # Restore the data directory from before recovery...
272 --force-cpdir $saved_datadir $MYSQLD_DATADIR
273
274 --echo # Corrupt the block_no...
275 perl;
276 open my $fh, "+<:raw", $ENV{CORRUPTED_LOG_FILE} or die "open failed: $!\n";
277 seek $fh, $ENV{CORRUPTED_BLOCK_OFFSET}, 0;
278 read $fh, my $packed_block_no, 4 or die "read failed: $!\n";
279 my $block_no = unpack('L>', $packed_block_no) + 42;
280 seek $fh, $ENV{CORRUPTED_BLOCK_OFFSET}, 0;
281 syswrite($fh, pack('L>', $block_no), 4) == 4 or die "write failed: $!\n";
282 close $fh or die "close failed: $!\n";
283 EOF
284
285 --echo # Start MySQL...
286 --error 3,42
287 --exec $MYSQLD_CMD $MYSQLD_ARGS --innodb_page_size=$INNODB_PAGE_SIZE --$UNKNOWN_PARAM --innodb-log-checksums=OFF --innodb_force_recovery_crash=3
288
289 --echo # Verify...
290
291 let SEARCH_PATTERN = $PATTERN_DATABASE_NOT_CLEAN;
292 # Expected: found
293 --source include/search_pattern.inc
294
295 let SEARCH_PATTERN = $PATTERN_STARTING_CRASH_RECOVERY;
296 # Expected: found
297 --source include/search_pattern.inc
298
299 let SEARCH_PATTERN = $PATTERN_RECOVERY_UP_TO $SCANNED_LSN;
300 # Expected: found
301 --replace_result $SCANNED_LSN SCANNED_LSN
302 --source include/search_pattern.inc
303
304 let SEARCH_PATTERN = $PATTERN_APPLYING_BATCH;
305 # Expected: found
306 --source include/search_pattern.inc
307
308 let SEARCH_PATTERN = $PATTERN_APPLY_BATCH_COMPLETED;
309 # Expected: found
310 --source include/search_pattern.inc
311
312 let SEARCH_PATTERN = $PATTERN_UNKNOWN_PARAM;
313 # Expected: not found
314 --source include/search_pattern.inc
315
316 --force-rmdir $MYSQLD_DATADIR
317
318 --echo ############################################################################################
319
320 --echo # Cleanup...
321 --force-rmdir $saved_datadir
322 --remove_file $CORRUPTION_INFO_FILE
323 --remove_file $SEARCH_FILE
324 let SEARCH_FILE=;
325 let SEARCH_PATTERN=;
326 let MYSQLD_ERROR_LOG=;
327 let MYSQLD_DATADIR=;
328 let RECOVERED_LSN=;
329 let SCANNED_LSN=;
330 let CORRUPTED_LOG_FILE_ID=;
331 let CORRUPTED_LOG_FILE=;
332 let CORRUPTED_BLOCK_OFFSET=;
333 let CORRUPTED_OFFSET=;
334 let CORRUPTED_REC_TYPE=;
335 let CORRUPTED_LOG_BLOCK=;
336 let CORRUPTION_INFO_FILE=;
337
338 --echo # Start MySQL...
339 --source include/start_mysqld.inc