MySQL 26.10.0
Source Code Documentation
log0log.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1995, 2026, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/**************************************************/ /**
29 @file include/log0log.h
30
31 Redo log - the main header.
32
33 Basic types are defined inside log0types.h.
34
35 Constant values are defined inside log0constants.h, but that
36 file should only be included by log0types.h.
37
38 The log_sys is defined in log0sys.h.
39
40 Functions related to the log buffer are declared in log0buf.h.
41
42 Functions related to the checkpoints are declared in log0chkp.h.
43
44 Functions related to the writer/flusher are declared in log0write.h.
45
46 Functions computing capacity of redo and related margins are declared
47 in log0files_capacity.h.
48
49 Functions doing IO to log files and formatting log blocks are declared
50 in log0files_io.h.
51
52 *******************************************************/
53
54#ifndef log0log_h
55#define log0log_h
56
57#include "log0files_capacity.h"
58#include "log0files_dict.h"
59#include "log0files_finder.h"
60#include "log0files_governor.h"
61#include "log0files_io.h"
63#include "log0sys.h"
64#include "log0types.h"
65
66/**************************************************/ /**
67
68 @name Log - LSN computations.
69
70 *******************************************************/
71
72/** @{ */
73
74/** Calculates lsn value for given sn value. Sequence of sn values
75enumerate all data bytes in the redo log. Sequence of lsn values
76enumerate all data bytes and bytes used for headers and footers
77of all log blocks in the redo log. For every LOG_BLOCK_DATA_SIZE
78bytes of data we have OS_FILE_LOG_BLOCK_SIZE bytes in the redo log.
79NOTE that LOG_BLOCK_DATA_SIZE + LOG_BLOCK_HDR_SIZE + LOG_BLOCK_TRL_SIZE
80== OS_FILE_LOG_BLOCK_SIZE. The calculated lsn value will always point
81to some data byte (will be % OS_FILE_LOG_BLOCK_SIZE >= LOG_BLOCK_HDR_SIZE,
82and < OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE).
83
84@param[in] sn sn value
85@return lsn value for the provided sn value */
86constexpr inline lsn_t log_translate_sn_to_lsn(sn_t sn) {
89}
90
91/** Calculates sn value for given lsn value.
92@see log_translate_sn_to_lsn
93@param[in] lsn lsn value
94@return sn value for the provided lsn value */
96 /* Calculate sn of the beginning of log block, which contains
97 the provided lsn value. */
99
100 /* Calculate offset for the provided lsn within the log block.
101 The offset includes LOG_BLOCK_HDR_SIZE bytes of block's header. */
102 const uint32_t diff = lsn % OS_FILE_LOG_BLOCK_SIZE;
103
104 if (diff < LOG_BLOCK_HDR_SIZE) {
105 /* The lsn points to some bytes inside the block's header.
106 Return sn for the beginning of the block. Note, that sn
107 values don't enumerate bytes of blocks' headers, so the
108 value of diff does not matter at all. */
109 return sn;
110 }
111
113 /* The lsn points to some bytes inside the block's footer.
114 Return sn for the beginning of the next block. Note, that
115 sn values don't enumerate bytes of blocks' footer, so the
116 value of diff does not matter at all. */
117 return sn + LOG_BLOCK_DATA_SIZE;
118 }
119
120 /* Add the offset but skip bytes of block's header. */
121 return sn + diff - LOG_BLOCK_HDR_SIZE;
122}
123
124/** Validates a given lsn value. Checks if the lsn value points to data
125bytes inside log block (not to some bytes in header/footer). It is used
126by assertions.
127@return true if lsn points to data bytes within log block */
129 return lsn >= LOG_START_LSN &&
131}
132
133/** @} */
134
135#ifndef UNIV_HOTBACKUP
136
137/**************************************************/ /**
138
139 @name Log - general functions.
140
141 *******************************************************/
142
143/** @{ */
144
145/** @return consistent sn value for locked state */
146static inline sn_t log_get_sn(const log_t &log) {
147 const sn_t sn = log.sn.load();
148 if ((sn & SN_LOCKED) != 0) {
149 return log.sn_locked.load();
150 } else {
151 return sn;
152 }
153}
154
155/** Gets the current lsn value. This value points to the first non
156reserved data byte in the redo log. When next user thread reserves
157space in the redo log, it starts at this lsn.
158
159If the last reservation finished exactly before footer of log block,
160this value points to the first byte after header of the next block.
161
162@note It is possible that the current lsn value does not fit free
163space in the log files or in the log buffer. In such case, user
164threads need to wait until the space becomes available.
165
166@return current lsn */
167inline lsn_t log_get_lsn(const log_t &log) {
169}
170
171/** Waits until there is free space for range of sn values ending
172at the provided sn, in both the log buffer and in the log files.
173@param[in] log redo log
174@param[in] end_sn end of the range of sn values */
175void log_wait_for_space(log_t &log, sn_t end_sn);
176
177/** Prints information about important lsn values used in the redo log,
178and some statistics about speed of writing and flushing of data.
179@param[in] log redo log for which print information
180@param[out] file file where to print */
181void log_print(const log_t &log, FILE *file);
182
183/** Refreshes the statistics used to print per-second averages in log_print().
184@param[in,out] log redo log */
185void log_refresh_stats(log_t &log);
186
187void log_update_exported_variables(const log_t &log);
188
189/** @} */
190
191/**************************************************/ /**
192
193 @name Log - initialization of the redo log system.
194
195 *******************************************************/
196
197/** @{ */
198
199/** Initializes log_sys and finds existing redo log files.
200It may also remove existing files if they are not marked as fully initialized
201(flag LOG_HEADER_FLAG_NOT_INITIALIZED exists in the newest file) - in such case
202this function will return the same DB_CANNOT_OPEN_FILE as if it found no files.
203
204After this call, the log_sys global variable is allocated and initialized.
205InnoDB might start recovery then.
206
207@remarks
208The redo log files are not resized in this function, because before resizing
209log files, InnoDB must run recovery and ensure log files are logically empty.
210
211
212@note Note that the redo log system is NOT ready for user writes after this
213call is finished. The proper order of calls looks like this:
214 - log_sys_init(),
215 - ib::redo::Handler::start_writing()
216and this sequence is executed inside srv_start() in srv0start.cc (interleaved
217with remaining logic of the srv_start())
218
219@note Note this function also verifies that REDO logs are in known format.
220
221@param[in] expect_no_files true means we should return DB_ERROR if log
222 files are present in the directory before
223 proceeding any further
224@return DB_SUCCESS or error */
225[[nodiscard]] dberr_t log_sys_init(bool expect_no_files);
226
227/** Close the log system and free all the related memory. */
228void log_sys_close();
229
230/** Resizes the write ahead buffer in the redo log.
231@param[in,out] log redo log
232@param[in] new_size new size (in bytes) */
233void log_write_ahead_resize(log_t &log, size_t new_size);
234
235/** @} */
236
237/**************************************************/ /**
238
239 @name Log - the log threads and mutexes
240
241 *******************************************************/
242
243/** @{ */
244
245/** Validates that all the log background threads are active.
246Used only to assert, that the state is correct. */
248
249/** Validates that all the log background threads are inactive.
250Used only to assert, that the state is correct. */
252
253/** Starts all the log background threads. This can be called only,
254when the threads are inactive. This should never be called concurrently.
255This may not be called during read-only mode.
256@param[in,out] log redo log */
258
259/** Stops all the log background threads. This can be called only,
260when the threads are active. This should never be called concurrently.
261This may not be called in read-only mode. Note that is is impossible
262to start log background threads in such case.
263@param[in,out] log redo log */
265
266/** Marks the flag which tells log threads to stop and wakes them.
267Does not wait until they are stopped.
268@param[in,out] log redo log */
270
271/** Wakes up all log threads which are alive.
272@param[in,out] log redo log */
273void log_wake_threads(log_t &log);
274
275/** @} */
276
277/**************************************************/ /**
278
279 @name Log - the log position locking.
280
281 *******************************************************/
282
283/** @{ */
284
285/** Lock redo log. Both current lsn and checkpoint lsn will not change
286until the redo log is unlocked.
287@param[in,out] log redo log to lock */
288void log_position_lock(log_t &log);
289
290/** Unlock the locked redo log.
291@param[in,out] log redo log to unlock */
292void log_position_unlock(log_t &log);
293
294/** Collect coordinates in the locked redo log.
295@param[in] log locked redo log
296@param[out] current_lsn stores current lsn there
297@param[out] checkpoint_lsn stores checkpoint lsn there */
298void log_position_collect_lsn_info(const log_t &log, lsn_t *current_lsn,
299 lsn_t *checkpoint_lsn);
300
301/** @} */
302
303/**************************************************/ /**
304
305 @name Log - persisting the flags.
306
307 *******************************************************/
308
309/** @{ */
310
311/** Disable redo logging and persist the information.
312@param[in,out] log redo log */
313void log_persist_disable(log_t &log);
314
315/** Enable redo logging and persist the information.
316@param[in,out] log redo log */
317void log_persist_enable(log_t &log);
318
319/** Persist the information that it is safe to restart server.
320@param[in,out] log redo log */
322
323/** Marks the redo log files as belonging to the initialized data directory
324with initialized set of redo log files. Flushes the log_flags without the
325flag LOG_HEADER_FLAG_NOT_INITIALIZED to the newest redo log file.
326@param[in,out] log redo log */
328
329/** Asserts that the log is not marked as crash-unsafe.
330@param[in,out] log redo log */
332
333/** @} */
334
335#endif /* !UNIV_HOTBACKUP */
336
337#endif /* !log0log_h */
virtual Lsn compute_end_lsn(Lsn start_lsn, size_t data_len) const =0
Returns the lsn at the requested position that is the end of the MTR data.
dberr_t
Definition: db0err.h:39
static Bigint * diff(Bigint *a, Bigint *b, Stack_alloc *alloc)
Definition: dtoa.cc:1081
constexpr sn_t SN_LOCKED
The sn bit to express locked state.
Definition: log0constants.h:162
constexpr lsn_t LOG_START_LSN
The counting of lsn's starts from this value: this must be non-zero.
Definition: log0constants.h:153
constexpr uint32_t LOG_BLOCK_HDR_SIZE
Size of the log block's header in bytes.
Definition: log0constants.h:297
constexpr uint32_t LOG_BLOCK_DATA_SIZE
Size of log block's data fragment (where actual data is stored).
Definition: log0constants.h:312
constexpr uint32_t LOG_BLOCK_TRL_SIZE
Size of the log block footer (trailer) in bytes.
Definition: log0constants.h:306
Redo log management of capacity.
In-memory dictionary of log files (keeps their meta data).
Redo log management of log files.
The log0files_io.
void log_update_exported_variables(const log_t &log)
Definition: log0log.cc:1054
void log_position_collect_lsn_info(const log_t &log, lsn_t *current_lsn, lsn_t *checkpoint_lsn)
Collect coordinates in the locked redo log.
Definition: log0log.cc:1299
sn_t log_translate_lsn_to_sn(lsn_t lsn)
Calculates sn value for given lsn value.
Definition: log0log.h:95
void log_persist_crash_safe(log_t &log)
Persist the information that it is safe to restart server.
Definition: log0log.cc:1361
void log_background_threads_inactive_validate()
Validates that all the log background threads are inactive.
Definition: log0log.cc:752
void log_persist_enable(log_t &log)
Enable redo logging and persist the information.
Definition: log0log.cc:1347
void log_crash_safe_validate(log_t &log)
Asserts that the log is not marked as crash-unsafe.
Definition: log0log.cc:1376
void log_wait_for_space(log_t &log, sn_t end_sn)
Waits until there is free space for range of sn values ending at the provided sn, in both the log buf...
dberr_t log_sys_init(bool expect_no_files)
Initializes log_sys and finds existing redo log files.
Definition: log0log.cc:1494
bool log_is_data_lsn(lsn_t lsn)
Validates a given lsn value.
Definition: log0log.h:128
void log_write_ahead_resize(log_t &log, size_t new_size)
Resizes the write ahead buffer in the redo log.
Definition: log0log.cc:1140
void log_position_lock(log_t &log)
Lock redo log.
Definition: log0log.cc:1285
void log_stop_background_threads(log_t &log)
Stops all the log background threads.
Definition: log0log.cc:803
void log_position_unlock(log_t &log)
Unlock the locked redo log.
Definition: log0log.cc:1292
void log_print(const log_t &log, FILE *file)
Prints information about important lsn values used in the redo log, and some statistics about speed o...
Definition: log0log.cc:961
lsn_t log_get_lsn(const log_t &log)
Gets the current lsn value.
Definition: log0log.h:167
void log_persist_disable(log_t &log)
Disable redo logging and persist the information.
Definition: log0log.cc:1354
static sn_t log_get_sn(const log_t &log)
Definition: log0log.h:146
void log_wake_threads(log_t &log)
Wakes up all log threads which are alive.
Definition: log0log.cc:861
void log_background_threads_active_validate()
Validates that all the log background threads are active.
Definition: log0log.cc:744
void log_sys_close()
Close the log system and free all the related memory.
Definition: log0log.cc:1704
void log_persist_initialized(log_t &log)
Marks the redo log files as belonging to the initialized data directory with initialized set of redo ...
Definition: log0log.cc:1368
void log_start_background_threads(log_t &log)
Starts all the log background threads.
Definition: log0log.cc:760
constexpr lsn_t log_translate_sn_to_lsn(sn_t sn)
Calculates lsn value for given sn value.
Definition: log0log.h:86
void log_stop_background_threads_nowait(log_t &log)
Marks the flag which tells log threads to stop and wakes them.
Definition: log0log.cc:854
void log_refresh_stats(log_t &log)
Refreshes the statistics used to print per-second averages in log_print().
Definition: log0log.cc:1049
Redo log - the log_sys.
Redo log basic types.
uint64_t sn_t
Type used for sn values, which enumerate bytes of data stored in the log.
Definition: log0types.h:86
uint64_t lsn_t
Type used for all log sequence number storage and arithmetic.
Definition: log0types.h:63
const std::string FILE("FILE")
Definition: os0file.h:89
Handler_interface * handler
Definition: log0log.cc:434
constexpr uint32_t OS_FILE_LOG_BLOCK_SIZE
The next value should be smaller or equal to the smallest sector size used on any disk.
Definition: os0file.h:192
Redo log - single data structure with state of the redo log system.
Definition: log0sys.h:77
atomic_sn_t sn
Current sn value.
Definition: log0sys.h:103
atomic_sn_t sn_locked
Intended sn value while x-locked.
Definition: log0sys.h:106
static uint64_t lsn
Definition: xcom_base.cc:446