Debian Codegraph @

storage/innobase/include/log0chkp.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 1995, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 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 */
68 void log_checkpointer(log_t *log_ptr);
69
70 /** Requests a checkpoint written for lsn greater or equal to provided one.
71 The log.limits_mutex has to be acquired before it is called, and it is not
72 released within this function.
73 @param[in,out] log redo log
74 @param[in] requested_lsn provided lsn (checkpoint should be not older) */
75 void 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 */
79 inline bool log_checkpointer_is_active() {
80 return srv_thread_is_active(srv_threads.m_log_checkpointer);
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 */
103 inline lsn_t log_get_checkpoint_lsn(const log_t &log) {
104 return log.last_checkpoint_lsn.load();
105 }
106
107 /** Calculates age of current checkpoint as number of bytes since
108 last checkpoint. This includes bytes for headers and footers of
109 all log blocks. The calculation is based on the latest written
110 checkpoint lsn, and the current lsn, which points to the first
111 non reserved data byte. Note that the current lsn could not fit
112 the free space in the log files. This means that the checkpoint
113 age could potentially be larger than capacity of the log files.
114 However we do the best effort to avoid such situations, and if
115 they happen, user threads wait until the space is reclaimed.
116 @param[in] log redo log
117 @return checkpoint age as number of bytes */
118 inline lsn_t log_get_checkpoint_age(const log_t &log) {
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
138 header number.
139 @param[in] checkpoint_header_no the given checkpoint header number
140 @return the opposite checkpoint header number */
141 Log_checkpoint_header_no log_next_checkpoint_header(
142 Log_checkpoint_header_no checkpoint_header_no);
143
144 /** Computes lsn up to which sync flush should be done or returns 0
145 if 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 */
149 lsn_t log_sync_flush_lsn(log_t &log);
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) */
164 void 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) */
171 bool 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
174 until all dirty pages have been flushed up to that lsn. Afterwards
175 requests 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 */
178 bool log_make_latest_checkpoint(log_t &log);
179
180 /** Make a checkpoint at the current lsn. Reads current lsn and waits
181 until all dirty pages have been flushed up to that lsn. Afterwards
182 requests a checkpoint write and waits until it is finished.
183 @return true iff current lsn was greater than last checkpoint lsn */
184 bool log_make_latest_checkpoint();
185
186 /** Updates the field log.dict_max_allowed_checkpoint_lsn. This is limitation
187 for lsn at which checkpoint might be written, imposed by cached changes to the
188 DD 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 */
191 void log_set_dict_max_allowed_checkpoint_lsn(log_t &log, lsn_t max_lsn);
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,
204 for 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 */
209 sn_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
212 current capacity of the redo log and current innodb_thread_concurrency value.
213 @param[in,out] log redo log */
214 void log_update_concurrency_margin(log_t &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
227 concurrency margin required for all threads. You should rather
228 use log_free_check().
229 @param[in] log redo log */
230 void log_free_check_wait(log_t &log);
231
232 /** Provides current margin used in the log_free_check calls.
233 It 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() */
236 lsn_t log_free_check_margin(const log_t &log);
237
238 /** Computes capacity of redo log available until log_free_check()
239 needs 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 */
244 lsn_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()
247 needs to wait. It calls log_free_check_margin(log) to obtain the
248 current log_free_check_margin.
249 @param[in] log redo log
250 @return lsn capacity up to free_check_wait happens */
251 lsn_t log_free_check_capacity(const log_t &log);
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 */
256 inline 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 */
266 inline bool log_free_check_is_required() {
267 ut_ad(log_sys != nullptr);
268 return log_free_check_is_required(*log_sys);
269 }
270
271 #ifdef UNIV_DEBUG
272 /** Performs debug checks to validate some of the assumptions. */
273 void log_free_check_validate();
274 #endif /* UNIV_DEBUG */
275
276 /** Reserves free_check_margin in the redo space for the current thread.
277 For further details please look at description of @see log_free_check_margin().
278 @param[in] log redo log */
279 inline void log_free_check(log_t &log) {
280 ut_d(log_free_check_validate());
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
285 if (log_free_check_is_required(log)) {
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
293 log_free_check_wait(log);
294 }
295 }
296
297 /** Checks for free space in the redo log. Must be called when no latches
298 are held (except those listed as exceptions). Any database operation must
299 call this before it has produced LOG_CHECKPOINT_FREE_PER_THREAD * UNIV_PAGE_SIZE
300 bytes of redo log records. That's because that is the margin in redo log we
301 reserve by calling this function.
302
303 @remarks
304 Checks if lsn corresponding to current log.sn exceeds log.free_check_limit_lsn,
305 in which case waits (until it does not exceed). This function is called before
306 starting a mini-transaction, because thread must not hold block latches when
307 calling this function. It is also important that the caller does NOT hold any
308 latch, 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.
313 For example, suppose a thread holding some latch X, which is neither used by
314 the page cleaners nor by the log flush process, called log_free_check() and
315 started 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
317 latch X. It needs to wait, because X has already been taken. Therefore, the
318 latched block cannot be flushed. If this block had old modifications
319 (low oldest_modification), it could effectively prevent any further attempts
320 to reclaim space in the redo log. The chain of waiting for each other threads
321 could obviously be even longer than the one in example. Therefore it is very
322 important not to call log_free_check() if we are holding any latchs which
323 might exist in any of such chains. As you can see, it is not that easy to see
324 if log_free_check() might be called. It is not only about direct holding
325 of block latches, but also such X (or Y acquired by thread holding such X),
326 could lead to a deadlock.
327
328 For sake of simplicity, you should better not keep any latch when calling to
329 the log_free_check() unless you are really sure about what you are doing. */
330 inline void log_free_check() {
331 ut_ad(log_sys != nullptr);
332 log_free_check(*log_sys);
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
346 log_limits_mutex must be acquired before a call (unless srv_is_being_started is
347 true).
348 @param[in,out] log redo log */
349 void log_update_limits_low(log_t &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 */
354 void 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.
367 The checkpoint is written to the given checkpoint header. Unless InnoDB
368 is starting: checkpointer, writer and files mutexes must be acquired
369 before 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 */
375 dberr_t log_files_write_checkpoint_low(
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
380 to the opened log file. The block is addressed by the given checkpoint_lsn,
381 filled with 0x00 and its data length points to checkpoint_lsn inside, making
382 the 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 */
389 dberr_t log_files_write_first_data_block_low(log_t &log,
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
395 checkpoint header with the checkpoint lsn. Flushes the file after the
396 write and updates the log.last_checkpoint_lsn.
397
398 @remarks Note that two checkpoint headers are used alternately for
399 consecutive checkpoints. If InnoDB crashed during the write, it would
400 still 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 */
404 dberr_t log_files_next_checkpoint(log_t &log, lsn_t lsn);
405
406 /** @} */
407
408 #endif /* !UNIV_HOTBACKUP */
409
410 #endif /* !log0chkp_h */