| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 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/trx0rseg.ic |
| 29 |
Rollback segment |
| 30 |
|
| 31 |
Created 3/26/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "mtr0log.h" |
| 35 |
#include "srv0srv.h" |
| 36 |
#include "trx0purge.h" |
| 37 |
#include "trx0sys.h" |
| 38 |
|
| 39 |
/** Gets a rollback segment header. |
| 40 |
@param[in] space Space where placed |
| 41 |
@param[in] page_no Page number of the header |
| 42 |
@param[in] page_size Page size |
| 43 |
@param[in,out] mtr Mini-transaction |
| 44 |
@return rollback segment header, page x-latched */ |
| 45 |
static inline trx_rsegf_t *trx_rsegf_get(space_id_t space, page_no_t page_no, |
| 46 |
const page_size_t &page_size, |
| 47 |
mtr_t *mtr) { |
| 48 |
buf_block_t *block; |
| 49 |
trx_rsegf_t *header; |
| 50 |
|
| 51 |
block = buf_page_get(page_id_t(space, page_no), page_size, RW_X_LATCH, |
| 52 |
UT_LOCATION_HERE, mtr); |
| 53 |
|
| 54 |
buf_block_dbg_add_level(block, SYNC_RSEG_HEADER); |
| 55 |
|
| 56 |
header = TRX_RSEG + buf_block_get_frame(block); |
| 57 |
|
| 58 |
return (header); |
| 59 |
} |
| 60 |
|
| 61 |
/** Gets a newly created rollback segment header. |
| 62 |
@param[in] space Space where placed |
| 63 |
@param[in] page_no Page number of the header |
| 64 |
@param[in] page_size Page size |
| 65 |
@param[in,out] mtr Mini-transaction |
| 66 |
@return rollback segment header, page x-latched */ |
| 67 |
static inline trx_rsegf_t *trx_rsegf_get_new(space_id_t space, |
| 68 |
page_no_t page_no, |
| 69 |
const page_size_t &page_size, |
| 70 |
mtr_t *mtr) { |
| 71 |
buf_block_t *block; |
| 72 |
trx_rsegf_t *header; |
| 73 |
|
| 74 |
block = buf_page_get(page_id_t(space, page_no), page_size, RW_X_LATCH, |
| 75 |
UT_LOCATION_HERE, mtr); |
| 76 |
|
| 77 |
buf_block_dbg_add_level(block, SYNC_RSEG_HEADER_NEW); |
| 78 |
|
| 79 |
header = TRX_RSEG + buf_block_get_frame(block); |
| 80 |
|
| 81 |
return (header); |
| 82 |
} |
| 83 |
|
| 84 |
/** Gets the file page number of the nth undo log slot. |
| 85 |
@return page number of the undo log segment */ |
| 86 |
static inline page_no_t trx_rsegf_get_nth_undo( |
| 87 |
trx_rsegf_t *rsegf, /*!< in: rollback segment header */ |
| 88 |
ulint n, /*!< in: index of slot */ |
| 89 |
mtr_t *mtr) /*!< in: mtr */ |
| 90 |
{ |
| 91 |
ut_a(n < TRX_RSEG_N_SLOTS); |
| 92 |
|
| 93 |
return (mtr_read_ulint(rsegf + TRX_RSEG_UNDO_SLOTS + n * TRX_RSEG_SLOT_SIZE, |
| 94 |
MLOG_4BYTES, mtr)); |
| 95 |
} |
| 96 |
|
| 97 |
/** Sets the file page number of the nth undo log slot. */ |
| 98 |
static inline void trx_rsegf_set_nth_undo( |
| 99 |
trx_rsegf_t *rsegf, /*!< in: rollback segment header */ |
| 100 |
ulint n, /*!< in: index of slot */ |
| 101 |
page_no_t page_no, /*!< in: page number of the undo log segment */ |
| 102 |
mtr_t *mtr) /*!< in: mtr */ |
| 103 |
{ |
| 104 |
ut_a(n < TRX_RSEG_N_SLOTS); |
| 105 |
|
| 106 |
mlog_write_ulint(rsegf + TRX_RSEG_UNDO_SLOTS + n * TRX_RSEG_SLOT_SIZE, |
| 107 |
page_no, MLOG_4BYTES, mtr); |
| 108 |
} |
| 109 |
|
| 110 |
/** Looks for a free slot for an undo log segment. |
| 111 |
@return slot index or ULINT_UNDEFINED if not found */ |
| 112 |
static inline ulint trx_rsegf_undo_find_free( |
| 113 |
trx_rsegf_t *rsegf, /*!< in: rollback segment header */ |
| 114 |
mtr_t *mtr) /*!< in: mtr */ |
| 115 |
{ |
| 116 |
ulint i; |
| 117 |
page_no_t page_no; |
| 118 |
ulint max_slots = TRX_RSEG_N_SLOTS; |
| 119 |
|
| 120 |
#ifdef UNIV_DEBUG |
| 121 |
if (trx_rseg_n_slots_debug) { |
| 122 |
max_slots = std::min(static_cast<ulint>(trx_rseg_n_slots_debug), |
| 123 |
static_cast<ulint>(TRX_RSEG_N_SLOTS)); |
| 124 |
} |
| 125 |
#endif |
| 126 |
|
| 127 |
for (i = 0; i < max_slots; i++) { |
| 128 |
page_no = trx_rsegf_get_nth_undo(rsegf, i, mtr); |
| 129 |
|
| 130 |
if (page_no == FIL_NULL) { |
| 131 |
return (i); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
return (ULINT_UNDEFINED); |
| 136 |
} |
| 137 |
|
| 138 |
/** Convert a 7-bit ID stored in a rollback pointer to an undo space ID. |
| 139 |
Before 8.0, this ID is called an rseg_id and is a slot in the TRX_SYS page |
| 140 |
where the space_id can be read. |
| 141 |
After 8.0 this is an undo space number which can be converted directly to |
| 142 |
an undo space ID. |
| 143 |
@param[in] id a 7-bit ID from a rollback pointer |
| 144 |
@param[in] is_temp true if rseg from Temp Tablespace else false. |
| 145 |
@return undo tablespace ID containing the rollback segment */ |
| 146 |
inline space_id_t trx_rseg_id_to_space_id(ulint id, bool is_temp) { |
| 147 |
/* The rseg_id must be an undo_space_num between 0 and 127. */ |
| 148 |
ut_ad(id < TRX_SYS_N_RSEGS); |
| 149 |
|
| 150 |
/* MySQL versions before v5.7.2 used the first 32 slots in the |
| 151 |
TRX_SYS page for redo rollback segments. v5.7.2 uses these seg_ids |
| 152 |
for no-redo rollback segments in the temporary tablespace. |
| 153 |
v8 will once again use them for durable/redo rollback segments. */ |
| 154 |
if (is_temp) { |
| 155 |
return (srv_tmp_space.space_id()); |
| 156 |
} |
| 157 |
|
| 158 |
/* If the ID is 0 then use the system tablespace. */ |
| 159 |
if (id == 0) { |
| 160 |
return (TRX_SYS_SPACE); |
| 161 |
} |
| 162 |
|
| 163 |
if (srv_upgrade_old_undo_found) { |
| 164 |
/* Interpret the id as a slot in the TRX_SYS page. */ |
| 165 |
trx_rseg_t *rseg = trx_sys->rsegs.find(id); |
| 166 |
return (rseg->space_id); |
| 167 |
} |
| 168 |
|
| 169 |
/* We assume at this point that all undo tablespaces have |
| 170 |
reserved space_ids and RSEG_ARRAY pages. */ |
| 171 |
ut_ad(!undo::spaces->empty()); |
| 172 |
|
| 173 |
/* The space_id_bank for this slot cannot change since this call is done |
| 174 |
by purge or MVCC. This space_id can only change during truncate when |
| 175 |
there are no more undo logs in this undo tablespace. */ |
| 176 |
space_id_t space_id = undo::num2id(static_cast<space_id_t>(id)); |
| 177 |
ut_ad(space_id != SPACE_UNKNOWN); |
| 178 |
|
| 179 |
return (space_id); |
| 180 |
} |
| 181 |
|
| 182 |
/** Get a pointer to the rollback segment directory header and x-latches its |
| 183 |
page. |
| 184 |
@param[in] space_id Undo Tablespace ID |
| 185 |
@param[in] mtr mtr |
| 186 |
@return pointer to rollback segment directory header with page x-latched. */ |
| 187 |
inline trx_rsegsf_t *trx_rsegsf_get(space_id_t space_id, mtr_t *mtr) { |
| 188 |
buf_block_t *block; |
| 189 |
trx_rsegsf_t *rsegs_header; |
| 190 |
|
| 191 |
ut_ad(mtr != nullptr); |
| 192 |
ut_ad(undo::is_reserved(space_id)); |
| 193 |
|
| 194 |
block = buf_page_get(page_id_t(space_id, FSP_RSEG_ARRAY_PAGE_NO), |
| 195 |
univ_page_size, RW_X_LATCH, UT_LOCATION_HERE, mtr); |
| 196 |
buf_block_dbg_add_level(block, SYNC_RSEG_ARRAY_HEADER); |
| 197 |
|
| 198 |
rsegs_header = RSEG_ARRAY_HEADER + buf_block_get_frame(block); |
| 199 |
|
| 200 |
return (rsegs_header); |
| 201 |
} |
| 202 |
|
| 203 |
/** Get the page number of the nth rollback segment slot in the |
| 204 |
RSEG_ARRAY page for this undo tablespace. |
| 205 |
@param[in] rsegs_header rollback segment array page header |
| 206 |
@param[in] slot slot index == rseg id |
| 207 |
@param[in] mtr mtr |
| 208 |
@return page number, FIL_NULL if slot unused */ |
| 209 |
inline page_no_t trx_rsegsf_get_page_no(trx_rsegsf_t *rsegs_header, ulint slot, |
| 210 |
mtr_t *mtr) { |
| 211 |
ut_ad(rsegs_header != nullptr); |
| 212 |
ut_ad(mtr != nullptr); |
| 213 |
ut_ad(slot < FSP_MAX_ROLLBACK_SEGMENTS); |
| 214 |
|
| 215 |
return (mtr_read_ulint( |
| 216 |
rsegs_header + RSEG_ARRAY_PAGES_OFFSET + slot * RSEG_ARRAY_SLOT_SIZE, |
| 217 |
MLOG_4BYTES, mtr)); |
| 218 |
} |
| 219 |
|
| 220 |
/** Sets the page number of the nth rollback segment slot in the |
| 221 |
independent undo tablespace. |
| 222 |
@param[in] rsegs_header rollback segment array page header |
| 223 |
@param[in] slot slot number on page == rseg id |
| 224 |
@param[in] page_no rollback regment header page number |
| 225 |
@param[in] mtr mtr */ |
| 226 |
static inline void trx_rsegsf_set_page_no(trx_rsegsf_t *rsegs_header, |
| 227 |
ulint slot, page_no_t page_no, |
| 228 |
mtr_t *mtr) { |
| 229 |
ut_ad(rsegs_header); |
| 230 |
|
| 231 |
mlog_write_ulint( |
| 232 |
rsegs_header + RSEG_ARRAY_PAGES_OFFSET + slot * RSEG_ARRAY_SLOT_SIZE, |
| 233 |
page_no, MLOG_4BYTES, mtr); |
| 234 |
} |