| 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.h |
| 29 |
The transaction |
| 30 |
|
| 31 |
Created 3/26/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef trx0trx_h |
| 35 |
#define trx0trx_h |
| 36 |
|
| 37 |
#include <atomic> |
| 38 |
#include <set> |
| 39 |
|
| 40 |
#include "ha_prototypes.h" |
| 41 |
|
| 42 |
#include "dict0types.h" |
| 43 |
#include "trx0types.h" |
| 44 |
#include "ut0new.h" |
| 45 |
|
| 46 |
#include "lock0types.h" |
| 47 |
#include "mem0mem.h" |
| 48 |
#include "que0types.h" |
| 49 |
#include "trx0xa.h" |
| 50 |
#include "usr0types.h" |
| 51 |
#include "ut0vec.h" |
| 52 |
#ifndef UNIV_HOTBACKUP |
| 53 |
#include "fts0fts.h" |
| 54 |
#endif /* !UNIV_HOTBACKUP */ |
| 55 |
#include "read0read.h" |
| 56 |
#include "sql/handler.h" // Xa_state_list |
| 57 |
#include "srv0srv.h" |
| 58 |
|
| 59 |
/* std::vector to store the trx id & table id of tables that needs to be |
| 60 |
* rollbacked. We take SHARED MDL on these tables inside |
| 61 |
* trx_recovery_rollback_thread before letting server accept connections */ |
| 62 |
extern std::vector<std::pair<trx_id_t, table_id_t>> to_rollback_trx_tables; |
| 63 |
|
| 64 |
// Forward declaration |
| 65 |
struct mtr_t; |
| 66 |
|
| 67 |
// Forward declaration |
| 68 |
class ReadView; |
| 69 |
|
| 70 |
// Forward declaration |
| 71 |
class Flush_observer; |
| 72 |
|
| 73 |
/** Dummy session used currently in MySQL interface */ |
| 74 |
extern sess_t *trx_dummy_sess; |
| 75 |
|
| 76 |
#ifndef UNIV_HOTBACKUP |
| 77 |
/** Set flush observer for the transaction |
| 78 |
@param[in,out] trx transaction struct |
| 79 |
@param[in] observer flush observer */ |
| 80 |
void trx_set_flush_observer(trx_t *trx, Flush_observer *observer); |
| 81 |
|
| 82 |
/** Set detailed error message for the transaction. |
| 83 |
@param[in] trx Transaction struct |
| 84 |
@param[in] msg Detailed error message */ |
| 85 |
void trx_set_detailed_error(trx_t *trx, const char *msg); |
| 86 |
|
| 87 |
/** Set detailed error message for the transaction from a file. Note that the |
| 88 |
file is rewinded before reading from it. */ |
| 89 |
void trx_set_detailed_error_from_file( |
| 90 |
trx_t *trx, /*!< in: transaction struct */ |
| 91 |
FILE *file); /*!< in: file to read message from */ |
| 92 |
/** Retrieves the error_info field from a trx. |
| 93 |
@return the error index */ |
| 94 |
static inline const dict_index_t *trx_get_error_index( |
| 95 |
const trx_t *trx); /*!< in: trx object */ |
| 96 |
/** Creates a transaction object for MySQL. |
| 97 |
@return own: transaction object */ |
| 98 |
trx_t *trx_allocate_for_mysql(void); |
| 99 |
/** Creates a transaction object for background operations by the master thread. |
| 100 |
@return own: transaction object */ |
| 101 |
trx_t *trx_allocate_for_background(void); |
| 102 |
|
| 103 |
/** Resurrect table locks for resurrected transactions. |
| 104 |
@param[in] all false: resurrect locks for dictionary transactions, |
| 105 |
true : resurrect locks for all transactions. */ |
| 106 |
void trx_resurrect_locks(bool all); |
| 107 |
|
| 108 |
/** Clear all resurrected table IDs. Needs to be called after all tables locks |
| 109 |
are resurrected. */ |
| 110 |
void trx_clear_resurrected_table_ids(); |
| 111 |
|
| 112 |
/** Free and initialize a transaction object instantiated during recovery. |
| 113 |
@param[in,out] trx transaction object to free and initialize */ |
| 114 |
void trx_free_resurrected(trx_t *trx); |
| 115 |
|
| 116 |
/** Free a transaction that was allocated by background or user threads. |
| 117 |
@param[in,out] trx transaction object to free */ |
| 118 |
void trx_free_for_background(trx_t *trx); |
| 119 |
|
| 120 |
/** At shutdown, frees a transaction object that represents either: |
| 121 |
- a PREPARED transaction, |
| 122 |
- or a recovered ACTIVE transaction. |
| 123 |
@param[in, out] trx transaction object to free */ |
| 124 |
void trx_free_prepared_or_active_recovered(trx_t *trx); |
| 125 |
|
| 126 |
/** Free a transaction object for MySQL. |
| 127 |
@param[in,out] trx transaction */ |
| 128 |
void trx_free_for_mysql(trx_t *trx); |
| 129 |
|
| 130 |
/** Disconnect a transaction from MySQL. |
| 131 |
@param[in,out] trx transaction */ |
| 132 |
void trx_disconnect_plain(trx_t *trx); |
| 133 |
|
| 134 |
/** Disconnect a prepared transaction from MySQL. |
| 135 |
@param[in,out] trx transaction */ |
| 136 |
void trx_disconnect_prepared(trx_t *trx); |
| 137 |
|
| 138 |
/** Creates trx objects for transactions and initializes the trx list of |
| 139 |
trx_sys at database start. Rollback segment and undo log lists must |
| 140 |
already exist when this function is called, because the lists of |
| 141 |
transactions to be rolled back or cleaned up are built based on the |
| 142 |
undo log lists. */ |
| 143 |
void trx_lists_init_at_db_start(void); |
| 144 |
|
| 145 |
/** Starts the transaction if it is not yet started. |
| 146 |
@param[in,out] trx Transaction |
| 147 |
@param[in] read_write True if read write transaction */ |
| 148 |
void trx_start_if_not_started_xa_low(trx_t *trx, bool read_write); |
| 149 |
|
| 150 |
/** Starts the transaction if it is not yet started. |
| 151 |
@param[in] trx Transaction |
| 152 |
@param[in] read_write True if read write transaction */ |
| 153 |
void trx_start_if_not_started_low(trx_t *trx, bool read_write); |
| 154 |
|
| 155 |
/** Starts a transaction for internal processing. */ |
| 156 |
void trx_start_internal_low(trx_t *trx); /*!< in/out: transaction */ |
| 157 |
|
| 158 |
/** Starts a read-only transaction for internal processing. |
| 159 |
@param[in,out] trx transaction to be started */ |
| 160 |
void trx_start_internal_read_only_low(trx_t *trx); |
| 161 |
|
| 162 |
/** Commits a transaction. */ |
| 163 |
void trx_commit(trx_t *trx); /*!< in/out: transaction */ |
| 164 |
|
| 165 |
/** Commits a transaction and a mini-transaction. |
| 166 |
@param[in,out] trx Transaction |
| 167 |
@param[in,out] mtr Mini-transaction (will be committed), or null if trx made no |
| 168 |
modifications */ |
| 169 |
void trx_commit_low(trx_t *trx, mtr_t *mtr); |
| 170 |
|
| 171 |
/** Cleans up a transaction at database startup. The cleanup is needed if |
| 172 |
the transaction already got to the middle of a commit when the database |
| 173 |
crashed, and we cannot roll it back. */ |
| 174 |
void trx_cleanup_at_db_startup(trx_t *trx); /*!< in: transaction */ |
| 175 |
/** Does the transaction commit for MySQL. |
| 176 |
@return DB_SUCCESS or error number */ |
| 177 |
dberr_t trx_commit_for_mysql(trx_t *trx); /*!< in/out: transaction */ |
| 178 |
|
| 179 |
/** |
| 180 |
Does the transaction prepare for MySQL. |
| 181 |
@param[in, out] trx Transaction instance to prepare */ |
| 182 |
|
| 183 |
dberr_t trx_prepare_for_mysql(trx_t *trx); |
| 184 |
|
| 185 |
/** This function is used to find number of prepared transactions and |
| 186 |
their transaction objects for a recovery. |
| 187 |
@return number of prepared transactions */ |
| 188 |
int trx_recover_for_mysql( |
| 189 |
XA_recover_txn *txn_list, /*!< in/out: prepared transactions */ |
| 190 |
ulint len, /*!< in: number of slots in xid_list */ |
| 191 |
MEM_ROOT *mem_root); /*!< in: memory for table names */ |
| 192 |
|
| 193 |
/** Find prepared transactions that are marked as prepared in TC, for recovery |
| 194 |
purposes. |
| 195 |
@param[in,out] xa_list prepared transactions state |
| 196 |
@return 0 if successful or error number */ |
| 197 |
int trx_recover_tc_for_mysql(Xa_state_list &xa_list); |
| 198 |
|
| 199 |
/** This function is used to find one X/Open XA distributed transaction |
| 200 |
which is in the prepared state |
| 201 |
@param[in] xid X/Open XA transaction identifier |
| 202 |
@return trx or NULL; on match, the trx->xid will be invalidated; |
| 203 |
note that the trx may have been committed */ |
| 204 |
trx_t *trx_get_trx_by_xid(const XID *xid); |
| 205 |
|
| 206 |
/** If required, flushes the log to disk if we called trx_commit_for_mysql() |
| 207 |
with trx->flush_log_later == true. */ |
| 208 |
void trx_commit_complete_for_mysql(trx_t *trx); /*!< in/out: transaction */ |
| 209 |
/** Marks the latest SQL statement ended. */ |
| 210 |
void trx_mark_sql_stat_end(trx_t *trx); /*!< in: trx handle */ |
| 211 |
/** Assigns a read view for a consistent read query. All the consistent reads |
| 212 |
within the same transaction will get the same read view, which is created |
| 213 |
when this function is first called for a new started transaction. */ |
| 214 |
ReadView *trx_assign_read_view(trx_t *trx); /*!< in: active transaction */ |
| 215 |
|
| 216 |
/** @return the transaction's read view or NULL if one not assigned. */ |
| 217 |
static inline ReadView *trx_get_read_view(trx_t *trx); |
| 218 |
|
| 219 |
/** @return the transaction's read view or NULL if one not assigned. */ |
| 220 |
static inline const ReadView *trx_get_read_view(const trx_t *trx); |
| 221 |
|
| 222 |
/** Prepares a transaction for commit/rollback. */ |
| 223 |
void trx_commit_or_rollback_prepare(trx_t *trx); /*!< in/out: transaction */ |
| 224 |
/** Creates a commit command node struct. |
| 225 |
@return own: commit node struct */ |
| 226 |
commit_node_t *trx_commit_node_create( |
| 227 |
mem_heap_t *heap); /*!< in: mem heap where created */ |
| 228 |
/** Performs an execution step for a commit type node in a query graph. |
| 229 |
@return query thread to run next, or NULL */ |
| 230 |
que_thr_t *trx_commit_step(que_thr_t *thr); /*!< in: query thread */ |
| 231 |
|
| 232 |
/** Prints info about a transaction. |
| 233 |
Caller must hold trx_sys->mutex. */ |
| 234 |
void trx_print_low(FILE *f, |
| 235 |
/*!< in: output stream */ |
| 236 |
const trx_t *trx, |
| 237 |
/*!< in: transaction */ |
| 238 |
ulint max_query_len, |
| 239 |
/*!< in: max query length to print, |
| 240 |
must be positive */ |
| 241 |
ulint n_rec_locks, |
| 242 |
/*!< in: lock_number_of_rows_locked(&trx->lock) */ |
| 243 |
ulint n_trx_locks, |
| 244 |
/*!< in: length of trx->lock.trx_locks */ |
| 245 |
ulint heap_size); |
| 246 |
/*!< in: mem_heap_get_size(trx->lock.lock_heap) */ |
| 247 |
|
| 248 |
/** Prints info about a transaction. |
| 249 |
The caller must hold lock_sys exclusive global latch and trx_sys->mutex. |
| 250 |
@param[in] f output stream |
| 251 |
@param[in] trx transaction |
| 252 |
@param[in] max_query_len max query length to print, must be positive */ |
| 253 |
void trx_print_latched(FILE *f, const trx_t *trx, ulint max_query_len); |
| 254 |
|
| 255 |
/** Prints info about a transaction. |
| 256 |
Acquires and releases lock_sys exclusive global latch and trx_sys->mutex. |
| 257 |
@param[in] f output stream |
| 258 |
@param[in] trx transaction |
| 259 |
@param[in] max_query_len max query length to print, must be positive */ |
| 260 |
void trx_print(FILE *f, const trx_t *trx, ulint max_query_len); |
| 261 |
|
| 262 |
/** Determine if a transaction is a dictionary operation. |
| 263 |
@return dictionary operation mode */ |
| 264 |
[[nodiscard]] static inline enum trx_dict_op_t trx_get_dict_operation( |
| 265 |
const trx_t *trx); /*!< in: transaction */ |
| 266 |
|
| 267 |
/** Flag a transaction a dictionary operation. |
| 268 |
@param[in,out] trx transaction |
| 269 |
@param[in] op operation, not TRX_DICT_OP_NONE */ |
| 270 |
static inline void trx_set_dict_operation(trx_t *trx, enum trx_dict_op_t op); |
| 271 |
|
| 272 |
/** Determines if a transaction is in the given state. |
| 273 |
The caller must hold trx_sys->mutex, or it must be the thread |
| 274 |
that is serving a running transaction. |
| 275 |
A running RW transaction must be in trx_sys->rw_trx_list. |
| 276 |
@param[in] trx Transaction. |
| 277 |
@param[in] state State. |
| 278 |
@return true if trx->state == state */ |
| 279 |
[[nodiscard]] static inline bool trx_state_eq(const trx_t *trx, |
| 280 |
trx_state_t state); |
| 281 |
#ifdef UNIV_DEBUG |
| 282 |
/** Determines if trx can be handled by current thread, which is when |
| 283 |
trx->mysql_thd is nullptr (a "background" trx) or equals current_thd. |
| 284 |
@param[in] trx The transaction to check |
| 285 |
@return true iff current thread can handle the transaction |
| 286 |
*/ |
| 287 |
bool trx_can_be_handled_by_current_thread(const trx_t *trx); |
| 288 |
|
| 289 |
/** Determines if trx can be handled by current thread, which is when |
| 290 |
trx->mysql_thd is nullptr (a "background" trx) or equals current_thd, |
| 291 |
or is a victim being killed by HP transaction run by the current thread. |
| 292 |
@param[in] trx The transaction to check |
| 293 |
@return true iff current thread can handle the transaction |
| 294 |
*/ |
| 295 |
bool trx_can_be_handled_by_current_thread_or_is_hp_victim(const trx_t *trx); |
| 296 |
|
| 297 |
/** Asserts that a transaction has been started. |
| 298 |
The caller must hold trx_sys->mutex. |
| 299 |
@return true if started */ |
| 300 |
[[nodiscard]] bool trx_assert_started(const trx_t *trx); /*!< in: transaction */ |
| 301 |
#endif /* UNIV_DEBUG */ |
| 302 |
|
| 303 |
/** Determines if the currently running transaction has been interrupted. |
| 304 |
@return true if interrupted */ |
| 305 |
bool trx_is_interrupted(const trx_t *trx); /*!< in: transaction */ |
| 306 |
/** Determines if the currently running transaction is in strict mode. |
| 307 |
@return true if strict */ |
| 308 |
bool trx_is_strict(trx_t *trx); /*!< in: transaction */ |
| 309 |
|
| 310 |
/** Compares the "weight" (or size) of two transactions. Transactions that |
| 311 |
have edited non-transactional tables are considered heavier than ones |
| 312 |
that have not. |
| 313 |
@return true if weight(a) >= weight(b) */ |
| 314 |
bool trx_weight_ge(const trx_t *a, /*!< in: the transaction to be compared */ |
| 315 |
const trx_t *b); /*!< in: the transaction to be compared */ |
| 316 |
/* Maximum length of a string that can be returned by |
| 317 |
trx_get_que_state_str(). */ |
| 318 |
constexpr uint32_t TRX_QUE_STATE_STR_MAX_LEN = 12; /* "ROLLING BACK" */ |
| 319 |
|
| 320 |
/** Retrieves transaction's que state in a human readable string. The string |
| 321 |
should not be free()'d or modified. |
| 322 |
@return string in the data segment */ |
| 323 |
static inline const char *trx_get_que_state_str( |
| 324 |
const trx_t *trx); /*!< in: transaction */ |
| 325 |
|
| 326 |
/** Retreieves the transaction ID. |
| 327 |
In a given point in time it is guaranteed that IDs of the running |
| 328 |
transactions are unique. The values returned by this function for readonly |
| 329 |
transactions may be reused, so a subsequent RO transaction may get the same ID |
| 330 |
as a RO transaction that existed in the past. The values returned by this |
| 331 |
function should be used for printing purposes only. |
| 332 |
@param[in] trx transaction whose id to retrieve |
| 333 |
@return transaction id */ |
| 334 |
static inline trx_id_t trx_get_id_for_print(const trx_t *trx); |
| 335 |
|
| 336 |
/** Assign a temp-tablespace bound rollback-segment to a transaction. |
| 337 |
@param[in,out] trx transaction that involves write to temp-table. */ |
| 338 |
void trx_assign_rseg_temp(trx_t *trx); |
| 339 |
|
| 340 |
/** Create the trx_t pool */ |
| 341 |
void trx_pool_init(); |
| 342 |
|
| 343 |
/** Destroy the trx_t pool */ |
| 344 |
void trx_pool_close(); |
| 345 |
|
| 346 |
/** |
| 347 |
Set the transaction as a read-write transaction if it is not already |
| 348 |
tagged as such. |
| 349 |
@param[in,out] trx Transaction that needs to be "upgraded" to RW from RO */ |
| 350 |
void trx_set_rw_mode(trx_t *trx); |
| 351 |
|
| 352 |
/** |
| 353 |
@param[in] requestor Transaction requesting the lock |
| 354 |
@param[in] holder Transaction holding the lock |
| 355 |
@return the transaction that will be rolled back, null don't care */ |
| 356 |
|
| 357 |
static inline const trx_t *trx_arbitrate(const trx_t *requestor, |
| 358 |
const trx_t *holder); |
| 359 |
|
| 360 |
/** |
| 361 |
@param[in] trx Transaction to check |
| 362 |
@return true if the transaction is a high priority transaction.*/ |
| 363 |
static inline bool trx_is_high_priority(const trx_t *trx); |
| 364 |
|
| 365 |
/** |
| 366 |
If this is a high priority transaction, |
| 367 |
kill all transactions that are blocking this transaction from acquiring locks. |
| 368 |
@param[in,out] trx High priority transaction */ |
| 369 |
void trx_kill_blocking(trx_t *trx); |
| 370 |
|
| 371 |
/** |
| 372 |
Check if redo/noredo rseg is modified for insert/update. |
| 373 |
@param[in] trx Transaction to check */ |
| 374 |
static inline bool trx_is_rseg_updated(const trx_t *trx); |
| 375 |
#endif /* !UNIV_HOTBACKUP */ |
| 376 |
|
| 377 |
typedef std::vector<ib_lock_t *, ut::allocator<ib_lock_t *>> lock_pool_t; |
| 378 |
|
| 379 |
/** Latching protocol for trx_lock_t::que_state. trx_lock_t::que_state |
| 380 |
captures the state of the query thread during the execution of a query. |
| 381 |
This is different from a transaction state. The query state of a transaction |
| 382 |
can be updated asynchronously by other threads. The other threads can be |
| 383 |
system threads, like the timeout monitor thread or user threads executing |
| 384 |
other queries. Another thing to be mindful of is that there is a delay between |
| 385 |
when a query thread is put into LOCK_WAIT state and before it actually starts |
| 386 |
waiting. Between these two events it is possible that the query thread is |
| 387 |
granted the lock it was waiting for, which implies that the state can be |
| 388 |
changed asynchronously. |
| 389 |
|
| 390 |
All these operations take place within the context of locking. Therefore state |
| 391 |
changes within the locking code must latch the shard with the wait_lock and |
| 392 |
the trx->mutex when changing trx->lock.que_state to TRX_QUE_LOCK_WAIT or |
| 393 |
trx->lock.wait_lock to non-NULL but when the lock wait ends it is sufficient |
| 394 |
to only acquire the trx->mutex. |
| 395 |
To query the state either of the mutexes is sufficient within the locking |
| 396 |
code and no mutex is required when the query thread is no longer waiting. */ |
| 397 |
|
| 398 |
/** The locks and state of an active transaction. |
| 399 |
Protected by exclusive lock_sys latch or trx->mutex combined with shared |
| 400 |
lock_sys latch (unless stated otherwise for particular field). */ |
| 401 |
struct trx_lock_t { |
| 402 |
/** Default constructor. */ |
| 403 |
trx_lock_t() = default; |
| 404 |
|
| 405 |
ulint n_active_thrs; /*!< number of active query threads */ |
| 406 |
|
| 407 |
trx_que_t que_state; /*!< valid when trx->state |
| 408 |
== TRX_STATE_ACTIVE: TRX_QUE_RUNNING, |
| 409 |
TRX_QUE_LOCK_WAIT, ... */ |
| 410 |
|
| 411 |
/** Incremented each time a lock is added or removed from the |
| 412 |
trx->lock.trx_locks, so that the thread which iterates over the list can spot |
| 413 |
a change if it occurred while it was reacquiring latches. |
| 414 |
Protected by trx->mutex. */ |
| 415 |
uint64_t trx_locks_version; |
| 416 |
|
| 417 |
/** If this transaction is waiting for a lock, then blocking_trx points to a |
| 418 |
transaction which holds a conflicting lock. |
| 419 |
It is possible that the transaction has trx->lock.wait_lock == nullptr, yet it |
| 420 |
has non-null value of trx->lock.blocking_trx. For example this can happen when |
| 421 |
we are in the process of moving locks from one heap_no to another. This |
| 422 |
however is always done while the lock_sys shards which contain the queues |
| 423 |
involved are latched and conceptually it is true that the blocking_trx is |
| 424 |
the one for which the transaction waits, even though temporarily there is no |
| 425 |
pointer to a particular WAITING lock object. |
| 426 |
|
| 427 |
This field is changed from null to non-null, when holding this->mutex and |
| 428 |
mutex for lock_sys shard containing the new value of trx->lock.wait_lock. |
| 429 |
The field is changed from non-null to different non-null value, while holding |
| 430 |
mutex for lock_sys shard containing the trx->lock.wait_lock. |
| 431 |
The field is changed from non-null to null, while holding this->mutex, |
| 432 |
mutex for lock_sys shard containing the old value of trx->lock.wait_lock, |
| 433 |
before it was changed to null. |
| 434 |
|
| 435 |
Readers might read it without any latch, but then they should validate the |
| 436 |
value, i.e. test if it is not-null, and points to a valid trx. |
| 437 |
To make any definite judgments one needs to latch the lock_sys shard |
| 438 |
containing the trx->lock.wait_lock. */ |
| 439 |
std::atomic<trx_t *> blocking_trx; |
| 440 |
|
| 441 |
/** The lock request of this transaction is waiting for. |
| 442 |
It might be NULL if the transaction is not currently waiting, or if the lock |
| 443 |
was temporarily removed during B-tree reorganization and will be recreated in |
| 444 |
a different queue. Such move is protected by latching the shards containing |
| 445 |
both queues, so the intermediate state should not be observed by readers who |
| 446 |
latch the old shard. |
| 447 |
|
| 448 |
Changes from NULL to non-NULL while holding trx->mutex and latching the shard |
| 449 |
containing the new wait_lock value. |
| 450 |
Changes from non-NULL to NULL while latching the shard containing the old |
| 451 |
wait_lock value. |
| 452 |
Never changes from non-NULL to other non-NULL directly. |
| 453 |
|
| 454 |
Readers should hold exclusive global latch on lock_sys, as in general they |
| 455 |
can't know what shard the lock belongs to before reading it. |
| 456 |
However, in debug assertions, where we strongly believe to know the value of |
| 457 |
this field in advance, we can: |
| 458 |
- read without any latch if we believe the value should be NULL |
| 459 |
- read latching only the shard containing the wait_lock we expect */ |
| 460 |
std::atomic<lock_t *> wait_lock; |
| 461 |
|
| 462 |
/** Stores the type of the most recent lock for which this trx had to wait. |
| 463 |
Set to lock_get_type_low(wait_lock) together with wait_lock in |
| 464 |
lock_set_lock_and_trx_wait(). |
| 465 |
This field is not cleared when wait_lock is set to NULL during |
| 466 |
lock_reset_lock_and_trx_wait() as in lock_wait_suspend_thread() we are |
| 467 |
interested in reporting the last known value of this field via |
| 468 |
thd_wait_begin(). When a thread has to wait for a lock, it first releases |
| 469 |
lock-sys latch, and then calls lock_wait_suspend_thread() where among other |
| 470 |
things it tries to report statistic via thd_wait_begin() about the kind of |
| 471 |
lock (THD_WAIT_ROW_LOCK vs THD_WAIT_TABLE_LOCK) that caused the wait. But |
| 472 |
there is a possibility that before it gets to call thd_wait_begin() some other |
| 473 |
thread could latch lock-sys and grant the lock and call |
| 474 |
lock_reset_lock_and_trx_wait(). In other words: in case another thread was |
| 475 |
quick enough to grant the lock, we still would like to report the reason for |
| 476 |
attempting to sleep. |
| 477 |
Another common scenario of "setting trx->lock.wait_lock to NULL" is page |
| 478 |
reorganization: when we have to move records between pages, we also move |
| 479 |
locks, and when doing so, we temporarily remove the old waiting lock, and then |
| 480 |
add another one. For example look at lock_rec_move_low(). It first calls |
| 481 |
lock_reset_lock_and_trx_wait() which changes trx->lock.wait_lock to NULL, but |
| 482 |
then calls lock_rec_add_to_queue() -> RecLock::create() -> RecLock::lock_add() |
| 483 |
-> lock_set_lock_and_trx_wait() to set it again to the new lock. This all |
| 484 |
happens while holding lock-sys latch, but we read wait_lock_type without this |
| 485 |
latch, so we should not clear the wait_lock_type simply because somebody |
| 486 |
changed wait_lock to NULL. |
| 487 |
Protected by trx->mutex. */ |
| 488 |
uint32_t wait_lock_type; |
| 489 |
|
| 490 |
bool was_chosen_as_deadlock_victim; |
| 491 |
/*!< when the transaction decides to |
| 492 |
wait for a lock, it sets this to false; |
| 493 |
if another transaction chooses this |
| 494 |
transaction as a victim in deadlock |
| 495 |
resolution, it sets this to true. |
| 496 |
Protected by trx->mutex. */ |
| 497 |
|
| 498 |
/** Lock wait started at this time. |
| 499 |
Writes under shared lock_sys latch combined with trx->mutex. |
| 500 |
Reads require either trx->mutex or exclusive lock_sys latch. */ |
| 501 |
std::chrono::system_clock::time_point wait_started; |
| 502 |
|
| 503 |
/** query thread belonging to this trx that is in QUE_THR_LOCK_WAIT state. |
| 504 |
For threads suspended in a lock wait, this is protected by lock_sys latch for |
| 505 |
the wait_lock's shard. |
| 506 |
Otherwise, this may only be modified by the thread that is serving the running |
| 507 |
transaction. |
| 508 |
*/ |
| 509 |
que_thr_t *wait_thr; |
| 510 |
|
| 511 |
/** Pre-allocated record locks. Protected by trx->mutex. */ |
| 512 |
lock_pool_t rec_pool; |
| 513 |
|
| 514 |
/** Pre-allocated table locks. Protected by trx->mutex. */ |
| 515 |
lock_pool_t table_pool; |
| 516 |
|
| 517 |
/** Next free record lock in pool. Protected by trx->mutex. */ |
| 518 |
ulint rec_cached; |
| 519 |
|
| 520 |
/** Next free table lock in pool. Protected by trx->mutex. */ |
| 521 |
ulint table_cached; |
| 522 |
|
| 523 |
/** Memory heap for trx_locks. Protected by trx->mutex */ |
| 524 |
mem_heap_t *lock_heap; |
| 525 |
|
| 526 |
/** Locks requested by the transaction. |
| 527 |
It is sorted so that LOCK_TABLE locks are before LOCK_REC locks. |
| 528 |
Modifications are protected by trx->mutex and shard of lock_sys mutex. |
| 529 |
Reads can be performed while holding trx->mutex or exclusive lock_sys latch. |
| 530 |
One can also check if this list is empty or not from the thread running this |
| 531 |
transaction without holding any latches, keeping in mind that other threads |
| 532 |
might modify the list in parallel (for example during implicit-to-explicit |
| 533 |
conversion, or when B-tree split or merge causes locks to be moved from one |
| 534 |
page to another) - we rely on assumption that such operations do not change |
| 535 |
the "emptiness" of the list and that one can check for emptiness in a safe |
| 536 |
manner (in current implementation length of the list is stored explicitly so |
| 537 |
one can read it without risking unsafe pointer operations) */ |
| 538 |
trx_lock_list_t trx_locks; |
| 539 |
|
| 540 |
/** AUTOINC locks held by this transaction. |
| 541 |
Note that these are also in the trx_locks list. |
| 542 |
This vector needs to be freed explicitly when the trx instance is destroyed. |
| 543 |
Protected by trx->mutex. */ |
| 544 |
ib_vector_t *autoinc_locks; |
| 545 |
|
| 546 |
/** Number of rec locks in this trx. |
| 547 |
It is modified with shared lock_sys latch. |
| 548 |
It is read with exclusive lock_sys latch. */ |
| 549 |
std::atomic<ulint> n_rec_locks; |
| 550 |
|
| 551 |
/** Used to indicate that every lock of this transaction placed on a record |
| 552 |
which is being purged should be inherited to the gap. |
| 553 |
Readers should hold a latch on the lock they'd like to learn about whether or |
| 554 |
not it should be inherited. |
| 555 |
Writers who want to set it to true, should hold a latch on the lock-sys queue |
| 556 |
they intend to add a lock to. |
| 557 |
Writers may set it to false at any time. */ |
| 558 |
std::atomic<bool> inherit_all; |
| 559 |
|
| 560 |
/** Weight of the waiting transaction used for scheduling. |
| 561 |
The higher the weight the more we are willing to grant a lock to this |
| 562 |
transaction. |
| 563 |
Values are updated and read without any synchronization beyond that provided |
| 564 |
by atomics, as slightly stale values do not hurt correctness, just the |
| 565 |
performance. */ |
| 566 |
std::atomic<trx_schedule_weight_t> schedule_weight; |
| 567 |
|
| 568 |
#ifdef UNIV_DEBUG |
| 569 |
/** When a transaction is forced to rollback due to a deadlock |
| 570 |
check or by another high priority transaction this is true. Used |
| 571 |
by debug checks in lock0lock.cc */ |
| 572 |
bool in_rollback; |
| 573 |
#endif /* UNIV_DEBUG */ |
| 574 |
|
| 575 |
/** The transaction called ha_innobase::start_stmt() to |
| 576 |
lock a table. Most likely a temporary table. */ |
| 577 |
bool start_stmt; |
| 578 |
}; |
| 579 |
|
| 580 |
/** Type used to store the list of tables that are modified by a given |
| 581 |
transaction. We store pointers to the table objects in memory because |
| 582 |
we know that a table object will not be destroyed while a transaction |
| 583 |
that modified it is running. */ |
| 584 |
typedef std::set<dict_table_t *, std::less<dict_table_t *>, |
| 585 |
ut::allocator<dict_table_t *>> |
| 586 |
trx_mod_tables_t; |
| 587 |
|
| 588 |
/** The transaction handle |
| 589 |
|
| 590 |
Normally, there is a 1:1 relationship between a transaction handle |
| 591 |
(trx) and a session (client connection). One session is associated |
| 592 |
with exactly one user transaction. There are some exceptions to this: |
| 593 |
|
| 594 |
* For DDL operations, a subtransaction is allocated that modifies the |
| 595 |
data dictionary tables. Lock waits and deadlocks are prevented by |
| 596 |
acquiring the dict_operation_lock before starting the subtransaction |
| 597 |
and releasing it after committing the subtransaction. |
| 598 |
|
| 599 |
* The purge system uses a special transaction that is not associated |
| 600 |
with any session. |
| 601 |
|
| 602 |
* If the system crashed or it was quickly shut down while there were |
| 603 |
transactions in the ACTIVE or PREPARED state, these transactions would |
| 604 |
no longer be associated with a session when the server is restarted. |
| 605 |
|
| 606 |
A session may be served by at most one thread at a time. The serving |
| 607 |
thread of a session might change in some MySQL implementations. |
| 608 |
Therefore we do not have std::this_thread::get_id() assertions in the code. |
| 609 |
|
| 610 |
Normally, only the thread that is currently associated with a running |
| 611 |
transaction may access (read and modify) the trx object, and it may do |
| 612 |
so without holding any mutex. The following are exceptions to this: |
| 613 |
|
| 614 |
* trx_rollback_resurrected() may access resurrected (connectionless) |
| 615 |
transactions while the system is already processing new user |
| 616 |
transactions. The trx_sys->mutex prevents a race condition between it |
| 617 |
and lock_trx_release_locks() [invoked by trx_commit()]. |
| 618 |
|
| 619 |
* Print of transactions may access transactions not associated with |
| 620 |
the current thread. The caller must be holding trx_sys->mutex and |
| 621 |
exclusive global lock_sys latch. |
| 622 |
|
| 623 |
* When a transaction handle is in the trx_sys->mysql_trx_list or |
| 624 |
trx_sys->trx_list, some of its fields must not be modified without |
| 625 |
holding trx_sys->mutex exclusively. |
| 626 |
|
| 627 |
* The locking code (in particular, deadlock checking and implicit to |
| 628 |
explicit conversion) will access transactions associated to other |
| 629 |
connections. The locks of transactions are protected by lock_sys latches |
| 630 |
and sometimes by trx->mutex. |
| 631 |
|
| 632 |
* Killing of asynchronous transactions. */ |
| 633 |
|
| 634 |
/** Represents an instance of rollback segment along with its state variables.*/ |
| 635 |
struct trx_undo_ptr_t { |
| 636 |
/** @return true iff no undo segment is allocated yet. */ |
| 637 |
bool is_empty() { return (insert_undo == nullptr && update_undo == nullptr); } |
| 638 |
|
| 639 |
/** @return true iff only insert undo segment is allocated. */ |
| 640 |
bool is_insert_only() { |
| 641 |
return (insert_undo != nullptr && update_undo == nullptr); |
| 642 |
} |
| 643 |
|
| 644 |
/** @return true iff update undo segment is allocated. */ |
| 645 |
bool is_update() { return update_undo != nullptr; } |
| 646 |
|
| 647 |
trx_rseg_t *rseg; /*!< rollback segment assigned to the |
| 648 |
transaction, or NULL if not assigned |
| 649 |
yet */ |
| 650 |
trx_undo_t *insert_undo; /*!< pointer to the insert undo log, or |
| 651 |
NULL if no inserts performed yet */ |
| 652 |
trx_undo_t *update_undo; /*!< pointer to the update undo log, or |
| 653 |
NULL if no update performed yet */ |
| 654 |
}; |
| 655 |
|
| 656 |
/** Rollback segments assigned to a transaction for undo logging. */ |
| 657 |
struct trx_rsegs_t { |
| 658 |
/** undo log ptr holding reference to a rollback segment that resides in |
| 659 |
system/undo tablespace used for undo logging of tables that needs |
| 660 |
to be recovered on crash. */ |
| 661 |
trx_undo_ptr_t m_redo; |
| 662 |
|
| 663 |
/** undo log ptr holding reference to a rollback segment that resides in |
| 664 |
temp tablespace used for undo logging of tables that doesn't need |
| 665 |
to be recovered on crash. */ |
| 666 |
trx_undo_ptr_t m_noredo; |
| 667 |
}; |
| 668 |
|
| 669 |
enum trx_rseg_type_t { |
| 670 |
TRX_RSEG_TYPE_NONE = 0, /*!< void rollback segment type. */ |
| 671 |
TRX_RSEG_TYPE_REDO, /*!< redo rollback segment. */ |
| 672 |
TRX_RSEG_TYPE_NOREDO /*!< non-redo rollback segment. */ |
| 673 |
}; |
| 674 |
|
| 675 |
struct trx_t { |
| 676 |
enum isolation_level_t { |
| 677 |
|
| 678 |
/** dirty read: non-locking SELECTs are performed so that we |
| 679 |
do not look at a possible earlier version of a record; thus |
| 680 |
they are not 'consistent' reads under this isolation level; |
| 681 |
otherwise like level 2 */ |
| 682 |
READ_UNCOMMITTED, |
| 683 |
|
| 684 |
/** somewhat Oracle-like isolation, except that in range UPDATE |
| 685 |
and DELETE we must block phantom rows with next-key locks; |
| 686 |
SELECT ... FOR UPDATE and ... LOCK IN SHARE MODE only lock |
| 687 |
the index records, NOT the gaps before them, and thus allow |
| 688 |
free inserting; each consistent read reads its own snapshot */ |
| 689 |
READ_COMMITTED, |
| 690 |
|
| 691 |
/** this is the default; all consistent reads in the same trx |
| 692 |
read the same snapshot; full next-key locking used in locking |
| 693 |
reads to block insertions into gaps */ |
| 694 |
REPEATABLE_READ, |
| 695 |
|
| 696 |
/** all plain SELECTs are converted to LOCK IN SHARE MODE |
| 697 |
reads */ |
| 698 |
SERIALIZABLE |
| 699 |
}; |
| 700 |
|
| 701 |
/** Default constructor */ |
| 702 |
trx_t() = default; |
| 703 |
|
| 704 |
/** Mutex protecting the fields `state` and `lock` (except some fields of |
| 705 |
`lock`, which are protected by lock_sys latches) */ |
| 706 |
mutable TrxMutex mutex; |
| 707 |
|
| 708 |
/* Note: in_depth was split from in_innodb for fixing a RO |
| 709 |
performance issue. Acquiring the trx_t::mutex for each row |
| 710 |
costs ~3% in performance. It is not required for correctness. |
| 711 |
Therefore we increment/decrement in_depth without holding any |
| 712 |
mutex. The assumption is that the Server will only ever call |
| 713 |
the handler from one thread. This is not true for kill_connection. |
| 714 |
Therefore in innobase_kill_connection. We don't increment this |
| 715 |
counter via TrxInInnoDB. */ |
| 716 |
|
| 717 |
uint32_t in_depth; /*!< Track nested TrxInInnoDB |
| 718 |
count */ |
| 719 |
|
| 720 |
uint32_t in_innodb; /*!< if the thread is executing |
| 721 |
in the InnoDB context count > 0. */ |
| 722 |
|
| 723 |
bool abort; /*!< if this flag is set then |
| 724 |
this transaction must abort when |
| 725 |
it can */ |
| 726 |
|
| 727 |
trx_id_t id; /*!< transaction id */ |
| 728 |
|
| 729 |
trx_id_t no; /*!< transaction serialization number: |
| 730 |
max trx id shortly before the |
| 731 |
transaction is moved to |
| 732 |
COMMITTED_IN_MEMORY state. |
| 733 |
Protected by trx_sys_t::mutex |
| 734 |
when trx->in_rw_trx_list. Initially |
| 735 |
set to TRX_ID_MAX. */ |
| 736 |
|
| 737 |
/** State of the trx from the point of view of concurrency control |
| 738 |
and the valid state transitions. |
| 739 |
|
| 740 |
Possible states: |
| 741 |
|
| 742 |
TRX_STATE_NOT_STARTED |
| 743 |
TRX_STATE_FORCED_ROLLBACK |
| 744 |
TRX_STATE_ACTIVE |
| 745 |
TRX_STATE_PREPARED |
| 746 |
TRX_STATE_COMMITTED_IN_MEMORY (alias below COMMITTED) |
| 747 |
|
| 748 |
Valid state transitions are: |
| 749 |
|
| 750 |
Regular transactions: |
| 751 |
* NOT_STARTED -> ACTIVE -> COMMITTED -> NOT_STARTED |
| 752 |
|
| 753 |
Auto-commit non-locking read-only: |
| 754 |
* NOT_STARTED -> ACTIVE -> NOT_STARTED |
| 755 |
|
| 756 |
XA (2PC): |
| 757 |
* NOT_STARTED -> ACTIVE -> PREPARED -> COMMITTED -> NOT_STARTED |
| 758 |
|
| 759 |
Recovered XA: |
| 760 |
* NOT_STARTED -> PREPARED -> COMMITTED -> (freed) |
| 761 |
|
| 762 |
XA (2PC) (shutdown or disconnect before ROLLBACK or COMMIT): |
| 763 |
* NOT_STARTED -> PREPARED -> (freed) |
| 764 |
|
| 765 |
Disconnected XA can become recovered: |
| 766 |
* ... -> ACTIVE -> PREPARED (connected) -> PREPARED (disconnected) |
| 767 |
Disconnected means from mysql e.g due to the mysql client disconnection. |
| 768 |
Latching and various transaction lists membership rules: |
| 769 |
|
| 770 |
XA (2PC) transactions are always treated as non-autocommit. |
| 771 |
|
| 772 |
Transitions to ACTIVE or NOT_STARTED occur when |
| 773 |
!in_rw_trx_list (no trx_sys->mutex needed). |
| 774 |
|
| 775 |
Autocommit non-locking read-only transactions move between states |
| 776 |
without holding any mutex. They are !in_rw_trx_list. |
| 777 |
|
| 778 |
All transactions, unless they are determined to be ac-nl-ro, |
| 779 |
explicitly tagged as read-only or read-write, will first be put |
| 780 |
on the read-only transaction list. Only when a !read-only transaction |
| 781 |
in the read-only list tries to acquire an X or IX lock on a table |
| 782 |
do we remove it from the read-only list and put it on the read-write |
| 783 |
list. During this switch we assign it a rollback segment. |
| 784 |
|
| 785 |
When a transaction is NOT_STARTED, it can be in_mysql_trx_list if |
| 786 |
it is a user transaction. It cannot be in rw_trx_list. |
| 787 |
|
| 788 |
ACTIVE->PREPARED->COMMITTED is only possible when trx->in_rw_trx_list. |
| 789 |
The transition ACTIVE->PREPARED is protected by trx_sys->mutex. |
| 790 |
|
| 791 |
ACTIVE->COMMITTED is possible when the transaction is in |
| 792 |
rw_trx_list. |
| 793 |
|
| 794 |
Transitions to COMMITTED are protected by trx->mutex. |
| 795 |
|
| 796 |
NOTE: Some of these state change constraints are an overkill, |
| 797 |
currently only required for a consistent view for printing stats. |
| 798 |
This unnecessarily adds a huge cost for the general case. */ |
| 799 |
|
| 800 |
std::atomic<trx_state_t> state; |
| 801 |
|
| 802 |
/* If set, this transaction should stop inheriting (GAP)locks. |
| 803 |
Generally set to true during transaction prepare for RC or lower |
| 804 |
isolation, if requested. Needed for replication replay where |
| 805 |
we don't want to get blocked on GAP locks taken for protecting |
| 806 |
concurrent unique insert or replace operation. */ |
| 807 |
bool skip_lock_inheritance; |
| 808 |
|
| 809 |
ReadView *read_view; /*!< consistent read view used in the |
| 810 |
transaction, or NULL if not yet set */ |
| 811 |
|
| 812 |
UT_LIST_NODE_T(trx_t) |
| 813 |
trx_list; /*!< list of transactions; |
| 814 |
protected by trx_sys->mutex. */ |
| 815 |
UT_LIST_NODE_T(trx_t) |
| 816 |
no_list; /*!< Required during view creation |
| 817 |
to check for the view limit for |
| 818 |
transactions that are committing */ |
| 819 |
|
| 820 |
/** Information about the transaction locks and state. |
| 821 |
Protected by trx->mutex or lock_sys latches or both */ |
| 822 |
trx_lock_t lock; |
| 823 |
|
| 824 |
/** |
| 825 |
false: a normal transaction |
| 826 |
true: a recovered transaction |
| 827 |
|
| 828 |
Set to true when srv_is_being_started for recovered transactions. |
| 829 |
Set to false without any protection in trx_init (where no other thread should |
| 830 |
access this object anyway). |
| 831 |
Can be read safely when holding trx_sys->mutex and trx belongs to rw_trx_list, |
| 832 |
as trx_init can not be called until trx leaves rw_trx_list which requires the |
| 833 |
trx_sys->mutex. |
| 834 |
*/ |
| 835 |
bool is_recovered; |
| 836 |
|
| 837 |
std::atomic<std::thread::id> killed_by; /*!< The thread ID that wants to |
| 838 |
kill this transaction asynchronously. |
| 839 |
This is required because we recursively |
| 840 |
enter the handlerton methods and need |
| 841 |
to distinguish between the kill thread |
| 842 |
and the transaction thread. |
| 843 |
|
| 844 |
Note: We need to be careful w.r.t the |
| 845 |
Thread Pool. The thread doing the kill |
| 846 |
should not leave InnoDB between the |
| 847 |
mark and the actual async kill because |
| 848 |
the running thread can change. */ |
| 849 |
|
| 850 |
/* These fields are not protected by any mutex. */ |
| 851 |
const char *op_info; /*!< English text describing the |
| 852 |
current operation, or an empty |
| 853 |
string */ |
| 854 |
|
| 855 |
/** Current isolation level */ |
| 856 |
isolation_level_t isolation_level; |
| 857 |
|
| 858 |
bool check_foreigns; /*!< normally true, but if the user |
| 859 |
wants to suppress foreign key checks, |
| 860 |
(in table imports, for example) we |
| 861 |
set this false */ |
| 862 |
/*------------------------------*/ |
| 863 |
/* MySQL has a transaction coordinator to coordinate two phase |
| 864 |
commit between multiple storage engines and the binary log. When |
| 865 |
an engine participates in a transaction, it's responsible for |
| 866 |
registering itself using the trans_register_ha() API. */ |
| 867 |
bool is_registered; /* This flag is set to true after the |
| 868 |
transaction has been registered with |
| 869 |
the coordinator using the XA API, and |
| 870 |
is set to false after commit or |
| 871 |
rollback. */ |
| 872 |
/*------------------------------*/ |
| 873 |
bool check_unique_secondary; |
| 874 |
/*!< normally true, but if the user |
| 875 |
wants to speed up inserts by |
| 876 |
suppressing unique key checks |
| 877 |
for secondary indexes when we decide |
| 878 |
if we can use the insert buffer for |
| 879 |
them, we set this false */ |
| 880 |
bool flush_log_later; /* In 2PC, we hold the |
| 881 |
prepare_commit mutex across |
| 882 |
both phases. In that case, we |
| 883 |
defer flush of the logs to disk |
| 884 |
until after we release the |
| 885 |
mutex. */ |
| 886 |
bool must_flush_log_later; /*!< this flag is set to true in |
| 887 |
trx_commit() if flush_log_later was |
| 888 |
true, and there were modifications by |
| 889 |
the transaction; in that case we must |
| 890 |
flush the log in |
| 891 |
trx_commit_complete_for_mysql() */ |
| 892 |
bool has_search_latch; |
| 893 |
/*!< true if this trx has latched the |
| 894 |
search system latch in S-mode. |
| 895 |
This now can only be true in |
| 896 |
row_search_mvcc, the btr search latch |
| 897 |
must has been released before exiting, |
| 898 |
and this flag would be set to false */ |
| 899 |
trx_dict_op_t dict_operation; /**< @see enum trx_dict_op_t */ |
| 900 |
|
| 901 |
bool ddl_operation; /*!< True if this trx involves dd table |
| 902 |
change */ |
| 903 |
bool ddl_must_flush; /*!< True if this trx involves dd table |
| 904 |
change, and must flush */ |
| 905 |
bool in_truncate; /* This trx is doing truncation */ |
| 906 |
|
| 907 |
/* Fields protected by the srv_conc_mutex. */ |
| 908 |
bool declared_to_be_inside_innodb; |
| 909 |
/*!< this is true if we have declared |
| 910 |
this transaction in |
| 911 |
srv_conc_enter_innodb to be inside the |
| 912 |
InnoDB engine */ |
| 913 |
uint32_t n_tickets_to_enter_innodb; |
| 914 |
/*!< this can be > 0 only when |
| 915 |
declared_to_... is true; when we come |
| 916 |
to srv_conc_innodb_enter, if the value |
| 917 |
here is > 0, we decrement this by 1 */ |
| 918 |
uint32_t dict_operation_lock_mode; |
| 919 |
/*!< 0, RW_S_LATCH, or RW_X_LATCH: |
| 920 |
the latch mode trx currently holds |
| 921 |
on dict_operation_lock. Protected |
| 922 |
by dict_operation_lock. */ |
| 923 |
|
| 924 |
/** Time the state last time became TRX_STATE_ACTIVE. */ |
| 925 |
std::atomic<std::chrono::system_clock::time_point> start_time{ |
| 926 |
std::chrono::system_clock::time_point{}}; |
| 927 |
static_assert(decltype(start_time)::is_always_lock_free); |
| 928 |
|
| 929 |
lsn_t commit_lsn; /*!< lsn at the time of the commit */ |
| 930 |
|
| 931 |
/*------------------------------*/ |
| 932 |
THD *mysql_thd; /*!< MySQL thread handle corresponding |
| 933 |
to this trx, or NULL */ |
| 934 |
|
| 935 |
const char *mysql_log_file_name; |
| 936 |
/*!< if MySQL binlog is used, this field |
| 937 |
contains a pointer to the latest file |
| 938 |
name; this is NULL if binlog is not |
| 939 |
used */ |
| 940 |
uint64_t mysql_log_offset; |
| 941 |
/*!< if MySQL binlog is used, this |
| 942 |
field contains the end offset of the |
| 943 |
binlog entry */ |
| 944 |
/*------------------------------*/ |
| 945 |
uint32_t n_mysql_tables_in_use; /*!< number of Innobase tables |
| 946 |
used in the processing of the current |
| 947 |
SQL statement in MySQL */ |
| 948 |
uint32_t mysql_n_tables_locked; |
| 949 |
/*!< how many tables the current SQL |
| 950 |
statement uses, except those |
| 951 |
in consistent read */ |
| 952 |
/*------------------------------*/ |
| 953 |
#ifdef UNIV_DEBUG |
| 954 |
/** True iff in trx_sys->rw_trx_list */ |
| 955 |
bool in_rw_trx_list; |
| 956 |
|
| 957 |
#endif /* UNIV_DEBUG */ |
| 958 |
UT_LIST_NODE_T(trx_t) |
| 959 |
mysql_trx_list; /*!< list of transactions created for |
| 960 |
MySQL; protected by trx_sys->mutex */ |
| 961 |
#ifdef UNIV_DEBUG |
| 962 |
bool in_mysql_trx_list; |
| 963 |
/*!< true if in |
| 964 |
trx_sys->mysql_trx_list */ |
| 965 |
#endif /* UNIV_DEBUG */ |
| 966 |
/*------------------------------*/ |
| 967 |
|
| 968 |
/** DB_SUCCESS if no error, otherwise error number. |
| 969 |
Accessed without any mutex only by the thread doing the transaction or, if it |
| 970 |
is suspended (waiting for a lock), by the thread holding this->mutex which |
| 971 |
has changed trx->lock.wait_lock to nullptr and will wake up the transaction.*/ |
| 972 |
dberr_t error_state; |
| 973 |
|
| 974 |
const dict_index_t *error_index; /*!< if the error number indicates a |
| 975 |
duplicate key error, a pointer to |
| 976 |
the problematic index is stored here */ |
| 977 |
ulint error_key_num; /*!< if the index creation fails to a |
| 978 |
duplicate key error, a mysql key |
| 979 |
number of that index is stored here */ |
| 980 |
sess_t *sess; /*!< session of the trx, NULL if none */ |
| 981 |
que_t *graph; /*!< query currently run in the session, |
| 982 |
or NULL if none; NOTE that the query |
| 983 |
belongs to the session, and it can |
| 984 |
survive over a transaction commit, if |
| 985 |
it is a stored procedure with a COMMIT |
| 986 |
WORK statement, for instance */ |
| 987 |
/*------------------------------*/ |
| 988 |
UT_LIST_BASE_NODE_T_EXTERN(trx_named_savept_t, trx_savepoints) |
| 989 |
trx_savepoints{}; /*!< savepoints set with SAVEPOINT ..., oldest first */ |
| 990 |
/*------------------------------*/ |
| 991 |
UndoMutex undo_mutex; /*!< mutex protecting the fields in this |
| 992 |
section (down to undo_no_arr), EXCEPT |
| 993 |
last_sql_stat_start, which can be |
| 994 |
accessed only when we know that there |
| 995 |
cannot be any activity in the undo |
| 996 |
logs! */ |
| 997 |
undo_no_t undo_no; /*!< next undo log record number to |
| 998 |
assign; since the undo log is |
| 999 |
private for a transaction, this |
| 1000 |
is a simple ascending sequence |
| 1001 |
with no gaps; thus it represents |
| 1002 |
the number of modified/inserted |
| 1003 |
rows in a transaction */ |
| 1004 |
space_id_t undo_rseg_space; |
| 1005 |
/*!< space id where last undo record |
| 1006 |
was written */ |
| 1007 |
trx_savept_t last_sql_stat_start; |
| 1008 |
/*!< undo_no when the last sql statement |
| 1009 |
was started: in case of an error, trx |
| 1010 |
is rolled back down to this undo |
| 1011 |
number; see note at undo_mutex! */ |
| 1012 |
trx_rsegs_t rsegs; /* rollback segments for undo logging */ |
| 1013 |
undo_no_t roll_limit; /*!< least undo number to undo during |
| 1014 |
a partial rollback; 0 otherwise */ |
| 1015 |
#ifdef UNIV_DEBUG |
| 1016 |
bool in_rollback; /*!< true when the transaction is |
| 1017 |
executing a partial or full rollback */ |
| 1018 |
#endif /* UNIV_DEBUG */ |
| 1019 |
ulint pages_undone; /*!< number of undo log pages undone |
| 1020 |
since the last undo log truncation */ |
| 1021 |
/*------------------------------*/ |
| 1022 |
ulint n_autoinc_rows; /*!< no. of AUTO-INC rows required for |
| 1023 |
an SQL statement. This is useful for |
| 1024 |
multi-row INSERTs */ |
| 1025 |
/*------------------------------*/ |
| 1026 |
bool read_only; /*!< true if transaction is flagged |
| 1027 |
as a READ-ONLY transaction. |
| 1028 |
if auto_commit && will_lock == 0 |
| 1029 |
then it will be handled as a |
| 1030 |
AC-NL-RO-SELECT (Auto Commit Non-Locking |
| 1031 |
Read Only Select). A read only |
| 1032 |
transaction will not be assigned an |
| 1033 |
UNDO log. */ |
| 1034 |
bool auto_commit; /*!< true if it is an autocommit */ |
| 1035 |
uint32_t will_lock; /*!< Will acquire some locks. Increment |
| 1036 |
each time we determine that a lock will |
| 1037 |
be acquired by the MySQL layer. */ |
| 1038 |
#ifndef UNIV_HOTBACKUP |
| 1039 |
/*------------------------------*/ |
| 1040 |
fts_trx_t *fts_trx; /*!< FTS information, or NULL if |
| 1041 |
transaction hasn't modified tables |
| 1042 |
with FTS indexes (yet). */ |
| 1043 |
doc_id_t fts_next_doc_id; /* The document id used for updates */ |
| 1044 |
/*------------------------------*/ |
| 1045 |
uint32_t flush_tables; /*!< if "covering" the FLUSH TABLES", |
| 1046 |
count of tables being flushed. */ |
| 1047 |
|
| 1048 |
/*------------------------------*/ |
| 1049 |
bool internal; /*!< true if it is a system/internal |
| 1050 |
transaction background task. Such |
| 1051 |
transactions are always treated as |
| 1052 |
read-write. */ |
| 1053 |
/*------------------------------*/ |
| 1054 |
/** Transaction persists GTID. */ |
| 1055 |
bool persists_gtid; |
| 1056 |
|
| 1057 |
#ifdef UNIV_DEBUG |
| 1058 |
ulint start_line; /*!< Track where it was started from */ |
| 1059 |
const char *start_file; /*!< Filename where it was started */ |
| 1060 |
#endif /* UNIV_DEBUG */ |
| 1061 |
|
| 1062 |
lint n_ref; /*!< Count of references, protected |
| 1063 |
by trx_t::mutex. We can't release the |
| 1064 |
locks nor commit the transaction until |
| 1065 |
this reference is 0. We can change |
| 1066 |
the state to COMMITTED_IN_MEMORY to |
| 1067 |
signify that it is no longer |
| 1068 |
"active". */ |
| 1069 |
|
| 1070 |
/** Version of this instance. It is incremented each time the |
| 1071 |
instance is re-used in trx_start_low(). It is used to track |
| 1072 |
whether a transaction has been restarted since it was tagged |
| 1073 |
for asynchronous rollback. */ |
| 1074 |
std::atomic_uint64_t version; |
| 1075 |
|
| 1076 |
XID *xid; /*!< X/Open XA transaction |
| 1077 |
identification to identify a |
| 1078 |
transaction branch */ |
| 1079 |
trx_mod_tables_t mod_tables; /*!< List of tables that were modified |
| 1080 |
by this transaction */ |
| 1081 |
#endif /* !UNIV_HOTBACKUP */ |
| 1082 |
/*------------------------------*/ |
| 1083 |
bool api_trx; /*!< trx started by InnoDB API */ |
| 1084 |
bool api_auto_commit; /*!< automatic commit */ |
| 1085 |
bool read_write; /*!< if read and write operation */ |
| 1086 |
|
| 1087 |
/** This flag is set for trx_t objects used by the purge sys. We use the flag |
| 1088 |
when validating mysql_trx_list in trx_sys_before_pre_dd_shutdown_validate. |
| 1089 |
Purge threads can have allocated trx_t objects visible in the mysql_trx_list |
| 1090 |
at this point during shutdown, this is acceptable so we need a way to signal |
| 1091 |
this fact. */ |
| 1092 |
bool purge_sys_trx; |
| 1093 |
/*------------------------------*/ |
| 1094 |
char *detailed_error; /*!< detailed error message for last |
| 1095 |
error, or empty. */ |
| 1096 |
Flush_observer *flush_observer; /*!< flush observer */ |
| 1097 |
|
| 1098 |
#ifdef UNIV_DEBUG |
| 1099 |
bool is_dd_trx; /*!< True if the transaction is used for |
| 1100 |
doing Non-locking Read-only Read |
| 1101 |
Committed on DD tables */ |
| 1102 |
#endif /* UNIV_DEBUG */ |
| 1103 |
ulint magic_n; |
| 1104 |
|
| 1105 |
bool is_read_uncommitted() const { |
| 1106 |
return (isolation_level == READ_UNCOMMITTED); |
| 1107 |
} |
| 1108 |
|
| 1109 |
bool releases_gap_locks_at_prepare() const { |
| 1110 |
return isolation_level <= READ_COMMITTED; |
| 1111 |
} |
| 1112 |
|
| 1113 |
bool skip_gap_locks() const { |
| 1114 |
switch (isolation_level) { |
| 1115 |
case READ_UNCOMMITTED: |
| 1116 |
case READ_COMMITTED: |
| 1117 |
return (true); |
| 1118 |
case REPEATABLE_READ: |
| 1119 |
case SERIALIZABLE: |
| 1120 |
return (false); |
| 1121 |
} |
| 1122 |
ut_d(ut_error); |
| 1123 |
ut_o(return (false)); |
| 1124 |
} |
| 1125 |
|
| 1126 |
bool allow_semi_consistent() const { return (skip_gap_locks()); } |
| 1127 |
/** Checks if this transaction releases locks on non matching records due to |
| 1128 |
low isolation level. |
| 1129 |
@return true iff in this transaction's isolation level locks on records which |
| 1130 |
do not match the WHERE clause are released */ |
| 1131 |
bool releases_non_matching_rows() const { return skip_gap_locks(); } |
| 1132 |
}; |
| 1133 |
|
| 1134 |
#ifndef UNIV_HOTBACKUP |
| 1135 |
/** |
| 1136 |
Transactions that aren't started by the MySQL server don't set |
| 1137 |
the trx_t::mysql_thd field. For such transactions we set the lock |
| 1138 |
wait timeout to 0 instead of the user configured value that comes |
| 1139 |
from innodb_lock_wait_timeout via trx_t::mysql_thd. |
| 1140 |
@param t transaction |
| 1141 |
@return lock wait timeout in seconds */ |
| 1142 |
static inline std::chrono::seconds trx_lock_wait_timeout_get(const trx_t *t) { |
| 1143 |
return thd_lock_wait_timeout(t->mysql_thd); |
| 1144 |
} |
| 1145 |
|
| 1146 |
/** |
| 1147 |
Determine if the transaction is a non-locking autocommit select |
| 1148 |
(implied read-only). |
| 1149 |
@param t transaction |
| 1150 |
@return true if non-locking autocommit select transaction. */ |
| 1151 |
static inline bool trx_is_autocommit_non_locking(const trx_t *t) { |
| 1152 |
return t->auto_commit && t->will_lock == 0; |
| 1153 |
} |
| 1154 |
|
| 1155 |
/** Check transaction state */ |
| 1156 |
static inline void check_trx_state(const trx_t *t) { |
| 1157 |
ut_ad(!trx_is_autocommit_non_locking(t)); |
| 1158 |
switch (t->state) { |
| 1159 |
case TRX_STATE_PREPARED: |
| 1160 |
/* fall through */ |
| 1161 |
case TRX_STATE_ACTIVE: |
| 1162 |
case TRX_STATE_COMMITTED_IN_MEMORY: |
| 1163 |
return; |
| 1164 |
case TRX_STATE_NOT_STARTED: |
| 1165 |
case TRX_STATE_FORCED_ROLLBACK: |
| 1166 |
break; |
| 1167 |
} |
| 1168 |
ut_error; |
| 1169 |
} |
| 1170 |
|
| 1171 |
/** |
| 1172 |
Assert that the transaction is in the trx_sys_t::rw_trx_list */ |
| 1173 |
static inline void assert_trx_in_rw_list(const trx_t *t) { |
| 1174 |
ut_ad(!t->read_only); |
| 1175 |
ut_ad(t->in_rw_trx_list == !(t->read_only || !t->rsegs.m_redo.rseg)); |
| 1176 |
check_trx_state(t); |
| 1177 |
} |
| 1178 |
|
| 1179 |
/** Check if transaction is free so that it can be re-initialized. |
| 1180 |
@param t transaction handle */ |
| 1181 |
static inline void assert_trx_is_free(const trx_t *t) { |
| 1182 |
ut_ad(trx_state_eq(t, TRX_STATE_NOT_STARTED) || |
| 1183 |
trx_state_eq(t, TRX_STATE_FORCED_ROLLBACK)); |
| 1184 |
ut_ad(!trx_is_rseg_updated(t)); |
| 1185 |
ut_ad(!MVCC::is_view_active(t->read_view)); |
| 1186 |
ut_ad((t)->lock.wait_thr == nullptr); |
| 1187 |
ut_ad(UT_LIST_GET_LEN((t)->lock.trx_locks) == 0); |
| 1188 |
ut_ad((t)->dict_operation == TRX_DICT_OP_NONE); |
| 1189 |
} |
| 1190 |
|
| 1191 |
/** Check if transaction is in-active so that it can be freed and put back to |
| 1192 |
transaction pool. |
| 1193 |
@param t transaction handle */ |
| 1194 |
static inline void assert_trx_is_inactive(const trx_t *t) { |
| 1195 |
assert_trx_is_free(t); |
| 1196 |
ut_ad(t->dict_operation_lock_mode == 0); |
| 1197 |
} |
| 1198 |
|
| 1199 |
#ifdef UNIV_DEBUG |
| 1200 |
/** Assert that an autocommit non-locking select cannot be in the |
| 1201 |
rw_trx_list and that it is a read-only transaction. |
| 1202 |
The transaction must be in the mysql_trx_list. */ |
| 1203 |
static inline void assert_trx_nonlocking_or_in_list(const trx_t *t) { |
| 1204 |
if (trx_is_autocommit_non_locking(t)) { |
| 1205 |
trx_state_t t_state = t->state; |
| 1206 |
ut_ad(t->read_only); |
| 1207 |
ut_ad(!t->is_recovered); |
| 1208 |
ut_ad(!t->in_rw_trx_list); |
| 1209 |
ut_ad(t->in_mysql_trx_list); |
| 1210 |
ut_ad(t_state == TRX_STATE_NOT_STARTED || |
| 1211 |
t_state == TRX_STATE_FORCED_ROLLBACK || t_state == TRX_STATE_ACTIVE); |
| 1212 |
} else { |
| 1213 |
check_trx_state(t); |
| 1214 |
} |
| 1215 |
} |
| 1216 |
#else /* UNIV_DEBUG */ |
| 1217 |
/** Assert that an autocommit non-locking select cannot be in the |
| 1218 |
rw_trx_list and that it is a read-only transaction. |
| 1219 |
The transaction must be in the mysql_trx_list. */ |
| 1220 |
#define assert_trx_nonlocking_or_in_list(trx) ((void)0) |
| 1221 |
#endif /* UNIV_DEBUG */ |
| 1222 |
|
| 1223 |
/** |
| 1224 |
Determine if the transaction is a non-locking autocommit select |
| 1225 |
with an explicit check for the read-only status. |
| 1226 |
@param t transaction |
| 1227 |
@return true if non-locking autocommit read-only transaction. */ |
| 1228 |
static inline bool trx_is_ac_nl_ro(const trx_t *t) { |
| 1229 |
return t->read_only && trx_is_autocommit_non_locking(t); |
| 1230 |
} |
| 1231 |
|
| 1232 |
/** |
| 1233 |
Increase the reference count. If the transaction is in state |
| 1234 |
TRX_STATE_COMMITTED_IN_MEMORY then the transaction is considered |
| 1235 |
committed and the reference count is not incremented. |
| 1236 |
@param trx Transaction that is being referenced */ |
| 1237 |
static inline void trx_reference(trx_t *trx); |
| 1238 |
|
| 1239 |
/** |
| 1240 |
Release the transaction. Decrease the reference count. |
| 1241 |
@param trx Transaction that is being released */ |
| 1242 |
static inline void trx_release_reference(trx_t *trx); |
| 1243 |
|
| 1244 |
/** |
| 1245 |
Check if the transaction is being referenced. */ |
| 1246 |
static inline bool trx_is_referenced(const trx_t *t) { return t->n_ref > 0; } |
| 1247 |
|
| 1248 |
/** Calculates the "weight" of a transaction. The weight of one transaction |
| 1249 |
is estimated as the number of altered rows + the number of locked rows. |
| 1250 |
@param t transaction |
| 1251 |
@return transaction weight */ |
| 1252 |
static inline uint64_t TRX_WEIGHT(const trx_t *t) { |
| 1253 |
return t->undo_no + UT_LIST_GET_LEN(t->lock.trx_locks); |
| 1254 |
} |
| 1255 |
|
| 1256 |
#ifdef UNIV_DEBUG |
| 1257 |
static inline void trx_start_if_not_started_xa(trx_t *t, bool rw, |
| 1258 |
ut::Location loc) { |
| 1259 |
t->start_line = loc.line; |
| 1260 |
t->start_file = loc.filename; |
| 1261 |
trx_start_if_not_started_xa_low(t, rw); |
| 1262 |
} |
| 1263 |
|
| 1264 |
static inline void trx_start_if_not_started(trx_t *t, bool rw, ut::Location l) { |
| 1265 |
t->start_line = l.line; |
| 1266 |
t->start_file = l.filename; |
| 1267 |
trx_start_if_not_started_low(t, rw); |
| 1268 |
} |
| 1269 |
|
| 1270 |
static inline void trx_start_internal(trx_t *t, ut::Location loc) { |
| 1271 |
t->start_line = loc.line; |
| 1272 |
t->start_file = loc.filename; |
| 1273 |
trx_start_internal_low(t); |
| 1274 |
} |
| 1275 |
|
| 1276 |
static inline void trx_start_internal_read_only(trx_t *t, ut::Location loc) { |
| 1277 |
t->start_line = loc.line; |
| 1278 |
t->start_file = loc.filename; |
| 1279 |
trx_start_internal_read_only_low(t); |
| 1280 |
} |
| 1281 |
#else |
| 1282 |
static inline void trx_start_if_not_started_xa(trx_t *t, bool rw, |
| 1283 |
ut::Location loc) { |
| 1284 |
trx_start_if_not_started_low(t, rw); |
| 1285 |
} |
| 1286 |
|
| 1287 |
static inline void trx_start_internal(trx_t *t, ut::Location loc) { |
| 1288 |
trx_start_internal_low(t); |
| 1289 |
} |
| 1290 |
|
| 1291 |
static inline void trx_start_internal_read_only(trx_t *t, ut::Location loc) { |
| 1292 |
trx_start_internal_read_only_low(t); |
| 1293 |
} |
| 1294 |
|
| 1295 |
static inline void trx_start_if_not_started(trx_t *t, bool rw, ut::Location l) { |
| 1296 |
trx_start_if_not_started_xa_low(t, rw); |
| 1297 |
} |
| 1298 |
#endif /* UNIV_DEBUG */ |
| 1299 |
|
| 1300 |
/* Transaction isolation levels (trx->isolation_level) */ |
| 1301 |
#define TRX_ISO_READ_UNCOMMITTED trx_t::READ_UNCOMMITTED |
| 1302 |
#define TRX_ISO_READ_COMMITTED trx_t::READ_COMMITTED |
| 1303 |
#define TRX_ISO_REPEATABLE_READ trx_t::REPEATABLE_READ |
| 1304 |
#define TRX_ISO_SERIALIZABLE trx_t::SERIALIZABLE |
| 1305 |
|
| 1306 |
/** |
| 1307 |
Check if transaction was started. Note, that after the check |
| 1308 |
situation might have already been changed (and note that holding |
| 1309 |
the trx_sys->mutex does not prevent state transitions for read-only |
| 1310 |
transactions). |
| 1311 |
@param[in] trx Transaction whose state we need to check |
| 1312 |
@return true if transaction is in state started */ |
| 1313 |
inline bool trx_was_started(const trx_t *trx) { |
| 1314 |
const auto trx_state = trx->state.load(std::memory_order_relaxed); |
| 1315 |
return trx_state != TRX_STATE_NOT_STARTED && |
| 1316 |
trx_state != TRX_STATE_FORCED_ROLLBACK; |
| 1317 |
} |
| 1318 |
|
| 1319 |
/** |
| 1320 |
Check if transaction is started. |
| 1321 |
@param[in] trx Transaction whose state we need to check |
| 1322 |
@return true if transaction is in state started */ |
| 1323 |
inline bool trx_is_started(const trx_t *trx) { |
| 1324 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 1325 |
return trx_was_started(trx); |
| 1326 |
} |
| 1327 |
|
| 1328 |
/** Commit node states */ |
| 1329 |
enum commit_node_state { |
| 1330 |
COMMIT_NODE_SEND = 1, /*!< about to send a commit signal to |
| 1331 |
the transaction */ |
| 1332 |
COMMIT_NODE_WAIT /*!< commit signal sent to the transaction, |
| 1333 |
waiting for completion */ |
| 1334 |
}; |
| 1335 |
|
| 1336 |
/** Commit command node in a query graph */ |
| 1337 |
struct commit_node_t { |
| 1338 |
que_common_t common; /*!< node type: QUE_NODE_COMMIT */ |
| 1339 |
enum commit_node_state state; /*!< node execution state */ |
| 1340 |
}; |
| 1341 |
|
| 1342 |
#ifdef UNIV_DEBUG |
| 1343 |
|
| 1344 |
/** Test if trx->mutex is owned by the current thread. */ |
| 1345 |
bool inline trx_mutex_own(const trx_t *trx) { return mutex_own(&trx->mutex); } |
| 1346 |
|
| 1347 |
/** |
| 1348 |
Verifies the invariants and records debug state related to latching rules. |
| 1349 |
Called during trx_mutex_enter before the actual mutex acquisition. |
| 1350 |
@param[in] trx The transaction for which trx_mutex_enter(trx) is |
| 1351 |
called |
| 1352 |
@param[in] allow_another If false, then no further calls to trx_mutex_enter |
| 1353 |
are allowed, until trx_mutex_exit(). |
| 1354 |
If true, then this must be the first trx acquisition |
| 1355 |
and we will allow one more. |
| 1356 |
*/ |
| 1357 |
void trx_before_mutex_enter(const trx_t *trx, bool allow_another); |
| 1358 |
|
| 1359 |
/** |
| 1360 |
Verifies the invariants and records debug state related to latching rules. |
| 1361 |
Called during trx_mutex_exit before the actual mutex release. |
| 1362 |
@param[in] trx The transaction for which trx_mutex_exit(trx) is called |
| 1363 |
*/ |
| 1364 |
void trx_before_mutex_exit(const trx_t *trx); |
| 1365 |
#endif |
| 1366 |
|
| 1367 |
/** |
| 1368 |
Please do not use this low-level macro. |
| 1369 |
Use trx_mutex_enter(t) instead. |
| 1370 |
In rare cases where you need to take two trx->mutex-es, take the first one |
| 1371 |
using trx_mutex_enter_first_of_two(t1), and the second one with |
| 1372 |
trx_mutex(2) |
| 1373 |
*/ |
| 1374 |
#define trx_mutex_enter_low(t, first_of_two) \ |
| 1375 |
do { \ |
| 1376 |
ut_ad(!trx_mutex_own(t)); \ |
| 1377 |
ut_d(trx_before_mutex_enter(t, first_of_two)); \ |
| 1378 |
mutex_enter(&t->mutex); \ |
| 1379 |
} while (0) |
| 1380 |
|
| 1381 |
/** Acquire the trx->mutex (and promise not to request any more). */ |
| 1382 |
#define trx_mutex_enter(t) trx_mutex_enter_low(t, false) |
| 1383 |
|
| 1384 |
/** Acquire the trx->mutex (and indicate we might request one more). */ |
| 1385 |
#define trx_mutex_enter_first_of_two(t) trx_mutex_enter_low(t, true) |
| 1386 |
|
| 1387 |
/** Release the trx->mutex. */ |
| 1388 |
#define trx_mutex_exit(t) \ |
| 1389 |
do { \ |
| 1390 |
ut_ad(trx_mutex_own(t)); \ |
| 1391 |
ut_d(trx_before_mutex_exit(t)); \ |
| 1392 |
mutex_exit(&t->mutex); \ |
| 1393 |
} while (0) |
| 1394 |
|
| 1395 |
/** Track if a transaction is executing inside InnoDB code. It acts |
| 1396 |
like a gate between the Server and InnoDB. */ |
| 1397 |
class TrxInInnoDB { |
| 1398 |
public: |
| 1399 |
/** |
| 1400 |
@param[in,out] trx Transaction entering InnoDB via the handler |
| 1401 |
@param[in] disable true if called from COMMIT/ROLLBACK method */ |
| 1402 |
TrxInInnoDB(trx_t *trx, bool disable = false) : m_trx(trx) { |
| 1403 |
enter(trx, disable); |
| 1404 |
} |
| 1405 |
|
| 1406 |
/** |
| 1407 |
Destructor */ |
| 1408 |
~TrxInInnoDB() { exit(m_trx); } |
| 1409 |
|
| 1410 |
/** |
| 1411 |
@return true if the transaction has been marked for asynchronous |
| 1412 |
rollback */ |
| 1413 |
bool is_aborted() const { return (is_aborted(m_trx)); } |
| 1414 |
|
| 1415 |
/** |
| 1416 |
@return true if the transaction can't be rolled back asynchronously */ |
| 1417 |
bool is_rollback_disabled() const { |
| 1418 |
return ((m_trx->in_innodb & TRX_FORCE_ROLLBACK_DISABLE) > 0); |
| 1419 |
} |
| 1420 |
|
| 1421 |
/** |
| 1422 |
@return true if the transaction has been marked for asynchronous |
| 1423 |
rollback */ |
| 1424 |
static bool is_aborted(const trx_t *trx) { |
| 1425 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 1426 |
|
| 1427 |
const auto trx_state = trx->state.load(std::memory_order_relaxed); |
| 1428 |
|
| 1429 |
if (trx_state == TRX_STATE_NOT_STARTED) { |
| 1430 |
return (false); |
| 1431 |
} |
| 1432 |
|
| 1433 |
ut_ad(srv_read_only_mode || trx->in_depth > 0); |
| 1434 |
ut_ad(srv_read_only_mode || trx->in_innodb > 0); |
| 1435 |
|
| 1436 |
return (trx->abort || trx_state == TRX_STATE_FORCED_ROLLBACK); |
| 1437 |
} |
| 1438 |
|
| 1439 |
/** |
| 1440 |
Start statement requested for transaction. |
| 1441 |
@param[in, out] trx Transaction at the start of a SQL statement */ |
| 1442 |
static void begin_stmt(trx_t *trx) { enter(trx, false); } |
| 1443 |
|
| 1444 |
/** |
| 1445 |
Note an end statement for transaction |
| 1446 |
@param[in, out] trx Transaction at end of a SQL statement */ |
| 1447 |
static void end_stmt(trx_t *trx) { exit(trx); } |
| 1448 |
|
| 1449 |
/** |
| 1450 |
@return true if the rollback is being initiated by the thread that |
| 1451 |
marked the transaction for asynchronous rollback */ |
| 1452 |
static bool is_async_rollback(const trx_t *trx) { |
| 1453 |
return trx->killed_by == std::this_thread::get_id(); |
| 1454 |
} |
| 1455 |
|
| 1456 |
private: |
| 1457 |
/** Note that we have crossed into InnoDB code. |
| 1458 |
@param[in] trx transaction |
| 1459 |
@param[in] disable true if called from COMMIT/ROLLBACK method */ |
| 1460 |
static void enter(trx_t *trx, bool disable) { |
| 1461 |
if (srv_read_only_mode) { |
| 1462 |
return; |
| 1463 |
} |
| 1464 |
|
| 1465 |
ut_ad(!is_async_rollback(trx)); |
| 1466 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 1467 |
|
| 1468 |
/* If it hasn't already been marked for async rollback. |
| 1469 |
and it will be committed/rolled back. */ |
| 1470 |
if (disable) { |
| 1471 |
trx_mutex_enter(trx); |
| 1472 |
if (!is_forced_rollback(trx) && is_started(trx) && |
| 1473 |
!trx_is_autocommit_non_locking(trx)) { |
| 1474 |
ut_ad(trx->killed_by == std::thread::id{}); |
| 1475 |
|
| 1476 |
/* This transaction has crossed the point of |
| 1477 |
no return and cannot be rolled back |
| 1478 |
asynchronously now. It must commit or rollback |
| 1479 |
synchronously. */ |
| 1480 |
|
| 1481 |
trx->in_innodb |= TRX_FORCE_ROLLBACK_DISABLE; |
| 1482 |
} |
| 1483 |
trx_mutex_exit(trx); |
| 1484 |
} |
| 1485 |
|
| 1486 |
/* Avoid excessive mutex acquire/release */ |
| 1487 |
++trx->in_depth; |
| 1488 |
|
| 1489 |
/* If trx->in_depth is greater than 1 then |
| 1490 |
transaction is already in InnoDB. */ |
| 1491 |
if (trx->in_depth > 1) { |
| 1492 |
return; |
| 1493 |
} |
| 1494 |
|
| 1495 |
trx_mutex_enter(trx); |
| 1496 |
|
| 1497 |
wait(trx); |
| 1498 |
|
| 1499 |
ut_ad((trx->in_innodb & TRX_FORCE_ROLLBACK_MASK) == 0); |
| 1500 |
|
| 1501 |
++trx->in_innodb; |
| 1502 |
|
| 1503 |
trx_mutex_exit(trx); |
| 1504 |
} |
| 1505 |
|
| 1506 |
/** |
| 1507 |
Note that we are exiting InnoDB code */ |
| 1508 |
static void exit(trx_t *trx) { |
| 1509 |
if (srv_read_only_mode) { |
| 1510 |
return; |
| 1511 |
} |
| 1512 |
|
| 1513 |
/* Avoid excessive mutex acquire/release */ |
| 1514 |
|
| 1515 |
ut_ad(trx->in_depth > 0); |
| 1516 |
|
| 1517 |
--trx->in_depth; |
| 1518 |
|
| 1519 |
if (trx->in_depth > 0) { |
| 1520 |
return; |
| 1521 |
} |
| 1522 |
|
| 1523 |
trx_mutex_enter(trx); |
| 1524 |
|
| 1525 |
ut_ad((trx->in_innodb & TRX_FORCE_ROLLBACK_MASK) > 0); |
| 1526 |
|
| 1527 |
--trx->in_innodb; |
| 1528 |
|
| 1529 |
trx_mutex_exit(trx); |
| 1530 |
} |
| 1531 |
|
| 1532 |
/* |
| 1533 |
@return true if it is a forced rollback, asynchronously */ |
| 1534 |
static bool is_forced_rollback(const trx_t *trx) { |
| 1535 |
ut_ad(trx_mutex_own(trx)); |
| 1536 |
|
| 1537 |
return ((trx->in_innodb & TRX_FORCE_ROLLBACK)) > 0; |
| 1538 |
} |
| 1539 |
|
| 1540 |
/** |
| 1541 |
Wait for the asynchronous rollback to complete, if it is in progress */ |
| 1542 |
static void wait(const trx_t *trx) { |
| 1543 |
ut_ad(trx_mutex_own(trx)); |
| 1544 |
|
| 1545 |
ulint loop_count = 0; |
| 1546 |
/* start with optimistic sleep time - 20 micro seconds. */ |
| 1547 |
ulint sleep_time = 20; |
| 1548 |
|
| 1549 |
while (is_forced_rollback(trx)) { |
| 1550 |
/* Wait for the async rollback to complete */ |
| 1551 |
|
| 1552 |
trx_mutex_exit(trx); |
| 1553 |
|
| 1554 |
loop_count++; |
| 1555 |
/* If the wait is long, don't hog the cpu. */ |
| 1556 |
if (loop_count < 100) { |
| 1557 |
/* 20 microseconds */ |
| 1558 |
sleep_time = 20; |
| 1559 |
} else if (loop_count < 1000) { |
| 1560 |
/* 1 millisecond */ |
| 1561 |
sleep_time = 1000; |
| 1562 |
} else { |
| 1563 |
/* 100 milliseconds */ |
| 1564 |
sleep_time = 100000; |
| 1565 |
} |
| 1566 |
|
| 1567 |
std::this_thread::sleep_for(std::chrono::microseconds(sleep_time)); |
| 1568 |
|
| 1569 |
trx_mutex_enter(trx); |
| 1570 |
} |
| 1571 |
} |
| 1572 |
|
| 1573 |
private: |
| 1574 |
/** |
| 1575 |
@return true if transaction is started */ |
| 1576 |
static bool is_started(const trx_t *trx) { |
| 1577 |
ut_ad(trx_mutex_own(trx)); |
| 1578 |
|
| 1579 |
return trx_is_started(trx); |
| 1580 |
} |
| 1581 |
|
| 1582 |
/** |
| 1583 |
Transaction instance crossing the handler boundary from the Server. */ |
| 1584 |
trx_t *m_trx; |
| 1585 |
}; |
| 1586 |
|
| 1587 |
/** Check if transaction is internal XA transaction |
| 1588 |
@param[in] trx transaction |
| 1589 |
@return true, iff internal XA transaction. */ |
| 1590 |
bool trx_is_mysql_xa(const trx_t *trx); |
| 1591 |
|
| 1592 |
/** Update transaction binlog file name and position from session THD. |
| 1593 |
@param[in,out] trx current transaction. */ |
| 1594 |
void trx_sys_update_binlog_position(trx_t *trx); |
| 1595 |
|
| 1596 |
/** Checks whether or not the transaction has been marked as prepared in TC. |
| 1597 |
@param[in] trx the transaction |
| 1598 |
@return true if the transaction is marked as prepared in TC, false otherwise. */ |
| 1599 |
bool trx_is_prepared_in_tc(trx_t const *trx); |
| 1600 |
|
| 1601 |
/** Does the 2nd phase of an XA transaction prepare for MySQL. |
| 1602 |
@param[in,out] trx Transaction instance to finish prepare |
| 1603 |
@return DB_SUCCESS or error number */ |
| 1604 |
dberr_t trx_set_prepared_in_tc_for_mysql(trx_t *trx); |
| 1605 |
|
| 1606 |
#include "trx0trx.ic" |
| 1607 |
#endif /* !UNIV_HOTBACKUP */ |
| 1608 |
|
| 1609 |
#endif |