| 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/trx0sys.ic |
| 29 |
Transaction system |
| 30 |
|
| 31 |
Created 3/26/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "data0type.h" |
| 35 |
#include "srv0srv.h" |
| 36 |
#include "trx0trx.h" |
| 37 |
#ifndef UNIV_HOTBACKUP |
| 38 |
#include "mtr0log.h" |
| 39 |
|
| 40 |
#include "my_dbug.h" |
| 41 |
|
| 42 |
/* The typedef for rseg slot in the file copy */ |
| 43 |
typedef byte trx_sysf_rseg_t; |
| 44 |
|
| 45 |
/* Rollback segment specification slot offsets */ |
| 46 |
/*-------------------------------------------------------------*/ |
| 47 |
/** space where the segment header is placed; starting with MySQL/InnoDB 5.1.7, |
| 48 |
this is UNIV_UNDEFINED if the slot is unused */ |
| 49 |
constexpr uint32_t TRX_SYS_RSEG_SPACE = 0; |
| 50 |
/** page number where the segment header is placed; this is FIL_NULL if the slot |
| 51 |
is unused */ |
| 52 |
constexpr uint32_t TRX_SYS_RSEG_PAGE_NO = 4; |
| 53 |
/*-------------------------------------------------------------*/ |
| 54 |
/* Size of a rollback segment specification slot */ |
| 55 |
constexpr uint32_t TRX_SYS_RSEG_SLOT_SIZE = 8; |
| 56 |
|
| 57 |
/** Writes the value of max_trx_id to the file based trx system header. */ |
| 58 |
void trx_sys_write_max_trx_id(void); |
| 59 |
|
| 60 |
/** Checks if a page address is the trx sys header page. |
| 61 |
@param[in] page_id page id |
| 62 |
@return true if trx sys header page */ |
| 63 |
static inline bool trx_sys_hdr_page(const page_id_t &page_id) { |
| 64 |
return (page_id.space() == TRX_SYS_SPACE && |
| 65 |
page_id.page_no() == TRX_SYS_PAGE_NO); |
| 66 |
} |
| 67 |
|
| 68 |
/** Gets a pointer to the transaction system header and x-latches its page. |
| 69 |
@return pointer to system header, page x-latched. */ |
| 70 |
static inline trx_sysf_t *trx_sysf_get(mtr_t *mtr) /*!< in: mtr */ |
| 71 |
{ |
| 72 |
buf_block_t *block; |
| 73 |
trx_sysf_t *header; |
| 74 |
|
| 75 |
ut_ad(mtr); |
| 76 |
|
| 77 |
block = buf_page_get(page_id_t(TRX_SYS_SPACE, TRX_SYS_PAGE_NO), |
| 78 |
univ_page_size, RW_X_LATCH, UT_LOCATION_HERE, mtr); |
| 79 |
buf_block_dbg_add_level(block, SYNC_TRX_SYS_HEADER); |
| 80 |
|
| 81 |
header = TRX_SYS + buf_block_get_frame(block); |
| 82 |
|
| 83 |
return (header); |
| 84 |
} |
| 85 |
|
| 86 |
/** Gets the space of the nth rollback segment slot in the trx system |
| 87 |
file copy. |
| 88 |
@return space id */ |
| 89 |
static inline space_id_t trx_sysf_rseg_get_space( |
| 90 |
trx_sysf_t *sys_header, /*!< in: trx sys header */ |
| 91 |
ulint slot, /*!< in: slot index == rseg id */ |
| 92 |
mtr_t *mtr) /*!< in: mtr */ |
| 93 |
{ |
| 94 |
ut_ad(sys_header); |
| 95 |
ut_ad(slot < TRX_SYS_N_RSEGS); |
| 96 |
|
| 97 |
return (mtr_read_ulint(sys_header + TRX_SYS_RSEGS + |
| 98 |
slot * TRX_SYS_RSEG_SLOT_SIZE + TRX_SYS_RSEG_SPACE, |
| 99 |
MLOG_4BYTES, mtr)); |
| 100 |
} |
| 101 |
|
| 102 |
/** Gets the page number of the nth rollback segment slot in the trx system |
| 103 |
header. |
| 104 |
@return page number, FIL_NULL if slot unused */ |
| 105 |
static inline page_no_t trx_sysf_rseg_get_page_no( |
| 106 |
trx_sysf_t *sys_header, /*!< in: trx system header */ |
| 107 |
ulint slot, /*!< in: slot index == rseg id */ |
| 108 |
mtr_t *mtr) /*!< in: mtr */ |
| 109 |
{ |
| 110 |
ut_ad(sys_header); |
| 111 |
ut_ad(slot < TRX_SYS_N_RSEGS); |
| 112 |
|
| 113 |
return (mtr_read_ulint(sys_header + TRX_SYS_RSEGS + |
| 114 |
slot * TRX_SYS_RSEG_SLOT_SIZE + |
| 115 |
TRX_SYS_RSEG_PAGE_NO, |
| 116 |
MLOG_4BYTES, mtr)); |
| 117 |
} |
| 118 |
|
| 119 |
/** Sets the space id of the nth rollback segment slot in the trx system |
| 120 |
file copy. */ |
| 121 |
static inline void trx_sysf_rseg_set_space( |
| 122 |
trx_sysf_t *sys_header, /*!< in: trx sys file copy */ |
| 123 |
ulint slot, /*!< in: slot index == rseg id */ |
| 124 |
space_id_t space, /*!< in: space id */ |
| 125 |
mtr_t *mtr) /*!< in: mtr */ |
| 126 |
{ |
| 127 |
ut_ad(sys_header); |
| 128 |
ut_ad(slot < TRX_SYS_N_RSEGS); |
| 129 |
|
| 130 |
mlog_write_ulint(sys_header + TRX_SYS_RSEGS + slot * TRX_SYS_RSEG_SLOT_SIZE + |
| 131 |
TRX_SYS_RSEG_SPACE, |
| 132 |
space, MLOG_4BYTES, mtr); |
| 133 |
} |
| 134 |
|
| 135 |
/** Sets the page number of the nth rollback segment slot in the trx system |
| 136 |
header. */ |
| 137 |
static inline void trx_sysf_rseg_set_page_no( |
| 138 |
trx_sysf_t *sys_header, /*!< in: trx sys header */ |
| 139 |
ulint slot, /*!< in: slot index == rseg id */ |
| 140 |
page_no_t page_no, /*!< in: page number, FIL_NULL if the |
| 141 |
slot is reset to unused */ |
| 142 |
mtr_t *mtr) /*!< in: mtr */ |
| 143 |
{ |
| 144 |
ut_ad(sys_header); |
| 145 |
ut_ad(slot < TRX_SYS_N_RSEGS); |
| 146 |
|
| 147 |
mlog_write_ulint(sys_header + TRX_SYS_RSEGS + slot * TRX_SYS_RSEG_SLOT_SIZE + |
| 148 |
TRX_SYS_RSEG_PAGE_NO, |
| 149 |
page_no, MLOG_4BYTES, mtr); |
| 150 |
} |
| 151 |
#endif /* !UNIV_HOTBACKUP */ |
| 152 |
|
| 153 |
/** Writes a trx id to an index page. In case that the id size changes in |
| 154 |
some future version, this function should be used instead of |
| 155 |
mach_write_... */ |
| 156 |
static inline void trx_write_trx_id( |
| 157 |
byte *ptr, /*!< in: pointer to memory where written */ |
| 158 |
trx_id_t id) /*!< in: id */ |
| 159 |
{ |
| 160 |
ut_ad(id > 0); |
| 161 |
mach_write_to_6(ptr, id); |
| 162 |
} |
| 163 |
|
| 164 |
#ifndef UNIV_HOTBACKUP |
| 165 |
/** Reads a trx id from an index page. In case that the id size changes in |
| 166 |
some future version, this function should be used instead of |
| 167 |
mach_read_... |
| 168 |
@return id */ |
| 169 |
static inline trx_id_t trx_read_trx_id( |
| 170 |
const byte *ptr) /*!< in: pointer to memory from where to read */ |
| 171 |
{ |
| 172 |
return (mach_read_from_6(ptr)); |
| 173 |
} |
| 174 |
|
| 175 |
static inline trx_t *trx_rw_is_active(trx_id_t trx_id, bool do_ref_count) { |
| 176 |
/* Fast checking without Trx_shard::mutex to see if trx_id can't be in the |
| 177 |
Trx_shard::active_rw_trxs because it's too small. This works, because |
| 178 |
whenever we add or remove an element from Trx_by_id_with_min::m_by_id under |
| 179 |
Trx_shard::mutex, we also modify the m_min_id to be lower or equal to |
| 180 |
each of ids inside Trx_by_id_with_min::m_by_id in the same critical |
| 181 |
section. We assume that trx_id is an identifier of a transaction for which the |
| 182 |
call to trx_sys_rw_trx_add(trx) happened-before the current call to |
| 183 |
trx_rw_is_active(trx_id,..) - indeed in current implementation of InnoDB, the |
| 184 |
trx_id comes from: |
| 185 |
- a recovered transaction (for which trx_sys_rw_trx_add happened before crash) |
| 186 |
- a header of a row (stored there by an active transaction which must have |
| 187 |
finished a call to trx_sys_rw_trx_add before) |
| 188 |
- a prepared transaction (which must have called trx_sys_rw_trx_add before |
| 189 |
becoming PREPAREd) |
| 190 |
so clearly trx_sys_rw_trx_add() happened-before. |
| 191 |
Under this assumption, if trx_id < Trx_shard::min_id, then the value of |
| 192 |
Trx_shard::min_id is loaded from a store which must have occurred in a |
| 193 |
critical section protected by Trx_shard::mutex which happened-after the |
| 194 |
trx_sys_rw_trx_add(trx) for the trx with trx->id==trx_id - it could not be an |
| 195 |
earlier critical section, by our assumption, and it couldn't be stored during |
| 196 |
that particular trx_sys_rw_trx_add(trx) because of the invariant for min_id. |
| 197 |
Further, given that the loaded value is larger than trx_id, it must be the |
| 198 |
case that during that critical section the trx with trx->id==trx_id no longer |
| 199 |
belonged to Trx_shard::active_rw_trxs.m_by_id. In other words, this load of |
| 200 |
min_id synchronizes with a store, which happens-after Trx_shard::mutex |
| 201 |
acquisition of a critical section in which trx_id was not in |
| 202 |
active_rw_trxs.m_by_id. So, if this thread were to check the contents |
| 203 |
of active_rw_trxs.m_by_id after the if's condition evaluated to true, |
| 204 |
it would first have to acquire Trx_shard::mutex, which could only succeed |
| 205 |
*after* that critical section, thus it would not find trx_id in it for sure. |
| 206 |
NOTE: to appreciate why the assumption is important, observe, that if we call |
| 207 |
the trx_rw_is_active(trx_id,..) with trx_id for which trx_sys_rw_trx_add(trx) |
| 208 |
wasn't called yet, then it could happen, that trx_id+TRX_SHARDS_N was already |
| 209 |
assigned and added to Trx_shard::active_rw_trxs.m_by_id, and Trx_shard::min_id |
| 210 |
was set to larger than trx_id, so we decide to return nullptr, even though, if |
| 211 |
we were to repeat the call in just a moment we might get a different result if |
| 212 |
the trx_sys_rw_trx_add() for the trx_id happens meanwhile. */ |
| 213 |
ut_ad(trx_id < trx_sys_get_next_trx_id_or_no()); |
| 214 |
auto &shard = trx_sys->get_shard_by_trx_id(trx_id); |
| 215 |
if (trx_id < shard.active_rw_trxs.peek().min_id()) { |
| 216 |
return nullptr; |
| 217 |
} |
| 218 |
return trx_sys->latch_and_execute_with_active_trx( |
| 219 |
trx_id, |
| 220 |
[&](trx_t *trx) { |
| 221 |
if (trx != nullptr && do_ref_count) { |
| 222 |
trx_reference(trx); |
| 223 |
} |
| 224 |
return trx; |
| 225 |
}, |
| 226 |
UT_LOCATION_HERE); |
| 227 |
} |
| 228 |
|
| 229 |
inline trx_id_t trx_sys_get_trx_id_write_margin() { |
| 230 |
DBUG_EXECUTE_IF("trx_sys_low_trx_id_write_margin", return 8;); |
| 231 |
return TRX_SYS_TRX_ID_WRITE_MARGIN; |
| 232 |
} |
| 233 |
|
| 234 |
/** Allocates a new transaction id or transaction number. |
| 235 |
@return new, allocated trx id or trx no */ |
| 236 |
inline trx_id_t trx_sys_allocate_trx_id_or_no() { |
| 237 |
ut_ad(trx_sys_mutex_own() || trx_sys_serialisation_mutex_own()); |
| 238 |
|
| 239 |
trx_id_t trx_id = trx_sys->next_trx_id_or_no.fetch_add(1); |
| 240 |
|
| 241 |
if (trx_id % trx_sys_get_trx_id_write_margin() == 0) { |
| 242 |
/* Reserve the next range of trx_id values. This thread has acquired |
| 243 |
either the trx_sys_mutex or the trx_sys_serialisation_mutex. |
| 244 |
|
| 245 |
Therefore at least one of these two mutexes, is latched and it stays |
| 246 |
latched until the call to trx_sys_write_max_trx_id() is finished. |
| 247 |
|
| 248 |
Meanwhile other threads could be acquiring the other of these two mutexes, |
| 249 |
reserving more and more trx_id values, until TRX_SYS_TRX_ID_WRITE_MARGIN |
| 250 |
next values are reserved, when another trx_sys_write_max_trx_id() would |
| 251 |
be called. */ |
| 252 |
|
| 253 |
trx_sys_write_max_trx_id(); |
| 254 |
} |
| 255 |
return trx_id; |
| 256 |
} |
| 257 |
|
| 258 |
inline trx_id_t trx_sys_allocate_trx_id() { |
| 259 |
ut_ad(trx_sys_mutex_own()); |
| 260 |
return trx_sys_allocate_trx_id_or_no(); |
| 261 |
} |
| 262 |
|
| 263 |
inline trx_id_t trx_sys_allocate_trx_no() { |
| 264 |
ut_ad(trx_sys_serialisation_mutex_own()); |
| 265 |
return trx_sys_allocate_trx_id_or_no(); |
| 266 |
} |
| 267 |
|
| 268 |
/** Reads trx->no up to which all transactions have been serialised. |
| 269 |
@return minimum value which is still possibly not serialised */ |
| 270 |
static inline trx_id_t trx_get_serialisation_min_trx_no(void) { |
| 271 |
return (trx_sys->serialisation_min_trx_no.load()); |
| 272 |
} |
| 273 |
|
| 274 |
inline trx_id_t trx_sys_get_next_trx_id_or_no() { |
| 275 |
return trx_sys->next_trx_id_or_no.load(); |
| 276 |
} |
| 277 |
|
| 278 |
/** Determine if there are incomplete transactions in the system. |
| 279 |
@return whether incomplete transactions need rollback */ |
| 280 |
static inline bool trx_sys_need_rollback() { |
| 281 |
ulint n_trx; |
| 282 |
|
| 283 |
trx_sys_mutex_enter(); |
| 284 |
|
| 285 |
n_trx = UT_LIST_GET_LEN(trx_sys->rw_trx_list); |
| 286 |
ut_ad(n_trx >= trx_sys->n_prepared_trx); |
| 287 |
n_trx -= trx_sys->n_prepared_trx; |
| 288 |
|
| 289 |
trx_sys_mutex_exit(); |
| 290 |
|
| 291 |
return (n_trx > 0); |
| 292 |
} |
| 293 |
|
| 294 |
static inline void trx_sys_rw_trx_add(trx_t *trx) { |
| 295 |
const trx_id_t trx_id = trx->id; |
| 296 |
ut_ad(trx_id != 0); |
| 297 |
const auto trx_shard_no = trx_get_shard_no(trx_id); |
| 298 |
DBUG_EXECUTE_IF("trx_sys_rw_trx_add_rc", { |
| 299 |
if (trx_shard_no == 123 && (trx_id / TRX_SHARDS_N) % 3 == 0) { |
| 300 |
std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 301 |
} |
| 302 |
}); |
| 303 |
trx_sys->shards[trx_shard_no].active_rw_trxs.latch_and_execute( |
| 304 |
[&](Trx_by_id_with_min &trx_by_id_with_min) { |
| 305 |
trx_by_id_with_min.insert(*trx); |
| 306 |
ut_ad(trx_by_id_with_min.min_id() <= trx_id); |
| 307 |
ut_ad(trx_by_id_with_min.min_id() % TRX_SHARDS_N == trx_shard_no); |
| 308 |
}, |
| 309 |
UT_LOCATION_HERE); |
| 310 |
} |
| 311 |
|
| 312 |
#endif /* !UNIV_HOTBACKUP */ |