Debian Codegraph @

storage/innobase/log/log0recv.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1997, 2026, Oracle and/or its affiliates.
4 Copyright (c) 2012, Facebook Inc.
5
6 This program is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License, version 2.0, as published by the
8 Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
21 for more details.
22
23 You should have received a copy of the GNU General Public License along with
24 this program; if not, write to the Free Software Foundation, Inc.,
25 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
27 *****************************************************************************/
28
29 /** @file log/log0recv.cc
30 Recovery
31
32 Created 9/20/1997 Heikki Tuuri
33 *******************************************************/
34
35 #include "ha_prototypes.h"
36
37 #include <my_aes.h>
38 #include <sys/types.h>
39
40 #include <array>
41 #include <iomanip>
42 #include <map>
43 #include <new>
44 #include <string>
45 #include <vector>
46
47 #include "arch0arch.h"
48 #include "btr0btr.h"
49 #include "btr0cur.h"
50 #include "buf0buf.h"
51 #include "buf0flu.h"
52 #include "clone0api.h"
53 #include "dict0dd.h"
54 #include "fil0fil.h"
55 #include "ha_prototypes.h"
56 #include "ibuf0ibuf.h"
57 #include "log0chkp.h" /* log_next_checkpoint_header */
58 #include "log0encryption.h" /* log_encryption_read */
59 #include "log0files_io.h"
60 #include "log0pre_8_0_30.h"
61 #include "log0recv.h"
62 #include "log0test.h"
63 #include "mem0mem.h"
64 #include "mtr0log.h"
65 #include "mtr0mtr.h"
66 #include "os0thread-create.h"
67 #include "page0cur.h"
68 #include "page0zip.h"
69 #include "trx0rec.h"
70 #include "trx0undo.h"
71 #include "ut0new.h"
72
73 #include "my_dbug.h"
74
75 #ifndef UNIV_HOTBACKUP
76 #include "buf0rea.h"
77 #include "ddl0ddl.h"
78 #include "srv0srv.h"
79 #include "srv0start.h"
80 #include "trx0purge.h"
81 #else /* !UNIV_HOTBACKUP */
82 #include "../meb/mutex.h"
83 #endif /* !UNIV_HOTBACKUP */
84
85 std::list<space_id_t> recv_encr_ts_list;
86
87 /** Log records are stored in the hash table in chunks at most of this size;
88 this must be less than UNIV_PAGE_SIZE as it is stored in the buffer pool */
89 #define RECV_DATA_BLOCK_SIZE (MEM_MAX_ALLOC_IN_BUF - sizeof(recv_data_t))
90
91 /** Read-ahead area in applying log records to file pages */
92 static const size_t RECV_READ_AHEAD_AREA = 32;
93
94 /** The recovery system */
95 recv_sys_t *recv_sys = nullptr;
96
97 /** true when applying redo log records during crash recovery; false
98 otherwise. Note that this is false while a background thread is
99 rolling back incomplete transactions. */
100 volatile bool recv_recovery_on;
101
102 #ifdef UNIV_HOTBACKUP
103 std::list<std::pair<space_id_t, lsn_t>> index_load_list;
104 volatile lsn_t backup_redo_log_flushed_lsn;
105
106 extern bool meb_is_space_loaded(const space_id_t space_id);
107
108 /* Re-define mutex macros to use the Mutex class defined by the MEB
109 source. MEB calls the routines in "fil0fil.cc" in parallel and,
110 therefore, the mutex protecting the critical sections of the tablespace
111 memory cache must be included also in the MEB compilation of this
112 module. (For other modules the mutex macros are defined as no ops in the
113 MEB compilation in "meb/src/include/bh_univ.i".) */
114
115 #undef mutex_enter
116 #undef mutex_exit
117 #undef mutex_own
118 #undef mutex_validate
119
120 #define mutex_enter(M) recv_mutex.lock()
121 #define mutex_exit(M) recv_mutex.unlock()
122 #define mutex_own(M) 1
123 #define mutex_validate(M) 1
124
125 /* Re-define the mutex macros for the mutex protecting the critical
126 sections of the log subsystem using an object of the meb::Mutex class. */
127
128 meb::Mutex recv_mutex;
129 extern meb::Mutex log_mutex;
130 meb::Mutex apply_log_mutex;
131
132 #undef log_mutex_enter
133 #undef log_mutex_exit
134 #define log_mutex_enter() log_mutex.lock()
135 #define log_mutex_exit() log_mutex.unlock()
136
137 /** Print important values from a page header.
138 @param[in] page page */
139 void meb_print_page_header(const page_t *page) {
140 ib::trace_1() << "space_id " << mach_read_from_4(page + FIL_PAGE_SPACE_ID)
141 << " page_nr " << mach_read_from_4(page + FIL_PAGE_OFFSET)
142 << " lsn " << mach_read_from_8(page + FIL_PAGE_LSN) << " type "
143 << mach_read_from_2(page + FIL_PAGE_TYPE);
144 }
145 #endif /* UNIV_HOTBACKUP */
146
147 // #ifndef UNIV_HOTBACKUP
148 PSI_memory_key mem_log_recv_page_hash_key;
149 PSI_memory_key mem_log_recv_space_hash_key;
150 // #endif /* !UNIV_HOTBACKUP */
151
152 /** true when recv_init_crash_recovery() has been called. */
153 bool recv_needed_recovery;
154
155 /** true if buf_page_is_corrupted() should check if the log sequence
156 number (FIL_PAGE_LSN) is in the future. Initially false, and set by
157 recv_recovery_from_checkpoint_start(). */
158 bool recv_lsn_checks_on;
159
160 /** If the following is true, the buffer pool file pages must be invalidated
161 after recovery and no ibuf operations are allowed; this becomes true if
162 the log record hash table becomes too full, and log records must be merged
163 to file pages already before the recovery is finished: in this case no
164 ibuf operations are allowed, as they could modify the pages read in the
165 buffer pool before the pages have been recovered to the up-to-date state.
166
167 true means that recovery is running and no operations on the log files
168 are allowed yet: the variable name is misleading. */
169 bool recv_no_ibuf_operations;
170
171 /** true When the redo log is being backed up */
172 bool recv_is_making_a_backup = false;
173
174 /** true when recovering from a backed up redo log file */
175 bool recv_is_from_backup = false;
176
177 /** The following counter is used to decide when to print info on
178 log scan */
179 static ulint recv_scan_print_counter;
180
181 /** The type of the previous parsed redo log record */
182 static mlog_id_t recv_previous_parsed_rec_type;
183
184 /** The offset of the previous parsed redo log record */
185 static ulint recv_previous_parsed_rec_offset;
186
187 /** The 'multi' flag of the previous parsed redo log record */
188 static ulint recv_previous_parsed_rec_is_multi;
189
190 /** This many frames must be left free in the buffer pool when we scan
191 the log and store the scanned log records in the buffer pool: we will
192 use these free frames to read in pages when we start applying the
193 log records to the database.
194 This is the default value. If the actual size of the buffer pool is
195 larger than 10 MB we'll set this value to 512. */
196 ulint recv_n_pool_free_frames;
197
198 /** The maximum lsn we see for a page during the recovery process. If this
199 is bigger than the lsn we are able to scan up to, that is an indication that
200 the recovery failed and the database may be corrupt. */
201 static lsn_t recv_max_page_lsn;
202
203 #ifndef UNIV_HOTBACKUP
204 #ifdef UNIV_PFS_THREAD
205 mysql_pfs_key_t recv_writer_thread_key;
206 #endif /* UNIV_PFS_THREAD */
207
208 static bool recv_writer_is_active() {
209 return srv_thread_is_active(srv_threads.m_recv_writer);
210 }
211
212 #endif /* !UNIV_HOTBACKUP */
213
214 /* prototypes */
215
216 #ifndef UNIV_HOTBACKUP
217
218 /** Reads a specified log segment to a buffer.
219 @param[in,out] log redo log
220 @param[in,out] buf buffer where to read
221 @param[in] start_lsn read area start
222 @param[in] end_lsn read area end
223 @return lsn up to which data was available on disk (ideally end_lsn)
224 or zero in case of error */
225 static lsn_t recv_read_log_seg(log_t &log, byte *buf, lsn_t start_lsn,
226 lsn_t end_lsn);
227
228 /** Initialize crash recovery environment. Can be called iff
229 recv_needed_recovery == false. */
230 static void recv_init_crash_recovery();
231 #endif /* !UNIV_HOTBACKUP */
232
233 /** Calculates the new value for lsn when more data is added to the log.
234 @param[in] lsn Old LSN
235 @param[in] len This many bytes of data is added, log block
236 headers not included
237 @return LSN after data addition */
238 lsn_t recv_calc_lsn_on_data_add(lsn_t lsn, os_offset_t len) {
239 os_offset_t frag_len;
240 os_offset_t lsn_len;
241
242 frag_len = (lsn % OS_FILE_LOG_BLOCK_SIZE) - LOG_BLOCK_HDR_SIZE;
243
244 ut_ad(frag_len <
245 OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_HDR_SIZE - LOG_BLOCK_TRL_SIZE);
246
247 lsn_len = len;
248
249 lsn_len +=
250 (lsn_len + frag_len) /
251 (OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_HDR_SIZE - LOG_BLOCK_TRL_SIZE) *
252 (LOG_BLOCK_HDR_SIZE + LOG_BLOCK_TRL_SIZE);
253
254 return lsn + lsn_len;
255 }
256
257 /** Destructor */
258 MetadataRecover::~MetadataRecover() {
259 for (auto &table : m_tables) {
260 ut::delete_(table.second);
261 }
262 }
263
264 /** Get the dynamic metadata of a specified table, create a new one
265 if not exist
266 @param[in] id table id
267 @return the metadata of the specified table */
268 PersistentTableMetadata *MetadataRecover::getMetadata(table_id_t id) {
269 PersistentTableMetadata *metadata = nullptr;
270 PersistentTables::iterator iter = m_tables.find(id);
271
272 if (iter == m_tables.end()) {
273 metadata = ut::new_withkey<PersistentTableMetadata>(
274 UT_NEW_THIS_FILE_PSI_KEY, id, 0);
275
276 m_tables.insert(std::make_pair(id, metadata));
277 } else {
278 metadata = iter->second;
279 ut_ad(metadata->get_table_id() == id);
280 }
281
282 ut_ad(metadata != nullptr);
283 return metadata;
284 }
285
286 /** Parse a dynamic metadata redo log of a table and store
287 the metadata locally
288 @param[in] id table id
289 @param[in] version table dynamic metadata version
290 @param[in] ptr redo log start
291 @param[in] end end of redo log
292 @retval ptr to next redo log record, nullptr if this log record
293 was truncated */
294 byte *MetadataRecover::parseMetadataLog(table_id_t id, uint64_t version,
295 byte *ptr, byte *end) {
296 if (ptr + 2 > end) {
297 /* At least we should get type byte and another one byte
298 for data, if not, it's an incomplete log */
299 return nullptr;
300 }
301
302 persistent_type_t type = static_cast<persistent_type_t>(ptr[0]);
303
304 ut_ad(dict_persist->persisters != nullptr);
305
306 Persister *persister = dict_persist->persisters->get(type);
307 if (persister == nullptr) {
308 recv_sys->found_corrupt_log = true;
309 return ptr;
310 }
311
312 ptr++;
313
314 PersistentTableMetadata *metadata = getMetadata(id);
315
316 PersistentTableMetadata new_entry{id, version};
317 bool corrupt;
318 ulint consumed = persister->read(new_entry, ptr, end - ptr, &corrupt);
319
320 if (corrupt) {
321 recv_sys->found_corrupt_log = true;
322 return ptr + consumed;
323 }
324
325 if (consumed == 0) {
326 return nullptr;
327 }
328
329 persister->aggregate(*metadata, new_entry);
330 return ptr + consumed;
331 }
332
333 /** Creates the recovery system. */
334 void recv_sys_create() {
335 if (recv_sys != nullptr) {
336 return;
337 }
338
339 recv_sys = static_cast<recv_sys_t *>(
340 ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*recv_sys)));
341 ut_a(recv_sys->last_block_first_mtr_boundary == 0);
342 mutex_create(LATCH_ID_RECV_SYS, &recv_sys->mutex);
343 mutex_create(LATCH_ID_RECV_WRITER, &recv_sys->writer_mutex);
344
345 recv_sys->spaces = nullptr;
346 }
347
348 /** Resize the recovery parsing buffer up to log_buffer_size */
349 static bool recv_sys_resize_buf() {
350 ut_ad(recv_sys->buf_len <= srv_log_buffer_size);
351
352 #ifndef UNIV_HOTBACKUP
353 /* If the buffer cannot be extended further, return false. */
354 if (recv_sys->buf_len == srv_log_buffer_size) {
355 ib::error(ER_IB_MSG_723, srv_log_buffer_size);
356 return false;
357 }
358 #else /* !UNIV_HOTBACKUP */
359 if ((recv_sys->buf_len >= srv_log_buffer_size) ||
360 (recv_sys->len >= srv_log_buffer_size)) {
361 ib::fatal(UT_LOCATION_HERE, ER_IB_ERR_LOG_PARSING_BUFFER_OVERFLOW)
362 << "Log parsing buffer overflow. Log parse failed. "
363 << "Please increase --limit-memory above "
364 << srv_log_buffer_size / 1024 / 1024 << " (MB)";
365 }
366 #endif /* !UNIV_HOTBACKUP */
367
368 /* Extend the buffer by double the current size with the resulting
369 size not more than srv_log_buffer_size. */
370 recv_sys->buf_len = ((recv_sys->buf_len * 2) >= srv_log_buffer_size)
371 ? srv_log_buffer_size
372 : recv_sys->buf_len * 2;
373
374 /* Resize the buffer to the new size. */
375 recv_sys->buf = static_cast<byte *>(ut::realloc_withkey(
376 UT_NEW_THIS_FILE_PSI_KEY, recv_sys->buf, recv_sys->buf_len));
377
378 ut_ad(recv_sys->buf != nullptr);
379
380 /* Return error and fail the recovery if not enough memory available */
381 if (recv_sys->buf == nullptr) {
382 ib::error(ER_IB_MSG_740);
383 return false;
384 }
385
386 ib::info(ER_IB_MSG_739, recv_sys->buf_len);
387 return true;
388 }
389
390 /** Free up recovery data structures. */
391 static void recv_sys_finish() {
392 #ifndef UNIV_HOTBACKUP
393 recv_sys->dblwr->recovered();
394 #endif /* !UNIV_HOTBACKUP */
395
396 if (recv_sys->spaces != nullptr) {
397 for (auto &space : *recv_sys->spaces) {
398 if (space.second.m_heap != nullptr) {
399 mem_heap_free(space.second.m_heap);
400 space.second.m_heap = nullptr;
401 }
402 }
403
404 ut::delete_(recv_sys->spaces);
405 }
406
407 ut::free(recv_sys->buf);
408 ut::delete_(recv_sys->metadata_recover);
409
410 recv_sys->buf = nullptr;
411 recv_sys->spaces = nullptr;
412 recv_sys->metadata_recover = nullptr;
413 }
414
415 /** Release recovery system mutexes. */
416 void recv_sys_close() {
417 if (recv_sys == nullptr) {
418 return;
419 }
420
421 recv_sys_finish();
422
423 #ifndef UNIV_HOTBACKUP
424 if (recv_sys->flush_start != nullptr) {
425 os_event_destroy(recv_sys->flush_start);
426 }
427
428 if (recv_sys->flush_end != nullptr) {
429 os_event_destroy(recv_sys->flush_end);
430 }
431
432 #endif /* !UNIV_HOTBACKUP */
433
434 ut::delete_(recv_sys->dblwr);
435
436 call_destructor(&recv_sys->deleted);
437 call_destructor(&recv_sys->missing_ids);
438 call_destructor(&recv_sys->saved_recs);
439
440 mutex_free(&recv_sys->mutex);
441
442 #ifndef UNIV_HOTBACKUP
443 ut_ad(!recv_writer_is_active());
444 #endif /* !UNIV_HOTBACKUP */
445 mutex_free(&recv_sys->writer_mutex);
446
447 ut::free(recv_sys);
448 recv_sys = nullptr;
449 }
450
451 #ifndef UNIV_HOTBACKUP
452 /** Reset the state of the recovery system variables. */
453 void recv_sys_var_init() {
454 recv_recovery_on = false;
455 recv_needed_recovery = false;
456 recv_lsn_checks_on = false;
457 recv_no_ibuf_operations = false;
458 recv_scan_print_counter = 0;
459 recv_previous_parsed_rec_type = MLOG_SINGLE_REC_FLAG;
460 recv_previous_parsed_rec_offset = 0;
461 recv_previous_parsed_rec_is_multi = 0;
462 recv_n_pool_free_frames = 256;
463 recv_max_page_lsn = 0;
464 }
465 #endif /* !UNIV_HOTBACKUP */
466
467 /** Get the number of bytes used by all the heaps
468 @return number of bytes used */
469 #ifndef UNIV_HOTBACKUP
470 static size_t recv_heap_used()
471 #else /* !UNIV_HOTBACKUP */
472 size_t meb_heap_used()
473 #endif /* !UNIV_HOTBACKUP */
474 {
475 size_t size = 0;
476
477 for (auto &space : *recv_sys->spaces) {
478 if (space.second.m_heap != nullptr) {
479 size += mem_heap_get_size(space.second.m_heap);
480 }
481 }
482
483 return size;
484 }
485
486 /** Prints diagnostic info of corrupt log.
487 @param[in] ptr pointer to corrupt log record
488 @param[in] type type of the log record (could be garbage)
489 @param[in] space tablespace ID (could be garbage)
490 @param[in] page_no page number (could be garbage)
491 @return whether processing should continue */
492 static bool recv_report_corrupt_log(const byte *ptr, int type, space_id_t space,
493 page_no_t page_no) {
494 ib::error(ER_IB_MSG_694);
495
496 ib::info(
497 ER_IB_MSG_695, type, ulong{space}, ulong{page_no},
498 ulonglong{recv_sys->recovered_lsn}, int{recv_previous_parsed_rec_type},
499 ulonglong{recv_previous_parsed_rec_is_multi},
500 ssize_t{ptr - recv_sys->buf}, ulonglong{recv_previous_parsed_rec_offset});
501
502 #ifdef UNIV_HOTBACKUP
503 ut_ad(ptr >= recv_sys->buf);
504 #endif /* UNIV_HOTBACKUP */
505 ut_ad(ptr <= recv_sys->buf + recv_sys->len);
506
507 const ulint limit = 100;
508 const ulint before = std::min(recv_previous_parsed_rec_offset, limit);
509 const ulint after = std::min(recv_sys->len - (ptr - recv_sys->buf), limit);
510
511 ib::info(ER_IB_MSG_696, ulonglong{before}, ulonglong{after});
512
513 ut_print_buf(
514 stderr, recv_sys->buf + recv_previous_parsed_rec_offset - before,
515 ptr - recv_sys->buf + before + after - recv_previous_parsed_rec_offset);
516 putc('\n', stderr);
517
518 #ifndef UNIV_HOTBACKUP
519 if (srv_force_recovery == 0) {
520 ib::info(ER_IB_MSG_697);
521
522 return false;
523 }
524
525 ib::warn(ER_IB_MSG_LOG_CORRUPT, FORCE_RECOVERY_MSG);
526 #endif /* !UNIV_HOTBACKUP */
527
528 return true;
529 }
530
531 void recv_sys_init() {
532 if (recv_sys->spaces != nullptr) {
533 return;
534 }
535
536 mutex_enter(&recv_sys->mutex);
537
538 #ifndef UNIV_HOTBACKUP
539 if (!srv_read_only_mode) {
540 recv_sys->flush_start = os_event_create();
541 recv_sys->flush_end = os_event_create();
542 }
543 #else /* !UNIV_HOTBACKUP */
544 recv_is_from_backup = true;
545 recv_sys->apply_file_operations = false;
546 #endif /* !UNIV_HOTBACKUP */
547
548 recv_sys->buf_len =
549 std::min<unsigned long>(RECV_PARSING_BUF_SIZE, srv_log_buffer_size);
550 recv_sys->buf = static_cast<byte *>(
551 ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, recv_sys->buf_len));
552
553 recv_sys->len = 0;
554 recv_sys->recovered_offset = 0;
555
556 using Spaces = recv_sys_t::Spaces;
557
558 recv_sys->spaces = ut::new_withkey<Spaces>(
559 ut::make_psi_memory_key(mem_log_recv_space_hash_key));
560
561 recv_sys->n_addrs = 0;
562
563 recv_sys->apply_log_recs = false;
564 recv_sys->apply_batch_on = false;
565 recv_sys->is_cloned_db = false;
566
567 recv_sys->found_corrupt_log = false;
568 recv_sys->found_corrupt_fs = false;
569
570 recv_max_page_lsn = 0;
571
572 recv_sys->dblwr =
573 ut::new_withkey<dblwr::recv::DBLWR>(UT_NEW_THIS_FILE_PSI_KEY);
574
575 new (&recv_sys->deleted) recv_sys_t::Missing_Ids();
576
577 new (&recv_sys->missing_ids) recv_sys_t::Missing_Ids();
578
579 new (&recv_sys->saved_recs) recv_sys_t::Mlog_records();
580
581 recv_sys->saved_recs.resize(recv_sys_t::MAX_SAVED_MLOG_RECS);
582
583 recv_sys->metadata_recover =
584 ut::new_withkey<MetadataRecover>(UT_NEW_THIS_FILE_PSI_KEY);
585
586 mutex_exit(&recv_sys->mutex);
587 }
588
589 /** Empties the hash table when it has been fully processed. */
590 static void recv_sys_empty_hash() {
591 ut_ad(mutex_own(&recv_sys->mutex));
592
593 if (recv_sys->n_addrs != 0) {
594 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_699, ulonglong{recv_sys->n_addrs});
595 }
596
597 for (auto &space : *recv_sys->spaces) {
598 if (space.second.m_heap != nullptr) {
599 mem_heap_free(space.second.m_heap);
600 space.second.m_heap = nullptr;
601 }
602 }
603
604 ut::delete_(recv_sys->spaces);
605
606 using Spaces = recv_sys_t::Spaces;
607
608 recv_sys->spaces = ut::new_withkey<Spaces>(
609 ut::make_psi_memory_key(mem_log_recv_space_hash_key));
610 }
611
612 /** Check the 4-byte checksum to the trailer checksum field of a log
613 block.
614 @param[in] block pointer to a log block
615 @return whether the checksum matches */
616 #ifndef UNIV_HOTBACKUP
617 static
618 #endif /* !UNIV_HOTBACKUP */
619 bool
620 log_block_checksum_is_ok(const byte *block) {
621 return !srv_log_checksums ||
622 log_block_get_checksum(block) == log_block_calc_checksum(block);
623 }
624
625 /** Get the page map for a tablespace. It will create one if one isn't found.
626 @param[in] space_id Tablespace ID for which page map required.
627 @param[in] create false if lookup only
628 @return the space data or null if not found */
629 static recv_sys_t::Space *recv_get_page_map(space_id_t space_id, bool create) {
630 auto it = recv_sys->spaces->find(space_id);
631
632 if (it != recv_sys->spaces->end()) {
633 return &it->second;
634
635 } else if (create) {
636 mem_heap_t *heap;
637
638 heap = mem_heap_create(256, UT_LOCATION_HERE, MEM_HEAP_FOR_RECV_SYS);
639
640 using Space = recv_sys_t::Space;
641 using Value = recv_sys_t::Spaces::value_type;
642
643 auto where = recv_sys->spaces->insert(it, Value{space_id, Space(heap)});
644
645 return &where->second;
646 }
647
648 return nullptr;
649 }
650
651 /** Gets the list of log records for a <space, page>.
652 @param[in] space_id Tablespace ID
653 @param[in] page_no Page number
654 @return the redo log entries or nullptr if not found */
655 static recv_addr_t *recv_get_rec(space_id_t space_id, page_no_t page_no) {
656 recv_sys_t::Space *space;
657
658 space = recv_get_page_map(space_id, false);
659
660 if (space != nullptr) {
661 auto it = space->m_pages.find(page_no);
662
663 if (it != space->m_pages.end()) {
664 return it->second;
665 }
666 }
667
668 return nullptr;
669 }
670
671 /** Checks if a given log data block could be considered a next valid block,
672 with regards to the epoch_no it has stored in its header, during the recovery.
673 @param[in] log_block_epoch_no epoch_no of the log data block to check
674 @param[in] last_epoch_no epoch_no of the last data block scanned
675 @return true iff the provided log block has valid epoch_no */
676 static bool log_block_epoch_no_is_valid(uint32_t log_block_epoch_no,
677 uint32_t last_epoch_no) {
678 const auto expected_next_epoch_no = last_epoch_no + 1;
679
680 return log_block_epoch_no == last_epoch_no ||
681 log_block_epoch_no == expected_next_epoch_no;
682 }
683
684 #ifndef UNIV_HOTBACKUP
685 /** Store the collected persistent dynamic metadata to
686 mysql.innodb_dynamic_metadata */
687 void MetadataRecover::store() {
688 ut_ad(dict_sys->dynamic_metadata != nullptr);
689 ut_ad(dict_persist->table_buffer != nullptr);
690
691 DDTableBuffer *table_buffer = dict_persist->table_buffer;
692
693 if (empty()) {
694 return;
695 }
696
697 mutex_enter(&dict_persist->mutex);
698
699 for (auto meta : m_tables) {
700 table_id_t table_id = meta.first;
701 PersistentTableMetadata *metadata = meta.second;
702 byte buffer[REC_MAX_DATA_SIZE];
703 size_t size;
704
705 size = dict_persist->persisters->write(*metadata, buffer);
706
707 dberr_t error =
708 table_buffer->replace(table_id, metadata->get_version(), buffer, size);
709 if (error != DB_SUCCESS) {
710 ut_d(ut_error);
711 }
712 }
713
714 mutex_exit(&dict_persist->mutex);
715 }
716
717 /** recv_writer thread tasked with flushing dirty pages from the buffer
718 pools. */
719 static void recv_writer_thread() {
720 ut_ad(!srv_read_only_mode);
721
722 /* The code flow is as follows:
723 Step 1: In recv_recovery_from_checkpoint_start().
724 Step 2: This recv_writer thread is started.
725 Step 3: In recv_recovery_from_checkpoint_finish().
726 Step 4: Wait for recv_writer thread to complete.
727 Step 5: Assert that recv_writer thread is not active anymore.
728
729 It is possible that the thread that is started in step 2,
730 becomes active only after step 4 and hence the assert in
731 step 5 fails. So mark this thread active only if necessary. */
732 mutex_enter(&recv_sys->writer_mutex);
733
734 if (!recv_recovery_on) {
735 mutex_exit(&recv_sys->writer_mutex);
736 return;
737 }
738 mutex_exit(&recv_sys->writer_mutex);
739
740 while (srv_shutdown_state.load() == SRV_SHUTDOWN_NONE) {
741 ut_a(srv_shutdown_state_matches([](auto state) {
742 return state == SRV_SHUTDOWN_NONE || state == SRV_SHUTDOWN_EXIT_THREADS;
743 }));
744
745 std::this_thread::sleep_for(std::chrono::milliseconds(100));
746
747 mutex_enter(&recv_sys->writer_mutex);
748
749 if (!recv_recovery_on) {
750 mutex_exit(&recv_sys->writer_mutex);
751 break;
752 }
753
754 if (log_test != nullptr) {
755 mutex_exit(&recv_sys->writer_mutex);
756 continue;
757 }
758
759 /* Flush pages from end of LRU if required */
760 os_event_reset(recv_sys->flush_end);
761 recv_sys->flush_type = BUF_FLUSH_LRU;
762 os_event_set(recv_sys->flush_start);
763 os_event_wait(recv_sys->flush_end);
764
765 mutex_exit(&recv_sys->writer_mutex);
766 }
767 }
768
769 #endif /* !UNIV_HOTBACKUP */
770
771 /** Frees the recovery system. */
772 void recv_sys_free() {
773 mutex_enter(&recv_sys->mutex);
774
775 recv_sys_finish();
776
777 #ifndef UNIV_HOTBACKUP
778 /* wake page cleaner up to progress */
779 if (!srv_read_only_mode) {
780 ut_ad(!recv_recovery_on);
781 ut_ad(!recv_writer_is_active());
782 if (buf_flush_event != nullptr) {
783 os_event_reset(buf_flush_event);
784 }
785 os_event_set(recv_sys->flush_start);
786 }
787 #endif /* !UNIV_HOTBACKUP */
788
789 /* Free encryption data structures. */
790 if (recv_sys->keys != nullptr) {
791 for (auto &key : *recv_sys->keys) {
792 if (key.ptr != nullptr) {
793 ut::free(key.ptr);
794 key.ptr = nullptr;
795 }
796
797 if (key.iv != nullptr) {
798 ut::free(key.iv);
799 key.iv = nullptr;
800 }
801 }
802
803 recv_sys->keys->swap(*recv_sys->keys);
804
805 ut::delete_(recv_sys->keys);
806 recv_sys->keys = nullptr;
807 }
808
809 mutex_exit(&recv_sys->mutex);
810 }
811
812 #ifndef UNIV_HOTBACKUP
813
814 /** Determine if a redo log from a version before MySQL 8.0.30 is clean.
815 @param[in,out] log redo log
816 @return error code
817 @retval DB_SUCCESS if the redo log is clean
818 @retval DB_ERROR if the redo log is corrupted or dirty */
819 dberr_t recv_verify_log_is_clean_pre_8_0_30(log_t &log) {
820 ut_a(log.m_format < Log_format::CURRENT);
821
822 const size_t n_files = log_files_number_of_existing_files(log.m_files);
823 ut_a(n_files >= 2);
824
825 ib::info(ER_IB_MSG_LOG_FORMAT_OLD, ulong{to_int(log.m_format)});
826
827 using namespace log_pre_8_0_30;
828
829 const auto logfile0 = log.m_files.file(0);
830 ut_a(logfile0 != log.m_files.end());
831
832 const os_offset_t file_size = logfile0->m_size_in_bytes;
833
834 /* For unknown reasons, InnoDB before 8.0.30 was choosing the latest
835 checkpoint by comparing checkpoints' numbers instead of checkpoints'
836 LSN values. These should be ordered the same and there shouldn't be
837 difference, but to preserve the full compatibility, we prefer to do
838 it the same way as it was (after 8.0.30, checkpoints are compared by
839 their LSN values because we no longer store checkpoint numbers). */
840 byte header_buf[OS_FILE_LOG_BLOCK_SIZE] = {};
841
842 Checkpoint_header chkp_header = {};
843 bool checkpoint_found = false;
844 for (auto hdr_no : {Log_checkpoint_header_no::HEADER_1,
845 Log_checkpoint_header_no::HEADER_2}) {
846 auto file_handle = logfile0->open(Log_file_access_mode::READ_ONLY);
847 if (!file_handle.is_open()) {
848 return DB_CANNOT_OPEN_FILE;
849 }
850
851 const dberr_t err =
852 log_checkpoint_header_read(file_handle, hdr_no, header_buf);
853 if (err != DB_SUCCESS) {
854 return DB_ERROR;
855 }
856
857 Checkpoint_header h;
858 if (!checkpoint_header_deserialize(header_buf, h)) {
859 continue;
860 }
861
862 if (!checkpoint_found || h.m_checkpoint_no > chkp_header.m_checkpoint_no) {
863 chkp_header = h;
864 checkpoint_found = true;
865 }
866 }
867
868 if (!checkpoint_found) {
869 ib::error(ER_IB_MSG_RECOVERY_CHECKPOINT_NOT_FOUND);
870 return DB_ERROR;
871 }
872
873 if (log_encryption_read(log, *logfile0) != DB_SUCCESS) {
874 return DB_ERROR;
875 }
876
877 os_offset_t source_offset =
878 chkp_header.m_checkpoint_offset % (file_size * n_files);
879
880 const Log_file_id file_id = source_offset / file_size;
881
882 source_offset %= file_size;
883
884 static const char *RTFM_LINK = REFMAN "upgrading.html";
885
886 byte buf[OS_FILE_LOG_BLOCK_SIZE];
887
888 auto file_handle =
889 Log_file::open(log.m_files_ctx, file_id, Log_file_access_mode::READ_ONLY,
890 log.m_encryption_metadata);
891 ut_a(file_handle.is_open());
892
893 const dberr_t err = file_handle.read(
894 ut_uint64_align_down(source_offset, OS_FILE_LOG_BLOCK_SIZE),
895 OS_FILE_LOG_BLOCK_SIZE, buf);
896 ut_a(err == DB_SUCCESS);
897
898 file_handle.close();
899
900 if (!log_block_checksum_is_ok(buf)) {
901 ib::error(ER_IB_MSG_LOG_FORMAT_OLD_AND_LOG_CORRUPTED,
902 log.m_creator_name.c_str(), RTFM_LINK);
903 return DB_ERROR;
904 }
905
906 /* On a shutdown with innodb-fast-shutdown < 2, the redo log will be
907 logically empty after the checkpoint LSN. */
908
909 if (log_block_get_data_len(buf) !=
910 (source_offset & (OS_FILE_LOG_BLOCK_SIZE - 1))) {
911 ib::error(ER_IB_MSG_LOG_FORMAT_OLD_AND_NO_CLEAN_SHUTDOWN,
912 log.m_creator_name.c_str(), RTFM_LINK);
913 return DB_ERROR;
914 }
915
916 /* This lsn might be larger than flushed_lsn found in system tablespace if the
917 shutdown wasn't slow. This isn't officially supported scenario, but we can
918 handle it if redo was logically empty, by creating new redo with start_lsn
919 larger than the checkpoint_lsn found here. */
920 recv_sys->checkpoint_lsn = chkp_header.m_checkpoint_lsn;
921
922 return DB_SUCCESS;
923 }
924
925 /** Describes location of a single checkpoint. */
926 struct Log_checkpoint_location {
927 /** File containing checkpoint header and checkpoint lsn. */
928 Log_file_id m_checkpoint_file_id{0};
929
930 /** Checkpoint header number. */
931 Log_checkpoint_header_no m_checkpoint_header_no{};
932
933 /** Checkpoint LSN. */
934 lsn_t m_checkpoint_lsn{0};
935 };
936
937 /** Find the latest checkpoint in the given log file.
938 @param[in] file_handle handle for the opened redo log file
939 @param[out] checkpoint the latest checkpoint found (if any)
940 @return true iff any checkpoint has been found */
941 [[nodiscard]] static bool recv_find_max_checkpoint(
942 log_t &, Log_file_handle &file_handle,
943 Log_checkpoint_location &checkpoint) {
944 bool found = false;
945 checkpoint = {};
946
947 for (auto checkpoint_header_no : {Log_checkpoint_header_no::HEADER_1,
948 Log_checkpoint_header_no::HEADER_2}) {
949 Log_checkpoint_header checkpoint_header;
950 const dberr_t err = log_checkpoint_header_read(
951 file_handle, checkpoint_header_no, checkpoint_header);
952 if (err != DB_SUCCESS) {
953 /* Crash if IO error on read */
954 ut_a(err == DB_CORRUPTION);
955 continue;
956 }
957
958 const lsn_t checkpoint_lsn = checkpoint_header.m_checkpoint_lsn;
959 if (checkpoint_lsn == 0) {
960 continue;
961 }
962
963 DBUG_PRINT("ib_log", ("checkpoint at " LSN_PF, checkpoint_lsn));
964
965 if (!found || checkpoint_lsn > checkpoint.m_checkpoint_lsn) {
966 ut_a(checkpoint_lsn >= LOG_START_LSN);
967 found = true;
968 checkpoint.m_checkpoint_file_id = file_handle.file_id();
969 checkpoint.m_checkpoint_header_no = checkpoint_header_no;
970 checkpoint.m_checkpoint_lsn = checkpoint_lsn;
971 }
972 }
973
974 return found;
975 }
976
977 /** Find the latest checkpoint (check all existing redo log files).
978 @param[in,out] log redo log
979 @param[out] checkpoint the latest checkpoint found (if any)
980 @return true iff any checkpoint has been found */
981 static bool recv_find_max_checkpoint(log_t &log,
982 Log_checkpoint_location &checkpoint) {
983 bool found = false;
984 checkpoint = {};
985
986 log_files_for_each(log.m_files, [&](const Log_file &file) {
987 auto file_handle = file.open(Log_file_access_mode::READ_ONLY);
988 ut_a(file_handle.is_open());
989
990 Log_checkpoint_location checkpoint_in_file;
991
992 if (!recv_find_max_checkpoint(log, file_handle, checkpoint_in_file)) {
993 return;
994 }
995
996 if (!file.contains(checkpoint_in_file.m_checkpoint_lsn)) {
997 const auto file_path = file_handle.file_path();
998 ib::error(ER_IB_MSG_RECOVERY_CHECKPOINT_OUTSIDE_LOG_FILE,
999 ulonglong{checkpoint_in_file.m_checkpoint_lsn},
1000 file_path.c_str(), ulonglong{file.m_start_lsn},
1001 ulonglong{file.m_end_lsn});
1002 return;
1003 }
1004
1005 if (!found ||
1006 checkpoint_in_file.m_checkpoint_lsn > checkpoint.m_checkpoint_lsn) {
1007 found = true;
1008 checkpoint = checkpoint_in_file;
1009 }
1010 });
1011
1012 return found;
1013 }
1014
1015 /** Reads in pages which have hashed log records, from an area around a given
1016 page number.
1017 @param[in] page_id Read the pages around this page number
1018 @return number of pages found */
1019 static ulint recv_read_in_area(const page_id_t &page_id) {
1020 page_no_t low_limit;
1021
1022 low_limit = page_id.page_no() - (page_id.page_no() % RECV_READ_AHEAD_AREA);
1023
1024 ulint n = 0;
1025
1026 std::array<page_no_t, RECV_READ_AHEAD_AREA> page_nos;
1027
1028 for (page_no_t page_no = low_limit;
1029 page_no < low_limit + RECV_READ_AHEAD_AREA; ++page_no) {
1030 recv_addr_t *recv_addr;
1031
1032 recv_addr = recv_get_rec(page_id.space(), page_no);
1033
1034 const page_id_t cur_page_id(page_id.space(), page_no);
1035
1036 if (recv_addr != nullptr && !buf_page_peek(cur_page_id)) {
1037 mutex_enter(&recv_sys->mutex);
1038
1039 if (recv_addr->state == RECV_NOT_PROCESSED) {
1040 recv_addr->state = RECV_BEING_READ;
1041
1042 page_nos[n] = page_no;
1043
1044 ++n;
1045 }
1046
1047 mutex_exit(&recv_sys->mutex);
1048 }
1049 }
1050
1051 if (n > 0) {
1052 /* There are pages that need to be read. Go ahead and read them
1053 for recovery. */
1054 buf_read_recv_pages(false, page_id.space(), &page_nos[0], n);
1055 }
1056
1057 return n;
1058 }
1059
1060 /** Apply the log records to a page
1061 @param[in,out] recv_addr Redo log records to apply */
1062 static void recv_apply_log_rec(recv_addr_t *recv_addr) {
1063 if (recv_addr->state == RECV_DISCARDED) {
1064 ut_a(recv_sys->n_addrs > 0);
1065 --recv_sys->n_addrs;
1066 return;
1067 }
1068
1069 bool found;
1070 const page_id_t page_id(recv_addr->space, recv_addr->page_no);
1071
1072 const page_size_t page_size =
1073 fil_space_get_page_size(recv_addr->space, &found);
1074
1075 if (!found || recv_sys->missing_ids.find(recv_addr->space) !=
1076 recv_sys->missing_ids.end()) {
1077 /* Tablespace was discarded or dropped after changes were
1078 made to it. Or, we have ignored redo log for this tablespace
1079 earlier and somehow it has been found now. We can't apply
1080 this redo log out of order. */
1081
1082 recv_addr->state = RECV_PROCESSED;
1083
1084 ut_a(recv_sys->n_addrs > 0);
1085 --recv_sys->n_addrs;
1086
1087 /* If the tablespace has been explicitly deleted, we
1088 can safely ignore it. */
1089
1090 if (recv_sys->deleted.find(recv_addr->space) == recv_sys->deleted.end()) {
1091 recv_sys->missing_ids.insert(recv_addr->space);
1092 }
1093
1094 } else if (recv_addr->state == RECV_NOT_PROCESSED) {
1095 mutex_exit(&recv_sys->mutex);
1096
1097 if (buf_page_peek(page_id)) {
1098 mtr_t mtr;
1099
1100 mtr_start(&mtr);
1101
1102 buf_block_t *block;
1103
1104 block =
1105 buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, &mtr);
1106
1107 buf_block_dbg_add_level(block, SYNC_NO_ORDER_CHECK);
1108
1109 recv_recover_page(false, block);
1110
1111 mtr_commit(&mtr);
1112
1113 } else {
1114 recv_read_in_area(page_id);
1115 }
1116
1117 mutex_enter(&recv_sys->mutex);
1118 }
1119 }
1120
1121 dberr_t recv_apply_hashed_log_recs(log_t &log, bool allow_ibuf) {
1122 for (;;) {
1123 mutex_enter(&recv_sys->mutex);
1124
1125 if (!recv_sys->apply_batch_on) {
1126 break;
1127 }
1128
1129 mutex_exit(&recv_sys->mutex);
1130
1131 std::this_thread::sleep_for(std::chrono::milliseconds(500));
1132 }
1133
1134 if (!allow_ibuf) {
1135 recv_no_ibuf_operations = true;
1136 }
1137
1138 recv_sys->apply_log_recs = true;
1139 recv_sys->apply_batch_on = true;
1140
1141 auto batch_size = recv_sys->n_addrs;
1142
1143 ib::info(ER_IB_MSG_707, ulonglong{batch_size});
1144
1145 static const size_t PCT = 10;
1146
1147 size_t pct = PCT;
1148 size_t applied = 0;
1149 auto unit = batch_size / PCT;
1150
1151 if (unit <= PCT) {
1152 pct = 100;
1153 unit = batch_size;
1154 }
1155
1156 auto start_time = std::chrono::steady_clock::now();
1157
1158 for (const auto &space : *recv_sys->spaces) {
1159 bool dropped;
1160
1161 if (space.first == TRX_SYS_SPACE) {
1162 dropped = false;
1163 } else {
1164 dberr_t err = fil_tablespace_open_for_recovery(space.first);
1165 if (err == DB_SUCCESS) {
1166 dropped = false;
1167 } else if (err == DB_CORRUPTION) {
1168 /* Page couldn't be recovered from doublewrite, we cannot proceed
1169 with recovery. Skip applying redos and abort the startup. */
1170 mutex_exit(&recv_sys->mutex);
1171 return err;
1172 } else {
1173 /* Tablespace was dropped. It should not have been scanned unless it
1174 is an undo space that was under construction. */
1175
1176 if (fil_tablespace_lookup_for_recovery(space.first)) {
1177 ut_ad(fsp_is_undo_tablespace(space.first));
1178 }
1179 dropped = true;
1180 }
1181 }
1182
1183 for (auto pages : space.second.m_pages) {
1184 ut_ad(pages.second->space == space.first);
1185
1186 if (dropped) {
1187 pages.second->state = RECV_DISCARDED;
1188 }
1189
1190 recv_apply_log_rec(pages.second);
1191
1192 ++applied;
1193
1194 if (unit == 0 || (applied % unit) == 0) {
1195 ib::info(ER_IB_MSG_708) << pct << "%";
1196
1197 pct += PCT;
1198
1199 start_time = std::chrono::steady_clock::now();
1200
1201 } else if (std::chrono::steady_clock::now() - start_time >=
1202 PRINT_INTERVAL) {
1203 start_time = std::chrono::steady_clock::now();
1204
1205 ib::info(ER_IB_MSG_709)
1206 << std::setprecision(2)
1207 << ((double)applied * 100) / (double)batch_size << "%";
1208 }
1209 }
1210 }
1211
1212 /* Wait until all the pages have been processed */
1213
1214 while (recv_sys->n_addrs != 0) {
1215 mutex_exit(&recv_sys->mutex);
1216
1217 std::this_thread::sleep_for(std::chrono::milliseconds(500));
1218
1219 mutex_enter(&recv_sys->mutex);
1220 }
1221
1222 if (!allow_ibuf) {
1223 /* Flush all the file pages to disk and invalidate them in
1224 the buffer pool */
1225 ut_d(log.disable_redo_writes = true);
1226 ut_a(recv_sys->flush_end != nullptr);
1227
1228 mutex_exit(&recv_sys->mutex);
1229
1230 /* Stop the recv_writer thread from issuing any LRU
1231 flush batches. */
1232 mutex_enter(&recv_sys->writer_mutex);
1233
1234 /* Wait for any currently run batch to end. */
1235 buf_flush_wait_LRU_batch_end();
1236
1237 os_event_reset(recv_sys->flush_end);
1238
1239 recv_sys->flush_type = BUF_FLUSH_LIST;
1240
1241 os_event_set(recv_sys->flush_start);
1242
1243 os_event_wait(recv_sys->flush_end);
1244
1245 buf_pool_invalidate();
1246
1247 /* Allow batches from recv_writer thread. */
1248 mutex_exit(&recv_sys->writer_mutex);
1249
1250 ut_d(log.disable_redo_writes = false);
1251
1252 mutex_enter(&recv_sys->mutex);
1253
1254 recv_no_ibuf_operations = false;
1255 }
1256
1257 recv_sys->apply_log_recs = false;
1258 recv_sys->apply_batch_on = false;
1259
1260 recv_sys_empty_hash();
1261
1262 mutex_exit(&recv_sys->mutex);
1263
1264 ib::info(ER_IB_MSG_710);
1265 return DB_SUCCESS;
1266 }
1267
1268 #else /* !UNIV_HOTBACKUP */
1269 /** Scans the log segment and n_bytes_scanned is set to the length of valid
1270 log scanned.
1271 @param[in] buf buffer containing log data
1272 @param[in] buf_len data length in that buffer
1273 @param[in,out] scanned_lsn LSN of buffer start, we return scanned
1274 lsn
1275 @param[in,out] scanned_epoch_no the highest scanned epoch number so far
1276 @param[out] block_no highest block no in scanned buffer.
1277 @param[out] n_bytes_scanned how much we were able to scan, smaller
1278 than buf_len if log data ended here
1279 +@param[out] has_encrypted_log set true, if buffer contains encrypted
1280 +redo log, set false otherwise */
1281 void meb_scan_log_seg(byte *buf, size_t buf_len, lsn_t *scanned_lsn,
1282 uint32_t *scanned_epoch_no, uint32_t *block_no,
1283 size_t *n_bytes_scanned, bool *has_encrypted_log) {
1284 *n_bytes_scanned = 0;
1285 *has_encrypted_log = false;
1286
1287 for (auto log_block = buf; log_block < buf + buf_len;
1288 log_block += OS_FILE_LOG_BLOCK_SIZE) {
1289 Log_data_block_header block_header;
1290 log_data_block_header_deserialize(log_block, block_header);
1291 uint32_t no = block_header.m_hdr_no;
1292 bool is_encrypted = log_block_get_encrypt_bit(log_block);
1293
1294 if (is_encrypted) {
1295 *has_encrypted_log = true;
1296 return;
1297 }
1298
1299 if (no != log_block_convert_lsn_to_hdr_no(*scanned_lsn) ||
1300 !log_block_checksum_is_ok(log_block)) {
1301 ib::trace_2() << "Scanned lsn: " << *scanned_lsn << " header no: " << no
1302 << " converted no: "
1303 << log_block_convert_lsn_to_hdr_no(*scanned_lsn)
1304 << " checksum: " << log_block_checksum_is_ok(log_block)
1305 << " block epoch no: " << block_header.m_epoch_no;
1306
1307 /* Garbage or an incompletely written log block */
1308
1309 log_block += OS_FILE_LOG_BLOCK_SIZE;
1310 break;
1311 }
1312
1313 if (*scanned_epoch_no > 0 &&
1314 !log_block_epoch_no_is_valid(block_header.m_epoch_no,
1315 *scanned_epoch_no)) {
1316 /* Garbage from a log buffer flush which was made
1317 before the most recent database recovery */
1318
1319 ib::trace_2() << "Scanned ep no: " << *scanned_epoch_no << " block ep no "
1320 << block_header.m_epoch_no;
1321
1322 break;
1323 }
1324
1325 const auto data_len = block_header.m_data_len;
1326
1327 *scanned_epoch_no = block_header.m_epoch_no;
1328 *scanned_lsn += data_len;
1329
1330 *n_bytes_scanned += data_len;
1331
1332 if (data_len < OS_FILE_LOG_BLOCK_SIZE) {
1333 /* Log data ends here */
1334
1335 break;
1336 }
1337 *block_no = no;
1338 }
1339 }
1340
1341 /** Apply a single log record stored in the hash table.
1342 @param[in,out] recv_addr a parsed log record
1343 @param[in,out] block a buffer pool frame for applying the record */
1344 void meb_apply_log_record(recv_addr_t *recv_addr, buf_block_t *block) {
1345 bool found;
1346 const page_id_t page_id(recv_addr->space, recv_addr->page_no);
1347
1348 const page_size_t &page_size =
1349 fil_space_get_page_size(recv_addr->space, &found);
1350
1351 ib::trace_3() << "meb_apply_log_record: recv state " << recv_addr->state
1352 << " space_id " << recv_addr->space << " page_nr "
1353 << recv_addr->page_no << " page size " << page_size << " found "
1354 << found;
1355
1356 if (!found) {
1357 recv_addr->state = RECV_DISCARDED;
1358
1359 mutex_enter(&recv_sys->mutex);
1360
1361 ut_a(recv_sys->n_addrs);
1362 --recv_sys->n_addrs;
1363
1364 mutex_exit(&recv_sys->mutex);
1365
1366 return;
1367 }
1368
1369 mutex_enter(&recv_sys->mutex);
1370
1371 /* We simulate a page read made by the buffer pool, to
1372 make sure the recovery apparatus works ok. We must init
1373 the block. */
1374
1375 meb_page_init(page_id, page_size, block);
1376
1377 /* Extend the tablespace's last file if the page_no
1378 does not fall inside its bounds; we assume the last
1379 file is auto-extending, and mysqlbackup copied the file
1380 when it still was smaller */
1381
1382 fil_space_t *space = fil_space_get(recv_addr->space);
1383
1384 bool success;
1385
1386 success = fil_space_extend(space, recv_addr->page_no + 1);
1387
1388 if (!success) {
1389 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_711)
1390 << "Cannot extend tablespace " << recv_addr->space << " to hold "
1391 << recv_addr->page_no << " pages";
1392 }
1393
1394 mutex_exit(&recv_sys->mutex);
1395
1396 /* Read the page from the tablespace file. */
1397
1398 dberr_t err;
1399
1400 if (page_size.is_compressed()) {
1401 err = fil_io(IORequestRead, true, page_id, page_size, 0,
1402 page_size.physical(), block->page.zip.data, nullptr);
1403
1404 if (err == DB_SUCCESS && !buf_zip_decompress(block, true)) {
1405 ut_error;
1406 }
1407 } else {
1408 err = fil_io(IORequestRead, true, page_id, page_size, 0,
1409 page_size.logical(), block->frame, nullptr);
1410 }
1411
1412 if (err != DB_SUCCESS) {
1413 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_712)
1414 << "Cannot read from tablespace " << recv_addr->space << " page number "
1415 << recv_addr->page_no;
1416 }
1417
1418 apply_log_mutex.lock();
1419
1420 /* Apply the log records to this page */
1421 recv_recover_page(false, block);
1422
1423 apply_log_mutex.unlock();
1424
1425 mutex_enter(&recv_sys->mutex);
1426
1427 /* Write the page back to the tablespace file using the
1428 fil0fil.cc routines */
1429
1430 buf_flush_init_for_writing(block, block->frame, buf_block_get_page_zip(block),
1431 mach_read_from_8(block->frame + FIL_PAGE_LSN),
1432 fsp_is_checksum_disabled(block->page.id.space()),
1433 true /* skip_lsn_check */);
1434
1435 mutex_exit(&recv_sys->mutex);
1436
1437 if (page_size.is_compressed()) {
1438 err = fil_io(IORequestWrite, true, page_id, page_size, 0,
1439 page_size.physical(), block->page.zip.data, nullptr);
1440 } else {
1441 err = fil_io(IORequestWrite, true, page_id, page_size, 0,
1442 page_size.logical(), block->frame, nullptr);
1443 }
1444
1445 if (err != DB_SUCCESS) {
1446 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_713)
1447 << "Cannot write to tablespace " << recv_addr->space << " page number "
1448 << recv_addr->page_no;
1449 }
1450 }
1451
1452 /** Apply a single log record stored in the hash table using default block.
1453 @param[in,out] recv_addr a parsed log record */
1454 void meb_apply_log_rec_func(recv_addr_t *recv_addr) {
1455 meb_apply_log_record(recv_addr, back_block1);
1456 }
1457
1458 /** Dummy wait function for meb_apply_log_recs_via_callback(). */
1459 void meb_nowait_func() { return; }
1460
1461 /** Applies log records in the hash table to a backup. */
1462 void meb_apply_log_recs() {
1463 meb_apply_log_recs_via_callback(meb_apply_log_rec_func, meb_nowait_func);
1464 }
1465
1466 /** Apply all log records in the hash table to a backup using callback
1467 functions. This function employes two callback functions that allow redo
1468 log records to be applied in parallel. The apply_log_record_function
1469 assigns a parsed redo log record for application. The
1470 apply_log_record_function is called repeatedly until all log records in
1471 the hash table are assigned for application. After that the
1472 wait_till_done_function is called once. The wait_till_done_function
1473 function blocks until the application of all the redo log records
1474 previously assigned with apply_log_record_function calls is complete.
1475 Even though this function assigns the log records in the hash table
1476 sequentially, the application of the log records may be done in parallel
1477 if the apply_log_record_function delegates the actual application work
1478 to multiple worker threads running in parallel.
1479 @param[in] apply_log_record_function a function that assigns one redo log
1480 record for application
1481 @param[in] wait_till_done_function a function that blocks until all
1482 assigned redo log records have been applied */
1483 void meb_apply_log_recs_via_callback(
1484 void (*apply_log_record_function)(recv_addr_t *),
1485 void (*wait_till_done_function)()) {
1486 ulint n_hash_cells = recv_sys->n_addrs;
1487 ulint i = 0;
1488
1489 recv_sys->apply_log_recs = true;
1490 recv_sys->apply_batch_on = true;
1491
1492 ib::info(ER_IB_MSG_714) << "Starting to apply a batch of log records to the"
1493 << " database...";
1494
1495 fputs("InnoDB: Progress in percent: ", stderr);
1496
1497 for (const auto &space : *recv_sys->spaces) {
1498 for (auto pages : space.second.m_pages) {
1499 ut_ad(pages.second->space == space.first);
1500
1501 (*apply_log_record_function)(pages.second);
1502 }
1503
1504 ++i;
1505 if ((100 * i) / n_hash_cells != (100 * (i + 1)) / n_hash_cells) {
1506 fprintf(stderr, "%lu ", (ulong)((100 * i) / n_hash_cells));
1507 fflush(stderr);
1508 }
1509 }
1510
1511 /* wait till all the redo log records have been applied */
1512 (*wait_till_done_function)();
1513
1514 /* write logs in next line */
1515 fprintf(stderr, "\n");
1516 recv_sys->apply_log_recs = false;
1517 recv_sys->apply_batch_on = false;
1518 recv_sys_empty_hash();
1519 }
1520
1521 #endif /* !UNIV_HOTBACKUP */
1522
1523 /** Check if redo log is for encryption information.
1524 @param[in] page_no Page number
1525 @param[in] space_id Tablespace identifier
1526 @param[in] start Redo log record body
1527 @param[in] end End of buffer
1528 @return true if encryption information. */
1529 static inline bool check_encryption(page_no_t page_no, space_id_t space_id,
1530 const byte *start, const byte *end) {
1531 /* Only page zero contains encryption metadata. */
1532 if (page_no != 0 || fsp_is_system_or_temp_tablespace(space_id) ||
1533 end < start + 4) {
1534 return false;
1535 }
1536
1537 bool found = false;
1538
1539 const page_size_t &page_size = fil_space_get_page_size(space_id, &found);
1540
1541 if (!found) {
1542 return false;
1543 }
1544
1545 auto encryption_offset = fsp_header_get_encryption_offset(page_size);
1546 auto offset = mach_read_from_2(start);
1547
1548 /* Encryption offset at page 0 is the only way we can identify encryption
1549 information as of today. Ideally we should have a separate redo type. */
1550 if (offset == encryption_offset) {
1551 auto len = mach_read_from_2(start + 2);
1552 ut_ad(len == Encryption::INFO_SIZE);
1553
1554 if (len != Encryption::INFO_SIZE) {
1555 /* purecov: begin inspected */
1556 ib::warn(ER_IB_WRN_ENCRYPTION_INFO_SIZE_MISMATCH, size_t{len},
1557 Encryption::INFO_SIZE);
1558 return false;
1559 /* purecov: end */
1560 }
1561 return true;
1562 }
1563
1564 return false;
1565 }
1566
1567 /** Try to parse a single log record body and also applies it if
1568 specified.
1569 @param[in] type Redo log entry type
1570 @param[in] ptr Redo log record body
1571 @param[in] end_ptr End of buffer
1572 @param[in] space_id Tablespace identifier
1573 @param[in] page_no Page number
1574 @param[in,out] block Buffer block, or nullptr if
1575 a page log record should not be applied
1576 or if it is a MLOG_FILE_ operation
1577 @param[in,out] mtr Mini-transaction, or nullptr if
1578 a page log record should not be applied
1579 @param[in] parsed_bytes Number of bytes parsed so far
1580 @param[in] start_lsn lsn for REDO record
1581 @return log record end, nullptr if not a complete record */
1582 static byte *recv_parse_or_apply_log_rec_body(
1583 mlog_id_t type, byte *ptr, byte *end_ptr, space_id_t space_id,
1584 page_no_t page_no, buf_block_t *block, mtr_t *mtr, ulint parsed_bytes,
1585 lsn_t start_lsn) {
1586 bool applying_redo = (block != nullptr);
1587
1588 switch (type) {
1589 #ifndef UNIV_HOTBACKUP
1590 case MLOG_FILE_DELETE:
1591
1592 return fil_tablespace_redo_delete(
1593 ptr, end_ptr, page_id_t(space_id, page_no), parsed_bytes,
1594 recv_sys->bytes_to_ignore_before_checkpoint != 0);
1595
1596 case MLOG_FILE_CREATE:
1597
1598 return fil_tablespace_redo_create(
1599 ptr, end_ptr, page_id_t(space_id, page_no), parsed_bytes,
1600 recv_sys->bytes_to_ignore_before_checkpoint != 0);
1601
1602 case MLOG_FILE_RENAME:
1603
1604 return fil_tablespace_redo_rename(
1605 ptr, end_ptr, page_id_t(space_id, page_no), parsed_bytes,
1606 recv_sys->bytes_to_ignore_before_checkpoint != 0);
1607
1608 case MLOG_FILE_EXTEND:
1609
1610 return fil_tablespace_redo_extend(
1611 ptr, end_ptr, page_id_t(space_id, page_no), parsed_bytes,
1612 recv_sys->bytes_to_ignore_before_checkpoint != 0);
1613 #else /* !UNIV_HOTBACKUP */
1614 // Mysqlbackup does not execute file operations. It cares for all
1615 // files to be at their final places when it applies the redo log.
1616 // The exception is the restore of an incremental_with_redo_log_only
1617 // backup.
1618 case MLOG_FILE_DELETE:
1619
1620 return fil_tablespace_redo_delete(
1621 ptr, end_ptr, page_id_t(space_id, page_no), parsed_bytes,
1622 !recv_sys->apply_file_operations);
1623
1624 case MLOG_FILE_CREATE:
1625
1626 return fil_tablespace_redo_create(
1627 ptr, end_ptr, page_id_t(space_id, page_no), parsed_bytes,
1628 !recv_sys->apply_file_operations);
1629
1630 case MLOG_FILE_RENAME:
1631
1632 return fil_tablespace_redo_rename(
1633 ptr, end_ptr, page_id_t(space_id, page_no), parsed_bytes,
1634 !recv_sys->apply_file_operations);
1635
1636 case MLOG_FILE_EXTEND:
1637
1638 return fil_tablespace_redo_extend(
1639 ptr, end_ptr, page_id_t(space_id, page_no), parsed_bytes,
1640 !recv_sys->apply_file_operations);
1641 #endif /* !UNIV_HOTBACKUP */
1642
1643 case MLOG_INDEX_LOAD:
1644 #ifdef UNIV_HOTBACKUP
1645 // While scanning redo logs during a backup operation a
1646 // MLOG_INDEX_LOAD type redo log record indicates, that a DDL
1647 // (create index, alter table...) is performed with
1648 // 'algorithm=inplace'. The affected tablespace must be re-copied
1649 // in the backup lock phase. Record it in the index_load_list.
1650 if (!recv_recovery_on) {
1651 index_load_list.emplace_back(
1652 std::pair<space_id_t, lsn_t>(space_id, recv_sys->recovered_lsn));
1653 }
1654 #endif /* UNIV_HOTBACKUP */
1655 if (end_ptr < ptr + 8) {
1656 return nullptr;
1657 }
1658
1659 return ptr + 8;
1660
1661 case MLOG_WRITE_STRING:
1662
1663 #ifdef UNIV_HOTBACKUP
1664 if (recv_recovery_on && meb_is_space_loaded(space_id)) {
1665 #endif /* UNIV_HOTBACKUP */
1666 /* For encrypted tablespace, we need to get the encryption key
1667 information before the page 0 is recovered. Otherwise, redo will not
1668 find the key to decrypt the data pages. */
1669 if (page_no == 0 && !applying_redo &&
1670 !fsp_is_system_or_temp_tablespace(space_id) &&
1671 /* For cloned db header page has the encryption information. */
1672 !recv_sys->is_cloned_db) {
1673 ut_ad(LSN_MAX != start_lsn);
1674 return fil_tablespace_redo_encryption(ptr, end_ptr, space_id,
1675 start_lsn);
1676 }
1677 #ifdef UNIV_HOTBACKUP
1678 }
1679 #endif /* UNIV_HOTBACKUP */
1680
1681 break;
1682
1683 default:
1684 break;
1685 }
1686
1687 page_t *page;
1688 page_zip_des_t *page_zip;
1689 dict_index_t *index = nullptr;
1690
1691 #ifdef UNIV_DEBUG
1692 ulint page_type;
1693 #endif /* UNIV_DEBUG */
1694
1695 #if defined(UNIV_HOTBACKUP) && defined(UNIV_DEBUG)
1696 ib::trace_3() << "recv_parse_or_apply_log_rec_body: type "
1697 << get_mlog_string(type) << " space_id " << space_id
1698 << " page_nr " << page_no << " ptr "
1699 << static_cast<const void *>(ptr) << " end_ptr "
1700 << static_cast<const void *>(end_ptr) << " block "
1701 << static_cast<const void *>(block) << " mtr "
1702 << static_cast<const void *>(mtr);
1703 #endif /* UNIV_HOTBACKUP && UNIV_DEBUG */
1704
1705 if (applying_redo) {
1706 /* Applying a page log record. */
1707 ut_ad(mtr != nullptr);
1708
1709 page = block->frame;
1710 page_zip = buf_block_get_page_zip(block);
1711
1712 ut_d(page_type = fil_page_get_type(page));
1713 #if defined(UNIV_HOTBACKUP) && defined(UNIV_DEBUG)
1714 if (page_type == 0) {
1715 meb_print_page_header(page);
1716 }
1717 #endif /* UNIV_HOTBACKUP && UNIV_DEBUG */
1718
1719 } else {
1720 /* Parsing a page log record. */
1721 ut_ad(mtr == nullptr);
1722 page = nullptr;
1723 page_zip = nullptr;
1724
1725 ut_d(page_type = FIL_PAGE_TYPE_ALLOCATED);
1726 }
1727
1728 const byte *old_ptr = ptr;
1729
1730 switch (type) {
1731 #ifdef UNIV_LOG_LSN_DEBUG
1732 case MLOG_LSN:
1733 /* The LSN is checked in recv_parse_log_rec(). */
1734 break;
1735 #endif /* UNIV_LOG_LSN_DEBUG */
1736 case MLOG_4BYTES:
1737
1738 ut_ad(page == nullptr || end_ptr > ptr + 2);
1739
1740 /* Most FSP flags can only be changed by CREATE or ALTER with
1741 ALGORITHM=COPY, so they do not change once the file
1742 is created. The SDI flag is the only one that can be
1743 changed by a recoverable transaction. So if there is
1744 change in FSP flags, update the in-memory space structure
1745 (fil_space_t) */
1746
1747 if (page != nullptr && page_no == 0 &&
1748 mach_read_from_2(ptr) == FSP_HEADER_OFFSET + FSP_SPACE_FLAGS) {
1749 ptr = mlog_parse_nbytes(MLOG_4BYTES, ptr, end_ptr, page, page_zip);
1750
1751 /* When applying log, we have complete records.
1752 They can be incomplete (ptr=nullptr) only during
1753 scanning (page==nullptr) */
1754
1755 ut_ad(ptr != nullptr);
1756
1757 fil_space_t *space = fil_space_acquire(space_id);
1758
1759 ut_ad(space != nullptr);
1760
1761 fil_space_set_flags(space, mach_read_from_4(FSP_HEADER_OFFSET +
1762 FSP_SPACE_FLAGS + page));
1763 fil_space_release(space);
1764
1765 break;
1766 }
1767
1768 [[fallthrough]];
1769
1770 case MLOG_1BYTE:
1771 /* If 'ALTER TABLESPACE ... ENCRYPTION' was in progress and page 0 has
1772 REDO entry for this, now while applying this entry, set
1773 encryption_op_in_progress flag now so that any other page of this
1774 tablespace in redo log is written accordingly. */
1775 if (page_no == 0 && page != nullptr && end_ptr >= ptr + 2) {
1776 ulint offs = mach_read_from_2(ptr);
1777
1778 fil_space_t *space = fil_space_acquire(space_id);
1779 ut_ad(space != nullptr);
1780 ulint offset = fsp_header_get_encryption_progress_offset(
1781 page_size_t(space->flags));
1782
1783 if (offs == offset) {
1784 ptr = mlog_parse_nbytes(MLOG_1BYTE, ptr, end_ptr, page, page_zip);
1785 byte op = mach_read_from_1(page + offset);
1786 switch (op) {
1787 case Encryption::ENCRYPT_IN_PROGRESS:
1788 space->encryption_op_in_progress =
1789 Encryption::Progress::ENCRYPTION;
1790 break;
1791 case Encryption::DECRYPT_IN_PROGRESS:
1792 space->encryption_op_in_progress =
1793 Encryption::Progress::DECRYPTION;
1794 break;
1795 default:
1796 space->encryption_op_in_progress = Encryption::Progress::NONE;
1797 break;
1798 }
1799 }
1800 fil_space_release(space);
1801 }
1802
1803 [[fallthrough]];
1804
1805 case MLOG_2BYTES:
1806 case MLOG_8BYTES:
1807 #ifdef UNIV_DEBUG
1808 if (page && page_type == FIL_PAGE_TYPE_ALLOCATED && end_ptr >= ptr + 2) {
1809 /* It is OK to set FIL_PAGE_TYPE and certain
1810 list node fields on an empty page. Any other
1811 write is not OK. */
1812
1813 /* NOTE: There may be bogus assertion failures for
1814 dict_hdr_create(), trx_rseg_header_create(),
1815 trx_sys_create_doublewrite_buf(), and
1816 trx_sysf_create().
1817 These are only called during database creation. */
1818
1819 ulint offs = mach_read_from_2(ptr);
1820
1821 switch (type) {
1822 default:
1823 ut_error;
1824 case MLOG_2BYTES:
1825 /* Note that this can fail when the
1826 redo log been written with something
1827 older than InnoDB Plugin 1.0.4. */
1828 ut_ad(
1829 offs == FIL_PAGE_TYPE ||
1830 offs == IBUF_TREE_SEG_HEADER + IBUF_HEADER + FSEG_HDR_OFFSET ||
1831 offs == PAGE_BTR_IBUF_FREE_LIST + PAGE_HEADER + FIL_ADDR_BYTE ||
1832 offs == PAGE_BTR_IBUF_FREE_LIST + PAGE_HEADER + FIL_ADDR_BYTE +
1833 FIL_ADDR_SIZE ||
1834 offs == PAGE_BTR_SEG_LEAF + PAGE_HEADER + FSEG_HDR_OFFSET ||
1835 offs == PAGE_BTR_SEG_TOP + PAGE_HEADER + FSEG_HDR_OFFSET ||
1836 offs == PAGE_BTR_IBUF_FREE_LIST_NODE + PAGE_HEADER +
1837 FIL_ADDR_BYTE + 0 /*FLST_PREV*/
1838 || offs == PAGE_BTR_IBUF_FREE_LIST_NODE + PAGE_HEADER +
1839 FIL_ADDR_BYTE + FIL_ADDR_SIZE /*FLST_NEXT*/);
1840 break;
1841 case MLOG_4BYTES:
1842 /* Note that this can fail when the
1843 redo log been written with something
1844 older than InnoDB Plugin 1.0.4. */
1845 ut_ad(
1846 0 ||
1847 offs == IBUF_TREE_SEG_HEADER + IBUF_HEADER + FSEG_HDR_SPACE ||
1848 offs == IBUF_TREE_SEG_HEADER + IBUF_HEADER + FSEG_HDR_PAGE_NO ||
1849 offs == PAGE_BTR_IBUF_FREE_LIST + PAGE_HEADER /* flst_init */
1850 ||
1851 offs == PAGE_BTR_IBUF_FREE_LIST + PAGE_HEADER + FIL_ADDR_PAGE ||
1852 offs == PAGE_BTR_IBUF_FREE_LIST + PAGE_HEADER + FIL_ADDR_PAGE +
1853 FIL_ADDR_SIZE ||
1854 offs == PAGE_BTR_SEG_LEAF + PAGE_HEADER + FSEG_HDR_PAGE_NO ||
1855 offs == PAGE_BTR_SEG_LEAF + PAGE_HEADER + FSEG_HDR_SPACE ||
1856 offs == PAGE_BTR_SEG_TOP + PAGE_HEADER + FSEG_HDR_PAGE_NO ||
1857 offs == PAGE_BTR_SEG_TOP + PAGE_HEADER + FSEG_HDR_SPACE ||
1858 offs == PAGE_BTR_IBUF_FREE_LIST_NODE + PAGE_HEADER +
1859 FIL_ADDR_PAGE + 0 /*FLST_PREV*/
1860 || offs == PAGE_BTR_IBUF_FREE_LIST_NODE + PAGE_HEADER +
1861 FIL_ADDR_PAGE + FIL_ADDR_SIZE /*FLST_NEXT*/);
1862 break;
1863 }
1864 }
1865 #endif /* UNIV_DEBUG */
1866
1867 ptr = mlog_parse_nbytes(type, ptr, end_ptr, page, page_zip);
1868
1869 if (ptr != nullptr && page != nullptr && page_no == 0 &&
1870 type == MLOG_4BYTES) {
1871 ulint offs = mach_read_from_2(old_ptr);
1872
1873 switch (offs) {
1874 fil_space_t *space;
1875 uint32_t val;
1876 default:
1877 break;
1878
1879 case FSP_HEADER_OFFSET + FSP_SPACE_FLAGS:
1880 case FSP_HEADER_OFFSET + FSP_SIZE:
1881 case FSP_HEADER_OFFSET + FSP_FREE_LIMIT:
1882 case FSP_HEADER_OFFSET + FSP_FREE + FLST_LEN:
1883
1884 space = fil_space_get(space_id);
1885
1886 ut_a(space != nullptr);
1887
1888 val = mach_read_from_4(page + offs);
1889
1890 switch (offs) {
1891 case FSP_HEADER_OFFSET + FSP_SPACE_FLAGS:
1892 space->flags = val;
1893 break;
1894
1895 case FSP_HEADER_OFFSET + FSP_SIZE:
1896
1897 space->size_in_header = val;
1898
1899 if (space->size >= val) {
1900 break;
1901 }
1902
1903 ib::info(ER_IB_MSG_718, ulong{space->id}, space->name,
1904 ulong{val});
1905
1906 if (fil_space_extend(space, val)) {
1907 break;
1908 }
1909
1910 ib::error(ER_IB_MSG_719, ulong{space->id}, space->name,
1911 ulong{val});
1912 break;
1913
1914 case FSP_HEADER_OFFSET + FSP_FREE_LIMIT:
1915 space->free_limit = val;
1916 break;
1917
1918 case FSP_HEADER_OFFSET + FSP_FREE + FLST_LEN:
1919 space->free_len = val;
1920 ut_ad(val == flst_get_len(page + offs));
1921 break;
1922 }
1923 }
1924 }
1925 break;
1926
1927 case MLOG_REC_INSERT:
1928
1929 ut_ad(!page || fil_page_type_is_index(page_type));
1930
1931 if (nullptr != (ptr = mlog_parse_index(ptr, end_ptr, &index))) {
1932 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
1933
1934 ptr = page_cur_parse_insert_rec(false, ptr, end_ptr, block, index, mtr);
1935 }
1936 break;
1937
1938 case MLOG_REC_INSERT_8027:
1939 case MLOG_COMP_REC_INSERT_8027:
1940
1941 ut_ad(!page || fil_page_type_is_index(page_type));
1942
1943 if (nullptr !=
1944 (ptr = mlog_parse_index_8027(
1945 ptr, end_ptr, type == MLOG_COMP_REC_INSERT_8027, &index))) {
1946 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
1947
1948 ptr = page_cur_parse_insert_rec(false, ptr, end_ptr, block, index, mtr);
1949 }
1950 break;
1951
1952 case MLOG_REC_CLUST_DELETE_MARK:
1953
1954 ut_ad(!page || fil_page_type_is_index(page_type));
1955
1956 if (nullptr != (ptr = mlog_parse_index(ptr, end_ptr, &index))) {
1957 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
1958
1959 ptr = btr_cur_parse_del_mark_set_clust_rec(ptr, end_ptr, page, page_zip,
1960 index);
1961 }
1962
1963 break;
1964
1965 case MLOG_REC_CLUST_DELETE_MARK_8027:
1966 case MLOG_COMP_REC_CLUST_DELETE_MARK_8027:
1967
1968 ut_ad(!page || fil_page_type_is_index(page_type));
1969
1970 if (nullptr !=
1971 (ptr = mlog_parse_index_8027(
1972 ptr, end_ptr, type == MLOG_COMP_REC_CLUST_DELETE_MARK_8027,
1973 &index))) {
1974 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
1975
1976 ptr = btr_cur_parse_del_mark_set_clust_rec(ptr, end_ptr, page, page_zip,
1977 index);
1978 }
1979
1980 break;
1981
1982 case MLOG_COMP_REC_SEC_DELETE_MARK:
1983
1984 ut_ad(!page || fil_page_type_is_index(page_type));
1985
1986 /* This log record type is obsolete, but we process it for
1987 backward compatibility with MySQL 5.0.3 and 5.0.4. */
1988
1989 ut_a(!page || page_is_comp(page));
1990 ut_a(!page_zip);
1991
1992 ptr = mlog_parse_index_8027(ptr, end_ptr, true, &index);
1993
1994 if (ptr == nullptr) {
1995 break;
1996 }
1997
1998 [[fallthrough]];
1999
2000 case MLOG_REC_SEC_DELETE_MARK:
2001
2002 ut_ad(!page || fil_page_type_is_index(page_type));
2003
2004 ptr = btr_cur_parse_del_mark_set_sec_rec(ptr, end_ptr, page, page_zip);
2005 break;
2006
2007 case MLOG_REC_UPDATE_IN_PLACE:
2008
2009 ut_ad(!page || fil_page_type_is_index(page_type));
2010
2011 if (nullptr != (ptr = mlog_parse_index(ptr, end_ptr, &index))) {
2012 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
2013
2014 ptr =
2015 btr_cur_parse_update_in_place(ptr, end_ptr, page, page_zip, index);
2016 }
2017
2018 break;
2019
2020 case MLOG_REC_UPDATE_IN_PLACE_8027:
2021 case MLOG_COMP_REC_UPDATE_IN_PLACE_8027:
2022
2023 ut_ad(!page || fil_page_type_is_index(page_type));
2024
2025 if (nullptr !=
2026 (ptr = mlog_parse_index_8027(
2027 ptr, end_ptr, type == MLOG_COMP_REC_UPDATE_IN_PLACE_8027,
2028 &index))) {
2029 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
2030
2031 ptr =
2032 btr_cur_parse_update_in_place(ptr, end_ptr, page, page_zip, index);
2033 }
2034
2035 break;
2036
2037 case MLOG_LIST_END_DELETE:
2038 case MLOG_LIST_START_DELETE:
2039
2040 ut_ad(!page || fil_page_type_is_index(page_type));
2041
2042 if (nullptr != (ptr = mlog_parse_index(ptr, end_ptr, &index))) {
2043 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
2044
2045 ptr = page_parse_delete_rec_list(type, ptr, end_ptr, block, index, mtr);
2046 }
2047
2048 break;
2049
2050 case MLOG_LIST_END_DELETE_8027:
2051 case MLOG_COMP_LIST_END_DELETE_8027:
2052 case MLOG_LIST_START_DELETE_8027:
2053 case MLOG_COMP_LIST_START_DELETE_8027:
2054
2055 ut_ad(!page || fil_page_type_is_index(page_type));
2056
2057 if (nullptr != (ptr = mlog_parse_index_8027(
2058 ptr, end_ptr,
2059 type == MLOG_COMP_LIST_END_DELETE_8027 ||
2060 type == MLOG_COMP_LIST_START_DELETE_8027,
2061 &index))) {
2062 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
2063
2064 ptr = page_parse_delete_rec_list(type, ptr, end_ptr, block, index, mtr);
2065 }
2066
2067 break;
2068
2069 case MLOG_LIST_END_COPY_CREATED:
2070
2071 ut_ad(!page || fil_page_type_is_index(page_type));
2072
2073 if (nullptr != (ptr = mlog_parse_index(ptr, end_ptr, &index))) {
2074 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
2075
2076 ptr = page_parse_copy_rec_list_to_created_page(ptr, end_ptr, block,
2077 index, mtr);
2078 }
2079
2080 break;
2081
2082 case MLOG_LIST_END_COPY_CREATED_8027:
2083 case MLOG_COMP_LIST_END_COPY_CREATED_8027:
2084
2085 ut_ad(!page || fil_page_type_is_index(page_type));
2086
2087 if (nullptr !=
2088 (ptr = mlog_parse_index_8027(
2089 ptr, end_ptr, type == MLOG_COMP_LIST_END_COPY_CREATED_8027,
2090 &index))) {
2091 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
2092
2093 ptr = page_parse_copy_rec_list_to_created_page(ptr, end_ptr, block,
2094 index, mtr);
2095 }
2096
2097 break;
2098
2099 case MLOG_PAGE_REORGANIZE:
2100
2101 ut_ad(!page || fil_page_type_is_index(page_type));
2102
2103 if (nullptr != (ptr = mlog_parse_index(ptr, end_ptr, &index))) {
2104 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
2105
2106 ptr = btr_parse_page_reorganize(ptr, end_ptr, index,
2107 type == MLOG_ZIP_PAGE_REORGANIZE_8027,
2108 block, mtr);
2109 }
2110
2111 break;
2112
2113 case MLOG_PAGE_REORGANIZE_8027:
2114 ut_ad(!page || fil_page_type_is_index(page_type));
2115 /* Uncompressed pages don't have any payload in the
2116 MTR so ptr and end_ptr can be, and are nullptr */
2117 mlog_parse_index_8027(ptr, end_ptr, false, &index);
2118 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
2119
2120 ptr = btr_parse_page_reorganize(ptr, end_ptr, index, false, block, mtr);
2121
2122 break;
2123
2124 case MLOG_ZIP_PAGE_REORGANIZE:
2125
2126 ut_ad(!page || fil_page_type_is_index(page_type));
2127
2128 if (nullptr != (ptr = mlog_parse_index(ptr, end_ptr, &index))) {
2129 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
2130
2131 ptr = btr_parse_page_reorganize(ptr, end_ptr, index, true, block, mtr);
2132 }
2133
2134 break;
2135
2136 case MLOG_COMP_PAGE_REORGANIZE_8027:
2137 case MLOG_ZIP_PAGE_REORGANIZE_8027:
2138
2139 ut_ad(!page || fil_page_type_is_index(page_type));
2140
2141 if (nullptr !=
2142 (ptr = mlog_parse_index_8027(ptr, end_ptr, true, &index))) {
2143 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
2144
2145 ptr = btr_parse_page_reorganize(ptr, end_ptr, index,
2146 type == MLOG_ZIP_PAGE_REORGANIZE_8027,
2147 block, mtr);
2148 }
2149
2150 break;
2151
2152 case MLOG_PAGE_CREATE:
2153 case MLOG_COMP_PAGE_CREATE:
2154
2155 /* Allow anything in page_type when creating a page. */
2156 ut_a(!page_zip);
2157
2158 page_parse_create(block, type == MLOG_COMP_PAGE_CREATE, FIL_PAGE_INDEX);
2159
2160 break;
2161
2162 case MLOG_PAGE_CREATE_RTREE:
2163 case MLOG_COMP_PAGE_CREATE_RTREE:
2164
2165 page_parse_create(block, type == MLOG_COMP_PAGE_CREATE_RTREE,
2166 FIL_PAGE_RTREE);
2167
2168 break;
2169
2170 case MLOG_PAGE_CREATE_SDI:
2171 case MLOG_COMP_PAGE_CREATE_SDI:
2172
2173 page_parse_create(block, type == MLOG_COMP_PAGE_CREATE_SDI, FIL_PAGE_SDI);
2174
2175 break;
2176
2177 case MLOG_UNDO_INSERT:
2178
2179 ut_ad(!page || page_type == FIL_PAGE_UNDO_LOG);
2180
2181 ptr = trx_undo_parse_add_undo_rec(ptr, end_ptr, page);
2182
2183 break;
2184
2185 case MLOG_UNDO_ERASE_END:
2186
2187 ut_ad(!page || page_type == FIL_PAGE_UNDO_LOG);
2188
2189 ptr = trx_undo_parse_erase_page_end(ptr, end_ptr, page, mtr);
2190
2191 break;
2192
2193 case MLOG_UNDO_INIT:
2194
2195 /* Allow anything in page_type when creating a page. */
2196
2197 ptr = trx_undo_parse_page_init(ptr, end_ptr, page, mtr);
2198
2199 break;
2200 case MLOG_UNDO_HDR_CREATE:
2201 case MLOG_UNDO_HDR_REUSE:
2202
2203 ut_ad(!page || page_type == FIL_PAGE_UNDO_LOG);
2204
2205 ptr = trx_undo_parse_page_header(type, ptr, end_ptr, page, mtr);
2206
2207 break;
2208
2209 case MLOG_REC_MIN_MARK:
2210 case MLOG_COMP_REC_MIN_MARK:
2211
2212 ut_ad(!page || fil_page_type_is_index(page_type));
2213
2214 /* On a compressed page, MLOG_COMP_REC_MIN_MARK
2215 will be followed by MLOG_COMP_REC_DELETE
2216 or MLOG_ZIP_WRITE_HEADER(FIL_PAGE_PREV, FIL_nullptr)
2217 in the same mini-transaction. */
2218
2219 ut_a(type == MLOG_COMP_REC_MIN_MARK || !page_zip);
2220
2221 ptr = btr_parse_set_min_rec_mark(
2222 ptr, end_ptr, type == MLOG_COMP_REC_MIN_MARK, page, mtr);
2223
2224 break;
2225
2226 case MLOG_REC_DELETE:
2227
2228 ut_ad(!page || fil_page_type_is_index(page_type));
2229
2230 if (nullptr != (ptr = mlog_parse_index(ptr, end_ptr, &index))) {
2231 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
2232
2233 ptr = page_cur_parse_delete_rec(ptr, end_ptr, block, index, mtr);
2234 }
2235
2236 break;
2237
2238 case MLOG_REC_DELETE_8027:
2239 case MLOG_COMP_REC_DELETE_8027:
2240
2241 ut_ad(!page || fil_page_type_is_index(page_type));
2242
2243 if (nullptr !=
2244 (ptr = mlog_parse_index_8027(
2245 ptr, end_ptr, type == MLOG_COMP_REC_DELETE_8027, &index))) {
2246 ut_a(!page || page_is_comp(page) == dict_table_is_comp(index->table));
2247
2248 ptr = page_cur_parse_delete_rec(ptr, end_ptr, block, index, mtr);
2249 }
2250
2251 break;
2252
2253 case MLOG_IBUF_BITMAP_INIT:
2254
2255 /* Allow anything in page_type when creating a page. */
2256
2257 ptr = ibuf_parse_bitmap_init(ptr, end_ptr, block, mtr);
2258
2259 break;
2260
2261 case MLOG_INIT_FILE_PAGE:
2262 case MLOG_INIT_FILE_PAGE2: {
2263 /* For clone, avoid initializing page-0. Page-0 should already have been
2264 initialized. This is to avoid erasing encryption information. We cannot
2265 update encryption information later with redo logged information for
2266 clone. Please check comments in MLOG_WRITE_STRING. */
2267 bool skip_init = (recv_sys->is_cloned_db && page_no == 0);
2268
2269 if (!skip_init) {
2270 /* Allow anything in page_type when creating a page. */
2271 ptr = fsp_parse_init_file_page(ptr, end_ptr, block);
2272 }
2273 break;
2274 }
2275
2276 case MLOG_WRITE_STRING: {
2277 ut_ad(!page || page_type != FIL_PAGE_TYPE_ALLOCATED || page_no == 0);
2278 bool is_encryption = check_encryption(page_no, space_id, ptr, end_ptr);
2279
2280 #ifndef UNIV_HOTBACKUP
2281 /* Reset in-mem encryption information for the tablespace here if this
2282 is "resetting encryprion info" log. */
2283 if (is_encryption && !recv_sys->is_cloned_db) {
2284 byte buf[Encryption::INFO_SIZE] = {0};
2285
2286 if (memcmp(ptr + 4, buf, Encryption::INFO_SIZE - 4) == 0) {
2287 ut_a(DB_SUCCESS == fil_reset_encryption(space_id));
2288 }
2289 }
2290
2291 #endif
2292 auto apply_page = page;
2293
2294 /* For clone recovery, skip applying encryption information from
2295 redo log. It is already updated in page 0. Redo log encryption
2296 information is encrypted with donor master key and must be ignored. */
2297 if (recv_sys->is_cloned_db && is_encryption) {
2298 apply_page = nullptr;
2299 }
2300
2301 ptr = mlog_parse_string(ptr, end_ptr, apply_page, page_zip);
2302 break;
2303 }
2304
2305 case MLOG_ZIP_WRITE_NODE_PTR:
2306
2307 ut_ad(!page || fil_page_type_is_index(page_type));
2308
2309 ptr = page_zip_parse_write_node_ptr(ptr, end_ptr, page, page_zip);
2310
2311 break;
2312
2313 case MLOG_ZIP_WRITE_BLOB_PTR:
2314
2315 ut_ad(!page || fil_page_type_is_index(page_type));
2316
2317 ptr = page_zip_parse_write_blob_ptr(ptr, end_ptr, page, page_zip);
2318
2319 break;
2320
2321 case MLOG_ZIP_WRITE_HEADER:
2322
2323 ut_ad(!page || fil_page_type_is_index(page_type));
2324
2325 ptr = page_zip_parse_write_header(ptr, end_ptr, page, page_zip);
2326
2327 break;
2328
2329 case MLOG_ZIP_PAGE_COMPRESS:
2330
2331 /* Allow anything in page_type when creating a page. */
2332 ptr = page_zip_parse_compress(ptr, end_ptr, page, page_zip);
2333 break;
2334
2335 case MLOG_ZIP_PAGE_COMPRESS_NO_DATA:
2336
2337 if (nullptr != (ptr = mlog_parse_index(ptr, end_ptr, &index))) {
2338 ut_a(!page || (page_is_comp(page) == dict_table_is_comp(index->table)));
2339
2340 ptr = page_zip_parse_compress_no_data(ptr, end_ptr, page, page_zip,
2341 index);
2342 }
2343
2344 break;
2345
2346 case MLOG_ZIP_PAGE_COMPRESS_NO_DATA_8027:
2347
2348 if (nullptr !=
2349 (ptr = mlog_parse_index_8027(ptr, end_ptr, true, &index))) {
2350 ut_a(!page || (page_is_comp(page) == dict_table_is_comp(index->table)));
2351
2352 ptr = page_zip_parse_compress_no_data(ptr, end_ptr, page, page_zip,
2353 index);
2354 }
2355
2356 break;
2357
2358 case MLOG_TEST:
2359 #ifndef UNIV_HOTBACKUP
2360 if (log_test != nullptr) {
2361 ptr = log_test->parse_mlog_rec(ptr, end_ptr);
2362 } else {
2363 /* Just parse and ignore record to pass it and go forward. Note that
2364 this record is also used in the innodb.log_first_rec_group mtr test.
2365 The record is written in the buf0flu.cc when flushing page in that
2366 case. */
2367 Log_test::Key key;
2368 Log_test::Value value;
2369 lsn_t start_lsn, end_lsn;
2370
2371 ptr = Log_test::parse_mlog_rec(ptr, end_ptr, key, value, start_lsn,
2372 end_lsn);
2373 }
2374 break;
2375 #endif /* !UNIV_HOTBACKUP */
2376 /* Fall through. */
2377
2378 default:
2379 ptr = nullptr;
2380 recv_sys->found_corrupt_log = true;
2381 }
2382
2383 if (index != nullptr) {
2384 dict_table_t *table = index->table;
2385
2386 dict_mem_index_free(index);
2387 dict_mem_table_free(table);
2388 }
2389
2390 return ptr;
2391 }
2392
2393 /** Adds a new log record to the hash table of log records.
2394 @param[in] type log record type
2395 @param[in] space_id Tablespace id
2396 @param[in] page_no page number
2397 @param[in] body log record body
2398 @param[in] rec_end log record end
2399 @param[in] start_lsn start lsn of the mtr
2400 @param[in] end_lsn end lsn of the mtr */
2401 static void recv_add_to_hash_table(mlog_id_t type, space_id_t space_id,
2402 page_no_t page_no, byte *body, byte *rec_end,
2403 lsn_t start_lsn, lsn_t end_lsn) {
2404 ut_ad(type != MLOG_FILE_DELETE);
2405 ut_ad(type != MLOG_FILE_CREATE);
2406 ut_ad(type != MLOG_FILE_RENAME);
2407 ut_ad(type != MLOG_FILE_EXTEND);
2408 ut_ad(type != MLOG_DUMMY_RECORD);
2409 ut_ad(type != MLOG_INDEX_LOAD);
2410
2411 recv_sys_t::Space *space;
2412
2413 space = recv_get_page_map(space_id, true);
2414
2415 recv_t *recv;
2416
2417 recv = static_cast<recv_t *>(mem_heap_alloc(space->m_heap, sizeof(*recv)));
2418
2419 recv->type = type;
2420 recv->end_lsn = end_lsn;
2421 recv->len = rec_end - body;
2422 recv->start_lsn = start_lsn;
2423
2424 auto it = space->m_pages.find(page_no);
2425
2426 recv_addr_t *recv_addr;
2427
2428 if (it != space->m_pages.end()) {
2429 recv_addr = it->second;
2430
2431 } else {
2432 recv_addr = static_cast<recv_addr_t *>(
2433 mem_heap_alloc(space->m_heap, sizeof(*recv_addr)));
2434
2435 recv_addr->space = space_id;
2436 recv_addr->page_no = page_no;
2437 recv_addr->state = RECV_NOT_PROCESSED;
2438
2439 UT_LIST_INIT(recv_addr->rec_list);
2440
2441 using Value = recv_sys_t::Pages::value_type;
2442
2443 space->m_pages.insert(it, Value{page_no, recv_addr});
2444
2445 ++recv_sys->n_addrs;
2446 }
2447
2448 UT_LIST_ADD_LAST(recv_addr->rec_list, recv);
2449
2450 recv_data_t **prev_field;
2451
2452 prev_field = &recv->data;
2453
2454 /* Store the log record body in chunks of less than UNIV_PAGE_SIZE:
2455 the heap grows into the buffer pool, and bigger chunks could not
2456 be allocated */
2457
2458 while (rec_end > body) {
2459 ulint len = rec_end - body;
2460
2461 if (len > RECV_DATA_BLOCK_SIZE) {
2462 len = RECV_DATA_BLOCK_SIZE;
2463 }
2464
2465 recv_data_t *recv_data;
2466
2467 recv_data = static_cast<recv_data_t *>(
2468 mem_heap_alloc(space->m_heap, sizeof(*recv_data) + len));
2469
2470 *prev_field = recv_data;
2471
2472 memcpy(recv_data + 1, body, len);
2473
2474 prev_field = &recv_data->next;
2475
2476 body += len;
2477 }
2478
2479 *prev_field = nullptr;
2480 }
2481
2482 /** Copies the log record body from recv to buf.
2483 @param[in] buf Buffer of length at least recv->len
2484 @param[in] recv Log record */
2485 static void recv_data_copy_to_buf(byte *buf, recv_t *recv) {
2486 ulint len = recv->len;
2487 recv_data_t *recv_data = recv->data;
2488
2489 while (len > 0) {
2490 ulint part_len;
2491
2492 if (len > RECV_DATA_BLOCK_SIZE) {
2493 part_len = RECV_DATA_BLOCK_SIZE;
2494 } else {
2495 part_len = len;
2496 }
2497
2498 memcpy(buf, ((byte *)recv_data) + sizeof(*recv_data), part_len);
2499
2500 buf += part_len;
2501 len -= part_len;
2502
2503 recv_data = recv_data->next;
2504 }
2505 }
2506
2507 bool recv_page_is_brand_new(buf_block_t *block) {
2508 mutex_enter(&recv_sys->mutex);
2509
2510 recv_addr_t *recv_addr;
2511 recv_addr = recv_get_rec(block->page.id.space(), block->page.id.page_no());
2512 if (recv_addr == nullptr) {
2513 /* no redo log treated as brand new */
2514 mutex_exit(&recv_sys->mutex);
2515 return true;
2516 }
2517
2518 auto recv = UT_LIST_GET_FIRST(recv_addr->rec_list);
2519 if (recv == nullptr) {
2520 /* no redo log treated as brand new */
2521 mutex_exit(&recv_sys->mutex);
2522 return true;
2523 }
2524 if (recv->type == MLOG_INIT_FILE_PAGE2 || recv->type == MLOG_INIT_FILE_PAGE) {
2525 mutex_exit(&recv_sys->mutex);
2526 return true;
2527 }
2528
2529 mutex_exit(&recv_sys->mutex);
2530 return false;
2531 }
2532
2533 /** Applies the hashed log records to the page, if the page lsn is less than
2534 the lsn of a log record. This can be called when a buffer page has just been
2535 read in, or also for a page already in the buffer pool.
2536
2537 @param[in] just_read_in true if the IO handler calls this for a freshly
2538 read page
2539 @param[in,out] block buffer block */
2540 void recv_recover_page_func(
2541 #ifndef UNIV_HOTBACKUP
2542 bool just_read_in,
2543 #endif /* !UNIV_HOTBACKUP */
2544 buf_block_t *block) {
2545 mutex_enter(&recv_sys->mutex);
2546
2547 if (recv_sys->apply_log_recs == false) {
2548 /* Log records should not be applied now */
2549
2550 mutex_exit(&recv_sys->mutex);
2551
2552 return;
2553 }
2554
2555 recv_addr_t *recv_addr;
2556
2557 recv_addr = recv_get_rec(block->page.id.space(), block->page.id.page_no());
2558
2559 if (recv_addr == nullptr || recv_addr->state == RECV_BEING_PROCESSED ||
2560 recv_addr->state == RECV_PROCESSED) {
2561 #ifndef UNIV_HOTBACKUP
2562 ut_ad(recv_addr == nullptr || recv_needed_recovery ||
2563 recv_sys->scanned_lsn < recv_sys->checkpoint_lsn);
2564 #endif /* !UNIV_HOTBACKUP */
2565
2566 mutex_exit(&recv_sys->mutex);
2567
2568 return;
2569 }
2570
2571 #ifndef UNIV_HOTBACKUP
2572 /* The following block is the scope of usage of the following bpage object
2573 reference.*/
2574 {
2575 buf_page_t &bpage = block->page;
2576
2577 if (!fsp_is_system_temporary(bpage.id.space()) &&
2578 (arch_page_sys != nullptr && arch_page_sys->is_active())) {
2579 page_t *frame;
2580 lsn_t frame_lsn;
2581
2582 frame = bpage.zip.data;
2583
2584 if (!frame) {
2585 frame = block->frame;
2586 }
2587 frame_lsn = mach_read_from_8(frame + FIL_PAGE_LSN);
2588
2589 arch_page_sys->track_page(&bpage, LSN_MAX, frame_lsn, true);
2590 }
2591 }
2592 #endif /* !UNIV_HOTBACKUP */
2593
2594 #ifndef UNIV_HOTBACKUP
2595 /* this is explicitly false in case of meb, skip the assert */
2596 ut_ad(recv_needed_recovery ||
2597 recv_sys->scanned_lsn < recv_sys->checkpoint_lsn);
2598
2599 DBUG_PRINT("ib_log", ("Applying log to page %u:%u", recv_addr->space,
2600 recv_addr->page_no));
2601
2602 #ifdef UNIV_DEBUG
2603 lsn_t max_lsn;
2604
2605 ut_d(max_lsn = log_sys->m_scanned_lsn);
2606 #endif /* UNIV_DEBUG */
2607 #else /* !UNIV_HOTBACKUP */
2608 ib::trace_2() << "Applying log to space_id " << recv_addr->space
2609 << " page_nr " << recv_addr->page_no;
2610 #endif /* !UNIV_HOTBACKUP */
2611
2612 recv_addr->state = RECV_BEING_PROCESSED;
2613
2614 mutex_exit(&recv_sys->mutex);
2615
2616 mtr_t mtr;
2617
2618 mtr_start(&mtr);
2619
2620 mtr_set_log_mode(&mtr, MTR_LOG_NONE);
2621
2622 page_t *page = block->frame;
2623
2624 page_zip_des_t *page_zip = buf_block_get_page_zip(block);
2625
2626 #ifndef UNIV_HOTBACKUP
2627 if (just_read_in) {
2628 /* Move the ownership of the x-latch on the page to
2629 this OS thread, so that we can acquire a second
2630 x-latch on it. This is needed for the operations to
2631 the page to pass the debug checks. */
2632
2633 rw_lock_x_lock_move_ownership(&block->lock);
2634 }
2635
2636 bool success = buf_page_get_known_nowait(
2637 RW_X_LATCH, block, Cache_hint::KEEP_OLD, __FILE__, __LINE__, &mtr);
2638 ut_a(success);
2639
2640 buf_block_dbg_add_level(block, SYNC_NO_ORDER_CHECK);
2641 #endif /* !UNIV_HOTBACKUP */
2642
2643 /* Read the newest modification lsn from the page */
2644 lsn_t page_lsn = mach_read_from_8(page + FIL_PAGE_LSN);
2645
2646 #ifndef UNIV_HOTBACKUP
2647
2648 /* It may be that the page has been modified in the buffer
2649 pool: read the newest modification LSN there */
2650
2651 lsn_t page_newest_lsn;
2652
2653 page_newest_lsn = buf_page_get_newest_modification(&block->page);
2654
2655 if (page_newest_lsn) {
2656 page_lsn = page_newest_lsn;
2657 }
2658 #else /* !UNIV_HOTBACKUP */
2659 /* In recovery from a backup we do not really use the buffer pool */
2660 lsn_t page_newest_lsn = 0;
2661 /* Count applied and skipped log records */
2662 size_t applied_recs = 0;
2663 size_t skipped_recs = 0;
2664 #endif /* !UNIV_HOTBACKUP */
2665
2666 #ifndef UNIV_HOTBACKUP
2667 lsn_t end_lsn = 0;
2668 #endif /* !UNIV_HOTBACKUP */
2669 lsn_t start_lsn = 0;
2670 bool modification_to_page = false;
2671
2672 for (auto recv : recv_addr->rec_list) {
2673 #ifndef UNIV_HOTBACKUP
2674 end_lsn = recv->end_lsn;
2675
2676 ut_ad(end_lsn <= max_lsn);
2677 #endif /* !UNIV_HOTBACKUP */
2678
2679 byte *buf = nullptr;
2680
2681 if (recv->len > RECV_DATA_BLOCK_SIZE) {
2682 /* We have to copy the record body to a separate
2683 buffer */
2684
2685 buf = static_cast<byte *>(
2686 ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, recv->len));
2687
2688 recv_data_copy_to_buf(buf, recv);
2689 } else if (recv->data != nullptr) {
2690 buf = ((byte *)(recv->data)) + sizeof(recv_data_t);
2691 } else {
2692 /* Redo record that does not have a payload, such as
2693 MLOG_UNDO_ERASE_END, MLOG_COMP_PAGE_CREATE, MLOG_INIT_FILE_PAGE2 etc.
2694 */
2695 ut_ad(recv->data == nullptr);
2696 ut_ad(recv->len == 0);
2697 }
2698
2699 if (recv->type == MLOG_INIT_FILE_PAGE) {
2700 page_lsn = page_newest_lsn;
2701
2702 memset(FIL_PAGE_LSN + page, 0, 8);
2703 memset(UNIV_PAGE_SIZE - FIL_PAGE_END_LSN_OLD_CHKSUM + page, 0, 8);
2704
2705 if (page_zip) {
2706 memset(FIL_PAGE_LSN + page_zip->data, 0, 8);
2707 }
2708 }
2709
2710 /* Ignore applying the redo logs for tablespace that is
2711 truncated. Truncated tablespaces are handled explicitly
2712 post-recovery, where we will restore the tablespace back
2713 to a normal state.
2714
2715 Applying redo at this stage will cause problems because the
2716 redo will have action recorded on page before tablespace
2717 was re-inited and that would lead to a problem later. */
2718
2719 if (recv->start_lsn >= page_lsn
2720 #ifndef UNIV_HOTBACKUP
2721 && undo::is_active(recv_addr->space)
2722 #endif /* !UNIV_HOTBACKUP */
2723 ) {
2724
2725 lsn_t end_lsn;
2726
2727 if (!modification_to_page) {
2728 #ifndef UNIV_HOTBACKUP
2729 ut_a(recv_needed_recovery);
2730 #endif /* !UNIV_HOTBACKUP */
2731 modification_to_page = true;
2732 start_lsn = recv->start_lsn;
2733 }
2734
2735 DBUG_PRINT("ib_log", ("apply " LSN_PF ":"
2736 " %s len " ULINTPF " page %u:%u",
2737 recv->start_lsn, get_mlog_string(recv->type),
2738 recv->len, recv_addr->space, recv_addr->page_no));
2739 /* Since buf can be a nullptr for record types without a payload we can
2740 end up with nullptr + 0 if we calc buf + recv->len. This is undefined
2741 behaviour. Avoid this by only calculating the end_ptr when there's
2742 actual data to work with, otherwise set it to nullptr. */
2743 unsigned char *buf_end = nullptr;
2744 if (buf != nullptr) {
2745 buf_end = buf + recv->len;
2746 }
2747 recv_parse_or_apply_log_rec_body(recv->type, buf, buf_end,
2748 recv_addr->space, recv_addr->page_no,
2749 block, &mtr, ULINT_UNDEFINED, LSN_MAX);
2750
2751 end_lsn = recv->start_lsn + recv->len;
2752
2753 mach_write_to_8(FIL_PAGE_LSN + page, end_lsn);
2754
2755 mach_write_to_8(UNIV_PAGE_SIZE - FIL_PAGE_END_LSN_OLD_CHKSUM + page,
2756 end_lsn);
2757
2758 if (page_zip) {
2759 mach_write_to_8(FIL_PAGE_LSN + page_zip->data, end_lsn);
2760 }
2761 #ifdef UNIV_HOTBACKUP
2762 ++applied_recs;
2763 } else {
2764 ++skipped_recs;
2765 #endif /* UNIV_HOTBACKUP */
2766 }
2767
2768 if (recv->len > RECV_DATA_BLOCK_SIZE) {
2769 ut::free(buf);
2770 }
2771 }
2772
2773 #ifdef UNIV_ZIP_DEBUG
2774 if (fil_page_index_page_check(page)) {
2775 page_zip_des_t *page_zip = buf_block_get_page_zip(block);
2776
2777 ut_a(!page_zip || page_zip_validate_low(page_zip, page, nullptr, false));
2778 }
2779 #endif /* UNIV_ZIP_DEBUG */
2780
2781 #ifndef UNIV_HOTBACKUP
2782 if (modification_to_page) {
2783 buf_flush_recv_note_modification(block, start_lsn, end_lsn);
2784 }
2785 #else /* !UNIV_HOTBACKUP */
2786 UT_NOT_USED(start_lsn);
2787 #endif /* !UNIV_HOTBACKUP */
2788
2789 /* Make sure that committing mtr does not change the modification
2790 LSN values of page */
2791 ut_a(mtr.get_log_mode() == MTR_LOG_NONE);
2792
2793 mtr_commit(&mtr);
2794
2795 mutex_enter(&recv_sys->mutex);
2796
2797 if (recv_max_page_lsn < page_lsn) {
2798 recv_max_page_lsn = page_lsn;
2799 }
2800
2801 recv_addr->state = RECV_PROCESSED;
2802
2803 ut_a(recv_sys->n_addrs > 0);
2804 --recv_sys->n_addrs;
2805
2806 mutex_exit(&recv_sys->mutex);
2807
2808 #ifdef UNIV_HOTBACKUP
2809 ib::trace_2() << "Applied " << applied_recs << " Skipped " << skipped_recs;
2810 #endif /* UNIV_HOTBACKUP */
2811 }
2812
2813 /** Tries to parse a single log record.
2814 @param[out] type log record type
2815 @param[in] ptr pointer to a buffer
2816 @param[in] end_ptr end of the buffer
2817 @param[out] space_id tablespace identifier
2818 @param[out] page_no page number
2819 @param[out] body start of log record body
2820 @return length of the record, or 0 if the record was not complete */
2821 static ulint recv_parse_log_rec(mlog_id_t *type, byte *ptr, byte *end_ptr,
2822 space_id_t *space_id, page_no_t *page_no,
2823 byte **body) {
2824 byte *new_ptr;
2825
2826 *body = nullptr;
2827
2828 UNIV_MEM_INVALID(type, sizeof *type);
2829 UNIV_MEM_INVALID(space_id, sizeof *space_id);
2830 UNIV_MEM_INVALID(page_no, sizeof *page_no);
2831 UNIV_MEM_INVALID(body, sizeof *body);
2832
2833 if (ptr == end_ptr) {
2834 return 0;
2835 }
2836
2837 switch (*ptr) {
2838 #ifdef UNIV_LOG_LSN_DEBUG
2839 case MLOG_LSN | MLOG_SINGLE_REC_FLAG:
2840 case MLOG_LSN:
2841
2842 new_ptr =
2843 mlog_parse_initial_log_record(ptr, end_ptr, type, space_id, page_no);
2844
2845 if (new_ptr != nullptr) {
2846 const lsn_t lsn = static_cast<lsn_t>(*space_id) << 32 | *page_no;
2847
2848 ut_a(lsn == recv_sys->recovered_lsn);
2849 }
2850
2851 *type = MLOG_LSN;
2852 return new_ptr == nullptr ? 0 : new_ptr - ptr;
2853 #endif /* UNIV_LOG_LSN_DEBUG */
2854
2855 case MLOG_MULTI_REC_END:
2856 case MLOG_DUMMY_RECORD:
2857 *page_no = FIL_NULL;
2858 *space_id = SPACE_UNKNOWN;
2859 *type = static_cast<mlog_id_t>(*ptr);
2860 return 1;
2861
2862 case MLOG_MULTI_REC_END | MLOG_SINGLE_REC_FLAG:
2863 case MLOG_DUMMY_RECORD | MLOG_SINGLE_REC_FLAG:
2864 recv_sys->found_corrupt_log = true;
2865 return 0;
2866
2867 case MLOG_TABLE_DYNAMIC_META:
2868 case MLOG_TABLE_DYNAMIC_META | MLOG_SINGLE_REC_FLAG:
2869
2870 table_id_t id;
2871 uint64_t version;
2872
2873 *page_no = FIL_NULL;
2874 *space_id = SPACE_UNKNOWN;
2875
2876 new_ptr =
2877 mlog_parse_initial_dict_log_record(ptr, end_ptr, type, &id, &version);
2878
2879 if (new_ptr != nullptr) {
2880 new_ptr = recv_sys->metadata_recover->parseMetadataLog(
2881 id, version, new_ptr, end_ptr);
2882 }
2883
2884 return new_ptr == nullptr ? 0 : new_ptr - ptr;
2885 }
2886
2887 new_ptr =
2888 mlog_parse_initial_log_record(ptr, end_ptr, type, space_id, page_no);
2889
2890 *body = new_ptr;
2891
2892 if (new_ptr == nullptr) {
2893 return 0;
2894 }
2895
2896 new_ptr = recv_parse_or_apply_log_rec_body(
2897 *type, new_ptr, end_ptr, *space_id, *page_no, nullptr, nullptr,
2898 new_ptr - ptr, recv_sys->recovered_lsn);
2899
2900 if (new_ptr == nullptr) {
2901 return 0;
2902 }
2903
2904 return new_ptr - ptr;
2905 }
2906
2907 /** Subtracts next number of bytes to ignore before we reach the checkpoint
2908 or returns information that there was nothing more to skip.
2909 @param[in] next_parsed_bytes number of next bytes that were parsed,
2910 which are supposed to be subtracted from bytes to ignore before checkpoint
2911 @retval true there were still bytes to ignore
2912 @retval false there was already 0 bytes to ignore, nothing changed. */
2913 static bool recv_update_bytes_to_ignore_before_checkpoint(
2914 size_t next_parsed_bytes) {
2915 auto &to_ignore = recv_sys->bytes_to_ignore_before_checkpoint;
2916
2917 if (to_ignore != 0) {
2918 if (to_ignore >= next_parsed_bytes) {
2919 to_ignore -= next_parsed_bytes;
2920 } else {
2921 to_ignore = 0;
2922 }
2923 return true;
2924 }
2925
2926 return false;
2927 }
2928
2929 /** Tracks changes of recovered_lsn and tracks proper values for what
2930 first_rec_group should be for consecutive blocks. Must be called when
2931 recv_sys->recovered_lsn is changed to next lsn pointing at boundary
2932 between consecutive parsed mini-transactions. */
2933 static void recv_track_changes_of_recovered_lsn() {
2934 if (recv_sys->parse_start_lsn == 0) {
2935 return;
2936 }
2937 /* If we have already found the first block with mtr beginning there,
2938 we started to track boundaries between blocks. Since then we track
2939 all proper values of first_rec_group for consecutive blocks.
2940 The reason for that is to ensure that the first_rec_group of the last
2941 block is correct. Even though we do not depend during this recovery
2942 on that value, it would become important if we crashed later, because
2943 the last recovered block would become the first used block in redo and
2944 since then we would depend on a proper value of first_rec_group there.
2945 The checksums of log blocks should detect if it was incorrect, but the
2946 checksums might be disabled in the configuration. */
2947 const auto old_block =
2948 recv_sys->previous_recovered_lsn / OS_FILE_LOG_BLOCK_SIZE;
2949
2950 const auto new_block = recv_sys->recovered_lsn / OS_FILE_LOG_BLOCK_SIZE;
2951
2952 if (old_block != new_block) {
2953 ut_a(new_block > old_block);
2954
2955 recv_sys->last_block_first_mtr_boundary = recv_sys->recovered_lsn;
2956 }
2957
2958 recv_sys->previous_recovered_lsn = recv_sys->recovered_lsn;
2959 }
2960
2961 /** Parse and store a single log record entry.
2962 @param[in] ptr start of buffer
2963 @param[in] end_ptr end of buffer
2964 @return true if end of processing */
2965 static bool recv_single_rec(byte *ptr, byte *end_ptr) {
2966 /* The mtr did not modify multiple pages */
2967
2968 lsn_t old_lsn = recv_sys->recovered_lsn;
2969
2970 /* Try to parse a log record, fetching its type, space id,
2971 page no, and a pointer to the body of the log record */
2972
2973 byte *body;
2974 mlog_id_t type;
2975 page_no_t page_no;
2976 space_id_t space_id;
2977
2978 ulint len =
2979 recv_parse_log_rec(&type, ptr, end_ptr, &space_id, &page_no, &body);
2980
2981 if (recv_sys->found_corrupt_log) {
2982 recv_report_corrupt_log(ptr, type, space_id, page_no);
2983
2984 #ifdef UNIV_HOTBACKUP
2985 return true;
2986 #endif /* UNIV_HOTBACKUP */
2987
2988 } else if (len == 0 || recv_sys->found_corrupt_fs) {
2989 return true;
2990 }
2991
2992 lsn_t new_recovered_lsn;
2993
2994 new_recovered_lsn = recv_calc_lsn_on_data_add(old_lsn, len);
2995
2996 if (new_recovered_lsn > recv_sys->scanned_lsn) {
2997 /* The log record filled a log block, and we
2998 require that also the next log block should
2999 have been scanned in */
3000
3001 return true;
3002 }
3003
3004 recv_previous_parsed_rec_type = type;
3005 recv_previous_parsed_rec_is_multi = 0;
3006 recv_previous_parsed_rec_offset = recv_sys->recovered_offset;
3007
3008 recv_sys->recovered_offset += len;
3009 recv_sys->recovered_lsn = new_recovered_lsn;
3010
3011 recv_track_changes_of_recovered_lsn();
3012
3013 if (recv_update_bytes_to_ignore_before_checkpoint(len)) {
3014 return false;
3015 }
3016
3017 switch (type) {
3018 case MLOG_DUMMY_RECORD:
3019 /* Do nothing */
3020 break;
3021
3022 #ifdef UNIV_LOG_LSN_DEBUG
3023 case MLOG_LSN:
3024 /* Do not add these records to the hash table.
3025 The page number and space id fields are misused
3026 for something else. */
3027 break;
3028 #endif /* UNIV_LOG_LSN_DEBUG */
3029
3030 default:
3031
3032 if (recv_recovery_on
3033 #ifndef UNIV_HOTBACKUP
3034 && (space_id == TRX_SYS_SPACE ||
3035 fil_tablespace_lookup_for_recovery(space_id))
3036 #endif /* !UNIV_HOTBACKUP */
3037 ) {
3038 recv_add_to_hash_table(type, space_id, page_no, body, ptr + len,
3039 old_lsn, recv_sys->recovered_lsn);
3040 }
3041
3042 [[fallthrough]];
3043
3044 case MLOG_INDEX_LOAD:
3045 case MLOG_FILE_DELETE:
3046 case MLOG_FILE_RENAME:
3047 case MLOG_FILE_CREATE:
3048 case MLOG_FILE_EXTEND:
3049 case MLOG_TABLE_DYNAMIC_META:
3050
3051 /* These were already handled by
3052 recv_parse_log_rec() and
3053 recv_parse_or_apply_log_rec_body(). */
3054
3055 DBUG_PRINT("ib_log",
3056 ("scan " LSN_PF ": log rec %s"
3057 " len " ULINTPF " " PAGE_ID_PF,
3058 old_lsn, get_mlog_string(type), len, space_id, page_no));
3059 break;
3060 }
3061
3062 return false;
3063 }
3064
3065 /** Parse and store a multiple record log entry.
3066 @param[in] ptr start of buffer
3067 @param[in] end_ptr end of buffer
3068 @return true if end of processing */
3069 static bool recv_multi_rec(byte *ptr, byte *end_ptr) {
3070 /* Check that all the records associated with the single mtr
3071 are included within the buffer */
3072
3073 ulint n_recs = 0;
3074 ulint total_len = 0;
3075
3076 for (;;) {
3077 mlog_id_t type = MLOG_BIGGEST_TYPE;
3078 byte *body;
3079 page_no_t page_no = 0;
3080 space_id_t space_id = 0;
3081
3082 ulint len =
3083 recv_parse_log_rec(&type, ptr, end_ptr, &space_id, &page_no, &body);
3084
3085 if (recv_sys->found_corrupt_log) {
3086 recv_report_corrupt_log(ptr, type, space_id, page_no);
3087
3088 return true;
3089
3090 } else if (len == 0) {
3091 return true;
3092
3093 } else if ((*ptr & MLOG_SINGLE_REC_FLAG)) {
3094 recv_sys->found_corrupt_log = true;
3095
3096 recv_report_corrupt_log(ptr, type, space_id, page_no);
3097
3098 return true;
3099
3100 } else if (recv_sys->found_corrupt_fs) {
3101 return true;
3102 }
3103
3104 recv_sys->save_rec(n_recs, space_id, page_no, type, body, len);
3105
3106 recv_previous_parsed_rec_type = type;
3107
3108 recv_previous_parsed_rec_offset = recv_sys->recovered_offset + total_len;
3109
3110 recv_previous_parsed_rec_is_multi = 1;
3111
3112 total_len += len;
3113 ++n_recs;
3114
3115 ptr += len;
3116
3117 if (type == MLOG_MULTI_REC_END) {
3118 DBUG_PRINT("ib_log", ("scan " LSN_PF ": multi-log end total_len " ULINTPF
3119 " n=" ULINTPF,
3120 recv_sys->recovered_lsn, total_len, n_recs));
3121
3122 break;
3123 }
3124
3125 DBUG_PRINT("ib_log",
3126 ("scan " LSN_PF ": multi-log rec %s len " ULINTPF " " PAGE_ID_PF,
3127 recv_sys->recovered_lsn, get_mlog_string(type), len, space_id,
3128 page_no));
3129 }
3130
3131 lsn_t new_recovered_lsn =
3132 recv_calc_lsn_on_data_add(recv_sys->recovered_lsn, total_len);
3133
3134 if (new_recovered_lsn > recv_sys->scanned_lsn) {
3135 /* The log record filled a log block, and we require
3136 that also the next log block should have been scanned in */
3137
3138 return true;
3139 }
3140
3141 /* Add all the records to the hash table */
3142
3143 ptr = recv_sys->buf + recv_sys->recovered_offset;
3144
3145 for (ulint i = 0; i < n_recs; i++) {
3146 lsn_t old_lsn = recv_sys->recovered_lsn;
3147
3148 /* This will apply MLOG_FILE_ records. */
3149 space_id_t space_id = 0;
3150 page_no_t page_no = 0;
3151
3152 mlog_id_t type = MLOG_BIGGEST_TYPE;
3153
3154 byte *body = nullptr;
3155 size_t len = 0;
3156
3157 /* Avoid parsing if we have the record saved already. */
3158 if (!recv_sys->get_saved_rec(i, space_id, page_no, type, body, len)) {
3159 len = recv_parse_log_rec(&type, ptr, end_ptr, &space_id, &page_no, &body);
3160 }
3161
3162 if (recv_sys->found_corrupt_log &&
3163 !recv_report_corrupt_log(ptr, type, space_id, page_no)) {
3164 return true;
3165
3166 } else if (recv_sys->found_corrupt_fs) {
3167 return true;
3168 }
3169
3170 ut_a(len != 0);
3171 ut_a(!(*ptr & MLOG_SINGLE_REC_FLAG));
3172
3173 recv_sys->recovered_offset += len;
3174
3175 recv_sys->recovered_lsn = recv_calc_lsn_on_data_add(old_lsn, len);
3176
3177 const bool apply = !recv_update_bytes_to_ignore_before_checkpoint(len);
3178
3179 switch (type) {
3180 case MLOG_MULTI_REC_END:
3181 recv_track_changes_of_recovered_lsn();
3182 /* Found the end mark for the records */
3183 return false;
3184
3185 #ifdef UNIV_LOG_LSN_DEBUG
3186 case MLOG_LSN:
3187 /* Do not add these records to the hash table.
3188 The page number and space id fields are misused
3189 for something else. */
3190 break;
3191 #endif /* UNIV_LOG_LSN_DEBUG */
3192
3193 case MLOG_FILE_DELETE:
3194 case MLOG_FILE_CREATE:
3195 case MLOG_FILE_RENAME:
3196 case MLOG_FILE_EXTEND:
3197 case MLOG_TABLE_DYNAMIC_META:
3198 /* case MLOG_TRUNCATE: Disabled for WL6378 */
3199 /* These were already handled by
3200 recv_parse_or_apply_log_rec_body(). */
3201 break;
3202
3203 default:
3204
3205 if (!apply) {
3206 break;
3207 }
3208
3209 if (recv_recovery_on
3210 #ifndef UNIV_HOTBACKUP
3211 && (space_id == TRX_SYS_SPACE ||
3212 fil_tablespace_lookup_for_recovery(space_id))
3213 #endif /* !UNIV_HOTBACKUP */
3214 ) {
3215
3216 recv_add_to_hash_table(type, space_id, page_no, body, ptr + len,
3217 old_lsn, new_recovered_lsn);
3218 }
3219 }
3220
3221 ptr += len;
3222 }
3223
3224 return false;
3225 }
3226
3227 /** Parse log records from a buffer and optionally store them to a
3228 hash table to wait merging to file pages. */
3229 static void recv_parse_log_recs() {
3230 ut_ad(recv_sys->parse_start_lsn != 0);
3231
3232 for (;;) {
3233 byte *ptr = recv_sys->buf + recv_sys->recovered_offset;
3234
3235 byte *end_ptr = recv_sys->buf + recv_sys->len;
3236
3237 if (ptr == end_ptr) {
3238 return;
3239 }
3240
3241 bool single_rec;
3242
3243 switch (*ptr) {
3244 #ifdef UNIV_LOG_LSN_DEBUG
3245 case MLOG_LSN:
3246 #endif /* UNIV_LOG_LSN_DEBUG */
3247 case MLOG_DUMMY_RECORD:
3248 single_rec = true;
3249 break;
3250 default:
3251 single_rec = !!(*ptr & MLOG_SINGLE_REC_FLAG);
3252 }
3253
3254 if (single_rec) {
3255 if (recv_single_rec(ptr, end_ptr)) {
3256 return;
3257 }
3258
3259 } else if (recv_multi_rec(ptr, end_ptr)) {
3260 return;
3261 }
3262 }
3263 }
3264
3265 /** Adds data from a new log block to the parsing buffer of recv_sys if
3266 recv_sys->parse_start_lsn is non-zero.
3267 @param[in] log_block log block
3268 @param[in] scanned_lsn lsn of how far we were able
3269 to find data in this log block
3270 @return true if more data added */
3271 static bool recv_sys_add_to_parsing_buf(const byte *log_block,
3272 lsn_t scanned_lsn) {
3273 ut_ad(scanned_lsn >= recv_sys->scanned_lsn);
3274
3275 if (!recv_sys->parse_start_lsn) {
3276 /* Cannot start parsing yet because no start point for
3277 it found */
3278
3279 return false;
3280 }
3281
3282 ulint more_len;
3283 ulint data_len = log_block_get_data_len(log_block);
3284
3285 if (recv_sys->parse_start_lsn >= scanned_lsn) {
3286 return false;
3287
3288 } else if (recv_sys->scanned_lsn >= scanned_lsn) {
3289 return false;
3290
3291 } else if (recv_sys->parse_start_lsn > recv_sys->scanned_lsn) {
3292 more_len = (ulint)(scanned_lsn - recv_sys->parse_start_lsn);
3293
3294 } else {
3295 more_len = (ulint)(scanned_lsn - recv_sys->scanned_lsn);
3296 }
3297
3298 if (more_len == 0) {
3299 return false;
3300 }
3301
3302 ut_ad(data_len >= more_len);
3303
3304 ulint start_offset = data_len - more_len;
3305
3306 if (start_offset < LOG_BLOCK_HDR_SIZE) {
3307 start_offset = LOG_BLOCK_HDR_SIZE;
3308 }
3309
3310 ulint end_offset = data_len;
3311
3312 if (end_offset > OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE) {
3313 end_offset = OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE;
3314 }
3315
3316 ut_ad(start_offset <= end_offset);
3317
3318 if (start_offset < end_offset) {
3319 memcpy(recv_sys->buf + recv_sys->len, log_block + start_offset,
3320 end_offset - start_offset);
3321
3322 recv_sys->len += end_offset - start_offset;
3323
3324 ut_a(recv_sys->len <= recv_sys->buf_len);
3325 }
3326
3327 return true;
3328 }
3329
3330 /** Moves the parsing buffer data left to the buffer start. */
3331 static void recv_reset_buffer() {
3332 ut_memmove(recv_sys->buf, recv_sys->buf + recv_sys->recovered_offset,
3333 recv_sys->len - recv_sys->recovered_offset);
3334
3335 recv_sys->len -= recv_sys->recovered_offset;
3336
3337 recv_sys->recovered_offset = 0;
3338 }
3339
3340 /** Scans log from a buffer and stores new log data to the parsing buffer.
3341 Parses and hashes the log records if new data found. Unless
3342 UNIV_HOTBACKUP is defined, this function will apply log records
3343 automatically when the hash table becomes full.
3344 @param[in,out] log redo log
3345 @param[in] max_memory we let the hash table of recs to grow to
3346 this size, at the maximum
3347 @param[in] buf buffer containing a log segment or garbage
3348 @param[in] len buffer length
3349 @param[in] start_lsn buffer start lsn
3350 @param[out] read_upto_lsn scanning succeeded up to this lsn
3351 @return true if not able to scan any more in this log */
3352 #ifndef UNIV_HOTBACKUP
3353 static bool recv_scan_log_recs(log_t &log,
3354 #else /* !UNIV_HOTBACKUP */
3355 bool meb_scan_log_recs(
3356 #endif /* !UNIV_HOTBACKUP */
3357 size_t max_memory, const byte *buf, size_t len,
3358 lsn_t start_lsn, lsn_t *read_upto_lsn) {
3359 const byte *log_block = buf;
3360 lsn_t scanned_lsn = start_lsn;
3361 bool finished = false;
3362 bool more_data = false;
3363
3364 ut_ad(start_lsn % OS_FILE_LOG_BLOCK_SIZE == 0);
3365 ut_ad(len % OS_FILE_LOG_BLOCK_SIZE == 0);
3366 ut_ad(len >= OS_FILE_LOG_BLOCK_SIZE);
3367
3368 do {
3369 ut_ad(!finished);
3370
3371 Log_data_block_header block_header;
3372 log_data_block_header_deserialize(log_block, block_header);
3373
3374 const uint32_t expected_hdr_no =
3375 log_block_convert_lsn_to_hdr_no(scanned_lsn);
3376
3377 if (block_header.m_hdr_no != expected_hdr_no) {
3378 /* Garbage or an incompletely written log block.
3379
3380 We will not report any error, because this can
3381 happen when InnoDB was killed while it was
3382 writing redo log. We simply treat this as an
3383 abrupt end of the redo log. */
3384
3385 finished = true;
3386
3387 break;
3388 }
3389
3390 if (!log_block_checksum_is_ok(log_block)) {
3391 uint32_t checksum1 = log_block_get_checksum(log_block);
3392 uint32_t checksum2 = log_block_calc_checksum(log_block);
3393 ib::error(ER_IB_MSG_720, ulong{block_header.m_hdr_no},
3394 ulonglong{scanned_lsn}, ulong{checksum1}, ulong{checksum2});
3395
3396 /* Garbage or an incompletely written log block.
3397
3398 This could be the result of killing the server
3399 while it was writing this log block. We treat
3400 this as an abrupt end of the redo log. */
3401
3402 finished = true;
3403
3404 break;
3405 }
3406
3407 const auto data_len = block_header.m_data_len;
3408
3409 if (scanned_lsn + data_len > recv_sys->scanned_lsn &&
3410 recv_sys->scanned_epoch_no > 0 &&
3411 !log_block_epoch_no_is_valid(block_header.m_epoch_no,
3412 recv_sys->scanned_epoch_no)) {
3413 /* Garbage from a log buffer flush which was made
3414 before the most recent database recovery */
3415
3416 finished = true;
3417
3418 break;
3419 }
3420
3421 if (!recv_sys->parse_start_lsn && block_header.m_first_rec_group > 0) {
3422 /* We found a point from which to start the parsing of log records */
3423
3424 recv_sys->parse_start_lsn = scanned_lsn + block_header.m_first_rec_group;
3425
3426 ib::info(ER_IB_MSG_1261)
3427 << "Starting to parse redo log at lsn = " << recv_sys->parse_start_lsn
3428 << ", whereas checkpoint_lsn = " << recv_sys->checkpoint_lsn
3429 << " and start_lsn = " << start_lsn;
3430
3431 if (recv_sys->parse_start_lsn < recv_sys->checkpoint_lsn) {
3432 /* We start to parse log records even before
3433 checkpoint_lsn, from the beginning of the log
3434 block which contains the checkpoint_lsn.
3435
3436 That's because the first group of log records
3437 in the log block, starts before checkpoint_lsn,
3438 and checkpoint_lsn could potentially point to
3439 the middle of some log record. We need to find
3440 the first group of log records that starts at
3441 or after checkpoint_lsn. This could be only
3442 achieved by traversing all groups of log records
3443 that start within the log block since the first
3444 one (to discover their beginnings we need to
3445 parse them). However, we don't want to report
3446 missing tablespaces for space_id in log records
3447 before checkpoint_lsn. Hence we need to ignore
3448 those records and that's why we need a counter
3449 of bytes to ignore. */
3450
3451 recv_sys->bytes_to_ignore_before_checkpoint =
3452 recv_sys->checkpoint_lsn - recv_sys->parse_start_lsn;
3453
3454 ut_a(recv_sys->bytes_to_ignore_before_checkpoint <=
3455 OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_HDR_SIZE);
3456
3457 ut_a(recv_sys->checkpoint_lsn % OS_FILE_LOG_BLOCK_SIZE +
3458 LOG_BLOCK_TRL_SIZE <
3459 OS_FILE_LOG_BLOCK_SIZE);
3460
3461 ut_a(recv_sys->parse_start_lsn % OS_FILE_LOG_BLOCK_SIZE >=
3462 LOG_BLOCK_HDR_SIZE);
3463 }
3464
3465 recv_sys->scanned_lsn = recv_sys->parse_start_lsn;
3466 recv_sys->recovered_lsn = recv_sys->parse_start_lsn;
3467
3468 recv_track_changes_of_recovered_lsn();
3469 }
3470
3471 scanned_lsn += data_len;
3472
3473 if (scanned_lsn > recv_sys->scanned_lsn) {
3474 #ifndef UNIV_HOTBACKUP
3475 if (!recv_needed_recovery && scanned_lsn > recv_sys->checkpoint_lsn) {
3476 if (srv_read_only_mode) {
3477 ut_a(srv_force_recovery < SRV_FORCE_NO_LOG_REDO);
3478 ib::warn(ER_IB_MSG_RECOVERY_SKIPPED_IN_READ_ONLY_MODE);
3479 *read_upto_lsn = scanned_lsn;
3480 return true;
3481 }
3482
3483 ib::info(ER_IB_MSG_722, ulonglong{recv_sys->scanned_lsn});
3484
3485 recv_init_crash_recovery();
3486 }
3487 #endif /* !UNIV_HOTBACKUP */
3488
3489 /* We were able to find more log data: add it to the
3490 parsing buffer if parse_start_lsn is already
3491 non-zero */
3492
3493 DBUG_EXECUTE_IF("simulate_3mb_mtr_recovery", {
3494 uint saved_len = recv_sys->len;
3495 recv_sys->len = 3 * 1024 * 1024;
3496 recv_sys_resize_buf();
3497 recv_sys->len = saved_len;
3498 });
3499
3500 if (recv_sys->len + 4 * OS_FILE_LOG_BLOCK_SIZE >= recv_sys->buf_len) {
3501 if (!recv_sys_resize_buf()) {
3502 recv_sys->found_corrupt_log = true;
3503
3504 #ifndef UNIV_HOTBACKUP
3505 if (srv_force_recovery == 0) {
3506 ib::error(ER_IB_MSG_724);
3507 return true;
3508 }
3509 #else /* !UNIV_HOTBACKUP */
3510 ib::fatal(UT_LOCATION_HERE,
3511 ER_IB_ERR_NOT_ENOUGH_MEMORY_FOR_PARSE_BUFFER)
3512 << "Insufficient memory for InnoDB parse buffer; want "
3513 << recv_sys->buf_len;
3514 #endif /* !UNIV_HOTBACKUP */
3515 }
3516 }
3517
3518 if (!recv_sys->found_corrupt_log) {
3519 more_data = recv_sys_add_to_parsing_buf(log_block, scanned_lsn);
3520 }
3521
3522 recv_sys->scanned_lsn = scanned_lsn;
3523
3524 recv_sys->scanned_epoch_no = block_header.m_epoch_no;
3525 }
3526
3527 if (data_len < OS_FILE_LOG_BLOCK_SIZE) {
3528 /* Log data for this group ends here */
3529 finished = true;
3530
3531 break;
3532
3533 } else {
3534 log_block += OS_FILE_LOG_BLOCK_SIZE;
3535 }
3536
3537 } while (log_block < buf + len);
3538
3539 *read_upto_lsn = scanned_lsn;
3540
3541 if (recv_needed_recovery ||
3542 (recv_is_from_backup && !recv_is_making_a_backup)) {
3543 ++recv_scan_print_counter;
3544
3545 if (finished || (recv_scan_print_counter % 80) == 0) {
3546 ib::info(ER_IB_MSG_725, ulonglong{scanned_lsn});
3547 }
3548 }
3549
3550 if (more_data && !recv_sys->found_corrupt_log) {
3551 /* Try to parse more log records */
3552
3553 recv_parse_log_recs();
3554
3555 #ifndef UNIV_HOTBACKUP
3556 if (recv_heap_used() > max_memory) {
3557 recv_apply_hashed_log_recs(log, false);
3558 }
3559 #endif /* !UNIV_HOTBACKUP */
3560
3561 if (recv_sys->recovered_offset > recv_sys->buf_len / 4) {
3562 /* Move parsing buffer data to the buffer start */
3563
3564 recv_reset_buffer();
3565 }
3566 }
3567
3568 return finished;
3569 }
3570
3571 #ifndef UNIV_HOTBACKUP
3572 static lsn_t recv_read_log_seg(log_t &log, byte *buf, lsn_t start_lsn,
3573 const lsn_t end_lsn) {
3574 log_background_threads_inactive_validate();
3575
3576 ut_a(start_lsn < end_lsn);
3577
3578 auto file = log.m_files.find(start_lsn);
3579
3580 if (file == log.m_files.end()) {
3581 /* Missing valid file ! */
3582 return start_lsn;
3583 }
3584
3585 auto file_handle = file->open(Log_file_access_mode::READ_ONLY);
3586 ut_a(file_handle.is_open());
3587
3588 do {
3589 os_offset_t source_offset;
3590
3591 source_offset = file->offset(start_lsn);
3592
3593 ut_a(end_lsn - start_lsn <= ULINT_MAX);
3594
3595 os_offset_t len = end_lsn - start_lsn;
3596
3597 ut_ad(len != 0);
3598
3599 bool switch_to_next_file = false;
3600
3601 if (source_offset + len > file->m_size_in_bytes) {
3602 /* If the above condition is true then len
3603 (which is unsigned) is > the expression below,
3604 so the typecast is ok */
3605 ut_a(file->m_size_in_bytes > source_offset);
3606 len = file->m_size_in_bytes - source_offset;
3607 switch_to_next_file = true;
3608 }
3609
3610 ++log.n_log_ios;
3611
3612 dberr_t err = log_data_blocks_read(file_handle, source_offset, len, buf);
3613
3614 if (err == DB_UNSUPPORTED) {
3615 /* The log block may be encrypted, read and update the log_sys */
3616 err = log_encryption_read(log);
3617 if (err != DB_SUCCESS) {
3618 return 0;
3619 }
3620
3621 /* Try again */
3622 err = log_data_blocks_read(file_handle, source_offset, len, buf);
3623 switch (err) {
3624 case DB_SUCCESS:
3625 break;
3626
3627 case DB_UNSUPPORTED:
3628 ib::error(ER_IB_MSG_CANT_DECRYPT_REDO_LOG, ulonglong{source_offset},
3629 file_handle.file_path().c_str());
3630 return 0;
3631
3632 default:
3633 return 0;
3634 }
3635 }
3636
3637 start_lsn += len;
3638 buf += len;
3639
3640 if (switch_to_next_file) {
3641 auto next_id = file->next_id();
3642
3643 const auto next_file = log.m_files.file(next_id);
3644
3645 if (next_file == log.m_files.end() || !next_file->contains(start_lsn)) {
3646 return start_lsn;
3647 }
3648
3649 file_handle.close();
3650
3651 file = next_file;
3652
3653 file_handle = file->open(Log_file_access_mode::READ_ONLY);
3654 ut_a(file_handle.is_open());
3655 }
3656
3657 } while (start_lsn != end_lsn);
3658
3659 ut_a(start_lsn == end_lsn);
3660
3661 return end_lsn;
3662 }
3663
3664 /** Scans log from a buffer and stores new log data to the parsing buffer.
3665 Parses and hashes the log records if new data found.
3666 @param[in,out] log redo log
3667 @param[in,out] checkpoint_lsn log sequence number found in checkpoint
3668 header. May be inexact (in a middle of
3669 an mtr which we can ignore, as it is
3670 already applied to tablespace files)
3671 until which all redo log has been
3672 scanned
3673 @return DB_SUCCESS if successfull */
3674 static dberr_t recv_recovery_begin(log_t &log, const lsn_t checkpoint_lsn) {
3675 mutex_enter(&recv_sys->mutex);
3676 recv_sys->len = 0;
3677 recv_sys->recovered_offset = 0;
3678 recv_sys->n_addrs = 0;
3679 recv_sys_empty_hash();
3680
3681 /* Since 8.0, we can start recovery at checkpoint_lsn which points
3682 to the middle of log record. In such case we first to need to find
3683 the beginning of the first group of log records, which is at lsn
3684 greater than the checkpoint_lsn. */
3685 recv_sys->parse_start_lsn = 0;
3686
3687 /* This is updated when we find value for parse_start_lsn. */
3688 recv_sys->bytes_to_ignore_before_checkpoint = 0;
3689
3690 recv_sys->checkpoint_lsn = checkpoint_lsn;
3691 recv_sys->scanned_lsn = checkpoint_lsn;
3692 recv_sys->recovered_lsn = checkpoint_lsn;
3693
3694 /* We have to trust that the first_rec_group in the first block is
3695 correct as we can't start parsing earlier to check it ourselves. */
3696 recv_sys->previous_recovered_lsn = checkpoint_lsn;
3697 recv_sys->last_block_first_mtr_boundary = 0;
3698
3699 recv_sys->scanned_epoch_no = 0;
3700 recv_previous_parsed_rec_type = MLOG_SINGLE_REC_FLAG;
3701 recv_previous_parsed_rec_offset = 0;
3702 recv_previous_parsed_rec_is_multi = 0;
3703 ut_ad(recv_max_page_lsn == 0);
3704
3705 mutex_exit(&recv_sys->mutex);
3706
3707 ulint max_mem =
3708 UNIV_PAGE_SIZE * (buf_pool_get_n_pages() -
3709 (recv_n_pool_free_frames * srv_buf_pool_instances));
3710
3711 lsn_t start_lsn =
3712 ut_uint64_align_down(checkpoint_lsn, OS_FILE_LOG_BLOCK_SIZE);
3713
3714 bool finished = false;
3715
3716 while (!finished) {
3717 const lsn_t end_lsn =
3718 recv_read_log_seg(log, log.buf, start_lsn, start_lsn + RECV_SCAN_SIZE);
3719
3720 if (end_lsn == 0) {
3721 return DB_ERROR;
3722 }
3723
3724 if (end_lsn == start_lsn) {
3725 /* This could happen if we crashed just after completing file,
3726 and before next file has been successfully created. */
3727 break;
3728 }
3729
3730 finished = recv_scan_log_recs(log, max_mem, log.buf, end_lsn - start_lsn,
3731 start_lsn, &log.m_scanned_lsn);
3732
3733 start_lsn = end_lsn;
3734 }
3735
3736 DBUG_PRINT("ib_log", ("scan " LSN_PF " completed", log.m_scanned_lsn));
3737 return DB_SUCCESS;
3738 }
3739
3740 /** Initialize crash recovery environment. Can be called iff
3741 recv_needed_recovery == false. */
3742 static void recv_init_crash_recovery() {
3743 ut_ad(!srv_read_only_mode);
3744 ut_a(!recv_needed_recovery);
3745
3746 recv_needed_recovery = true;
3747
3748 ib::info(ER_IB_MSG_726);
3749 ib::info(ER_IB_MSG_727);
3750
3751 recv_sys->dblwr->recover();
3752
3753 if (srv_force_recovery < SRV_FORCE_NO_LOG_REDO) {
3754 /* Spawn the background thread to flush dirty pages
3755 from the buffer pools. */
3756
3757 srv_threads.m_recv_writer =
3758 os_thread_create(recv_writer_thread_key, 0, recv_writer_thread);
3759
3760 srv_threads.m_recv_writer.start();
3761 }
3762 }
3763 #endif /* !UNIV_HOTBACKUP */
3764
3765 #ifndef UNIV_HOTBACKUP
3766
3767 dberr_t recv_recovery_from_checkpoint_start(log_t &log, lsn_t flush_lsn) {
3768 /* Initialize red-black tree for fast insertions into the
3769 flush_list during recovery process. */
3770 buf_flush_init_flush_rbt();
3771
3772 if (srv_force_recovery >= SRV_FORCE_NO_LOG_REDO) {
3773 ib::info(ER_IB_MSG_728);
3774
3775 /* We leave redo log not started and this is read-only mode. */
3776 ut_a(log.sn == 0);
3777 ut_a(srv_read_only_mode);
3778
3779 return DB_SUCCESS;
3780 }
3781
3782 recv_recovery_on = true;
3783
3784 ut_a(log.m_format == Log_format::CURRENT);
3785
3786 /* Look for the latest checkpoint */
3787 Log_checkpoint_location checkpoint;
3788 if (!recv_find_max_checkpoint(log, checkpoint)) {
3789 ib::error(ER_IB_MSG_RECOVERY_CHECKPOINT_NOT_FOUND);
3790 return DB_ERROR;
3791 }
3792
3793 const auto checkpoint_file = log.m_files.find(checkpoint.m_checkpoint_lsn);
3794
3795 /* When reading checkpoints from redo log files, error would be reported
3796 if checkpoint_lsn was outside the redo log file from which it was read,
3797 and such file would be skipped. If no checkpoint was found because of that,
3798 then recv_find_max_checkpoint would return false. Therefore here we know
3799 that InnoDB found a valid checkpoint (for which there is a redo log file
3800 which contains the checkpoint_lsn). */
3801 if (checkpoint_file == log.m_files.end()) {
3802 ut_d(ut_error);
3803 ut_o(return DB_ERROR);
3804 }
3805
3806 log.last_checkpoint_lsn.store(checkpoint.m_checkpoint_lsn);
3807
3808 const auto file_path = log_file_path(log.m_files_ctx, checkpoint_file->m_id);
3809 ib::info(ER_IB_MSG_LOG_CHECKPOINT_FOUND,
3810 ulonglong{checkpoint.m_checkpoint_lsn}, file_path.c_str());
3811
3812 Log_checkpoint_header checkpoint_header;
3813
3814 auto checkpoint_file_handle =
3815 checkpoint_file->open(Log_file_access_mode::READ_ONLY);
3816
3817 if (!checkpoint_file_handle.is_open()) {
3818 return DB_CANNOT_OPEN_FILE;
3819 }
3820
3821 dberr_t err = log_checkpoint_header_read(checkpoint_file_handle,
3822 checkpoint.m_checkpoint_header_no,
3823 checkpoint_header);
3824 if (err != DB_SUCCESS) {
3825 return err;
3826 }
3827
3828 checkpoint_file_handle.close();
3829
3830 const lsn_t checkpoint_lsn = checkpoint.m_checkpoint_lsn;
3831
3832 ut_a(checkpoint_lsn == checkpoint_header.m_checkpoint_lsn);
3833
3834 /* Start reading the log from the checkpoint LSN up. */
3835
3836 ut_ad(RECV_SCAN_SIZE <= log.buf_size);
3837
3838 ut_ad(recv_sys->n_addrs == 0);
3839
3840 /* NOTE: we always do a 'recovery' at startup, but only if
3841 there is something wrong we will print a message to the
3842 user about recovery: */
3843
3844 if (checkpoint_lsn != flush_lsn) {
3845 if (checkpoint_lsn < flush_lsn) {
3846 ib::warn(ER_IB_MSG_RECOVERY_CHECKPOINT_FROM_BEFORE_CLEAN_SHUTDOWN,
3847 ulonglong{checkpoint_lsn}, ulonglong{flush_lsn});
3848 }
3849
3850 if (!recv_needed_recovery) {
3851 ib::info(ER_IB_MSG_RECOVERY_IS_NEEDED, ulonglong{flush_lsn},
3852 ulonglong{checkpoint_lsn});
3853
3854 if (srv_read_only_mode) {
3855 ib::error(ER_IB_MSG_RECOVERY_IN_READ_ONLY);
3856
3857 return DB_ERROR;
3858 }
3859
3860 recv_init_crash_recovery();
3861 }
3862 }
3863
3864 err = recv_recovery_begin(log, checkpoint_lsn);
3865 if (err != DB_SUCCESS) {
3866 return err;
3867 }
3868
3869 if (srv_read_only_mode && log.m_scanned_lsn > checkpoint_lsn) {
3870 ib::error(ER_IB_MSG_RECOVERY_IN_READ_ONLY);
3871 return DB_ERROR;
3872 }
3873
3874 lsn_t recovered_lsn;
3875
3876 recovered_lsn = recv_sys->recovered_lsn;
3877
3878 ut_a(recv_needed_recovery || checkpoint_lsn == recovered_lsn);
3879
3880 ut_a(!srv_read_only_mode || !recv_needed_recovery);
3881 ut_a(!srv_read_only_mode || checkpoint_lsn == recovered_lsn);
3882
3883 log.recovered_lsn = recovered_lsn;
3884
3885 ut_a(log.m_files.find(recovered_lsn) != log.m_files.end());
3886
3887 /* If it is at block boundary, add header size. */
3888 auto check_scanned_lsn = log.m_scanned_lsn;
3889 if (check_scanned_lsn % OS_FILE_LOG_BLOCK_SIZE == 0) {
3890 check_scanned_lsn += LOG_BLOCK_HDR_SIZE;
3891 }
3892
3893 if (check_scanned_lsn < checkpoint_lsn ||
3894 check_scanned_lsn < recv_max_page_lsn) {
3895 ib::error(ER_IB_MSG_737, ulonglong{log.m_scanned_lsn},
3896 ulonglong{checkpoint_lsn}, ulonglong{recv_max_page_lsn});
3897 }
3898
3899 if (recovered_lsn < checkpoint_lsn) {
3900 /* No harm in trying to do RO access. */
3901 if (!srv_read_only_mode) {
3902 ut_error;
3903 }
3904
3905 return DB_ERROR;
3906 }
3907
3908 if ((recv_sys->found_corrupt_log && srv_force_recovery == 0) ||
3909 recv_sys->found_corrupt_fs) {
3910 return DB_ERROR;
3911 }
3912
3913 /* Disallow checkpoints until recovery is finished, and changes gathered
3914 in recv_sys->metadata_recover (dict_metadata) are transferred to
3915 dict_table_t objects (happens in srv0start.cc). */
3916
3917 err = log_start(log, checkpoint_lsn, recovered_lsn, false);
3918 if (err != DB_SUCCESS) {
3919 return err;
3920 }
3921
3922 /* Make the preservation of max checkpoint info on disk certain by writing
3923 the checkpoint also to the other checkpoint header. After that both headers
3924 will have the same checkpoint_lsn. This is an extra protection in case next
3925 checkpoint write will become corrupted because of crash during the write. */
3926
3927 if (!srv_read_only_mode) {
3928 log.next_checkpoint_header_no =
3929 log_next_checkpoint_header(checkpoint.m_checkpoint_header_no);
3930
3931 err = log_files_next_checkpoint(log, checkpoint_lsn);
3932 if (err != DB_SUCCESS) {
3933 return err;
3934 }
3935 }
3936
3937 mutex_enter(&recv_sys->mutex);
3938 recv_sys->apply_log_recs = true;
3939 mutex_exit(&recv_sys->mutex);
3940
3941 /* The database is now ready to start almost normal processing of user
3942 transactions: transaction rollbacks and the application of the log
3943 records in the hash table can be run in background. */
3944
3945 return DB_SUCCESS;
3946 }
3947
3948 /** Check the page type, if there is a mismtach then throw
3949 fatal error. It may so happen that data file before 5.7 GA version
3950 may contain uninitialized bytes in the FIL_PAGE_TYPE field.
3951 @param[in] page_id Page id to verify
3952 @param[in] type Expected page type
3953 */
3954 static void verify_page_type(page_id_t page_id, page_type_t type) {
3955 mtr_t mtr;
3956 mtr_start(&mtr);
3957 /* We should not write to redo log before checkpointing is enabled as it risks
3958 running out of space, and we don't expect to write anything in this mtr.
3959 It should be read only */
3960 mtr_set_log_mode(&mtr, MTR_LOG_NO_REDO);
3961
3962 const auto *block =
3963 buf_page_get(page_id, univ_page_size, RW_S_LATCH, UT_LOCATION_HERE, &mtr);
3964
3965 const auto page_type = fil_page_get_type(block->frame);
3966 if (page_type != type) {
3967 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_INVALID_PAGE_TYPE, unsigned{type},
3968 unsigned{page_type}, ulong{page_id.space()},
3969 ulong{page_id.page_no()});
3970 }
3971 mtr_commit(&mtr);
3972 }
3973
3974 MetadataRecover *recv_recovery_from_checkpoint_finish(bool aborting) {
3975 /* Make sure that the recv_writer thread is done. This is
3976 required because it grabs various mutexes and we want to
3977 ensure that when we enable sync_order_checks there is no
3978 mutex currently held by any thread. */
3979 mutex_enter(&recv_sys->writer_mutex);
3980
3981 /* Restore state. */
3982 if (recv_sys->is_meb_db) dblwr::g_mode = recv_sys->dblwr_state;
3983
3984 /* Free the resources of the recovery system */
3985 recv_recovery_on = false;
3986
3987 /* By acquiring the mutex we ensure that the recv_writer thread
3988 won't trigger any more LRU batches. Now wait for currently
3989 in progress batches to finish. */
3990 buf_flush_wait_LRU_batch_end();
3991
3992 mutex_exit(&recv_sys->writer_mutex);
3993
3994 uint32_t count = 0;
3995
3996 while (recv_writer_is_active()) {
3997 ++count;
3998
3999 std::this_thread::sleep_for(std::chrono::milliseconds(100));
4000
4001 if (count >= 600) {
4002 ib::info(ER_IB_MSG_738);
4003 count = 0;
4004 }
4005 }
4006
4007 MetadataRecover *metadata{};
4008
4009 if (!aborting) {
4010 std::swap(metadata, recv_sys->metadata_recover);
4011 }
4012
4013 recv_sys_free();
4014
4015 if (!aborting) {
4016 /* Validate a few system page types that were left uninitialized
4017 by older versions of MySQL. */
4018 verify_page_type({IBUF_SPACE_ID, FSP_IBUF_HEADER_PAGE_NO},
4019 FIL_PAGE_TYPE_SYS);
4020 verify_page_type({TRX_SYS_SPACE, FSP_FIRST_RSEG_PAGE_NO},
4021 FIL_PAGE_TYPE_SYS);
4022 verify_page_type({TRX_SYS_SPACE, TRX_SYS_PAGE_NO}, FIL_PAGE_TYPE_TRX_SYS);
4023 verify_page_type({TRX_SYS_SPACE, FSP_DICT_HDR_PAGE_NO}, FIL_PAGE_TYPE_SYS);
4024 }
4025
4026 /* Free up the flush_rbt. */
4027 buf_flush_free_flush_rbt();
4028
4029 return metadata;
4030 }
4031
4032 #endif /* !UNIV_HOTBACKUP */
4033
4034 #if defined(UNIV_DEBUG) || defined(UNIV_HOTBACKUP)
4035 /** Return string name of the redo log record type.
4036 @param[in] type record log record enum
4037 @return string name of record log record */
4038 const char *get_mlog_string(mlog_id_t type) {
4039 switch (type) {
4040 case MLOG_SINGLE_REC_FLAG:
4041 return "MLOG_SINGLE_REC_FLAG";
4042
4043 case MLOG_1BYTE:
4044 return "MLOG_1BYTE";
4045
4046 case MLOG_2BYTES:
4047 return "MLOG_2BYTES";
4048
4049 case MLOG_4BYTES:
4050 return "MLOG_4BYTES";
4051
4052 case MLOG_8BYTES:
4053 return "MLOG_8BYTES";
4054
4055 case MLOG_REC_INSERT_8027:
4056 return "MLOG_REC_INSERT_8027";
4057
4058 case MLOG_REC_CLUST_DELETE_MARK_8027:
4059 return "MLOG_REC_CLUST_DELETE_MARK_8027";
4060
4061 case MLOG_REC_SEC_DELETE_MARK:
4062 return "MLOG_REC_SEC_DELETE_MARK";
4063
4064 case MLOG_REC_UPDATE_IN_PLACE_8027:
4065 return "MLOG_REC_UPDATE_IN_PLACE_8027";
4066
4067 case MLOG_REC_DELETE_8027:
4068 return "MLOG_REC_DELETE_8027";
4069
4070 case MLOG_LIST_END_DELETE_8027:
4071 return "MLOG_LIST_END_DELETE_8027";
4072
4073 case MLOG_LIST_START_DELETE_8027:
4074 return "MLOG_LIST_START_DELETE_8027";
4075
4076 case MLOG_LIST_END_COPY_CREATED_8027:
4077 return "MLOG_LIST_END_COPY_CREATED_8027";
4078
4079 case MLOG_PAGE_REORGANIZE_8027:
4080 return "MLOG_PAGE_REORGANIZE_8027";
4081
4082 case MLOG_PAGE_CREATE:
4083 return "MLOG_PAGE_CREATE";
4084
4085 case MLOG_UNDO_INSERT:
4086 return "MLOG_UNDO_INSERT";
4087
4088 case MLOG_UNDO_ERASE_END:
4089 return "MLOG_UNDO_ERASE_END";
4090
4091 case MLOG_UNDO_INIT:
4092 return "MLOG_UNDO_INIT";
4093
4094 case MLOG_UNDO_HDR_REUSE:
4095 return "MLOG_UNDO_HDR_REUSE";
4096
4097 case MLOG_UNDO_HDR_CREATE:
4098 return "MLOG_UNDO_HDR_CREATE";
4099
4100 case MLOG_REC_MIN_MARK:
4101 return "MLOG_REC_MIN_MARK";
4102
4103 case MLOG_IBUF_BITMAP_INIT:
4104 return "MLOG_IBUF_BITMAP_INIT";
4105
4106 #ifdef UNIV_LOG_LSN_DEBUG
4107 case MLOG_LSN:
4108 return "MLOG_LSN";
4109 #endif /* UNIV_LOG_LSN_DEBUG */
4110
4111 case MLOG_INIT_FILE_PAGE:
4112 return "MLOG_INIT_FILE_PAGE";
4113
4114 case MLOG_WRITE_STRING:
4115 return "MLOG_WRITE_STRING";
4116
4117 case MLOG_MULTI_REC_END:
4118 return "MLOG_MULTI_REC_END";
4119
4120 case MLOG_DUMMY_RECORD:
4121 return "MLOG_DUMMY_RECORD";
4122
4123 case MLOG_FILE_DELETE:
4124 return "MLOG_FILE_DELETE";
4125
4126 case MLOG_COMP_REC_MIN_MARK:
4127 return "MLOG_COMP_REC_MIN_MARK";
4128
4129 case MLOG_COMP_PAGE_CREATE:
4130 return "MLOG_COMP_PAGE_CREATE";
4131
4132 case MLOG_COMP_REC_INSERT_8027:
4133 return "MLOG_COMP_REC_INSERT_8027";
4134
4135 case MLOG_COMP_REC_CLUST_DELETE_MARK_8027:
4136 return "MLOG_COMP_REC_CLUST_DELETE_MARK_8027";
4137
4138 case MLOG_COMP_REC_SEC_DELETE_MARK:
4139 return "MLOG_COMP_REC_SEC_DELETE_MARK";
4140
4141 case MLOG_COMP_REC_UPDATE_IN_PLACE_8027:
4142 return "MLOG_COMP_REC_UPDATE_IN_PLACE_8027";
4143
4144 case MLOG_COMP_REC_DELETE_8027:
4145 return "MLOG_COMP_REC_DELETE_8027";
4146
4147 case MLOG_COMP_LIST_END_DELETE_8027:
4148 return "MLOG_COMP_LIST_END_DELETE_8027";
4149
4150 case MLOG_COMP_LIST_START_DELETE_8027:
4151 return "MLOG_COMP_LIST_START_DELETE_8027";
4152
4153 case MLOG_COMP_LIST_END_COPY_CREATED_8027:
4154 return "MLOG_COMP_LIST_END_COPY_CREATED_8027";
4155
4156 case MLOG_COMP_PAGE_REORGANIZE_8027:
4157 return "MLOG_COMP_PAGE_REORGANIZE_8027";
4158
4159 case MLOG_FILE_CREATE:
4160 return "MLOG_FILE_CREATE";
4161
4162 case MLOG_ZIP_WRITE_NODE_PTR:
4163 return "MLOG_ZIP_WRITE_NODE_PTR";
4164
4165 case MLOG_ZIP_WRITE_BLOB_PTR:
4166 return "MLOG_ZIP_WRITE_BLOB_PTR";
4167
4168 case MLOG_ZIP_WRITE_HEADER:
4169 return "MLOG_ZIP_WRITE_HEADER";
4170
4171 case MLOG_ZIP_PAGE_COMPRESS:
4172 return "MLOG_ZIP_PAGE_COMPRESS";
4173
4174 case MLOG_ZIP_PAGE_COMPRESS_NO_DATA_8027:
4175 return "MLOG_ZIP_PAGE_COMPRESS_NO_DATA_8027";
4176
4177 case MLOG_ZIP_PAGE_REORGANIZE_8027:
4178 return "MLOG_ZIP_PAGE_REORGANIZE_8027";
4179
4180 case MLOG_FILE_RENAME:
4181 return "MLOG_FILE_RENAME";
4182
4183 case MLOG_FILE_EXTEND:
4184 return "MLOG_FILE_EXTEND";
4185
4186 case MLOG_PAGE_CREATE_RTREE:
4187 return "MLOG_PAGE_CREATE_RTREE";
4188
4189 case MLOG_COMP_PAGE_CREATE_RTREE:
4190 return "MLOG_COMP_PAGE_CREATE_RTREE";
4191
4192 case MLOG_INIT_FILE_PAGE2:
4193 return "MLOG_INIT_FILE_PAGE2";
4194
4195 case MLOG_INDEX_LOAD:
4196 return "MLOG_INDEX_LOAD";
4197
4198 /* Disabled for WL6378
4199 case MLOG_TRUNCATE:
4200 return "MLOG_TRUNCATE";
4201 */
4202
4203 case MLOG_TABLE_DYNAMIC_META:
4204 return "MLOG_TABLE_DYNAMIC_META";
4205
4206 case MLOG_PAGE_CREATE_SDI:
4207 return "MLOG_PAGE_CREATE_SDI";
4208
4209 case MLOG_COMP_PAGE_CREATE_SDI:
4210 return "MLOG_COMP_PAGE_CREATE_SDI";
4211
4212 case MLOG_REC_INSERT:
4213 return "MLOG_REC_INSERT";
4214
4215 case MLOG_REC_CLUST_DELETE_MARK:
4216 return "MLOG_REC_CLUST_DELETE_MARK";
4217
4218 case MLOG_REC_DELETE:
4219 return "MLOG_REC_DELETE";
4220
4221 case MLOG_REC_UPDATE_IN_PLACE:
4222 return "MLOG_REC_UPDATE_IN_PLACE";
4223
4224 case MLOG_LIST_END_COPY_CREATED:
4225 return "MLOG_LIST_END_COPY_CREATED";
4226
4227 case MLOG_PAGE_REORGANIZE:
4228 return "MLOG_PAGE_REORGANIZE";
4229
4230 case MLOG_ZIP_PAGE_REORGANIZE:
4231 return "MLOG_ZIP_PAGE_REORGANIZE";
4232
4233 case MLOG_ZIP_PAGE_COMPRESS_NO_DATA:
4234 return "MLOG_ZIP_PAGE_COMPRESS_NO_DATA";
4235
4236 case MLOG_LIST_END_DELETE:
4237 return "MLOG_LIST_END_DELETE";
4238
4239 case MLOG_LIST_START_DELETE:
4240 return "MLOG_LIST_START_DELETE";
4241
4242 case MLOG_TEST:
4243 return "MLOG_TEST";
4244 }
4245
4246 assert(0);
4247
4248 return nullptr;
4249 }
4250 #endif /* UNIV_DEBUG || UNIV_HOTBACKUP */