1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
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 also distributed with certain software (including but not
17 limited to OpenSSL) that is licensed under separate terms, as designated in a
18 particular file or component or in included license documentation. The authors
19 of MySQL hereby grant you an additional permission to link the program and
20 your derivative works with the separately licensed software that they have
21 included with MySQL.
22 
23 This program is distributed in the hope that it will be useful, but WITHOUT
24 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
25 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
26 for more details.
27 
28 You should have received a copy of the GNU General Public License along with
29 this program; if not, write to the Free Software Foundation, Inc.,
30 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
31 
32 *****************************************************************************/
33 
34 /**************************************************/ /**
35  @file include/log0log.h
36 
37  Redo log constants and functions.
38 
39  Types are defined inside log0types.h.
40 
41  Created 12/9/1995 Heikki Tuuri
42  *******************************************************/
43 
44 #ifndef log0log_h
45 #define log0log_h
46 
47 #include "dyn0buf.h"
48 #include "univ.i"
49 #ifndef UNIV_HOTBACKUP
50 #include "sync0rw.h"
51 #endif /* !UNIV_HOTBACKUP */
52 
53 #include "log0test.h"
54 #include "log0types.h"
55 #include "my_compiler.h"
56 
57 /** Prefix for name of log file, e.g. "ib_logfile" */
58 constexpr const char *const ib_logfile_basename = "ib_logfile";
59 
60 /* base name length(10) + length for decimal digits(22) */
61 constexpr uint32_t MAX_LOG_FILE_NAME = 32;
62 
63 /** Magic value to use instead of log checksums when they are disabled. */
64 constexpr uint32_t LOG_NO_CHECKSUM_MAGIC = 0xDEADBEEFUL;
65 
66 /** Absolute margin for the free space in the log, before a new query step
67 which modifies the database, is started. Expressed in number of pages. */
68 constexpr uint32_t LOG_CHECKPOINT_EXTRA_FREE = 8;
69 
70 /** Per thread margin for the free space in the log, before a new query step
71 which modifies the database, is started. It's multiplied by maximum number
72 of threads, that can concurrently enter mini transactions. Expressed in
73 number of pages. */
74 constexpr uint32_t LOG_CHECKPOINT_FREE_PER_THREAD = 4;
75 
76 /** Controls asynchronous making of a new checkpoint.
77 Should be bigger than LOG_POOL_PREFLUSH_RATIO_SYNC. */
78 constexpr uint32_t LOG_POOL_CHECKPOINT_RATIO_ASYNC = 32;
79 
80 /** Controls synchronous preflushing of modified buffer pages. */
81 constexpr uint32_t LOG_POOL_PREFLUSH_RATIO_SYNC = 16;
82 
83 /** Controls asynchronous preflushing of modified buffer pages.
84 Should be less than the LOG_POOL_PREFLUSH_RATIO_SYNC. */
85 constexpr uint32_t LOG_POOL_PREFLUSH_RATIO_ASYNC = 8;
86 
87 /** The counting of lsn's starts from this value: this must be non-zero. */
88 constexpr lsn_t LOG_START_LSN = 16 * OS_FILE_LOG_BLOCK_SIZE;
89 
90 /* Offsets used in a log block header. */
91 
92 /** Block number which must be > 0 and is allowed to wrap around at 1G.
93 The highest bit is set to 1, if this is the first block in a call to
94 fil_io (for possibly many consecutive blocks). */
95 constexpr uint32_t LOG_BLOCK_HDR_NO = 0;
96 
97 /** Mask used to get the highest bit in the hdr_no field. */
98 constexpr uint32_t LOG_BLOCK_FLUSH_BIT_MASK = 0x80000000UL;
99 
100 /** Maximum allowed block's number (stored in hdr_no). */
101 constexpr uint32_t LOG_BLOCK_MAX_NO = 0x3FFFFFFFUL + 1;
102 
103 /** Number of bytes written to this block (also header bytes). */
104 constexpr uint32_t LOG_BLOCK_HDR_DATA_LEN = 4;
105 
106 /** Mask used to get the highest bit in the data len field,
107 this bit is to indicate if this block is encrypted or not. */
108 constexpr uint32_t LOG_BLOCK_ENCRYPT_BIT_MASK = 0x8000UL;
109 
110 /** Offset of the first start of mtr log record group in this log block.
111 0 if none. If the value is the same as LOG_BLOCK_HDR_DATA_LEN, it means
112 that the first rec group has not yet been concatenated to this log block,
113 but if it will, it will start at this offset.
114 
115 An archive recovery can start parsing the log records starting from this
116 offset in this log block, if value is not 0. */
117 constexpr uint32_t LOG_BLOCK_FIRST_REC_GROUP = 6;
118 
119 /** 4 lower bytes of the value of log_sys->next_checkpoint_no when the log
120 block was last written to: if the block has not yet been written full,
121 this value is only updated before a log buffer flush. */
122 constexpr uint32_t LOG_BLOCK_CHECKPOINT_NO = 8;
123 
124 /** Size of the log block's header in bytes. */
125 constexpr uint32_t LOG_BLOCK_HDR_SIZE = 12;
126 
127 /* Offsets used in a log block's footer (refer to the end of the block). */
128 
129 /** 4 byte checksum of the log block contents. In InnoDB versions < 3.23.52
130 this did not contain the checksum, but the same value as .._HDR_NO. */
131 constexpr uint32_t LOG_BLOCK_CHECKSUM = 4;
132 
133 /** Size of the log block footer (trailer) in bytes. */
134 constexpr uint32_t LOG_BLOCK_TRL_SIZE = 4;
135 
136 static_assert(LOG_BLOCK_HDR_SIZE + LOG_BLOCK_TRL_SIZE < OS_FILE_LOG_BLOCK_SIZE,
137               "Header + footer cannot be larger than the whole log block.");
138 
139 /** Size of log block's data fragment (where actual data is stored). */
140 constexpr uint32_t LOG_BLOCK_DATA_SIZE =
141     OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_HDR_SIZE - LOG_BLOCK_TRL_SIZE;
142 
143 /** Ensure, that 64 bits are enough to represent lsn values, when 63 bits
144 are used to represent sn values. It is enough to ensure that lsn < 2*sn,
145 and that is guaranteed if the overhead enumerated in lsn sequence is not
146 bigger than number of actual data bytes. */
147 static_assert(LOG_BLOCK_HDR_SIZE + LOG_BLOCK_TRL_SIZE < LOG_BLOCK_DATA_SIZE,
148               "Overhead in LSN sequence cannot be bigger than actual data.");
149 
150 /** Maximum possible sn value. */
151 constexpr sn_t SN_MAX = (1ULL << 62) - 1;
152 
153 /** Maximum possible lsn value is slightly higher than the maximum sn value,
154 because lsn sequence enumerates also bytes used for headers and footers of
155 all log blocks. However, still 64-bits are enough to represent the maximum
156 lsn value, because only 63 bits are used to represent sn value. */
157 constexpr lsn_t LSN_MAX = (1ULL << 63) - 1;
158 
159 /* Offsets inside the checkpoint pages (redo log format version 1). */
160 
161 /** Checkpoint number. It's incremented by one for each consecutive checkpoint.
162 During recovery, all headers are scanned, and one with the maximum checkpoint
163 number is used for the recovery (checkpoint_lsn from the header is used). */
164 constexpr uint32_t LOG_CHECKPOINT_NO = 0;
165 
166 /** Checkpoint lsn. Recovery starts from this lsn and searches for the first
167 log record group that starts since then. In InnoDB < 8.0, it was exact value
168 at which the first log record group started. Because of the relaxed order in
169 flush lists, checkpoint lsn values are not precise anymore (the maximum delay
170 related to the relaxed order in flush lists, is subtracted from oldest_lsn,
171 when writing a checkpoint). */
172 constexpr uint32_t LOG_CHECKPOINT_LSN = 8;
173 
174 /** Offset within the log files, which corresponds to checkpoint lsn.
175 Used for calibration of lsn and offset calculations. */
176 constexpr uint32_t LOG_CHECKPOINT_OFFSET = 16;
177 
178 /** Size of the log buffer, when the checkpoint write was started.
179 It seems to be write-only field in InnoDB. Not used by recovery.
180 
181 @note
182 Note that when the log buffer is being resized, all the log background threads
183 are stopped, so there no is concurrent checkpoint write (the log_checkpointer
184 thread is stopped). */
185 constexpr uint32_t LOG_CHECKPOINT_LOG_BUF_SIZE = 24;
186 
187 /** Offsets used in a log file header */
188 
189 /** Log file header format identifier (32-bit unsigned big-endian integer).
190 This used to be called LOG_GROUP_ID and always written as 0,
191 because InnoDB never supported more than one copy of the redo log. */
192 constexpr uint32_t LOG_HEADER_FORMAT = 0;
193 
194 /** 4 unused (zero-initialized) bytes. */
195 constexpr uint32_t LOG_HEADER_PAD1 = 4;
196 
197 /** LSN of the start of data in this log file (with format version 1 and 2). */
198 constexpr uint32_t LOG_HEADER_START_LSN = 8;
199 
200 /** A null-terminated string which will contain either the string 'MEB'
201 and the MySQL version if the log file was created by mysqlbackup,
202 or 'MySQL' and the MySQL version that created the redo log file. */
203 constexpr uint32_t LOG_HEADER_CREATOR = 16;
204 
205 /** End of the log file creator field. */
206 constexpr uint32_t LOG_HEADER_CREATOR_END = LOG_HEADER_CREATOR + 32;
207 
208 /** 32 BITs flag */
209 constexpr uint32_t LOG_HEADER_FLAGS = LOG_HEADER_CREATOR_END;
210 
211 /** Flag at BIT-1 to indicate if redo logging is disabled or not. */
212 constexpr uint32_t LOG_HEADER_FLAG_NO_LOGGING = 1;
213 
214 /** Flag at BIT-2 to indicate if server is not recoverable on crash. This
215 is set only when redo logging is disabled and unset on slow shutdown after
216 all pages are flushed to disk. */
217 constexpr uint32_t LOG_HEADER_FLAG_CRASH_UNSAFE = 2;
218 
219 /** Maximum BIT position number. Should be set to the latest added. */
220 constexpr uint32_t LOG_HEADER_FLAG_MAX = LOG_HEADER_FLAG_CRASH_UNSAFE;
221 
222 /** Current total size of LOG header. */
223 constexpr uint32_t LOG_HEADER_SIZE = LOG_HEADER_FLAGS + 4;
224 
225 /** Set a specific bit in flag.
226 @param[in]      flag    bit flag
227 @param[in]      bit     set bit */
LOG_HEADER_SET_FLAG(uint32_t & flag,uint32_t bit)228 inline void LOG_HEADER_SET_FLAG(uint32_t &flag, uint32_t bit) {
229   ut_ad(bit > 0);
230   ut_ad(bit <= LOG_HEADER_FLAG_MAX);
231   flag |= static_cast<uint32_t>(1UL << (bit - 1));
232 }
233 
234 /** Check a specific bit in flag.
235 @param[in]      flag    bit flag
236 @param[in]      bit     check bit
237 @return true, iff bit is set in flag. */
LOG_HEADER_CHECK_FLAG(uint32_t flag,uint32_t bit)238 inline bool LOG_HEADER_CHECK_FLAG(uint32_t flag, uint32_t bit) {
239   return ((flag & (1ULL << (bit - 1))) > 0);
240 }
241 
242 /** Contents of the LOG_HEADER_CREATOR field */
243 #define LOG_HEADER_CREATOR_CURRENT "MySQL " INNODB_VERSION_STR
244 #define LOG_HEADER_CREATOR_8018  "MySQL 8.0.18" // (v3 format)
245 
246 /** Header is created during DB clone */
247 #define LOG_HEADER_CREATOR_CLONE "MySQL Clone"
248 
249 /** First checkpoint field in the log header. We write alternately to
250 the checkpoint fields when we make new checkpoints. This field is only
251 defined in the first log file. */
252 constexpr uint32_t LOG_CHECKPOINT_1 = OS_FILE_LOG_BLOCK_SIZE;
253 
254 /** Log Encryption information in redo log header. */
255 constexpr uint32_t LOG_ENCRYPTION = 2 * OS_FILE_LOG_BLOCK_SIZE;
256 
257 /** Second checkpoint field in the header of the first log file. */
258 constexpr uint32_t LOG_CHECKPOINT_2 = 3 * OS_FILE_LOG_BLOCK_SIZE;
259 
260 /** Size of log file's header. */
261 constexpr uint32_t LOG_FILE_HDR_SIZE = 4 * OS_FILE_LOG_BLOCK_SIZE;
262 
263 /** Constants related to server variables (default, min and max values). */
264 
265 /** Default value of innodb_log_write_max_size (in bytes). */
266 constexpr ulint INNODB_LOG_WRITE_MAX_SIZE_DEFAULT = 4096;
267 
268 /** Default value of innodb_log_checkpointer_every (in milliseconds). */
269 constexpr ulong INNODB_LOG_CHECKPOINT_EVERY_DEFAULT = 1000;  // 1000ms = 1s
270 
271 /** Default value of innodb_log_writer_spin_delay (in spin rounds).
272 We measured that 1000 spin round takes 4us. We decided to select 1ms
273 as the maximum time for busy waiting. Therefore it corresponds to 250k
274 spin rounds. Note that first wait on event takes 50us-100us (even if 10us
275 is passed), so it is 5%-10% of the total time that we have already spent
276 on busy waiting, when we fall back to wait on event. */
277 constexpr ulong INNODB_LOG_WRITER_SPIN_DELAY_DEFAULT = 250000;
278 
279 /** Default value of innodb_log_writer_timeout (in microseconds).
280 Note that it will anyway take at least 50us. */
281 constexpr ulong INNODB_LOG_WRITER_TIMEOUT_DEFAULT = 10;
282 
283 /** Default value of innodb_log_spin_cpu_abs_lwm.
284 Expressed in percent (80 stands for 80%) of a single CPU core. */
285 constexpr ulong INNODB_LOG_SPIN_CPU_ABS_LWM_DEFAULT = 80;
286 
287 /** Default value of innodb_log_spin_cpu_pct_hwm.
288 Expressed in percent (50 stands for 50%) of all CPU cores. */
289 constexpr uint INNODB_LOG_SPIN_CPU_PCT_HWM_DEFAULT = 50;
290 
291 /** Default value of innodb_log_wait_for_write_spin_delay (in spin rounds).
292 Read about INNODB_LOG_WRITER_SPIN_DELAY_DEFAULT.
293 Number of spin rounds is calculated according to current usage of CPU cores.
294 If the usage is smaller than lwm percents of single core, then max rounds = 0.
295 If the usage is smaller than 50% of hwm percents of all cores, then max rounds
296 is decreasing linearly from 10x innodb_log_writer_spin_delay to 1x (for 50%).
297 Then in range from 50% of hwm to 100% of hwm, the max rounds stays equal to
298 the innodb_log_writer_spin_delay, because it doesn't make sense to use too
299 short waits. Hence this is minimum value for the max rounds when non-zero
300 value is being used. */
301 constexpr ulong INNODB_LOG_WAIT_FOR_WRITE_SPIN_DELAY_DEFAULT = 25000;
302 
303 /** Default value of innodb_log_wait_for_write_timeout (in microseconds). */
304 constexpr ulong INNODB_LOG_WAIT_FOR_WRITE_TIMEOUT_DEFAULT = 1000;
305 
306 /** Default value of innodb_log_wait_for_flush_spin_delay (in spin rounds).
307 Read about INNODB_LOG_WAIT_FOR_WRITE_SPIN_DELAY_DEFAULT. The same mechanism
308 applies here (to compute max rounds). */
309 constexpr ulong INNODB_LOG_WAIT_FOR_FLUSH_SPIN_DELAY_DEFAULT = 25000;
310 
311 /** Default value of innodb_log_wait_for_flush_spin_hwm (in microseconds). */
312 constexpr ulong INNODB_LOG_WAIT_FOR_FLUSH_SPIN_HWM_DEFAULT = 400;
313 
314 /** Default value of innodb_log_wait_for_flush_timeout (in microseconds). */
315 constexpr ulong INNODB_LOG_WAIT_FOR_FLUSH_TIMEOUT_DEFAULT = 1000;
316 
317 /** Default value of innodb_log_flusher_spin_delay (in spin rounds).
318 Read about INNODB_LOG_WRITER_SPIN_DELAY_DEFAULT. */
319 constexpr ulong INNODB_LOG_FLUSHER_SPIN_DELAY_DEFAULT = 250000;
320 
321 /** Default value of innodb_log_flusher_timeout (in microseconds).
322 Note that it will anyway take at least 50us. */
323 constexpr ulong INNODB_LOG_FLUSHER_TIMEOUT_DEFAULT = 10;
324 
325 /** Default value of innodb_log_write_notifier_spin_delay (in spin rounds). */
326 constexpr ulong INNODB_LOG_WRITE_NOTIFIER_SPIN_DELAY_DEFAULT = 0;
327 
328 /** Default value of innodb_log_write_notifier_timeout (in microseconds). */
329 constexpr ulong INNODB_LOG_WRITE_NOTIFIER_TIMEOUT_DEFAULT = 10;
330 
331 /** Default value of innodb_log_flush_notifier_spin_delay (in spin rounds). */
332 constexpr ulong INNODB_LOG_FLUSH_NOTIFIER_SPIN_DELAY_DEFAULT = 0;
333 
334 /** Default value of innodb_log_flush_notifier_timeout (in microseconds). */
335 constexpr ulong INNODB_LOG_FLUSH_NOTIFIER_TIMEOUT_DEFAULT = 10;
336 
337 /** Default value of innodb_log_closer_spin_delay (in spin rounds). */
338 constexpr ulong INNODB_LOG_CLOSER_SPIN_DELAY_DEFAULT = 0;
339 
340 /** Default value of innodb_log_closer_timeout (in microseconds). */
341 constexpr ulong INNODB_LOG_CLOSER_TIMEOUT_DEFAULT = 1000;
342 
343 /** Default value of innodb_log_buffer_size (in bytes). */
344 constexpr ulong INNODB_LOG_BUFFER_SIZE_DEFAULT = 16 * 1024 * 1024UL;
345 
346 /** Minimum allowed value of innodb_log_buffer_size. */
347 constexpr ulong INNODB_LOG_BUFFER_SIZE_MIN = 256 * 1024UL;
348 
349 /** Maximum allowed value of innodb_log_buffer_size. */
350 constexpr ulong INNODB_LOG_BUFFER_SIZE_MAX = ULONG_MAX;
351 
352 /** Default value of innodb_log_recent_written_size (in bytes). */
353 constexpr ulong INNODB_LOG_RECENT_WRITTEN_SIZE_DEFAULT = 1024 * 1024;
354 
355 /** Minimum allowed value of innodb_log_recent_written_size. */
356 constexpr ulong INNODB_LOG_RECENT_WRITTEN_SIZE_MIN = OS_FILE_LOG_BLOCK_SIZE;
357 
358 /** Maximum allowed value of innodb_log_recent_written_size. */
359 constexpr ulong INNODB_LOG_RECENT_WRITTEN_SIZE_MAX = 1024 * 1024 * 1024UL;
360 
361 /** Default value of innodb_log_recent_closed_size (in bytes). */
362 constexpr ulong INNODB_LOG_RECENT_CLOSED_SIZE_DEFAULT = 2 * 1024 * 1024;
363 
364 /** Minimum allowed value of innodb_log_recent_closed_size. */
365 constexpr ulong INNODB_LOG_RECENT_CLOSED_SIZE_MIN = OS_FILE_LOG_BLOCK_SIZE;
366 
367 /** Maximum allowed value of innodb_log_recent_closed_size. */
368 constexpr ulong INNODB_LOG_RECENT_CLOSED_SIZE_MAX = 1024 * 1024 * 1024UL;
369 
370 /** Default value of innodb_log_events (number of events). */
371 constexpr ulong INNODB_LOG_EVENTS_DEFAULT = 2048;
372 
373 /** Minimum allowed value of innodb_log_events. */
374 constexpr ulong INNODB_LOG_EVENTS_MIN = 1;
375 
376 /** Maximum allowed value of innodb_log_events. */
377 constexpr ulong INNODB_LOG_EVENTS_MAX = 1024 * 1024 * 1024UL;
378 
379 /** Default value of innodb_log_write_ahead_size (in bytes). */
380 constexpr ulong INNODB_LOG_WRITE_AHEAD_SIZE_DEFAULT = 8192;
381 
382 /** Minimum allowed value of innodb_log_write_ahead_size. */
383 constexpr ulong INNODB_LOG_WRITE_AHEAD_SIZE_MIN = OS_FILE_LOG_BLOCK_SIZE;
384 
385 /** Maximum allowed value of innodb_log_write_ahead_size. */
386 constexpr ulint INNODB_LOG_WRITE_AHEAD_SIZE_MAX =
387     UNIV_PAGE_SIZE_DEF;  // 16kB...
388 
389 /** Value to which MLOG_TEST records should sum up within a group. */
390 constexpr int64_t MLOG_TEST_VALUE = 10000;
391 
392 /** Maximum size of single MLOG_TEST record (in bytes). */
393 constexpr uint32_t MLOG_TEST_MAX_REC_LEN = 100;
394 
395 /** Maximum number of MLOG_TEST records in single group of log records. */
396 constexpr uint32_t MLOG_TEST_GROUP_MAX_REC_N = 100;
397 
398 /** Bytes consumed by MLOG_TEST record with an empty payload. */
399 constexpr uint32_t MLOG_TEST_REC_OVERHEAD = 37;
400 
401 /** Redo log system (singleton). */
402 extern log_t *log_sys;
403 
404 /** Pointer to the log checksum calculation function. Changes are protected
405 by log_mutex_enter_all, which also stops the log background threads. */
406 extern log_checksum_func_t log_checksum_algorithm_ptr;
407 
408 /** This version is used to recreate the log files in the same format that
409 PXB read. i.e. if PXB is processing redo from 8.0.19 or below (v3), then
410 it has to recreate log files with v3 header. If PXB is processing redo from
411 8.0.20 above (v4), it has to recreate log files with v4 header.
412 We cannot reuse log_sys->format for this purpose because it is dellocated and re-initialized during PXB prepare */
413 extern uint32_t log_detected_format;
414 
415 #ifndef UNIV_HOTBACKUP
416 /** Represents currently running test of redo log, nullptr otherwise. */
417 extern std::unique_ptr<Log_test> log_test;
418 #endif /* !UNIV_HOTBACKUP */
419 
420 /* Declaration of inline functions (definition is available in log0log.ic). */
421 
422 /** Gets a log block flush bit. The flush bit is set, if and only if,
423 the block was the first block written in a call to fil_io().
424 
425 During recovery, when encountered the flush bit, recovery code can be
426 pretty sure, that all previous blocks belong to a completed fil_io(),
427 because the block with flush bit belongs to the next call to fil_io(),
428 which could only be started after the previous one has been finished.
429 
430 @param[in]	log_block	log block
431 @return true if this block was the first to be written in fil_io(). */
432 inline bool log_block_get_flush_bit(const byte *log_block);
433 
434 /** Sets the log block flush bit.
435 @param[in,out]	log_block	log block (must have hdr_no != 0)
436 @param[in]	value		value to set */
437 inline void log_block_set_flush_bit(byte *log_block, bool value);
438 
439 /** Gets a log block number stored in the header. The number corresponds
440 to lsn range for data stored in the block.
441 
442 During recovery, when a next block is being parsed, a next range of lsn
443 values is expected to be read. This corresponds to a log block number
444 increased by one. However, if a smaller number is read from the header,
445 it is then considered the end of the redo log and recovery is finished.
446 In such case, the next block is most likely an empty block or a block
447 from the past, because the redo log is written in circular manner.
448 
449 @param[in]	log_block	log block (may be invalid or empty block)
450 @return log block number stored in the block header */
451 inline uint32_t log_block_get_hdr_no(const byte *log_block);
452 
453 /** Sets the log block number stored in the header.
454 NOTE that this must be set before the flush bit!
455 
456 @param[in,out]	log_block	log block
457 @param[in]	n		log block number: must be in (0, 1G] */
458 inline void log_block_set_hdr_no(byte *log_block, uint32_t n);
459 
460 /** Gets a log block data length.
461 @param[in]	log_block	log block
462 @return log block data length measured as a byte offset from the block start */
463 inline uint32_t log_block_get_data_len(const byte *log_block);
464 
465 /** Sets the log block data length.
466 @param[in,out]	log_block	log block
467 @param[in]	len		data length (@see log_block_get_data_len) */
468 inline void log_block_set_data_len(byte *log_block, ulint len);
469 
470 /** Gets an offset to the beginning of the first group of log records
471 in a given log block.
472 @param[in]	log_block	log block
473 @return first mtr log record group byte offset from the block start,
474 0 if none. */
475 inline uint32_t log_block_get_first_rec_group(const byte *log_block);
476 
477 /** Sets an offset to the beginning of the first group of log records
478 in a given log block.
479 @param[in,out]	log_block	log block
480 @param[in]	offset		offset, 0 if none */
481 inline void log_block_set_first_rec_group(byte *log_block, uint32_t offset);
482 
483 /** Gets a log block checkpoint number field (4 lowest bytes).
484 @param[in]	log_block	log block
485 @return checkpoint no (4 lowest bytes) */
486 inline uint32_t log_block_get_checkpoint_no(const byte *log_block);
487 
488 /** Sets a log block checkpoint number field (4 lowest bytes).
489 @param[in,out]	log_block	log block
490 @param[in]	no		checkpoint no */
491 inline void log_block_set_checkpoint_no(byte *log_block, uint64_t no);
492 
493 /** Converts a lsn to a log block number. Consecutive log blocks have
494 consecutive numbers (unless the sequence wraps). It is guaranteed that
495 the calculated number is greater than zero.
496 
497 @param[in]	lsn	lsn of a byte within the block
498 @return log block number, it is > 0 and <= 1G */
499 inline uint32_t log_block_convert_lsn_to_no(lsn_t lsn);
500 
501 /** Calculates the checksum for a log block.
502 @param[in]	log_block	log block
503 @return checksum */
504 inline uint32_t log_block_calc_checksum(const byte *log_block);
505 
506 /** Calculates the checksum for a log block using the MySQL 5.7 algorithm.
507 @param[in]	log_block	log block
508 @return checksum */
509 inline uint32_t log_block_calc_checksum_crc32(const byte *log_block);
510 
511 /** Calculates the checksum for a log block using the "no-op" algorithm.
512 @param[in]     log_block   log block
513 @return        checksum */
514 inline uint32_t log_block_calc_checksum_none(const byte *log_block);
515 
516 /** Gets value of a log block checksum field.
517 @param[in]	log_block	log block
518 @return checksum */
519 inline uint32_t log_block_get_checksum(const byte *log_block);
520 
521 /** Sets value of a log block checksum field.
522 @param[in,out]	log_block	log block
523 @param[in]	checksum	checksum */
524 inline void log_block_set_checksum(byte *log_block, uint32_t checksum);
525 
526 /** Stores a 4-byte checksum to the trailer checksum field of a log block.
527 This is used before writing the log block to disk. The checksum in a log
528 block is used in recovery to check the consistency of the log block.
529 @param[in]	log_block	 log block (completely filled in!) */
530 inline void log_block_store_checksum(byte *log_block);
531 
532 /** Gets the current lsn value. This value points to the first non
533 reserved data byte in the redo log. When next user thread reserves
534 space in the redo log, it starts at this lsn.
535 
536 If the last reservation finished exactly before footer of log block,
537 this value points to the first byte after header of the next block.
538 
539 NOTE that it is possible that the current lsn value does not fit
540 free space in the log files or in the log buffer. In such case,
541 user threads need to wait until the space becomes available.
542 
543 @return current lsn */
544 inline lsn_t log_get_lsn(const log_t &log);
545 
546 /** Gets the last checkpoint lsn stored and flushed to disk.
547 @return last checkpoint lsn */
548 inline lsn_t log_get_checkpoint_lsn(const log_t &log);
549 
550 #ifndef UNIV_HOTBACKUP
551 
552 /** @return true iff log_free_check should be executed. */
553 inline bool log_needs_free_check();
554 
555 /** Any database operation should call this when it has modified more than
556 about 4 pages. NOTE that this function may only be called when the thread
557 owns no synchronization objects except the dictionary mutex.
558 
559 Checks if current log.sn exceeds log.free_check_limit_sn, in which case waits.
560 This is supposed to guarantee that we would not run out of space in the log
561 files when holding latches of some dirty pages (which could end up in
562 a deadlock, because flush of the latched dirty pages could be required
563 to reclaim the space and it is impossible to flush latched pages). */
564 inline void log_free_check();
565 
566 /** Calculates lsn value for given sn value. Sequence of sn values
567 enumerate all data bytes in the redo log. Sequence of lsn values
568 enumerate all data bytes and bytes used for headers and footers
569 of all log blocks in the redo log. For every LOG_BLOCK_DATA_SIZE
570 bytes of data we have OS_FILE_LOG_BLOCK_SIZE bytes in the redo log.
571 NOTE that LOG_BLOCK_DATA_SIZE + LOG_BLOCK_HDR_SIZE + LOG_BLOCK_TRL_SIZE
572 == OS_FILE_LOG_BLOCK_SIZE. The calculated lsn value will always point
573 to some data byte (will be % OS_FILE_LOG_BLOCK_SIZE >= LOG_BLOCK_HDR_SIZE,
574 and < OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE).
575 
576 @param[in]	sn	sn value
577 @return lsn value for the provided sn value */
578 constexpr inline lsn_t log_translate_sn_to_lsn(lsn_t sn);
579 
580 /** Calculates sn value for given lsn value.
581 @see log_translate_sn_to_lsn
582 @param[in]	lsn	lsn value
583 @return sn value for the provided lsn value */
584 inline lsn_t log_translate_lsn_to_sn(lsn_t lsn);
585 
586 #endif /* !UNIV_HOTBACKUP */
587 
588 /** Validates a given lsn value. Checks if the lsn value points to data
589 bytes inside log block (not to some bytes in header/footer). It is used
590 by assertions.
591 @return true if lsn points to data bytes within log block */
592 inline bool log_lsn_validate(lsn_t lsn);
593 
594 #ifndef UNIV_HOTBACKUP
595 
596 /** Calculates age of current checkpoint as number of bytes since
597 last checkpoint. This includes bytes for headers and footers of
598 all log blocks. The calculation is based on the latest written
599 checkpoint lsn, and the current lsn, which points to the first
600 non reserved data byte. Note that the current lsn could not fit
601 the free space in the log files. This means that the checkpoint
602 age could potentially be larger than capacity of the log files.
603 However we do the best effort to avoid such situations, and if
604 they happen, user threads wait until the space is reclaimed.
605 @param[in]	log	redo log
606 @return checkpoint age as number of bytes */
607 inline lsn_t log_get_checkpoint_age(const log_t &log);
608 
609 /* Declaration of log_buffer functions (definition in log0buf.cc). */
610 
611 /** Reserves space in the redo log for following write operations.
612 Space is reserved for a given number of data bytes. Additionally
613 bytes for required headers and footers of log blocks are reserved.
614 
615 After the space is reserved, range of lsn values from a start_lsn
616 to an end_lsn is assigned. The log writer thread cannot proceed
617 further than to the start_lsn, until a link start_lsn -> end_lsn
618 has been added to the log recent written buffer.
619 
620 NOTE that the link is added after data is written to the reserved
621 space in the log buffer. It is very critical to do all these steps
622 as fast as possible, because very likely the log writer thread is
623 waiting for the link.
624 */
625 MY_COMPILER_DIAGNOSTIC_PUSH()
626 MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG()
627 /**
628 @see @ref sect_redo_log_buf_reserve
629 */
630 MY_COMPILER_DIAGNOSTIC_POP()
631 /**
632 @param[in,out]	log	redo log
633 @param[in]	len	number of data bytes to reserve for write
634 @return handle that represents the reservation */
635 Log_handle log_buffer_reserve(log_t &log, size_t len);
636 
637 /** Writes data to the log buffer. The space in the redo log has to be
638 reserved before calling to this function and lsn pointing to inside the
639 reserved range of lsn values has to be provided.
640 
641 The write does not have to cover the whole reserved space, but may not
642 overflow it. If it does not cover, then returned value should be used
643 to start the next write operation. Note that finally we must use exactly
644 all the reserved space.
645 
646 */
647 MY_COMPILER_DIAGNOSTIC_PUSH()
648 MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG()
649 /**
650 @see @ref sect_redo_log_buf_write
651 */
652 MY_COMPILER_DIAGNOSTIC_POP()
653 /**
654 @param[in,out]	log		redo log
655 @param[in]	handle		handle for the reservation of space
656 @param[in]	str		memory to write data from
657 @param[in]	str_len		number of bytes to write
658 @param[in]	start_lsn	lsn to start writing at (the reserved space)
659 
660 @return end_lsn after writing the data (in the reserved space), could be
661 used to start the next write operation if there is still free space in
662 the reserved space */
663 lsn_t log_buffer_write(log_t &log, const Log_handle &handle, const byte *str,
664                        size_t str_len, lsn_t start_lsn);
665 
666 /** Adds a link start_lsn -> end_lsn to the log recent written buffer.
667 
668 This function must be called after the data has been written to the
669 fragment of log buffer represented by range [start_lsn, end_lsn).
670 After the link is added, the log writer may write the data to disk.
671 
672 NOTE that still dirty pages for the [start_lsn, end_lsn) are not added
673 to flush lists when this function is called.
674 
675 */
676 MY_COMPILER_DIAGNOSTIC_PUSH()
677 MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG()
678 /**
679 @see @ref sect_redo_log_buf_add_links_to_recent_written
680 */
681 MY_COMPILER_DIAGNOSTIC_POP()
682 /**
683 @param[in,out]	log		redo log
684 @param[in]	handle		handle for the reservation of space
685 @param[in]	start_lsn	start_lsn of the link to add
686 @param[in]	end_lsn		end_lsn of the link to add */
687 void log_buffer_write_completed(log_t &log, const Log_handle &handle,
688                                 lsn_t start_lsn, lsn_t end_lsn);
689 
690 /** Modifies header of log block in the log buffer, which contains
691 a given lsn value, and sets offset to the first group of log records
692 within the block.
693 
694 This is used by mtr after writing a log record group which ends at
695 lsn belonging to different log block than lsn at which the group was
696 started. When write was finished at the last data byte of log block,
697 it is considered ended in the next log block, because the next data
698 byte belongs to that block.
699 
700 During recovery, when recovery is started in the middle of some group
701 of log records, it first looks for the beginning of the next group.
702 
703 @param[in,out]	log			redo log
704 @param[in]	handle			handle for the reservation of space
705 @param[in]	rec_group_end_lsn	lsn at which the first log record
706 group starts within the block containing this lsn value */
707 void log_buffer_set_first_record_group(log_t &log, const Log_handle &handle,
708                                        lsn_t rec_group_end_lsn);
709 
710 /** Adds a link start_lsn -> end_lsn to the log recent closed buffer.
711 
712 This is called after all dirty pages related to [start_lsn, end_lsn)
713 have been added to corresponding flush lists.
714 For detailed explanation - @see log0write.cc.
715 
716 */
717 MY_COMPILER_DIAGNOSTIC_PUSH()
718 MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG()
719 /**
720 @see @ref sect_redo_log_add_link_to_recent_closed
721 */
722 MY_COMPILER_DIAGNOSTIC_POP()
723 /**
724 @param[in,out]	log		redo log
725 @param[in]	handle		handle for the reservation of space */
726 void log_buffer_close(log_t &log, const Log_handle &handle);
727 
728 /** Write to the log file up to the last log entry.
729 @param[in,out]	log	redo log
730 @param[in]	sync	whether we want the written log
731 also to be flushed to disk. */
732 void log_buffer_flush_to_disk(log_t &log, bool sync = true);
733 
734 /** Requests flush of the log buffer.
735 @param[in]	sync	true: wait until the flush is done */
736 inline void log_buffer_flush_to_disk(bool sync = true);
737 
738 /** @return lsn up to which all writes to log buffer have been finished */
739 inline lsn_t log_buffer_ready_for_write_lsn(const log_t &log);
740 
741 /** @return lsn up to which all dirty pages have been added to flush list */
742 inline lsn_t log_buffer_dirty_pages_added_up_to_lsn(const log_t &log);
743 
744 /** @return capacity of the recent_closed, or 0 if !log_use_threads() */
745 inline lsn_t log_buffer_flush_order_lag(const log_t &log);
746 
747 /** Get last redo block from redo buffer and end LSN. Note that it takes
748 x-lock on the log buffer for a short period. Out values are always set,
749 even when provided last_block is nullptr.
750 @param[in,out]	log		redo log
751 @param[out]	last_lsn	end lsn of last mtr
752 @param[out]	last_block	last redo block
753 @param[in,out]	block_len	length in bytes */
754 void log_buffer_get_last_block(log_t &log, lsn_t &last_lsn, byte *last_block,
755                                uint32_t &block_len);
756 
757 /** Advances log.buf_ready_for_write_lsn using links in the recent written
758 buffer. It's used by the log writer thread only.
759 @param[in]	log	redo log
760 @return true if and only if the lsn has been advanced */
761 bool log_advance_ready_for_write_lsn(log_t &log);
762 
763 /** Advances log.buf_dirty_pages_added_up_to_lsn using links in the recent
764 closed buffer. It's used by the log closer thread only.
765 @param[in]	log	redo log
766 @return true if and only if the lsn has been advanced */
767 bool log_advance_dirty_pages_added_up_to_lsn(log_t &log);
768 
769 /** Validates that all slots in log recent written buffer for lsn values
770 in range between begin and end, are empty. Used during tests, crashes the
771 program if validation does not pass.
772 @param[in]	log		redo log which buffer is validated
773 @param[in]	begin		validation start (inclusive)
774 @param[in]	end		validation end (exclusive) */
775 void log_recent_written_empty_validate(const log_t &log, lsn_t begin,
776                                        lsn_t end);
777 
778 /** Validates that all slots in log recent closed buffer for lsn values
779 in range between begin and end, are empty. Used during tests, crashes the
780 program if validation does not pass.
781 @param[in]	log		redo log which buffer is validated
782 @param[in]	begin		validation start (inclusive)
783 @param[in]	end		validation end (exclusive) */
784 void log_recent_closed_empty_validate(const log_t &log, lsn_t begin, lsn_t end);
785 
786 /* Declaration of remaining functions. */
787 
788 /** Waits until there is free space in the log recent closed buffer
789 for any links start_lsn -> end_lsn, which start at provided start_lsn.
790 It does not add any link.
791 
792 This is called just before dirty pages for [start_lsn, end_lsn)
793 are added to flush lists. That's because we need to guarantee,
794 that the delay until dirty page is added to flush list is limited.
795 For detailed explanation - @see log0write.cc.
796 
797 */
798 MY_COMPILER_DIAGNOSTIC_PUSH()
799 MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG()
800 /**
801 @see @ref sect_redo_log_add_dirty_pages
802 */
803 MY_COMPILER_DIAGNOSTIC_POP()
804 /**
805 @param[in,out]	log   redo log
806 @param[in]      lsn   lsn on which we wait (for any link: lsn -> x) */
807 void log_wait_for_space_in_log_recent_closed(log_t &log, lsn_t lsn);
808 
809 /** Waits until there is free space in the log buffer. The free space has to be
810 available for range of sn values ending at the provided sn.
811 */
812 MY_COMPILER_DIAGNOSTIC_PUSH()
813 MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG()
814 /**
815 @see @ref sect_redo_log_waiting_for_writer
816 */
817 MY_COMPILER_DIAGNOSTIC_POP()
818 /**
819 @param[in]     log     redo log
820 @param[in]     end_sn  end of the range of sn values */
821 void log_wait_for_space_in_log_buf(log_t &log, sn_t end_sn);
822 
823 /** Waits until there is free space for range of sn values ending
824 at the provided sn, in both the log buffer and in the log files.
825 @param[in]	log       redo log
826 @param[in]	end_sn    end of the range of sn values */
827 void log_wait_for_space(log_t &log, sn_t end_sn);
828 
829 /** Computes capacity of redo log available until log_free_check()
830 reaches point where it needs to wait.
831 @param[in]  log       redo log
832 @return lsn capacity up to free_check_wait happens */
833 lsn_t log_get_free_check_capacity(const log_t &log);
834 
835 /** When the oldest dirty page age exceeds this value, we start
836 an asynchronous preflush of dirty pages.
837 @param[in]  log       redo log
838 @return age of dirty page at which async preflush is started */
839 lsn_t log_get_max_modified_age_async(const log_t &log);
840 
841 /** Waits until there is free space in log files which includes
842 concurrency margin required for all threads. You should rather
843 use log_free_check().
844 */
845 MY_COMPILER_DIAGNOSTIC_PUSH()
846 MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG()
847 /**
848 @see @ref sect_redo_log_reclaim_space
849 */
850 MY_COMPILER_DIAGNOSTIC_POP()
851 /**
852 @param[in]     log   redo log */
853 void log_free_check_wait(log_t &log);
854 
855 /** Updates limits related to free space in redo log files:
856 log.available_for_checkpoint_lsn and log.free_check_limit_sn.
857 @param[in,out]  log         redo log */
858 void log_update_limits(log_t &log);
859 
860 /** Updates limit used when writing to log buffer. Note that the
861 log buffer may have space for log records for which we still do
862 not have space in log files (for larger lsn values).
863 @param[in,out]   log        redo log */
864 void log_update_buf_limit(log_t &log);
865 
866 /** Updates limit used when writing to log buffer, according to provided
867 write_lsn. It must be <= log.write_lsn.load() to protect from log buffer
868 overwrites.
869 @param[in,out]   log        redo log
870 @param[in]       write_lsn  value <= log.write_lsn.load() */
871 void log_update_buf_limit(log_t &log, lsn_t write_lsn);
872 
873 /** Waits until the redo log is written up to a provided lsn.
874 @param[in]  log             redo log
875 @param[in]  lsn             lsn to wait for
876 @param[in]  flush_to_disk   true: wait until it is flushed
877 @return statistics about waiting inside */
878 Wait_stats log_write_up_to(log_t &log, lsn_t lsn, bool flush_to_disk);
879 
880 /* Read the first log file header to get the encryption
881 information if it exist.
882 @return true if success */
883 bool log_read_encryption();
884 
885 /** Write the encryption info into the log file header(the 3rd block).
886 It just need to flush the file header block with current master key.
887 @param[in]	key	encryption key
888 @param[in]	iv	encryption iv
889 @param[in]	is_boot	if it is for bootstrap
890 @return true if success. */
891 bool log_write_encryption(byte *key, byte *iv, bool is_boot);
892 
893 /** Rotate the redo log encryption
894 It will re-encrypt the redo log encryption metadata and write it to
895 redo log file header.
896 @return true if success. */
897 bool log_rotate_encryption();
898 
899 /** Rotate default master key for redo log encryption. */
900 void redo_rotate_default_master_key();
901 
902 /** Requests a sharp checkpoint write for provided or greater lsn.
903 @param[in,out]	log	redo log
904 @param[in]	sync	true -> wait until it is finished
905 @param[in]  lsn   lsn for which we need checkpoint (or greater chkp) */
906 void log_request_checkpoint(log_t &log, bool sync, lsn_t lsn);
907 
908 /** Requests a fuzzy checkpoint write (for lsn currently available
909 for checkpointing).
910 @param[in,out]	log	redo log
911 @param[in]	sync	true -> wait until it is finished */
912 void log_request_checkpoint(log_t &log, bool sync);
913 
914 /** Make a checkpoint at the current lsn. Reads current lsn and waits
915 until all dirty pages have been flushed up to that lsn. Afterwards
916 requests a checkpoint write and waits until it is finished.
917 @param[in,out]	log	redo log
918 @return true iff current lsn was greater than last checkpoint lsn */
919 bool log_make_latest_checkpoint(log_t &log);
920 
921 /** Make a checkpoint at the current lsn. Reads current lsn and waits
922 until all dirty pages have been flushed up to that lsn. Afterwards
923 requests a checkpoint write and waits until it is finished.
924 @return true iff current lsn was greater than last checkpoint lsn */
925 bool log_make_latest_checkpoint();
926 
927 /** Reads a log file header page to log.checkpoint_buf.
928 @param[in,out]	log	redo log
929 @param[in]	header	0 or LOG_CHECKPOINT_1 or LOG_CHECKPOINT2 */
930 void log_files_header_read(log_t &log, uint32_t header);
931 
932 /** Fill redo log header.
933 @param[out]	buf		filled buffer
934 @param[in]	start_lsn	log start LSN
935 @param[in]	creator		creator of the header
936 @param[in]	no_logging	redo logging is disabled
937 @param[in]	crash_unsafe	it is not safe to crash */
938 void log_files_header_fill(byte *buf, lsn_t start_lsn, const char *creator,
939                            bool no_logging, bool crash_unsafe);
940 
941 /** Writes a log file header to the log file space.
942 @param[in]	log		redo log
943 @param[in]	nth_file	header for the nth file in the log files
944 @param[in]	start_lsn	log file data starts at this lsn */
945 void log_files_header_flush(log_t &log, uint32_t nth_file, lsn_t start_lsn);
946 
947 /** Changes format of redo files to previous format version.
948 
949 @note Note this will work between the two formats 5_7_9 & current because
950 the only change is the version number */
951 void log_files_downgrade(log_t &log);
952 
953 /** Writes the next checkpoint info to header of the first log file.
954 Note that two pages of the header are used alternately for consecutive
955 checkpoints. If we crashed during the write, we would still have the
956 previous checkpoint info and recovery would work.
957 @param[in,out]	log			redo log
958 @param[in]	next_checkpoint_lsn	writes checkpoint at this lsn */
959 void log_files_write_checkpoint(log_t &log, lsn_t next_checkpoint_lsn);
960 
961 /** Updates current_file_lsn and current_file_real_offset to correspond
962 to a given lsn. For this function to work, the values must already be
963 initialized to correspond to some lsn, for instance, a checkpoint lsn.
964 @param[in,out]	log	redo log
965 @param[in]	lsn	log sequence number to set files_start_lsn at */
966 void log_files_update_offsets(log_t &log, lsn_t lsn);
967 
968 /** Acquires the log buffer s-lock.
969 @param[in,out]	log	redo log
970 @return lock no, must be passed to s_lock_exit() */
971 size_t log_buffer_s_lock_enter(log_t &log);
972 
973 /** Releases the log buffer s-lock.
974 @param[in,out]	log	redo log
975 @param[in]	lock_no	lock no received from s_lock_enter() */
976 void log_buffer_s_lock_exit(log_t &log, size_t lock_no);
977 
978 /** Acquires the log buffer x-lock.
979 @param[in,out]	log	redo log */
980 void log_buffer_x_lock_enter(log_t &log);
981 
982 /** Releases the log buffer x-lock.
983 @param[in,out]	log	redo log */
984 void log_buffer_x_lock_exit(log_t &log);
985 #endif /* !UNIV_HOTBACKUP */
986 
987 /** Calculates offset within log files, excluding headers of log files.
988 @param[in]	log		redo log
989 @param[in]	offset		real offset (including log file headers)
990 @return	size offset excluding log file headers (<= offset) */
991 uint64_t log_files_size_offset(const log_t &log, uint64_t offset);
992 
993 /** Calculates offset within log files, including headers of log files.
994 @param[in]	log		redo log
995 @param[in]	offset		size offset (excluding log file headers)
996 @return real offset including log file headers (>= offset) */
997 uint64_t log_files_real_offset(const log_t &log, uint64_t offset);
998 
999 /** Calculates offset within log files, including headers of log files,
1000 for the provided lsn value.
1001 @param[in]	log	redo log
1002 @param[in]	lsn	log sequence number
1003 @return real offset within the log files */
1004 uint64_t log_files_real_offset_for_lsn(const log_t &log, lsn_t lsn);
1005 #ifndef UNIV_HOTBACKUP
1006 
1007 /** Changes size of the log buffer. This is a thread-safe version.
1008 It is used by SET GLOBAL innodb_log_buffer_size = X.
1009 @param[in,out]	log		redo log
1010 @param[in]	new_size	requested new size
1011 @return true iff succeeded in resize */
1012 bool log_buffer_resize(log_t &log, size_t new_size);
1013 
1014 /** Changes size of the log buffer. This is a non-thread-safe version
1015 which might be invoked only when there are no concurrent possible writes
1016 to the log buffer. It is used in log_buffer_reserve() when a requested
1017 size to reserve is larger than size of the log buffer.
1018 @param[in,out]	log		redo log
1019 @param[in]	new_size	requested new size
1020 @param[in]	end_lsn		maximum lsn written to log buffer
1021 @return true iff succeeded in resize */
1022 bool log_buffer_resize_low(log_t &log, size_t new_size, lsn_t end_lsn);
1023 
1024 /** Resizes the write ahead buffer in the redo log.
1025 @param[in,out]	log		redo log
1026 @param[in]	new_size	new size (in bytes) */
1027 void log_write_ahead_resize(log_t &log, size_t new_size);
1028 
1029 /** Updates the field log.dict_max_allowed_checkpoint_lsn.
1030 @param[in,out]  log      redo log
1031 @param[in]      max_lsn  new value for the field */
1032 void log_set_dict_max_allowed_checkpoint_lsn(log_t &log, lsn_t max_lsn);
1033 
1034 /** Updates log.dict_persist_margin and recompute free check limit.
1035 @param[in,out]  log     redo log
1036 @param[in]      margin  new value for log.dict_persist_margin */
1037 void log_set_dict_persist_margin(log_t &log, sn_t margin);
1038 
1039 /** Increase concurrency_margin used inside log_free_check() calls. */
1040 void log_increase_concurrency_margin(log_t &log);
1041 
1042 /** Prints information about important lsn values used in the redo log,
1043 and some statistics about speed of writing and flushing of data.
1044 @param[in]	log	redo log for which print information
1045 @param[out]	file	file where to print */
1046 void log_print(const log_t &log, FILE *file);
1047 
1048 /** Refreshes the statistics used to print per-second averages in log_print().
1049 @param[in,out]	log	redo log */
1050 void log_refresh_stats(log_t &log);
1051 
1052 /** Creates the first checkpoint ever in the log files. Used during
1053 initialization of new log files. Flushes:
1054   - header of the first log file (including checkpoint headers),
1055   - log block with data addressed by the checkpoint lsn.
1056 @param[in,out]	log	redo log
1057 @param[in]	lsn	the first checkpoint lsn */
1058 void log_create_first_checkpoint(log_t &log, lsn_t lsn);
1059 
1060 /** Calculates limits for maximum age of checkpoint and maximum age of
1061 the oldest page.
1062 @param[in,out]	log	redo log */
1063 void log_calc_max_ages(log_t &log);
1064 
1065 /** Updates concurrency margin. Uses current value of srv_thread_concurrency.
1066 @param[in,out]	log	redo log
1067 @retval true if success
1068 @retval false if the redo log is too small to accommodate the number of
1069 OS threads in the database server */
1070 bool log_calc_concurrency_margin(log_t &log);
1071 
1072 /** Initializes the log system. Note that the log system is not ready
1073 for user writes after this call is finished. It should be followed by
1074 a call to log_start. Also, log background threads need to be started
1075 manually using log_start_background_threads afterwards.
1076 
1077 Hence the proper order of calls looks like this:
1078         - log_sys_init(),
1079         - log_start(),
1080         - log_start_background_threads().
1081 
1082 @param[in]	n_files		number of log files
1083 @param[in]	file_size	size of each log file in bytes
1084 @param[in]	space_id	space id of the file space with log files */
1085 bool log_sys_init(uint32_t n_files, uint64_t file_size, space_id_t space_id);
1086 
1087 /** Starts the initialized redo log system using a provided
1088 checkpoint_lsn and current lsn. Block for current_lsn must
1089 be properly initialized in the log buffer prior to calling
1090 this function. Therefore a proper value of first_rec_group
1091 must be set for that block before log_start is called.
1092 @param[in,out]  log             redo log
1093 @param[in]      checkpoint_no	  checkpoint no (sequential number)
1094 @param[in]      checkpoint_lsn  checkpoint lsn
1095 @param[in]      start_lsn       current lsn to start at */
1096 void log_start(log_t &log, checkpoint_no_t checkpoint_no, lsn_t checkpoint_lsn,
1097                lsn_t start_lsn);
1098 
1099 /** Validates that the log writer thread is active.
1100 Used only to assert, that the state is correct.
1101 @param[in]	log	redo log */
1102 void log_writer_thread_active_validate(const log_t &log);
1103 
1104 /** Validates that the log closer thread is active.
1105 Used only to assert, that the state is correct.
1106 @param[in]	log	redo log */
1107 void log_closer_thread_active_validate(const log_t &log);
1108 
1109 /** Validates that the log writer, flusher threads are active.
1110 Used only to assert, that the state is correct.
1111 @param[in]	log	redo log */
1112 void log_background_write_threads_active_validate(const log_t &log);
1113 
1114 /** Validates that all the log background threads are active.
1115 Used only to assert, that the state is correct.
1116 @param[in]	log	redo log */
1117 void log_background_threads_active_validate(const log_t &log);
1118 
1119 /** Validates that all the log background threads are inactive.
1120 Used only to assert, that the state is correct.
1121 @param[in]	log	redo log */
1122 void log_background_threads_inactive_validate(const log_t &log);
1123 
1124 /** Starts all the log background threads. This can be called only,
1125 when the threads are inactive. This should never be called concurrently.
1126 This may not be called during read-only mode.
1127 @param[in,out]	log	redo log */
1128 void log_start_background_threads(log_t &log);
1129 
1130 /** Stops all the log background threads. This can be called only,
1131 when the threads are active. This should never be called concurrently.
1132 This may not be called in read-only mode. Note that is is impossible
1133 to start log background threads in such case.
1134 @param[in,out]	log	redo log */
1135 void log_stop_background_threads(log_t &log);
1136 
1137 /** Marks the flag which tells log threads to stop and wakes them.
1138 Does not wait until they are stopped. */
1139 void log_stop_background_threads_nowait(log_t &log);
1140 
1141 /** Wakes up all log threads which are alive. */
1142 void log_wake_threads(log_t &log);
1143 
1144 /** Free the log system data structures. Deallocate all the related memory. */
1145 void log_sys_close();
1146 
1147 /** The log writer thread co-routine.
1148  */
1149 MY_COMPILER_DIAGNOSTIC_PUSH()
1150 MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG()
1151 /**
1152 @see @ref sect_redo_log_writer
1153 */
1154 MY_COMPILER_DIAGNOSTIC_POP()
1155 /**
1156 @param[in,out]	log_ptr		pointer to redo log */
1157 void log_writer(log_t *log_ptr);
1158 
1159 /** The log flusher thread co-routine.
1160  */
1161 MY_COMPILER_DIAGNOSTIC_PUSH()
1162 MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG()
1163 /**
1164 @see @ref sect_redo_log_flusher
1165 */
1166 MY_COMPILER_DIAGNOSTIC_POP()
1167 /**
1168 @param[in,out]	log_ptr		pointer to redo log */
1169 void log_flusher(log_t *log_ptr);
1170 
1171 /** The log flush notifier thread co-routine.
1172  */
1173 MY_COMPILER_DIAGNOSTIC_PUSH()
1174 MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG()
1175 /**
1176 @see @ref sect_redo_log_flush_notifier
1177 */
1178 MY_COMPILER_DIAGNOSTIC_POP()
1179 /**
1180 @param[in,out]	log_ptr		pointer to redo log */
1181 void log_flush_notifier(log_t *log_ptr);
1182 
1183 /** The log write notifier thread co-routine.
1184  */
1185 MY_COMPILER_DIAGNOSTIC_PUSH()
1186 MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG()
1187 /**
1188 @see @ref sect_redo_log_write_notifier
1189 */
1190 MY_COMPILER_DIAGNOSTIC_POP()
1191 /**
1192 @param[in,out]	log_ptr		pointer to redo log */
1193 void log_write_notifier(log_t *log_ptr);
1194 
1195 /** The log closer thread co-routine.
1196  */
1197 MY_COMPILER_DIAGNOSTIC_PUSH()
1198 MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG()
1199 /**
1200 @see @ref sect_redo_log_closer
1201 */
1202 MY_COMPILER_DIAGNOSTIC_POP()
1203 /**
1204 @param[in,out]	log_ptr		pointer to redo log */
1205 void log_closer(log_t *log_ptr);
1206 
1207 /** The log checkpointer thread co-routine.
1208  */
1209 MY_COMPILER_DIAGNOSTIC_PUSH()
1210 MY_COMPILER_CLANG_WORKAROUND_REF_DOCBUG()
1211 /**
1212 @see @ref sect_redo_log_checkpointer
1213 */
1214 MY_COMPILER_DIAGNOSTIC_POP()
1215 /**
1216 @param[in,out]	log_ptr		pointer to redo log */
1217 void log_checkpointer(log_t *log_ptr);
1218 
1219 #define log_buffer_x_lock_own(log) log.sn_lock.x_own()
1220 
1221 #define log_checkpointer_mutex_enter(log) \
1222   mutex_enter(&((log).checkpointer_mutex))
1223 
1224 #define log_checkpointer_mutex_exit(log) mutex_exit(&((log).checkpointer_mutex))
1225 
1226 #define log_checkpointer_mutex_own(log) \
1227   (mutex_own(&((log).checkpointer_mutex)) || !log_checkpointer_is_active())
1228 
1229 #define log_closer_mutex_enter(log) mutex_enter(&((log).closer_mutex))
1230 
1231 #define log_closer_mutex_exit(log) mutex_exit(&((log).closer_mutex))
1232 
1233 #define log_closer_mutex_own(log) \
1234   (mutex_own(&((log).closer_mutex)) || !log_closer_is_active())
1235 
1236 #define log_flusher_mutex_enter(log) mutex_enter(&((log).flusher_mutex))
1237 
1238 #define log_flusher_mutex_enter_nowait(log) \
1239   mutex_enter_nowait(&((log).flusher_mutex))
1240 
1241 #define log_flusher_mutex_exit(log) mutex_exit(&((log).flusher_mutex))
1242 
1243 #define log_flusher_mutex_own(log) \
1244   (mutex_own(&((log).flusher_mutex)) || !log_flusher_is_active())
1245 
1246 #define log_flush_notifier_mutex_enter(log) \
1247   mutex_enter(&((log).flush_notifier_mutex))
1248 
1249 #define log_flush_notifier_mutex_exit(log) \
1250   mutex_exit(&((log).flush_notifier_mutex))
1251 
1252 #define log_flush_notifier_mutex_own(log) \
1253   (mutex_own(&((log).flush_notifier_mutex)) || !log_flush_notifier_is_active())
1254 
1255 #define log_writer_mutex_enter(log) mutex_enter(&((log).writer_mutex))
1256 
1257 #define log_writer_mutex_enter_nowait(log) \
1258   mutex_enter_nowait(&((log).writer_mutex))
1259 
1260 #define log_writer_mutex_exit(log) mutex_exit(&((log).writer_mutex))
1261 
1262 #define log_writer_mutex_own(log) \
1263   (mutex_own(&((log).writer_mutex)) || !log_writer_is_active())
1264 
1265 #define log_write_notifier_mutex_enter(log) \
1266   mutex_enter(&((log).write_notifier_mutex))
1267 
1268 #define log_write_notifier_mutex_exit(log) \
1269   mutex_exit(&((log).write_notifier_mutex))
1270 
1271 #define log_write_notifier_mutex_own(log) \
1272   (mutex_own(&((log).write_notifier_mutex)) || !log_write_notifier_is_active())
1273 
1274 #define log_limits_mutex_enter(log) mutex_enter(&((log).limits_mutex))
1275 
1276 #define log_limits_mutex_exit(log) mutex_exit(&((log).limits_mutex))
1277 
1278 #define log_limits_mutex_own(log) mutex_own(&(log).limits_mutex)
1279 
1280 #define LOG_SYNC_POINT(a)                \
1281   do {                                   \
1282     DEBUG_SYNC_C(a);                     \
1283     DBUG_EXECUTE_IF(a, DBUG_SUICIDE();); \
1284     if (log_test != nullptr) {           \
1285       log_test->sync_point(a);           \
1286     }                                    \
1287   } while (0)
1288 
1289 /** Lock redo log. Both current lsn and checkpoint lsn will not change
1290 until the redo log is unlocked.
1291 @param[in,out]	log	redo log to lock */
1292 void log_position_lock(log_t &log);
1293 
1294 /** Unlock the locked redo log.
1295 @param[in,out]	log	redo log to unlock */
1296 void log_position_unlock(log_t &log);
1297 
1298 /** Collect coordinates in the locked redo log.
1299 @param[in]	log		locked redo log
1300 @param[out]	current_lsn	stores current lsn there
1301 @param[out]	checkpoint_lsn	stores checkpoint lsn there */
1302 void log_position_collect_lsn_info(const log_t &log, lsn_t *current_lsn,
1303                                    lsn_t *checkpoint_lsn);
1304 
1305 /** Checks if log writer thread is active.
1306 @return true if and only if the log writer thread is active */
1307 inline bool log_writer_is_active();
1308 
1309 /** Checks if log write notifier thread is active.
1310 @return true if and only if the log write notifier thread is active */
1311 inline bool log_write_notifier_is_active();
1312 
1313 /** Checks if log flusher thread is active.
1314 @return true if and only if the log flusher thread is active */
1315 inline bool log_flusher_is_active();
1316 
1317 /** Checks if log flush notifier thread is active.
1318 @return true if and only if the log flush notifier thread is active */
1319 inline bool log_flush_notifier_is_active();
1320 
1321 /** Checks if log closer thread is active.
1322 @return true if and only if the log closer thread is active */
1323 inline bool log_closer_is_active();
1324 
1325 /** Checks if log checkpointer thread is active.
1326 @return true if and only if the log checkpointer thread is active */
1327 inline bool log_checkpointer_is_active();
1328 
1329 /** Writes encryption information to log header.
1330 @param[in,out]  buf          log file header
1331 @param[in]      key          encryption key
1332 @param[in]      iv           encryption iv
1333 @param[in]      is_boot      if it's for bootstrap
1334 @param[in]      encrypt_key  encrypt with master key */
1335 bool log_file_header_fill_encryption(byte *buf, byte *key, byte *iv,
1336                                      bool is_boot, bool encrypt_key);
1337 
1338 /** Disable redo logging and persist the information.
1339 @param[in,out]	log	redo log */
1340 void log_persist_disable(log_t &log);
1341 
1342 /** Enable redo logging and persist the information.
1343 @param[in,out]	log	redo log */
1344 void log_persist_enable(log_t &log);
1345 
1346 /** Persist the information that it is safe to restart server.
1347 @param[in,out]	log	redo log */
1348 void log_persist_crash_safe(log_t &log);
1349 
1350 #else /* !UNIV_HOTBACKUP */
1351 
1352 #ifdef UNIV_DEBUG
1353 
1354 /** Print a log file header.
1355 @param[in]	block	pointer to the log buffer */
1356 void meb_log_print_file_hdr(byte *block);
1357 
1358 #endif /* UNIV_DEBUG */
1359 
1360 #endif /* !UNIV_HOTBACKUP */
1361 
1362 #include "log0log.ic"
1363 
1364 #endif /* !log0log_h */
1365