| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2009, Google Inc. |
| 5 |
|
| 6 |
Portions of this file contain modifications contributed and copyrighted by |
| 7 |
Google, Inc. Those modifications are gratefully acknowledged and are described |
| 8 |
briefly in the InnoDB documentation. The contributions by Google are |
| 9 |
incorporated with their permission, and subject to the conditions contained in |
| 10 |
the file COPYING.Google. |
| 11 |
|
| 12 |
This program is free software; you can redistribute it and/or modify it under |
| 13 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 14 |
Free Software Foundation. |
| 15 |
|
| 16 |
This program is designed to work with certain software (including |
| 17 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 18 |
as designated in a particular file or component or in included license |
| 19 |
documentation. The authors of MySQL hereby grant you an additional |
| 20 |
permission to link the program and your derivative works with the |
| 21 |
separately licensed software that they have either included with |
| 22 |
the program or referenced in the documentation. |
| 23 |
|
| 24 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 25 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 26 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 27 |
for more details. |
| 28 |
|
| 29 |
You should have received a copy of the GNU General Public License along with |
| 30 |
this program; if not, write to the Free Software Foundation, Inc., |
| 31 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 32 |
|
| 33 |
*****************************************************************************/ |
| 34 |
|
| 35 |
/**************************************************/ /** |
| 36 |
@file include/log0buf.h |
| 37 |
|
| 38 |
Redo log functions related to the log buffer. |
| 39 |
|
| 40 |
*******************************************************/ |
| 41 |
|
| 42 |
#ifndef log0buf_h |
| 43 |
#define log0buf_h |
| 44 |
|
| 45 |
/* MEB should neither write to the log buffer nor maintain |
| 46 |
the log buffer or recent_written / recent_closed buffers. */ |
| 47 |
#ifndef UNIV_HOTBACKUP |
| 48 |
|
| 49 |
/* log.recent_written, log.recent_closed */ |
| 50 |
#include "log0sys.h" |
| 51 |
|
| 52 |
/* log_t&, lsn_t, constants */ |
| 53 |
#include "log0types.h" |
| 54 |
|
| 55 |
/**************************************************/ /** |
| 56 |
|
| 57 |
@name Log - writing to the log buffer. |
| 58 |
|
| 59 |
@remarks |
| 60 |
These functions are designed for mtr_commit(), |
| 61 |
and used only there (except log0log-t.cc). |
| 62 |
|
| 63 |
*******************************************************/ |
| 64 |
|
| 65 |
/** @{ */ |
| 66 |
|
| 67 |
/** Handle which is used for writes to the log buffer. */ |
| 68 |
struct Log_handle { |
| 69 |
/** LSN of the first data byte. */ |
| 70 |
lsn_t start_lsn; |
| 71 |
|
| 72 |
/** LSN after the last data byte. */ |
| 73 |
lsn_t end_lsn; |
| 74 |
}; |
| 75 |
|
| 76 |
/** Acquires the log buffer x-lock. |
| 77 |
@param[in,out] log redo log */ |
| 78 |
void log_buffer_x_lock_enter(log_t &log); |
| 79 |
|
| 80 |
/** Releases the log buffer x-lock. |
| 81 |
@param[in,out] log redo log */ |
| 82 |
void log_buffer_x_lock_exit(log_t &log); |
| 83 |
|
| 84 |
/** Reserves space in the redo log for following write operations. |
| 85 |
Space is reserved for a given number of data bytes. Additionally |
| 86 |
bytes for required headers and footers of log blocks are reserved. |
| 87 |
|
| 88 |
After the space is reserved, range of lsn values from a start_lsn |
| 89 |
to an end_lsn is assigned. The log writer thread cannot proceed |
| 90 |
further than to the start_lsn, until a link start_lsn -> end_lsn |
| 91 |
has been added to the log recent written buffer. |
| 92 |
|
| 93 |
NOTE that the link is added after data is written to the reserved |
| 94 |
space in the log buffer. It is very critical to do all these steps |
| 95 |
as fast as possible, because very likely the log writer thread is |
| 96 |
waiting for the link. |
| 97 |
|
| 98 |
@param[in,out] log redo log |
| 99 |
@param[in] len number of data bytes to reserve for write |
| 100 |
@return handle that represents the reservation */ |
| 101 |
Log_handle log_buffer_reserve(log_t &log, size_t len); |
| 102 |
|
| 103 |
/** Writes data to the log buffer. The space in the redo log has to be |
| 104 |
reserved before calling to this function and lsn pointing to inside the |
| 105 |
reserved range of lsn values has to be provided. |
| 106 |
|
| 107 |
The write does not have to cover the whole reserved space, but may not |
| 108 |
overflow it. If it does not cover, then returned value should be used |
| 109 |
to start the next write operation. Note that finally we must use exactly |
| 110 |
all the reserved space. |
| 111 |
|
| 112 |
@param[in,out] log redo log |
| 113 |
@param[in] str memory to write data from |
| 114 |
@param[in] str_len number of bytes to write |
| 115 |
@param[in] start_lsn lsn to start writing at (the reserved space) |
| 116 |
|
| 117 |
@return end_lsn after writing the data (in the reserved space), could be |
| 118 |
used to start the next write operation if there is still free space in |
| 119 |
the reserved space */ |
| 120 |
lsn_t log_buffer_write(log_t &log, const byte *str, size_t str_len, |
| 121 |
lsn_t start_lsn); |
| 122 |
|
| 123 |
/** Adds a link start_lsn -> end_lsn to the log recent written buffer. |
| 124 |
|
| 125 |
This function must be called after the data has been written to the |
| 126 |
fragment of log buffer represented by range [start_lsn, end_lsn). |
| 127 |
After the link is added, the log writer may write the data to disk. |
| 128 |
|
| 129 |
NOTE that still dirty pages for the [start_lsn, end_lsn) are not added |
| 130 |
to flush lists when this function is called. |
| 131 |
|
| 132 |
@param[in,out] log redo log |
| 133 |
@param[in] start_lsn start_lsn of the link to add |
| 134 |
@param[in] end_lsn end_lsn of the link to add */ |
| 135 |
void log_buffer_write_completed(log_t &log, lsn_t start_lsn, lsn_t end_lsn); |
| 136 |
|
| 137 |
/** Modifies header of log block in the log buffer, which contains |
| 138 |
a given lsn value, and sets offset to the first group of log records |
| 139 |
within the block. |
| 140 |
|
| 141 |
This is used by mtr after writing a log record group which ends at |
| 142 |
lsn belonging to different log block than lsn at which the group was |
| 143 |
started. When write was finished at the last data byte of log block, |
| 144 |
it is considered ended in the next log block, because the next data |
| 145 |
byte belongs to that block. |
| 146 |
|
| 147 |
During recovery, when recovery is started in the middle of some group |
| 148 |
of log records, it first looks for the beginning of the next group. |
| 149 |
|
| 150 |
@param[in,out] log redo log |
| 151 |
@param[in] rec_group_end_lsn lsn at which the first log record |
| 152 |
group starts within the block containing this lsn value */ |
| 153 |
void log_buffer_set_first_record_group(log_t &log, lsn_t rec_group_end_lsn); |
| 154 |
|
| 155 |
/** Adds a link start_lsn -> end_lsn to the log recent closed buffer. |
| 156 |
|
| 157 |
This is called after all dirty pages related to [start_lsn, end_lsn) |
| 158 |
have been added to corresponding flush lists. |
| 159 |
For detailed explanation - @see log0write.cc. |
| 160 |
|
| 161 |
@param[in,out] log redo log |
| 162 |
@param[in] handle handle for the reservation of space */ |
| 163 |
void log_buffer_close(log_t &log, const Log_handle &handle); |
| 164 |
|
| 165 |
/** @} */ |
| 166 |
|
| 167 |
/**************************************************/ /** |
| 168 |
|
| 169 |
@name Log - management of the log buffer. |
| 170 |
|
| 171 |
*******************************************************/ |
| 172 |
|
| 173 |
/** @{ */ |
| 174 |
|
| 175 |
/** Updates limit used when writing to log buffer. Note that the |
| 176 |
log buffer may have space for log records for which we still do |
| 177 |
not have space in log files (for larger lsn values). |
| 178 |
@param[in,out] log redo log */ |
| 179 |
void log_update_buf_limit(log_t &log); |
| 180 |
|
| 181 |
/** Updates limit used when writing to log buffer, according to provided |
| 182 |
write_lsn. It must be <= log.write_lsn.load() to protect from log buffer |
| 183 |
overwrites. |
| 184 |
@param[in,out] log redo log |
| 185 |
@param[in] write_lsn value <= log.write_lsn.load() */ |
| 186 |
void log_update_buf_limit(log_t &log, lsn_t write_lsn); |
| 187 |
|
| 188 |
/** Write to the log file up to the last log entry. |
| 189 |
@param[in,out] log redo log |
| 190 |
@param[in] sync whether we want the written log |
| 191 |
also to be flushed to disk. */ |
| 192 |
void log_buffer_flush_to_disk(log_t &log, bool sync = true); |
| 193 |
|
| 194 |
/** Requests flush of the log buffer. |
| 195 |
@param[in] sync true: wait until the flush is done */ |
| 196 |
inline void log_buffer_flush_to_disk(bool sync = true) { |
| 197 |
log_buffer_flush_to_disk(*log_sys, sync); |
| 198 |
} |
| 199 |
|
| 200 |
/** Writes the log buffer to the log file. It is intended to be called from |
| 201 |
background master thread periodically. If the log writer threads are active, |
| 202 |
this function writes nothing. */ |
| 203 |
void log_buffer_sync_in_background(); |
| 204 |
|
| 205 |
/** Get last redo block from redo buffer and end LSN. Note that it takes |
| 206 |
x-lock on the log buffer for a short period. Out values are always set, |
| 207 |
even when provided last_block is nullptr. |
| 208 |
@param[in,out] log redo log |
| 209 |
@param[out] last_lsn end lsn of last mtr |
| 210 |
@param[out] last_block last redo block |
| 211 |
@param[in,out] block_len length in bytes */ |
| 212 |
void log_buffer_get_last_block(log_t &log, lsn_t &last_lsn, byte *last_block, |
| 213 |
uint32_t &block_len); |
| 214 |
|
| 215 |
/** Changes size of the log buffer. This is a thread-safe version. |
| 216 |
It is used by SET GLOBAL innodb_log_buffer_size = X. |
| 217 |
@param[in,out] log redo log |
| 218 |
@param[in] new_size requested new size |
| 219 |
@return true iff succeeded in resize */ |
| 220 |
bool log_buffer_resize(log_t &log, size_t new_size); |
| 221 |
|
| 222 |
/** Changes size of the log buffer. This is a non-thread-safe version |
| 223 |
which might be invoked only when there are no concurrent possible writes |
| 224 |
to the log buffer. It is used in log_buffer_reserve() when a requested |
| 225 |
size to reserve is larger than size of the log buffer. |
| 226 |
@param[in,out] log redo log |
| 227 |
@param[in] new_size requested new size |
| 228 |
@param[in] end_lsn maximum lsn written to log buffer |
| 229 |
@return true iff succeeded in resize */ |
| 230 |
bool log_buffer_resize_low(log_t &log, size_t new_size, lsn_t end_lsn); |
| 231 |
|
| 232 |
/** @} */ |
| 233 |
|
| 234 |
/**************************************************/ /** |
| 235 |
|
| 236 |
@name Log - the recent written, the recent closed buffers. |
| 237 |
|
| 238 |
*******************************************************/ |
| 239 |
|
| 240 |
/** @{ */ |
| 241 |
|
| 242 |
#define log_closer_mutex_enter(log) mutex_enter(&((log).closer_mutex)) |
| 243 |
|
| 244 |
#define log_closer_mutex_enter_nowait(log) \ |
| 245 |
mutex_enter_nowait(&((log).closer_mutex)) |
| 246 |
|
| 247 |
#define log_closer_mutex_exit(log) mutex_exit(&((log).closer_mutex)) |
| 248 |
|
| 249 |
/** @return lsn up to which all writes to log buffer have been finished */ |
| 250 |
inline lsn_t log_buffer_ready_for_write_lsn(const log_t &log) { |
| 251 |
return log.recent_written.tail(); |
| 252 |
} |
| 253 |
|
| 254 |
/** @return lsn up to which all dirty pages have been added to flush list */ |
| 255 |
inline lsn_t log_buffer_dirty_pages_added_up_to_lsn(const log_t &log) { |
| 256 |
return log.recent_closed.tail(); |
| 257 |
} |
| 258 |
|
| 259 |
/** @return capacity of the recent_closed, or 0 if !log_use_threads() */ |
| 260 |
inline lsn_t log_buffer_flush_order_lag(const log_t &log) { |
| 261 |
return log.recent_closed.capacity(); |
| 262 |
} |
| 263 |
|
| 264 |
/** Advances log.buf_ready_for_write_lsn using links in the recent written |
| 265 |
buffer. It's used by the log writer thread only. |
| 266 |
@param[in] log redo log */ |
| 267 |
void log_advance_ready_for_write_lsn(log_t &log); |
| 268 |
|
| 269 |
/** Validates that all slots in log recent written buffer for lsn values |
| 270 |
in range between begin and end, are empty. Used during tests, crashes the |
| 271 |
program if validation does not pass. |
| 272 |
@param[in] log redo log which buffer is validated |
| 273 |
@param[in] begin validation start (inclusive) |
| 274 |
@param[in] end validation end (exclusive) */ |
| 275 |
void log_recent_written_empty_validate(const log_t &log, lsn_t begin, |
| 276 |
lsn_t end); |
| 277 |
|
| 278 |
/** Validates that all slots in log recent closed buffer for lsn values |
| 279 |
in range between begin and end, are empty. Used during tests, crashes the |
| 280 |
program if validation does not pass. |
| 281 |
@param[in] log redo log which buffer is validated |
| 282 |
@param[in] begin validation start (inclusive) |
| 283 |
@param[in] end validation end (exclusive) */ |
| 284 |
void log_recent_closed_empty_validate(const log_t &log, lsn_t begin, lsn_t end); |
| 285 |
|
| 286 |
/** Waits until there is free space in the log recent closed buffer |
| 287 |
for any links start_lsn -> end_lsn, which start at provided start_lsn. |
| 288 |
It does not add any link. |
| 289 |
|
| 290 |
This is called just before dirty pages for [start_lsn, end_lsn) |
| 291 |
are added to flush lists. That's because we need to guarantee, |
| 292 |
that the delay until dirty page is added to flush list is limited. |
| 293 |
For detailed explanation - @see log0write.cc. |
| 294 |
|
| 295 |
@param[in,out] log redo log |
| 296 |
@param[in] lsn lsn on which we wait (for any link: lsn -> x) */ |
| 297 |
void log_wait_for_space_in_log_recent_closed(log_t &log, lsn_t lsn); |
| 298 |
|
| 299 |
/** @} */ |
| 300 |
|
| 301 |
#endif /* !UNIV_HOTBACKUP */ |
| 302 |
|
| 303 |
#endif /* !log0buf_h */ |