| 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 |
/** @file include/mtr0log.h |
| 29 |
Mini-transaction logging routines |
| 30 |
|
| 31 |
Created 12/7/1995 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef mtr0log_h |
| 35 |
#define mtr0log_h |
| 36 |
|
| 37 |
#include "dyn0buf.h" |
| 38 |
#include "mtr0mtr.h" |
| 39 |
#include "univ.i" |
| 40 |
|
| 41 |
// Forward declaration |
| 42 |
struct dict_index_t; |
| 43 |
|
| 44 |
/* Index logging version */ |
| 45 |
constexpr uint8_t INDEX_LOG_VERSION_0 = 0; |
| 46 |
constexpr uint8_t INDEX_LOG_VERSION_CURRENT = 1; |
| 47 |
constexpr uint8_t INDEX_LOG_VERSION_MAX = INDEX_LOG_VERSION_CURRENT; |
| 48 |
|
| 49 |
#define COMPACT_FLAG 0x01 |
| 50 |
#define VERSION_FLAG 0x02 |
| 51 |
#define INSTANT_FLAG 0x04 |
| 52 |
|
| 53 |
#define IS_INSTANT(flags) (flags & INSTANT_FLAG) |
| 54 |
#define IS_VERSIONED(flags) (flags & VERSION_FLAG) |
| 55 |
#define IS_COMPACT(flags) (flags & COMPACT_FLAG) |
| 56 |
|
| 57 |
#define SET_INSTANT(flags) (flag |= INSTANT_FLAG) |
| 58 |
#define SET_VERSIONED(flags) (flag |= VERSION_FLAG) |
| 59 |
#define SET_COMPACT(flags) (flag |= COMPACT_FLAG) |
| 60 |
|
| 61 |
/* Size of initial info on REDO log |
| 62 |
1 byte for LOG TYPE |
| 63 |
3-5 bytes for SPACE ID |
| 64 |
3-5 bytes for PAGE OFFSET */ |
| 65 |
constexpr size_t REDO_LOG_INITIAL_INFO_SIZE = 11; |
| 66 |
|
| 67 |
/** Writes 1, 2 or 4 bytes to a file page. Writes the corresponding log |
| 68 |
record to the mini-transaction log if mtr is not NULL. */ |
| 69 |
void mlog_write_ulint( |
| 70 |
byte *ptr, /*!< in: pointer where to write */ |
| 71 |
ulint val, /*!< in: value to write */ |
| 72 |
mlog_id_t type, /*!< in: MLOG_1BYTE, MLOG_2BYTES, MLOG_4BYTES */ |
| 73 |
mtr_t *mtr); /*!< in: mini-transaction handle */ |
| 74 |
|
| 75 |
/** Writes 8 bytes to a file page. Writes the corresponding log |
| 76 |
record to the mini-transaction log, only if mtr is not NULL */ |
| 77 |
void mlog_write_ull(byte *ptr, /*!< in: pointer where to write */ |
| 78 |
uint64_t val, /*!< in: value to write */ |
| 79 |
mtr_t *mtr); /*!< in: mini-transaction handle */ |
| 80 |
/** Writes a string to a file page buffered in the buffer pool. Writes the |
| 81 |
corresponding log record to the mini-transaction log. */ |
| 82 |
void mlog_write_string(byte *ptr, /*!< in: pointer where to write */ |
| 83 |
const byte *str, /*!< in: string to write */ |
| 84 |
ulint len, /*!< in: string length */ |
| 85 |
mtr_t *mtr); /*!< in: mini-transaction handle */ |
| 86 |
/** Logs a write of a string to a file page buffered in the buffer pool. |
| 87 |
Writes the corresponding log record to the mini-transaction log. */ |
| 88 |
void mlog_log_string(byte *ptr, /*!< in: pointer written to */ |
| 89 |
ulint len, /*!< in: string length */ |
| 90 |
mtr_t *mtr); /*!< in: mini-transaction handle */ |
| 91 |
/** Writes initial part of a log record consisting of one-byte item |
| 92 |
type and four-byte space and page numbers. */ |
| 93 |
void mlog_write_initial_log_record( |
| 94 |
const byte *ptr, /*!< in: pointer to (inside) a buffer |
| 95 |
frame holding the file page where |
| 96 |
modification is made */ |
| 97 |
mlog_id_t type, /*!< in: log item type: MLOG_1BYTE, ... */ |
| 98 |
mtr_t *mtr); /*!< in: mini-transaction handle */ |
| 99 |
|
| 100 |
/** Catenates 1 - 4 bytes to the mtr log. The value is not compressed. |
| 101 |
@param[in,out] dyn_buf buffer to write |
| 102 |
@param[in] val value to write |
| 103 |
@param[in] type type of value to write */ |
| 104 |
static inline void mlog_catenate_ulint(mtr_buf_t *dyn_buf, ulint val, |
| 105 |
mlog_id_t type); |
| 106 |
|
| 107 |
/** Catenates 1 - 4 bytes to the mtr log. |
| 108 |
@param[in] mtr mtr |
| 109 |
@param[in] val value to write |
| 110 |
@param[in] type MLOG_1BYTE, MLOG_2BYTES, MLOG_4BYTES */ |
| 111 |
static inline void mlog_catenate_ulint(mtr_t *mtr, ulint val, mlog_id_t type); |
| 112 |
|
| 113 |
/** Catenates n bytes to the mtr log. |
| 114 |
@param[in] mtr Mini-transaction |
| 115 |
@param[in] str String to write |
| 116 |
@param[in] len String length */ |
| 117 |
void mlog_catenate_string(mtr_t *mtr, const byte *str, ulint len); |
| 118 |
|
| 119 |
/** Catenates a compressed ulint to mlog. |
| 120 |
@param[in] mtr mtr |
| 121 |
@param[in] val value to write */ |
| 122 |
static inline void mlog_catenate_ulint_compressed(mtr_t *mtr, ulint val); |
| 123 |
|
| 124 |
/** Catenates a compressed 64-bit integer to mlog. |
| 125 |
@param[in] mtr mtr |
| 126 |
@param[in] val value to write */ |
| 127 |
static inline void mlog_catenate_ull_compressed(mtr_t *mtr, uint64_t val); |
| 128 |
|
| 129 |
/** Opens a buffer to mlog. It must be closed with mlog_close. |
| 130 |
@param[in,out] mtr mtr |
| 131 |
@param[in] size buffer size in bytes; MUST be smaller than |
| 132 |
DYN_ARRAY_DATA_SIZE! |
| 133 |
@param[out] buffer mlog buffer pointer if opened successfully |
| 134 |
@retval true if opened successfully. |
| 135 |
@retval false if not opened. One case is when redo is disabled for mtr. */ |
| 136 |
[[nodiscard]] static inline bool mlog_open(mtr_t *mtr, ulint size, |
| 137 |
byte *&buffer); |
| 138 |
|
| 139 |
/** Opens a buffer to mlog. It must be closed with mlog_close. |
| 140 |
This is used for writing log for metadata changes |
| 141 |
@param[in,out] mtr mtr |
| 142 |
@param[in] size buffer size in bytes; MUST be smaller than |
| 143 |
DYN_ARRAY_DATA_SIZE! |
| 144 |
@param[out] buffer mlog buffer pointer if opened successfully |
| 145 |
@retval true if opened successfully. |
| 146 |
@retval false if not opened. One case is when redo is disabled for mtr. */ |
| 147 |
[[nodiscard]] static inline bool mlog_open_metadata(mtr_t *mtr, ulint size, |
| 148 |
byte *&buffer); |
| 149 |
|
| 150 |
/** Closes a buffer opened to mlog. |
| 151 |
@param[in] mtr mtr |
| 152 |
@param[in] ptr buffer space from ptr up was not used */ |
| 153 |
static inline void mlog_close(mtr_t *mtr, byte *ptr); |
| 154 |
|
| 155 |
/** Writes a log record about a dictionary operation, which would cost |
| 156 |
at most 23 bytes. |
| 157 |
@param[in] type Redo log record type |
| 158 |
@param[in] id Table id |
| 159 |
@param[in] version Table dynamic metadata version |
| 160 |
@param[in,out] log_ptr Current end of mini-transaction log |
| 161 |
@param[in,out] mtr Mini-transaction |
| 162 |
@return end of mini-transaction log */ |
| 163 |
static inline byte *mlog_write_initial_dict_log_record( |
| 164 |
mlog_id_t type, table_id_t id, uint64_t version, byte *log_ptr, mtr_t *mtr); |
| 165 |
|
| 166 |
/** Writes a log record about an operation. |
| 167 |
@param[in] type Redo log record type |
| 168 |
@param[in] space_id Tablespace identifier |
| 169 |
@param[in] page_no Page number |
| 170 |
@param[in,out] log_ptr Current end of mini-transaction log |
| 171 |
@param[in,out] mtr Mini-transaction |
| 172 |
@return end of mini-transaction log */ |
| 173 |
static inline byte *mlog_write_initial_log_record_low(mlog_id_t type, |
| 174 |
space_id_t space_id, |
| 175 |
page_no_t page_no, |
| 176 |
byte *log_ptr, |
| 177 |
mtr_t *mtr); |
| 178 |
|
| 179 |
#ifndef UNIV_HOTBACKUP |
| 180 |
/** Writes the initial part of a log record (3..11 bytes). |
| 181 |
If the implementation of this function is changed, all size parameters to |
| 182 |
mlog_open() should be adjusted accordingly! |
| 183 |
@param[in] ptr pointer to (inside) a buffer frame holding the file |
| 184 |
page where modification is made |
| 185 |
@param[in] type log item type: MLOG_1BYTE, ... |
| 186 |
@param[in] log_ptr pointer to mtr log which has been opened |
| 187 |
@param[in] mtr mtr |
| 188 |
@return new value of log_ptr */ |
| 189 |
static inline byte *mlog_write_initial_log_record_fast(const byte *ptr, |
| 190 |
mlog_id_t type, |
| 191 |
byte *log_ptr, |
| 192 |
mtr_t *mtr); |
| 193 |
#else /* !UNIV_HOTBACKUP */ |
| 194 |
#define mlog_write_initial_log_record(ptr, type, mtr) ((void)0) |
| 195 |
#define mlog_write_initial_log_record_fast(ptr, type, log_ptr, mtr) ((byte *)0) |
| 196 |
#endif /* !UNIV_HOTBACKUP */ |
| 197 |
|
| 198 |
/** Parses an initial log record written by mlog_write_initial_dict_log_record. |
| 199 |
@param[in] ptr buffer |
| 200 |
@param[in] end_ptr buffer end |
| 201 |
@param[out] type log record type, should be |
| 202 |
MLOG_TABLE_DYNAMIC_META |
| 203 |
@param[out] id table id |
| 204 |
@param[out] version table dynamic metadata version |
| 205 |
@return parsed record end, NULL if not a complete record */ |
| 206 |
byte *mlog_parse_initial_dict_log_record(const byte *ptr, const byte *end_ptr, |
| 207 |
mlog_id_t *type, table_id_t *id, |
| 208 |
uint64_t *version); |
| 209 |
|
| 210 |
/** Parses an initial log record written by mlog_write_initial_log_record. |
| 211 |
@return parsed record end, NULL if not a complete record */ |
| 212 |
byte *mlog_parse_initial_log_record( |
| 213 |
const byte *ptr, /*!< in: buffer */ |
| 214 |
const byte *end_ptr, /*!< in: buffer end */ |
| 215 |
mlog_id_t *type, /*!< out: log record type: MLOG_1BYTE, ... */ |
| 216 |
space_id_t *space, /*!< out: space id */ |
| 217 |
page_no_t *page_no); /*!< out: page number */ |
| 218 |
/** Parses a log record written by mlog_write_ulint or mlog_write_ull. |
| 219 |
@return parsed record end, NULL if not a complete record */ |
| 220 |
byte *mlog_parse_nbytes( |
| 221 |
mlog_id_t type, /*!< in: log record type: MLOG_1BYTE, ... */ |
| 222 |
const byte *ptr, /*!< in: buffer */ |
| 223 |
const byte *end_ptr, /*!< in: buffer end */ |
| 224 |
byte *page, /*!< in: page where to apply the log record, |
| 225 |
or NULL */ |
| 226 |
void *page_zip); /*!< in/out: compressed page, or NULL */ |
| 227 |
/** Parses a log record written by mlog_write_string. |
| 228 |
@return parsed record end, NULL if not a complete record */ |
| 229 |
byte *mlog_parse_string( |
| 230 |
byte *ptr, /*!< in: buffer */ |
| 231 |
byte *end_ptr, /*!< in: buffer end */ |
| 232 |
byte *page, /*!< in: page where to apply the log record, or NULL */ |
| 233 |
void *page_zip); /*!< in/out: compressed page, or NULL */ |
| 234 |
|
| 235 |
/** Opens a buffer for mlog, writes the initial log record and, if needed, the |
| 236 |
field lengths of an index. Reserves space for further log entries. The log |
| 237 |
entry must be closed with mtr_close(). |
| 238 |
@param[in,out] mtr Mini-transaction |
| 239 |
@param[in] rec Index record or page |
| 240 |
@param[in] index Record descriptor |
| 241 |
@param[in] type Log item type |
| 242 |
@param[in] size Requested buffer size in bytes. if 0, calls |
| 243 |
mlog_close() and returns false. |
| 244 |
@param[out] log_ptr Log buffer pointer |
| 245 |
@retval true if opened successfully. |
| 246 |
@retval false if not opened. One case is when redo is disabled for mtr. */ |
| 247 |
bool mlog_open_and_write_index(mtr_t *mtr, const byte *rec, |
| 248 |
const dict_index_t *index, mlog_id_t type, |
| 249 |
size_t size, byte *&log_ptr); |
| 250 |
|
| 251 |
/** Parses a log record written by mlog_open_and_write_index. |
| 252 |
@param[in] ptr buffer |
| 253 |
@param[in] end_ptr buffer end |
| 254 |
@param[out] index own: dummy index |
| 255 |
@return parsed record end, NULL if not a complete record */ |
| 256 |
byte *mlog_parse_index(byte *ptr, const byte *end_ptr, dict_index_t **index); |
| 257 |
|
| 258 |
/** Parses a log record written by mlog_open_and_write_index in version <= 8027. |
| 259 |
This function should never be changed and should be removed once recovery from |
| 260 |
mysql-8.0.27 is not needed anymore. |
| 261 |
@param[in] ptr buffer |
| 262 |
@param[in] end_ptr buffer end |
| 263 |
@param[in] comp true=compact row format |
| 264 |
@param[out] index own: dummy index |
| 265 |
@return parsed record end, NULL if not a complete record */ |
| 266 |
byte *mlog_parse_index_8027(byte *ptr, const byte *end_ptr, bool comp, |
| 267 |
dict_index_t **index); |
| 268 |
|
| 269 |
/** Insert, update, and maybe other functions may use this value to define an |
| 270 |
extra mlog buffer size for variable size data */ |
| 271 |
constexpr auto MLOG_BUF_MARGIN = 256; |
| 272 |
|
| 273 |
#include "mtr0log.ic" |
| 274 |
|
| 275 |
#endif /* mtr0log_h */ |