| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/**************************************************/ /** |
| 29 |
@file include/log0log.h |
| 30 |
|
| 31 |
Redo log - the main header. |
| 32 |
|
| 33 |
Basic types are defined inside log0types.h. |
| 34 |
|
| 35 |
Constant values are defined inside log0constants.h, but that |
| 36 |
file should only be included by log0types.h. |
| 37 |
|
| 38 |
The log_sys is defined in log0sys.h. |
| 39 |
|
| 40 |
Functions related to the log buffer are declared in log0buf.h. |
| 41 |
|
| 42 |
Functions related to the checkpoints are declared in log0chkp.h. |
| 43 |
|
| 44 |
Functions related to the writer/flusher are declared in log0write.h. |
| 45 |
|
| 46 |
Functions computing capacity of redo and related margins are declared |
| 47 |
in log0files_capacity.h. |
| 48 |
|
| 49 |
Functions doing IO to log files and formatting log blocks are declared |
| 50 |
in log0files_io.h. |
| 51 |
|
| 52 |
*******************************************************/ |
| 53 |
|
| 54 |
#ifndef log0log_h |
| 55 |
#define log0log_h |
| 56 |
|
| 57 |
#include "log0files_capacity.h" |
| 58 |
#include "log0files_dict.h" |
| 59 |
#include "log0files_finder.h" |
| 60 |
#include "log0files_governor.h" |
| 61 |
#include "log0files_io.h" |
| 62 |
#include "log0sys.h" |
| 63 |
#include "log0types.h" |
| 64 |
|
| 65 |
/**************************************************/ /** |
| 66 |
|
| 67 |
@name Log - LSN computations. |
| 68 |
|
| 69 |
*******************************************************/ |
| 70 |
|
| 71 |
/** @{ */ |
| 72 |
|
| 73 |
/** Calculates lsn value for given sn value. Sequence of sn values |
| 74 |
enumerate all data bytes in the redo log. Sequence of lsn values |
| 75 |
enumerate all data bytes and bytes used for headers and footers |
| 76 |
of all log blocks in the redo log. For every LOG_BLOCK_DATA_SIZE |
| 77 |
bytes of data we have OS_FILE_LOG_BLOCK_SIZE bytes in the redo log. |
| 78 |
NOTE that LOG_BLOCK_DATA_SIZE + LOG_BLOCK_HDR_SIZE + LOG_BLOCK_TRL_SIZE |
| 79 |
== OS_FILE_LOG_BLOCK_SIZE. The calculated lsn value will always point |
| 80 |
to some data byte (will be % OS_FILE_LOG_BLOCK_SIZE >= LOG_BLOCK_HDR_SIZE, |
| 81 |
and < OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE). |
| 82 |
|
| 83 |
@param[in] sn sn value |
| 84 |
@return lsn value for the provided sn value */ |
| 85 |
constexpr inline lsn_t log_translate_sn_to_lsn(sn_t sn) { |
| 86 |
return sn / LOG_BLOCK_DATA_SIZE * OS_FILE_LOG_BLOCK_SIZE + |
| 87 |
sn % LOG_BLOCK_DATA_SIZE + LOG_BLOCK_HDR_SIZE; |
| 88 |
} |
| 89 |
|
| 90 |
/** Calculates sn value for given lsn value. |
| 91 |
@see log_translate_sn_to_lsn |
| 92 |
@param[in] lsn lsn value |
| 93 |
@return sn value for the provided lsn value */ |
| 94 |
inline sn_t log_translate_lsn_to_sn(lsn_t lsn) { |
| 95 |
/* Calculate sn of the beginning of log block, which contains |
| 96 |
the provided lsn value. */ |
| 97 |
const sn_t sn = lsn / OS_FILE_LOG_BLOCK_SIZE * LOG_BLOCK_DATA_SIZE; |
| 98 |
|
| 99 |
/* Calculate offset for the provided lsn within the log block. |
| 100 |
The offset includes LOG_BLOCK_HDR_SIZE bytes of block's header. */ |
| 101 |
const uint32_t diff = lsn % OS_FILE_LOG_BLOCK_SIZE; |
| 102 |
|
| 103 |
if (diff < LOG_BLOCK_HDR_SIZE) { |
| 104 |
/* The lsn points to some bytes inside the block's header. |
| 105 |
Return sn for the beginning of the block. Note, that sn |
| 106 |
values don't enumerate bytes of blocks' headers, so the |
| 107 |
value of diff does not matter at all. */ |
| 108 |
return sn; |
| 109 |
} |
| 110 |
|
| 111 |
if (diff > OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE) { |
| 112 |
/* The lsn points to some bytes inside the block's footer. |
| 113 |
Return sn for the beginning of the next block. Note, that |
| 114 |
sn values don't enumerate bytes of blocks' footer, so the |
| 115 |
value of diff does not matter at all. */ |
| 116 |
return sn + LOG_BLOCK_DATA_SIZE; |
| 117 |
} |
| 118 |
|
| 119 |
/* Add the offset but skip bytes of block's header. */ |
| 120 |
return sn + diff - LOG_BLOCK_HDR_SIZE; |
| 121 |
} |
| 122 |
|
| 123 |
/** Validates a given lsn value. Checks if the lsn value points to data |
| 124 |
bytes inside log block (not to some bytes in header/footer). It is used |
| 125 |
by assertions. |
| 126 |
@return true if lsn points to data bytes within log block */ |
| 127 |
inline bool log_is_data_lsn(lsn_t lsn) { |
| 128 |
const uint32_t offset = lsn % OS_FILE_LOG_BLOCK_SIZE; |
| 129 |
|
| 130 |
return lsn >= LOG_START_LSN && offset >= LOG_BLOCK_HDR_SIZE && |
| 131 |
offset < OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE; |
| 132 |
} |
| 133 |
|
| 134 |
/** @} */ |
| 135 |
|
| 136 |
#ifndef UNIV_HOTBACKUP |
| 137 |
|
| 138 |
/**************************************************/ /** |
| 139 |
|
| 140 |
@name Log - general functions. |
| 141 |
|
| 142 |
*******************************************************/ |
| 143 |
|
| 144 |
/** @{ */ |
| 145 |
|
| 146 |
/** @return consistent sn value for locked state */ |
| 147 |
static inline sn_t log_get_sn(const log_t &log) { |
| 148 |
const sn_t sn = log.sn.load(); |
| 149 |
if ((sn & SN_LOCKED) != 0) { |
| 150 |
return log.sn_locked.load(); |
| 151 |
} else { |
| 152 |
return sn; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/** Gets the current lsn value. This value points to the first non |
| 157 |
reserved data byte in the redo log. When next user thread reserves |
| 158 |
space in the redo log, it starts at this lsn. |
| 159 |
|
| 160 |
If the last reservation finished exactly before footer of log block, |
| 161 |
this value points to the first byte after header of the next block. |
| 162 |
|
| 163 |
@note It is possible that the current lsn value does not fit free |
| 164 |
space in the log files or in the log buffer. In such case, user |
| 165 |
threads need to wait until the space becomes available. |
| 166 |
|
| 167 |
@return current lsn */ |
| 168 |
inline lsn_t log_get_lsn(const log_t &log) { |
| 169 |
return log_translate_sn_to_lsn(log_get_sn(log)); |
| 170 |
} |
| 171 |
|
| 172 |
/** Waits until there is free space for range of sn values ending |
| 173 |
at the provided sn, in both the log buffer and in the log files. |
| 174 |
@param[in] log redo log |
| 175 |
@param[in] end_sn end of the range of sn values */ |
| 176 |
void log_wait_for_space(log_t &log, sn_t end_sn); |
| 177 |
|
| 178 |
/** Prints information about important lsn values used in the redo log, |
| 179 |
and some statistics about speed of writing and flushing of data. |
| 180 |
@param[in] log redo log for which print information |
| 181 |
@param[out] file file where to print */ |
| 182 |
void log_print(const log_t &log, FILE *file); |
| 183 |
|
| 184 |
/** Refreshes the statistics used to print per-second averages in log_print(). |
| 185 |
@param[in,out] log redo log */ |
| 186 |
void log_refresh_stats(log_t &log); |
| 187 |
|
| 188 |
void log_update_exported_variables(const log_t &log); |
| 189 |
|
| 190 |
/** @} */ |
| 191 |
|
| 192 |
/**************************************************/ /** |
| 193 |
|
| 194 |
@name Log - initialization of the redo log system. |
| 195 |
|
| 196 |
*******************************************************/ |
| 197 |
|
| 198 |
/** @{ */ |
| 199 |
|
| 200 |
/** Initializes log_sys and finds existing redo log files, or creates a new |
| 201 |
set of redo log files. |
| 202 |
|
| 203 |
New redo log files are created in following cases: |
| 204 |
- there are no existing redo log files in the log directory, |
| 205 |
- existing set of redo log files is not marked as fully initialized |
| 206 |
(flag LOG_HEADER_FLAG_NOT_INITIALIZED exists in the newest file). |
| 207 |
|
| 208 |
After this call, the log_sys global variable is allocated and initialized. |
| 209 |
InnoDB might start recovery then. |
| 210 |
|
| 211 |
@remarks |
| 212 |
The redo log files are not resized in this function, because before resizing |
| 213 |
log files, InnoDB must run recovery and ensure log files are logically empty. |
| 214 |
The redo resize is currently the only scenario in which the initialized log_sys |
| 215 |
might become closed by log_sys_close() and then re-initialized by another call |
| 216 |
to log_sys_init(). |
| 217 |
|
| 218 |
@note Note that the redo log system is NOT ready for user writes after this |
| 219 |
call is finished. The proper order of calls looks like this: |
| 220 |
- log_sys_init(), |
| 221 |
- log_start(), |
| 222 |
- log_start_background_threads() |
| 223 |
and this sequence is executed inside srv_start() in srv0start.cc (interleaved |
| 224 |
with remaining logic of the srv_start()) |
| 225 |
|
| 226 |
@note Note this function also verifies that REDO logs are in known format. |
| 227 |
|
| 228 |
@param[in] expect_no_files true means we should return DB_ERROR if log |
| 229 |
files are present in the directory before |
| 230 |
proceeding any further |
| 231 |
@param[in] flushed_lsn lsn at which new redo log files might be |
| 232 |
started if they had to be created during |
| 233 |
this call; this should be lsn stored in |
| 234 |
the system tablespace header at offset |
| 235 |
FIL_PAGE_FILE_FLUSH_LSN if the data |
| 236 |
directory has been initialized; |
| 237 |
@param[out] new_files_lsn updated to the lsn of the first checkpoint |
| 238 |
created in the new log files if new log files |
| 239 |
are created; else: 0 |
| 240 |
@return DB_SUCCESS or error */ |
| 241 |
dberr_t log_sys_init(bool expect_no_files, lsn_t flushed_lsn, |
| 242 |
lsn_t &new_files_lsn); |
| 243 |
|
| 244 |
/** Starts the initialized redo log system using a provided |
| 245 |
checkpoint_lsn and current lsn. Block for current_lsn must |
| 246 |
be properly initialized in the log buffer prior to calling |
| 247 |
this function. Therefore a proper value of first_rec_group |
| 248 |
must be set for that block before log_start is called. |
| 249 |
@param[in,out] log redo log |
| 250 |
@param[in] checkpoint_lsn checkpoint lsn |
| 251 |
@param[in] start_lsn current lsn to start at |
| 252 |
@param[in] allow_checkpoints true iff allows writing newer checkpoints |
| 253 |
@return DB_SUCCESS or error */ |
| 254 |
dberr_t log_start(log_t &log, lsn_t checkpoint_lsn, lsn_t start_lsn, |
| 255 |
bool allow_checkpoints = true); |
| 256 |
|
| 257 |
/** Close the log system and free all the related memory. */ |
| 258 |
void log_sys_close(); |
| 259 |
|
| 260 |
/** Resizes the write ahead buffer in the redo log. |
| 261 |
@param[in,out] log redo log |
| 262 |
@param[in] new_size new size (in bytes) */ |
| 263 |
void log_write_ahead_resize(log_t &log, size_t new_size); |
| 264 |
|
| 265 |
/** @} */ |
| 266 |
|
| 267 |
/**************************************************/ /** |
| 268 |
|
| 269 |
@name Log - the log threads and mutexes |
| 270 |
|
| 271 |
*******************************************************/ |
| 272 |
|
| 273 |
/** @{ */ |
| 274 |
|
| 275 |
/** Validates that all the log background threads are active. |
| 276 |
Used only to assert, that the state is correct. |
| 277 |
@param[in] log redo log */ |
| 278 |
void log_background_threads_active_validate(const log_t &log); |
| 279 |
|
| 280 |
/** Validates that all the log background threads are inactive. |
| 281 |
Used only to assert, that the state is correct. */ |
| 282 |
void log_background_threads_inactive_validate(); |
| 283 |
|
| 284 |
/** Starts all the log background threads. This can be called only, |
| 285 |
when the threads are inactive. This should never be called concurrently. |
| 286 |
This may not be called during read-only mode. |
| 287 |
@param[in,out] log redo log */ |
| 288 |
void log_start_background_threads(log_t &log); |
| 289 |
|
| 290 |
/** Stops all the log background threads. This can be called only, |
| 291 |
when the threads are active. This should never be called concurrently. |
| 292 |
This may not be called in read-only mode. Note that is is impossible |
| 293 |
to start log background threads in such case. |
| 294 |
@param[in,out] log redo log */ |
| 295 |
void log_stop_background_threads(log_t &log); |
| 296 |
|
| 297 |
/** Marks the flag which tells log threads to stop and wakes them. |
| 298 |
Does not wait until they are stopped. |
| 299 |
@param[in,out] log redo log */ |
| 300 |
void log_stop_background_threads_nowait(log_t &log); |
| 301 |
|
| 302 |
/** Function similar to @see log_stop_background_threads() except that it |
| 303 |
stops all the log threads in such a way, that the redo log will be logically |
| 304 |
empty after the threads are stopped. |
| 305 |
@note It is caller responsibility to ensure that all threads other than the |
| 306 |
log_files_governor cannot produce new redo log records when this function |
| 307 |
is being called. */ |
| 308 |
void log_make_empty_and_stop_background_threads(log_t &log); |
| 309 |
|
| 310 |
/** Wakes up all log threads which are alive. |
| 311 |
@param[in,out] log redo log */ |
| 312 |
void log_wake_threads(log_t &log); |
| 313 |
|
| 314 |
#define log_limits_mutex_enter(log) mutex_enter(&((log).limits_mutex)) |
| 315 |
|
| 316 |
#define log_limits_mutex_exit(log) mutex_exit(&((log).limits_mutex)) |
| 317 |
|
| 318 |
#define log_limits_mutex_own(log) mutex_own(&(log).limits_mutex) |
| 319 |
|
| 320 |
/** @} */ |
| 321 |
|
| 322 |
/**************************************************/ /** |
| 323 |
|
| 324 |
@name Log - the log position locking. |
| 325 |
|
| 326 |
*******************************************************/ |
| 327 |
|
| 328 |
/** @{ */ |
| 329 |
|
| 330 |
/** Lock redo log. Both current lsn and checkpoint lsn will not change |
| 331 |
until the redo log is unlocked. |
| 332 |
@param[in,out] log redo log to lock */ |
| 333 |
void log_position_lock(log_t &log); |
| 334 |
|
| 335 |
/** Unlock the locked redo log. |
| 336 |
@param[in,out] log redo log to unlock */ |
| 337 |
void log_position_unlock(log_t &log); |
| 338 |
|
| 339 |
/** Collect coordinates in the locked redo log. |
| 340 |
@param[in] log locked redo log |
| 341 |
@param[out] current_lsn stores current lsn there |
| 342 |
@param[out] checkpoint_lsn stores checkpoint lsn there */ |
| 343 |
void log_position_collect_lsn_info(const log_t &log, lsn_t *current_lsn, |
| 344 |
lsn_t *checkpoint_lsn); |
| 345 |
|
| 346 |
/** @} */ |
| 347 |
|
| 348 |
/**************************************************/ /** |
| 349 |
|
| 350 |
@name Log - persisting the flags. |
| 351 |
|
| 352 |
*******************************************************/ |
| 353 |
|
| 354 |
/** @{ */ |
| 355 |
|
| 356 |
/** Disable redo logging and persist the information. |
| 357 |
@param[in,out] log redo log */ |
| 358 |
void log_persist_disable(log_t &log); |
| 359 |
|
| 360 |
/** Enable redo logging and persist the information. |
| 361 |
@param[in,out] log redo log */ |
| 362 |
void log_persist_enable(log_t &log); |
| 363 |
|
| 364 |
/** Persist the information that it is safe to restart server. |
| 365 |
@param[in,out] log redo log */ |
| 366 |
void log_persist_crash_safe(log_t &log); |
| 367 |
|
| 368 |
/** Marks the redo log files as belonging to the initialized data directory |
| 369 |
with initialized set of redo log files. Flushes the log_flags without the |
| 370 |
flag LOG_HEADER_FLAG_NOT_INITIALIZED to the newest redo log file. |
| 371 |
@param[in,out] log redo log */ |
| 372 |
void log_persist_initialized(log_t &log); |
| 373 |
|
| 374 |
/** Asserts that the log is not marked as crash-unsafe. |
| 375 |
@param[in,out] log redo log */ |
| 376 |
void log_crash_safe_validate(log_t &log); |
| 377 |
|
| 378 |
/** @} */ |
| 379 |
|
| 380 |
#endif /* !UNIV_HOTBACKUP */ |
| 381 |
|
| 382 |
#endif /* !log0log_h */ |