| 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.ic |
| 29 |
Mini-transaction logging routines |
| 30 |
|
| 31 |
Created 12/7/1995 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "buf0dblwr.h" |
| 35 |
#include "fsp0types.h" |
| 36 |
#include "mach0data.h" |
| 37 |
#include "trx0types.h" |
| 38 |
|
| 39 |
#include "mysqld_error.h" |
| 40 |
|
| 41 |
static inline bool mlog_open(mtr_t *mtr, ulint size, byte *&buffer) { |
| 42 |
mtr->set_modified(); |
| 43 |
return (mlog_open_metadata(mtr, size, buffer)); |
| 44 |
} |
| 45 |
|
| 46 |
static inline bool mlog_open_metadata(mtr_t *mtr, ulint size, byte *&buffer) { |
| 47 |
if (mtr_get_log_mode(mtr) == MTR_LOG_NONE || |
| 48 |
mtr_get_log_mode(mtr) == MTR_LOG_NO_REDO) { |
| 49 |
buffer = nullptr; |
| 50 |
return (false); |
| 51 |
} |
| 52 |
|
| 53 |
buffer = mtr->get_log()->open(size); |
| 54 |
return (buffer != nullptr); |
| 55 |
} |
| 56 |
|
| 57 |
/** Closes a buffer opened to mlog. */ |
| 58 |
static inline void mlog_close( |
| 59 |
mtr_t *mtr, /*!< in: mtr */ |
| 60 |
byte *ptr) /*!< in: buffer space from ptr up was not used */ |
| 61 |
{ |
| 62 |
ut_ad(mtr_get_log_mode(mtr) != MTR_LOG_NONE); |
| 63 |
ut_ad(mtr_get_log_mode(mtr) != MTR_LOG_NO_REDO); |
| 64 |
|
| 65 |
mtr->get_log()->close(ptr); |
| 66 |
} |
| 67 |
|
| 68 |
/** Concatenates 1 - 4 bytes to the mtr log. The value is not compressed. */ |
| 69 |
static inline void mlog_catenate_ulint( |
| 70 |
mtr_buf_t *mtr_buf, /*!< in/out: buffer to write */ |
| 71 |
ulint val, /*!< in: value to write */ |
| 72 |
mlog_id_t type) /*!< in: type of value to write */ |
| 73 |
{ |
| 74 |
static_assert(MLOG_1BYTE == 1); |
| 75 |
static_assert(MLOG_2BYTES == 2); |
| 76 |
static_assert(MLOG_4BYTES == 4); |
| 77 |
static_assert(MLOG_8BYTES == 8); |
| 78 |
|
| 79 |
byte *ptr = mtr_buf->push<byte *>(type); |
| 80 |
|
| 81 |
switch (type) { |
| 82 |
case MLOG_4BYTES: |
| 83 |
mach_write_to_4(ptr, val); |
| 84 |
break; |
| 85 |
case MLOG_2BYTES: |
| 86 |
mach_write_to_2(ptr, val); |
| 87 |
break; |
| 88 |
case MLOG_1BYTE: |
| 89 |
mach_write_to_1(ptr, val); |
| 90 |
break; |
| 91 |
default: |
| 92 |
ut_error; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** Concatenates 1 - 4 bytes to the mtr log. The value is not compressed. */ |
| 97 |
static inline void mlog_catenate_ulint( |
| 98 |
mtr_t *mtr, /*!< in/out: mtr */ |
| 99 |
ulint val, /*!< in: value to write */ |
| 100 |
mlog_id_t type) /*!< in: MLOG_1BYTE, MLOG_2BYTES, MLOG_4BYTES */ |
| 101 |
{ |
| 102 |
if (mtr_get_log_mode(mtr) == MTR_LOG_NONE || |
| 103 |
mtr_get_log_mode(mtr) == MTR_LOG_NO_REDO) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
mlog_catenate_ulint(mtr->get_log(), val, type); |
| 108 |
} |
| 109 |
|
| 110 |
/** Concatenates a compressed ulint to mlog. */ |
| 111 |
static inline void mlog_catenate_ulint_compressed( |
| 112 |
mtr_t *mtr, /*!< in: mtr */ |
| 113 |
ulint val) /*!< in: value to write */ |
| 114 |
{ |
| 115 |
byte *log_ptr = nullptr; |
| 116 |
|
| 117 |
/* If no logging is requested, we may return now */ |
| 118 |
if (!mlog_open(mtr, 10, log_ptr)) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
log_ptr += mach_write_compressed(log_ptr, val); |
| 123 |
|
| 124 |
mlog_close(mtr, log_ptr); |
| 125 |
} |
| 126 |
|
| 127 |
/** Catenates a compressed 64-bit integer to mlog. */ |
| 128 |
static inline void mlog_catenate_ull_compressed( |
| 129 |
mtr_t *mtr, /*!< in: mtr */ |
| 130 |
uint64_t val) /*!< in: value to write */ |
| 131 |
{ |
| 132 |
byte *log_ptr = nullptr; |
| 133 |
|
| 134 |
/* If no logging is requested, we may return now */ |
| 135 |
if (!mlog_open(mtr, 15, log_ptr)) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
log_ptr += mach_u64_write_compressed(log_ptr, val); |
| 140 |
|
| 141 |
mlog_close(mtr, log_ptr); |
| 142 |
} |
| 143 |
|
| 144 |
static inline byte *mlog_write_initial_dict_log_record(mlog_id_t type, |
| 145 |
table_id_t id, |
| 146 |
uint64_t version, |
| 147 |
byte *log_ptr, |
| 148 |
mtr_t *mtr) { |
| 149 |
ut_ad(type <= MLOG_BIGGEST_TYPE); |
| 150 |
ut_ad(type == MLOG_TABLE_DYNAMIC_META); |
| 151 |
|
| 152 |
mach_write_to_1(log_ptr, type); |
| 153 |
log_ptr++; |
| 154 |
|
| 155 |
log_ptr += mach_u64_write_much_compressed(log_ptr, id); |
| 156 |
log_ptr += mach_u64_write_much_compressed(log_ptr, version); |
| 157 |
|
| 158 |
mtr->added_rec(); |
| 159 |
return (log_ptr); |
| 160 |
} |
| 161 |
|
| 162 |
/** Writes a log record about an operation. |
| 163 |
@param[in] type Redo log record type |
| 164 |
@param[in] space_id Tablespace identifier |
| 165 |
@param[in] page_no Page number |
| 166 |
@param[in,out] log_ptr Current end of mini-transaction log |
| 167 |
@param[in,out] mtr Mini-transaction |
| 168 |
@return end of mini-transaction log */ |
| 169 |
static inline byte *mlog_write_initial_log_record_low(mlog_id_t type, |
| 170 |
space_id_t space_id, |
| 171 |
page_no_t page_no, |
| 172 |
byte *log_ptr, |
| 173 |
mtr_t *mtr) { |
| 174 |
ut_ad(type <= MLOG_BIGGEST_TYPE); |
| 175 |
|
| 176 |
mach_write_to_1(log_ptr, type); |
| 177 |
log_ptr++; |
| 178 |
|
| 179 |
log_ptr += mach_write_compressed(log_ptr, space_id); |
| 180 |
log_ptr += mach_write_compressed(log_ptr, page_no); |
| 181 |
|
| 182 |
mtr->added_rec(); |
| 183 |
return (log_ptr); |
| 184 |
} |
| 185 |
|
| 186 |
#ifndef UNIV_HOTBACKUP |
| 187 |
/** Writes the initial part of a log record (3..11 bytes). |
| 188 |
If the implementation of this function is changed, all |
| 189 |
size parameters to mlog_open() should be adjusted accordingly! |
| 190 |
@return new value of log_ptr */ |
| 191 |
static inline byte *mlog_write_initial_log_record_fast( |
| 192 |
const byte *ptr, /*!< in: pointer to (inside) a buffer |
| 193 |
frame holding the file page where |
| 194 |
modification is made */ |
| 195 |
mlog_id_t type, /*!< in: log item type: MLOG_1BYTE, ... */ |
| 196 |
byte *log_ptr, /*!< in: pointer to mtr log which has |
| 197 |
been opened */ |
| 198 |
mtr_t *mtr) /*!< in/out: mtr */ |
| 199 |
{ |
| 200 |
const byte *page; |
| 201 |
space_id_t space; |
| 202 |
page_no_t offset; |
| 203 |
|
| 204 |
ut_ad(log_ptr); |
| 205 |
ut_d(mtr->memo_modify_page(ptr)); |
| 206 |
|
| 207 |
page = (const byte *)ut_align_down(ptr, UNIV_PAGE_SIZE); |
| 208 |
space = mach_read_from_4(page + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID); |
| 209 |
offset = mach_read_from_4(page + FIL_PAGE_OFFSET); |
| 210 |
|
| 211 |
/* check whether the page is in the doublewrite buffer; |
| 212 |
the doublewrite buffer is located in pages |
| 213 |
FSP_EXTENT_SIZE, ..., 3 * FSP_EXTENT_SIZE - 1 in the |
| 214 |
system tablespace */ |
| 215 |
|
| 216 |
/* We don't redo log the double write buffer */ |
| 217 |
if (space == TRX_SYS_SPACE && offset >= FSP_EXTENT_SIZE && |
| 218 |
offset < 3 * FSP_EXTENT_SIZE) { |
| 219 |
return (log_ptr); |
| 220 |
} |
| 221 |
|
| 222 |
return (mlog_write_initial_log_record_low(type, space, offset, log_ptr, mtr)); |
| 223 |
} |
| 224 |
#endif /* !UNIV_HOTBACKUP */ |