Debian Codegraph @

storage/innobase/include/log0recv.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 1997, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/log0recv.h
29 Recovery
30
31 Created 9/20/1997 Heikki Tuuri
32 *******************************************************/
33
34 #ifndef log0recv_h
35 #define log0recv_h
36
37 #include "buf0types.h"
38 #include "dict0types.h"
39 #include "hash0hash.h"
40 #include "log0sys.h"
41 #include "mtr0types.h"
42
43 /* OS_FILE_LOG_BLOCK_SIZE */
44 #include "os0file.h"
45
46 #include "ut0byte.h"
47 #include "ut0new.h"
48
49 #include <list>
50 #include <unordered_map>
51 #include <unordered_set>
52
53 class MetadataRecover;
54 class PersistentTableMetadata;
55
56 #ifdef UNIV_HOTBACKUP
57
58 struct recv_addr_t;
59
60 /** list of tablespaces, that experienced an inplace DDL during a backup op */
61 extern std::list<std::pair<space_id_t, lsn_t>> index_load_list;
62 /** the last redo log flush len as seen by MEB */
63 extern volatile lsn_t backup_redo_log_flushed_lsn;
64 /** true when the redo log is being backed up */
65 extern bool recv_is_making_a_backup;
66
67 /** Scans the log segment and n_bytes_scanned is set to the length of valid
68 log scanned.
69 @param[in] buf buffer containing log data
70 @param[in] buf_len data length in that buffer
71 @param[in,out] scanned_lsn lsn of buffer start, we return scanned
72 lsn
73 @param[in,out] scanned_checkpoint_no 4 lowest bytes of the highest scanned
74 @param[out] block_no highest block no in scanned buffer.
75 checkpoint number so far
76 @param[out] n_bytes_scanned how much we were able to scan, smaller
77 than buf_len if log data ended here
78 @param[out] has_encrypted_log set true, if buffer contains encrypted
79 redo log, set false otherwise */
80 void meb_scan_log_seg(byte *buf, size_t buf_len, lsn_t *scanned_lsn,
81 uint32_t *scanned_checkpoint_no, uint32_t *block_no,
82 size_t *n_bytes_scanned, bool *has_encrypted_log);
83
84 /** Applies the hashed log records to the page, if the page lsn is less than the
85 lsn of a log record. This can be called when a buffer page has just been
86 read in, or also for a page already in the buffer pool.
87
88
89 @param[in,out] block buffer block */
90 void recv_recover_page_func(buf_block_t *block);
91
92 /** Wrapper for recv_recover_page_func().
93 Applies the hashed log records to the page, if the page lsn is less than the
94 lsn of a log record. This can be called when a buffer page has just been
95 read in, or also for a page already in the buffer pool.
96 @param jri in: true if just read in (the i/o handler calls this for
97 a freshly read page)
98 @param block in,out: the buffer block
99 */
100 static inline void recv_recover_page(bool jri [[maybe_unused]],
101 buf_block_t *block) {
102 recv_recover_page_func(block);
103 }
104
105 /** Applies log records in the hash table to a backup. */
106 void meb_apply_log_recs(void);
107
108 /** Applies log records in the hash table to a backup using a callback
109 functions.
110 @param[in] apply_log_record_function function for apply
111 @param[in] wait_till_done_function function for wait */
112 void meb_apply_log_recs_via_callback(
113 void (*apply_log_record_function)(recv_addr_t *),
114 void (*wait_till_done_function)());
115
116 /** Applies a log record in the hash table to a backup.
117 @param[in] recv_addr chain of log records
118 @param[in,out] block buffer block to apply the records to */
119 void meb_apply_log_record(recv_addr_t *recv_addr, buf_block_t *block);
120
121 /** Process a file name passed as an input
122 @param[in] name absolute path of tablespace file
123 @param[in] space_id the tablespace ID
124 @retval true if able to process file successfully.
125 @retval false if unable to process the file */
126 void meb_fil_name_process(const char *name, space_id_t space_id);
127
128 /** Scans log from a buffer and stores new log data to the parsing buffer.
129 Parses and hashes the log records if new data found. Unless
130 UNIV_HOTBACKUP is defined, this function will apply log records
131 automatically when the hash table becomes full.
132 @param[in] available_memory we let the hash table of recs
133 to grow to this size, at the maximum
134 @param[in] buf buffer containing a log
135 segment or garbage
136 @param[in] len buffer length
137 @param[in] start_lsn buffer start lsn
138 @param[out] group_scanned_lsn scanning succeeded up to this lsn
139 @retval true if limit_lsn has been reached, or not able to scan any
140 more in this log group
141 @retval false otherwise */
142 bool meb_scan_log_recs(size_t available_memory, const byte *buf, size_t len,
143 lsn_t start_lsn, lsn_t *group_scanned_lsn);
144
145 /** Check the 4-byte checksum to the trailer checksum field of a log
146 block.
147 @param[in] block pointer to a log block
148 @return whether the checksum matches */
149 bool log_block_checksum_is_ok(const byte *block);
150 #else /* UNIV_HOTBACKUP */
151
152 /** Applies the hashed log records to the page, if the page lsn is less than the
153 lsn of a log record. This can be called when a buffer page has just been
154 read in, or also for a page already in the buffer pool.
155
156
157 @param[in] just_read_in true if the IO handler calls this for a freshly
158 read page
159 @param[in,out] block buffer block */
160 void recv_recover_page_func(bool just_read_in, buf_block_t *block);
161
162 /** Wrapper for recv_recover_page_func().
163 Applies the hashed log records to the page, if the page lsn is less than the
164 lsn of a log record. This can be called when a buffer page has just been
165 read in, or also for a page already in the buffer pool.
166 @param jri in: true if just read in (the i/o handler calls this for
167 a freshly read page)
168 @param[in,out] block buffer block */
169 static inline void recv_recover_page(bool jri, buf_block_t *block) {
170 recv_recover_page_func(jri, block);
171 }
172
173 #endif /* UNIV_HOTBACKUP */
174
175 /** Frees the recovery system. */
176 void recv_sys_free();
177
178 /** Reset the state of the recovery system variables. */
179 void recv_sys_var_init();
180
181 #ifdef UNIV_HOTBACKUP
182 /** Get the number of bytes used by all the heaps
183 @return number of bytes used */
184 size_t meb_heap_used();
185 #endif /* UNIV_HOTBACKUP */
186
187 /** Returns true if recovery is currently running.
188 @return recv_recovery_on */
189 [[nodiscard]] static inline bool recv_recovery_is_on();
190
191 /** Returns true if the page is brand new (the next log record is init_file_page
192 or no records to apply).
193 @param[in] block buffer block
194 @return true if brand new */
195 bool recv_page_is_brand_new(buf_block_t *block);
196
197 /** Start recovering from a redo log checkpoint.
198 @see recv_recovery_from_checkpoint_finish
199 @param[in,out] log redo log
200 @param[in] flush_lsn lsn stored at offset FIL_PAGE_FILE_FLUSH_LSN
201 in the system tablespace header
202 @return error code or DB_SUCCESS */
203 [[nodiscard]] dberr_t recv_recovery_from_checkpoint_start(log_t &log,
204 lsn_t flush_lsn);
205
206 /** Determine if a redo log from a version before MySQL 8.0.30 is clean.
207 @param[in,out] log redo log
208 @return error code
209 @retval DB_SUCCESS if the redo log is clean
210 @retval DB_ERROR if the redo log is corrupted or dirty */
211 dberr_t recv_verify_log_is_clean_pre_8_0_30(log_t &log);
212
213 /** Complete the recovery from the latest checkpoint.
214 @param[in] aborting true if the server has to abort due to an error
215 @return recovered persistent metadata or nullptr if aborting*/
216 [[nodiscard]] MetadataRecover *recv_recovery_from_checkpoint_finish(
217 bool aborting);
218
219 /** Creates the recovery system. */
220 void recv_sys_create();
221
222 /** Release recovery system mutexes. */
223 void recv_sys_close();
224
225 /** Inits the recovery system for a recovery operation. */
226 void recv_sys_init();
227
228 /** Calculates the new value for lsn when more data is added to the log.
229 @param[in] lsn Old LSN
230 @param[in] len This many bytes of data is added, log block
231 headers not included
232 @return LSN after data addition */
233 lsn_t recv_calc_lsn_on_data_add(lsn_t lsn, os_offset_t len);
234
235 /** Empties the hash table of stored log records, applying them to appropriate
236 pages.
237 @param[in,out] log redo log
238 @param[in] allow_ibuf if true, ibuf operations are allowed during
239 the application; if false, no ibuf operations
240 are allowed, and after the application all
241 file pages are flushed to disk and invalidated
242 in buffer pool: this alternative means that
243 no new log records can be generated during
244 the application; the caller must in this case
245 own the log mutex */
246 dberr_t recv_apply_hashed_log_recs(log_t &log, bool allow_ibuf);
247
248 #if defined(UNIV_DEBUG) || defined(UNIV_HOTBACKUP)
249 /** Return string name of the redo log record type.
250 @param[in] type record log record enum
251 @return string name of record log record */
252 const char *get_mlog_string(mlog_id_t type);
253 #endif /* UNIV_DEBUG || UNIV_HOTBACKUP */
254
255 /** Block of log record data */
256 struct recv_data_t {
257 /** pointer to the next block or NULL. The log record data
258 is stored physically immediately after this struct, max amount
259 RECV_DATA_BLOCK_SIZE bytes of it */
260
261 recv_data_t *next;
262 };
263
264 /** Stored log record struct */
265 struct recv_t {
266 using Node = UT_LIST_NODE_T(recv_t);
267
268 /** Log record type */
269 mlog_id_t type;
270
271 /** Log record body length in bytes */
272 ulint len;
273
274 /** Chain of blocks containing the log record body */
275 recv_data_t *data;
276
277 /** Start lsn of the log segment written by the mtr which generated
278 this log record: NOTE that this is not necessarily the start lsn of
279 this log record */
280 lsn_t start_lsn;
281
282 /** End lsn of the log segment written by the mtr which generated
283 this log record: NOTE that this is not necessarily the end LSN of
284 this log record */
285 lsn_t end_lsn;
286
287 /** List node, list anchored in recv_addr_t */
288 Node rec_list;
289 };
290
291 /** States of recv_addr_t */
292 enum recv_addr_state {
293
294 /** not yet processed */
295 RECV_NOT_PROCESSED,
296
297 /** page is being read */
298 RECV_BEING_READ,
299
300 /** log records are being applied on the page */
301 RECV_BEING_PROCESSED,
302
303 /** log records have been applied on the page */
304 RECV_PROCESSED,
305
306 /** log records have been discarded because the tablespace
307 does not exist */
308 RECV_DISCARDED
309 };
310
311 /** Hashed page file address struct */
312 struct recv_addr_t {
313 using List = UT_LIST_BASE_NODE_T(recv_t, rec_list);
314
315 /** recovery state of the page */
316 recv_addr_state state;
317
318 /** Space ID */
319 space_id_t space;
320
321 /** Page number */
322 page_no_t page_no;
323
324 /** List of log records for this page */
325 List rec_list;
326 };
327
328 // Forward declaration
329 namespace dblwr {
330 namespace recv {
331 class DBLWR;
332 }
333 } // namespace dblwr
334
335 /** Class to parse persistent dynamic metadata redo log, store and
336 merge them and apply them to in-memory table objects finally */
337 class MetadataRecover {
338 using PersistentTables = std::map<
339 table_id_t, PersistentTableMetadata *, std::less<table_id_t>,
340 ut::allocator<std::pair<const table_id_t, PersistentTableMetadata *>>>;
341
342 public:
343 /** Default constructor */
344 MetadataRecover() UNIV_NOTHROW = default;
345
346 /** Destructor */
347 ~MetadataRecover();
348
349 /** Parse a dynamic metadata redo log of a table and store
350 the metadata locally
351 @param[in] id table id
352 @param[in] version table dynamic metadata version
353 @param[in] ptr redo log start
354 @param[in] end end of redo log
355 @retval ptr to next redo log record, nullptr if this log record
356 was truncated */
357 byte *parseMetadataLog(table_id_t id, uint64_t version, byte *ptr, byte *end);
358
359 /** Store the collected persistent dynamic metadata to
360 mysql.innodb_dynamic_metadata */
361 void store();
362
363 /** If there is any metadata to be applied
364 @return true if any metadata to be applied, otherwise false */
365 bool empty() const { return m_tables.empty(); }
366
367 private:
368 /** Get the dynamic metadata of a specified table,
369 create a new one if not exist
370 @param[in] id table id
371 @return the metadata of the specified table */
372 PersistentTableMetadata *getMetadata(table_id_t id);
373
374 private:
375 /** Map used to store and merge persistent dynamic metadata */
376 PersistentTables m_tables;
377 };
378
379 /** Recovery system data structure */
380 struct recv_sys_t {
381 using Pages =
382 std::unordered_map<page_no_t, recv_addr_t *, std::hash<page_no_t>,
383 std::equal_to<page_no_t>>;
384
385 /** Every space has its own heap and pages that belong to it. */
386 struct Space {
387 /** Constructor
388 @param[in,out] heap Heap to use for the log records. */
389 explicit Space(mem_heap_t *heap) : m_heap(heap), m_pages() {}
390
391 /** Default constructor */
392 Space() : m_heap(), m_pages() {}
393
394 /** Memory heap of log records and file addresses */
395 mem_heap_t *m_heap;
396
397 /** Pages that need to be recovered */
398 Pages m_pages;
399 };
400
401 using Missing_Ids = std::unordered_set<space_id_t>;
402
403 using Spaces = std::unordered_map<space_id_t, Space, std::hash<space_id_t>,
404 std::equal_to<space_id_t>>;
405
406 /* Recovery encryption information */
407 struct Encryption_Key {
408 /** Tablespace ID */
409 space_id_t space_id;
410
411 /** LSN of REDO log encryption entry */
412 lsn_t lsn;
413
414 /** Encryption key */
415 byte *ptr;
416
417 /** Encryption IV */
418 byte *iv;
419 };
420
421 using Encryption_Keys = std::vector<Encryption_Key>;
422
423 /** Mini transaction log record. */
424 struct Mlog_record {
425 /* Space ID */
426 space_id_t space_id;
427 /* Page number */
428 page_no_t page_no;
429 /* Log type */
430 mlog_id_t type;
431 /* Log body */
432 const byte *body;
433 /* Record size */
434 size_t size;
435 };
436
437 using Mlog_records = std::vector<Mlog_record, ut::allocator<Mlog_record>>;
438
439 /** While scanning logs for multi-record mini-transaction (mtr), we have two
440 passes. In first pass, we check if all the logs of the mtr is present in
441 current recovery buffer or not. If yes, then in second pass we go through the
442 logs again the add to hash table for apply. To avoid parsing multiple times,
443 we save the parsed records in first pass and reuse them in second pass.
444
445 Parsing of redo log takes significant amount of time and this optimization of
446 avoiding second parse gave about 1.8x speed up on recovery scan time of 1G of
447 redo log from sysbench rw test.
448
449 There is currently no limit for maximum number of logs in an mtr. Practically,
450 from sysbench rw test recovery with 1G of redo log to recover from the record
451 count were spread from 3 - 1235 with majority between 600 - 700. So, it is
452 likely by saving 1k records we could avoid most of the re-parsing overhead.
453 Considering possible bigger number of records in other load and future changes
454 the limit for number of saved records is kept at 8k. The same value from the
455 contribution patch. The memory requirement 32 x 8k = 256k seems fine as one
456 time overhead for the entire instance. */
457 static constexpr size_t MAX_SAVED_MLOG_RECS = 8 * 1024;
458
459 /** Save mlog record information. Silently returns if cannot save. Works only
460 in single threaded recovery scanner.
461 @param[in] rec_num record number in multi record group
462 @param[in] space_id space ID for the log record
463 @param[in] page_no page number for the log record
464 @param[in] type log record type
465 @param[in] body pointer to log record body in recovery buffer
466 @param[in] len length of the log record */
467 void save_rec(size_t rec_num, space_id_t space_id, page_no_t page_no,
468 mlog_id_t type, const byte *body, size_t len) {
469 /* No more space to save log. */
470 if (rec_num >= MAX_SAVED_MLOG_RECS) {
471 return;
472 }
473
474 ut_ad(rec_num < saved_recs.size());
475
476 if (rec_num >= saved_recs.size()) {
477 return;
478 }
479
480 auto &saved_rec = saved_recs[rec_num];
481
482 saved_rec.space_id = space_id;
483 saved_rec.page_no = page_no;
484 saved_rec.type = type;
485 saved_rec.body = body;
486 saved_rec.size = len;
487 }
488
489 /** Return saved mlog record information, if there. Works only
490 in single threaded recovery scanner.
491 @param[in] rec_num record number in multi record group
492 @param[out] space_id space ID for the log record
493 @param[out] page_no page number for the log record
494 @param[out] type log record type
495 @param[out] body pointer to log record body in recovery buffer
496 @param[out] len length of the log record
497 @return true iff saved record data is found. */
498 bool get_saved_rec(size_t rec_num, space_id_t &space_id, page_no_t &page_no,
499 mlog_id_t &type, byte *&body, size_t &len) {
500 if (rec_num >= MAX_SAVED_MLOG_RECS) {
501 return false;
502 }
503
504 ut_ad(rec_num < saved_recs.size());
505
506 if (rec_num >= saved_recs.size()) {
507 return false;
508 }
509
510 auto &saved_rec = saved_recs[rec_num];
511
512 space_id = saved_rec.space_id;
513 page_no = saved_rec.page_no;
514 type = saved_rec.type;
515 body = const_cast<byte *>(saved_rec.body);
516 len = saved_rec.size;
517
518 return true;
519 }
520
521 #ifndef UNIV_HOTBACKUP
522
523 /*!< mutex protecting the fields apply_log_recs, n_addrs, and the
524 state field in each recv_addr struct */
525 ib_mutex_t mutex;
526
527 /** mutex coordinating flushing between recv_writer_thread and
528 the recovery thread. */
529 ib_mutex_t writer_mutex;
530
531 /** event to activate page cleaner threads */
532 os_event_t flush_start;
533
534 /** event to signal that the page cleaner has finished the request */
535 os_event_t flush_end;
536
537 /** type of the flush request. BUF_FLUSH_LRU: flush end of LRU,
538 keeping free blocks. BUF_FLUSH_LIST: flush all of blocks. */
539 buf_flush_t flush_type;
540
541 #else /* !UNIV_HOTBACKUP */
542 bool apply_file_operations;
543 #endif /* !UNIV_HOTBACKUP */
544
545 /** This is true when log rec application to pages is allowed;
546 this flag tells the i/o-handler if it should do log record
547 application */
548 bool apply_log_recs;
549
550 /** This is true when a log rec application batch is running */
551 bool apply_batch_on;
552
553 /** Buffer for parsing log records */
554 byte *buf;
555
556 /** Size of the parsing buffer */
557 size_t buf_len;
558
559 /** Amount of data in buf */
560 ulint len;
561
562 /** This is the lsn from which we were able to start parsing
563 log records and adding them to the hash table; zero if a suitable
564 start point not found yet */
565 lsn_t parse_start_lsn;
566
567 /** Checkpoint lsn that was used during recovery (read from file). */
568 lsn_t checkpoint_lsn;
569
570 /** Number of data bytes to ignore until we reach checkpoint_lsn. */
571 ulint bytes_to_ignore_before_checkpoint;
572
573 /** The log data has been scanned up to this lsn */
574 lsn_t scanned_lsn;
575
576 /** The log data has been scanned up to this epoch_no */
577 uint32_t scanned_epoch_no;
578
579 /** Start offset of non-parsed log records in buf */
580 ulint recovered_offset;
581
582 /** The log records have been parsed up to this lsn */
583 lsn_t recovered_lsn;
584
585 /** The previous value of recovered_lsn - before we parsed the last mtr.
586 It is equal to recovered_lsn before we parsed any mtr. This is used to
587 find moments in which recovered_lsn moves to the next block in which case
588 we should update the last_block_first_rec_group (described below). */
589 lsn_t previous_recovered_lsn;
590
591 /** Tracks what should be the proper value of first_rec_group field in the
592 header of the block to which recovered_lsn belongs. It might be also zero,
593 in which case it means we do not know. */
594 lsn_t last_block_first_mtr_boundary{};
595
596 /** Set when finding a corrupt log block or record, or there
597 is a log parsing buffer overflow */
598 bool found_corrupt_log;
599
600 /** Set when an inconsistency with the file system contents
601 is detected during log scan or apply */
602 bool found_corrupt_fs;
603
604 /** Data directory has been recognized as cloned data directory. */
605 bool is_cloned_db;
606
607 /** Data directory has been recognized as data directory from MEB. */
608 bool is_meb_db;
609
610 /** Doublewrite buffer state before MEB recovery starts. We restore to this
611 state after MEB recovery completes and disable the doublewrite buffer during
612 MEB recovery. */
613 bool dblwr_state;
614
615 /** Hash table of pages, indexed by SpaceID. */
616 Spaces *spaces;
617
618 /** Number of not processed hashed file addresses in the hash table */
619 ulint n_addrs;
620
621 /** Doublewrite buffer pages, destroyed after recovery completes */
622 dblwr::recv::DBLWR *dblwr;
623
624 /** We store and merge all table persistent data here during
625 scanning redo logs */
626 MetadataRecover *metadata_recover;
627
628 /** Encryption Key information per tablespace ID */
629 Encryption_Keys *keys;
630
631 /** Tablespace IDs that were ignored during redo log apply. */
632 Missing_Ids missing_ids;
633
634 /** Tablespace IDs that were explicitly deleted. */
635 Missing_Ids deleted;
636
637 /* Saved log records to avoid second round parsing log. */
638 Mlog_records saved_recs;
639 };
640
641 /** The recovery system */
642 extern recv_sys_t *recv_sys;
643
644 /** true when applying redo log records during crash recovery; false
645 otherwise. Note that this is false while a background thread is
646 rolling back incomplete transactions. */
647 extern volatile bool recv_recovery_on;
648
649 /** If the following is true, the buffer pool file pages must be invalidated
650 after recovery and no ibuf operations are allowed; this becomes true if
651 the log record hash table becomes too full, and log records must be merged
652 to file pages already before the recovery is finished: in this case no
653 ibuf operations are allowed, as they could modify the pages read in the
654 buffer pool before the pages have been recovered to the up-to-date state.
655
656 true means that recovery is running and no operations on the log files
657 are allowed yet: the variable name is misleading. */
658 extern bool recv_no_ibuf_operations;
659
660 /** true when recv_init_crash_recovery() has been called. */
661 extern bool recv_needed_recovery;
662
663 /** true if buf_page_is_corrupted() should check if the log sequence
664 number (FIL_PAGE_LSN) is in the future. Initially false, and set by
665 recv_recovery_from_checkpoint_start(). */
666 extern bool recv_lsn_checks_on;
667
668 /** Size of the parsing buffer; it must accommodate RECV_SCAN_SIZE many
669 times! */
670 constexpr uint32_t RECV_PARSING_BUF_SIZE = 2 * 1024 * 1024;
671
672 /** Size of block reads when the log groups are scanned forward to do a
673 roll-forward */
674 #define RECV_SCAN_SIZE (4 * UNIV_PAGE_SIZE)
675
676 /** This many frames must be left free in the buffer pool when we scan
677 the log and store the scanned log records in the buffer pool: we will
678 use these free frames to read in pages when we start applying the
679 log records to the database. */
680 extern ulint recv_n_pool_free_frames;
681
682 /** A list of tablespaces for which (un)encryption process was not
683 completed before crash. */
684 extern std::list<space_id_t> recv_encr_ts_list;
685
686 #include "log0recv.ic"
687
688 #endif