| 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_governor.cc |
| 30 |
|
| 31 |
Redo log files - governor. |
| 32 |
|
| 33 |
This module contains functions, which are useful during startup, for: |
| 34 |
-# redo log files creation / deletion, |
| 35 |
-# initialization on existing set of redo log files. |
| 36 |
|
| 37 |
However, the major part of this module is the log_files_governor's |
| 38 |
implementation. This thread is fully responsible for: |
| 39 |
-# cooperation with the set of redo log consumers, including: |
| 40 |
-# finding out the oldest redo log consumer |
| 41 |
-# determining the oldest needed lsn (files ending at smaller lsn |
| 42 |
might be consumed), |
| 43 |
-# rushing the oldest redo log consumer when it's lagging too much, |
| 44 |
-# consumption of the oldest redo log files, including: |
| 45 |
-# finding the files that might be consumed, |
| 46 |
-# deciding if consumption is really needed or might be postponed, |
| 47 |
-# deciding if consumed redo log files should be recycled or removed, |
| 48 |
-# removing or renaming the redo log files, |
| 49 |
-# all updates of log.m_capacity object (@see Log_files_capacity), |
| 50 |
-# supervising pending redo resizes, helping to finish them when needed, by: |
| 51 |
-# truncating the newest redo log file if its end is too far away, |
| 52 |
-# writing dummy redo records to complete the file if intake is too slow, |
| 53 |
-# managing the set of unused redo log files, including: |
| 54 |
-# resizing them if needed, |
| 55 |
-# creating a spare file ahead, |
| 56 |
-# recycling the consumed files, |
| 57 |
-# deleting the consumed files if needed. |
| 58 |
|
| 59 |
This module is responsible for managing redo log files on disk and keeping them |
| 60 |
in-sync with in-memory data structures: log.m_files, log.m_encryption_metadata. |
| 61 |
|
| 62 |
*******************************************************/ |
| 63 |
|
| 64 |
/* std::for_each */ |
| 65 |
#include <algorithm> |
| 66 |
|
| 67 |
/* log_buffer_flush_to_disk */ |
| 68 |
#include "log0buf.h" |
| 69 |
|
| 70 |
/* log_files_write_first_data_block_low, log_files_write_checkpoint_low */ |
| 71 |
#include "log0chkp.h" |
| 72 |
|
| 73 |
/* log_encryption_generate_metadata */ |
| 74 |
#include "log0encryption.h" |
| 75 |
|
| 76 |
/* log.m_capacity, ... */ |
| 77 |
#include "log0files_capacity.h" |
| 78 |
|
| 79 |
/* log_create_unused_file, ... */ |
| 80 |
#include "log0files_io.h" |
| 81 |
|
| 82 |
#include "log0files_governor.h" |
| 83 |
|
| 84 |
/* log_persist_initialized, ... */ |
| 85 |
#include "log0log.h" |
| 86 |
|
| 87 |
/* log_t::X */ |
| 88 |
#include "log0sys.h" |
| 89 |
|
| 90 |
/* log_sync_point, srv_is_being_started */ |
| 91 |
#include "log0test.h" |
| 92 |
|
| 93 |
/* LOG_N_FILES, ... */ |
| 94 |
#include "log0types.h" |
| 95 |
|
| 96 |
/* log_writer_mutex_own */ |
| 97 |
#include "log0write.h" |
| 98 |
|
| 99 |
/* os_offset_t */ |
| 100 |
#include "os0file.h" |
| 101 |
|
| 102 |
#ifdef UNIV_DEBUG |
| 103 |
|
| 104 |
/* current_thd */ |
| 105 |
#include "sql/current_thd.h" |
| 106 |
|
| 107 |
/* debug_sync_set_action */ |
| 108 |
#include "sql/debug_sync.h" |
| 109 |
|
| 110 |
#endif /* UNIV_DEBUG */ |
| 111 |
|
| 112 |
/* create_internal_thd, destroy_internal_thd */ |
| 113 |
#include "sql/sql_thd_internal_api.h" |
| 114 |
|
| 115 |
/* srv_read_only_mode */ |
| 116 |
#include "srv0srv.h" |
| 117 |
|
| 118 |
/* RECOVERY_CRASH, ... */ |
| 119 |
#include "srv0start.h" |
| 120 |
|
| 121 |
/* ut_uint64_align_down */ |
| 122 |
#include "ut0byte.h" |
| 123 |
|
| 124 |
/* ut::set<Log_file_id>, ut::vector */ |
| 125 |
#include "ut0new.h" |
| 126 |
|
| 127 |
#ifndef UNIV_HOTBACKUP |
| 128 |
|
| 129 |
/**************************************************/ /** |
| 130 |
|
| 131 |
@name Log - files management. |
| 132 |
|
| 133 |
*******************************************************/ |
| 134 |
|
| 135 |
/** @{ */ |
| 136 |
|
| 137 |
/** Validates that new log files might be created. */ |
| 138 |
static void log_files_create_allowed_validate(); |
| 139 |
|
| 140 |
/** Validates that log.m_files might be accessed from the current thread. |
| 141 |
|
| 142 |
Validates that log.m_files_mutex is acquired unless srv_is_being_stated is true. |
| 143 |
|
| 144 |
@param[in] log redo log */ |
| 145 |
static void log_files_access_allowed_validate(const log_t &log); |
| 146 |
|
| 147 |
/** Validates that log.m_files might be accessed from the current thread |
| 148 |
and the current thread is allowed to perform write IO for redo log files, |
| 149 |
or create / remove / rename the existing redo log files. |
| 150 |
|
| 151 |
Validates that both log.m_files_mutex and log.writer_mutex are acquired |
| 152 |
unless srv_is_being_stated is true. |
| 153 |
|
| 154 |
@param[in] log redo log */ |
| 155 |
static void log_files_write_allowed_validate(const log_t &log); |
| 156 |
|
| 157 |
/** Prepares a log file header according to: |
| 158 |
- meta data provided by parameters (file_start_lsn), |
| 159 |
- and fields: log.m_format, log.m_creator_name, log.m_log_flags, |
| 160 |
log.m_log_uuid. |
| 161 |
|
| 162 |
Requirement: log.m_files_mutex acquired before calling this function |
| 163 |
(unless srv_is_being_started). |
| 164 |
|
| 165 |
@param[in] log redo log |
| 166 |
@param[in] file_start_lsn start lsn of the file |
| 167 |
@return the prepared log file header */ |
| 168 |
static Log_file_header log_files_prepare_header(const log_t &log, |
| 169 |
lsn_t file_start_lsn); |
| 170 |
|
| 171 |
/** Prepares a log file header according to: |
| 172 |
- meta data currently stored for the given log file in log.m_files, |
| 173 |
- and fields: log.m_format, log.m_creator_name, log.m_log_flags, |
| 174 |
log.m_log_uuid. |
| 175 |
|
| 176 |
Requirement: log.m_files_mutex acquired before calling this function |
| 177 |
(unless srv_is_being_started). |
| 178 |
|
| 179 |
@param[in] log redo log |
| 180 |
@param[in] file log file for which header is prepared |
| 181 |
@return the prepared log file header */ |
| 182 |
static Log_file_header log_files_prepare_header(const log_t &log, |
| 183 |
const Log_file &file); |
| 184 |
|
| 185 |
/** Asserts that all log files with id greater or equal to id of the file |
| 186 |
containing the oldest lsn, have not been consumed / marked for consumption. |
| 187 |
|
| 188 |
Requirement: log.m_files_mutex acquired before calling this function |
| 189 |
(unless srv_is_being_started). |
| 190 |
|
| 191 |
@param[in] log redo log */ |
| 192 |
static void log_files_validate_not_consumed(const log_t &log); |
| 193 |
|
| 194 |
/** Asserts that the current file exists and contains |
| 195 |
the log_files_newest_needed_lsn(log). |
| 196 |
|
| 197 |
Requirement: log.m_files_mutex acquired before calling this function |
| 198 |
(unless srv_is_being_started). |
| 199 |
|
| 200 |
@param[in] log redo log */ |
| 201 |
static void log_files_validate_current_file(const log_t &log); |
| 202 |
|
| 203 |
/** Creates a new redo log file and resizes the file to the size returned by |
| 204 |
log.m_capacity.next_file_size(). If checkpoint_lsn != 0, it also must hold: |
| 205 |
checkpoint_lsn >= start_lsn. The checkpoint information is stored to both |
| 206 |
checkpoint headers of the new file. When create_first_data_block is true, |
| 207 |
the first data block is modified also in the log buffer and written to disk |
| 208 |
- that can only be used when there are no concurrent writes to the log buffer. |
| 209 |
|
| 210 |
Requirement: log.m_files_mutex and log.writer_mutex acquired before calling |
| 211 |
this function (unless srv_is_being_started). |
| 212 |
|
| 213 |
@param[in,out] log redo log |
| 214 |
@param[in] file_id id of the log file to create |
| 215 |
@param[in] start_lsn start_lsn for the log file |
| 216 |
@param[in] checkpoint_lsn lsn of the current checkpoint if it |
| 217 |
is located in the file, or zero |
| 218 |
@param[in] create_first_data_block true iff should format the first data |
| 219 |
block in log.buf and write to the file |
| 220 |
@return DB_SUCCESS or error code */ |
| 221 |
static dberr_t log_files_create_file(log_t &log, Log_file_id file_id, |
| 222 |
lsn_t start_lsn, lsn_t checkpoint_lsn, |
| 223 |
bool create_first_data_block); |
| 224 |
|
| 225 |
/** Callback type for callbacks called for a provided log file header. |
| 226 |
@param[in] file_id id of the log file which owns the header |
| 227 |
@param[in,out] file_header initially filled header of redo file, |
| 228 |
accordingly to its currently known meta data; |
| 229 |
should be updated by callback if needed */ |
| 230 |
typedef std::function<void(Log_file_id file_id, Log_file_header &file_header)> |
| 231 |
Log_file_header_callback; |
| 232 |
|
| 233 |
/** Rewrites header of each of log files except the header of the newest file. |
| 234 |
|
| 235 |
Requirement: log.m_files_mutex and log.writer_mutex acquired before calling |
| 236 |
this function (unless srv_is_being_started). |
| 237 |
|
| 238 |
@param[in,out] log redo log |
| 239 |
@param[in] before_write callback which can modify the prepared header, |
| 240 |
before the write |
| 241 |
@param[in] after_write callback called after the successful write |
| 242 |
@return DB_SUCCESS or error (if writing to disk failed) */ |
| 243 |
static dberr_t log_files_rewrite_old_headers( |
| 244 |
log_t &log, Log_file_header_callback before_write, |
| 245 |
Log_file_header_callback after_write); |
| 246 |
|
| 247 |
/** Rewrites header of the newest log file, preparing it accordingly to the |
| 248 |
current metadata of the file, which is stored in memory (in log.m_files). |
| 249 |
|
| 250 |
Requirement: log.m_files_mutex and log.writer_mutex acquired before calling |
| 251 |
this function (unless srv_is_being_started). |
| 252 |
|
| 253 |
@param[in,out] log redo log |
| 254 |
@param[in] cbk callback which can modify the prepared log file header, |
| 255 |
before the write to disk |
| 256 |
@return DB_SUCCESS or error (if writing to disk failed) */ |
| 257 |
static dberr_t log_files_rewrite_newest_header(log_t &log, |
| 258 |
Log_file_header_callback cbk); |
| 259 |
|
| 260 |
/** Rewrites header of the current log file (log.m_current_file) marking the |
| 261 |
file as not full in log_flags stored in the header. Updates metadata stored |
| 262 |
in log.m_files and log.m_current_file for that file. |
| 263 |
|
| 264 |
@remarks This is required to be called just before InnoDB can remove log files |
| 265 |
from future (which it does when recovery didn't advance up to the last file). |
| 266 |
|
| 267 |
Requirement: log.m_files_mutex and log.writer_mutex acquired before calling |
| 268 |
this function (unless srv_is_being_started). |
| 269 |
|
| 270 |
@param[in,out] log redo log |
| 271 |
@return DB_SUCCESS or error (if writing to disk failed) */ |
| 272 |
static dberr_t log_files_mark_current_file_as_incomplete(log_t &log); |
| 273 |
|
| 274 |
/** Rewrites header of the current log file (log.m_current_file) marking the |
| 275 |
file as full in log_flags stored in the header. Updates metadata stored in |
| 276 |
log.m_files and log.m_current_file for that file. |
| 277 |
|
| 278 |
Requirement: log.m_files_mutex and log.writer_mutex acquired before calling |
| 279 |
this function (unless srv_is_being_started). |
| 280 |
|
| 281 |
@param[in,out] log redo log |
| 282 |
@return DB_SUCCESS or error (if writing to disk failed) */ |
| 283 |
static dberr_t log_files_mark_current_file_as_full(log_t &log); |
| 284 |
|
| 285 |
/** Updates log.m_current_file and (re-)opens that file (therefore also the |
| 286 |
log.m_current_file_handle becomes updated). |
| 287 |
|
| 288 |
Requirement: log.m_files_mutex, log.writer_mutex and log.flusher_mutex acquired |
| 289 |
before calling this function (unless srv_is_being_started). |
| 290 |
|
| 291 |
@param[in,out] log redo log */ |
| 292 |
static void log_files_update_current_file_low(log_t &log); |
| 293 |
|
| 294 |
/** Finds the oldest LSN required by some of currently registered log consumers. |
| 295 |
This is based on log_consumer_get_oldest(). |
| 296 |
|
| 297 |
Requirement: log.m_files_mutex acquired before calling this function |
| 298 |
(unless srv_is_being_started). |
| 299 |
|
| 300 |
@param[in] log redo log |
| 301 |
@return maximum lsn up to which all log consumers have consumed the redo log */ |
| 302 |
static lsn_t log_files_oldest_needed_lsn(const log_t &log); |
| 303 |
|
| 304 |
/** Finds the newest LSN which potentially might be interesting for currently |
| 305 |
registered log consumers. |
| 306 |
|
| 307 |
@remarks This is currently simply returning the log.write_lsn, but to preserve |
| 308 |
the design clean this function exists as a complementary to the |
| 309 |
@see log_files_oldest_needed_lsn(). |
| 310 |
|
| 311 |
Requirement: log.m_files_mutex acquired before calling this function |
| 312 |
(unless srv_is_being_started). |
| 313 |
|
| 314 |
@param[in] log redo log |
| 315 |
@return the newest LSN (log.write_lsn) */ |
| 316 |
static lsn_t log_files_newest_needed_lsn(const log_t &log); |
| 317 |
|
| 318 |
/** Removes redo log files for LSN ranges (from future) with m_start_lsn larger |
| 319 |
than log_files_newest_needed_lsn(log). The files are removed from disk and from |
| 320 |
the log.m_files. |
| 321 |
|
| 322 |
Requirement: log.m_files_mutex and log.writer_mutex acquired before calling |
| 323 |
this function (unless srv_is_being_started). |
| 324 |
|
| 325 |
@param[in,out] log redo log |
| 326 |
@return DB_SUCCESS or error (if rewriting header of the current file failed) */ |
| 327 |
static dberr_t log_files_remove_from_future(log_t &log); |
| 328 |
|
| 329 |
/** Removes the file from disk and after that from log.m_files. |
| 330 |
|
| 331 |
Requirement: log.m_files_mutex and log.writer_mutex acquired before calling |
| 332 |
this function (unless srv_is_being_started). |
| 333 |
|
| 334 |
@param[in,out] log redo log |
| 335 |
@param[in] file_id id of the log file to remove |
| 336 |
@return DB_SUCCESS or error */ |
| 337 |
static dberr_t log_files_remove_file(log_t &log, Log_file_id file_id); |
| 338 |
|
| 339 |
/** Computes current logical size of the redo log and the minimum suggested |
| 340 |
soft (excluding extra_margin) logical capacity for the current redo log data. |
| 341 |
Note that this two values could be based on different redo log consumers, |
| 342 |
because different redo log consumers might be related to different margins |
| 343 |
preserved between size and capacity. |
| 344 |
|
| 345 |
Requirement: log.m_files_mutex acquired before calling this function |
| 346 |
(unless srv_is_being_started). |
| 347 |
|
| 348 |
@param[in] log redo log |
| 349 |
@return pair of logical redo log size and suggesed logical soft capacity */ |
| 350 |
static std::pair<lsn_t, lsn_t> log_files_logical_size_and_checkpoint_age( |
| 351 |
const log_t &log); |
| 352 |
|
| 353 |
/** Provides size for the next redo log file that will be created, unless |
| 354 |
there is not enough free space in current physical capacity, in which case |
| 355 |
it returns zero. If the zero was returned, then it's guaranteed that either: |
| 356 |
- some consumed log files might be removed or recycled, |
| 357 |
- or some log files might become consumed and then removed or recycled. |
| 358 |
These actions are executed by the log_files_governor thread, and that will |
| 359 |
certainly lead to state in which log_files_next_file_size() returns non-zero, |
| 360 |
because that's the property guaranteed by the strategy in Log_files_capacity. |
| 361 |
Therefore for other threads, which are waiting on this function to return non |
| 362 |
zero value, it's enough to wait. However, while waiting for that, these threads |
| 363 |
must not hold m_files_mutex. This function cannot wait itself, because it needs |
| 364 |
m_files_mutex prior to be called and guarantees not to release the mutex itself. |
| 365 |
|
| 366 |
@remarks This function should be used only in scenarios in which it is important |
| 367 |
if there is free space to create the next redo log file. In scenarios which do |
| 368 |
not care about that, the log.m_capacity.next_file_size() should be used instead. |
| 369 |
|
| 370 |
Requirement: log.m_files_mutex acquired before calling this function |
| 371 |
(unless srv_is_being_started). |
| 372 |
|
| 373 |
@param[in] log redo log |
| 374 |
@return size of next redo log file or 0 if it cannot be created */ |
| 375 |
static os_offset_t log_files_next_file_size(const log_t &log); |
| 376 |
|
| 377 |
/** Updates capacity limitations (log.m_capacity.* and log.free_check_sn). |
| 378 |
If redo resize was in progress, and conditions to consider it finished |
| 379 |
became satisfied, this call would result in marking the resize as done. |
| 380 |
|
| 381 |
Requirement: log.m_files_mutex acquired before calling this function |
| 382 |
(unless srv_is_being_started). |
| 383 |
|
| 384 |
@param[in,out] log redo log */ |
| 385 |
static void log_files_update_capacity_limits(log_t &log); |
| 386 |
|
| 387 |
/** Generates at least a given bytes of intake to the redo log. |
| 388 |
|
| 389 |
Requirement: none of log.m_files_mutex, log.writer_mutex, log.flusher_mutex, |
| 390 |
log.checkpointer_mutex is acquired when the function is called. |
| 391 |
|
| 392 |
Requirement: the log_files_governor thread still hasn't promised not to |
| 393 |
generate dummy redo records (!log.m_no_more_dummy_records_promised). |
| 394 |
|
| 395 |
@param[in,out] log redo log |
| 396 |
@param[in] min_size min number of bytes to write to the log buffer */ |
| 397 |
static void log_files_generate_dummy_records(log_t &log, lsn_t min_size); |
| 398 |
|
| 399 |
/** Checks if redo log file truncation is allowed. It is guaranteed that |
| 400 |
the conditions checked by this function are based on properties guarded |
| 401 |
by log.m_files_mutex. Note that this function checks only for allowance. |
| 402 |
It does not check if truncate is recommended to be done. |
| 403 |
|
| 404 |
@remarks This function currently checks if there is just a single redo log |
| 405 |
consumer (and log_checkpointer is expected to be that one) in which case |
| 406 |
(and only then) the truncation is allowed. |
| 407 |
|
| 408 |
Requirement: log.m_files_mutex acquired before calling this function |
| 409 |
(unless srv_is_being_started). |
| 410 |
|
| 411 |
@param[in] log redo log |
| 412 |
@return true iff redo log file truncation is allowed */ |
| 413 |
static bool log_files_is_truncate_allowed(const log_t &log); |
| 414 |
|
| 415 |
/** Truncates the redo log file. It must be called when there is exactly |
| 416 |
one redo log file. |
| 417 |
|
| 418 |
Requirement: log.m_files_mutex and log.writer_mutex acquired before calling |
| 419 |
this function (unless srv_is_being_started) and log_files_is_truncate_allowed() |
| 420 |
must be allowing to do the truncation and there must exist exactly one redo |
| 421 |
log file. |
| 422 |
|
| 423 |
@param[in,out] log redo log */ |
| 424 |
static void log_files_truncate(log_t &log); |
| 425 |
|
| 426 |
/** Marks the given file as consumed by the registered redo log consumers. |
| 427 |
It allows to recycle or remove the file later, when |
| 428 |
log_files_process_consumed_files() is called. |
| 429 |
|
| 430 |
Requirement: log.m_files_mutex acquired before calling this function |
| 431 |
(unless srv_is_being_started). |
| 432 |
|
| 433 |
@param[in,out] log redo log |
| 434 |
@param[in] file_id id of the file which should be marked as consumed */ |
| 435 |
static void log_files_mark_consumed_file(log_t &log, Log_file_id file_id); |
| 436 |
|
| 437 |
/** Marks each redo log file which became consumed by all registered redo log |
| 438 |
consumers, as consumed. It allows to recycle or remove those files later, |
| 439 |
when log_files_process_consumed_files() is called. |
| 440 |
|
| 441 |
Requirement: log.m_files_mutex acquired before calling this function |
| 442 |
(unless srv_is_being_started). |
| 443 |
|
| 444 |
@param[in,out] log redo log */ |
| 445 |
static void log_files_mark_consumed_files(log_t &log); |
| 446 |
|
| 447 |
/** Removes the given consumed redo log file. This is called when the redo log |
| 448 |
files marked as consumed are being processed (recycled or removed). |
| 449 |
|
| 450 |
Requirement: log.m_files_mutex and log.writer_mutex acquired before calling |
| 451 |
this function (unless srv_is_being_started) and the given file must have been |
| 452 |
marked as consumed before calling this function. |
| 453 |
|
| 454 |
@param[in,out] log redo log |
| 455 |
@param[in] file_id id of the file which should be removed |
| 456 |
@retval true removed successfully |
| 457 |
@retval false failed to remove (disk error) */ |
| 458 |
static bool log_files_remove_consumed_file(log_t &log, Log_file_id file_id); |
| 459 |
|
| 460 |
/** Recycles the given consumed redo log file. This is called when the redo log |
| 461 |
files marked as consumed are being processed (recycled or removed). The file |
| 462 |
becomes renamed and joins the set of unused (spare) redo log files. |
| 463 |
|
| 464 |
Requirement: log.m_files_mutex and log.writer_mutex acquired before calling |
| 465 |
this function (unless srv_is_being_started) and the given file must have been |
| 466 |
marked as consumed before calling this function. |
| 467 |
|
| 468 |
@param[in,out] log redo log |
| 469 |
@param[in] file_id id of the file which should be recycled |
| 470 |
@param[in] unused_file_size size to which the file should be resized |
| 471 |
@retval true recycled (including rename) successfully |
| 472 |
@retval false failed to rename (disk error) */ |
| 473 |
static bool log_files_recycle_file(log_t &log, Log_file_id file_id, |
| 474 |
os_offset_t unused_file_size); |
| 475 |
|
| 476 |
/** Process the given consumed redo log file (recycle or remove the file). |
| 477 |
|
| 478 |
Requirement: log.m_files_mutex and log.writer_mutex acquired before calling |
| 479 |
this function (unless srv_is_being_started) and the given file must have been |
| 480 |
marked as consumed before calling this function. |
| 481 |
|
| 482 |
@param[in,out] log redo log |
| 483 |
@param[in] file_id id of the file which should be processed |
| 484 |
@retval true processed (recycled or removed) successfully |
| 485 |
@retval false failed to process (disk error) */ |
| 486 |
static bool log_files_process_consumed_file(log_t &log, Log_file_id file_id); |
| 487 |
|
| 488 |
/** Process all redo log files marked as consumed (recycling or removing each). |
| 489 |
|
| 490 |
Requirement: log.m_files_mutex and log.writer_mutex acquired before calling |
| 491 |
this function (unless srv_is_being_started). |
| 492 |
|
| 493 |
@param[in,out] log redo log */ |
| 494 |
static void log_files_process_consumed_files(log_t &log); |
| 495 |
|
| 496 |
/** Create a spare unused redo log file if there was no such file. |
| 497 |
|
| 498 |
Requirement: log.m_files_mutex acquired before calling this function |
| 499 |
(unless srv_is_being_started). |
| 500 |
|
| 501 |
@param[in,out] log redo log */ |
| 502 |
static void log_files_create_next_as_unused_if_needed(log_t &log); |
| 503 |
|
| 504 |
/** Checks if the redo log consumption, according to the measured average |
| 505 |
speed of consumption, is too slow to consume the oldest file in reasonable |
| 506 |
time (10 seconds). |
| 507 |
|
| 508 |
Requirement: log.m_files_mutex acquired before calling this function |
| 509 |
(unless srv_is_being_started). |
| 510 |
|
| 511 |
@param[in] log redo log |
| 512 |
@return true iff the redo log consumption is not going to consume the |
| 513 |
oldest redo log file soon */ |
| 514 |
static bool log_files_consuming_oldest_file_takes_too_long(const log_t &log); |
| 515 |
|
| 516 |
/** Checks if the redo log, according to the measured average speed, |
| 517 |
is not being filled fast enough to fill the oldest redo log file in |
| 518 |
reasonable time (10 seconds). Note, that if there are multiple redo |
| 519 |
log files, then the oldest file has already been filled, in which |
| 520 |
case this function would return false. |
| 521 |
|
| 522 |
Requirement: log.m_files_mutex acquired before calling this function |
| 523 |
(unless srv_is_being_started). |
| 524 |
|
| 525 |
@param[in] log redo log |
| 526 |
@return true iff the redo log is not being filled with new data |
| 527 |
fast enough to fill the oldest redo log file soon */ |
| 528 |
static bool log_files_filling_oldest_file_takes_too_long(const log_t &log); |
| 529 |
|
| 530 |
/** Checks if there are any reasons to rush consumption of the oldest redo |
| 531 |
log file, that is if either: |
| 532 |
- rushing consumption has been requested explicitly (by the log_writer |
| 533 |
which is waiting for a next available file), |
| 534 |
- or the redo log is being resized down. |
| 535 |
|
| 536 |
Requirement: log.m_files_mutex acquired before calling this function |
| 537 |
(unless srv_is_being_started). |
| 538 |
|
| 539 |
@param[in] log redo log |
| 540 |
@return true iff consumption of the oldest redo log files should be rushed */ |
| 541 |
static bool log_files_should_rush_oldest_file_consumption(const log_t &log); |
| 542 |
|
| 543 |
/** Ensures that unused redo log files have log.m_capacity.next_file_size(). |
| 544 |
|
| 545 |
Requirement: log.m_files_mutex acquired before calling this function |
| 546 |
(unless srv_is_being_started). |
| 547 |
|
| 548 |
@param[in,out] log redo log */ |
| 549 |
static void log_files_adjust_unused_files_sizes(log_t &log); |
| 550 |
|
| 551 |
/** Result of execution of log_files_governor_iteration_low(). */ |
| 552 |
enum class Log_files_governor_iteration_result { |
| 553 |
/** Execution needs to be repeated, but this time when having |
| 554 |
the log.writer_mutex acquired. */ |
| 555 |
RETRY_WITH_WRITER_MUTEX, |
| 556 |
|
| 557 |
/** Execution completed successfully, the governor should produce |
| 558 |
extra intake to help the pending redo resize to be finished. */ |
| 559 |
COMPLETED_BUT_NEED_MORE_INTAKE, |
| 560 |
|
| 561 |
/** Execution completed successfully, nothing to be done more. */ |
| 562 |
COMPLETED |
| 563 |
}; |
| 564 |
|
| 565 |
/** Tries to perform a single iteration of the log_files_governor thread. |
| 566 |
However, it might turn out, that the log.writer_mutex is required to perform |
| 567 |
required actions. In such case (happens only when has_writer_mutex is false) |
| 568 |
this function returns earlier, without completing all the steps and returns the |
| 569 |
Log_files_governor_iteration_result::RETRY_WITH_WRITER_MUTEX value. Then caller |
| 570 |
is responsible for repeating the call, after acquiring the log.writer_mutex. |
| 571 |
|
| 572 |
Requirement: log.m_files_mutex not acquired before calling this function and |
| 573 |
log.writer_mutex acquired iff has_writer_mutex is true. |
| 574 |
|
| 575 |
@param[in,out] log redo log |
| 576 |
@param[in] has_writer_mutex true iff this thread has log.writer_mutex |
| 577 |
acquired |
| 578 |
@return @see Log_files_governor_iteration_result */ |
| 579 |
static Log_files_governor_iteration_result log_files_governor_iteration_low( |
| 580 |
log_t &log, bool has_writer_mutex); |
| 581 |
|
| 582 |
/** Performs a single iteration of the log_files_governor thread. It first tries |
| 583 |
to perform the iteration without the log.writer_mutex. However, if that failed |
| 584 |
then this function would retry the attempt, after acquiring the |
| 585 |
log.writer_mutex. |
| 586 |
|
| 587 |
Requirement: none of log.m_files_mutex, log.writer_mutex acquired before calling |
| 588 |
this function. |
| 589 |
|
| 590 |
@param[in,out] log redo log */ |
| 591 |
static void log_files_governor_iteration(log_t &log); |
| 592 |
|
| 593 |
static dberr_t log_files_prepare_unused_file(log_t &log, Log_file_id file_id, |
| 594 |
lsn_t start_lsn, |
| 595 |
lsn_t checkpoint_lsn, |
| 596 |
bool create_first_data_block, |
| 597 |
os_offset_t &file_size); |
| 598 |
|
| 599 |
static lsn_t log_files_oldest_needed_lsn(const log_t &log) { |
| 600 |
log_files_access_allowed_validate(log); |
| 601 |
lsn_t oldest_need_lsn; |
| 602 |
log_consumer_get_oldest(log, oldest_need_lsn); |
| 603 |
return oldest_need_lsn; |
| 604 |
} |
| 605 |
|
| 606 |
static lsn_t log_files_newest_needed_lsn(const log_t &log) { |
| 607 |
log_files_access_allowed_validate(log); |
| 608 |
return log.write_lsn.load(); |
| 609 |
} |
| 610 |
|
| 611 |
static std::pair<lsn_t, lsn_t> log_files_logical_size_and_checkpoint_age( |
| 612 |
const log_t &log) { |
| 613 |
log_files_access_allowed_validate(log); |
| 614 |
|
| 615 |
const lsn_t oldest_lsn = log_files_oldest_needed_lsn(log); |
| 616 |
|
| 617 |
const lsn_t checkpoint_lsn = log.last_checkpoint_lsn.load(); |
| 618 |
|
| 619 |
const lsn_t newest_lsn = log_files_newest_needed_lsn(log); |
| 620 |
|
| 621 |
const lsn_t size = ut_uint64_align_up(newest_lsn, OS_FILE_LOG_BLOCK_SIZE) - |
| 622 |
ut_uint64_align_down(oldest_lsn, OS_FILE_LOG_BLOCK_SIZE); |
| 623 |
|
| 624 |
/* Note that log_files_next_checkpoint() updates log.last_checkpoint_lsn |
| 625 |
under m_files_mutex which we hold here, so it couldn't increase meanwhile. */ |
| 626 |
ut_a(checkpoint_lsn <= newest_lsn); |
| 627 |
|
| 628 |
const lsn_t checkpoint_age = newest_lsn - checkpoint_lsn; |
| 629 |
|
| 630 |
return {size, checkpoint_age}; |
| 631 |
} |
| 632 |
|
| 633 |
static os_offset_t log_files_next_file_size(const log_t &log) { |
| 634 |
log_files_access_allowed_validate(log); |
| 635 |
|
| 636 |
const os_offset_t file_size = log.m_capacity.next_file_size(); |
| 637 |
|
| 638 |
if (log.m_capacity.current_physical_capacity() < |
| 639 |
log_files_size_of_existing_files(log.m_files) + file_size) { |
| 640 |
/* Note: it might happen if the log_files_governor hasn't yet |
| 641 |
consumed (or processed all consumed) log files. However, it's |
| 642 |
safe to wait after releasing m_files_mutex in case this function |
| 643 |
returned 0, because the log_files_governor will be able to consume |
| 644 |
and process the required files, so there is no cycle. */ |
| 645 |
#ifdef UNIV_DEBUG |
| 646 |
const lsn_t oldest_lsn = log_files_oldest_needed_lsn(log); |
| 647 |
const auto oldest_file = log.m_files.begin(); |
| 648 |
ut_a(oldest_file != log.m_files.end()); |
| 649 |
ut_a(!oldest_file->contains(oldest_lsn)); |
| 650 |
#endif |
| 651 |
return 0; |
| 652 |
} |
| 653 |
return file_size; |
| 654 |
} |
| 655 |
|
| 656 |
static Log_file_id log_files_next_unused_id(log_t &log) { |
| 657 |
log_files_validate_current_file(log); |
| 658 |
return Log_file::next_id(log.m_current_file.m_id, |
| 659 |
log.m_unused_files_count + 1); |
| 660 |
} |
| 661 |
|
| 662 |
static void log_files_create_allowed_validate() { |
| 663 |
/* During shutdown we might also allow to do some maintenance. */ |
| 664 |
ut_a(srv_is_being_started || |
| 665 |
srv_shutdown_state.load() >= SRV_SHUTDOWN_LAST_PHASE); |
| 666 |
|
| 667 |
ut_a(!srv_read_only_mode); |
| 668 |
ut_a(!recv_recovery_is_on()); |
| 669 |
|
| 670 |
log_background_threads_inactive_validate(); |
| 671 |
} |
| 672 |
|
| 673 |
static void log_files_access_allowed_validate(const log_t &log) { |
| 674 |
ut_ad(log_files_mutex_own(log) || srv_is_being_started); |
| 675 |
} |
| 676 |
|
| 677 |
static void log_files_write_allowed_validate(const log_t &log) { |
| 678 |
log_files_access_allowed_validate(log); |
| 679 |
ut_a(!srv_read_only_mode); |
| 680 |
ut_ad(log_writer_mutex_own(log)); |
| 681 |
} |
| 682 |
|
| 683 |
static Log_file_header log_files_prepare_header(const log_t &log, |
| 684 |
lsn_t file_start_lsn) { |
| 685 |
log_files_access_allowed_validate(log); |
| 686 |
|
| 687 |
Log_file_header file_header; |
| 688 |
file_header.m_format = to_int(log.m_format); |
| 689 |
file_header.m_start_lsn = file_start_lsn; |
| 690 |
file_header.m_creator_name = log.m_creator_name; |
| 691 |
file_header.m_log_flags = log.m_log_flags; |
| 692 |
file_header.m_log_uuid = log.m_log_uuid; |
| 693 |
return file_header; |
| 694 |
} |
| 695 |
|
| 696 |
static Log_file_header log_files_prepare_header(const log_t &log, |
| 697 |
const Log_file &file) { |
| 698 |
log_files_access_allowed_validate(log); |
| 699 |
|
| 700 |
ut_ad(file.m_start_lsn == log.m_files.file(file.m_id)->m_start_lsn); |
| 701 |
auto file_header = log_files_prepare_header(log, file.m_start_lsn); |
| 702 |
if (file.m_full) { |
| 703 |
log_file_header_set_flag(file_header.m_log_flags, |
| 704 |
LOG_HEADER_FLAG_FILE_FULL); |
| 705 |
} |
| 706 |
return file_header; |
| 707 |
} |
| 708 |
|
| 709 |
static void log_files_validate_not_consumed(const log_t &log) { |
| 710 |
log_files_access_allowed_validate(log); |
| 711 |
log_files_for_each(log.m_files, log_files_oldest_needed_lsn(log), |
| 712 |
log_files_newest_needed_lsn(log), |
| 713 |
[](const Log_file &file) { ut_a(!file.m_consumed); }); |
| 714 |
} |
| 715 |
|
| 716 |
static void log_files_validate_current_file(const log_t &log) { |
| 717 |
log_files_access_allowed_validate(log); |
| 718 |
const auto file = log.m_files.file(log.m_current_file.m_id); |
| 719 |
ut_a(file != log.m_files.end()); |
| 720 |
ut_a(!file->m_consumed); |
| 721 |
ut_a(*file == log.m_current_file); |
| 722 |
const lsn_t newest_lsn = log_files_newest_needed_lsn(log); |
| 723 |
ut_a(file->contains(newest_lsn) || newest_lsn == file->m_end_lsn); |
| 724 |
} |
| 725 |
|
| 726 |
static void log_files_mark_consumed_file(log_t &log, Log_file_id file_id) { |
| 727 |
log_files_access_allowed_validate(log); |
| 728 |
|
| 729 |
const auto file = log.m_files.file(file_id); |
| 730 |
ut_a(file != log.m_files.end()); |
| 731 |
ut_a(file->m_id == file_id); |
| 732 |
ut_a(!file->m_consumed); |
| 733 |
|
| 734 |
/* We are not consuming file which has LSN range containing oldest_lsn. */ |
| 735 |
const lsn_t oldest_lsn = log_files_oldest_needed_lsn(log); |
| 736 |
ut_a(!file->contains(oldest_lsn)); |
| 737 |
|
| 738 |
/* We are not consuming file which has LSN range containing newest_lsn, |
| 739 |
unless this is lsn pointing exactly on the beginning of the file. */ |
| 740 |
const lsn_t newest_lsn = log_files_newest_needed_lsn(log); |
| 741 |
ut_a(!file->contains(newest_lsn)); |
| 742 |
|
| 743 |
/* We are not consuming file on path from the oldest file to the newest. */ |
| 744 |
log_files_for_each(log.m_files, oldest_lsn, newest_lsn, |
| 745 |
[file_id](const Log_file &f) { ut_a(f.m_id != file_id); }); |
| 746 |
|
| 747 |
log.m_files.set_consumed(file_id); |
| 748 |
|
| 749 |
DBUG_PRINT("ib_log", ("consumed log file %zu (LSN " LSN_PF ".." LSN_PF ")", |
| 750 |
size_t{file_id}, file->m_start_lsn, file->m_end_lsn)); |
| 751 |
|
| 752 |
log_files_validate_not_consumed(log); |
| 753 |
} |
| 754 |
|
| 755 |
static dberr_t log_files_remove_from_future(log_t &log) { |
| 756 |
log_files_write_allowed_validate(log); |
| 757 |
|
| 758 |
const lsn_t newest_lsn = log_files_newest_needed_lsn(log); |
| 759 |
ut_a(newest_lsn >= LOG_START_LSN); |
| 760 |
|
| 761 |
/* NOTE: This list has to be built, because log_files_remove_file() |
| 762 |
removes file from the log.m_files, so InnoDB cannot call it when |
| 763 |
iterating the log.m_files. */ |
| 764 |
|
| 765 |
std::vector<Log_file_id> to_remove; |
| 766 |
|
| 767 |
log_files_for_each(log.m_files, [&](const Log_file &file) { |
| 768 |
if (!file.m_consumed && newest_lsn <= file.m_start_lsn && |
| 769 |
file.m_id != log.m_current_file.m_id) { |
| 770 |
to_remove.push_back(file.m_id); |
| 771 |
} |
| 772 |
}); |
| 773 |
|
| 774 |
if (!to_remove.empty()) { |
| 775 |
const dberr_t err = log_files_mark_current_file_as_incomplete(log); |
| 776 |
if (err != DB_SUCCESS) { |
| 777 |
return err; |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
log_files_validate_current_file(log); |
| 782 |
|
| 783 |
for (Log_file_id file_id : to_remove) { |
| 784 |
const dberr_t err = log_files_remove_file(log, file_id); |
| 785 |
if (err != DB_SUCCESS) { |
| 786 |
return err; |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
log_files_validate_current_file(log); |
| 791 |
|
| 792 |
return DB_SUCCESS; |
| 793 |
} |
| 794 |
|
| 795 |
static void log_files_mark_consumed_files(log_t &log) { |
| 796 |
log_files_access_allowed_validate(log); |
| 797 |
|
| 798 |
const lsn_t oldest_lsn = log_files_oldest_needed_lsn(log); |
| 799 |
|
| 800 |
log_files_validate_current_file(log); |
| 801 |
|
| 802 |
log_files_for_each(log.m_files, [&](const Log_file &file) { |
| 803 |
if (!file.m_consumed && file.m_end_lsn <= oldest_lsn) { |
| 804 |
log_files_mark_consumed_file(log, file.m_id); |
| 805 |
} |
| 806 |
}); |
| 807 |
} |
| 808 |
|
| 809 |
namespace log_files { |
| 810 |
|
| 811 |
static os_offset_t physical_size(const log_t &log, |
| 812 |
os_offset_t unused_file_size) { |
| 813 |
return log_files_size_of_existing_files(log.m_files) + |
| 814 |
log.m_unused_files_count * unused_file_size; |
| 815 |
} |
| 816 |
|
| 817 |
static bool physical_capacity_allows_to_recycle(const log_t &log, |
| 818 |
os_offset_t removed_file_size, |
| 819 |
os_offset_t unused_file_size) { |
| 820 |
const auto current_total_physical_size = physical_size(log, unused_file_size); |
| 821 |
|
| 822 |
const auto planned_total_physical_size = |
| 823 |
current_total_physical_size + unused_file_size - removed_file_size; |
| 824 |
|
| 825 |
return planned_total_physical_size <= |
| 826 |
log.m_capacity.current_physical_capacity(); |
| 827 |
} |
| 828 |
|
| 829 |
static bool physical_capacity_allows_to_create(const log_t &log, |
| 830 |
os_offset_t unused_file_size) { |
| 831 |
return physical_capacity_allows_to_recycle(log, 0, unused_file_size); |
| 832 |
} |
| 833 |
|
| 834 |
static size_t number_of_files(const log_t &log) { |
| 835 |
return log_files_number_of_existing_files(log.m_files) + |
| 836 |
log.m_unused_files_count; |
| 837 |
} |
| 838 |
|
| 839 |
static bool is_newest_lsn_nearby_the_end(const log_t &log) { |
| 840 |
const auto margin = log.m_capacity.next_file_earlier_margin(); |
| 841 |
const lsn_t newest_lsn = log_files_newest_needed_lsn(log); |
| 842 |
return newest_lsn + margin >= log.m_current_file.m_end_lsn; |
| 843 |
} |
| 844 |
|
| 845 |
static bool number_of_files_enforced_to_allow(const log_t &log) { |
| 846 |
return log.m_unused_files_count == 0 && is_newest_lsn_nearby_the_end(log); |
| 847 |
} |
| 848 |
|
| 849 |
/** Checks if current number of redo files allows to recycle the oldest one. |
| 850 |
@remarks |
| 851 |
The strategy is to keep the total number of log files equal to LOG_N_FILES |
| 852 |
(including unused files), unless the newest_lsn is close to the end of file |
| 853 |
and there are no unused files (in which case, the creation is also allowed, |
| 854 |
because at least one unused file is allowed, as long as we won't exceed |
| 855 |
current_physical_capacity, which is checked by the caller). |
| 856 |
@return true iff allowed to recycle log file according to number of files */ |
| 857 |
static bool number_of_files_allows_to_recycle(const log_t &log) { |
| 858 |
return number_of_files(log) <= LOG_N_FILES || |
| 859 |
number_of_files_enforced_to_allow(log); |
| 860 |
} |
| 861 |
|
| 862 |
/** Checks if current number of redo files allows to create a new unused file. |
| 863 |
@see number_of_files_allows_to_recycle |
| 864 |
@return true iff allowed to create unused file according to number of files */ |
| 865 |
static bool number_of_files_allows_to_create(const log_t &log) { |
| 866 |
return number_of_files(log) + 1 <= LOG_N_FILES || |
| 867 |
number_of_files_enforced_to_allow(log); |
| 868 |
} |
| 869 |
|
| 870 |
static bool might_recycle_file(const log_t &log, os_offset_t removed_file_size, |
| 871 |
os_offset_t unused_file_size) { |
| 872 |
return number_of_files_allows_to_recycle(log) && |
| 873 |
physical_capacity_allows_to_recycle(log, removed_file_size, |
| 874 |
unused_file_size); |
| 875 |
} |
| 876 |
|
| 877 |
static bool might_create_unused_file(const log_t &log, |
| 878 |
os_offset_t unused_file_size) { |
| 879 |
return number_of_files_allows_to_create(log) && |
| 880 |
physical_capacity_allows_to_create(log, unused_file_size); |
| 881 |
} |
| 882 |
|
| 883 |
/** Checks if consumption of the oldest redo log files needs to be done or |
| 884 |
might be postponed. |
| 885 |
|
| 886 |
@remarks |
| 887 |
This check is being used in order to keep more log files, even if according to |
| 888 |
all registered redo log consumers, they are not needed anymore. The motivation |
| 889 |
for that is to support external redo consumers which are not being registered |
| 890 |
and preserve for them comparable chances to succeed to chances they had in older |
| 891 |
versions of MySQL. This is just to be gentle, and this is not always guaranteed. |
| 892 |
In particular, we do not provide such properties when it is not comfortable for |
| 893 |
the InnoDB. This function is supposed to tell when it is comfortable for InnoDB |
| 894 |
to provide such properties. The log files consumption must not be postponed in |
| 895 |
any of the following cases: |
| 896 |
|
| 897 |
1. The log_files_governor has been explicitly requested to consume more files, |
| 898 |
which could happen e.g. in mtr test awaiting until there is just one file, |
| 899 |
or when there is no next file (in which case requested by log_writer). |
| 900 |
|
| 901 |
2. There are no spare (unused) log files (at least one is needed for log_writer |
| 902 |
so it could switch to a next log file smoothly if it needed to do so). |
| 903 |
|
| 904 |
3. Redo log is supposed to be resized down (current_capacity hasn't yet reached |
| 905 |
the target_capacity). |
| 906 |
|
| 907 |
4. Redo log is supposed to be resized up (unused files might be resized easily |
| 908 |
so it is preferable to consume files). |
| 909 |
|
| 910 |
@param[in] log redo log |
| 911 |
@return true iff redo log files consumption is needed */ |
| 912 |
bool is_consumption_needed(const log_t &log) { |
| 913 |
DBUG_EXECUTE_IF("log_force_consumption", return true;); |
| 914 |
const auto current_size = physical_size(log, log.m_capacity.next_file_size()); |
| 915 |
const auto target_capacity = log.m_capacity.target_physical_capacity(); |
| 916 |
const auto current_capacity = log.m_capacity.current_physical_capacity(); |
| 917 |
|
| 918 |
ut_a(current_size <= current_capacity); |
| 919 |
|
| 920 |
return /* case 1. */ log.m_requested_files_consumption || |
| 921 |
/* case 2. */ log.m_unused_files_count == 0 || |
| 922 |
/* case 3. */ target_capacity < current_capacity || |
| 923 |
/* case 4. */ current_size < current_capacity; |
| 924 |
} |
| 925 |
|
| 926 |
} // namespace log_files |
| 927 |
|
| 928 |
static dberr_t log_files_remove_file(log_t &log, Log_file_id file_id) { |
| 929 |
log_files_write_allowed_validate(log); |
| 930 |
ut_a(!log.m_files.empty()); |
| 931 |
const auto file = log.m_files.file(file_id); |
| 932 |
ut_a(file != log.m_files.end()); |
| 933 |
ut_a(file->m_id == file_id); |
| 934 |
const dberr_t remove_err = log_remove_file(log.m_files_ctx, file_id); |
| 935 |
if (remove_err != DB_SUCCESS) { |
| 936 |
return remove_err; |
| 937 |
} |
| 938 |
log.m_files.erase(file_id); |
| 939 |
os_event_set(log.m_file_removed_event); |
| 940 |
return DB_SUCCESS; |
| 941 |
} |
| 942 |
|
| 943 |
static bool log_files_remove_consumed_file(log_t &log, Log_file_id file_id) { |
| 944 |
log_files_write_allowed_validate(log); |
| 945 |
ut_a(!log.m_files.empty()); |
| 946 |
ut_a(log.m_files.begin()->m_id == file_id); |
| 947 |
ut_a(log.m_files.begin()->m_consumed); |
| 948 |
return log_files_remove_file(log, file_id) == DB_SUCCESS; |
| 949 |
} |
| 950 |
|
| 951 |
static bool log_files_recycle_file(log_t &log, Log_file_id file_id, |
| 952 |
os_offset_t unused_file_size) { |
| 953 |
log_files_write_allowed_validate(log); |
| 954 |
ut_a(!log.m_files.empty()); |
| 955 |
|
| 956 |
const auto file = log.m_files.file(file_id); |
| 957 |
ut_a(file == log.m_files.begin()); |
| 958 |
|
| 959 |
/* For example: #ib_redo10 -> #ib_redo10_tmp */ |
| 960 |
dberr_t err = log_mark_file_as_unused(log.m_files_ctx, file_id); |
| 961 |
if (err != DB_SUCCESS) { |
| 962 |
return false; |
| 963 |
} |
| 964 |
|
| 965 |
const auto next_unused_id = log_files_next_unused_id(log); |
| 966 |
|
| 967 |
/* For example: #ib_redo10_tmp -> #ib_redo15_tmp */ |
| 968 |
err = log_rename_unused_file(log.m_files_ctx, file_id, next_unused_id); |
| 969 |
ut_a(err == DB_SUCCESS); |
| 970 |
|
| 971 |
/* For example: resize #ib_redo15_tmp to innodb_redo_log_capacity / 32 */ |
| 972 |
err = |
| 973 |
log_resize_unused_file(log.m_files_ctx, next_unused_id, unused_file_size); |
| 974 |
ut_a(err == DB_SUCCESS); |
| 975 |
|
| 976 |
log.m_unused_files_count++; |
| 977 |
log.m_files.erase(file_id); |
| 978 |
return true; |
| 979 |
} |
| 980 |
|
| 981 |
static bool log_files_process_consumed_file(log_t &log, Log_file_id file_id) { |
| 982 |
log_files_write_allowed_validate(log); |
| 983 |
const auto file = log.m_files.file(file_id); |
| 984 |
ut_a(file != log.m_files.end()); |
| 985 |
ut_a(file->m_consumed); |
| 986 |
log_files_validate_current_file(log); |
| 987 |
|
| 988 |
const os_offset_t unused_file_size = log.m_capacity.next_file_size(); |
| 989 |
|
| 990 |
if (log_files::might_recycle_file(log, file->m_size_in_bytes, |
| 991 |
unused_file_size)) { |
| 992 |
return log_files_recycle_file(log, file_id, unused_file_size); |
| 993 |
} else { |
| 994 |
return log_files_remove_consumed_file(log, file_id); |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
static void log_files_process_consumed_files(log_t &log) { |
| 999 |
log_files_write_allowed_validate(log); |
| 1000 |
|
| 1001 |
/* NOTE: This list has to be built, because log_files_process_consumed_file() |
| 1002 |
removes file from the log.m_files, so InnoDB cannot call it when iterating |
| 1003 |
the log.m_files. */ |
| 1004 |
|
| 1005 |
ut::vector<Log_file_id> to_process; |
| 1006 |
|
| 1007 |
log_files_for_each(log.m_files, [&](const Log_file &file) { |
| 1008 |
if (file.m_consumed) { |
| 1009 |
to_process.push_back(file.m_id); |
| 1010 |
} |
| 1011 |
}); |
| 1012 |
|
| 1013 |
bool any_processed = false; |
| 1014 |
for (Log_file_id file_id : to_process) { |
| 1015 |
if (!log_files_process_consumed_file(log, file_id)) { |
| 1016 |
break; |
| 1017 |
} |
| 1018 |
any_processed = true; |
| 1019 |
} |
| 1020 |
|
| 1021 |
if (any_processed) { |
| 1022 |
log_files_update_capacity_limits(log); |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
static void log_files_create_next_as_unused_if_needed(log_t &log) { |
| 1027 |
log_files_access_allowed_validate(log); |
| 1028 |
log_files_validate_current_file(log); |
| 1029 |
|
| 1030 |
const os_offset_t unused_file_size = log.m_capacity.next_file_size(); |
| 1031 |
|
| 1032 |
if (!log_files::might_create_unused_file(log, unused_file_size)) { |
| 1033 |
return; |
| 1034 |
} |
| 1035 |
|
| 1036 |
const Log_file_id file_id = log_files_next_unused_id(log); |
| 1037 |
|
| 1038 |
const dberr_t err = |
| 1039 |
log_create_unused_file(log.m_files_ctx, file_id, unused_file_size); |
| 1040 |
|
| 1041 |
if (err == DB_SUCCESS) { |
| 1042 |
log.m_unused_files_count++; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
dberr_t log_files_produce_file(log_t &log) { |
| 1047 |
log_files_write_allowed_validate(log); |
| 1048 |
log_files_validate_current_file(log); |
| 1049 |
|
| 1050 |
const lsn_t start_lsn = log.m_current_file.m_end_lsn; |
| 1051 |
|
| 1052 |
ut_a(start_lsn >= LOG_START_LSN); |
| 1053 |
ut_a(start_lsn % OS_FILE_LOG_BLOCK_SIZE == 0); |
| 1054 |
ut_a(start_lsn == log_files_newest_needed_lsn(log)); |
| 1055 |
|
| 1056 |
Log_file_id file_id = log.m_current_file.next_id(); |
| 1057 |
ut_a(log.m_files.file(file_id) == log.m_files.end()); |
| 1058 |
|
| 1059 |
if (log_files_next_file_size(log) == 0) { |
| 1060 |
return DB_OUT_OF_DISK_SPACE; |
| 1061 |
} |
| 1062 |
|
| 1063 |
log_sync_point("log_before_file_produced"); |
| 1064 |
|
| 1065 |
ut_a(start_lsn >= log.last_checkpoint_lsn.load()); |
| 1066 |
|
| 1067 |
{ |
| 1068 |
const dberr_t err = |
| 1069 |
log_files_create_file(log, file_id, start_lsn, 0, false); |
| 1070 |
|
| 1071 |
if (err != DB_SUCCESS) { |
| 1072 |
return err; |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
log_sync_point("log_before_file_marked_as_full"); |
| 1077 |
|
| 1078 |
ut_a(file_id > log.m_current_file.m_id); |
| 1079 |
/* Because newer file is created, we can mark the previously newest file |
| 1080 |
as full. That is useful, because if during recovery InnoDB couldn't find |
| 1081 |
next redo log file, it could then determine the reason for that: |
| 1082 |
- if server was crashed just before log_files_produce_file was called |
| 1083 |
(but after the last log block to file was written), |
| 1084 |
- or if the newest redo log file was simply lost (e.g. FS corruption). */ |
| 1085 |
{ |
| 1086 |
const dberr_t err = log_files_mark_current_file_as_full(log); |
| 1087 |
|
| 1088 |
ut_a(err == DB_SUCCESS); |
| 1089 |
} |
| 1090 |
|
| 1091 |
log_flusher_mutex_enter(log); |
| 1092 |
log_files_update_current_file_low(log); |
| 1093 |
log_flusher_mutex_exit(log); |
| 1094 |
|
| 1095 |
ut_a(log.m_current_file.m_id == file_id); |
| 1096 |
|
| 1097 |
DBUG_PRINT("ib_log", ("produced log file %zu (LSN " LSN_PF ".." LSN_PF ")", |
| 1098 |
size_t{file_id}, log.m_current_file.m_start_lsn, |
| 1099 |
log.m_current_file.m_end_lsn)); |
| 1100 |
|
| 1101 |
log_files_validate_not_consumed(log); |
| 1102 |
|
| 1103 |
log_sync_point("log_after_file_produced"); |
| 1104 |
os_event_set(log.m_files_governor_event); |
| 1105 |
|
| 1106 |
return DB_SUCCESS; |
| 1107 |
} |
| 1108 |
|
| 1109 |
static void log_files_update_capacity_limits(log_t &log) { |
| 1110 |
log_files_access_allowed_validate(log); |
| 1111 |
|
| 1112 |
IB_mutex_guard limits_lock{&log.limits_mutex, UT_LOCATION_HERE}; |
| 1113 |
|
| 1114 |
lsn_t logical_size, checkpoint_age; |
| 1115 |
std::tie(logical_size, checkpoint_age) = |
| 1116 |
log_files_logical_size_and_checkpoint_age(log); |
| 1117 |
|
| 1118 |
log.m_capacity.update(log.m_files, logical_size, checkpoint_age); |
| 1119 |
|
| 1120 |
log_update_limits_low(log); |
| 1121 |
log_update_exported_variables(log); |
| 1122 |
} |
| 1123 |
|
| 1124 |
static bool log_files_consuming_oldest_file_takes_too_long(const log_t &log) { |
| 1125 |
log_files_access_allowed_validate(log); |
| 1126 |
ut_a(!log.m_files.empty()); |
| 1127 |
|
| 1128 |
if (!(log_checkpointer_is_active() && log.m_allow_checkpoints.load())) { |
| 1129 |
return false; |
| 1130 |
} |
| 1131 |
|
| 1132 |
const lsn_t oldest_lsn = log_files_oldest_needed_lsn(log); |
| 1133 |
|
| 1134 |
const auto oldest_file = log.m_files.find(oldest_lsn); |
| 1135 |
ut_a(oldest_file != log.m_files.end()); |
| 1136 |
|
| 1137 |
/** Maximum time it might take to consume the oldest redo log file |
| 1138 |
since now. If the average lsn consumption speed shows that most likely |
| 1139 |
this time will not be enough for the oldest file to become consumed, |
| 1140 |
then the oldest redo log consumer must be requested to proceed faster. */ |
| 1141 |
|
| 1142 |
const uint32_t MAX_CONSUMPTION_TIME_IN_SEC = 10; |
| 1143 |
|
| 1144 |
const lsn_t predicted_oldest_lsn = |
| 1145 |
oldest_lsn + |
| 1146 |
log.m_files_stats.m_lsn_consumption_per_1s * MAX_CONSUMPTION_TIME_IN_SEC; |
| 1147 |
|
| 1148 |
const lsn_t newest_lsn = log_files_newest_needed_lsn(log); |
| 1149 |
|
| 1150 |
return predicted_oldest_lsn <= newest_lsn && |
| 1151 |
oldest_file->contains(predicted_oldest_lsn); |
| 1152 |
} |
| 1153 |
|
| 1154 |
static bool log_files_filling_oldest_file_takes_too_long(const log_t &log) { |
| 1155 |
log_files_access_allowed_validate(log); |
| 1156 |
ut_a(!log.m_files.empty()); |
| 1157 |
|
| 1158 |
if (!(log_checkpointer_is_active() && log.m_allow_checkpoints.load())) { |
| 1159 |
return false; |
| 1160 |
} |
| 1161 |
|
| 1162 |
DBUG_EXECUTE_IF("log_force_truncate", return true;); |
| 1163 |
|
| 1164 |
const lsn_t oldest_lsn = log_files_oldest_needed_lsn(log); |
| 1165 |
|
| 1166 |
const auto oldest_file = log.m_files.find(oldest_lsn); |
| 1167 |
ut_a(oldest_file != log.m_files.end()); |
| 1168 |
|
| 1169 |
const uint32_t MAX_FILL_TIME_IN_SEC = 10; |
| 1170 |
|
| 1171 |
const lsn_t predicted_newest_lsn = |
| 1172 |
log_files_newest_needed_lsn(log) + |
| 1173 |
log.m_files_stats.m_lsn_production_per_1s * MAX_FILL_TIME_IN_SEC; |
| 1174 |
|
| 1175 |
/* Check if next 10-seconds of current avg. intake would result in |
| 1176 |
the newest lsn still being inside the oldest redo log file. */ |
| 1177 |
|
| 1178 |
return oldest_file->contains(predicted_newest_lsn); |
| 1179 |
} |
| 1180 |
|
| 1181 |
void Log_files_stats::update(const log_t &log) { |
| 1182 |
log_files_access_allowed_validate(log); |
| 1183 |
|
| 1184 |
/* Check if stats should be updated (so called "successful call"). */ |
| 1185 |
const auto now = Log_clock::now(); |
| 1186 |
const auto duration = std::chrono::duration_cast<std::chrono::seconds>( |
| 1187 |
now - m_last_update_time); |
| 1188 |
if (duration.count() < 1.0) { |
| 1189 |
/* Failed to update stats (not enough time elapsed since last update) */ |
| 1190 |
return; |
| 1191 |
} |
| 1192 |
/* It is a next successful call to update(). */ |
| 1193 |
m_last_update_time = now; |
| 1194 |
|
| 1195 |
/* Update m_lsn_consumption_per_1s, m_oldest_lsn_on_update. */ |
| 1196 |
const lsn_t oldest_lsn = log_files_oldest_needed_lsn(log); |
| 1197 |
if (m_oldest_lsn_on_update != 0) { |
| 1198 |
const lsn_t lsn_diff = oldest_lsn - m_oldest_lsn_on_update; |
| 1199 |
m_lsn_consumption_per_1s = lsn_diff / duration.count(); |
| 1200 |
} |
| 1201 |
m_oldest_lsn_on_update = oldest_lsn; |
| 1202 |
|
| 1203 |
/* Update m_lsn_production_per_1s, m_newest_lsn_on_update. */ |
| 1204 |
const lsn_t newest_lsn = log_files_newest_needed_lsn(log); |
| 1205 |
if (m_newest_lsn_on_update != 0) { |
| 1206 |
const lsn_t lsn_diff = newest_lsn - m_newest_lsn_on_update; |
| 1207 |
m_lsn_production_per_1s = lsn_diff / duration.count(); |
| 1208 |
} |
| 1209 |
m_newest_lsn_on_update = newest_lsn; |
| 1210 |
} |
| 1211 |
|
| 1212 |
static void log_files_adjust_unused_files_sizes(log_t &log) { |
| 1213 |
log_files_access_allowed_validate(log); |
| 1214 |
const os_offset_t next_file_size = log.m_capacity.next_file_size(); |
| 1215 |
if (log.m_unused_file_size != next_file_size) { |
| 1216 |
const auto ret = log_remove_unused_files(log.m_files_ctx); |
| 1217 |
ut_a(ret.first == DB_SUCCESS); |
| 1218 |
|
| 1219 |
log.m_unused_files_count = 0; |
| 1220 |
log.m_unused_file_size = next_file_size; |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
static bool log_files_should_rush_oldest_file_consumption(const log_t &log) { |
| 1225 |
log_files_access_allowed_validate(log); |
| 1226 |
return log.m_capacity.is_resizing_down() || log.m_requested_files_consumption; |
| 1227 |
} |
| 1228 |
|
| 1229 |
static Log_files_governor_iteration_result log_files_governor_iteration_low( |
| 1230 |
log_t &log, bool has_writer_mutex) { |
| 1231 |
using Iteration_result = Log_files_governor_iteration_result; |
| 1232 |
|
| 1233 |
IB_mutex_guard files_latch{&(log.m_files_mutex), UT_LOCATION_HERE}; |
| 1234 |
log_files_update_capacity_limits(log); |
| 1235 |
log_files_adjust_unused_files_sizes(log); |
| 1236 |
|
| 1237 |
if (log_files::is_consumption_needed(log)) { |
| 1238 |
log_files_mark_consumed_files(log); |
| 1239 |
} |
| 1240 |
if (log_files_number_of_consumed_files(log.m_files) != 0) { |
| 1241 |
if (has_writer_mutex) { |
| 1242 |
log_files_process_consumed_files(log); |
| 1243 |
} else { |
| 1244 |
return Iteration_result::RETRY_WITH_WRITER_MUTEX; |
| 1245 |
} |
| 1246 |
} |
| 1247 |
|
| 1248 |
if (log.m_requested_files_consumption && log_files_next_file_size(log) != 0) { |
| 1249 |
/* The log_writer thread called log_files_wait_for_next_file_available(), |
| 1250 |
which checked that log_files_next_file_size() returned zero and set |
| 1251 |
m_requested_files_consumption to true under the the log.m_files_mutex. |
| 1252 |
This would force rushing the consumption of the oldest redo log file. |
| 1253 |
However, log_files_next_file_size() is no longer zero so there is no |
| 1254 |
reason to force the consumption any longer. */ |
| 1255 |
|
| 1256 |
log.m_requested_files_consumption = false; |
| 1257 |
} |
| 1258 |
|
| 1259 |
log.m_files_stats.update(log); |
| 1260 |
|
| 1261 |
bool needs_more_intake = false; |
| 1262 |
|
| 1263 |
if (log_files_should_rush_oldest_file_consumption(log)) { |
| 1264 |
/* Consider special actions to get rid of the oldest file sooner. |
| 1265 |
This include following possible actions: |
| 1266 |
- rushing the oldest redo log consumer to consume faster, |
| 1267 |
- truncating the redo log file if the oldest file is also the newest, |
| 1268 |
- requesting extra intake generated with usage of dummy redo records. */ |
| 1269 |
|
| 1270 |
if (log_files_consuming_oldest_file_takes_too_long(log)) { |
| 1271 |
lsn_t oldest_needed_lsn; |
| 1272 |
/* Note, that there is a possible race because the consumer |
| 1273 |
has possibly already consumed what we wanted to request. |
| 1274 |
Such spurious claims / requests are not considered dangerous. */ |
| 1275 |
if (auto *consumer = log_consumer_get_oldest(log, oldest_needed_lsn)) { |
| 1276 |
ut_a(!log.m_files.empty()); |
| 1277 |
/* LOG_FILE_HDR_SIZE bytes of next file are not counted in the lsn |
| 1278 |
sequence, but the LOG_BLOCK_HDR_SIZE bytes of the first log data |
| 1279 |
block are counted. Because oldest_file->m_end_lsn % |
| 1280 |
OS_FILE_LOG_BLOCK_SIZE == 0, we need to add LOG_BLOCK_HDR_SIZE to |
| 1281 |
build a proper lsn (pointing on data byte). */ |
| 1282 |
const auto next_file_lsn = |
| 1283 |
log.m_files.begin()->m_end_lsn + LOG_BLOCK_HDR_SIZE; |
| 1284 |
if (next_file_lsn <= log_get_lsn(log)) { |
| 1285 |
IB_mutex_guard limits_lock{&log.limits_mutex, UT_LOCATION_HERE}; |
| 1286 |
consumer->consumption_requested(next_file_lsn); |
| 1287 |
} |
| 1288 |
} |
| 1289 |
} |
| 1290 |
|
| 1291 |
if (log_files_filling_oldest_file_takes_too_long(log)) { |
| 1292 |
/* If there is more than one file, then the oldest file is already |
| 1293 |
filled, so filling it will never be considered taking too long. */ |
| 1294 |
|
| 1295 |
if (log_files_is_truncate_allowed(log)) { |
| 1296 |
if (has_writer_mutex) { |
| 1297 |
log_files_truncate(log); |
| 1298 |
} else { |
| 1299 |
return Iteration_result::RETRY_WITH_WRITER_MUTEX; |
| 1300 |
} |
| 1301 |
} |
| 1302 |
} |
| 1303 |
|
| 1304 |
/* Re-check if filling the oldest file still takes too long, |
| 1305 |
because the oldest file might have become truncated. */ |
| 1306 |
|
| 1307 |
needs_more_intake = log_files_filling_oldest_file_takes_too_long(log); |
| 1308 |
} |
| 1309 |
|
| 1310 |
log_files_create_next_as_unused_if_needed(log); |
| 1311 |
|
| 1312 |
os_event_set(log.m_files_governor_iteration_event); |
| 1313 |
|
| 1314 |
return needs_more_intake ? Iteration_result::COMPLETED_BUT_NEED_MORE_INTAKE |
| 1315 |
: Iteration_result::COMPLETED; |
| 1316 |
} |
| 1317 |
|
| 1318 |
static void log_files_governor_iteration(log_t &log) { |
| 1319 |
using Iteration_result = Log_files_governor_iteration_result; |
| 1320 |
|
| 1321 |
/* We can't use log_writer_mutex_own() here, because it could return true |
| 1322 |
when the log_writer thread was inactive (gone). Note, that even though the |
| 1323 |
log_writer thread's activity is checked before calling this function, there |
| 1324 |
is no protection for that condition, so the log_writer thread could become |
| 1325 |
inactive meanwhile. Even in such case, this function might still need to |
| 1326 |
acquire the writer_mutex, in order to remove the oldest redo log files, |
| 1327 |
or to truncate the single file. |
| 1328 |
|
| 1329 |
Note, that the property that the log.writer_mutex hasn't been acquired yet, |
| 1330 |
is also important for the mechanism which generates dummy redo records, to |
| 1331 |
avoid a possible deadlock when there was no space in the log buffer. |
| 1332 |
|
| 1333 |
However, the function which generates dummy redo records could only be called |
| 1334 |
if the log_files_governor thread still hasn't promised not to generate dummy |
| 1335 |
redo records, and for such promise the log_writer thread is waiting before it |
| 1336 |
decides to stop and can become inactive. */ |
| 1337 |
ut_ad(!mutex_own(&log.writer_mutex)); |
| 1338 |
ut_ad(!log_files_mutex_own(log)); |
| 1339 |
|
| 1340 |
log_sync_point("log_before_file_consume"); |
| 1341 |
|
| 1342 |
Iteration_result result = log_files_governor_iteration_low(log, false); |
| 1343 |
|
| 1344 |
if (result == Iteration_result::RETRY_WITH_WRITER_MUTEX) { |
| 1345 |
IB_mutex_guard writer_latch{&(log.writer_mutex), UT_LOCATION_HERE}; |
| 1346 |
result = log_files_governor_iteration_low(log, true); |
| 1347 |
ut_a(result != Iteration_result::RETRY_WITH_WRITER_MUTEX); |
| 1348 |
} |
| 1349 |
|
| 1350 |
if (log.m_no_more_dummy_records_requested.load()) { |
| 1351 |
log.m_no_more_dummy_records_promised.store(true); |
| 1352 |
|
| 1353 |
} else if (result == Iteration_result::COMPLETED_BUT_NEED_MORE_INTAKE && |
| 1354 |
!log_free_check_is_required(log)) { |
| 1355 |
ut_ad(!log.m_no_more_dummy_records_promised.load()); |
| 1356 |
log_files_generate_dummy_records(log, LOG_FILES_DUMMY_INTAKE_SIZE); |
| 1357 |
} |
| 1358 |
} |
| 1359 |
|
| 1360 |
void log_files_governor(log_t *log_ptr) { |
| 1361 |
ut_a(log_ptr != nullptr); |
| 1362 |
log_t &log = *log_ptr; |
| 1363 |
|
| 1364 |
ut_d(log.m_files_governor_thd = create_internal_thd()); |
| 1365 |
|
| 1366 |
// We need to initialize a temporary to work around a gcc12 bug. |
| 1367 |
Log_files_stats tmp{}; |
| 1368 |
log.m_files_stats = tmp; |
| 1369 |
|
| 1370 |
while (true) { |
| 1371 |
/* We note down value of this event's sig_count before calling the |
| 1372 |
log_files_governor_iteration() to avoid waiting on the event after |
| 1373 |
the call is finished if the event was signalled meanwhile. |
| 1374 |
|
| 1375 |
The log_writer's finish is announced by log_stop_background_threads() |
| 1376 |
setting this event, so not to miss it, we note down sig_count before |
| 1377 |
checking log_writer's status. */ |
| 1378 |
const auto sig_count = os_event_reset(log.m_files_governor_event); |
| 1379 |
if (!log_writer_is_active()) { |
| 1380 |
break; |
| 1381 |
} |
| 1382 |
|
| 1383 |
log_files_governor_iteration(log); |
| 1384 |
os_event_wait_time_low(log.m_files_governor_event, |
| 1385 |
std::chrono::milliseconds{10}, sig_count); |
| 1386 |
} |
| 1387 |
|
| 1388 |
{ |
| 1389 |
IB_mutex_guard writer_latch{&(log.writer_mutex), UT_LOCATION_HERE}; |
| 1390 |
IB_mutex_guard files_latch{&(log.m_files_mutex), UT_LOCATION_HERE}; |
| 1391 |
log_files_update_capacity_limits(log); |
| 1392 |
log_files_mark_consumed_files(log); |
| 1393 |
log_files_process_consumed_files(log); |
| 1394 |
} |
| 1395 |
|
| 1396 |
ut_d(destroy_internal_thd(log.m_files_governor_thd)); |
| 1397 |
} |
| 1398 |
|
| 1399 |
void log_files_wait_for_next_file_available(log_t &log) { |
| 1400 |
log_files_mutex_enter(log); |
| 1401 |
|
| 1402 |
const auto sig_count = os_event_reset(log.m_file_removed_event); |
| 1403 |
|
| 1404 |
if (log_files_next_file_size(log) != 0) { |
| 1405 |
log_files_mutex_exit(log); |
| 1406 |
return; |
| 1407 |
} |
| 1408 |
|
| 1409 |
log.m_requested_files_consumption = true; |
| 1410 |
|
| 1411 |
log_files_mutex_exit(log); |
| 1412 |
|
| 1413 |
os_event_set(log.m_files_governor_event); |
| 1414 |
|
| 1415 |
log_sync_point("log_before_waiting_for_next_file"); |
| 1416 |
|
| 1417 |
/* Wait for 100ms or until some log file is removed. */ |
| 1418 |
os_event_wait_time_low(log.m_file_removed_event, |
| 1419 |
std::chrono::milliseconds{100}, sig_count); |
| 1420 |
} |
| 1421 |
|
| 1422 |
static dberr_t log_files_prepare_unused_file(log_t &log, Log_file_id file_id, |
| 1423 |
lsn_t start_lsn, |
| 1424 |
lsn_t checkpoint_lsn, |
| 1425 |
bool create_first_data_block, |
| 1426 |
os_offset_t &file_size) { |
| 1427 |
log_files_write_allowed_validate(log); |
| 1428 |
|
| 1429 |
file_size = log_files_next_file_size(log); |
| 1430 |
ut_a(file_size != 0); /* verified in log_files_produce_file() */ |
| 1431 |
|
| 1432 |
lsn_t end_lsn; |
| 1433 |
const bool end_lsn_can_be_computed = |
| 1434 |
log_file_compute_end_lsn(start_lsn, file_size, end_lsn); |
| 1435 |
ut_a(end_lsn_can_be_computed); |
| 1436 |
|
| 1437 |
ut_a(checkpoint_lsn == 0 || start_lsn <= checkpoint_lsn); |
| 1438 |
ut_a(checkpoint_lsn < end_lsn); |
| 1439 |
|
| 1440 |
dberr_t err; |
| 1441 |
|
| 1442 |
if (log.m_unused_files_count > 0) { |
| 1443 |
err = log_resize_unused_file(log.m_files_ctx, file_id, file_size); |
| 1444 |
if (err != DB_SUCCESS) { |
| 1445 |
return err; |
| 1446 |
} |
| 1447 |
|
| 1448 |
} else { |
| 1449 |
err = log_create_unused_file(log.m_files_ctx, file_id, file_size); |
| 1450 |
if (err != DB_SUCCESS) { |
| 1451 |
return err; |
| 1452 |
} |
| 1453 |
log.m_unused_files_count++; |
| 1454 |
} |
| 1455 |
|
| 1456 |
const Log_file_header header = log_files_prepare_header(log, start_lsn); |
| 1457 |
|
| 1458 |
auto file_handle = |
| 1459 |
Log_file::open(log.m_files_ctx, file_id, Log_file_access_mode::READ_WRITE, |
| 1460 |
log.m_encryption_metadata, Log_file_type::UNUSED); |
| 1461 |
if (file_handle.is_open()) { |
| 1462 |
err = log_file_header_write(file_handle, header); |
| 1463 |
} else { |
| 1464 |
err = DB_CANNOT_OPEN_FILE; |
| 1465 |
} |
| 1466 |
if (err != DB_SUCCESS) { |
| 1467 |
return err; |
| 1468 |
} |
| 1469 |
|
| 1470 |
RECOVERY_CRASH(9); |
| 1471 |
|
| 1472 |
/* Write the first checkpoint twice to overwrite both checkpoint headers. */ |
| 1473 |
err = log_files_write_checkpoint_low( |
| 1474 |
log, file_handle, Log_checkpoint_header_no::HEADER_1, checkpoint_lsn); |
| 1475 |
if (err != DB_SUCCESS) { |
| 1476 |
return err; |
| 1477 |
} |
| 1478 |
err = log_files_write_checkpoint_low( |
| 1479 |
log, file_handle, Log_checkpoint_header_no::HEADER_2, checkpoint_lsn); |
| 1480 |
if (err != DB_SUCCESS) { |
| 1481 |
return err; |
| 1482 |
} |
| 1483 |
|
| 1484 |
if (create_first_data_block) { |
| 1485 |
ut_a(checkpoint_lsn >= start_lsn); |
| 1486 |
err = log_files_write_first_data_block_low(log, file_handle, checkpoint_lsn, |
| 1487 |
start_lsn); |
| 1488 |
} else { |
| 1489 |
/* A new log file should have 0 data blocks written. This is guaranteed, |
| 1490 |
because the file either: |
| 1491 |
- became created and resized, in which case it is filled with 0x00, |
| 1492 |
- or became recycled, in which case it contains old data blocks, |
| 1493 |
which have smaller epoch_no or hdr_no field, marking end of recovery. |
| 1494 |
Note, that InnoDB cannot write the first data block as empty one here, |
| 1495 |
because it would potentially have an invalid first_rec_group field. */ |
| 1496 |
err = DB_SUCCESS; |
| 1497 |
} |
| 1498 |
|
| 1499 |
return err; |
| 1500 |
} |
| 1501 |
|
| 1502 |
static dberr_t log_files_create_file(log_t &log, Log_file_id file_id, |
| 1503 |
lsn_t start_lsn, lsn_t checkpoint_lsn, |
| 1504 |
bool create_first_data_block) { |
| 1505 |
log_files_write_allowed_validate(log); |
| 1506 |
|
| 1507 |
os_offset_t file_size; |
| 1508 |
dberr_t err = |
| 1509 |
log_files_prepare_unused_file(log, file_id, start_lsn, checkpoint_lsn, |
| 1510 |
create_first_data_block, file_size); |
| 1511 |
|
| 1512 |
const auto file_path = log_file_path(log.m_files_ctx, file_id); |
| 1513 |
|
| 1514 |
if (err != DB_SUCCESS) { |
| 1515 |
ib::error(ER_IB_MSG_LOG_FILE_PREPARE_ON_CREATE_FAILED, file_path.c_str(), |
| 1516 |
static_cast<int>(err), ulonglong{start_lsn}); |
| 1517 |
return err; |
| 1518 |
} |
| 1519 |
|
| 1520 |
RECOVERY_CRASH(10); |
| 1521 |
|
| 1522 |
err = log_mark_file_as_in_use(log.m_files_ctx, file_id); |
| 1523 |
|
| 1524 |
if (err != DB_SUCCESS) { |
| 1525 |
const auto unused_file_path = |
| 1526 |
log_file_path_for_unused_file(log.m_files_ctx, file_id); |
| 1527 |
ib::error(ER_IB_MSG_LOG_FILE_RENAME_ON_CREATE_FAILED, |
| 1528 |
unused_file_path.c_str(), file_path.c_str(), |
| 1529 |
static_cast<int>(err)); |
| 1530 |
return err; |
| 1531 |
} |
| 1532 |
|
| 1533 |
if (!log.m_files.empty()) { |
| 1534 |
auto it = log.m_files.end(); |
| 1535 |
it--; |
| 1536 |
ut_a(it->next_id() == file_id); |
| 1537 |
ut_a(it->m_end_lsn == start_lsn); |
| 1538 |
} |
| 1539 |
|
| 1540 |
log.m_files.add(file_id, file_size, start_lsn, false, |
| 1541 |
log.m_encryption_metadata); |
| 1542 |
log.m_unused_files_count--; |
| 1543 |
|
| 1544 |
RECOVERY_CRASH(11); |
| 1545 |
return DB_SUCCESS; |
| 1546 |
} |
| 1547 |
|
| 1548 |
dberr_t log_files_create(log_t &log, lsn_t flushed_lsn) { |
| 1549 |
log_files_initialize_on_empty_redo(log); |
| 1550 |
|
| 1551 |
dberr_t err; |
| 1552 |
ut_a(log_is_data_lsn(flushed_lsn)); |
| 1553 |
log_files_create_allowed_validate(); |
| 1554 |
RECOVERY_CRASH(8); |
| 1555 |
|
| 1556 |
/* Do not allow to create new log files if redo log directory isn't empty. */ |
| 1557 |
ut::vector<Log_file_id> listed_files; |
| 1558 |
err = log_list_existing_files(log.m_files_ctx, listed_files); |
| 1559 |
ut_a(err == DB_SUCCESS); |
| 1560 |
ut_a(listed_files.empty()); |
| 1561 |
|
| 1562 |
ut_a(log.m_format == Log_format::CURRENT); |
| 1563 |
ut_a(log.m_creator_name == LOG_HEADER_CREATOR_CURRENT); |
| 1564 |
|
| 1565 |
log.m_log_flags = Log_flags{0}; |
| 1566 |
log_file_header_set_flag(log.m_log_flags, LOG_HEADER_FLAG_NOT_INITIALIZED); |
| 1567 |
|
| 1568 |
log.m_log_uuid = log_generate_uuid(); |
| 1569 |
|
| 1570 |
/* Create the first checkpoint and flush headers of the first log |
| 1571 |
file (the flushed headers store information about the checkpoint, |
| 1572 |
format of redo log and that is neither created by mysqlbackup |
| 1573 |
nor by clone). */ |
| 1574 |
/* Start lsn stored in header of the first log file is divisible |
| 1575 |
by OS_FILE_LOG_BLOCK_SIZE. Also, we want the MTR data to start |
| 1576 |
immediately after the header. |
| 1577 |
To achieve this, flushed_lsn should point to header's end.*/ |
| 1578 |
|
| 1579 |
ut_a(flushed_lsn % OS_FILE_LOG_BLOCK_SIZE == LOG_BLOCK_HDR_SIZE); |
| 1580 |
const lsn_t file_start_lsn = flushed_lsn - LOG_BLOCK_HDR_SIZE; |
| 1581 |
|
| 1582 |
ut_a(log.last_checkpoint_lsn.load() == 0); |
| 1583 |
|
| 1584 |
err = log_files_create_file(log, LOG_FIRST_FILE_ID, file_start_lsn, |
| 1585 |
flushed_lsn, true); |
| 1586 |
if (err != DB_SUCCESS) { |
| 1587 |
return err; |
| 1588 |
} |
| 1589 |
|
| 1590 |
log.last_checkpoint_lsn.store(flushed_lsn); |
| 1591 |
|
| 1592 |
/* If the redo log is set to be encrypted, |
| 1593 |
initialize encryption information. */ |
| 1594 |
if (srv_redo_log_encrypt) { |
| 1595 |
if (!Encryption::check_keyring()) { |
| 1596 |
ib::error(ER_IB_MSG_1065); |
| 1597 |
return DB_ERROR; |
| 1598 |
} |
| 1599 |
if (log_encryption_generate_metadata(log) != DB_SUCCESS) { |
| 1600 |
return DB_ERROR; |
| 1601 |
} |
| 1602 |
} |
| 1603 |
|
| 1604 |
RECOVERY_CRASH(12); |
| 1605 |
|
| 1606 |
log_persist_initialized(log); |
| 1607 |
|
| 1608 |
ib::info(ER_IB_MSG_LOG_FILES_INITIALIZED, ulonglong{flushed_lsn}); |
| 1609 |
|
| 1610 |
RECOVERY_CRASH(13); |
| 1611 |
|
| 1612 |
return DB_SUCCESS; |
| 1613 |
} |
| 1614 |
|
| 1615 |
void log_files_remove(log_t &log) { |
| 1616 |
/* InnoDB doesn't want to end up in situation, that it removed redo files |
| 1617 |
and can't create new redo files. */ |
| 1618 |
log_files_create_allowed_validate(); |
| 1619 |
|
| 1620 |
if (log.m_current_file_handle.is_open()) { |
| 1621 |
log.m_current_file_handle.close(); |
| 1622 |
} |
| 1623 |
|
| 1624 |
/* Remove any old log files. */ |
| 1625 |
|
| 1626 |
#ifdef UNIV_DEBUG |
| 1627 |
dberr_t first_file_remove_err; |
| 1628 |
Log_file_id first_file_id; |
| 1629 |
std::tie(first_file_remove_err, first_file_id) = |
| 1630 |
log_remove_file(log.m_files_ctx); |
| 1631 |
ut_a(first_file_remove_err == DB_SUCCESS || |
| 1632 |
first_file_remove_err == DB_NOT_FOUND); |
| 1633 |
|
| 1634 |
/* Crashing after deleting the first |
| 1635 |
file should be recoverable. The buffer |
| 1636 |
pool was clean, and we can simply create |
| 1637 |
all log files from the scratch. */ |
| 1638 |
RECOVERY_CRASH(7); |
| 1639 |
#endif /* UNIV_DEBUG */ |
| 1640 |
|
| 1641 |
auto remove_files_ret = log_remove_files(log.m_files_ctx); |
| 1642 |
ut_a(remove_files_ret.first == DB_SUCCESS); |
| 1643 |
|
| 1644 |
#ifdef UNIV_DEBUG |
| 1645 |
if (first_file_remove_err == DB_SUCCESS) { |
| 1646 |
remove_files_ret.second.push_back(first_file_id); |
| 1647 |
} |
| 1648 |
#endif /* UNIV_DEBUG */ |
| 1649 |
|
| 1650 |
for (Log_file_id id : remove_files_ret.second) { |
| 1651 |
log.m_files.erase(id); |
| 1652 |
} |
| 1653 |
|
| 1654 |
const auto ret = log_remove_unused_files(log.m_files_ctx); |
| 1655 |
ut_a(ret.first == DB_SUCCESS); |
| 1656 |
|
| 1657 |
log.m_unused_files_count = 0; |
| 1658 |
} |
| 1659 |
|
| 1660 |
dberr_t log_files_start(log_t &log) { |
| 1661 |
ut_a(!log_writer_is_active()); |
| 1662 |
ut_a(!log_checkpointer_is_active()); |
| 1663 |
ut_a(!log_files_governor_is_active()); |
| 1664 |
|
| 1665 |
/** Existing log files are marked as not consumed. */ |
| 1666 |
log_files_for_each(log.m_files, |
| 1667 |
[](const Log_file &file) { ut_a(!file.m_consumed); }); |
| 1668 |
|
| 1669 |
if (srv_read_only_mode) { |
| 1670 |
log_update_exported_variables(log); |
| 1671 |
/* We are not allowed to consume in read-only mode. */ |
| 1672 |
return DB_SUCCESS; |
| 1673 |
} |
| 1674 |
|
| 1675 |
if (log.m_format < Log_format::VERSION_8_0_30) { |
| 1676 |
/* We are not allowed to consume log files of format |
| 1677 |
older than 8.0.30. */ |
| 1678 |
return DB_SUCCESS; |
| 1679 |
} |
| 1680 |
|
| 1681 |
ut_a(log_get_lsn(log) == log.write_lsn.load()); |
| 1682 |
ut_a(log_get_lsn(log) == log.flushed_to_disk_lsn.load()); |
| 1683 |
|
| 1684 |
log_files_update_current_file_low(log); |
| 1685 |
|
| 1686 |
log_files_mark_consumed_files(log); |
| 1687 |
|
| 1688 |
log_files_validate_not_consumed(log); |
| 1689 |
|
| 1690 |
ut_a(!srv_read_only_mode); |
| 1691 |
|
| 1692 |
log_files_initialize_on_existing_redo(log); |
| 1693 |
|
| 1694 |
/* It could happen that write_lsn was in a new file, but flushed_to_disk_lsn |
| 1695 |
was still in the previous file when server rebooted. In such case we might |
| 1696 |
have recovery ended in the previous file successfully and need to remove the |
| 1697 |
new file (containing some unflushed data). */ |
| 1698 |
const dberr_t err = log_files_remove_from_future(log); |
| 1699 |
if (err != DB_SUCCESS) { |
| 1700 |
return err; |
| 1701 |
} |
| 1702 |
|
| 1703 |
log_files_process_consumed_files(log); |
| 1704 |
|
| 1705 |
return DB_SUCCESS; |
| 1706 |
} |
| 1707 |
|
| 1708 |
static dberr_t log_files_mark_current_file_as_incomplete(log_t &log) { |
| 1709 |
log_files_write_allowed_validate(log); |
| 1710 |
auto header = log_files_prepare_header(log, log.m_current_file); |
| 1711 |
log_file_header_reset_flag(header.m_log_flags, LOG_HEADER_FLAG_FILE_FULL); |
| 1712 |
const dberr_t err = log_file_header_write(log.m_current_file_handle, header); |
| 1713 |
if (err != DB_SUCCESS) { |
| 1714 |
return err; |
| 1715 |
} |
| 1716 |
const std::string file_path = |
| 1717 |
log_file_path(log.m_files_ctx, log.m_current_file.m_id); |
| 1718 |
ib::info(ER_IB_MSG_LOG_FILE_MARK_CURRENT_AS_INCOMPLETE, file_path.c_str()); |
| 1719 |
log.m_current_file.m_full = false; |
| 1720 |
log.m_files.set_incomplete(log.m_current_file.m_id); |
| 1721 |
return DB_SUCCESS; |
| 1722 |
} |
| 1723 |
|
| 1724 |
static dberr_t log_files_mark_current_file_as_full(log_t &log) { |
| 1725 |
log_files_write_allowed_validate(log); |
| 1726 |
|
| 1727 |
const auto file_id = log.m_current_file.m_id; |
| 1728 |
|
| 1729 |
{ |
| 1730 |
bool found_newer = false; |
| 1731 |
for (const auto &f : log.m_files) { |
| 1732 |
if (f.m_id < file_id) { |
| 1733 |
ut_a(f.m_full); |
| 1734 |
} else { |
| 1735 |
ut_a(!f.m_full); |
| 1736 |
if (file_id < f.m_id) { |
| 1737 |
found_newer = true; |
| 1738 |
} |
| 1739 |
} |
| 1740 |
} |
| 1741 |
ut_a(found_newer); |
| 1742 |
} |
| 1743 |
|
| 1744 |
const auto file = log.m_files.file(file_id); |
| 1745 |
ut_a(file != log.m_files.end()); |
| 1746 |
|
| 1747 |
/* Prepare header with updated log_flags. */ |
| 1748 |
auto header = log_files_prepare_header(log, *file); |
| 1749 |
ut_a(!log_file_header_check_flag(header.m_log_flags, |
| 1750 |
LOG_HEADER_FLAG_FILE_FULL)); |
| 1751 |
log_file_header_set_flag(header.m_log_flags, LOG_HEADER_FLAG_FILE_FULL); |
| 1752 |
/* Flush to disk. */ |
| 1753 |
auto file_handle = file->open(Log_file_access_mode::WRITE_ONLY); |
| 1754 |
if (!file_handle.is_open()) { |
| 1755 |
return DB_CANNOT_OPEN_FILE; |
| 1756 |
} |
| 1757 |
const dberr_t err = log_file_header_write(file_handle, header); |
| 1758 |
if (err != DB_SUCCESS) { |
| 1759 |
return err; |
| 1760 |
} |
| 1761 |
/* Update in in-memory dictionary of log files. */ |
| 1762 |
log.m_current_file.m_full = true; |
| 1763 |
log.m_files.set_full(file_id); |
| 1764 |
return DB_SUCCESS; |
| 1765 |
} |
| 1766 |
|
| 1767 |
static dberr_t log_files_rewrite_old_headers( |
| 1768 |
log_t &log, Log_file_header_callback before_write, |
| 1769 |
Log_file_header_callback after_write) { |
| 1770 |
log_files_write_allowed_validate(log); |
| 1771 |
ut_a(!log.m_files.empty()); |
| 1772 |
|
| 1773 |
auto newest_it = log.m_files.end(); |
| 1774 |
--newest_it; |
| 1775 |
|
| 1776 |
for (const auto &file : log.m_files) { |
| 1777 |
if (file.m_id == newest_it->m_id) { |
| 1778 |
continue; |
| 1779 |
} |
| 1780 |
ut_a(file.m_id < newest_it->m_id); |
| 1781 |
|
| 1782 |
auto header = log_files_prepare_header(log, file); |
| 1783 |
before_write(file.m_id, header); |
| 1784 |
|
| 1785 |
auto file_handle = file.open(Log_file_access_mode::WRITE_ONLY); |
| 1786 |
if (!file_handle.is_open()) { |
| 1787 |
return DB_CANNOT_OPEN_FILE; |
| 1788 |
} |
| 1789 |
|
| 1790 |
const dberr_t err = log_file_header_write(file_handle, header); |
| 1791 |
if (err != DB_SUCCESS) { |
| 1792 |
return err; |
| 1793 |
} |
| 1794 |
|
| 1795 |
after_write(file.m_id, header); |
| 1796 |
} |
| 1797 |
return DB_SUCCESS; |
| 1798 |
} |
| 1799 |
|
| 1800 |
static dberr_t log_files_rewrite_newest_header( |
| 1801 |
log_t &log, Log_file_header_callback update_callback) { |
| 1802 |
log_files_write_allowed_validate(log); |
| 1803 |
ut_a(!log.m_files.empty()); |
| 1804 |
|
| 1805 |
auto it = log.m_files.end(); |
| 1806 |
--it; |
| 1807 |
|
| 1808 |
auto header = log_files_prepare_header(log, *it); |
| 1809 |
|
| 1810 |
update_callback(it->m_id, header); |
| 1811 |
|
| 1812 |
auto file_handle = it->open(Log_file_access_mode::WRITE_ONLY); |
| 1813 |
if (!file_handle.is_open()) { |
| 1814 |
return DB_CANNOT_OPEN_FILE; |
| 1815 |
} |
| 1816 |
|
| 1817 |
return log_file_header_write(file_handle, header); |
| 1818 |
} |
| 1819 |
|
| 1820 |
dberr_t log_files_persist_flags(log_t &log, Log_flags log_flags) { |
| 1821 |
const dberr_t err = log_files_rewrite_newest_header( |
| 1822 |
log, [&](Log_file_id, Log_file_header &header) { |
| 1823 |
ut_a(header.m_log_flags == log.m_log_flags); |
| 1824 |
header.m_log_flags = log_flags; |
| 1825 |
}); |
| 1826 |
if (err != DB_SUCCESS) { |
| 1827 |
return err; |
| 1828 |
} |
| 1829 |
log.m_log_flags = log_flags; |
| 1830 |
return DB_SUCCESS; |
| 1831 |
} |
| 1832 |
|
| 1833 |
dberr_t log_files_reset_creator_and_set_full(log_t &log) { |
| 1834 |
const std::string new_creator = LOG_HEADER_CREATOR_CURRENT; |
| 1835 |
|
| 1836 |
const dberr_t rewrite_old_err = log_files_rewrite_old_headers( |
| 1837 |
log, |
| 1838 |
[&](Log_file_id, Log_file_header &header) { |
| 1839 |
ut_a(header.m_creator_name == log.m_creator_name); |
| 1840 |
header.m_creator_name = new_creator; |
| 1841 |
log_file_header_set_flag(header.m_log_flags, LOG_HEADER_FLAG_FILE_FULL); |
| 1842 |
}, |
| 1843 |
[&](Log_file_id file_id, Log_file_header &) { |
| 1844 |
log.m_files.set_full(file_id); |
| 1845 |
}); |
| 1846 |
if (rewrite_old_err != DB_SUCCESS) { |
| 1847 |
return rewrite_old_err; |
| 1848 |
} |
| 1849 |
|
| 1850 |
const dberr_t rewrite_newest_err = log_files_rewrite_newest_header( |
| 1851 |
log, [&](Log_file_id, Log_file_header &header) { |
| 1852 |
ut_a(header.m_creator_name == log.m_creator_name); |
| 1853 |
header.m_creator_name = new_creator; |
| 1854 |
}); |
| 1855 |
if (rewrite_newest_err != DB_SUCCESS) { |
| 1856 |
return rewrite_newest_err; |
| 1857 |
} |
| 1858 |
|
| 1859 |
log.m_creator_name = new_creator; |
| 1860 |
return DB_SUCCESS; |
| 1861 |
} |
| 1862 |
|
| 1863 |
void log_files_update_encryption( |
| 1864 |
log_t &log, const Encryption_metadata &encryption_metadata) { |
| 1865 |
log_files_access_allowed_validate(log); |
| 1866 |
log.m_encryption_metadata = encryption_metadata; |
| 1867 |
} |
| 1868 |
|
| 1869 |
static void log_files_update_current_file_low(log_t &log) { |
| 1870 |
log_files_write_allowed_validate(log); |
| 1871 |
ut_ad(log_flusher_mutex_own(log)); |
| 1872 |
|
| 1873 |
const lsn_t newest_lsn = log_files_newest_needed_lsn(log); |
| 1874 |
ut_a(newest_lsn >= LOG_START_LSN); |
| 1875 |
|
| 1876 |
const auto it = log.m_files.find(newest_lsn); |
| 1877 |
ut_a(it != log.m_files.end()); |
| 1878 |
ut_a(!it->m_consumed); |
| 1879 |
|
| 1880 |
if (log.m_current_file_handle.is_open()) { |
| 1881 |
log.m_current_file_handle.close(); |
| 1882 |
} |
| 1883 |
|
| 1884 |
log.m_current_file = *it; |
| 1885 |
|
| 1886 |
log.m_current_file_handle = |
| 1887 |
log.m_current_file.open(Log_file_access_mode::WRITE_ONLY); |
| 1888 |
|
| 1889 |
ut_a(log.m_current_file_handle.is_open()); |
| 1890 |
|
| 1891 |
log_files_validate_current_file(log); |
| 1892 |
} |
| 1893 |
|
| 1894 |
static void log_files_generate_dummy_records(log_t &log, lsn_t min_bytes) { |
| 1895 |
ut_ad(log_writer_is_active()); |
| 1896 |
ut_ad(!log_writer_mutex_own(log)); |
| 1897 |
ut_ad(log_checkpointer_is_active()); |
| 1898 |
ut_ad(log.m_allow_checkpoints.load()); |
| 1899 |
ut_ad(!log_checkpointer_mutex_own(log)); |
| 1900 |
ut_ad(log_flusher_is_active()); |
| 1901 |
ut_ad(!log_flusher_mutex_own(log)); |
| 1902 |
ut_ad(!log_files_mutex_own(log)); |
| 1903 |
ut_ad(!log.m_no_more_dummy_records_promised.load()); |
| 1904 |
|
| 1905 |
ut_d(const lsn_t start_lsn = log_get_lsn(log)); |
| 1906 |
byte *buf; |
| 1907 |
mtr_t mtr; |
| 1908 |
mtr_start(&mtr); |
| 1909 |
lsn_t bytes_stored = 0; |
| 1910 |
while (bytes_stored < min_bytes && mlog_open(&mtr, 1, buf)) { |
| 1911 |
mach_write_to_1(buf, MLOG_DUMMY_RECORD); |
| 1912 |
mlog_close(&mtr, buf + 1); |
| 1913 |
mtr.added_rec(); |
| 1914 |
++bytes_stored; |
| 1915 |
} |
| 1916 |
mtr_commit(&mtr); |
| 1917 |
ut_ad(start_lsn + bytes_stored <= log_get_lsn(log)); |
| 1918 |
log_buffer_flush_to_disk(log, false); |
| 1919 |
} |
| 1920 |
|
| 1921 |
static bool log_files_is_truncate_allowed(const log_t &log) { |
| 1922 |
log_files_access_allowed_validate(log); |
| 1923 |
|
| 1924 |
/* It is guaranteed that checkpointer consumer is always there. |
| 1925 |
Note, that we cannot use log.m_consumers.find() because it would |
| 1926 |
expect Log_consumer* as argument and we can only provide the |
| 1927 |
const Log_consumer*, because we have const ref to log_t here. */ |
| 1928 |
ut_a(std::find_if(log.m_consumers.begin(), log.m_consumers.end(), |
| 1929 |
[&log](Log_consumer *consumer) { |
| 1930 |
return consumer == &log.m_checkpoint_consumer; |
| 1931 |
}) != log.m_consumers.end()); |
| 1932 |
|
| 1933 |
/* Allow truncation of redo files only if there are no other consumers |
| 1934 |
than redo log checkpointer. The truncation acquires the checkpointer |
| 1935 |
mutex. */ |
| 1936 |
return log.m_consumers.size() == 1; |
| 1937 |
} |
| 1938 |
|
| 1939 |
void log_files_dummy_records_request_disable(log_t &log) { |
| 1940 |
log.m_no_more_dummy_records_requested.store(true); |
| 1941 |
} |
| 1942 |
|
| 1943 |
void log_files_dummy_records_disable(log_t &log) { |
| 1944 |
log_files_dummy_records_request_disable(log); |
| 1945 |
while (!log.m_no_more_dummy_records_promised.load()) { |
| 1946 |
os_event_set(log.m_files_governor_event); |
| 1947 |
std::this_thread::sleep_for(std::chrono::microseconds(10)); |
| 1948 |
} |
| 1949 |
} |
| 1950 |
|
| 1951 |
static void log_files_truncate(log_t &log) { |
| 1952 |
log_files_write_allowed_validate(log); |
| 1953 |
ut_a(log_files_is_truncate_allowed(log)); |
| 1954 |
|
| 1955 |
/* We add one to prevent truncating the file to exactly the current write |
| 1956 |
position as we want to use log_files_update_current_file_low() which in |
| 1957 |
case lsn is at boundary between two files, tries to open the next */ |
| 1958 |
const os_offset_t end_offset = ut_uint64_align_up( |
| 1959 |
log.m_current_file.offset(log.write_lsn.load() + 1), UNIV_PAGE_SIZE); |
| 1960 |
|
| 1961 |
const os_offset_t new_size = |
| 1962 |
std::max(end_offset, log.m_capacity.next_file_size()); |
| 1963 |
|
| 1964 |
if (log.m_current_file.m_size_in_bytes <= new_size) { |
| 1965 |
return; |
| 1966 |
} |
| 1967 |
|
| 1968 |
IB_mutex_guard flusher_latch{&(log.flusher_mutex), UT_LOCATION_HERE}; |
| 1969 |
|
| 1970 |
if (log.m_current_file_handle.is_open()) { |
| 1971 |
log.m_current_file_handle.close(); |
| 1972 |
} |
| 1973 |
|
| 1974 |
const auto file_path = |
| 1975 |
log_file_path(log.m_files_ctx, log.m_current_file.m_id); |
| 1976 |
ib::info(ER_IB_MSG_LOG_FILE_TRUNCATE, file_path.c_str()); |
| 1977 |
|
| 1978 |
const dberr_t err = |
| 1979 |
log_resize_file(log.m_files_ctx, log.m_current_file.m_id, new_size); |
| 1980 |
ut_a(err == DB_SUCCESS); |
| 1981 |
|
| 1982 |
log.m_files.set_size(log.m_current_file.m_id, new_size); |
| 1983 |
|
| 1984 |
log_files_update_current_file_low(log); |
| 1985 |
log.write_ahead_end_offset = 0; |
| 1986 |
|
| 1987 |
log_files_update_capacity_limits(log); |
| 1988 |
} |
| 1989 |
|
| 1990 |
/** @} */ |
| 1991 |
|
| 1992 |
/**************************************************/ /** |
| 1993 |
|
| 1994 |
@name Log - files initialization and handling sysvar updates |
| 1995 |
|
| 1996 |
*******************************************************/ |
| 1997 |
|
| 1998 |
/** @{ */ |
| 1999 |
|
| 2000 |
/** Computes initial capacity limits and size suggested for the next log file. |
| 2001 |
Called after existing redo log files have been discovered (log.m_files), or |
| 2002 |
when logically empty redo log is being initialized. |
| 2003 |
|
| 2004 |
Requirement: srv_is_being_started is true. |
| 2005 |
|
| 2006 |
@param[in,out] log redo log |
| 2007 |
@param[in] current_logical_size current logical size of the redo log |
| 2008 |
@param[in] current_checkpoint_age current checkpoint age */ |
| 2009 |
static void log_files_initialize(log_t &log, lsn_t current_logical_size, |
| 2010 |
lsn_t current_checkpoint_age) { |
| 2011 |
ut_a(srv_is_being_started); |
| 2012 |
ut_a(log.m_files_ctx.m_files_ruleset == Log_files_ruleset::CURRENT); |
| 2013 |
log.m_capacity.initialize(log.m_files, current_logical_size, |
| 2014 |
current_checkpoint_age); |
| 2015 |
log_update_limits_low(log); |
| 2016 |
log_update_exported_variables(log); |
| 2017 |
} |
| 2018 |
|
| 2019 |
void log_files_initialize_on_empty_redo(log_t &log) { |
| 2020 |
log_files_initialize(log, 0, 0); |
| 2021 |
} |
| 2022 |
|
| 2023 |
void log_files_initialize_on_existing_redo(log_t &log) { |
| 2024 |
lsn_t logical_size, checkpoint_age; |
| 2025 |
std::tie(logical_size, checkpoint_age) = |
| 2026 |
log_files_logical_size_and_checkpoint_age(log); |
| 2027 |
|
| 2028 |
log_files_initialize(log, logical_size, checkpoint_age); |
| 2029 |
} |
| 2030 |
|
| 2031 |
/** Waits until the log_files_governor performs a next iteration of its loop. |
| 2032 |
Notifies the log_files_governor thread (to ensure it is soon). |
| 2033 |
@param[in,out] log redo log */ |
| 2034 |
static void log_files_wait_until_next_governor_iteration(log_t &log) { |
| 2035 |
const auto sig_count = os_event_reset(log.m_files_governor_iteration_event); |
| 2036 |
os_event_set(log.m_files_governor_event); |
| 2037 |
os_event_wait_low(log.m_files_governor_iteration_event, sig_count); |
| 2038 |
} |
| 2039 |
|
| 2040 |
void log_files_resize_requested(log_t &log) { |
| 2041 |
log_files_wait_until_next_governor_iteration(log); |
| 2042 |
} |
| 2043 |
|
| 2044 |
void log_files_thread_concurrency_updated(log_t &log) { |
| 2045 |
log_files_wait_until_next_governor_iteration(log); |
| 2046 |
} |
| 2047 |
|
| 2048 |
/** @} */ |
| 2049 |
|
| 2050 |
#endif /* !UNIV_HOTBACKUP */ |