| 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/trx0trx.ic |
| 29 |
The transaction |
| 30 |
|
| 31 |
Created 3/26/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "read0read.h" |
| 35 |
|
| 36 |
static inline bool trx_state_eq(const trx_t *trx, trx_state_t state) { |
| 37 |
const auto trx_state = trx->state.load(std::memory_order_relaxed); |
| 38 |
#ifdef UNIV_DEBUG |
| 39 |
switch (trx_state) { |
| 40 |
case TRX_STATE_PREPARED: |
| 41 |
|
| 42 |
ut_ad(!trx_is_autocommit_non_locking(trx)); |
| 43 |
return trx_state == state; |
| 44 |
|
| 45 |
case TRX_STATE_ACTIVE: |
| 46 |
|
| 47 |
assert_trx_nonlocking_or_in_list(trx); |
| 48 |
return state == trx_state; |
| 49 |
|
| 50 |
case TRX_STATE_COMMITTED_IN_MEMORY: |
| 51 |
|
| 52 |
check_trx_state(trx); |
| 53 |
return state == trx_state; |
| 54 |
|
| 55 |
case TRX_STATE_NOT_STARTED: |
| 56 |
case TRX_STATE_FORCED_ROLLBACK: |
| 57 |
|
| 58 |
/* These states are not allowed for running transactions. */ |
| 59 |
ut_a(state == TRX_STATE_NOT_STARTED || |
| 60 |
state == TRX_STATE_FORCED_ROLLBACK); |
| 61 |
|
| 62 |
ut_ad(!trx->in_rw_trx_list); |
| 63 |
|
| 64 |
return true; |
| 65 |
} |
| 66 |
ut_error; |
| 67 |
#else /* UNIV_DEBUG */ |
| 68 |
return trx_state == state; |
| 69 |
#endif /* UNIV_DEBUG */ |
| 70 |
} |
| 71 |
|
| 72 |
/** Retrieves the index causing error from a trx. |
| 73 |
@param[in] trx trx object |
| 74 |
@return the error index */ |
| 75 |
static inline const dict_index_t *trx_get_error_index(const trx_t *trx) { |
| 76 |
return (trx->error_index); |
| 77 |
} |
| 78 |
|
| 79 |
/** Retrieves transaction's que state in a human readable string. The string |
| 80 |
should not be free()'d or modified. |
| 81 |
@return string in the data segment */ |
| 82 |
static inline const char *trx_get_que_state_str( |
| 83 |
const trx_t *trx) /*!< in: transaction */ |
| 84 |
{ |
| 85 |
/* be sure to adjust TRX_QUE_STATE_STR_MAX_LEN if you change this */ |
| 86 |
switch (trx->lock.que_state) { |
| 87 |
case TRX_QUE_RUNNING: |
| 88 |
return ("RUNNING"); |
| 89 |
case TRX_QUE_LOCK_WAIT: |
| 90 |
return ("LOCK WAIT"); |
| 91 |
case TRX_QUE_ROLLING_BACK: |
| 92 |
return ("ROLLING BACK"); |
| 93 |
case TRX_QUE_COMMITTING: |
| 94 |
return ("COMMITTING"); |
| 95 |
default: |
| 96 |
return ("UNKNOWN"); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** Retreieves the transaction ID. |
| 101 |
In a given point in time it is guaranteed that IDs of the running |
| 102 |
transactions are unique. The values returned by this function for readonly |
| 103 |
transactions may be reused, so a subsequent RO transaction may get the same ID |
| 104 |
as a RO transaction that existed in the past. The values returned by this |
| 105 |
function should be used for printing purposes only. |
| 106 |
@param[in] trx transaction whose id to retrieve |
| 107 |
@return transaction id */ |
| 108 |
static inline trx_id_t trx_get_id_for_print(const trx_t *trx) { |
| 109 |
/* Readonly and transactions whose intentions are unknown (whether |
| 110 |
they will eventually do a WRITE) don't have trx_t::id assigned (it is |
| 111 |
0 for those transactions). Transaction IDs in |
| 112 |
information_schema.innodb_trx.trx_id, |
| 113 |
performance_schema.data_locks.engine_transaction_id, |
| 114 |
performance_schema.data_lock_waits.requesting_engine_transaction_id, |
| 115 |
performance_schema.data_lock_waits.blocking_engine_transaction_id |
| 116 |
should match because those tables |
| 117 |
could be used in an SQL JOIN on those columns. Also trx_t::id is |
| 118 |
printed by SHOW ENGINE INNODB STATUS, and in logs, so we must have the |
| 119 |
same value printed everywhere consistently. */ |
| 120 |
|
| 121 |
/* DATA_TRX_ID_LEN is the storage size in bytes. */ |
| 122 |
static const trx_id_t max_trx_id = (1ULL << (DATA_TRX_ID_LEN * CHAR_BIT)) - 1; |
| 123 |
|
| 124 |
ut_ad(trx->id <= max_trx_id); |
| 125 |
|
| 126 |
/* on some 32bit architectures casting trx_t* (4 bytes) directly to |
| 127 |
trx_id_t (8 bytes unsigned) does sign extension and the resulting value |
| 128 |
has highest 32 bits set to 1, so the number is unnecessarily huge. |
| 129 |
Also there is no guarantee that we will obtain the same integer each time. |
| 130 |
Casting to uintptr_t first, and then extending to 64 bits keeps the highest |
| 131 |
bits clean. */ |
| 132 |
|
| 133 |
return (trx->id != 0 |
| 134 |
? trx->id |
| 135 |
: trx_id_t{reinterpret_cast<uintptr_t>(trx)} | (max_trx_id + 1)); |
| 136 |
} |
| 137 |
|
| 138 |
/** Determine if a transaction is a dictionary operation. |
| 139 |
@return dictionary operation mode */ |
| 140 |
static inline enum trx_dict_op_t trx_get_dict_operation( |
| 141 |
const trx_t *trx) /*!< in: transaction */ |
| 142 |
{ |
| 143 |
trx_dict_op_t op = static_cast<trx_dict_op_t>(trx->dict_operation); |
| 144 |
|
| 145 |
#ifdef UNIV_DEBUG |
| 146 |
switch (op) { |
| 147 |
case TRX_DICT_OP_NONE: |
| 148 |
case TRX_DICT_OP_TABLE: |
| 149 |
case TRX_DICT_OP_INDEX: |
| 150 |
return (op); |
| 151 |
} |
| 152 |
ut_error; |
| 153 |
#else /* UNIV_DEBUG */ |
| 154 |
return (op); |
| 155 |
#endif |
| 156 |
} |
| 157 |
/** Flag a transaction a dictionary operation. */ |
| 158 |
static inline void trx_set_dict_operation( |
| 159 |
trx_t *trx, /*!< in/out: transaction */ |
| 160 |
enum trx_dict_op_t op) /*!< in: operation, not |
| 161 |
TRX_DICT_OP_NONE */ |
| 162 |
{ |
| 163 |
#ifdef UNIV_DEBUG |
| 164 |
enum trx_dict_op_t old_op = trx_get_dict_operation(trx); |
| 165 |
|
| 166 |
switch (op) { |
| 167 |
case TRX_DICT_OP_NONE: |
| 168 |
ut_error; |
| 169 |
break; |
| 170 |
case TRX_DICT_OP_TABLE: |
| 171 |
switch (old_op) { |
| 172 |
case TRX_DICT_OP_NONE: |
| 173 |
case TRX_DICT_OP_INDEX: |
| 174 |
case TRX_DICT_OP_TABLE: |
| 175 |
goto ok; |
| 176 |
} |
| 177 |
ut_error; |
| 178 |
break; |
| 179 |
case TRX_DICT_OP_INDEX: |
| 180 |
ut_ad(old_op == TRX_DICT_OP_NONE); |
| 181 |
break; |
| 182 |
} |
| 183 |
ok: |
| 184 |
#endif /* UNIV_DEBUG */ |
| 185 |
|
| 186 |
trx->dict_operation = op; |
| 187 |
} |
| 188 |
|
| 189 |
/** Check if redo rseg is modified for insert/update. */ |
| 190 |
static inline bool trx_is_redo_rseg_updated( |
| 191 |
const trx_t *trx) /*!< in: transaction */ |
| 192 |
{ |
| 193 |
return (trx->rsegs.m_redo.insert_undo != nullptr || |
| 194 |
trx->rsegs.m_redo.update_undo != nullptr); |
| 195 |
} |
| 196 |
|
| 197 |
/** Check if noredo rseg is modified for insert/update. */ |
| 198 |
static inline bool trx_is_temp_rseg_updated( |
| 199 |
const trx_t *trx) /*!< in: transaction */ |
| 200 |
{ |
| 201 |
return (trx->rsegs.m_noredo.insert_undo != nullptr || |
| 202 |
trx->rsegs.m_noredo.update_undo != nullptr); |
| 203 |
} |
| 204 |
|
| 205 |
/** Check if redo/noredo rseg is modified for insert/update. */ |
| 206 |
static inline bool trx_is_rseg_updated(const trx_t *trx) /*!< in: transaction */ |
| 207 |
{ |
| 208 |
return (trx_is_redo_rseg_updated(trx) || trx_is_temp_rseg_updated(trx)); |
| 209 |
} |
| 210 |
|
| 211 |
/** Check if redo/nonredo rseg is valid. */ |
| 212 |
static inline bool trx_is_rseg_assigned( |
| 213 |
const trx_t *trx) /*!< in: transaction */ |
| 214 |
{ |
| 215 |
return (trx->rsegs.m_redo.rseg != nullptr || |
| 216 |
trx->rsegs.m_noredo.rseg != nullptr); |
| 217 |
} |
| 218 |
|
| 219 |
static inline void trx_reference(trx_t *trx) { |
| 220 |
trx_mutex_enter(trx); |
| 221 |
ut_a(!trx_state_eq(trx, TRX_STATE_COMMITTED_IN_MEMORY)); |
| 222 |
ut_ad(trx->n_ref >= 0); |
| 223 |
++trx->n_ref; |
| 224 |
trx_mutex_exit(trx); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
Release the transaction. Decrease the reference count. |
| 229 |
@param trx Transaction that is being released */ |
| 230 |
static inline void trx_release_reference(trx_t *trx) { |
| 231 |
trx_mutex_enter(trx); |
| 232 |
|
| 233 |
ut_ad(trx->n_ref > 0); |
| 234 |
--trx->n_ref; |
| 235 |
|
| 236 |
trx_mutex_exit(trx); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
@param trx Get the active view for this transaction, if one exists |
| 241 |
@return the transaction's read view or NULL if one not assigned. */ |
| 242 |
static inline ReadView *trx_get_read_view(trx_t *trx) { |
| 243 |
return (!MVCC::is_view_active(trx->read_view) ? nullptr : trx->read_view); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
@param trx Get the active view for this transaction, if one exists |
| 248 |
@return the transaction's read view or NULL if one not assigned. */ |
| 249 |
static inline const ReadView *trx_get_read_view(const trx_t *trx) { |
| 250 |
return (!MVCC::is_view_active(trx->read_view) ? nullptr : trx->read_view); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
@param[in] trx Transaction to check |
| 255 |
@return true if the transaction is a high priority transaction.*/ |
| 256 |
static inline bool trx_is_high_priority(const trx_t *trx) { |
| 257 |
if (trx->mysql_thd == nullptr) { |
| 258 |
return (false); |
| 259 |
} |
| 260 |
|
| 261 |
return (thd_trx_priority(trx->mysql_thd) > 0); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
@param[in] requestor Transaction requesting the lock |
| 266 |
@param[in] holder Transaction holding the lock |
| 267 |
@return the transaction that will be rolled back, null don't care */ |
| 268 |
static inline const trx_t *trx_arbitrate(const trx_t *requestor, |
| 269 |
const trx_t *holder) { |
| 270 |
ut_ad(!trx_is_autocommit_non_locking(holder)); |
| 271 |
ut_ad(!trx_is_autocommit_non_locking(requestor)); |
| 272 |
|
| 273 |
/* Note: Background stats collection transactions also acquire |
| 274 |
locks on user tables. They don't have an associated MySQL session |
| 275 |
instance. */ |
| 276 |
|
| 277 |
if (requestor->mysql_thd == nullptr) { |
| 278 |
ut_ad(!trx_is_high_priority(requestor)); |
| 279 |
|
| 280 |
if (trx_is_high_priority(holder)) { |
| 281 |
return (requestor); |
| 282 |
} else { |
| 283 |
return (nullptr); |
| 284 |
} |
| 285 |
|
| 286 |
} else if (holder->mysql_thd == nullptr) { |
| 287 |
ut_ad(!trx_is_high_priority(holder)); |
| 288 |
|
| 289 |
if (trx_is_high_priority(requestor)) { |
| 290 |
return (holder); |
| 291 |
} |
| 292 |
|
| 293 |
return (nullptr); |
| 294 |
} |
| 295 |
|
| 296 |
const THD *victim = |
| 297 |
thd_trx_arbitrate(requestor->mysql_thd, holder->mysql_thd); |
| 298 |
|
| 299 |
ut_ad(victim == nullptr || victim == requestor->mysql_thd || |
| 300 |
victim == holder->mysql_thd); |
| 301 |
|
| 302 |
if (victim != nullptr) { |
| 303 |
return (victim == requestor->mysql_thd ? requestor : holder); |
| 304 |
} |
| 305 |
|
| 306 |
return (nullptr); |
| 307 |
} |