Debian Codegraph @

storage/innobase/log/log0log.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1995, 2026, Oracle and/or its affiliates.
4 Copyright (c) 2009, Google Inc.
5
6 Portions of this file contain modifications contributed and copyrighted by
7 Google, Inc. Those modifications are gratefully acknowledged and are described
8 briefly in the InnoDB documentation. The contributions by Google are
9 incorporated with their permission, and subject to the conditions contained in
10 the file COPYING.Google.
11
12 This program is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License, version 2.0, as published by the
14 Free Software Foundation.
15
16 This program is designed to work with certain software (including
17 but not limited to OpenSSL) that is licensed under separate terms,
18 as designated in a particular file or component or in included license
19 documentation. The authors of MySQL hereby grant you an additional
20 permission to link the program and your derivative works with the
21 separately licensed software that they have either included with
22 the program or referenced in the documentation.
23
24 This program is distributed in the hope that it will be useful, but WITHOUT
25 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
26 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
27 for more details.
28
29 You should have received a copy of the GNU General Public License along with
30 this program; if not, write to the Free Software Foundation, Inc.,
31 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32
33 *****************************************************************************/
34
35 /**************************************************/ /**
36 @file log/log0log.cc
37
38 Redo log system - provides durability for unflushed modifications
39 to contents of data pages.
40
41 This file covers general maintenance, including:
42 -# Allocation and deallocation of the redo log data structures.
43 -# Initialization and shutdown of the redo log.
44 -# Start / stop for the log background threads.
45 -# Runtime updates of server variables.
46 -# Extending size of the redo log buffers.
47 -# Locking redo position (for replication).
48
49 Created 12/9/1995 Heikki Tuuri
50 *******************************************************/
51
52 #ifndef UNIV_HOTBACKUP
53
54 /* time, difftime */
55 #include <time.h>
56
57 /* fprintf */
58 #include <cstdio>
59
60 /* std::memcpy */
61 #include <cstring>
62
63 /* arch_log_sys, arch_wake_threads */
64 #include "arch0arch.h"
65
66 /* dblwr::set(), ... */
67 #include "buf0dblwr.h"
68
69 /* fil_space_t */
70 #include "fil0fil.h"
71
72 /* log_update_buf_limit, ... */
73 #include "log0buf.h"
74
75 /* log_checkpointer_mutex */
76 #include "log0chkp.h"
77
78 /* log_calc_max_ages, ... */
79 #include "log0files_capacity.h"
80
81 /* log_create_new_files */
82 #include "log0files_governor.h"
83
84 /* log_limits_mutex, ... */
85 #include "log0log.h"
86
87 /* redo_log_archive_init, ... */
88 #include "log0meb.h"
89
90 /* log_pre_8_0_30::FILE_BASE_NAME */
91 #include "log0pre_8_0_30.h"
92
93 /* recv_sys->dblwr_state, ... */
94 #include "log0recv.h"
95
96 /* log_t::X */
97 #include "log0sys.h"
98
99 /* log_writer_mutex */
100 #include "log0write.h"
101
102 /* mtr_t::s_logging */
103 #include "mtr0mtr.h"
104
105 /* os_event_create, ... */
106 #include "os0event.h"
107
108 /* os_thread_create, ... */
109 #include "os0thread-create.h"
110
111 /* os_event_sleep */
112 #include "os0thread.h"
113
114 /* srv_log_buffer_size, ... */
115 #include "srv0srv.h"
116
117 /* srv_is_being_started */
118 #include "srv0start.h"
119
120 /* mysql_pfs_key_t */
121 #include "sync0sync.h"
122
123 /* LATCH_ID_LOG_WRITER, ... */
124 #include "sync0types.h"
125
126 /* ut_uint64_align_down */
127 #include "ut0byte.h"
128
129 /* Link_buf */
130 #include "ut0link_buf.h"
131
132 /* UT_DELETE_ARRAY */
133 #include "ut0new.h"
134
135 // clang-format off
136 /**
137 @page PAGE_INNODB_REDO_LOG Innodb redo log
138
139 @section sect_redo_log_general General idea of redo log
140
141 The redo log is a write ahead log of changes applied to contents of data pages.
142 It provides durability for all changes applied to the pages. In case of crash,
143 it is used to recover modifications to pages that were modified but have not
144 been flushed to disk.
145
146 @note In case of a shutdown (unless innodb-fast-shutdown = 2), the redo log
147 should be logically empty. This means that after the checkpoint lsn there
148 should be no records to apply. However the log files still could contain
149 some old data (which is not used during the recovery process).
150
151 Every change to content of a data page must be done through a mini-transaction
152 (so called mtr - mtr_t), which in mtr_commit() writes all its log records
153 to the redo log.
154
155 @remarks Normally these changes are performed using the mlog_write_ulint()
156 or similar function. In some page-level operations, only a code number of
157 a c-function and its parameters are written to the redo log, to reduce the
158 size of the redo log. You should not add parameters to such functions
159 (e.g. trx_undo_header_create(), trx_undo_insert_header_reuse()).
160 You should not add functionality which can either change when compared to older
161 versions, or which is dependent on data outside of the page being modified.
162 Therefore all functions must implement self-contained page transformation
163 and it should be unchanged if you don't have very essential reasons to change
164 the log semantics or format.
165
166 Single mtr can cover changes to multiple pages. In case of crash, either the
167 whole set of changes from a given mtr is recovered or none of the changes.
168
169 During life time of a mtr, a log of changes is collected inside an internal
170 buffer of the mtr. It contains multiple log records, which describe changes
171 applied to possibly different modified pages. When the mtr is committed, all
172 the log records are written to the log buffer within a single group of the log
173 records. Procedure:
174
175 -# Total number of data bytes of log records is calculated.
176 -# Space for the log records is reserved. Range of lsn values is assigned for
177 a group of log records.
178 -# %Log records are written to the reserved space in the log buffer.
179 -# Modified pages are marked as dirty and moved to flush lists.
180 All the dirty pages are marked with the same range of lsn values.
181 -# Reserved space is closed.
182
183 Background threads are responsible for writing of new changes in the log buffer
184 to the log files. User threads that require durability for the logged records,
185 have to wait until the log gets flushed up to the required point.
186
187 During recovery only complete groups of log records are recovered and applied.
188 Example given, if we had rotation in a tree, which resulted in changes to three
189 nodes (pages), we have a guarantee, that either the whole rotation is recovered
190 or nothing, so we will not end up with a tree that has incorrect structure.
191
192 Consecutive bytes written to the redo log are enumerated by the lsn values.
193 Every single byte written to the log buffer corresponds to current lsn
194 increased by one.
195
196 Data in the redo log is structured in consecutive blocks of 512 bytes
197 (_OS_FILE_LOG_BLOCK_SIZE_). Each block contains a header of 12 bytes
198 (_LOG_BLOCK_HDR_SIZE_) and a footer of 4 bytes (_LOG_BLOCK_TRL_SIZE_).
199 These extra bytes are also enumerated by lsn values. Whenever we refer to
200 data bytes, we mean actual bytes of log records - not bytes of headers and
201 footers of log blocks. The sequence of enumerated data bytes, is called the
202 sn values. All headers and footers of log blocks are added within the log
203 buffer, where data is actually stored in proper redo format.
204
205 When a user transaction commits, extra mtr is committed (related to undo log),
206 and then user thread waits until the redo log is flushed up to the point,
207 where log records of that mtr end.
208
209 When a dirty page is being flushed, a thread doing the flush, first needs to
210 wait until the redo log gets flushed up to the newest modification of the page.
211 Afterwards the page might be flushed. In case of crash, we might end up with
212 the newest version of the page and without any earlier versions of the page.
213 Then other pages, which potentially have not been flushed before the crash,
214 need to be recovered to that version. This applies to:
215 * pages modified within the same group of log records,
216 * and pages modified within any earlier group of log records.
217
218 @section sect_redo_log_architecture Architecture of redo log
219
220 @subsection subsect_redo_log_data_layers Data layers
221
222 Redo log consists of following data layers:
223
224 -# %Log files (typically 4 - 32 GB) - physical redo files that reside on
225 the disk.
226
227 -# %Log buffer (64 MB by default) - groups data to write to log files,
228 formats data in proper way: include headers/footers of log blocks,
229 calculates checksums, maintains boundaries of record groups.
230
231 -# %Log recent written buffer (e.g. 4MB) - tracks recent writes to the
232 log buffer. Allows to have concurrent writes to the log buffer and tracks
233 up to which lsn all such writes have been already finished.
234
235 -# %Log recent closed buffer (e.g. 4MB) - tracks for which recent writes,
236 corresponding dirty pages have been already added to the flush lists.
237 Allows to relax order in which dirty pages have to be added to the flush
238 lists and tracks up to which lsn, all dirty pages have been added.
239 This is required to not make checkpoint at lsn which is larger than
240 oldest_modification of some dirty page, which still has not been added
241 to the flush list (because user thread was scheduled out).
242
243 -# %Log write ahead buffer (e.g. 4kB) - used to write ahead more bytes
244 to the redo files, to avoid read-on-write problem. This buffer is also
245 used when we need to write an incomplete log block, which might
246 concurrently be receiving even more data from next user threads. In such
247 case we first copy the incomplete block to the write ahead buffer.
248
249 @subsection subsect_redo_log_general_rules General rules
250
251 -# User threads write their redo data only to the log buffer.
252
253 -# User threads write concurrently to the log buffer, without synchronization
254 between each other.
255
256 -# The log recent written buffer is maintained to track concurrent writes.
257
258 -# Background log threads write and flush the log buffer to disk.
259
260 -# User threads do not touch log files. Background log threads are the only
261 allowed to touch the log files.
262
263 -# User threads wait for the background threads when they need flushed redo.
264
265 -# %Events per log block are exposed by redo log for users interested in waiting
266 for the flushed redo.
267
268 -# Users can see up to which point log has been written / flushed.
269
270 -# User threads need to wait if there is no space in the log buffer.
271
272 @diafile storage/innobase/log/arch_writing.dia "Writing to the redo log"
273
274 -# User threads add dirty pages to flush lists in the relaxed order.
275
276 -# Order in which user threads reserve ranges of lsn values, order in which
277 they write to the log buffer, and order in which they add dirty pages to
278 flush lists, could all be three completely different orders.
279
280 -# User threads do not write checkpoints (are not allowed to touch log files).
281
282 -# Checkpoint is automatically written from time to time by a background thread.
283
284 -# User threads can request a forced write of checkpoint and wait.
285
286 -# User threads need to wait if there is no space in the log files.
287
288 @diafile storage/innobase/log/arch_deleting.dia "Reclaiming space in the redo log"
289
290 -# Well thought out and tested set of _MONITOR_ counters is maintained and
291 documented.
292
293 -# All settings are configurable through server variables, but the new server
294 variables are hidden unless a special _EXPERIMENTAL_ mode has been defined
295 when running cmake.
296
297 -# All the new buffers could be resized dynamically during runtime. In practice,
298 only size of the log buffer is accessible without the _EXPERIMENTAL_ mode.
299
300 @note
301 This is a functional change - the log buffer could be resized dynamically
302 by users (also decreased).
303
304 @section sect_redo_log_lsn_values Glossary of lsn values
305
306 Different fragments of head of the redo log are tracked by different values:
307 - @ref subsect_redo_log_write_lsn,
308 - @ref subsect_redo_log_buf_ready_for_write_lsn,
309 - @ref subsect_redo_log_sn.
310
311 Different fragments of the redo log's tail are tracked by different values:
312 - @ref subsect_redo_log_buf_dirty_pages_added_up_to_lsn,
313 - @ref subsect_redo_log_available_for_checkpoint_lsn,
314 - @ref subsect_redo_log_last_checkpoint_lsn.
315
316 @subsection subsect_redo_log_write_lsn log.write_lsn
317
318 Up to this lsn we have written all data to log files. It's the beginning of
319 the unwritten log buffer. Older bytes in the buffer are not required and might
320 be overwritten in cyclic manner for lsn values larger by _log.buf_size_.
321
322 Value is updated by: [log writer thread](@ref sect_redo_log_writer).
323
324 @subsection subsect_redo_log_buf_ready_for_write_lsn log.buf_ready_for_write_lsn
325
326 Up to this lsn, all concurrent writes to log buffer have been finished.
327 We don't need older part of the log recent-written buffer.
328
329 It obviously holds:
330
331 log.buf_ready_for_write_lsn >= log.write_lsn
332
333 Value is updated by: [log writer thread](@ref sect_redo_log_writer).
334
335 @subsection subsect_redo_log_flushed_to_disk_lsn log.flushed_to_disk_lsn
336
337 Up to this lsn, we have written and flushed data to log files.
338
339 It obviously holds:
340
341 log.flushed_to_disk_lsn <= log.write_lsn
342
343 Value is updated by: [log flusher thread](@ref sect_redo_log_flusher).
344
345 @subsection subsect_redo_log_sn log.sn
346
347 Corresponds to current lsn. Maximum assigned sn value (enumerates only
348 data bytes).
349
350 It obviously holds:
351
352 log.sn >= log_translate_lsn_to_sn(log.buf_ready_for_write_lsn)
353
354 Value is updated by: user threads during reservation of space.
355
356 @subsection subsect_redo_log_buf_dirty_pages_added_up_to_lsn
357 log.buf_dirty_pages_added_up_to_lsn
358
359 Up to this lsn user threads have added all dirty pages to flush lists.
360
361 The redo log records are allowed to be deleted not further than up to this lsn.
362 That's because there could be a page with _oldest_modification_ smaller than
363 the minimum _oldest_modification_ available in flush lists. Note that such page
364 is just about to be added to flush list by a user thread, but there is no mutex
365 protecting access to the minimum _oldest_modification_, which would be acquired
366 by the user thread before writing to redo log. Hence for any lsn greater than
367 _buf_dirty_pages_added_up_to_lsn_ we cannot trust that flush lists are complete
368 and minimum calculated value (or its approximation) is valid.
369
370 @note
371 Note that we do not delete redo log records physically, but we still can delete
372 them logically by doing checkpoint at given lsn.
373
374 It holds (unless the log writer thread misses an update of the
375 @ref subsect_redo_log_buf_ready_for_write_lsn):
376
377 log.buf_dirty_pages_added_up_to_lsn <= log.buf_ready_for_write_lsn.
378
379 @subsection subsect_redo_log_available_for_checkpoint_lsn
380 log.available_for_checkpoint_lsn
381
382 Up to this lsn all dirty pages have been flushed to disk. However, this value
383 is not guaranteed to be the maximum such value. As insertion order to flush
384 lists is relaxed, the buf_pool_get_oldest_modification_approx() returns
385 modification time of some page that was inserted the earliest, it doesn't
386 have to be the oldest modification though. However, the maximum difference
387 between the first page in flush list, and one with the oldest modification
388 lsn is limited by the number of entries in the log recent closed buffer.
389
390 That's why from result of buf_pool_get_oldest_modification_approx() size of
391 the log recent closed buffer is subtracted. The result is used to update the
392 lsn available for a next checkpoint.
393
394 This has impact on the redo format, because the checkpoint_lsn can now point
395 to the middle of some group of log records (even to the middle of a single
396 log record). Log files with such checkpoint are not recoverable by older
397 versions of InnoDB by default.
398
399 Value is updated by:
400 [log checkpointer thread](@ref sect_redo_log_checkpointer).
401
402 @see @ref sect_redo_log_add_dirty_pages
403
404 @subsection subsect_redo_log_last_checkpoint_lsn log.last_checkpoint_lsn
405
406 Up to this lsn all dirty pages have been flushed to disk and the lsn value
407 has been flushed to the header of the redo log file containing that lsn.
408
409 The lsn value points to place where recovery is supposed to start. Data bytes
410 for smaller lsn values are not required and might be overwritten (log files
411 are circular). One could consider them logically deleted.
412
413 Value is updated by:
414 [log checkpointer thread](@ref sect_redo_log_checkpointer).
415
416 It holds:
417
418 log.last_checkpoint_lsn
419 <= log.available_for_checkpoint_lsn
420 <= log.buf_dirty_pages_added_up_to_lsn.
421
422
423 Read more about redo log details:
424 - @subpage PAGE_INNODB_REDO_LOG_BUF
425 - @subpage PAGE_INNODB_REDO_LOG_THREADS
426 - @subpage PAGE_INNODB_REDO_LOG_FORMAT
427
428 *******************************************************/
429 // clang-format on
430
431 /** Redo log system. Singleton used to populate global pointer. */
432 ut::aligned_pointer<log_t, ut::INNODB_CACHE_LINE_SIZE> *log_sys_object;
433
434 /** Redo log system (singleton). */
435 log_t *log_sys;
436
437 #ifdef UNIV_PFS_MEMORY
438 PSI_memory_key log_buffer_memory_key;
439 #endif /* UNIV_PFS_MEMORY */
440
441 #ifdef UNIV_PFS_THREAD
442
443 /** PFS key for the log files governor thread. */
444 mysql_pfs_key_t log_files_governor_thread_key;
445
446 /** PFS key for the log writer thread. */
447 mysql_pfs_key_t log_writer_thread_key;
448
449 /** PFS key for the log checkpointer thread. */
450 mysql_pfs_key_t log_checkpointer_thread_key;
451
452 /** PFS key for the log flusher thread. */
453 mysql_pfs_key_t log_flusher_thread_key;
454
455 /** PFS key for the log flush notifier thread. */
456 mysql_pfs_key_t log_flush_notifier_thread_key;
457
458 /** PFS key for the log write notifier thread. */
459 mysql_pfs_key_t log_write_notifier_thread_key;
460
461 #endif /* UNIV_PFS_THREAD */
462
463 /** Allocates the log system and initializes all log mutexes and log events. */
464 static void log_sys_create();
465
466 /** Handles the log creator name stored on the disk (in the redo log files).
467 Does whatever is required to handle the creator name and decides if further
468 initialization of InnoDB might be allowed.
469 @remarks Recognizes log creator by its name and marks that within recv_sys
470 by setting recv_sys->is_meb_db or recv_sys->is_cloned_db. Disables the dblwr
471 if log files were created by MEB. Emits message to the error log when the
472 recognized creator name is not an usual MySQL's creator name.
473 @param[in] log redo log with log.m_creator_name read from files
474 @retval DB_SUCCESS if further initialization might be continued (note that
475 potentially the creator name could be recognized as an unknown one)
476 @retval DB_ERROR if further initialization must not be continued (for example
477 because a foreign creator name was detected but read-only mode is turned on) */
478 static dberr_t log_sys_handle_creator(log_t &log);
479
480 /** Recognizes log format and emits corresponding message to the error log.
481 Returns DB_ERROR if format is too old and no longer supported or format is
482 not the current one and innodb_force_recovery != 0 is passed.
483 @param[in] log redo log
484 @return DB_SUCCESS or error */
485 static dberr_t log_sys_check_format(const log_t &log);
486
487 /** Checks if the redo log directory exists, can be listed and contains
488 at least one redo log file.
489 @remarks Redo log file is recognized only by looking at the file name,
490 accordingly to the configured ruleset (@see log_configure()).
491 @param[in] ctx redo log files context
492 @param[in,out] path path to redo log directory checked during this
493 call
494 @param[out] found_files true iff found at least one redo log file
495 @retval DB_SUCCESS if succeeded to list the log directory
496 @retval DB_ERROR if failed to list the existing log directory
497 @retval DB_NOT_FOUND if the log directory does not exist */
498 static dberr_t log_sys_check_directory(const Log_files_context &ctx,
499 std::string &path, bool &found_files);
500
501 /** Free the log system data structures. Deallocate all the related memory. */
502 static void log_sys_free();
503
504 /** Calculates proper size for the log buffer and allocates the log buffer.
505 @param[out] log redo log */
506 static void log_allocate_buffer(log_t &log);
507
508 /** Deallocates the log buffer.
509 @param[out] log redo log */
510 static void log_deallocate_buffer(log_t &log);
511
512 /** Allocates the log write-ahead buffer (aligned to system page for
513 easier migrations between NUMA nodes).
514 @param[out] log redo log */
515 static void log_allocate_write_ahead_buffer(log_t &log);
516
517 /** Deallocates the log write-ahead buffer.
518 @param[out] log redo log */
519 static void log_deallocate_write_ahead_buffer(log_t &log);
520
521 /** Allocates the array with flush events.
522 @param[out] log redo log */
523 static void log_allocate_flush_events(log_t &log);
524
525 /** Deallocates the array with flush events.
526 @param[out] log redo log */
527 static void log_deallocate_flush_events(log_t &log);
528
529 /** Deallocates the array with write events.
530 @param[out] log redo log */
531 static void log_deallocate_write_events(log_t &log);
532
533 /** Allocates the array with write events.
534 @param[out] log redo log */
535 static void log_allocate_write_events(log_t &log);
536
537 /** Allocates the log recent written buffer.
538 @param[out] log redo log */
539 static void log_allocate_recent_written(log_t &log);
540
541 /** Deallocates the log recent written buffer.
542 @param[out] log redo log */
543 static void log_deallocate_recent_written(log_t &log);
544
545 /** Allocates the log recent closed buffer.
546 @param[out] log redo log */
547 static void log_allocate_recent_closed(log_t &log);
548
549 /** Deallocates the log recent closed buffer.
550 @param[out] log redo log */
551 static void log_deallocate_recent_closed(log_t &log);
552
553 /** Resets the log encryption buffer (used to write encryption headers).
554 @param[out] log redo log */
555 static void log_reset_encryption_buffer(log_t &log);
556
557 /** Calculates proper size of the log buffer and updates related fields.
558 Calculations are based on current value of srv_log_buffer_size. Note,
559 that the proper size of the log buffer should be a power of two.
560 @param[out] log redo log */
561 static void log_calc_buf_size(log_t &log);
562
563 /** Pauses writer, flusher and notifiers and switches user threads
564 to write log as former version.
565 NOTE: These pause/resume functions should be protected by mutex while serving.
566 The caller innodb_log_writer_threads_update() is protected
567 by LOCK_global_system_variables in mysqld.
568 @param[out] log redo log */
569 static void log_pause_writer_threads(log_t &log);
570
571 /** Resumes writer, flusher and notifiers and switches user threads
572 not to write log.
573 @param[out] log redo log */
574 static void log_resume_writer_threads(log_t &log);
575
576 /**************************************************/ /**
577
578 @name Allocation and deallocation of log_sys
579
580 *******************************************************/
581
582 /** @{ */
583
584 static void log_sys_create() {
585 ut_a(log_sys == nullptr);
586
587 /* The log_sys_object is pointer to aligned_pointer. That's
588 temporary solution until we refactor redo log more.
589
590 That's required for now, because the aligned_pointer, has dtor
591 which tries to free the memory and as long as this is global
592 variable it will have the dtor called. However because we can
593 exit without proper cleanup for redo log in some cases, we
594 need to forbid dtor calls then. */
595
596 using log_t_aligned_pointer = std::decay_t<decltype(*log_sys_object)>;
597 log_sys_object =
598 ut::new_withkey<log_t_aligned_pointer>(UT_NEW_THIS_FILE_PSI_KEY);
599 log_sys_object->alloc_withkey(UT_NEW_THIS_FILE_PSI_KEY);
600
601 log_sys = *log_sys_object;
602
603 log_t &log = *log_sys;
604
605 /* Initialize simple value fields. */
606 log.dict_persist_margin.store(0);
607 log.periodical_checkpoints_enabled = false;
608 log.m_format = Log_format::CURRENT;
609 log.m_creator_name = LOG_HEADER_CREATOR_CURRENT;
610 log.n_log_ios_old = log.n_log_ios;
611 log.last_printout_time = time(nullptr);
612 log.m_requested_files_consumption = false;
613 log.m_writer_inside_extra_margin = false;
614 log.m_oldest_need_lsn_lowerbound = 0;
615 log.m_unused_files_count = 0;
616 log.m_encryption_metadata = {};
617
618 log.checkpointer_event = os_event_create();
619 log.closer_event = os_event_create();
620 log.write_notifier_event = os_event_create();
621 log.flush_notifier_event = os_event_create();
622 log.writer_event = os_event_create();
623 log.flusher_event = os_event_create();
624 log.old_flush_event = os_event_create();
625 os_event_set(log.old_flush_event);
626 log.writer_threads_resume_event = os_event_create();
627 os_event_set(log.writer_threads_resume_event);
628 log.sn_lock_event = os_event_create();
629 os_event_set(log.sn_lock_event);
630 log.m_files_governor_event = os_event_create();
631 log.m_files_governor_iteration_event = os_event_create();
632 log.m_file_removed_event = os_event_create();
633 log.next_checkpoint_event = os_event_create();
634
635 mutex_create(LATCH_ID_LOG_CHECKPOINTER, &log.checkpointer_mutex);
636 mutex_create(LATCH_ID_LOG_CLOSER, &log.closer_mutex);
637 mutex_create(LATCH_ID_LOG_WRITER, &log.writer_mutex);
638 mutex_create(LATCH_ID_LOG_FLUSHER, &log.flusher_mutex);
639 mutex_create(LATCH_ID_LOG_WRITE_NOTIFIER, &log.write_notifier_mutex);
640 mutex_create(LATCH_ID_LOG_FLUSH_NOTIFIER, &log.flush_notifier_mutex);
641 mutex_create(LATCH_ID_LOG_LIMITS, &log.limits_mutex);
642 mutex_create(LATCH_ID_LOG_FILES, &log.m_files_mutex);
643 mutex_create(LATCH_ID_LOG_SN_MUTEX, &log.sn_x_lock_mutex);
644
645 #ifdef UNIV_PFS_RWLOCK
646 /* pfs_psi is separated from sn_lock_inst,
647 because not needed for non debug build. */
648 log.pfs_psi =
649 PSI_RWLOCK_CALL(init_rwlock)(log_sn_lock_key.m_value, &log.pfs_psi);
650 #endif /* UNIV_PFS_RWLOCK */
651 #ifdef UNIV_DEBUG
652 /* initialize rw_lock without pfs_psi */
653 log.sn_lock_inst = static_cast<rw_lock_t *>(
654 ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*log.sn_lock_inst)));
655 new (log.sn_lock_inst) rw_lock_t;
656 rw_lock_create_func(log.sn_lock_inst, LATCH_ID_LOG_SN, UT_LOCATION_HERE);
657 #endif /* UNIV_DEBUG */
658
659 /* Allocate buffers. */
660 log_allocate_buffer(log);
661 log_allocate_write_ahead_buffer(log);
662 log_allocate_recent_written(log);
663 log_allocate_recent_closed(log);
664 log_allocate_flush_events(log);
665 log_allocate_write_events(log);
666
667 log_reset_encryption_buffer(log);
668
669 log_calc_buf_size(log);
670
671 log_consumer_register(log, &(log.m_checkpoint_consumer));
672 }
673
674 static void log_fix_first_rec_group(lsn_t block_lsn,
675 Log_data_block_header &block_header,
676 lsn_t start_lsn) {
677 const uint16_t on_disc_value = block_header.m_first_rec_group;
678 uint16_t change_to_value = 0; // no change
679 if (recv_sys != nullptr && recv_sys->last_block_first_mtr_boundary != 0 &&
680 block_lsn < recv_sys->last_block_first_mtr_boundary &&
681 recv_sys->last_block_first_mtr_boundary <
682 block_lsn + OS_FILE_LOG_BLOCK_SIZE &&
683 on_disc_value !=
684 recv_sys->last_block_first_mtr_boundary % OS_FILE_LOG_BLOCK_SIZE) {
685 ut_a(log_is_data_lsn(recv_sys->last_block_first_mtr_boundary));
686 change_to_value =
687 recv_sys->last_block_first_mtr_boundary % OS_FILE_LOG_BLOCK_SIZE;
688 /* We must not start with invalid first_rec_group in the first block,
689 because if we crashed, we could be unable to recover. We do NOT have
690 guarantee that the first_rec_group was correct because recovery did
691 not report error. The first_rec_group was used only to locate the
692 beginning of the log for recovery. For later blocks it was not used.
693 It might be corrupted on disk and stay unnoticed if checksums for
694 log blocks are disabled. In such case it would be better to repair
695 it now instead of relying on the broken value and risking data loss.
696 We emit warning to notice user about the situation. We repair that
697 only in the log buffer. */
698 } else if (on_disc_value == 0) {
699 /* Again, if it was zero, for any reason, we prefer to fix it
700 before starting (we emit warning). */
701 change_to_value = start_lsn % OS_FILE_LOG_BLOCK_SIZE;
702 }
703 if (change_to_value != 0) {
704 ib::warn(ER_IB_RECV_FIRST_REC_GROUP_INVALID, uint(on_disc_value),
705 uint(change_to_value));
706 block_header.m_first_rec_group = change_to_value;
707 }
708 }
709
710 dberr_t log_start(log_t &log, lsn_t checkpoint_lsn, lsn_t start_lsn,
711 bool allow_checkpoints) {
712 ut_a(log_sys != nullptr);
713 ut_a(checkpoint_lsn >= OS_FILE_LOG_BLOCK_SIZE);
714 ut_a(checkpoint_lsn >= LOG_START_LSN);
715 ut_a(start_lsn >= checkpoint_lsn);
716 ut_a(arch_log_sys == nullptr || !arch_log_sys->is_active());
717 /* If flags in the header on disc indicate redo logging was disabled, then let
718 the module know logging should be turned off. We need to do it before anyone
719 attempts to write to the redo log, and conveniently the call to log_start()
720 indicates the moment the caller wants to start writing to the redo log. The
721 mtr_t::s_logging.disable(..) will set LOG_HEADER_FLAG_CRASH_UNSAFE flag in
722 redo header on disc, which we only know how to do in the currently supported
723 format. We should never write to files which are in the older format. This is
724 why we call this function only after upgrade was already performed. */
725 ut_a(log.m_format == Log_format::CURRENT);
726 if (log_file_header_check_flag(log.m_log_flags, LOG_HEADER_FLAG_NO_LOGGING)) {
727 auto result = mtr_t::s_logging.disable(nullptr);
728 /* Currently never fails. */
729 ut_a(result == 0);
730 srv_redo_log = false;
731 }
732
733 log.write_to_file_requests_total.store(0);
734 log.write_to_file_requests_interval.store(std::chrono::seconds::zero());
735
736 log.recovered_lsn = start_lsn;
737 log.last_checkpoint_lsn = checkpoint_lsn;
738 log.available_for_checkpoint_lsn = checkpoint_lsn;
739 log.m_allow_checkpoints.store(allow_checkpoints);
740
741 ut_a((log.sn.load(std::memory_order_acquire) & SN_LOCKED) == 0);
742 log.sn = log_translate_lsn_to_sn(log.recovered_lsn);
743 log.sn_locked = log_translate_lsn_to_sn(log.recovered_lsn);
744
745 if ((start_lsn + LOG_BLOCK_TRL_SIZE) % OS_FILE_LOG_BLOCK_SIZE == 0) {
746 start_lsn += LOG_BLOCK_TRL_SIZE + LOG_BLOCK_HDR_SIZE;
747 } else if (start_lsn % OS_FILE_LOG_BLOCK_SIZE == 0) {
748 start_lsn += LOG_BLOCK_HDR_SIZE;
749 }
750 ut_a(start_lsn > LOG_START_LSN);
751
752 log.recent_written.add_link(0, start_lsn);
753 log.recent_written.advance_tail();
754 ut_a(log_buffer_ready_for_write_lsn(log) == start_lsn);
755
756 log.recent_closed.add_link(0, start_lsn);
757 log.recent_closed.advance_tail();
758 ut_a(log_buffer_dirty_pages_added_up_to_lsn(log) == start_lsn);
759
760 log.write_lsn = start_lsn;
761 log.flushed_to_disk_lsn = start_lsn;
762
763 log.write_ahead_end_offset = 0;
764
765 lsn_t block_lsn;
766 byte *block;
767
768 block_lsn = ut_uint64_align_down(start_lsn, OS_FILE_LOG_BLOCK_SIZE);
769
770 ut_a(block_lsn % log.buf_size + OS_FILE_LOG_BLOCK_SIZE <= log.buf_size);
771
772 block = static_cast<byte *>(log.buf) + block_lsn % log.buf_size;
773
774 Log_data_block_header block_header;
775
776 {
777 auto file = log.m_files.find(block_lsn);
778 ut_a(file != log.m_files.end());
779 auto file_handle = file->open(Log_file_access_mode::READ_ONLY);
780 ut_a(file_handle.is_open());
781
782 const auto err = log_data_blocks_read(file_handle, file->offset(block_lsn),
783 OS_FILE_LOG_BLOCK_SIZE, block);
784 if (err != DB_SUCCESS) {
785 return err;
786 }
787 }
788 log_data_block_header_deserialize(block, block_header);
789
790 /* FOLLOWING IS NOT NEEDED IF WE DON'T ALLOW DISABLING crc32 checksum */
791 log_fix_first_rec_group(block_lsn, block_header, start_lsn);
792
793 block_header.set_lsn(block_lsn);
794 /* The last mtr in the block might have been incomplete, thus we trim the
795 block to the start_lsn which is the end of the last mtr found in recovery */
796 block_header.m_data_len = start_lsn - block_lsn;
797 ut_ad(LOG_BLOCK_HDR_SIZE <= block_header.m_first_rec_group);
798 ut_ad(block_header.m_first_rec_group <= block_header.m_data_len);
799
800 log_data_block_header_serialize(block_header, block);
801
802 log_update_buf_limit(log, start_lsn);
803
804 const dberr_t err = log_files_start(log);
805
806 /* Do not reorder writes above, below this line. For x86 this
807 protects only from unlikely compile-time reordering. */
808 std::atomic_thread_fence(std::memory_order_release);
809
810 return err;
811 }
812
813 static void log_sys_free() {
814 ut_a(log_sys != nullptr);
815
816 log_t &log = *log_sys;
817
818 log_consumer_unregister(log, &(log.m_checkpoint_consumer));
819
820 log_deallocate_write_events(log);
821 log_deallocate_flush_events(log);
822 log_deallocate_recent_closed(log);
823 log_deallocate_recent_written(log);
824 log_deallocate_write_ahead_buffer(log);
825 log_deallocate_buffer(log);
826
827 #ifdef UNIV_DEBUG
828 rw_lock_free_func(log.sn_lock_inst);
829 ut::free(log.sn_lock_inst);
830 log.sn_lock_inst = nullptr;
831 #endif /* UNIV_DEBUG */
832 #ifdef UNIV_PFS_RWLOCK
833 if (log.pfs_psi != nullptr) {
834 PSI_RWLOCK_CALL(destroy_rwlock)(log.pfs_psi);
835 log.pfs_psi = nullptr;
836 }
837 #endif /* UNIV_PFS_RWLOCK */
838
839 mutex_free(&log.m_files_mutex);
840 mutex_free(&log.sn_x_lock_mutex);
841 mutex_free(&log.limits_mutex);
842 mutex_free(&log.write_notifier_mutex);
843 mutex_free(&log.flush_notifier_mutex);
844 mutex_free(&log.flusher_mutex);
845 mutex_free(&log.writer_mutex);
846 mutex_free(&log.closer_mutex);
847 mutex_free(&log.checkpointer_mutex);
848
849 os_event_destroy(log.next_checkpoint_event);
850 os_event_destroy(log.write_notifier_event);
851 os_event_destroy(log.flush_notifier_event);
852 os_event_destroy(log.closer_event);
853 os_event_destroy(log.checkpointer_event);
854 os_event_destroy(log.writer_event);
855 os_event_destroy(log.flusher_event);
856 os_event_destroy(log.old_flush_event);
857 os_event_destroy(log.writer_threads_resume_event);
858 os_event_destroy(log.m_files_governor_event);
859 os_event_destroy(log.m_files_governor_iteration_event);
860 os_event_destroy(log.m_file_removed_event);
861 os_event_destroy(log.sn_lock_event);
862
863 log_sys_object->dealloc();
864 ut::delete_(log_sys_object);
865 log_sys_object = nullptr;
866
867 log_sys = nullptr;
868 }
869
870 /** @} */
871
872 /**************************************************/ /**
873
874 @name Start / stop of background threads
875
876 *******************************************************/
877
878 /** @{ */
879
880 void log_writer_thread_active_validate() { ut_a(log_writer_is_active()); }
881
882 void log_background_write_threads_active_validate(const log_t &log) {
883 ut_ad(!log.disable_redo_writes);
884
885 ut_a(log_writer_is_active());
886 ut_a(log_flusher_is_active());
887 }
888
889 void log_background_threads_active_validate(const log_t &log) {
890 log_background_write_threads_active_validate(log);
891
892 ut_a(log_write_notifier_is_active());
893 ut_a(log_flush_notifier_is_active());
894 ut_a(log_checkpointer_is_active());
895 ut_a(log_files_governor_is_active());
896 }
897
898 void log_background_threads_inactive_validate() {
899 ut_a(!log_files_governor_is_active());
900 ut_a(!log_checkpointer_is_active());
901 ut_a(!log_write_notifier_is_active());
902 ut_a(!log_flush_notifier_is_active());
903 ut_a(!log_writer_is_active());
904 ut_a(!log_flusher_is_active());
905 }
906
907 void log_start_background_threads(log_t &log) {
908 ib::info(ER_IB_MSG_1258) << "Log background threads are being started...";
909
910 log_background_threads_inactive_validate();
911
912 ut_ad(!log.disable_redo_writes);
913 ut_a(!srv_read_only_mode);
914 ut_a(log.sn.load() > 0);
915
916 log.should_stop_threads.store(false);
917 log.writer_threads_paused.store(false);
918
919 srv_threads.m_log_checkpointer =
920 os_thread_create(log_checkpointer_thread_key, 0, log_checkpointer, &log);
921
922 srv_threads.m_log_flush_notifier = os_thread_create(
923 log_flush_notifier_thread_key, 0, log_flush_notifier, &log);
924
925 srv_threads.m_log_flusher =
926 os_thread_create(log_flusher_thread_key, 0, log_flusher, &log);
927
928 srv_threads.m_log_write_notifier = os_thread_create(
929 log_write_notifier_thread_key, 0, log_write_notifier, &log);
930
931 srv_threads.m_log_writer =
932 os_thread_create(log_writer_thread_key, 0, log_writer, &log);
933
934 srv_threads.m_log_files_governor = os_thread_create(
935 log_files_governor_thread_key, 0, log_files_governor, &log);
936
937 log.m_no_more_dummy_records_requested.store(false);
938 log.m_no_more_dummy_records_promised.store(false);
939
940 srv_threads.m_log_checkpointer.start();
941 srv_threads.m_log_flush_notifier.start();
942 srv_threads.m_log_flusher.start();
943 srv_threads.m_log_write_notifier.start();
944 srv_threads.m_log_writer.start();
945 srv_threads.m_log_files_governor.start();
946
947 log_background_threads_active_validate(log);
948
949 log_control_writer_threads(log);
950
951 meb::redo_log_archive_init();
952 }
953
954 void log_stop_background_threads(log_t &log) {
955 /* We cannot stop threads when x-lock is acquired, because of scenario:
956 * log_checkpointer starts log_checkpoint()
957 * log_checkpoint() asks to persist dd dynamic metadata
958 * dict_persist_to_dd_table_buffer() tries to write to redo
959 * but cannot lock on log.sn
960 * so log_checkpointer thread waits for this thread
961 until the x-lock is released
962 * but this thread waits until log background threads
963 have been stopped - log_checkpointer is not stopped. */
964 ut_ad(!rw_lock_own(log.sn_lock_inst, RW_LOCK_X));
965
966 ib::info(ER_IB_MSG_1259) << "Log background threads are being closed...";
967
968 meb::redo_log_archive_deinit();
969
970 log_background_threads_active_validate(log);
971
972 ut_a(!srv_read_only_mode);
973
974 log_resume_writer_threads(log);
975
976 log_files_dummy_records_disable(log);
977
978 log.should_stop_threads.store(true);
979
980 /* Wait until threads are closed. */
981 while (log_writer_is_active()) {
982 os_event_set(log.writer_event);
983 std::this_thread::sleep_for(std::chrono::microseconds(10));
984 }
985 while (log_write_notifier_is_active()) {
986 os_event_set(log.write_notifier_event);
987 std::this_thread::sleep_for(std::chrono::microseconds(10));
988 }
989 while (log_flusher_is_active()) {
990 os_event_set(log.flusher_event);
991 std::this_thread::sleep_for(std::chrono::microseconds(10));
992 }
993 while (log_flush_notifier_is_active()) {
994 os_event_set(log.flush_notifier_event);
995 std::this_thread::sleep_for(std::chrono::microseconds(10));
996 }
997 while (log_checkpointer_is_active()) {
998 os_event_set(log.checkpointer_event);
999 std::this_thread::sleep_for(std::chrono::microseconds(10));
1000 }
1001 while (log_files_governor_is_active()) {
1002 os_event_set(log.m_files_governor_event);
1003 std::this_thread::sleep_for(std::chrono::microseconds(10));
1004 }
1005
1006 log_background_threads_inactive_validate();
1007 }
1008
1009 void log_stop_background_threads_nowait(log_t &log) {
1010 log_resume_writer_threads(log);
1011 log_files_dummy_records_request_disable(log);
1012 log.should_stop_threads.store(true);
1013 log_wake_threads(log);
1014 }
1015
1016 void log_make_empty_and_stop_background_threads(log_t &log) {
1017 log_files_dummy_records_disable(log);
1018
1019 while (log_make_latest_checkpoint(log)) {
1020 /* It could happen, that when writing a new checkpoint,
1021 DD dynamic metadata was persisted, making some pages
1022 dirty (with the persisted data) and writing new redo
1023 records to protect those modifications. In such case,
1024 current lsn would be higher than lsn and we would need
1025 another iteration to ensure, that checkpoint lsn points
1026 to the newest lsn. */
1027 }
1028
1029 log_stop_background_threads(log);
1030 }
1031
1032 void log_wake_threads(log_t &log) {
1033 if (log_files_governor_is_active()) {
1034 os_event_set(log.m_files_governor_event);
1035 }
1036 if (log_checkpointer_is_active()) {
1037 os_event_set(log.checkpointer_event);
1038 }
1039 if (log_writer_is_active()) {
1040 os_event_set(log.writer_event);
1041 }
1042 if (log_flusher_is_active()) {
1043 os_event_set(log.flusher_event);
1044 }
1045 if (log_write_notifier_is_active()) {
1046 os_event_set(log.write_notifier_event);
1047 }
1048 }
1049
1050 static void log_pause_writer_threads(log_t &log) {
1051 /* protected by LOCK_global_system_variables */
1052 if (!log.writer_threads_paused.load()) {
1053 os_event_reset(log.writer_threads_resume_event);
1054 log.writer_threads_paused.store(true);
1055 if (log_writer_is_active()) {
1056 os_event_set(log.writer_event);
1057 }
1058 if (log_flusher_is_active()) {
1059 os_event_set(log.flusher_event);
1060 }
1061 if (log_write_notifier_is_active()) {
1062 os_event_set(log.write_notifier_event);
1063 }
1064 if (log_flush_notifier_is_active()) {
1065 os_event_set(log.flush_notifier_event);
1066 }
1067
1068 /* wakeup waiters to use the log writer threads */
1069 for (size_t i = 0; i < log.write_events_size; ++i) {
1070 os_event_set(log.write_events[i]);
1071 }
1072 for (size_t i = 0; i < log.flush_events_size; ++i) {
1073 os_event_set(log.flush_events[i]);
1074 }
1075
1076 /* confirms *_notifier_thread accepted to pause */
1077 while (log.write_notifier_resume_lsn.load(std::memory_order_acquire) == 0 ||
1078 log.flush_notifier_resume_lsn.load(std::memory_order_acquire) == 0) {
1079 ut_a(log_write_notifier_is_active());
1080 ut_a(log_flush_notifier_is_active());
1081 std::this_thread::sleep_for(std::chrono::milliseconds(1));
1082 }
1083 }
1084 }
1085
1086 static void log_resume_writer_threads(log_t &log) {
1087 /* protected by LOCK_global_system_variables */
1088 if (log.writer_threads_paused.load()) {
1089 log.writer_threads_paused.store(false);
1090
1091 /* wakeup waiters not to use the log writer threads */
1092 os_event_set(log.old_flush_event);
1093
1094 /* gives resume lsn for each notifiers */
1095 log.write_notifier_resume_lsn.store(log.write_lsn.load());
1096 log.flush_notifier_resume_lsn.store(log.flushed_to_disk_lsn.load());
1097 os_event_set(log.writer_threads_resume_event);
1098
1099 /* confirms *_notifier_resume_lsn have been accepted */
1100 while (log.write_notifier_resume_lsn.load(std::memory_order_acquire) != 0 ||
1101 log.flush_notifier_resume_lsn.load(std::memory_order_acquire) != 0) {
1102 std::this_thread::sleep_for(std::chrono::milliseconds(1));
1103 ut_a(log_write_notifier_is_active());
1104 ut_a(log_flush_notifier_is_active());
1105 os_event_set(log.writer_threads_resume_event);
1106 }
1107 }
1108 }
1109
1110 void log_control_writer_threads(log_t &log) {
1111 /* pause/resume the log writer threads based on innodb_log_writer_threads
1112 value. NOTE: This function is protected by LOCK_global_system_variables
1113 in mysqld by called from innodb_log_writer_threads_update() */
1114 if (srv_log_writer_threads) {
1115 log_resume_writer_threads(log);
1116 } else {
1117 log_pause_writer_threads(log);
1118 }
1119 }
1120
1121 /** @} */
1122
1123 /**************************************************/ /**
1124
1125 @name Status printing
1126
1127 *******************************************************/
1128
1129 /** @{ */
1130
1131 void log_print(const log_t &log, FILE *file) {
1132 lsn_t last_checkpoint_lsn;
1133 lsn_t dirty_pages_added_up_to_lsn;
1134 lsn_t ready_for_write_lsn;
1135 lsn_t write_lsn;
1136 lsn_t flush_lsn;
1137 lsn_t max_assigned_lsn;
1138 lsn_t current_lsn;
1139 lsn_t oldest_lsn;
1140 uint64_t file_min_id;
1141 uint64_t file_max_id;
1142
1143 log_files_mutex_enter(log);
1144
1145 last_checkpoint_lsn = log.last_checkpoint_lsn.load();
1146 dirty_pages_added_up_to_lsn = log_buffer_dirty_pages_added_up_to_lsn(log);
1147 ready_for_write_lsn = log_buffer_ready_for_write_lsn(log);
1148 write_lsn = log.write_lsn.load();
1149 flush_lsn = log.flushed_to_disk_lsn.load();
1150 max_assigned_lsn = log_get_lsn(log);
1151 current_lsn = log_get_lsn(log);
1152 file_min_id = log.m_files.begin()->m_id;
1153 file_max_id = log.m_current_file.m_id;
1154
1155 log_limits_mutex_enter(log);
1156 oldest_lsn = log.available_for_checkpoint_lsn;
1157 log_limits_mutex_exit(log);
1158
1159 log_files_mutex_exit(log);
1160
1161 fprintf(file,
1162 "Log capacity " LSN_PF
1163 "\n"
1164 "Log capacity used " LSN_PF
1165 "\n"
1166 "Log sequence number " LSN_PF
1167 "\n"
1168 "Log buffer assigned up to " LSN_PF
1169 "\n"
1170 "Log buffer completed up to " LSN_PF
1171 "\n"
1172 "Log written up to " LSN_PF
1173 "\n"
1174 "Log flushed up to " LSN_PF
1175 "\n"
1176 "Added dirty pages up to " LSN_PF
1177 "\n"
1178 "Pages flushed up to " LSN_PF
1179 "\n"
1180 "Last checkpoint at " LSN_PF
1181 "\n"
1182 "Log minimum file id is " UINT64PF
1183 "\n"
1184 "Log maximum file id is " UINT64PF "\n",
1185 (uint64_t)srv_redo_log_capacity, (uint64_t)srv_redo_log_capacity_used,
1186 current_lsn, max_assigned_lsn, ready_for_write_lsn, write_lsn,
1187 flush_lsn, dirty_pages_added_up_to_lsn, oldest_lsn,
1188 last_checkpoint_lsn, file_min_id, file_max_id);
1189
1190 time_t current_time = time(nullptr);
1191
1192 double time_elapsed = difftime(current_time, log.last_printout_time);
1193
1194 if (time_elapsed <= 0) {
1195 time_elapsed = 1;
1196 }
1197
1198 fprintf(
1199 file, UINT64PF " log i/o's done, %.2f log i/o's/second\n", log.n_log_ios,
1200 static_cast<double>(log.n_log_ios - log.n_log_ios_old) / time_elapsed);
1201
1202 log.n_log_ios_old = log.n_log_ios;
1203 log.last_printout_time = current_time;
1204 }
1205
1206 void log_refresh_stats(log_t &log) {
1207 log.n_log_ios_old = log.n_log_ios;
1208 log.last_printout_time = time(nullptr);
1209 }
1210
1211 void log_update_exported_variables(const log_t &log) {
1212 export_vars.innodb_redo_log_read_only = srv_read_only_mode;
1213
1214 export_vars.innodb_redo_log_uuid = log.m_log_uuid;
1215
1216 export_vars.innodb_redo_log_checkpoint_lsn = log_get_checkpoint_lsn(log);
1217
1218 export_vars.innodb_redo_log_flushed_to_disk_lsn =
1219 log.flushed_to_disk_lsn.load();
1220
1221 export_vars.innodb_redo_log_current_lsn = log_get_lsn(log);
1222 }
1223
1224 /** @} */
1225
1226 /**************************************************/ /**
1227
1228 @name Resizing of buffers
1229
1230 *******************************************************/
1231
1232 /** @{ */
1233
1234 bool log_buffer_resize_low(log_t &log, size_t new_size, lsn_t end_lsn) {
1235 ut_ad(log_checkpointer_mutex_own(log));
1236 ut_ad(log_writer_mutex_own(log));
1237
1238 const lsn_t start_lsn =
1239 ut_uint64_align_down(log.write_lsn.load(), OS_FILE_LOG_BLOCK_SIZE);
1240
1241 end_lsn = ut_uint64_align_up(end_lsn, OS_FILE_LOG_BLOCK_SIZE);
1242
1243 if (end_lsn == start_lsn) {
1244 end_lsn += OS_FILE_LOG_BLOCK_SIZE;
1245 }
1246
1247 ut_ad(end_lsn - start_lsn <= log.buf_size);
1248
1249 if (end_lsn - start_lsn > new_size) {
1250 return false;
1251 }
1252
1253 /* Save the contents. */
1254 byte *tmp_buf = ut::new_arr_withkey<byte>(UT_NEW_THIS_FILE_PSI_KEY,
1255 ut::Count(end_lsn - start_lsn));
1256 for (auto i = start_lsn; i < end_lsn; i += OS_FILE_LOG_BLOCK_SIZE) {
1257 std::memcpy(&tmp_buf[i - start_lsn], &log.buf[i % log.buf_size],
1258 OS_FILE_LOG_BLOCK_SIZE);
1259 }
1260
1261 /* Re-allocate log buffer. */
1262 srv_log_buffer_size = static_cast<ulong>(new_size);
1263 log_deallocate_buffer(log);
1264 log_allocate_buffer(log);
1265
1266 /* Restore the contents. */
1267 for (auto i = start_lsn; i < end_lsn; i += OS_FILE_LOG_BLOCK_SIZE) {
1268 std::memcpy(&log.buf[i % new_size], &tmp_buf[i - start_lsn],
1269 OS_FILE_LOG_BLOCK_SIZE);
1270 }
1271 ut::delete_arr(tmp_buf);
1272
1273 log_calc_buf_size(log);
1274
1275 log_update_buf_limit(log);
1276
1277 ut_a(srv_log_buffer_size == log.buf_size);
1278
1279 ib::info(ER_IB_MSG_1260) << "srv_log_buffer_size was extended to "
1280 << log.buf_size << ".";
1281
1282 return true;
1283 }
1284
1285 bool log_buffer_resize(log_t &log, size_t new_size) {
1286 log_buffer_x_lock_enter(log);
1287
1288 const lsn_t end_lsn = log_get_lsn(log);
1289
1290 log_checkpointer_mutex_enter(log);
1291 log_writer_mutex_enter(log);
1292
1293 const bool ret = log_buffer_resize_low(log, new_size, end_lsn);
1294
1295 log_writer_mutex_exit(log);
1296 log_checkpointer_mutex_exit(log);
1297 log_buffer_x_lock_exit(log);
1298
1299 return ret;
1300 }
1301
1302 void log_write_ahead_resize(log_t &log, size_t new_size) {
1303 ut_a(new_size >= INNODB_LOG_WRITE_AHEAD_SIZE_MIN);
1304 ut_a(new_size <= INNODB_LOG_WRITE_AHEAD_SIZE_MAX);
1305
1306 log_writer_mutex_enter(log);
1307
1308 log_deallocate_write_ahead_buffer(log);
1309 srv_log_write_ahead_size = static_cast<ulong>(new_size);
1310
1311 log.write_ahead_end_offset =
1312 ut_uint64_align_down(log.write_ahead_end_offset, new_size);
1313
1314 log_allocate_write_ahead_buffer(log);
1315
1316 log_writer_mutex_exit(log);
1317 }
1318
1319 static void log_calc_buf_size(log_t &log) {
1320 ut_a(srv_log_buffer_size >= INNODB_LOG_BUFFER_SIZE_MIN);
1321 ut_a(srv_log_buffer_size <= INNODB_LOG_BUFFER_SIZE_MAX);
1322
1323 log.buf_size = srv_log_buffer_size;
1324
1325 /* The following update has to be the last operation during resize
1326 procedure of log buffer. That's because since this moment, possibly
1327 new concurrent writes for higher sn will start (which were waiting
1328 for free space in the log buffer). */
1329
1330 log.buf_size_sn = log_translate_lsn_to_sn(log.buf_size);
1331 }
1332
1333 /** @} */
1334
1335 /**************************************************/ /**
1336
1337 @name Allocation / deallocation of buffers
1338
1339 *******************************************************/
1340
1341 /** @{ */
1342
1343 static void log_allocate_buffer(log_t &log) {
1344 ut_a(srv_log_buffer_size >= INNODB_LOG_BUFFER_SIZE_MIN);
1345 ut_a(srv_log_buffer_size <= INNODB_LOG_BUFFER_SIZE_MAX);
1346 ut_a(srv_log_buffer_size >= 4 * UNIV_PAGE_SIZE);
1347
1348 #ifdef UNIV_PFS_MEMORY
1349 log.buf.alloc_withkey(ut::make_psi_memory_key(log_buffer_memory_key),
1350 ut::Count{srv_log_buffer_size});
1351 #else
1352 log.buf.alloc_withkey(ut::make_psi_memory_key(PSI_NOT_INSTRUMENTED),
1353 ut::Count{srv_log_buffer_size});
1354 #endif
1355 }
1356
1357 static void log_deallocate_buffer(log_t &log) { log.buf.dealloc(); }
1358
1359 static void log_allocate_write_ahead_buffer(log_t &log) {
1360 ut_a(srv_log_write_ahead_size >= INNODB_LOG_WRITE_AHEAD_SIZE_MIN);
1361 ut_a(srv_log_write_ahead_size <= INNODB_LOG_WRITE_AHEAD_SIZE_MAX);
1362
1363 log.write_ahead_buf_size = srv_log_write_ahead_size;
1364 log.write_ahead_buf.alloc_withkey(UT_NEW_THIS_FILE_PSI_KEY,
1365 ut::Count{log.write_ahead_buf_size});
1366 }
1367
1368 static void log_deallocate_write_ahead_buffer(log_t &log) {
1369 log.write_ahead_buf.dealloc();
1370 }
1371
1372 static void log_allocate_flush_events(log_t &log) {
1373 const size_t n = srv_log_flush_events;
1374
1375 ut_a(log.flush_events == nullptr);
1376 ut_a(n >= 1);
1377 ut_a((n & (n - 1)) == 0);
1378
1379 log.flush_events_size = n;
1380 log.flush_events =
1381 ut::new_arr_withkey<os_event_t>(UT_NEW_THIS_FILE_PSI_KEY, ut::Count{n});
1382
1383 for (size_t i = 0; i < log.flush_events_size; ++i) {
1384 log.flush_events[i] = os_event_create();
1385 }
1386 }
1387
1388 static void log_deallocate_flush_events(log_t &log) {
1389 ut_a(log.flush_events != nullptr);
1390
1391 for (size_t i = 0; i < log.flush_events_size; ++i) {
1392 os_event_destroy(log.flush_events[i]);
1393 }
1394
1395 ut::delete_arr(log.flush_events);
1396 log.flush_events = nullptr;
1397 }
1398
1399 static void log_allocate_write_events(log_t &log) {
1400 const size_t n = srv_log_write_events;
1401
1402 ut_a(log.write_events == nullptr);
1403 ut_a(n >= 1);
1404 ut_a((n & (n - 1)) == 0);
1405
1406 log.write_events_size = n;
1407 log.write_events =
1408 ut::new_arr_withkey<os_event_t>(UT_NEW_THIS_FILE_PSI_KEY, ut::Count{n});
1409
1410 for (size_t i = 0; i < log.write_events_size; ++i) {
1411 log.write_events[i] = os_event_create();
1412 }
1413 }
1414
1415 static void log_deallocate_write_events(log_t &log) {
1416 ut_a(log.write_events != nullptr);
1417
1418 for (size_t i = 0; i < log.write_events_size; ++i) {
1419 os_event_destroy(log.write_events[i]);
1420 }
1421
1422 ut::delete_arr(log.write_events);
1423 log.write_events = nullptr;
1424 }
1425
1426 static void log_allocate_recent_written(log_t &log) {
1427 log.recent_written = Link_buf<lsn_t>{srv_log_recent_written_size};
1428 }
1429 static void log_deallocate_recent_written(log_t &log) {
1430 log.recent_written.validate_no_links();
1431 log.recent_written = {};
1432 }
1433
1434 static void log_allocate_recent_closed(log_t &log) {
1435 log.recent_closed = Link_buf<lsn_t>{srv_log_recent_closed_size};
1436 }
1437
1438 static void log_deallocate_recent_closed(log_t &log) {
1439 log.recent_closed.validate_no_links();
1440 log.recent_closed = {};
1441 }
1442
1443 static void log_reset_encryption_buffer(log_t &log) {
1444 std::memset(log.m_encryption_buf, 0x00, OS_FILE_LOG_BLOCK_SIZE);
1445 }
1446
1447 /** @} */
1448
1449 /**************************************************/ /**
1450
1451 @name Log position locking (for replication)
1452
1453 *******************************************************/
1454
1455 /** @{ */
1456
1457 void log_position_lock(log_t &log) {
1458 log_buffer_x_lock_enter(log);
1459
1460 log_checkpointer_mutex_enter(log);
1461 }
1462
1463 void log_position_unlock(log_t &log) {
1464 log_checkpointer_mutex_exit(log);
1465
1466 log_buffer_x_lock_exit(log);
1467 }
1468
1469 void log_position_collect_lsn_info(const log_t &log, lsn_t *current_lsn,
1470 lsn_t *checkpoint_lsn) {
1471 ut_ad(rw_lock_own(log.sn_lock_inst, RW_LOCK_X));
1472 ut_ad(log_checkpointer_mutex_own(log));
1473
1474 *checkpoint_lsn = log.last_checkpoint_lsn.load();
1475
1476 *current_lsn = log_get_lsn(log);
1477
1478 /* Ensure we have redo log started. */
1479 ut_a(*current_lsn >= LOG_START_LSN);
1480 ut_a(*checkpoint_lsn >= LOG_START_LSN);
1481
1482 /* Obviously current lsn cannot point to before checkpoint. */
1483 ut_a(*current_lsn >= *checkpoint_lsn);
1484 }
1485
1486 /** @} */
1487
1488 /**************************************************/ /**
1489
1490 @name Log - persisting flags
1491
1492 *******************************************************/
1493
1494 /** @{ */
1495
1496 /** Updates and persists the updated log flags to disk.
1497 @param[in,out] log redo log
1498 @param[in] update_function functor called on existing flags,
1499 supposed to make changes */
1500 template <typename T>
1501 static void log_update_flags(log_t &log, T update_function) {
1502 ut_a(!srv_read_only_mode);
1503
1504 log_writer_mutex_enter(log); /* writing to log files */
1505 log_files_mutex_enter(log); /* accessing log.m_files */
1506
1507 Log_flags log_flags = log.m_log_flags;
1508 update_function(log_flags);
1509
1510 const dberr_t err = log_files_persist_flags(log, log_flags);
1511 ut_a(err == DB_SUCCESS);
1512
1513 log_files_mutex_exit(log);
1514 log_writer_mutex_exit(log);
1515 }
1516
1517 void log_persist_enable(log_t &log) {
1518 log_update_flags(log, [](Log_flags &log_flags) {
1519 log_file_header_reset_flag(log_flags, LOG_HEADER_FLAG_NO_LOGGING);
1520 log_file_header_reset_flag(log_flags, LOG_HEADER_FLAG_CRASH_UNSAFE);
1521 });
1522 }
1523
1524 void log_persist_disable(log_t &log) {
1525 log_update_flags(log, [](Log_flags &log_flags) {
1526 log_file_header_set_flag(log_flags, LOG_HEADER_FLAG_NO_LOGGING);
1527 log_file_header_set_flag(log_flags, LOG_HEADER_FLAG_CRASH_UNSAFE);
1528 });
1529 }
1530
1531 void log_persist_crash_safe(log_t &log) {
1532 log_update_flags(log, [](Log_flags &log_flags) {
1533 ut_a(log_file_header_check_flag(log_flags, LOG_HEADER_FLAG_NO_LOGGING));
1534 log_file_header_reset_flag(log_flags, LOG_HEADER_FLAG_CRASH_UNSAFE);
1535 });
1536 }
1537
1538 void log_persist_initialized(log_t &log) {
1539 log_update_flags(log, [](Log_flags &log_flags) {
1540 constexpr auto flag_to_reset = LOG_HEADER_FLAG_NOT_INITIALIZED;
1541 ut_a(log_file_header_check_flag(log_flags, flag_to_reset));
1542 log_file_header_reset_flag(log_flags, flag_to_reset);
1543 });
1544 }
1545
1546 void log_crash_safe_validate(log_t &log) {
1547 log_files_mutex_enter(log); /* accessing log.m_files */
1548 ut_a(!log_file_header_check_flag(log.m_log_flags,
1549 LOG_HEADER_FLAG_CRASH_UNSAFE));
1550 log_files_mutex_exit(log);
1551 }
1552
1553 /** @} */
1554
1555 /**************************************************/ /**
1556
1557 @name Log - log system initialization.
1558
1559 *******************************************************/
1560
1561 /** @{ */
1562
1563 static dberr_t log_sys_handle_creator(log_t &log) {
1564 auto str_starts_with = [](const std::string &a, const std::string &b) {
1565 return a.size() >= b.size() && a.substr(0, b.size()) == b;
1566 };
1567
1568 const auto &creator_name = log.m_creator_name;
1569
1570 if (str_starts_with(creator_name, "MEB")) {
1571 /* Disable the double write buffer. MEB ensures that the data pages
1572 are consistent. Therefore the dblwr is superfluous. Secondly, the dblwr
1573 file is not redo logged and we can have pages in there that were written
1574 after the redo log was copied by MEB. */
1575
1576 /* Restore state after recovery completes. */
1577 recv_sys->dblwr_state = dblwr::g_mode;
1578 dblwr::g_mode = dblwr::Mode::OFF;
1579 dblwr::set();
1580
1581 recv_sys->is_meb_db = true;
1582
1583 if (srv_read_only_mode) {
1584 ib::error(ER_IB_MSG_LOG_FILES_CREATED_BY_MEB_AND_READ_ONLY_MODE);
1585 return DB_ERROR;
1586 }
1587
1588 /* This log file was created by mysqlbackup --restore: print
1589 a note to the user about it */
1590 ib::info(ER_IB_MSG_LOG_FILES_CREATED_BY_MEB, creator_name.c_str());
1591
1592 } else if (str_starts_with(creator_name, LOG_HEADER_CREATOR_CLONE)) {
1593 recv_sys->is_cloned_db = true;
1594 /* Refuse clone database recovery in read only mode. */
1595 if (srv_read_only_mode) {
1596 ib::error(ER_IB_MSG_LOG_FILES_CREATED_BY_CLONE_AND_READ_ONLY_MODE);
1597 return DB_ERROR;
1598 }
1599 if (log.m_format < Log_format::CURRENT) {
1600 ib::error(ER_IB_MSG_LOG_UPGRADE_CLONED_DB, ulong{to_int(log.m_format)});
1601 return DB_ERROR;
1602 }
1603 ib::info(ER_IB_MSG_LOG_FILES_CREATED_BY_CLONE);
1604
1605 } else if (!str_starts_with(creator_name, "MySQL ")) {
1606 ib::warn(ER_IB_MSG_LOG_FILES_CREATED_BY_UNKNOWN_CREATOR,
1607 creator_name.c_str());
1608 }
1609 return DB_SUCCESS;
1610 }
1611
1612 static dberr_t log_sys_check_format(const log_t &log) {
1613 switch (log.m_format) {
1614 case Log_format::LEGACY:
1615 ib::error(ER_IB_MSG_LOG_FORMAT_BEFORE_5_7_9,
1616 ulong{to_int(Log_format::LEGACY)});
1617 return DB_ERROR;
1618
1619 case Log_format::VERSION_5_7_9:
1620 case Log_format::VERSION_8_0_1:
1621 case Log_format::VERSION_8_0_3:
1622 case Log_format::VERSION_8_0_19:
1623 case Log_format::VERSION_8_0_28:
1624 ib::info(ER_IB_MSG_LOG_FORMAT_BEFORE_8_0_30, ulong{to_int(log.m_format)});
1625 break;
1626
1627 case Log_format::CURRENT:
1628 break;
1629
1630 default:
1631 /* The log_files_find_and_analyze() would return error if format
1632 was invalid, and InnoDB would quit in log_sys_init() before
1633 calling this function. */
1634 ut_error;
1635 }
1636
1637 if (log.m_format < Log_format::CURRENT && srv_force_recovery != 0) {
1638 /* We say no to running with forced recovery and old format.
1639 User should rather use previous version of MySQL and recover
1640 properly before he switches to newer version. */
1641 const auto directory = log_directory_path(log.m_files_ctx);
1642 ib::error(ER_IB_MSG_LOG_UPGRADE_FORCED_RECV, ulong{to_int(log.m_format)});
1643 return DB_ERROR;
1644 }
1645
1646 return DB_SUCCESS;
1647 }
1648
1649 static dberr_t log_sys_check_directory(const Log_files_context &ctx,
1650 std::string &path, bool &found_files) {
1651 path = log_directory_path(ctx);
1652 if (!os_file_exists(path.c_str())) {
1653 return DB_NOT_FOUND;
1654 }
1655 ut::vector<Log_file_id> listed_files;
1656 const dberr_t err = log_list_existing_files(ctx, listed_files);
1657 if (err != DB_SUCCESS) {
1658 return DB_ERROR;
1659 }
1660 found_files = !listed_files.empty();
1661 return DB_SUCCESS;
1662 }
1663
1664 dberr_t log_sys_init(bool expect_no_files, lsn_t flushed_lsn,
1665 lsn_t &new_files_lsn) {
1666 ut_a(log_is_data_lsn(flushed_lsn));
1667 ut_a(log_sys == nullptr);
1668
1669 new_files_lsn = 0;
1670
1671 Log_files_context log_files_ctx{srv_log_group_home_dir,
1672 Log_files_ruleset::PRE_8_0_30};
1673
1674 std::string root_path;
1675 bool found_files_in_root{false};
1676 dberr_t err =
1677 log_sys_check_directory(log_files_ctx, root_path, found_files_in_root);
1678
1679 /* Report error if innodb_log_group_home_dir / datadir has not been found or
1680 could not be listed. It's a proper decision for all redo format versions:
1681 - older formats store there ib_logfile* files directly,
1682 - newer formats store there #innodb_redo subdirectory. */
1683 if (err != DB_SUCCESS) {
1684 ib::error(ER_IB_MSG_LOG_INIT_DIR_LIST_FAILED, root_path.c_str());
1685 return err;
1686 }
1687
1688 Log_file_handle::s_on_before_read = [](Log_file_id, Log_file_type file_type,
1689 os_offset_t, os_offset_t read_size) {
1690 ut_a(file_type == Log_file_type::NORMAL);
1691 ut_a(srv_is_being_started);
1692 #ifndef UNIV_HOTBACKUP
1693 srv_stats.data_read.add(read_size);
1694 #endif /* !UNIV_HOTBACKUP */
1695 };
1696
1697 Log_file_handle::s_on_before_write =
1698 [](Log_file_id file_id, Log_file_type file_type, os_offset_t write_offset,
1699 os_offset_t write_size) {
1700 ut_a(!srv_read_only_mode);
1701 if (!srv_is_being_started) {
1702 ut_a(log_sys != nullptr);
1703 auto file = log_sys->m_files.file(file_id);
1704 if (file_type == Log_file_type::NORMAL) {
1705 ut_a(file != log_sys->m_files.end());
1706 ut_a((file_id == log_sys->m_current_file.m_id &&
1707 write_offset + write_size <= file->m_size_in_bytes) ||
1708 write_offset + write_size <= LOG_FILE_HDR_SIZE);
1709 } else {
1710 ut_a(file == log_sys->m_files.end());
1711 ut_a(file_id == log_sys->m_current_file.next_id());
1712 }
1713 }
1714 #ifndef UNIV_HOTBACKUP
1715 srv_stats.data_written.add(write_size);
1716 #endif
1717 };
1718
1719 #ifndef _WIN32
1720 Log_file_handle::s_skip_fsyncs =
1721 (srv_unix_file_flush_method == SRV_UNIX_O_DSYNC ||
1722 srv_unix_file_flush_method == SRV_UNIX_NOSYNC);
1723 #endif /* !_WIN32 */
1724
1725 if (!found_files_in_root) {
1726 log_files_ctx =
1727 Log_files_context{srv_log_group_home_dir, Log_files_ruleset::CURRENT};
1728
1729 std::string subdir_path;
1730 bool found_files_in_subdir{false};
1731 err = log_sys_check_directory(log_files_ctx, subdir_path,
1732 found_files_in_subdir);
1733
1734 switch (err) {
1735 case DB_SUCCESS:
1736 if (expect_no_files && found_files_in_subdir) {
1737 ib::error(ER_IB_MSG_LOG_INIT_DIR_NOT_EMPTY_WONT_INITIALIZE,
1738 subdir_path.c_str());
1739 return DB_ERROR;
1740 }
1741 if (!srv_read_only_mode) {
1742 /* The problem is that a lot of people is not aware
1743 that sending SHUTDOWN command does not end when the
1744 server is no longer running, but earlier (obvious!).
1745 Starting MySQL without waiting on previous instance
1746 stopped, seems a bad idea and it often led to
1747 quick failures here if we did not retry. */
1748 for (size_t retries = 0;; ++retries) {
1749 const auto remove_unused_files_ret =
1750 log_remove_unused_files(log_files_ctx);
1751 if (remove_unused_files_ret.first == DB_SUCCESS) {
1752 break;
1753 }
1754 ut_a(retries < 300);
1755 std::this_thread::sleep_for(std::chrono::seconds(1));
1756 }
1757 }
1758 break;
1759 case DB_NOT_FOUND:
1760 /* The #innodb_redo directory has not been found. */
1761 if (expect_no_files) {
1762 /* InnoDB needs to create new directory #innodb_redo. */
1763 if (!os_file_create_directory(subdir_path.c_str(), false)) {
1764 return DB_ERROR;
1765 }
1766 } else {
1767 /* InnoDB does not start if neither ib_logfile* files were found,
1768 nor the #innodb_redo directory was found. User should be informed
1769 about the problem and decide to either:
1770 - use older version of MySQL (<= 8.0.29) and do a non-fast shutdown,
1771 - or create the missing #innodb_redo */
1772 ib::error(ER_IB_MSG_LOG_INIT_DIR_MISSING_SUBDIR, LOG_DIRECTORY_NAME,
1773 log_pre_8_0_30::FILE_BASE_NAME, root_path.c_str());
1774 return DB_ERROR;
1775 }
1776 break;
1777 default:
1778 ib::error(ER_IB_MSG_LOG_INIT_DIR_LIST_FAILED, subdir_path.c_str());
1779 return err;
1780 }
1781
1782 } else {
1783 /* Found existing files in old location for redo files (PRE_8_0_30).
1784 If expected to see no files (and create new), return error emitting
1785 the error message. */
1786 if (expect_no_files) {
1787 ib::error(ER_IB_MSG_LOG_INIT_DIR_NOT_EMPTY_WONT_INITIALIZE,
1788 root_path.c_str());
1789 return DB_ERROR;
1790 }
1791 }
1792
1793 log_sys_create();
1794 ut_a(log_sys != nullptr);
1795 log_t &log = *log_sys;
1796
1797 bool is_concurrency_margin_safe;
1798 log_concurrency_margin(
1799 Log_files_capacity::soft_logical_capacity_for_hard(
1800 Log_files_capacity::hard_logical_capacity_for_physical(
1801 srv_redo_log_capacity_used)),
1802 is_concurrency_margin_safe);
1803
1804 if (!is_concurrency_margin_safe) {
1805 os_offset_t min_redo_log_capacity = srv_redo_log_capacity_used;
1806 os_offset_t max_redo_log_capacity = LOG_CAPACITY_MAX;
1807 while (min_redo_log_capacity < max_redo_log_capacity) {
1808 const os_offset_t capacity_to_check =
1809 (min_redo_log_capacity + max_redo_log_capacity) / 2;
1810
1811 log_concurrency_margin(
1812 Log_files_capacity::soft_logical_capacity_for_hard(
1813 Log_files_capacity::hard_logical_capacity_for_physical(
1814 capacity_to_check)),
1815 is_concurrency_margin_safe);
1816
1817 if (is_concurrency_margin_safe) {
1818 max_redo_log_capacity = capacity_to_check;
1819 } else {
1820 min_redo_log_capacity = capacity_to_check + 1;
1821 }
1822 }
1823
1824 /* The innodb_redo_log_capacity is always rounded to 1M */
1825 min_redo_log_capacity =
1826 ut_uint64_align_up(min_redo_log_capacity, 1024UL * 1024);
1827
1828 ib::error(ER_IB_MSG_LOG_PARAMS_CONCURRENCY_MARGIN_UNSAFE,
1829 ulonglong{srv_redo_log_capacity_used / 1024 / 1024},
1830 ulong{srv_thread_concurrency},
1831 ulonglong{min_redo_log_capacity / 1024 / 1024},
1832 INNODB_PARAMETERS_MSG);
1833
1834 return DB_ERROR;
1835 }
1836
1837 log.m_files_ctx = std::move(log_files_ctx);
1838
1839 if (expect_no_files) {
1840 ut_a(srv_force_recovery < SRV_FORCE_NO_LOG_REDO);
1841 ut_a(!srv_read_only_mode);
1842
1843 ut_a(log.m_files_ctx.m_files_ruleset == Log_files_ruleset::CURRENT);
1844
1845 new_files_lsn = flushed_lsn;
1846 return log_files_create(log, flushed_lsn);
1847 }
1848
1849 if (srv_force_recovery >= SRV_FORCE_NO_LOG_REDO) {
1850 return DB_SUCCESS;
1851 }
1852
1853 Log_files_dict files{log.m_files_ctx};
1854 Log_format format;
1855 std::string creator_name;
1856 Log_flags log_flags;
1857 Log_uuid log_uuid;
1858
1859 ut_a(srv_force_recovery < SRV_FORCE_NO_LOG_REDO);
1860
1861 auto res = log_files_find_and_analyze(
1862 srv_read_only_mode, log.m_encryption_metadata, files, format,
1863 creator_name, log_flags, log_uuid);
1864 switch (res) {
1865 case Log_files_find_result::FOUND_VALID_FILES:
1866 log.m_format = format;
1867 log.m_creator_name = creator_name;
1868 log.m_log_flags = log_flags;
1869 log.m_log_uuid = log_uuid;
1870 log.m_files = std::move(files);
1871 break;
1872
1873 case Log_files_find_result::FOUND_UNINITIALIZED_FILES:
1874 ut_a(format == Log_format::CURRENT);
1875 [[fallthrough]];
1876 case Log_files_find_result::FOUND_NO_FILES:
1877 ut_a(log.m_files_ctx.m_files_ruleset == Log_files_ruleset::CURRENT);
1878 ut_a(files.empty());
1879
1880 if (srv_read_only_mode) {
1881 ut_a(srv_force_recovery < SRV_FORCE_NO_LOG_REDO);
1882 ib::error(ER_IB_MSG_LOG_FILES_CREATE_AND_READ_ONLY_MODE);
1883 return DB_ERROR;
1884 }
1885
1886 {
1887 const auto ret = log_remove_files(log.m_files_ctx);
1888 ut_a(ret.first == DB_SUCCESS);
1889 }
1890 new_files_lsn =
1891 flushed_lsn % OS_FILE_LOG_BLOCK_SIZE == LOG_BLOCK_HDR_SIZE
1892 ? flushed_lsn
1893 : ut_uint64_align_up(flushed_lsn, OS_FILE_LOG_BLOCK_SIZE) +
1894 LOG_BLOCK_HDR_SIZE;
1895 return log_files_create(log, new_files_lsn);
1896
1897 case Log_files_find_result::SYSTEM_ERROR:
1898 case Log_files_find_result::FOUND_CORRUPTED_FILES:
1899 case Log_files_find_result::FOUND_DISABLED_FILES:
1900 case Log_files_find_result::FOUND_VALID_FILES_BUT_MISSING_NEWEST:
1901 return DB_ERROR;
1902 }
1903
1904 /* Check format of the redo log and emit information to the error log,
1905 if the format was not the newest one. */
1906 err = log_sys_check_format(log);
1907 if (err != DB_SUCCESS) {
1908 return err;
1909 }
1910
1911 /* Check creator of log files and mark fields of recv_sys: is_cloned_db,
1912 is_meb_db if needed. */
1913 return log_sys_handle_creator(log);
1914 }
1915
1916 void log_sys_close() {
1917 log_sys_free();
1918 ut_a(log_sys == nullptr);
1919 }
1920
1921 /** @} */
1922
1923 #endif /* !UNIV_HOTBACKUP */