| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2009, Google Inc. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License, version 2.0, |
| 8 |
as published by the Free Software Foundation. |
| 9 |
|
| 10 |
This program is designed to work with certain software (including |
| 11 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 12 |
as designated in a particular file or component or in included license |
| 13 |
documentation. The authors of MySQL hereby grant you an additional |
| 14 |
permission to link the program and your derivative works with the |
| 15 |
separately licensed software that they have either included with |
| 16 |
the program or referenced in the documentation. |
| 17 |
|
| 18 |
This program is distributed in the hope that it will be useful, |
| 19 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
GNU General Public License, version 2.0, for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License |
| 24 |
along with this program; if not, write to the Free Software |
| 25 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 |
|
| 27 |
*****************************************************************************/ |
| 28 |
|
| 29 |
/**************************************************/ /** |
| 30 |
@file log/log0chkp.cc |
| 31 |
|
| 32 |
Redo log checkpointing. |
| 33 |
|
| 34 |
File consists of four groups: |
| 35 |
1. Coordination between log and buffer pool (oldest_lsn). |
| 36 |
2. Making checkpoints (including the log_checkpointer thread). |
| 37 |
3. Free check. |
| 38 |
|
| 39 |
*******************************************************/ |
| 40 |
|
| 41 |
/* std::chrono::X */ |
| 42 |
#include <chrono> |
| 43 |
|
| 44 |
/* std::memcpy */ |
| 45 |
#include <cstring> |
| 46 |
|
| 47 |
/* arch_page_sys */ |
| 48 |
#include "arch0arch.h" |
| 49 |
|
| 50 |
/* buf_pool_get_oldest_modification_lwm, page_id_t */ |
| 51 |
#include "buf0buf.h" |
| 52 |
|
| 53 |
/* buf_flush_fsync */ |
| 54 |
#include "buf0flu.h" |
| 55 |
|
| 56 |
/* dict_persist_to_dd_table_buffer */ |
| 57 |
#include "dict0dict.h" |
| 58 |
|
| 59 |
/* log_buffer_dirty_pages_added_up_to_lsn */ |
| 60 |
#include "log0buf.h" |
| 61 |
|
| 62 |
#include "log0chkp.h" |
| 63 |
|
| 64 |
/* log_can_encrypt */ |
| 65 |
#include "log0encryption.h" |
| 66 |
|
| 67 |
/* log_files_header_flush, ... */ |
| 68 |
#include "log0files_io.h" |
| 69 |
|
| 70 |
/* log_limits_mutex, ... */ |
| 71 |
#include "log0log.h" |
| 72 |
|
| 73 |
/* recv_recovery_is_on() */ |
| 74 |
#include "log0recv.h" |
| 75 |
|
| 76 |
/* log_t::X */ |
| 77 |
#include "log0sys.h" |
| 78 |
|
| 79 |
/* log_sync_point, log_test */ |
| 80 |
#include "log0test.h" |
| 81 |
|
| 82 |
/* OS_FILE_LOG_BLOCK_SIZE, ... */ |
| 83 |
#include "log0types.h" |
| 84 |
|
| 85 |
/* log_writer_mutex */ |
| 86 |
#include "log0write.h" |
| 87 |
|
| 88 |
/* mach_write_to_4, ... */ |
| 89 |
#include "mach0data.h" |
| 90 |
|
| 91 |
/* DBUG_PRINT, ... */ |
| 92 |
#include "my_dbug.h" |
| 93 |
|
| 94 |
/* os_event_wait_time_low */ |
| 95 |
#include "os0event.h" |
| 96 |
|
| 97 |
/* MONITOR_INC, ... */ |
| 98 |
#include "srv0mon.h" |
| 99 |
|
| 100 |
/* srv_read_only_mode */ |
| 101 |
#include "srv0srv.h" |
| 102 |
|
| 103 |
/* srv_is_being_started */ |
| 104 |
#include "srv0start.h" |
| 105 |
|
| 106 |
/* ut_uint64_align_down */ |
| 107 |
#include "ut0byte.h" |
| 108 |
|
| 109 |
#ifndef UNIV_HOTBACKUP |
| 110 |
|
| 111 |
/** Updates lsn available for checkpoint. |
| 112 |
@param[in,out] log redo log */ |
| 113 |
static void log_update_available_for_checkpoint_lsn(log_t &log); |
| 114 |
|
| 115 |
/** Checks if checkpoint should be written. Checks time elapsed since the last |
| 116 |
checkpoint, age of the last checkpoint and if there was any extra request to |
| 117 |
write the checkpoint (e.g. coming from log_make_latest_checkpoint()). |
| 118 |
@return true if checkpoint should be written */ |
| 119 |
static bool log_should_checkpoint(log_t &log); |
| 120 |
|
| 121 |
/** Considers writing next checkpoint. Checks if checkpoint should be written |
| 122 |
(using log_should_checkpoint()) and writes the checkpoint if that's the case. */ |
| 123 |
static void log_consider_checkpoint(log_t &log); |
| 124 |
|
| 125 |
/** Considers requesting page cleaners to execute sync flush. */ |
| 126 |
static void log_consider_sync_flush(log_t &log); |
| 127 |
|
| 128 |
/** Makes a checkpoint. Note that this function does not flush dirty blocks |
| 129 |
from the buffer pool. It only checks what is lsn of the oldest modification |
| 130 |
in the buffer pool, and writes information about the lsn in log files. |
| 131 |
@param[in,out] log redo log */ |
| 132 |
static void log_checkpoint(log_t &log); |
| 133 |
|
| 134 |
/** Calculates time that elapsed since last checkpoint. |
| 135 |
@return Time duration elapsed since the last checkpoint */ |
| 136 |
static std::chrono::steady_clock::duration log_checkpoint_time_elapsed( |
| 137 |
const log_t &log); |
| 138 |
|
| 139 |
/** Waits for checkpoint advanced to at least that lsn. |
| 140 |
@param[in] log redo log |
| 141 |
@param[in] lsn lsn up to which we are waiting */ |
| 142 |
static void log_wait_for_checkpoint(const log_t &log, lsn_t lsn); |
| 143 |
|
| 144 |
/** Requests for urgent flush of dirty pages, to advance oldest_lsn |
| 145 |
in flush lists to provided value. This should force page cleaners |
| 146 |
to perform the sync-flush in which case the innodb_max_io_capacity |
| 147 |
is not respected. This should be called when we are close to running |
| 148 |
out of space in redo log (close to free_check_limit_lsn). |
| 149 |
@param[in] log redo log |
| 150 |
@param[in] new_oldest oldest_lsn to stop flush at (or greater) |
| 151 |
@retval true requested page flushing |
| 152 |
@retval false did not request page flushing (either because it is |
| 153 |
unit test for redo log or sync flushing is disabled |
| 154 |
by sys_var: innodb_flush_sync) */ |
| 155 |
static bool log_request_sync_flush(const log_t &log, lsn_t new_oldest); |
| 156 |
|
| 157 |
/**************************************************/ /** |
| 158 |
|
| 159 |
@name Log - coordination with buffer pool and oldest_lsn |
| 160 |
|
| 161 |
*******************************************************/ |
| 162 |
|
| 163 |
/** @{ */ |
| 164 |
|
| 165 |
/** Calculates lsn at which we might write a next checkpoint. It does the |
| 166 |
best effort, but possibly the maximum allowed lsn, could be even bigger. |
| 167 |
That's because the order of dirty pages in flush lists has been relaxed, |
| 168 |
and we don't want to spend time on traversing the whole flush lists here. |
| 169 |
|
| 170 |
Note that some flush lists could be empty, and some additions of dirty pages |
| 171 |
could be pending (threads have written data to the log buffer and became |
| 172 |
scheduled out just before adding the dirty pages). That's why the calculated |
| 173 |
value cannot be larger than the log.buf_dirty_pages_added_up_to_lsn (only up |
| 174 |
to this lsn value we are sure, that all the dirty pages have been added). |
| 175 |
|
| 176 |
It is guaranteed, that the returned value will not be smaller than |
| 177 |
the log.last_checkpoint_lsn. |
| 178 |
|
| 179 |
@return lsn for which we might write the checkpoint */ |
| 180 |
static lsn_t log_compute_available_for_checkpoint_lsn(const log_t &log) { |
| 181 |
/* The log_buffer_dirty_pages_added_up_to_lsn() can only increase, |
| 182 |
and that happens only after all related dirty pages have been added |
| 183 |
to the flush lists. |
| 184 |
|
| 185 |
Hence, to avoid issues related to race conditions, we follow order: |
| 186 |
|
| 187 |
1. Note lsn up to which all dirty pages have already been |
| 188 |
added to flush lists. |
| 189 |
|
| 190 |
2. Check buffer pool to get LWM lsn for unflushed dirty pages |
| 191 |
added to flush lists. |
| 192 |
|
| 193 |
3. Flush lists were empty (no LWM) => use [1] as LWM. |
| 194 |
|
| 195 |
4. Checkpoint LSN could be min(LWM, flushed_to_disk_lsn). */ |
| 196 |
|
| 197 |
log_sync_point("log_get_available_for_chkp_lsn_before_dpa"); |
| 198 |
|
| 199 |
const lsn_t dpa_lsn = log_buffer_dirty_pages_added_up_to_lsn(log); |
| 200 |
|
| 201 |
ut_ad(dpa_lsn >= log.last_checkpoint_lsn.load() || |
| 202 |
!log_checkpointer_mutex_own(log)); |
| 203 |
|
| 204 |
log_sync_point("log_get_available_for_chkp_lsn_before_buf_pool"); |
| 205 |
|
| 206 |
lsn_t lwm_lsn = buf_pool_get_oldest_modification_lwm(); |
| 207 |
|
| 208 |
/* We cannot return lsn larger than dpa_lsn, |
| 209 |
because some mtr's commit could be in the middle, after |
| 210 |
its log records have been written to log buffer, but before |
| 211 |
its dirty pages have been added to flush lists. */ |
| 212 |
|
| 213 |
if (lwm_lsn == 0) { |
| 214 |
/* Empty flush list. */ |
| 215 |
lwm_lsn = dpa_lsn; |
| 216 |
} else { |
| 217 |
lwm_lsn = std::min(lwm_lsn, dpa_lsn); |
| 218 |
} |
| 219 |
|
| 220 |
/* Cannot go beyond flushed lsn. |
| 221 |
|
| 222 |
We cannot write checkpoint at higher lsn than lsn up to which |
| 223 |
redo is flushed to disk. We must not wait for log writer/flusher |
| 224 |
in log_checkpoint(). Therefore we need to limit lsn for checkpoint. |
| 225 |
That's because we would risk a deadlock otherwise - because writer |
| 226 |
waits for advanced checkpoint, when it detected that there is no |
| 227 |
free space in log files. |
| 228 |
|
| 229 |
However, note that the deadlock would happen only if we created |
| 230 |
log records without dirty pages (during page flush we anyway wait |
| 231 |
for redo flushed up to page's newest_modification). */ |
| 232 |
|
| 233 |
const lsn_t flushed_lsn = log.flushed_to_disk_lsn.load(); |
| 234 |
|
| 235 |
lsn_t lsn = std::min(lwm_lsn, flushed_lsn); |
| 236 |
|
| 237 |
/* We expect in recovery that checkpoint_lsn is within data area |
| 238 |
of log block. In future we could get rid of this assumption, but |
| 239 |
we would need to ensure that recovery handles that properly. |
| 240 |
|
| 241 |
For that, we would better refactor log0recv.cc and separate two |
| 242 |
phases: |
| 243 |
1. Looking for the proper mtr boundary to start at (only parse). |
| 244 |
2. Actual parsing and applying changes. */ |
| 245 |
|
| 246 |
if (lsn % OS_FILE_LOG_BLOCK_SIZE == 0) { |
| 247 |
/* Do not make checkpoints at block boundary. |
| 248 |
|
| 249 |
We hopefully will get rid of this exception and allow |
| 250 |
recovery to start at arbitrary checkpoint value. */ |
| 251 |
lsn = lsn - OS_FILE_LOG_BLOCK_SIZE + LOG_BLOCK_HDR_SIZE; |
| 252 |
} |
| 253 |
|
| 254 |
ut_a(lsn % OS_FILE_LOG_BLOCK_SIZE >= LOG_BLOCK_HDR_SIZE); |
| 255 |
|
| 256 |
ut_a(lsn % OS_FILE_LOG_BLOCK_SIZE < |
| 257 |
OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE); |
| 258 |
|
| 259 |
lsn = std::max(lsn, log.last_checkpoint_lsn.load()); |
| 260 |
|
| 261 |
ut_ad(lsn >= log.last_checkpoint_lsn.load() || |
| 262 |
!log_checkpointer_mutex_own(log)); |
| 263 |
|
| 264 |
ut_a(lsn <= log.flushed_to_disk_lsn.load()); |
| 265 |
|
| 266 |
return lsn; |
| 267 |
} |
| 268 |
|
| 269 |
static void log_update_available_for_checkpoint_lsn(log_t &log) { |
| 270 |
/* Note: log.m_allow_checkpoints is set to true after recovery is finished, |
| 271 |
and changes gathered in recv_sys->metadata_recover are applied to dict_table_t |
| 272 |
objects; or in log_start() if recovery was not needed. We can't trust |
| 273 |
flush lists until recovery is finished, so we must not update lsn available |
| 274 |
for checkpoint (as update would be based on what we can see inside them). */ |
| 275 |
if (!log.m_allow_checkpoints.load(std::memory_order_acquire)) { |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
/* Update lsn available for checkpoint. */ |
| 280 |
log.recent_closed.advance_tail(); |
| 281 |
const lsn_t oldest_lsn = log_compute_available_for_checkpoint_lsn(log); |
| 282 |
|
| 283 |
log_limits_mutex_enter(log); |
| 284 |
|
| 285 |
/* 1. The oldest_lsn can decrease in case previously buffer pool flush |
| 286 |
lists were empty and now a new dirty page appeared, which causes |
| 287 |
a maximum delay of log.recent_closed_size being suddenly subtracted. |
| 288 |
|
| 289 |
2. Race between concurrent log_update_available_for_checkpoint_lsn is |
| 290 |
also possible. */ |
| 291 |
|
| 292 |
if (oldest_lsn > log.available_for_checkpoint_lsn) { |
| 293 |
log.available_for_checkpoint_lsn = oldest_lsn; |
| 294 |
} |
| 295 |
|
| 296 |
log_limits_mutex_exit(log); |
| 297 |
} |
| 298 |
|
| 299 |
/** @} */ |
| 300 |
|
| 301 |
/**************************************************/ /** |
| 302 |
|
| 303 |
@name Log - making checkpoints |
| 304 |
|
| 305 |
*******************************************************/ |
| 306 |
|
| 307 |
/** @{ */ |
| 308 |
|
| 309 |
void log_set_dict_max_allowed_checkpoint_lsn(log_t &log, lsn_t max_lsn) { |
| 310 |
log_limits_mutex_enter(log); |
| 311 |
log.dict_max_allowed_checkpoint_lsn = max_lsn; |
| 312 |
log_limits_mutex_exit(log); |
| 313 |
} |
| 314 |
|
| 315 |
static lsn_t log_determine_checkpoint_lsn(log_t &log) { |
| 316 |
ut_ad(log_checkpointer_mutex_own(log)); |
| 317 |
ut_ad(log.m_allow_checkpoints.load()); |
| 318 |
|
| 319 |
log_limits_mutex_enter(log); |
| 320 |
|
| 321 |
const lsn_t oldest_lsn = log.available_for_checkpoint_lsn; |
| 322 |
|
| 323 |
const lsn_t dict_lsn = log.dict_max_allowed_checkpoint_lsn; |
| 324 |
|
| 325 |
log_limits_mutex_exit(log); |
| 326 |
|
| 327 |
ut_a(dict_lsn == 0 || dict_lsn >= log.last_checkpoint_lsn.load()); |
| 328 |
|
| 329 |
if (dict_lsn == 0) { |
| 330 |
return oldest_lsn; |
| 331 |
} else { |
| 332 |
return std::min(oldest_lsn, dict_lsn); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
dberr_t log_files_next_checkpoint(log_t &log, lsn_t next_checkpoint_lsn) { |
| 337 |
ut_ad(log_checkpointer_mutex_own(log)); |
| 338 |
ut_a(!srv_read_only_mode); |
| 339 |
|
| 340 |
IB_mutex_guard writer_latch{&(log.writer_mutex), UT_LOCATION_HERE}; |
| 341 |
IB_mutex_guard files_latch{&(log.m_files_mutex), UT_LOCATION_HERE}; |
| 342 |
|
| 343 |
const auto next_file = log.m_files.find(next_checkpoint_lsn); |
| 344 |
ut_a(next_file != log.m_files.end()); |
| 345 |
|
| 346 |
auto next_file_handle = next_file->open(Log_file_access_mode::WRITE_ONLY); |
| 347 |
if (!next_file_handle.is_open()) { |
| 348 |
return DB_CANNOT_OPEN_FILE; |
| 349 |
} |
| 350 |
|
| 351 |
log_sync_point("log_before_checkpoint_write"); |
| 352 |
|
| 353 |
const lsn_t prev_checkpoint_lsn = log.last_checkpoint_lsn.load(); |
| 354 |
if (prev_checkpoint_lsn != 0) { |
| 355 |
const auto prev_file = log.m_files.find(prev_checkpoint_lsn); |
| 356 |
ut_a(prev_file != log.m_files.end()); |
| 357 |
|
| 358 |
if (prev_file->m_id != next_file->m_id) { |
| 359 |
/* Checkpoint is moved to the next log file. */ |
| 360 |
if (log_can_encrypt(*log_sys)) { |
| 361 |
/* Write the encryption header to the new checkpoint file. */ |
| 362 |
const dberr_t err = |
| 363 |
log_encryption_header_write(next_file_handle, log.m_encryption_buf); |
| 364 |
if (err != DB_SUCCESS) { |
| 365 |
return err; |
| 366 |
} |
| 367 |
} |
| 368 |
/* Wake up log_files_governor because it potentially might consume |
| 369 |
the previous log file (once we release the files_mutex). */ |
| 370 |
os_event_set(log.m_files_governor_event); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
const dberr_t err = log_files_write_checkpoint_low( |
| 375 |
log, next_file_handle, log.next_checkpoint_header_no, |
| 376 |
next_checkpoint_lsn); |
| 377 |
|
| 378 |
if (err != DB_SUCCESS) { |
| 379 |
return err; |
| 380 |
} |
| 381 |
|
| 382 |
log_sync_point("log_before_checkpoint_flush"); |
| 383 |
|
| 384 |
next_file_handle.fsync(); |
| 385 |
|
| 386 |
DBUG_PRINT("ib_log", ("checkpoint info written")); |
| 387 |
|
| 388 |
log.next_checkpoint_header_no = |
| 389 |
log_next_checkpoint_header(log.next_checkpoint_header_no); |
| 390 |
|
| 391 |
log_sync_point("log_before_checkpoint_lsn_update"); |
| 392 |
|
| 393 |
log.last_checkpoint_lsn.store(next_checkpoint_lsn); |
| 394 |
|
| 395 |
ut_a(!next_file->m_consumed); |
| 396 |
|
| 397 |
log_sync_point("log_before_checkpoint_limits_update"); |
| 398 |
|
| 399 |
log_limits_mutex_enter(log); |
| 400 |
log_update_limits_low(log); |
| 401 |
log_update_exported_variables(log); |
| 402 |
log.dict_max_allowed_checkpoint_lsn = 0; |
| 403 |
log_limits_mutex_exit(log); |
| 404 |
|
| 405 |
if (log.m_writer_inside_extra_margin) { |
| 406 |
log_writer_check_if_exited_extra_margin(log); |
| 407 |
} |
| 408 |
|
| 409 |
os_event_set(log.next_checkpoint_event); |
| 410 |
|
| 411 |
return DB_SUCCESS; |
| 412 |
} |
| 413 |
|
| 414 |
Log_checkpoint_header_no log_next_checkpoint_header( |
| 415 |
Log_checkpoint_header_no checkpoint_header_no) { |
| 416 |
switch (checkpoint_header_no) { |
| 417 |
case Log_checkpoint_header_no::HEADER_1: |
| 418 |
return Log_checkpoint_header_no::HEADER_2; |
| 419 |
case Log_checkpoint_header_no::HEADER_2: |
| 420 |
return Log_checkpoint_header_no::HEADER_1; |
| 421 |
default: |
| 422 |
ut_error; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
dberr_t log_files_write_checkpoint_low( |
| 427 |
log_t &log, Log_file_handle &checkpoint_file_handle, |
| 428 |
Log_checkpoint_header_no checkpoint_header_no, lsn_t checkpoint_lsn) { |
| 429 |
ut_ad(checkpoint_lsn == 0 || log_checkpointer_mutex_own(log)); |
| 430 |
ut_ad(log_writer_mutex_own(log)); |
| 431 |
ut_ad(srv_is_being_started || log_files_mutex_own(log)); |
| 432 |
ut_a(!srv_read_only_mode); |
| 433 |
|
| 434 |
DBUG_PRINT("ib_log", ("checkpoint at " LSN_PF " written", checkpoint_lsn)); |
| 435 |
|
| 436 |
Log_checkpoint_header checkpoint_header; |
| 437 |
checkpoint_header.m_checkpoint_lsn = checkpoint_lsn; |
| 438 |
|
| 439 |
return log_checkpoint_header_write(checkpoint_file_handle, |
| 440 |
checkpoint_header_no, checkpoint_header); |
| 441 |
} |
| 442 |
|
| 443 |
static void log_checkpoint(log_t &log) { |
| 444 |
ut_ad(log_checkpointer_mutex_own(log)); |
| 445 |
ut_a(!srv_read_only_mode); |
| 446 |
ut_ad(!srv_checkpoint_disabled); |
| 447 |
ut_ad(log.m_allow_checkpoints.load()); |
| 448 |
|
| 449 |
/* Read the comment from log_should_checkpoint() from just before |
| 450 |
acquiring the limits mutex. It is ok if available_for_checkpoint_lsn |
| 451 |
is advanced just after we released limits_mutex here. It can only be |
| 452 |
increaed. Also, if the value for which we will write checkpoint is |
| 453 |
higher than the value for which we decided that it is worth to write |
| 454 |
checkpoint (in log_should_checkpoint) - it is even better for us. */ |
| 455 |
|
| 456 |
const lsn_t checkpoint_lsn = log_determine_checkpoint_lsn(log); |
| 457 |
|
| 458 |
if (arch_page_sys != nullptr) { |
| 459 |
arch_page_sys->flush_at_checkpoint(checkpoint_lsn); |
| 460 |
} |
| 461 |
|
| 462 |
log_sync_point("log_before_checkpoint_data_flush"); |
| 463 |
|
| 464 |
buf_flush_fsync(); |
| 465 |
|
| 466 |
if (log_test != nullptr) { |
| 467 |
log_test->fsync_written_pages(); |
| 468 |
} |
| 469 |
|
| 470 |
ut_a(checkpoint_lsn >= log.last_checkpoint_lsn.load()); |
| 471 |
|
| 472 |
ut_a(checkpoint_lsn <= log_buffer_dirty_pages_added_up_to_lsn(log)); |
| 473 |
|
| 474 |
#ifdef UNIV_DEBUG |
| 475 |
if (checkpoint_lsn > log.flushed_to_disk_lsn.load()) { |
| 476 |
/* We need log_flusher, because we need redo flushed up |
| 477 |
to the oldest_lsn, and it's not been flushed yet. */ |
| 478 |
|
| 479 |
log_background_threads_active_validate(log); |
| 480 |
} |
| 481 |
#endif |
| 482 |
|
| 483 |
ut_a(log.flushed_to_disk_lsn.load() >= checkpoint_lsn); |
| 484 |
|
| 485 |
const auto current_time = std::chrono::high_resolution_clock::now(); |
| 486 |
|
| 487 |
log.last_checkpoint_time = current_time; |
| 488 |
|
| 489 |
DBUG_PRINT("ib_log", ("Starting checkpoint at " LSN_PF, checkpoint_lsn)); |
| 490 |
|
| 491 |
const dberr_t err = log_files_next_checkpoint(log, checkpoint_lsn); |
| 492 |
if (err != DB_SUCCESS) { |
| 493 |
return; |
| 494 |
} |
| 495 |
|
| 496 |
DBUG_PRINT("ib_log", |
| 497 |
("checkpoint ended at " LSN_PF ", log flushed to " LSN_PF, |
| 498 |
log.last_checkpoint_lsn.load(), log.flushed_to_disk_lsn.load())); |
| 499 |
|
| 500 |
MONITOR_INC(MONITOR_LOG_CHECKPOINTS); |
| 501 |
|
| 502 |
DBUG_EXECUTE_IF("crash_after_checkpoint", DBUG_SUICIDE();); |
| 503 |
} |
| 504 |
|
| 505 |
dberr_t log_files_write_first_data_block_low(log_t &log, |
| 506 |
Log_file_handle &file_handle, |
| 507 |
lsn_t checkpoint_lsn, |
| 508 |
lsn_t file_start_lsn) { |
| 509 |
ut_a(!srv_read_only_mode); |
| 510 |
ut_a(file_handle.is_open()); |
| 511 |
|
| 512 |
/* Create the first, empty log block. */ |
| 513 |
const lsn_t block_lsn = |
| 514 |
ut_uint64_align_down(checkpoint_lsn, OS_FILE_LOG_BLOCK_SIZE); |
| 515 |
|
| 516 |
const uint16_t data_end = checkpoint_lsn % OS_FILE_LOG_BLOCK_SIZE; |
| 517 |
|
| 518 |
/* Write the first empty log block to the log buffer. */ |
| 519 |
Log_data_block_header block_header; |
| 520 |
block_header.set_lsn(block_lsn); |
| 521 |
block_header.m_first_rec_group = block_header.m_data_len = data_end; |
| 522 |
|
| 523 |
byte block[OS_FILE_LOG_BLOCK_SIZE] = {}; |
| 524 |
log_data_block_header_serialize(block_header, block); |
| 525 |
|
| 526 |
std::memcpy(log.buf + block_lsn % log.buf_size, block, |
| 527 |
OS_FILE_LOG_BLOCK_SIZE); |
| 528 |
|
| 529 |
/* Write the first empty log block to the file. */ |
| 530 |
const os_offset_t block_offset = Log_file::offset(block_lsn, file_start_lsn); |
| 531 |
return log_data_blocks_write(file_handle, block_offset, |
| 532 |
OS_FILE_LOG_BLOCK_SIZE, block); |
| 533 |
} |
| 534 |
|
| 535 |
void log_request_checkpoint_low(log_t &log, lsn_t requested_lsn) { |
| 536 |
ut_a(requested_lsn <= log_get_lsn(log)); |
| 537 |
ut_ad(log_limits_mutex_own(log)); |
| 538 |
|
| 539 |
ut_a(requested_lsn % OS_FILE_LOG_BLOCK_SIZE >= LOG_BLOCK_HDR_SIZE); |
| 540 |
|
| 541 |
ut_a(requested_lsn % OS_FILE_LOG_BLOCK_SIZE < |
| 542 |
OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE); |
| 543 |
|
| 544 |
/* Update log.requested_checkpoint_lsn only to greater value. */ |
| 545 |
|
| 546 |
if (requested_lsn > log.requested_checkpoint_lsn) { |
| 547 |
log.requested_checkpoint_lsn = requested_lsn; |
| 548 |
|
| 549 |
if (requested_lsn > log.last_checkpoint_lsn.load()) { |
| 550 |
os_event_set(log.checkpointer_event); |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
static void log_wait_for_checkpoint(const log_t &log, lsn_t requested_lsn) { |
| 556 |
ut_d(log_background_threads_active_validate(log)); |
| 557 |
|
| 558 |
auto stop_condition = [&log, requested_lsn](bool) { |
| 559 |
return log.last_checkpoint_lsn.load() >= requested_lsn; |
| 560 |
}; |
| 561 |
|
| 562 |
ut::wait_for(0, std::chrono::microseconds{100}, stop_condition); |
| 563 |
} |
| 564 |
|
| 565 |
static bool log_request_checkpoint_validate(const log_t &log) { |
| 566 |
ut_ad(log_limits_mutex_own(log)); |
| 567 |
|
| 568 |
#ifdef UNIV_DEBUG |
| 569 |
if (srv_checkpoint_disabled) { |
| 570 |
/* Checkpoints are disabled. Pretend it succeeded. */ |
| 571 |
ib::info(ER_IB_MSG_1233) << "Checkpoint explicitly disabled!"; |
| 572 |
|
| 573 |
return false; |
| 574 |
} |
| 575 |
#endif /* UNIV_DEBUG */ |
| 576 |
|
| 577 |
return true; |
| 578 |
} |
| 579 |
|
| 580 |
void log_request_checkpoint(log_t &log, bool sync) { |
| 581 |
log_update_available_for_checkpoint_lsn(log); |
| 582 |
|
| 583 |
log_limits_mutex_enter(log); |
| 584 |
|
| 585 |
if (!log_request_checkpoint_validate(log)) { |
| 586 |
log_limits_mutex_exit(log); |
| 587 |
if (sync) { |
| 588 |
ut_error; |
| 589 |
} |
| 590 |
return; |
| 591 |
} |
| 592 |
|
| 593 |
const lsn_t lsn = log.available_for_checkpoint_lsn; |
| 594 |
|
| 595 |
log_request_checkpoint_low(log, lsn); |
| 596 |
|
| 597 |
log_limits_mutex_exit(log); |
| 598 |
|
| 599 |
if (sync) { |
| 600 |
log_wait_for_checkpoint(log, lsn); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
bool log_request_latest_checkpoint(log_t &log, lsn_t &requested_lsn) { |
| 605 |
const lsn_t lsn = log_get_lsn(log); |
| 606 |
|
| 607 |
if (lsn <= log.last_checkpoint_lsn.load()) { |
| 608 |
return false; |
| 609 |
} |
| 610 |
|
| 611 |
log_limits_mutex_enter(log); |
| 612 |
|
| 613 |
if (!log_request_checkpoint_validate(log)) { |
| 614 |
log_limits_mutex_exit(log); |
| 615 |
ut_error; |
| 616 |
} |
| 617 |
|
| 618 |
requested_lsn = lsn; |
| 619 |
|
| 620 |
log_request_checkpoint_low(log, requested_lsn); |
| 621 |
|
| 622 |
log_limits_mutex_exit(log); |
| 623 |
|
| 624 |
return true; |
| 625 |
} |
| 626 |
|
| 627 |
bool log_make_latest_checkpoint(log_t &log) { |
| 628 |
lsn_t lsn; |
| 629 |
if (!log_request_latest_checkpoint(log, lsn)) { |
| 630 |
return false; |
| 631 |
} |
| 632 |
|
| 633 |
log_wait_for_checkpoint(log, lsn); |
| 634 |
|
| 635 |
return true; |
| 636 |
} |
| 637 |
|
| 638 |
bool log_make_latest_checkpoint() { |
| 639 |
return log_make_latest_checkpoint(*log_sys); |
| 640 |
} |
| 641 |
|
| 642 |
static bool log_request_sync_flush(const log_t &log, lsn_t new_oldest) { |
| 643 |
if (log_test != nullptr) { |
| 644 |
return false; |
| 645 |
} |
| 646 |
|
| 647 |
/* A flush is urgent: we have to do a synchronous flush, |
| 648 |
because the oldest dirty page is too old. |
| 649 |
|
| 650 |
Note, that this could fire even if we did not run out |
| 651 |
of space in log files (users still may write to redo). */ |
| 652 |
|
| 653 |
if (new_oldest == LSN_MAX |
| 654 |
/* Forced flush request is processed by page_cleaner, if |
| 655 |
it's not active, then we must do flush ourselves. */ |
| 656 |
|| !buf_flush_page_cleaner_is_active() |
| 657 |
/* Reason unknown. */ |
| 658 |
|| srv_is_being_started) { |
| 659 |
buf_flush_sync_all_buf_pools(); |
| 660 |
|
| 661 |
return true; |
| 662 |
|
| 663 |
} else if (srv_flush_sync) { |
| 664 |
/* Wake up page cleaner asking to perform sync flush |
| 665 |
(unless user explicitly disabled sync-flushes). */ |
| 666 |
|
| 667 |
int64_t sig_count = os_event_reset(buf_flush_tick_event); |
| 668 |
|
| 669 |
os_event_set(buf_flush_event); |
| 670 |
|
| 671 |
/* Wait until flush is finished or timeout happens. This is to delay |
| 672 |
furious checkpoint writing when sync flush is active. However, if the |
| 673 |
log_writer entered its extra_margin, it's better to be more aggressive |
| 674 |
with checkpoint writing, because the problem very likely is related to |
| 675 |
missing log_free_check() calls and oldest dirt page being also the newest |
| 676 |
page that was modified and can't be flushed due to missing space in redo. |
| 677 |
In such case, it is very desired to move checkpoint forward even a little |
| 678 |
bit. If there is a sequence of such pages, then it becomes problematic and |
| 679 |
we would better not delay the checkpointing that much. |
| 680 |
|
| 681 |
The log.m_writer_inside_extra_margin is read without mutex protection for |
| 682 |
performance reasons (not to keep the mutex acquired when waiting below). |
| 683 |
In case of torn read or race, in the worst case we would use different |
| 684 |
timeout than the desired one. It doesn't affect correctness. */ |
| 685 |
|
| 686 |
const auto time_to_wait_ms = log.m_writer_inside_extra_margin ? 1 : 1000; |
| 687 |
|
| 688 |
os_event_wait_time_low(buf_flush_tick_event, |
| 689 |
std::chrono::milliseconds{time_to_wait_ms}, |
| 690 |
sig_count); |
| 691 |
|
| 692 |
return true; |
| 693 |
|
| 694 |
} else { |
| 695 |
return false; |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
lsn_t log_sync_flush_lsn(log_t &log) { |
| 700 |
/* Note: log.m_allow_checkpoints is set to true after recovery is finished, |
| 701 |
and changes gathered in recv_sys->metadata_recover are applied to dict_table_t |
| 702 |
objects; or in log_start() if recovery was not needed. Until that happens |
| 703 |
checkpoints are disallowed, so sync flush decisions (based on checkpoint age) |
| 704 |
should be postponed. */ |
| 705 |
if (!log.m_allow_checkpoints.load(std::memory_order_acquire)) { |
| 706 |
return 0; |
| 707 |
} |
| 708 |
|
| 709 |
log_update_available_for_checkpoint_lsn(log); |
| 710 |
|
| 711 |
/* We acquire limits mutex only for a short period. Afterwards these |
| 712 |
values might be changed (advanced to higher values). However, in the |
| 713 |
worst case we would request sync flush for too small value, and the |
| 714 |
function which requests the sync flush is safe to be used with any |
| 715 |
lsn value. It ensures itself that maximum of all requested lsn values |
| 716 |
is taken. In next iteration of log_checkpointer we would notice the |
| 717 |
higher values and re-request the sync flush if needed (or user threads |
| 718 |
waiting in log_free_check() would request it themselves meanwhile). */ |
| 719 |
|
| 720 |
log_limits_mutex_enter(log); |
| 721 |
const lsn_t oldest_lsn = log.available_for_checkpoint_lsn; |
| 722 |
const lsn_t requested_checkpoint_lsn = log.requested_checkpoint_lsn; |
| 723 |
log_limits_mutex_exit(log); |
| 724 |
|
| 725 |
lsn_t flush_up_to = oldest_lsn; |
| 726 |
|
| 727 |
lsn_t current_lsn = log_get_lsn(log); |
| 728 |
|
| 729 |
ut_a(flush_up_to <= current_lsn); |
| 730 |
|
| 731 |
if (current_lsn == flush_up_to) { |
| 732 |
return 0; |
| 733 |
} |
| 734 |
|
| 735 |
const lsn_t margin = log_free_check_margin(log); |
| 736 |
|
| 737 |
const lsn_t adaptive_flush_max_age = log.m_capacity.adaptive_flush_max_age(); |
| 738 |
|
| 739 |
if (current_lsn + margin - oldest_lsn > adaptive_flush_max_age) { |
| 740 |
ut_a(current_lsn + margin > adaptive_flush_max_age); |
| 741 |
|
| 742 |
flush_up_to = current_lsn + margin - adaptive_flush_max_age; |
| 743 |
} |
| 744 |
|
| 745 |
if (requested_checkpoint_lsn > flush_up_to) { |
| 746 |
flush_up_to = requested_checkpoint_lsn; |
| 747 |
} |
| 748 |
|
| 749 |
if (flush_up_to > current_lsn) { |
| 750 |
flush_up_to = current_lsn; |
| 751 |
} |
| 752 |
|
| 753 |
if (flush_up_to > oldest_lsn) { |
| 754 |
flush_up_to += log_buffer_flush_order_lag(log); |
| 755 |
|
| 756 |
return flush_up_to; |
| 757 |
} |
| 758 |
|
| 759 |
return 0; |
| 760 |
} |
| 761 |
|
| 762 |
static void log_consider_sync_flush(log_t &log) { |
| 763 |
ut_ad(log_checkpointer_mutex_own(log)); |
| 764 |
|
| 765 |
const auto flush_up_to = log_sync_flush_lsn(log); |
| 766 |
|
| 767 |
if (flush_up_to != 0) { |
| 768 |
log_checkpointer_mutex_exit(log); |
| 769 |
|
| 770 |
log_request_sync_flush(log, flush_up_to); |
| 771 |
|
| 772 |
log_checkpointer_mutex_enter(log); |
| 773 |
|
| 774 |
/* It's very probable that forced flush will result in maximum |
| 775 |
lsn available for creating a new checkpoint, just try to update |
| 776 |
it to not wait for next checkpointer loop. */ |
| 777 |
log_update_available_for_checkpoint_lsn(log); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
static std::chrono::steady_clock::duration log_checkpoint_time_elapsed( |
| 782 |
const log_t &log) { |
| 783 |
ut_ad(log_checkpointer_mutex_own(log)); |
| 784 |
|
| 785 |
return std::chrono::high_resolution_clock::now() - log.last_checkpoint_time; |
| 786 |
} |
| 787 |
|
| 788 |
static bool log_should_checkpoint(log_t &log) { |
| 789 |
lsn_t last_checkpoint_lsn; |
| 790 |
lsn_t oldest_lsn; |
| 791 |
lsn_t current_lsn; |
| 792 |
lsn_t requested_checkpoint_lsn; |
| 793 |
lsn_t checkpoint_age; |
| 794 |
bool periodical_checkpoints_enabled; |
| 795 |
|
| 796 |
ut_ad(log_checkpointer_mutex_own(log)); |
| 797 |
|
| 798 |
#ifdef UNIV_DEBUG |
| 799 |
if (srv_checkpoint_disabled) { |
| 800 |
return false; |
| 801 |
} |
| 802 |
#endif /* UNIV_DEBUG */ |
| 803 |
|
| 804 |
/* Note: log.allow_checkpoints is set to true after recovery is finished, |
| 805 |
and changes gathered in recv_sys->metadata_recover are applied to dict_table_t |
| 806 |
objects; or in log_start() if recovery was not needed. We can't reclaim |
| 807 |
free space in redo log until DD dynamic metadata records are safe. */ |
| 808 |
if (!log.m_allow_checkpoints.load(std::memory_order_acquire)) { |
| 809 |
return false; |
| 810 |
} |
| 811 |
|
| 812 |
last_checkpoint_lsn = log.last_checkpoint_lsn.load(); |
| 813 |
|
| 814 |
/* We read the values under log_limits_mutex and release the mutex. |
| 815 |
The values might be changed just afterwards and that's fine. Note, |
| 816 |
they can only become increased. Either we decided to write chkp on |
| 817 |
too small value or we did not decide and we could decide in next |
| 818 |
iteration of the thread's loop. The only risk is that checkpointer |
| 819 |
could go waiting on event and miss the signaled requirement to write |
| 820 |
checkpoint at higher lsn, which was requested just after we released |
| 821 |
the mutex. This is impossible, because we read sig_count of the event |
| 822 |
when we reset the event which happens before this point and then pass |
| 823 |
the sig_count to the function responsible for waiting. If sig_count |
| 824 |
is changed it means new notifications are there and we instantly start |
| 825 |
next iteration. The event is signaled under the limits_mutex in the |
| 826 |
same critical section in which requirements are updated. */ |
| 827 |
|
| 828 |
log_limits_mutex_enter(log); |
| 829 |
oldest_lsn = log.available_for_checkpoint_lsn; |
| 830 |
requested_checkpoint_lsn = log.requested_checkpoint_lsn; |
| 831 |
periodical_checkpoints_enabled = log.periodical_checkpoints_enabled; |
| 832 |
log_limits_mutex_exit(log); |
| 833 |
|
| 834 |
if (oldest_lsn <= last_checkpoint_lsn) { |
| 835 |
return false; |
| 836 |
} |
| 837 |
|
| 838 |
current_lsn = log_get_lsn(log); |
| 839 |
|
| 840 |
ut_a(last_checkpoint_lsn <= oldest_lsn); |
| 841 |
ut_a(oldest_lsn <= current_lsn); |
| 842 |
|
| 843 |
const lsn_t margin = log_free_check_margin(log); |
| 844 |
|
| 845 |
checkpoint_age = current_lsn + margin - last_checkpoint_lsn; |
| 846 |
|
| 847 |
/* Update checkpoint_lsn stored in header of log files if: |
| 848 |
a) periodical checkpoints are enabled and either more than 1s |
| 849 |
elapsed since the last checkpoint or checkpoint could be |
| 850 |
written in the next redo log file, |
| 851 |
b) or checkpoint age is greater than aggressive_checkpoint_min_age, |
| 852 |
c) or it was requested to have greater checkpoint_lsn, |
| 853 |
and oldest_lsn allows to satisfy the request. */ |
| 854 |
|
| 855 |
if ((last_checkpoint_lsn < requested_checkpoint_lsn && |
| 856 |
requested_checkpoint_lsn <= oldest_lsn) || |
| 857 |
checkpoint_age >= log.m_capacity.aggressive_checkpoint_min_age()) { |
| 858 |
return true; |
| 859 |
} |
| 860 |
|
| 861 |
DBUG_EXECUTE_IF("periodical_checkpoint_disabled", |
| 862 |
periodical_checkpoints_enabled = false;); |
| 863 |
|
| 864 |
if (!periodical_checkpoints_enabled) { |
| 865 |
return false; |
| 866 |
} |
| 867 |
|
| 868 |
/* Below is the check if a periodical checkpoint should be written. */ |
| 869 |
IB_mutex_guard files_lock{&log.m_files_mutex, UT_LOCATION_HERE}; |
| 870 |
|
| 871 |
const auto checkpoint_file = log.m_files.find(last_checkpoint_lsn); |
| 872 |
ut_a(checkpoint_file != log.m_files.end()); |
| 873 |
ut_a(!checkpoint_file->m_consumed); |
| 874 |
|
| 875 |
const auto checkpoint_time_elapsed = log_checkpoint_time_elapsed(log); |
| 876 |
|
| 877 |
ut_a(last_checkpoint_lsn < checkpoint_file->m_end_lsn); |
| 878 |
|
| 879 |
return checkpoint_time_elapsed >= get_srv_log_checkpoint_every() || |
| 880 |
checkpoint_file->m_end_lsn < oldest_lsn; |
| 881 |
} |
| 882 |
|
| 883 |
static void log_consider_checkpoint(log_t &log) { |
| 884 |
ut_ad(log_checkpointer_mutex_own(log)); |
| 885 |
|
| 886 |
if (!log_should_checkpoint(log)) { |
| 887 |
return; |
| 888 |
} |
| 889 |
|
| 890 |
/* It's clear that a new checkpoint should be written. |
| 891 |
So do write back the dynamic metadata. Since the checkpointer |
| 892 |
mutex is low-level one, it has to be released first. */ |
| 893 |
log_checkpointer_mutex_exit(log); |
| 894 |
|
| 895 |
if (log_test == nullptr) { |
| 896 |
dict_persist_to_dd_table_buffer(); |
| 897 |
} |
| 898 |
|
| 899 |
log_checkpointer_mutex_enter(log); |
| 900 |
|
| 901 |
/* We need to re-check if checkpoint should really be |
| 902 |
written, because we re-acquired the checkpointer_mutex. |
| 903 |
Some conditions could have changed - e.g. user could |
| 904 |
acquire the mutex and specify srv_checkpoint_disabled=T. |
| 905 |
Instead of trying to figure out which conditions could |
| 906 |
have changed, we follow a simple way and perform a full |
| 907 |
re-check of all conditions. */ |
| 908 |
if (!log_should_checkpoint(log)) { |
| 909 |
return; |
| 910 |
} |
| 911 |
|
| 912 |
log_checkpoint(log); |
| 913 |
} |
| 914 |
|
| 915 |
void log_checkpointer(log_t *log_ptr) { |
| 916 |
ut_a(log_ptr != nullptr); |
| 917 |
|
| 918 |
log_t &log = *log_ptr; |
| 919 |
|
| 920 |
ut_d(log.m_checkpointer_thd = create_internal_thd()); |
| 921 |
|
| 922 |
static const uint64_t log_busy_checkpoint_interval = |
| 923 |
7; /*SRV_MASTER_CHECKPOINT_INTERVAL*/ |
| 924 |
auto old_activity_count = srv_get_activity_count(); |
| 925 |
ulint error = OS_SYNC_TIME_EXCEEDED; |
| 926 |
|
| 927 |
for (;;) { |
| 928 |
log_checkpointer_mutex_enter(log); |
| 929 |
|
| 930 |
const auto sig_count = os_event_reset(log.checkpointer_event); |
| 931 |
const lsn_t requested_checkpoint_lsn = log.requested_checkpoint_lsn; |
| 932 |
|
| 933 |
bool system_is_busy = false; |
| 934 |
if (error == OS_SYNC_TIME_EXCEEDED && |
| 935 |
srv_check_activity(old_activity_count)) { |
| 936 |
old_activity_count = srv_get_activity_count(); |
| 937 |
/* system is busy. takes longer interval. */ |
| 938 |
system_is_busy = true; |
| 939 |
} |
| 940 |
|
| 941 |
if (error != OS_SYNC_TIME_EXCEEDED || !system_is_busy || |
| 942 |
requested_checkpoint_lsn > |
| 943 |
log.last_checkpoint_lsn.load(std::memory_order_acquire) || |
| 944 |
log_checkpoint_time_elapsed(log) >= |
| 945 |
log_busy_checkpoint_interval * get_srv_log_checkpoint_every()) { |
| 946 |
/* Consider flushing some dirty pages. */ |
| 947 |
log_consider_sync_flush(log); |
| 948 |
|
| 949 |
log_sync_point("log_checkpointer_before_consider_checkpoint"); |
| 950 |
|
| 951 |
/* Consider writing checkpoint. */ |
| 952 |
log_consider_checkpoint(log); |
| 953 |
} |
| 954 |
|
| 955 |
log_checkpointer_mutex_exit(log); |
| 956 |
|
| 957 |
if (requested_checkpoint_lsn > |
| 958 |
log.last_checkpoint_lsn.load(std::memory_order_relaxed)) { |
| 959 |
/* not satisfied. retry. */ |
| 960 |
error = 0; |
| 961 |
} else { |
| 962 |
error = os_event_wait_time_low(log.checkpointer_event, |
| 963 |
get_srv_log_checkpoint_every(), sig_count); |
| 964 |
} |
| 965 |
|
| 966 |
/* Check if we should close the thread. */ |
| 967 |
if (log.should_stop_threads.load()) { |
| 968 |
ut_ad(!log.writer_threads_paused.load()); |
| 969 |
if (!log_flusher_is_active() && !log_writer_is_active()) { |
| 970 |
lsn_t end_lsn = log.write_lsn.load(); |
| 971 |
|
| 972 |
ut_a(log_is_data_lsn(end_lsn)); |
| 973 |
ut_a(end_lsn == log.flushed_to_disk_lsn.load()); |
| 974 |
ut_a(end_lsn == log_buffer_ready_for_write_lsn(log)); |
| 975 |
|
| 976 |
ut_a(end_lsn >= log_buffer_dirty_pages_added_up_to_lsn(log)); |
| 977 |
|
| 978 |
if (log_buffer_dirty_pages_added_up_to_lsn(log) == end_lsn) { |
| 979 |
/* All confirmed reservations have been written |
| 980 |
to redo and all dirty pages related to those |
| 981 |
writes have been added to flush lists. |
| 982 |
|
| 983 |
However, there could be user threads, which are |
| 984 |
in the middle of log_buffer_reserve(), reserved |
| 985 |
range of sn values, but could not confirm. |
| 986 |
|
| 987 |
Note that because log_writer is already not alive, |
| 988 |
the only possible reason guaranteed by its death, |
| 989 |
is that there is x-lock at end_lsn, in which case |
| 990 |
end_lsn separates two regions in log buffer: |
| 991 |
completely full and completely empty. */ |
| 992 |
const lsn_t ready_lsn = log_buffer_ready_for_write_lsn(log); |
| 993 |
|
| 994 |
const lsn_t current_lsn = log_get_lsn(log); |
| 995 |
|
| 996 |
if (current_lsn > ready_lsn) { |
| 997 |
log.recent_written.validate_no_links(ready_lsn, current_lsn); |
| 998 |
log.recent_closed.validate_no_links(ready_lsn, current_lsn); |
| 999 |
} |
| 1000 |
|
| 1001 |
break; |
| 1002 |
} |
| 1003 |
/* We need to wait until remaining dirty pages |
| 1004 |
have been added. */ |
| 1005 |
} |
| 1006 |
/* We prefer to wait until all writing is done. */ |
| 1007 |
} |
| 1008 |
} |
| 1009 |
|
| 1010 |
ut_d(destroy_internal_thd(log.m_checkpointer_thd)); |
| 1011 |
} |
| 1012 |
|
| 1013 |
/** @} */ |
| 1014 |
|
| 1015 |
/**************************************************/ /** |
| 1016 |
|
| 1017 |
@name Log - free check |
| 1018 |
|
| 1019 |
*******************************************************/ |
| 1020 |
|
| 1021 |
/** @{ */ |
| 1022 |
|
| 1023 |
sn_t log_concurrency_margin(lsn_t log_capacity, bool &is_safe) { |
| 1024 |
/* Add number of background threads that might use mini-transactions |
| 1025 |
and modify pages (generating new redo records). */ |
| 1026 |
|
| 1027 |
/* NOTE: When srv_thread_concurrency = 0 (stands for unlimited thread |
| 1028 |
concurrency), we compute the concurrency margin only for the background |
| 1029 |
threads. There is no guarantee provided by the log_free_check calls then. */ |
| 1030 |
|
| 1031 |
const size_t max_total_threads = |
| 1032 |
srv_thread_concurrency + LOG_BACKGROUND_THREADS_USING_RW_MTRS; |
| 1033 |
|
| 1034 |
/* A thread, which keeps latches of the oldest dirty pages, might |
| 1035 |
need to finish its mini-transaction to unlock those pages and allow |
| 1036 |
to flush them and advance checkpoint (to reclaim free space in redo). |
| 1037 |
Therefore check of free space must be performed when thread is not |
| 1038 |
holding latches of pages (or other latches which prevent other threads, |
| 1039 |
waiting for such latches, from finishing their mini-transactions). |
| 1040 |
Ideally each thread should check for free space, when not holding any |
| 1041 |
latches, before it starts next mini-transaction. However, to mitigate |
| 1042 |
performance drawbacks, we decided that few (still, limited number) |
| 1043 |
mini-transactions could be executed between consecutive such checks. |
| 1044 |
Also, each mini-transaction needs to have limited space it might take |
| 1045 |
in the redo log. Thanks to that, capacity of redo reserved by single |
| 1046 |
thread between its consecutive checks of free space, is limited. |
| 1047 |
It is guaranteed not to exceed: |
| 1048 |
LOG_CHECKPOINT_FREE_PER_THREAD * UNIV_PAGE_SIZE. |
| 1049 |
@note The aforementioned checks of free space are handled by calls to |
| 1050 |
log_free_check(). */ |
| 1051 |
const auto margin_per_thread = |
| 1052 |
LOG_CHECKPOINT_FREE_PER_THREAD * UNIV_PAGE_SIZE; |
| 1053 |
|
| 1054 |
/* We have guarantee to have at most max_threads concurrent threads. |
| 1055 |
Each of them might need the free space reservation for itself, for |
| 1056 |
writes between checks (because in the worst case, they could all |
| 1057 |
check together there is enough space in the same time, before any |
| 1058 |
of them starts to commit any mini-transaction. |
| 1059 |
@note This mechanism works only if number of threads is really capped |
| 1060 |
by the provided value. However, there is currently no semaphore which |
| 1061 |
would ensure that the promise holds. What's more, we actually know that |
| 1062 |
it holds only when innodb_thread_concurrency is non-zero (stands for |
| 1063 |
limited concurrency). */ |
| 1064 |
sn_t margin = margin_per_thread * max_total_threads; |
| 1065 |
|
| 1066 |
/* Add margin for the log_files_governor, so it could safely use dummy |
| 1067 |
log records to fill up the current redo log file if needed (during resize). |
| 1068 |
@see LOG_FILES_DUMMY_INTAKE_SIZE */ |
| 1069 |
margin += LOG_FILES_DUMMY_INTAKE_SIZE; |
| 1070 |
|
| 1071 |
/* Add extra safety calculated from redo-size. This is yet another |
| 1072 |
"just in case", but being proportional to the total redo capacity. */ |
| 1073 |
margin += ut_uint64_align_down( |
| 1074 |
static_cast<lsn_t>(LOG_EXTRA_CONC_MARGIN_PCT / 100.0 * log_capacity), |
| 1075 |
OS_FILE_LOG_BLOCK_SIZE); |
| 1076 |
|
| 1077 |
/* If maximum number of concurrent threads is relatively big in comparison |
| 1078 |
to the total capacity of redo log, it might happen, that the concurrency |
| 1079 |
margin required to avoid deadlocks, is too big. In such case, we use smaller |
| 1080 |
margin and report that the margin is unsafe for current concurrency and redo |
| 1081 |
capacity. It's up to user to take required steps to protect from deadlock. */ |
| 1082 |
|
| 1083 |
const auto max_margin = log_translate_lsn_to_sn(ut_uint64_align_down( |
| 1084 |
log_capacity * |
| 1085 |
(LOG_CONCCURENCY_MARGIN_MAX_PCT + LOG_EXTRA_CONC_MARGIN_PCT) / 100.0, |
| 1086 |
OS_FILE_LOG_BLOCK_SIZE)); |
| 1087 |
|
| 1088 |
if (margin > max_margin) { |
| 1089 |
margin = max_margin; |
| 1090 |
is_safe = false; |
| 1091 |
} else { |
| 1092 |
is_safe = true; |
| 1093 |
} |
| 1094 |
|
| 1095 |
return margin; |
| 1096 |
} |
| 1097 |
|
| 1098 |
void log_update_concurrency_margin(log_t &log) { |
| 1099 |
ut_ad(srv_is_being_started || log_limits_mutex_own(log)); |
| 1100 |
|
| 1101 |
const lsn_t log_capacity = log.m_capacity.soft_logical_capacity(); |
| 1102 |
|
| 1103 |
bool is_safe; |
| 1104 |
const sn_t margin = log_concurrency_margin(log_capacity, is_safe); |
| 1105 |
|
| 1106 |
log.concurrency_margin.store(margin); |
| 1107 |
log.concurrency_margin_is_safe.store(is_safe); |
| 1108 |
|
| 1109 |
MONITOR_SET(MONITOR_LOG_CONCURRENCY_MARGIN, margin); |
| 1110 |
} |
| 1111 |
|
| 1112 |
void log_update_limits_low(log_t &log) { |
| 1113 |
ut_ad(srv_is_being_started || |
| 1114 |
(log_files_mutex_own(log) && log_limits_mutex_own(log))); |
| 1115 |
|
| 1116 |
log_update_concurrency_margin(log); |
| 1117 |
|
| 1118 |
if (log.m_writer_inside_extra_margin) { |
| 1119 |
/* Stop all new incoming user threads at safe place. */ |
| 1120 |
log.free_check_limit_lsn.store(0); |
| 1121 |
return; |
| 1122 |
} |
| 1123 |
|
| 1124 |
const lsn_t current_lsn = log_get_lsn(log); |
| 1125 |
const lsn_t log_capacity = log_free_check_capacity(log); |
| 1126 |
lsn_t oldest_needed_lsn; |
| 1127 |
auto consumer = log_consumer_get_oldest(log, oldest_needed_lsn); |
| 1128 |
|
| 1129 |
const lsn_t limit_lsn = oldest_needed_lsn + log_capacity; |
| 1130 |
|
| 1131 |
log.free_check_limit_lsn.store(limit_lsn); |
| 1132 |
|
| 1133 |
/* During the server start, we do not own the limits mutex and the only |
| 1134 |
consumer here is checkpointer thread. We can't call consumption_requested() |
| 1135 |
without the mutex, and even if we could, it is not permitted to advance the |
| 1136 |
checkpoint during recovery. So we skip the consumption_requested() in the |
| 1137 |
recovery part. */ |
| 1138 |
if (!srv_is_being_started && current_lsn > log.free_check_limit_lsn.load()) { |
| 1139 |
consumer->consumption_requested(current_lsn - log_capacity); |
| 1140 |
if (log.m_THREADS_WAITING_FOR_REDO_throttler.apply()) { |
| 1141 |
ib::log_warn(ER_IB_MSG_WAITING_ON_LAGGING_REDO_LOG_CONSUMER, |
| 1142 |
consumer->get_name().c_str(), ulonglong{oldest_needed_lsn}); |
| 1143 |
log_sync_point("threads_waiting_on_lagging_consumer"); |
| 1144 |
} |
| 1145 |
} |
| 1146 |
} |
| 1147 |
void log_set_dict_persist_margin(log_t &log, sn_t margin) { |
| 1148 |
log_files_mutex_enter(log); |
| 1149 |
log_limits_mutex_enter(log); |
| 1150 |
log.dict_persist_margin.store(margin); |
| 1151 |
log_update_limits_low(*log_sys); |
| 1152 |
log_limits_mutex_exit(log); |
| 1153 |
log_files_mutex_exit(log); |
| 1154 |
} |
| 1155 |
|
| 1156 |
lsn_t log_free_check_margin(const log_t &log) { |
| 1157 |
sn_t margins = log.concurrency_margin.load(); |
| 1158 |
|
| 1159 |
margins += log.dict_persist_margin.load(); |
| 1160 |
|
| 1161 |
return log_translate_sn_to_lsn(margins); |
| 1162 |
} |
| 1163 |
|
| 1164 |
lsn_t log_free_check_capacity(const log_t &log, lsn_t free_check_margin) { |
| 1165 |
ut_ad(srv_is_being_started || log_limits_mutex_own(log)); |
| 1166 |
const lsn_t soft_logical_capacity = log.m_capacity.soft_logical_capacity(); |
| 1167 |
ut_a(free_check_margin < soft_logical_capacity); |
| 1168 |
return ut_uint64_align_down(soft_logical_capacity - free_check_margin, |
| 1169 |
OS_FILE_LOG_BLOCK_SIZE); |
| 1170 |
} |
| 1171 |
|
| 1172 |
lsn_t log_free_check_capacity(const log_t &log) { |
| 1173 |
return log_free_check_capacity(log, log_free_check_margin(log)); |
| 1174 |
} |
| 1175 |
|
| 1176 |
void log_free_check_wait(log_t &log) { |
| 1177 |
DBUG_EXECUTE_IF("log_free_check_skip", return;); |
| 1178 |
|
| 1179 |
const lsn_t current_lsn = log_get_lsn(log); |
| 1180 |
|
| 1181 |
/* We are not sure here if the Log Checkpointer is the most lagging consumer, |
| 1182 |
but checking that requires acquiring two mutexes, and is a job of |
| 1183 |
log_update_limits_low(), which is only executed infrequently from a few |
| 1184 |
background threads, to not cause congestion. Waking up Log Checkpointer |
| 1185 |
spuriously is not a big problem - it will check there's nothing to do and go |
| 1186 |
to sleep again. However, if the Log Checkpointer actually is the one blocking |
| 1187 |
everyone, then the sooner we wake it up, the better, so we don't want to wait |
| 1188 |
for background threads to do it. */ |
| 1189 |
os_event_set(log.checkpointer_event); |
| 1190 |
auto stop_condition = [&log, current_lsn](bool) { |
| 1191 |
return current_lsn <= log.free_check_limit_lsn.load(); |
| 1192 |
}; |
| 1193 |
|
| 1194 |
const auto wait_stats = |
| 1195 |
ut::wait_for(0, std::chrono::microseconds{100}, stop_condition); |
| 1196 |
|
| 1197 |
MONITOR_INC_WAIT_STATS(MONITOR_LOG_ON_FILE_SPACE_, wait_stats); |
| 1198 |
} |
| 1199 |
|
| 1200 |
#ifdef UNIV_DEBUG |
| 1201 |
void log_free_check_validate() { |
| 1202 |
/* This function may be called while holding some latches. This is OK, |
| 1203 |
as long as we are not holding any latches on buffer blocks or file spaces. |
| 1204 |
The following latches are not held by any thread that frees up redo log |
| 1205 |
space. */ |
| 1206 |
static const latch_level_t latches[] = { |
| 1207 |
SYNC_NO_ORDER_CHECK, /* used for non-labeled latches */ |
| 1208 |
SYNC_RSEGS, /* rsegs->x_lock in trx_rseg_create() */ |
| 1209 |
SYNC_UNDO_DDL, /* undo::ddl_mutex */ |
| 1210 |
SYNC_UNDO_SPACES, /* undo::spaces::m_latch */ |
| 1211 |
SYNC_FTS_CACHE, /* fts_cache_t::lock */ |
| 1212 |
SYNC_DICT, /* dict_sys->mutex in commit_try_rebuild() */ |
| 1213 |
SYNC_DICT_OPERATION, /* X-latch in commit_try_rebuild() */ |
| 1214 |
SYNC_INDEX_TREE /* index->lock */ |
| 1215 |
}; |
| 1216 |
|
| 1217 |
sync_allowed_latches check(latches, |
| 1218 |
latches + sizeof(latches) / sizeof(*latches)); |
| 1219 |
|
| 1220 |
if (sync_check_iterate(check)) { |
| 1221 |
#ifndef UNIV_NO_ERR_MSGS |
| 1222 |
ib::error(ER_IB_MSG_1381) |
| 1223 |
#else |
| 1224 |
ib::error() |
| 1225 |
#endif |
| 1226 |
<< "log_free_check() was called while holding an un-listed latch."; |
| 1227 |
ut_error; |
| 1228 |
} |
| 1229 |
mtr_t::check_my_thread_mtrs_are_not_latching(); |
| 1230 |
} |
| 1231 |
#endif /* !UNIV_DEBUG */ |
| 1232 |
|
| 1233 |
/** @} */ |
| 1234 |
|
| 1235 |
#endif /* !UNIV_HOTBACKUP */ |