MySQL 8.4.6
Source Code Documentation
log0chkp.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1995, 2025, 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/log0chkp.h
30
31 Redo log functions related to checkpointing and log free check.
32
33 *******************************************************/
34
35#ifndef log0chkp_h
36#define log0chkp_h
37
38#ifndef UNIV_HOTBACKUP
39
40/* log_get_sn */
41#include "log0log.h"
42
43/* log.last_checkpoint_lsn */
44#include "log0sys.h"
45
46/* log_t&, lsn_t */
47#include "log0types.h"
48
49/* srv_threads.*, srv_read_only_mode, ... */
50#include "srv0srv.h"
51
52/* sync_check_iterate */
53#include "sync0debug.h"
54
55/* sync_allowed_latches, latch_level_t */
56#include "sync0types.h"
57
58/**************************************************/ /**
59
60 @name Log - checkpointer thread and checkpointer mutex.
61
62 *******************************************************/
63
64/** @{ */
65
66/** The log checkpointer thread routine.
67@param[in,out] log_ptr pointer to redo log */
68void log_checkpointer(log_t *log_ptr);
69
70/** Requests a checkpoint written for lsn greater or equal to provided one.
71The log.limits_mutex has to be acquired before it is called, and it is not
72released within this function.
73@param[in,out] log redo log
74@param[in] requested_lsn provided lsn (checkpoint should be not older) */
75void log_request_checkpoint_low(log_t &log, lsn_t requested_lsn);
76
77/** Checks if log checkpointer thread is active.
78@return true if and only if the log checkpointer thread is active */
81}
82
83#define log_checkpointer_mutex_enter(log) \
84 mutex_enter(&((log).checkpointer_mutex))
85
86#define log_checkpointer_mutex_exit(log) mutex_exit(&((log).checkpointer_mutex))
87
88#define log_checkpointer_mutex_own(log) \
89 (mutex_own(&((log).checkpointer_mutex)) || !log_checkpointer_is_active())
90
91/** @} */
92
93/**************************************************/ /**
94
95 @name Log - basic information about checkpoints.
96
97 *******************************************************/
98
99/** @{ */
100
101/** Gets the last checkpoint lsn stored and flushed to disk.
102@return last checkpoint lsn */
104 return log.last_checkpoint_lsn.load();
105}
106
107/** Calculates age of current checkpoint as number of bytes since
108last checkpoint. This includes bytes for headers and footers of
109all log blocks. The calculation is based on the latest written
110checkpoint lsn, and the current lsn, which points to the first
111non reserved data byte. Note that the current lsn could not fit
112the free space in the log files. This means that the checkpoint
113age could potentially be larger than capacity of the log files.
114However we do the best effort to avoid such situations, and if
115they happen, user threads wait until the space is reclaimed.
116@param[in] log redo log
117@return checkpoint age as number of bytes */
119 const lsn_t last_checkpoint_lsn = log.last_checkpoint_lsn.load();
120
121 const lsn_t current_lsn = log_get_lsn(log);
122
123 if (current_lsn <= last_checkpoint_lsn) {
124 /* Writes or reads have been somehow reordered.
125 Note that this function does not provide any lock,
126 and does not assume any lock existing. Therefore
127 the calculated result is already outdated when the
128 function is finished. Hence, we might assume that
129 this time we calculated age = 0, because checkpoint
130 lsn is close to current lsn if such race happened. */
131 return 0;
132 }
133
134 return current_lsn - last_checkpoint_lsn;
135}
136
137/** Provides opposite checkpoint header number to the given checkpoint
138header number.
139@param[in] checkpoint_header_no the given checkpoint header number
140@return the opposite checkpoint header number */
142 Log_checkpoint_header_no checkpoint_header_no);
143
144/** Computes lsn up to which sync flush should be done or returns 0
145if there is no need to execute sync flush now.
146@param[in,out] log redo log
147@return lsn for which we want to have oldest_lsn >= lsn in each BP,
148 or 0 if there is no need for sync flush */
150
151/** @} */
152
153/**************************************************/ /**
154
155 @name Log - requests to make checkpoint.
156
157 *******************************************************/
158
159/** @{ */
160
161/** Requests a fuzzy checkpoint write (for currently available lsn).
162@param[in, out] log redo log
163@param[in] sync whether request is sync (function should wait) */
164void log_request_checkpoint(log_t &log, bool sync);
165
166/** Requests a checkpoint at the current lsn.
167@param[in,out] log redo log
168@param[out] requested_lsn lsn for which checkpoint was requested, or
169 stays unmodified if it wasn't requested
170@return true iff requested (false if checkpoint_lsn was already at that lsn) */
171bool log_request_latest_checkpoint(log_t &log, lsn_t &requested_lsn);
172
173/** Make a checkpoint at the current lsn. Reads current lsn and waits
174until all dirty pages have been flushed up to that lsn. Afterwards
175requests a checkpoint write and waits until it is finished.
176@param[in,out] log redo log
177@return true iff current lsn was greater than last checkpoint lsn */
179
180/** Make a checkpoint at the current lsn. Reads current lsn and waits
181until all dirty pages have been flushed up to that lsn. Afterwards
182requests a checkpoint write and waits until it is finished.
183@return true iff current lsn was greater than last checkpoint lsn */
185
186/** Updates the field log.dict_max_allowed_checkpoint_lsn. This is limitation
187for lsn at which checkpoint might be written, imposed by cached changes to the
188DD table buffer. It is called from DD code.
189@param[in,out] log redo log
190@param[in] max_lsn new value for the limitation */
192
193/** @} */
194
195/**************************************************/ /**
196
197 @name Log - concurrency margins.
198
199 *******************************************************/
200
201/** @{ */
202
203/** Computes concurrency margin to be used within log_free_check calls,
204for a given redo log capacity (soft_logical_capacity).
205@param[in] log_capacity redo log capacity (soft)
206@param[out] is_safe true iff the computed margin wasn't truncated
207 because of too small log_capacity
208@return the computed margin */
209sn_t log_concurrency_margin(lsn_t log_capacity, bool &is_safe);
210
211/** Updates log.concurrency_margin and log.concurrency_margin_is_safe for the
212current capacity of the redo log and current innodb_thread_concurrency value.
213@param[in,out] log redo log */
215
216/** @} */
217
218/**************************************************/ /**
219
220 @name Log - free check waiting.
221
222 *******************************************************/
223
224/** @{ */
225
226/** Waits until there is free space in log files which includes
227concurrency margin required for all threads. You should rather
228use log_free_check().
229@param[in] log redo log */
230void log_free_check_wait(log_t &log);
231
232/** Provides current margin used in the log_free_check calls.
233It is a sum of dict_persist_margin and concurrency_margin.
234@param[in] log redo log
235@return margin that would be used in log_free_check() */
237
238/** Computes capacity of redo log available until log_free_check()
239needs to wait. It uses a provided size of the log_free_check_margin.
240@param[in] log redo log
241@param[in] free_check_margin size of the log_free_check_margin;
242 @see log_free_check_margin(log)
243@return lsn capacity up to free_check_wait happens */
244lsn_t log_free_check_capacity(const log_t &log, lsn_t free_check_margin);
245
246/** Computes capacity of redo log available until log_free_check()
247needs to wait. It calls log_free_check_margin(log) to obtain the
248current log_free_check_margin.
249@param[in] log redo log
250@return lsn capacity up to free_check_wait happens */
252
253/** Checks if log_free_check() call should better be executed.
254@param[in] log redo log
255@return true iff log_free_check should be executed */
256inline bool log_free_check_is_required(const log_t &log) {
257 if (srv_read_only_mode) {
258 return false;
259 }
260 const lsn_t lsn = log_get_lsn(log);
261 return lsn > log.free_check_limit_lsn.load();
262}
263
264/** Checks if log_free_check() call should better be executed.
265@return true iff log_free_check should be executed */
267 ut_ad(log_sys != nullptr);
269}
270
271#ifdef UNIV_DEBUG
272/** Performs debug checks to validate some of the assumptions. */
274#endif /* UNIV_DEBUG */
275
276/** Reserves free_check_margin in the redo space for the current thread.
277For further details please look at description of @see log_free_check_margin().
278@param[in] log redo log */
279inline void log_free_check(log_t &log) {
281
282 /** We prefer to wait now for the space in log file, because now
283 are not holding any latches of dirty pages. */
284
286 /* We need to wait, because the concurrency margin could be violated
287 if we let all threads to go forward after making this check now.
288
289 The waiting procedure is rather unlikely to happen for proper my.cnf.
290 Therefore we extracted the code to separate function, to make the
291 inlined log_free_check() small. */
292
294 }
295}
296
297/** Checks for free space in the redo log. Must be called when no latches
298are held (except those listed as exceptions). Any database operation must
299call this before it has produced LOG_CHECKPOINT_FREE_PER_THREAD * UNIV_PAGE_SIZE
300bytes of redo log records. That's because that is the margin in redo log we
301reserve by calling this function.
302
303@remarks
304Checks if lsn corresponding to current log.sn exceeds log.free_check_limit_lsn,
305in which case waits (until it does not exceed). This function is called before
306starting a mini-transaction, because thread must not hold block latches when
307calling this function. It is also important that the caller does NOT hold any
308latch, that might be tried to be acquired:
309 - by the page cleaner (e.g. page/block latches),
310 - or by the log flush process (e.g. file space latches),
311 - or by any other thread, which might at that time already hold another
312 latch, that could further lead to a similar problem in chain of threads.
313For example, suppose a thread holding some latch X, which is neither used by
314the page cleaners nor by the log flush process, called log_free_check() and
315started to wait for the free space. Another thread, holding block's latch
316(which obviously might be needed for the page cleaners) tries to acquire the
317latch X. It needs to wait, because X has already been taken. Therefore, the
318latched block cannot be flushed. If this block had old modifications
319(low oldest_modification), it could effectively prevent any further attempts
320to reclaim space in the redo log. The chain of waiting for each other threads
321could obviously be even longer than the one in example. Therefore it is very
322important not to call log_free_check() if we are holding any latchs which
323might exist in any of such chains. As you can see, it is not that easy to see
324if log_free_check() might be called. It is not only about direct holding
325of block latches, but also such X (or Y acquired by thread holding such X),
326could lead to a deadlock.
327
328For sake of simplicity, you should better not keep any latch when calling to
329the log_free_check() unless you are really sure about what you are doing. */
330inline void log_free_check() {
331 ut_ad(log_sys != nullptr);
333}
334
335/** @} */
336
337/**************************************************/ /**
338
339 @name Log - free check updates.
340
341 *******************************************************/
342
343/** @{ */
344
345/** Updates log.free_check_limit_lsn in the log. The log_files_mutex and
346log_limits_mutex must be acquired before a call (unless srv_is_being_started is
347true).
348@param[in,out] log redo log */
350
351/** Updates log.dict_persist_margin and recompute free check limit.
352@param[in,out] log redo log
353@param[in] margin new value for log.dict_persist_margin */
354void log_set_dict_persist_margin(log_t &log, sn_t margin);
355
356/** @} */
357
358/**************************************************/ /**
359
360 @name Log - other functions related to checkpoints.
361
362 *******************************************************/
363
364/** @{ */
365
366/** Writes checkpoint to the file containing the written checkpoint_lsn.
367The checkpoint is written to the given checkpoint header. Unless InnoDB
368is starting: checkpointer, writer and files mutexes must be acquired
369before calling this function.
370@param[in,out] log redo log
371@param[in] checkpoint_file_handle handle to opened file
372@param[in] checkpoint_header_no checkpoint header to be written
373@param[in] next_checkpoint_lsn the checkpoint lsn to write
374@return DB_SUCCESS or error */
376 log_t &log, Log_file_handle &checkpoint_file_handle,
377 Log_checkpoint_header_no checkpoint_header_no, lsn_t next_checkpoint_lsn);
378
379/** Writes the first data block to the log file using the provided handle
380to the opened log file. The block is addressed by the given checkpoint_lsn,
381filled with 0x00 and its data length points to checkpoint_lsn inside, making
382the block logically empty.
383@remarks This is used only during creation of new log files.
384@param[in,out] log redo log
385@param[in] file_handle handle to the opened log file
386@param[in] checkpoint_lsn the checkpoint lsn
387@param[in] file_start_lsn start_lsn of the file
388@return DB_SUCCESS or error */
390 Log_file_handle &file_handle,
391 lsn_t checkpoint_lsn,
392 lsn_t file_start_lsn);
393
394/** Writes the next checkpoint to the log file, by writing a single
395checkpoint header with the checkpoint lsn. Flushes the file after the
396write and updates the log.last_checkpoint_lsn.
397
398@remarks Note that two checkpoint headers are used alternately for
399consecutive checkpoints. If InnoDB crashed during the write, it would
400still have the previous checkpoint info and recovery would work.
401@param[in,out] log redo log
402@param[in] lsn writes checkpoint at this lsn
403@return DB_SUCCESS or error */
405
406/** @} */
407
408#endif /* !UNIV_HOTBACKUP */
409
410#endif /* !log0chkp_h */
Handle which allows to do reads / writes for the opened file.
Definition: log0types.h:309
dberr_t
Definition: db0err.h:39
lsn_t log_get_checkpoint_age(const log_t &log)
Calculates age of current checkpoint as number of bytes since last checkpoint.
Definition: log0chkp.h:118
bool log_request_latest_checkpoint(log_t &log, lsn_t &requested_lsn)
Requests a checkpoint at the current lsn.
Definition: log0chkp.cc:604
void log_request_checkpoint_low(log_t &log, lsn_t requested_lsn)
Requests a checkpoint written for lsn greater or equal to provided one.
Definition: log0chkp.cc:535
void log_free_check_validate()
Performs debug checks to validate some of the assumptions.
Definition: log0chkp.cc:1201
bool log_free_check_is_required(const log_t &log)
Checks if log_free_check() call should better be executed.
Definition: log0chkp.h:256
lsn_t log_get_checkpoint_lsn(const log_t &log)
Gets the last checkpoint lsn stored and flushed to disk.
Definition: log0chkp.h:103
void log_update_concurrency_margin(log_t &log)
Updates log.concurrency_margin and log.concurrency_margin_is_safe for the current capacity of the red...
Definition: log0chkp.cc:1098
dberr_t log_files_write_first_data_block_low(log_t &log, Log_file_handle &file_handle, lsn_t checkpoint_lsn, lsn_t file_start_lsn)
Writes the first data block to the log file using the provided handle to the opened log file.
Definition: log0chkp.cc:505
lsn_t log_free_check_capacity(const log_t &log, lsn_t free_check_margin)
Computes capacity of redo log available until log_free_check() needs to wait.
Definition: log0chkp.cc:1164
void log_free_check_wait(log_t &log)
Waits until there is free space in log files which includes concurrency margin required for all threa...
Definition: log0chkp.cc:1176
void log_set_dict_max_allowed_checkpoint_lsn(log_t &log, lsn_t max_lsn)
Updates the field log.dict_max_allowed_checkpoint_lsn.
Definition: log0chkp.cc:309
lsn_t log_free_check_margin(const log_t &log)
Provides current margin used in the log_free_check calls.
Definition: log0chkp.cc:1156
void log_free_check(log_t &log)
Reserves free_check_margin in the redo space for the current thread.
Definition: log0chkp.h:279
bool log_checkpointer_is_active()
Checks if log checkpointer thread is active.
Definition: log0chkp.h:79
sn_t log_concurrency_margin(lsn_t log_capacity, bool &is_safe)
Computes concurrency margin to be used within log_free_check calls, for a given redo log capacity (so...
Definition: log0chkp.cc:1023
void log_update_limits_low(log_t &log)
Updates log.free_check_limit_lsn in the log.
Definition: log0chkp.cc:1112
dberr_t log_files_next_checkpoint(log_t &log, lsn_t lsn)
Writes the next checkpoint to the log file, by writing a single checkpoint header with the checkpoint...
Definition: log0chkp.cc:336
bool log_make_latest_checkpoint(log_t &log)
Make a checkpoint at the current lsn.
Definition: log0chkp.cc:627
void log_checkpointer(log_t *log_ptr)
The log checkpointer thread routine.
Definition: log0chkp.cc:915
void log_set_dict_persist_margin(log_t &log, sn_t margin)
Updates log.dict_persist_margin and recompute free check limit.
Definition: log0chkp.cc:1147
void log_request_checkpoint(log_t &log, bool sync)
Requests a fuzzy checkpoint write (for currently available lsn).
Definition: log0chkp.cc:580
dberr_t log_files_write_checkpoint_low(log_t &log, Log_file_handle &checkpoint_file_handle, Log_checkpoint_header_no checkpoint_header_no, lsn_t next_checkpoint_lsn)
Writes checkpoint to the file containing the written checkpoint_lsn.
Definition: log0chkp.cc:426
Log_checkpoint_header_no log_next_checkpoint_header(Log_checkpoint_header_no checkpoint_header_no)
Provides opposite checkpoint header number to the given checkpoint header number.
Definition: log0chkp.cc:414
lsn_t log_sync_flush_lsn(log_t &log)
Computes lsn up to which sync flush should be done or returns 0 if there is no need to execute sync f...
Definition: log0chkp.cc:699
Redo log - the main header.
lsn_t log_get_lsn(const log_t &log)
Gets the current lsn value.
Definition: log0log.h:168
Redo log - the log_sys.
log_t * log_sys
Redo log system (singleton).
Definition: log0log.cc:435
Redo log basic types.
Log_checkpoint_header_no
Enumerates checkpoint headers in the redo log file.
Definition: log0types.h:95
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
The server main program.
bool srv_thread_is_active(const IB_thread &thread)
Check if given thread is still active.
Definition: srv0srv.cc:3255
bool srv_read_only_mode
Set if InnoDB must operate in read-only mode.
Definition: srv0srv.cc:200
Srv_threads srv_threads
Structure with state of srv background threads.
Definition: srv0srv.cc:104
IB_thread m_log_checkpointer
Redo checkpointer thread.
Definition: srv0srv.h:176
Redo log - single data structure with state of the redo log system.
Definition: log0sys.h:77
atomic_lsn_t free_check_limit_lsn
Maximum lsn up to which there is free space in the redo log.
Definition: log0sys.h:660
atomic_lsn_t last_checkpoint_lsn
Latest checkpoint lsn.
Definition: log0sys.h:700
Debug checks for latches, header file.
Global types for sync.
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:105
#define ut_d(EXPR)
Debug statement.
Definition: ut0dbg.h:107
static uint64_t lsn
Definition: xcom_base.cc:446