MySQL 9.1.0
Source Code Documentation
log0chkp.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1995, 2024, 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/** Checks if log checkpointer thread is active.
71@return true if and only if the log checkpointer thread is active */
74}
75
76#define log_checkpointer_mutex_enter(log) \
77 mutex_enter(&((log).checkpointer_mutex))
78
79#define log_checkpointer_mutex_exit(log) mutex_exit(&((log).checkpointer_mutex))
80
81#define log_checkpointer_mutex_own(log) \
82 (mutex_own(&((log).checkpointer_mutex)) || !log_checkpointer_is_active())
83
84/** @} */
85
86/**************************************************/ /**
87
88 @name Log - basic information about checkpoints.
89
90 *******************************************************/
91
92/** @{ */
93
94/** Gets the last checkpoint lsn stored and flushed to disk.
95@return last checkpoint lsn */
97 return log.last_checkpoint_lsn.load();
98}
99
100/** Calculates age of current checkpoint as number of bytes since
101last checkpoint. This includes bytes for headers and footers of
102all log blocks. The calculation is based on the latest written
103checkpoint lsn, and the current lsn, which points to the first
104non reserved data byte. Note that the current lsn could not fit
105the free space in the log files. This means that the checkpoint
106age could potentially be larger than capacity of the log files.
107However we do the best effort to avoid such situations, and if
108they happen, user threads wait until the space is reclaimed.
109@param[in] log redo log
110@return checkpoint age as number of bytes */
112 const lsn_t last_checkpoint_lsn = log.last_checkpoint_lsn.load();
113
114 const lsn_t current_lsn = log_get_lsn(log);
115
116 if (current_lsn <= last_checkpoint_lsn) {
117 /* Writes or reads have been somehow reordered.
118 Note that this function does not provide any lock,
119 and does not assume any lock existing. Therefore
120 the calculated result is already outdated when the
121 function is finished. Hence, we might assume that
122 this time we calculated age = 0, because checkpoint
123 lsn is close to current lsn if such race happened. */
124 return 0;
125 }
126
127 return current_lsn - last_checkpoint_lsn;
128}
129
130/** Provides opposite checkpoint header number to the given checkpoint
131header number.
132@param[in] checkpoint_header_no the given checkpoint header number
133@return the opposite checkpoint header number */
135 Log_checkpoint_header_no checkpoint_header_no);
136
137/** Computes lsn up to which sync flush should be done or returns 0
138if there is no need to execute sync flush now.
139@param[in,out] log redo log
140@return lsn for which we want to have oldest_lsn >= lsn in each BP,
141 or 0 if there is no need for sync flush */
143
144/** @} */
145
146/**************************************************/ /**
147
148 @name Log - requests to make checkpoint.
149
150 *******************************************************/
151
152/** @{ */
153
154/** Requests a fuzzy checkpoint write (for currently available lsn).
155@param[in, out] log redo log
156@param[in] sync whether request is sync (function should wait) */
157void log_request_checkpoint(log_t &log, bool sync);
158
159/** Requests a checkpoint written in the next log file (not in the one,
160to which current log.last_checkpoint_lsn belongs to).
161@param[in,out] log redo log */
163
164/** Requests a checkpoint at the current lsn.
165@param[in,out] log redo log
166@param[out] requested_lsn lsn for which checkpoint was requested, or
167 stays unmodified if it wasn't requested
168@return true iff requested (false if checkpoint_lsn was already at that lsn) */
169bool log_request_latest_checkpoint(log_t &log, lsn_t &requested_lsn);
170
171/** Make a checkpoint at the current lsn. Reads current lsn and waits
172until all dirty pages have been flushed up to that lsn. Afterwards
173requests a checkpoint write and waits until it is finished.
174@param[in,out] log redo log
175@return true iff current lsn was greater than last checkpoint lsn */
177
178/** Make a checkpoint at the current lsn. Reads current lsn and waits
179until all dirty pages have been flushed up to that lsn. Afterwards
180requests a checkpoint write and waits until it is finished.
181@return true iff current lsn was greater than last checkpoint lsn */
183
184/** Updates the field log.dict_max_allowed_checkpoint_lsn. This is limitation
185for lsn at which checkpoint might be written, imposed by cached changes to the
186DD table buffer. It is called from DD code.
187@param[in,out] log redo log
188@param[in] max_lsn new value for the limitation */
190
191/** @} */
192
193/**************************************************/ /**
194
195 @name Log - concurrency margins.
196
197 *******************************************************/
198
199/** @{ */
200
201/** Computes concurrency margin to be used within log_free_check calls,
202for a given redo log capacity (soft_logical_capacity).
203@param[in] log_capacity redo log capacity (soft)
204@param[out] is_safe true iff the computed margin wasn't truncated
205 because of too small log_capacity
206@return the computed margin */
207sn_t log_concurrency_margin(lsn_t log_capacity, bool &is_safe);
208
209/** Updates log.concurrency_margin and log.concurrency_margin_is_safe for the
210current capacity of the redo log and current innodb_thread_concurrency value.
211@param[in,out] log redo log */
213
214/** @} */
215
216/**************************************************/ /**
217
218 @name Log - free check waiting.
219
220 *******************************************************/
221
222/** @{ */
223
224/** Waits until there is free space in log files which includes
225concurrency margin required for all threads. You should rather
226use log_free_check().
227@param[in] log redo log */
228void log_free_check_wait(log_t &log);
229
230/** Provides current margin used in the log_free_check calls.
231It is a sum of dict_persist_margin and concurrency_margin.
232@param[in] log redo log
233@return margin that would be used in log_free_check() */
235
236/** Computes capacity of redo log available until log_free_check()
237needs to wait. It uses a provided size of the log_free_check_margin.
238@param[in] log redo log
239@param[in] free_check_margin size of the log_free_check_margin;
240 @see log_free_check_margin(log)
241@return lsn capacity up to free_check_wait happens */
242lsn_t log_free_check_capacity(const log_t &log, lsn_t free_check_margin);
243
244/** Computes capacity of redo log available until log_free_check()
245needs to wait. It calls log_free_check_margin(log) to obtain the
246current log_free_check_margin.
247@param[in] log redo log
248@return lsn capacity up to free_check_wait happens */
250
251/** Checks if log_free_check() call should better be executed.
252@param[in] log redo log
253@return true iff log_free_check should be executed */
254inline bool log_free_check_is_required(const log_t &log) {
255 if (srv_read_only_mode) {
256 return false;
257 }
258 const lsn_t lsn = log_get_lsn(log);
259 return lsn > log.free_check_limit_lsn.load();
260}
261
262/** Checks if log_free_check() call should better be executed.
263@return true iff log_free_check should be executed */
265 ut_ad(log_sys != nullptr);
267}
268
269#ifdef UNIV_DEBUG
270/** Performs debug checks to validate some of the assumptions. */
272#endif /* UNIV_DEBUG */
273
274/** Reserves free_check_margin in the redo space for the current thread.
275For further details please look at description of @see log_free_check_margin().
276@param[in] log redo log */
277inline void log_free_check(log_t &log) {
279
280 /** We prefer to wait now for the space in log file, because now
281 are not holding any latches of dirty pages. */
282
284 /* We need to wait, because the concurrency margin could be violated
285 if we let all threads to go forward after making this check now.
286
287 The waiting procedure is rather unlikely to happen for proper my.cnf.
288 Therefore we extracted the code to separate function, to make the
289 inlined log_free_check() small. */
290
292 }
293}
294
295/** Checks for free space in the redo log. Must be called when no latches
296are held (except those listed as exceptions). Any database operation must
297call this before it has produced LOG_CHECKPOINT_FREE_PER_THREAD * UNIV_PAGE_SIZE
298bytes of redo log records. That's because that is the margin in redo log we
299reserve by calling this function.
300
301@remarks
302Checks if lsn corresponding to current log.sn exceeds log.free_check_limit_lsn,
303in which case waits (until it does not exceed). This function is called before
304starting a mini-transaction, because thread must not hold block latches when
305calling this function. It is also important that the caller does NOT hold any
306latch, that might be tried to be acquired:
307 - by the page cleaner (e.g. page/block latches),
308 - or by the log flush process (e.g. file space latches),
309 - or by any other thread, which might at that time already hold another
310 latch, that could further lead to a similar problem in chain of threads.
311For example, suppose a thread holding some latch X, which is neither used by
312the page cleaners nor by the log flush process, called log_free_check() and
313started to wait for the free space. Another thread, holding block's latch
314(which obviously might be needed for the page cleaners) tries to acquire the
315latch X. It needs to wait, because X has already been taken. Therefore, the
316latched block cannot be flushed. If this block had old modifications
317(low oldest_modification), it could effectively prevent any further attempts
318to reclaim space in the redo log. The chain of waiting for each other threads
319could obviously be even longer than the one in example. Therefore it is very
320important not to call log_free_check() if we are holding any latchs which
321might exist in any of such chains. As you can see, it is not that easy to see
322if log_free_check() might be called. It is not only about direct holding
323of block latches, but also such X (or Y acquired by thread holding such X),
324could lead to a deadlock.
325
326For sake of simplicity, you should better not keep any latch when calling to
327the log_free_check() unless you are really sure about what you are doing. */
328inline void log_free_check() {
329 ut_ad(log_sys != nullptr);
331}
332
333/** @} */
334
335/**************************************************/ /**
336
337 @name Log - free check updates.
338
339 *******************************************************/
340
341/** @{ */
342
343/** Updates log.free_check_limit_lsn in the log. The log_limits_mutex
344must be acquired before a call (unless srv_is_being_started is true).
345@param[in,out] log redo log */
347
348/** Updates log.dict_persist_margin and recompute free check limit.
349@param[in,out] log redo log
350@param[in] margin new value for log.dict_persist_margin */
351void log_set_dict_persist_margin(log_t &log, sn_t margin);
352
353/** @} */
354
355/**************************************************/ /**
356
357 @name Log - other functions related to checkpoints.
358
359 *******************************************************/
360
361/** @{ */
362
363/** Writes checkpoint to the file containing the written checkpoint_lsn.
364The checkpoint is written to the given checkpoint header. Unless InnoDB
365is starting: checkpointer, writer and files mutexes must be acquired
366before calling this function.
367@param[in,out] log redo log
368@param[in] checkpoint_file_handle handle to opened file
369@param[in] checkpoint_header_no checkpoint header to be written
370@param[in] next_checkpoint_lsn the checkpoint lsn to write
371@return DB_SUCCESS or error */
373 log_t &log, Log_file_handle &checkpoint_file_handle,
374 Log_checkpoint_header_no checkpoint_header_no, lsn_t next_checkpoint_lsn);
375
376/** Writes the first data block to the log file using the provided handle
377to the opened log file. The block is addressed by the given checkpoint_lsn,
378filled with 0x00 and its data length points to checkpoint_lsn inside, making
379the block logically empty.
380@remarks This is used only during creation of new log files.
381@param[in,out] log redo log
382@param[in] file_handle handle to the opened log file
383@param[in] checkpoint_lsn the checkpoint lsn
384@param[in] file_start_lsn start_lsn of the file
385@return DB_SUCCESS or error */
387 Log_file_handle &file_handle,
388 lsn_t checkpoint_lsn,
389 lsn_t file_start_lsn);
390
391/** Writes the next checkpoint to the log file, by writing a single
392checkpoint header with the checkpoint lsn. Flushes the file after the
393write and updates the log.last_checkpoint_lsn.
394
395@remarks Note that two checkpoint headers are used alternately for
396consecutive checkpoints. If InnoDB crashed during the write, it would
397still have the previous checkpoint info and recovery would work.
398@param[in,out] log redo log
399@param[in] lsn writes checkpoint at this lsn
400@return DB_SUCCESS or error */
402
403/** @} */
404
405#endif /* !UNIV_HOTBACKUP */
406
407#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:111
bool log_request_latest_checkpoint(log_t &log, lsn_t &requested_lsn)
Requests a checkpoint at the current lsn.
Definition: log0chkp.cc:666
void log_free_check_validate()
Performs debug checks to validate some of the assumptions.
Definition: log0chkp.cc:1254
bool log_free_check_is_required(const log_t &log)
Checks if log_free_check() call should better be executed.
Definition: log0chkp.h:254
lsn_t log_get_checkpoint_lsn(const log_t &log)
Gets the last checkpoint lsn stored and flushed to disk.
Definition: log0chkp.h:96
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:1160
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:519
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:1209
void log_request_checkpoint_in_next_file(log_t &log)
Requests a checkpoint written in the next log file (not in the one, to which current log....
Definition: log0chkp.cc:660
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:1221
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:323
lsn_t log_free_check_margin(const log_t &log)
Provides current margin used in the log_free_check calls.
Definition: log0chkp.cc:1201
void log_free_check(log_t &log)
Reserves free_check_margin in the redo space for the current thread.
Definition: log0chkp.h:277
bool log_checkpointer_is_active()
Checks if log checkpointer thread is active.
Definition: log0chkp.h:72
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:1085
void log_update_limits_low(log_t &log)
Updates log.free_check_limit_lsn in the log.
Definition: log0chkp.cc:1174
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:350
bool log_make_latest_checkpoint(log_t &log)
Make a checkpoint at the current lsn.
Definition: log0chkp.cc:689
void log_checkpointer(log_t *log_ptr)
The log checkpointer thread routine.
Definition: log0chkp.cc:977
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:1194
void log_request_checkpoint(log_t &log, bool sync)
Requests a fuzzy checkpoint write (for currently available lsn).
Definition: log0chkp.cc:594
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:440
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:428
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:761
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:431
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:3261
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:661
atomic_lsn_t last_checkpoint_lsn
Latest checkpoint lsn.
Definition: log0sys.h:701
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