MySQL 8.3.0
Source Code Documentation
log0chkp.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1995, 2023, 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 also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/**************************************************/ /**
28 @file include/log0chkp.h
29
30 Redo log functions related to checkpointing and log free check.
31
32 *******************************************************/
33
34#ifndef log0chkp_h
35#define log0chkp_h
36
37#ifndef UNIV_HOTBACKUP
38
39/* log_get_sn */
40#include "log0log.h"
41
42/* log.last_checkpoint_lsn */
43#include "log0sys.h"
44
45/* log_t&, lsn_t */
46#include "log0types.h"
47
48/* srv_threads.*, srv_read_only_mode, ... */
49#include "srv0srv.h"
50
51/* sync_check_iterate */
52#include "sync0debug.h"
53
54/* sync_allowed_latches, latch_level_t */
55#include "sync0types.h"
56
57/**************************************************/ /**
58
59 @name Log - checkpointer thread and checkpointer mutex.
60
61 *******************************************************/
62
63/** @{ */
64
65/** The log checkpointer thread routine.
66@param[in,out] log_ptr pointer to redo log */
67void log_checkpointer(log_t *log_ptr);
68
69/** Checks if log checkpointer thread is active.
70@return true if and only if the log checkpointer thread is active */
73}
74
75#define log_checkpointer_mutex_enter(log) \
76 mutex_enter(&((log).checkpointer_mutex))
77
78#define log_checkpointer_mutex_exit(log) mutex_exit(&((log).checkpointer_mutex))
79
80#define log_checkpointer_mutex_own(log) \
81 (mutex_own(&((log).checkpointer_mutex)) || !log_checkpointer_is_active())
82
83/** @} */
84
85/**************************************************/ /**
86
87 @name Log - basic information about checkpoints.
88
89 *******************************************************/
90
91/** @{ */
92
93/** Gets the last checkpoint lsn stored and flushed to disk.
94@return last checkpoint lsn */
96 return log.last_checkpoint_lsn.load();
97}
98
99/** Calculates age of current checkpoint as number of bytes since
100last checkpoint. This includes bytes for headers and footers of
101all log blocks. The calculation is based on the latest written
102checkpoint lsn, and the current lsn, which points to the first
103non reserved data byte. Note that the current lsn could not fit
104the free space in the log files. This means that the checkpoint
105age could potentially be larger than capacity of the log files.
106However we do the best effort to avoid such situations, and if
107they happen, user threads wait until the space is reclaimed.
108@param[in] log redo log
109@return checkpoint age as number of bytes */
111
112/** Provides opposite checkpoint header number to the given checkpoint
113header number.
114@param[in] checkpoint_header_no the given checkpoint header number
115@return the opposite checkpoint header number */
117 Log_checkpoint_header_no checkpoint_header_no);
118
119/** Computes lsn up to which sync flush should be done or returns 0
120if there is no need to execute sync flush now.
121@param[in,out] log redo log
122@return lsn for which we want to have oldest_lsn >= lsn in each BP,
123 or 0 if there is no need for sync flush */
125
126/** @} */
127
128/**************************************************/ /**
129
130 @name Log - requests to make checkpoint.
131
132 *******************************************************/
133
134/** @{ */
135
136/** Requests a fuzzy checkpoint write (for currently available lsn).
137@param[in, out] log redo log
138@param[in] sync whether request is sync (function should wait) */
139void log_request_checkpoint(log_t &log, bool sync);
140
141/** Requests a checkpoint written in the next log file (not in the one,
142to which current log.last_checkpoint_lsn belongs to).
143@param[in,out] log redo log */
145
146/** Requests a checkpoint at the current lsn.
147@param[in,out] log redo log
148@param[out] requested_lsn lsn for which checkpoint was requested, or
149 stays unmodified if it wasn't requested
150@return true iff requested (false if checkpoint_lsn was already at that lsn) */
151bool log_request_latest_checkpoint(log_t &log, lsn_t &requested_lsn);
152
153/** Make a checkpoint at the current lsn. Reads current lsn and waits
154until all dirty pages have been flushed up to that lsn. Afterwards
155requests a checkpoint write and waits until it is finished.
156@param[in,out] log redo log
157@return true iff current lsn was greater than last checkpoint lsn */
159
160/** Make a checkpoint at the current lsn. Reads current lsn and waits
161until all dirty pages have been flushed up to that lsn. Afterwards
162requests a checkpoint write and waits until it is finished.
163@return true iff current lsn was greater than last checkpoint lsn */
165
166/** Updates the field log.dict_max_allowed_checkpoint_lsn. This is limitation
167for lsn at which checkpoint might be written, imposed by cached changes to the
168DD table buffer. It is called from DD code.
169@param[in,out] log redo log
170@param[in] max_lsn new value for the limitation */
172
173/** @} */
174
175/**************************************************/ /**
176
177 @name Log - concurrency margins.
178
179 *******************************************************/
180
181/** @{ */
182
183/** Computes concurrency margin to be used within log_free_check calls,
184for a given redo log capacity (soft_logical_capacity).
185@param[in] log_capacity redo log capacity (soft)
186@param[out] is_safe true iff the computed margin wasn't truncated
187 because of too small log_capacity
188@return the computed margin */
189sn_t log_concurrency_margin(lsn_t log_capacity, bool &is_safe);
190
191/** Updates log.concurrency_margin and log.concurrency_margin_is_safe for the
192current capacity of the redo log and current innodb_thread_concurrency value.
193@param[in,out] log redo log */
195
196/** @} */
197
198/**************************************************/ /**
199
200 @name Log - free check waiting.
201
202 *******************************************************/
203
204/** @{ */
205
206/** Waits until there is free space in log files which includes
207concurrency margin required for all threads. You should rather
208use log_free_check().
209@param[in] log redo log */
210void log_free_check_wait(log_t &log);
211
212/** Provides current margin used in the log_free_check calls.
213It is a sum of dict_persist_margin and concurrency_margin.
214@param[in] log redo log
215@return margin that would be used in log_free_check() */
217
218/** Computes capacity of redo log available until log_free_check()
219needs to wait. It uses a provided size of the log_free_check_margin.
220@param[in] log redo log
221@param[in] free_check_margin size of the log_free_check_margin;
222 @see log_free_check_margin(log)
223@return lsn capacity up to free_check_wait happens */
224lsn_t log_free_check_capacity(const log_t &log, lsn_t free_check_margin);
225
226/** Computes capacity of redo log available until log_free_check()
227needs to wait. It calls log_free_check_margin(log) to obtain the
228current log_free_check_margin.
229@param[in] log redo log
230@return lsn capacity up to free_check_wait happens */
232
233/** Checks if log_free_check() call should better be executed.
234@param[in] log redo log
235@return true iff log_free_check should be executed */
236inline bool log_free_check_is_required(const log_t &log) {
237 if (srv_read_only_mode) {
238 return false;
239 }
240 const lsn_t lsn = log_get_lsn(log);
241 return lsn > log.free_check_limit_lsn.load();
242}
243
244/** Checks if log_free_check() call should better be executed.
245@return true iff log_free_check should be executed */
247 ut_ad(log_sys != nullptr);
249}
250
251#ifdef UNIV_DEBUG
252/** Performs debug checks to validate some of the assumptions. */
254#endif /* UNIV_DEBUG */
255
256/** Reserves free_check_margin in the redo space for the current thread.
257For further details please look at description of @see log_free_check_margin().
258@param[in] log redo log */
259inline void log_free_check(log_t &log) {
261
262 /** We prefer to wait now for the space in log file, because now
263 are not holding any latches of dirty pages. */
264
266 /* We need to wait, because the concurrency margin could be violated
267 if we let all threads to go forward after making this check now.
268
269 The waiting procedure is rather unlikely to happen for proper my.cnf.
270 Therefore we extracted the code to separate function, to make the
271 inlined log_free_check() small. */
272
274 }
275}
276
277/** Checks for free space in the redo log. Must be called when no latches
278are held (except those listed as exceptions). Any database operation must
279call this before it has produced LOG_CHECKPOINT_FREE_PER_THREAD * UNIV_PAGE_SIZE
280bytes of redo log records. That's because that is the margin in redo log we
281reserve by calling this function.
282
283@remarks
284Checks if lsn corresponding to current log.sn exceeds log.free_check_limit_lsn,
285in which case waits (until it does not exceed). This function is called before
286starting a mini-transaction, because thread must not hold block latches when
287calling this function. It is also important that the caller does NOT hold any
288latch, that might be tried to be acquired:
289 - by the page cleaner (e.g. page/block latches),
290 - or by the log flush process (e.g. file space latches),
291 - or by any other thread, which might at that time already hold another
292 latch, that could further lead to a similar problem in chain of threads.
293For example, suppose a thread holding some latch X, which is neither used by
294the page cleaners nor by the log flush process, called log_free_check() and
295started to wait for the free space. Another thread, holding block's latch
296(which obviously might be needed for the page cleaners) tries to acquire the
297latch X. It needs to wait, because X has already been taken. Therefore, the
298latched block cannot be flushed. If this block had old modifications
299(low oldest_modification), it could effectively prevent any further attempts
300to reclaim space in the redo log. The chain of waiting for each other threads
301could obviously be even longer than the one in example. Therefore it is very
302important not to call log_free_check() if we are holding any latchs which
303might exist in any of such chains. As you can see, it is not that easy to see
304if log_free_check() might be called. It is not only about direct holding
305of block latches, but also such X (or Y acquired by thread holding such X),
306could lead to a deadlock.
307
308For sake of simplicity, you should better not keep any latch when calling to
309the log_free_check() unless you are really sure about what you are doing. */
310inline void log_free_check() {
311 ut_ad(log_sys != nullptr);
313}
314
315/** @} */
316
317/**************************************************/ /**
318
319 @name Log - free check updates.
320
321 *******************************************************/
322
323/** @{ */
324
325/** Updates log.free_check_limit_lsn in the log. The log_limits_mutex
326must be acquired before a call (unless srv_is_being_started is true).
327@param[in,out] log redo log */
329
330/** Updates log.dict_persist_margin and recompute free check limit.
331@param[in,out] log redo log
332@param[in] margin new value for log.dict_persist_margin */
333void log_set_dict_persist_margin(log_t &log, sn_t margin);
334
335/** @} */
336
337/**************************************************/ /**
338
339 @name Log - other functions related to checkpoints.
340
341 *******************************************************/
342
343/** @{ */
344
345/** Writes checkpoint to the file containing the written checkpoint_lsn.
346The checkpoint is written to the given checkpoint header. Unless InnoDB
347is starting: checkpointer, writer and files mutexes must be acquired
348before calling this function.
349@param[in,out] log redo log
350@param[in] checkpoint_file_handle handle to opened file
351@param[in] checkpoint_header_no checkpoint header to be written
352@param[in] next_checkpoint_lsn the checkpoint lsn to write
353@return DB_SUCCESS or error */
355 log_t &log, Log_file_handle &checkpoint_file_handle,
356 Log_checkpoint_header_no checkpoint_header_no, lsn_t next_checkpoint_lsn);
357
358/** Writes the first data block to the log file using the provided handle
359to the opened log file. The block is addressed by the given checkpoint_lsn,
360filled with 0x00 and its data length points to checkpoint_lsn inside, making
361the block logically empty.
362@remarks This is used only during creation of new log files.
363@param[in,out] log redo log
364@param[in] file_handle handle to the opened log file
365@param[in] checkpoint_lsn the checkpoint lsn
366@param[in] file_start_lsn start_lsn of the file
367@return DB_SUCCESS or error */
369 Log_file_handle &file_handle,
370 lsn_t checkpoint_lsn,
371 lsn_t file_start_lsn);
372
373/** Writes the next checkpoint to the log file, by writing a single
374checkpoint header with the checkpoint lsn. Flushes the file after the
375write and updates the log.last_checkpoint_lsn.
376
377@remarks Note that two checkpoint headers are used alternately for
378consecutive checkpoints. If InnoDB crashed during the write, it would
379still have the previous checkpoint info and recovery would work.
380@param[in,out] log redo log
381@param[in] lsn writes checkpoint at this lsn
382@return DB_SUCCESS or error */
384
385/** @} */
386
387#endif /* !UNIV_HOTBACKUP */
388
389#endif /* !log0chkp_h */
Handle which allows to do reads / writes for the opened file.
Definition: log0types.h:308
dberr_t
Definition: db0err.h:38
lsn_t log_get_checkpoint_age(const log_t &log)
Calculates age of current checkpoint as number of bytes since last checkpoint.
Definition: log0chkp.cc:1073
bool log_request_latest_checkpoint(log_t &log, lsn_t &requested_lsn)
Requests a checkpoint at the current lsn.
Definition: log0chkp.cc:664
void log_free_check_validate()
Performs debug checks to validate some of the assumptions.
Definition: log0chkp.cc:1271
bool log_free_check_is_required(const log_t &log)
Checks if log_free_check() call should better be executed.
Definition: log0chkp.h:236
lsn_t log_get_checkpoint_lsn(const log_t &log)
Gets the last checkpoint lsn stored and flushed to disk.
Definition: log0chkp.h:95
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:1177
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:517
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:1226
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:658
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:1238
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:321
lsn_t log_free_check_margin(const log_t &log)
Provides current margin used in the log_free_check calls.
Definition: log0chkp.cc:1218
void log_free_check(log_t &log)
Reserves free_check_margin in the redo space for the current thread.
Definition: log0chkp.h:259
bool log_checkpointer_is_active()
Checks if log checkpointer thread is active.
Definition: log0chkp.h:71
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:1102
void log_update_limits_low(log_t &log)
Updates log.free_check_limit_lsn in the log.
Definition: log0chkp.cc:1191
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:348
bool log_make_latest_checkpoint(log_t &log)
Make a checkpoint at the current lsn.
Definition: log0chkp.cc:687
void log_checkpointer(log_t *log_ptr)
The log checkpointer thread routine.
Definition: log0chkp.cc:975
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:1211
void log_request_checkpoint(log_t &log, bool sync)
Requests a fuzzy checkpoint write (for currently available lsn).
Definition: log0chkp.cc:592
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:438
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:426
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:759
Redo log - the main header.
lsn_t log_get_lsn(const log_t &log)
Gets the current lsn value.
Definition: log0log.h:167
Redo log - the log_sys.
log_t * log_sys
Redo log system (singleton).
Definition: log0log.cc:434
Redo log basic types.
Log_checkpoint_header_no
Enumerates checkpoint headers in the redo log file.
Definition: log0types.h:94
uint64_t sn_t
Type used for sn values, which enumerate bytes of data stored in the log.
Definition: log0types.h:85
uint64_t lsn_t
Type used for all log sequence number storage and arithmetic.
Definition: log0types.h:62
The server main program.
bool srv_thread_is_active(const IB_thread &thread)
Check if given thread is still active.
Definition: srv0srv.cc:3254
bool srv_read_only_mode
Set if InnoDB must operate in read-only mode.
Definition: srv0srv.cc:197
Srv_threads srv_threads
Structure with state of srv background threads.
Definition: srv0srv.cc:102
IB_thread m_log_checkpointer
Redo checkpointer thread.
Definition: srv0srv.h:174
Redo log - single data structure with state of the redo log system.
Definition: log0sys.h:76
atomic_lsn_t free_check_limit_lsn
Maximum lsn up to which there is free space in the redo log.
Definition: log0sys.h:659
atomic_lsn_t last_checkpoint_lsn
Latest checkpoint lsn.
Definition: log0sys.h:699
Debug checks for latches, header file.
Global types for sync.
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:104
#define ut_d(EXPR)
Debug statement.
Definition: ut0dbg.h:106
static uint64_t lsn
Definition: xcom_base.cc:445