Debian Codegraph @

storage/innobase/log/log0files_capacity.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2019, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the 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,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /**************************************************/ /**
29 @file log/log0files_capacity.cc
30
31 `physical_capacity`
32 (total size on disk except tmp files)
33 |
34 +- `*2/LOG_N_FILES`
35 | ("snake's head&tail cages")
36 |
37 +- `*FREE_RATIO`
38 ("the snake's body")
39 |
40 +- `OVERHEAD`
41 | (space which shouldn't be used for deltas)
42 | |
43 | +- `(LOG_N_FILES - 1) * LOG_FILE_HDR_SIZE`
44 | | (file headers in cages with snake's body)
45 | |
46 | +- `LOG_EXTRA_SAFETY_MARGIN`
47 | (just in case)
48 |
49 +- `lsn_capacity`
50 (space to use for deltas)
51 |
52 +- `next_file_earlier_margin`
53 | ("snake's tongue" - we want to never *need* more than LOG_N_FILES-1)
54 |
55 +- `hard_logical_capacity`
56 (this is how log writer sees the capacity)
57 |
58 +- `*LOG_EXTRA_WRITER_MARGIN_PCT/100`
59 | (log writer's private workspace to pull of "desperate rescue")
60 |
61 +- `soft_logical_capacity`
62 (this is how threads other than log writer see the capacity)
63 |
64 +- `free_check_margin`
65 | (it's "reserved" - you may write to it, if you did reservation)
66 | |
67 | +- `concurrency_margin`
68 | | (space jointly reserved by threads in log_free_check_wait)
69 | | |
70 | | +- `margin_per_thread * max_total_threads`
71 | | |
72 | | +- `LOG_FILES_DUMMY_INTAKE_SIZE`
73 | | | (dummy intake which might be required during redo resize)
74 | | |
75 | | +- `LOG_EXTRA_CONC_MARGIN_PCT / 100.0 * soft_logical_capacity`
76 | | (just in case)
77 | |
78 | +- `dict_persist_margin`
79 | (reserved for future dd metadata changes writes on checkpoint)
80 |
81 +- `log_free_check_capacity`
82 (if redo is this long threads should wait in log_free_check_wait)
83
84 *******************************************************/
85
86 /* ceil, floor */
87 #include <cmath>
88
89 /* arch_log_sys */
90 #include "arch0arch.h"
91
92 /* log_free_check_margin, log_free_check_capacity */
93 #include "log0chkp.h"
94
95 /* Log_files_capacity */
96 #include "log0files_capacity.h"
97
98 /* log_get_lsn, log_limits_mutex */
99 #include "log0log.h"
100
101 /* log_t::X */
102 #include "log0sys.h"
103
104 /* log_sync_point */
105 #include "log0test.h"
106
107 /* LOG_CHECKPOINT_FREE_PER_THREAD, ... */
108 #include "log0types.h"
109
110 /* srv_thread_concurrency */
111 #include "srv0conc.h"
112
113 /* MONITOR_SET */
114 #include "srv0mon.h"
115
116 /* srv_is_being_started */
117 #include "srv0start.h"
118
119 /* ut_uint64_align_down */
120 #include "ut0byte.h"
121
122 #ifndef UNIV_HOTBACKUP
123
124 void log_files_capacity_get_limits(const log_t &log,
125 lsn_t &limit_for_free_check,
126 lsn_t &limit_for_dirty_page_age) {
127 IB_mutex_guard limits_latch{&(log.limits_mutex), UT_LOCATION_HERE};
128
129 const lsn_t adaptive_flush_min_age = log.m_capacity.adaptive_flush_min_age();
130 ut_a(adaptive_flush_min_age != 0);
131
132 const lsn_t margin = log_free_check_margin(log);
133 ut_a(margin < adaptive_flush_min_age);
134
135 limit_for_free_check = log_free_check_capacity(log, margin);
136
137 limit_for_dirty_page_age = ut_uint64_align_down(
138 adaptive_flush_min_age - margin, OS_FILE_LOG_BLOCK_SIZE);
139 }
140
141 #endif /* !UNIV_HOTBACKUP */
142
143 /**************************************************/ /**
144
145 @name Log_files_capacity implementation
146
147 *******************************************************/
148
149 /** @{ */
150
151 void Log_files_capacity::initialize(const Log_files_dict &files,
152 lsn_t current_logical_size,
153 lsn_t current_checkpoint_age) {
154 m_resize_mode = Log_resize_mode::NONE;
155 m_current_physical_capacity = LOG_CAPACITY_MAX;
156
157 os_offset_t min_t = LOG_CAPACITY_MIN, max_t = LOG_CAPACITY_MAX;
158
159 /* One could compute the m_target_physical_capacity backward by reverting
160 computations made by criteria inside is_target_reached_for_resizing_down(),
161 but the binary-search-based approach seems safer and is fast enough. */
162 while (min_t / 1024 * 1024UL < max_t / 1024 * 1024UL) {
163 m_target_physical_capacity =
164 ut_uint64_align_down((min_t + max_t) / 2, 1024 * 1024UL);
165
166 if (is_target_reached_for_resizing_down(files, current_logical_size)) {
167 max_t = m_target_physical_capacity;
168 } else {
169 min_t = m_target_physical_capacity + 1024 * 1024UL;
170 }
171 }
172
173 /* In external tools, which don't need to resize the redo log,
174 there is srv_redo_log_capacity == srv_redo_log_capacity_used == 0
175 (no target for redo size). */
176 ut_a(LOG_CAPACITY_MIN <= srv_redo_log_capacity_used ||
177 (srv_redo_log_capacity == 0 && srv_redo_log_capacity_used == 0));
178
179 m_target_physical_capacity = m_current_physical_capacity =
180 std::max(max_t, os_offset_t{srv_redo_log_capacity_used});
181
182 ut_a(is_target_reached_for_resizing_down(files, current_logical_size));
183
184 update_exposed(
185 hard_logical_capacity_for_physical(m_current_physical_capacity));
186
187 update(files, current_logical_size, current_checkpoint_age);
188 }
189
190 void Log_files_capacity::update(const Log_files_dict &files,
191 lsn_t current_logical_size,
192 lsn_t current_checkpoint_age) {
193 ut_a(m_current_physical_capacity > 0);
194
195 /* Check if a new goal has been set and start a new resize if needed
196 (cancelling a pending resize if there is one). */
197 update_target();
198
199 /* Check if the existing goal has been reached. */
200 update_if_target_reached(files, current_logical_size);
201
202 const lsn_t hard_logical_capacity =
203 get_suggested_hard_logical_capacity(current_checkpoint_age);
204
205 /* Update fields of m_exposed, which describe logical capacity limitations,
206 which are exposed by this class to the page cleaner coordinator and the log
207 writer threads. */
208 update_exposed(hard_logical_capacity);
209
210 /* Update InnoDB status variables reflecting all possible changes which have
211 been done within this update() call. */
212 update_status_variables(files, current_logical_size);
213 }
214
215 void Log_files_capacity::cancel_resize() {
216 if (m_resize_mode == Log_resize_mode::NONE) {
217 /* There is no resize in progress, return now to avoid emitting
218 the message to the error log. */
219 return;
220 }
221 m_resize_mode = Log_resize_mode::NONE;
222 m_target_physical_capacity = m_current_physical_capacity;
223 ib::info(ER_IB_MSG_LOG_FILES_RESIZE_CANCELLED);
224 }
225
226 void Log_files_capacity::update_target() {
227 const os_offset_t target_physical_capacity = srv_redo_log_capacity_used;
228 if (m_target_physical_capacity == target_physical_capacity) {
229 /* Target has not been changed since last call to update_target().
230 Return now to avoid emitting messages to the error log. */
231 return;
232 }
233 if (target_physical_capacity == 0) {
234 /* There is no target. No resize is needed. This allows to use
235 Log_files_capacity in external tools which don't need to resize
236 the redo log. */
237 return;
238 }
239
240 /* Target has been changed (the innodb_redo_log_capacity has been changed),
241 so first: cancel any resize operation which possibly is in progress. */
242 cancel_resize();
243
244 /* There is no resize in progress now. */
245 ut_a(m_resize_mode == Log_resize_mode::NONE);
246 ut_a(m_current_physical_capacity == m_target_physical_capacity);
247
248 /* Start a new resize if needed. Note, that user could have started
249 a downsize operation and then reset the innodb_redo_log_capacity to its
250 previous value (equal to m_current_physical_capacity). In such case,
251 it is enough that the cancel_resize() emitted message to the error log,
252 and all the required work has already been done by the cancel_resize(). */
253 if (target_physical_capacity != m_current_physical_capacity) {
254 m_target_physical_capacity = target_physical_capacity;
255
256 ib::info(ER_IB_MSG_LOG_FILES_RESIZE_REQUESTED,
257 ulonglong{m_current_physical_capacity} / (1024 * 1024UL),
258 ulonglong{m_target_physical_capacity} / (1024 * 1024UL));
259
260 if (m_target_physical_capacity < m_current_physical_capacity) {
261 m_resize_mode = Log_resize_mode::RESIZING_DOWN;
262 } else {
263 ut_a(m_resize_mode == Log_resize_mode::NONE);
264 m_current_physical_capacity = m_target_physical_capacity;
265 ib::info(ER_IB_MSG_LOG_FILES_RESIZE_FINISHED,
266 ulonglong{m_current_physical_capacity} / (1024 * 1024UL));
267 }
268 }
269
270 ut_a(m_target_physical_capacity <= m_current_physical_capacity);
271 }
272
273 lsn_t Log_files_capacity::hard_logical_capacity_for_physical(
274 os_offset_t physical_capacity) {
275 constexpr auto LOG_CONCURRENT_MARGIN_MIN = log_translate_sn_to_lsn(
276 LOG_BACKGROUND_THREADS_USING_RW_MTRS * LOG_CHECKPOINT_FREE_PER_THREAD *
277 UNIV_PAGE_SIZE_MAX +
278 LOG_FILES_DUMMY_INTAKE_SIZE);
279
280 constexpr auto LOG_WRITER_SOFT_CAPACITY_MIN =
281 UNIV_PAGE_SIZE_MAX +
282 LOG_CONCURRENT_MARGIN_MIN / (LOG_CONCCURENCY_MARGIN_MAX_PCT / 100.0);
283
284 constexpr auto LOG_WRITER_HARD_CAPACITY_MIN =
285 LOG_WRITER_SOFT_CAPACITY_MIN / (1 - LOG_EXTRA_WRITER_MARGIN_PCT / 100.0);
286
287 /* The goal is to have one file free, so InnoDB could always create a next
288 redo log file. Because logical redo data might begin at the very end of the
289 oldest redo file, and end at the very beginning of the newest file, we need
290 to ensure its size leaves room for at least two files, which would guarantee
291 that at most LOG_N_FILES-1 exist. */
292 constexpr double FREE_FILE_RATIO = 1.0 * (LOG_N_FILES - 2) / LOG_N_FILES;
293
294 /* At most LOG_N_FILES - 1 are expected to be seen (LOG_N_FILES for a short
295 moment when new redo file is created but the oldest hasn't yet been removed,
296 but in this case, the oldest file isn't providing any capacity).
297
298 Each of these files has header which occupies LOG_FILE_HDR_SIZE bytes. That
299 gives (LOG_N_FILES - 1) * LOG_FILE_HDR_SIZE bytes on disk which do not give
300 any space for bytes counted to lsn sequence. Additionally, it is guaranteed
301 that extra LOG_EXTRA_SAFETY_MARGIN bytes, within the space occupied on disk,
302 are never allocated to redo data. */
303
304 const os_offset_t OVERHEAD =
305 (LOG_N_FILES - 1) * LOG_FILE_HDR_SIZE + LOG_EXTRA_SAFETY_MARGIN;
306
307 constexpr lsn_t NEXT_FILE_EARLIER_MARGIN_FOR_LOG_CAPACITY_MIN =
308 (LOG_NEXT_FILE_EARLIER_MARGIN / 100.0) * LOG_CAPACITY_MIN / LOG_N_FILES +
309 OS_FILE_LOG_BLOCK_SIZE;
310
311 static_assert(
312 LOG_WRITER_HARD_CAPACITY_MIN + OVERHEAD + OS_FILE_LOG_BLOCK_SIZE +
313 NEXT_FILE_EARLIER_MARGIN_FOR_LOG_CAPACITY_MIN <=
314 LOG_CAPACITY_MIN * FREE_FILE_RATIO,
315 "The minimum redo capacity should be considered good enough for "
316 "innodb_thread_concurrency = 0 and 64k pages.");
317
318 ut_a(LOG_CAPACITY_MIN <= physical_capacity);
319
320 /* Combining these two assertions we get: */
321 ut_a(LOG_WRITER_HARD_CAPACITY_MIN + OS_FILE_LOG_BLOCK_SIZE <=
322 physical_capacity * FREE_FILE_RATIO - OVERHEAD);
323
324 const lsn_t lsn_capacity = physical_capacity * FREE_FILE_RATIO - OVERHEAD;
325
326 const auto ret = ut_uint64_align_down(
327 lsn_capacity - next_file_earlier_margin(physical_capacity),
328 OS_FILE_LOG_BLOCK_SIZE);
329
330 ut_a(LOG_WRITER_HARD_CAPACITY_MIN <= ret);
331
332 return ret;
333 }
334
335 bool Log_files_capacity::is_target_reached_for_logical_size(
336 lsn_t current_logical_size) const {
337 return current_logical_size <=
338 soft_logical_capacity_for_hard(
339 hard_logical_capacity_for_physical(m_target_physical_capacity));
340 }
341
342 bool Log_files_capacity::is_target_reached_for_physical_size(
343 os_offset_t current_physical_size) const {
344 /* Note, that is_target_reached_for_logical_size() guarantees that this
345 condition should also hold unless there was no time to consume old redo
346 log files yet or other than checkpointer redo log consumers prevented the
347 consumption. Note, that the log_files_governor updates Log_files_capacity
348 before it decides if consumption is needed. Also, after starting up InnoDB
349 it might happen that the oldest files might be consumed, but before actual
350 consumption is done, InnoDB must not assume redo is resized down. */
351 constexpr double FREE_FILE_RATIO = (LOG_N_FILES - 1) * 1.0 / LOG_N_FILES;
352 return current_physical_size <= FREE_FILE_RATIO * m_target_physical_capacity;
353 }
354
355 bool Log_files_capacity::is_target_reached_for_max_file_size(
356 const Log_files_dict &files) const {
357 auto it = log_files_find_largest(files);
358
359 return it == files.end() ||
360 it->m_size_in_bytes <= m_target_physical_capacity / LOG_N_FILES;
361 }
362
363 bool Log_files_capacity::is_target_reached_for_resizing_down(
364 const Log_files_dict &files, lsn_t current_logical_size) const {
365 return is_target_reached_for_logical_size(current_logical_size) &&
366 is_target_reached_for_physical_size(
367 log_files_size_of_existing_files(files)) &&
368 is_target_reached_for_max_file_size(files);
369 }
370
371 void Log_files_capacity::update_if_target_reached(const Log_files_dict &files,
372 lsn_t current_logical_size) {
373 switch (m_resize_mode) {
374 case Log_resize_mode::NONE:
375 break;
376 case Log_resize_mode::RESIZING_DOWN:
377 if (is_target_reached_for_resizing_down(files, current_logical_size)) {
378 m_current_physical_capacity = m_target_physical_capacity;
379 m_resize_mode = Log_resize_mode::NONE;
380 ib::info(ER_IB_MSG_LOG_FILES_RESIZE_FINISHED,
381 ulonglong{m_current_physical_capacity} / (1024 * 1024UL));
382 }
383 break;
384 default:
385 ut_error;
386 }
387 }
388
389 void Log_files_capacity::update_status_variables(const Log_files_dict &files,
390 lsn_t current_logical_size) {
391 const auto current_physical_size = log_files_size_of_existing_files(files);
392 ut_a(current_physical_size <= m_current_physical_capacity);
393
394 update_resize_status_variable();
395
396 export_vars.innodb_redo_log_logical_size = ulonglong{current_logical_size};
397 export_vars.innodb_redo_log_physical_size = ulonglong{current_physical_size};
398 export_vars.innodb_redo_log_capacity_resized =
399 ulonglong{m_current_physical_capacity};
400
401 #ifndef UNIV_HOTBACKUP
402 log_sync_point("log_status_variables_updated");
403 #endif /* !UNIV_HOTBACKUP */
404 }
405
406 void Log_files_capacity::update_resize_status_variable() {
407 const char *status;
408 switch (m_resize_mode) {
409 case Log_resize_mode::NONE:
410 status = "OK";
411 break;
412 case Log_resize_mode::RESIZING_DOWN:
413 status = "Resizing down";
414 break;
415 default:
416 ut_error;
417 return;
418 }
419 ut_a(sizeof(export_vars.innodb_redo_log_resize_status) >= strlen(status) + 1);
420 strcpy(export_vars.innodb_redo_log_resize_status, status);
421 }
422
423 lsn_t Log_files_capacity::guess_hard_logical_capacity_for_soft(
424 lsn_t soft_logical_capacity) {
425 const double ratio = 1.0 - LOG_EXTRA_WRITER_MARGIN_PCT / 100.0;
426 return ut_uint64_align_up(
427 static_cast<lsn_t>(ceil(soft_logical_capacity / ratio)),
428 OS_FILE_LOG_BLOCK_SIZE);
429 }
430
431 lsn_t Log_files_capacity::soft_logical_capacity_for_hard(
432 lsn_t hard_logical_capacity) {
433 const double ratio = 1.0 - LOG_EXTRA_WRITER_MARGIN_PCT / 100.0;
434 return ut_uint64_align_down(
435 static_cast<lsn_t>(floor(hard_logical_capacity * ratio)),
436 OS_FILE_LOG_BLOCK_SIZE);
437 }
438
439 lsn_t Log_files_capacity::guess_soft_logical_capacity_for_sync_flush(
440 lsn_t adaptive_flush_max_age) {
441 const double ratio = 1.0 - 1.0 / LOG_FORCING_ADAPTIVE_FLUSH_RATIO_MAX;
442 return ut_uint64_align_up(
443 static_cast<lsn_t>(ceil(adaptive_flush_max_age / ratio)),
444 OS_FILE_LOG_BLOCK_SIZE);
445 }
446
447 lsn_t Log_files_capacity::sync_flush_logical_capacity_for_soft(
448 lsn_t soft_logical_capacity) {
449 const double ratio = 1.0 - 1.0 / LOG_FORCING_ADAPTIVE_FLUSH_RATIO_MAX;
450 return ut_uint64_align_down(
451 static_cast<lsn_t>(floor(soft_logical_capacity * ratio)),
452 OS_FILE_LOG_BLOCK_SIZE);
453 }
454
455 lsn_t Log_files_capacity::get_suggested_hard_logical_capacity(
456 lsn_t current_checkpoint_age) const {
457 switch (m_resize_mode) {
458 case Log_resize_mode::NONE:
459 ut_a(m_current_physical_capacity == m_target_physical_capacity);
460 return hard_logical_capacity_for_physical(m_current_physical_capacity);
461
462 case Log_resize_mode::RESIZING_DOWN:
463 return std::max(
464 hard_logical_capacity_for_physical(m_target_physical_capacity),
465 std::min(m_exposed.m_hard_logical_capacity.load(),
466 guess_hard_logical_capacity_for_soft(
467 guess_soft_logical_capacity_for_sync_flush(
468 current_checkpoint_age))));
469
470 default:
471 ut_error;
472 }
473 }
474
475 void Log_files_capacity::update_exposed(lsn_t hard_logical_capacity) {
476 const lsn_t soft_logical_capacity =
477 soft_logical_capacity_for_hard(hard_logical_capacity);
478
479 ut_a(2 * OS_FILE_LOG_BLOCK_SIZE <= soft_logical_capacity);
480
481 m_exposed.m_hard_logical_capacity.store(hard_logical_capacity);
482
483 m_exposed.m_soft_logical_capacity.store(soft_logical_capacity);
484
485 /* Set limits used in flushing and checkpointing mechanism. */
486
487 m_exposed.m_adaptive_flush_max_age.store(
488 sync_flush_logical_capacity_for_soft(soft_logical_capacity));
489
490 m_exposed.m_adaptive_flush_min_age.store(ut_uint64_align_down(
491 soft_logical_capacity -
492 soft_logical_capacity / LOG_FORCING_ADAPTIVE_FLUSH_RATIO_MIN,
493 OS_FILE_LOG_BLOCK_SIZE));
494
495 m_exposed.m_agressive_checkpoint_min_age.store(ut_uint64_align_down(
496 soft_logical_capacity -
497 soft_logical_capacity / LOG_AGGRESSIVE_CHECKPOINT_RATIO_MIN,
498 OS_FILE_LOG_BLOCK_SIZE));
499 }
500
501 lsn_t Log_files_capacity::hard_logical_capacity() const {
502 return m_exposed.m_hard_logical_capacity.load();
503 }
504
505 lsn_t Log_files_capacity::soft_logical_capacity() const {
506 return m_exposed.m_soft_logical_capacity.load();
507 }
508
509 lsn_t Log_files_capacity::adaptive_flush_min_age() const {
510 return m_exposed.m_adaptive_flush_min_age.load();
511 }
512
513 lsn_t Log_files_capacity::adaptive_flush_max_age() const {
514 return m_exposed.m_adaptive_flush_max_age.load();
515 }
516
517 lsn_t Log_files_capacity::aggressive_checkpoint_min_age() const {
518 return m_exposed.m_agressive_checkpoint_min_age.load();
519 }
520
521 bool Log_files_capacity::is_resizing_down() const {
522 return m_target_physical_capacity < m_current_physical_capacity;
523 }
524
525 os_offset_t Log_files_capacity::target_physical_capacity() const {
526 return m_target_physical_capacity;
527 }
528
529 os_offset_t Log_files_capacity::current_physical_capacity() const {
530 return m_current_physical_capacity;
531 }
532
533 os_offset_t Log_files_capacity::next_file_size() const {
534 return next_file_size(m_target_physical_capacity);
535 }
536
537 os_offset_t Log_files_capacity::next_file_size(os_offset_t physical_capacity) {
538 const auto file_size =
539 ut_uint64_align_down(physical_capacity / LOG_N_FILES, UNIV_PAGE_SIZE);
540 ut_a(LOG_FILE_MIN_SIZE <= file_size);
541 ut_a(file_size <= LOG_FILE_MAX_SIZE);
542 ut_a(file_size % UNIV_PAGE_SIZE == 0);
543 return file_size;
544 }
545
546 lsn_t Log_files_capacity::next_file_earlier_margin(
547 os_offset_t physical_capacity) {
548 const auto file_size = next_file_size(physical_capacity);
549 return ut_uint64_align_up(
550 ceil(LOG_NEXT_FILE_EARLIER_MARGIN / 100.0 * file_size),
551 OS_FILE_LOG_BLOCK_SIZE);
552 }
553
554 lsn_t Log_files_capacity::next_file_earlier_margin() const {
555 return next_file_earlier_margin(m_target_physical_capacity);
556 }
557
558 /** @} */