| 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 trx/trx0trx.cc |
| 29 |
The transaction |
| 30 |
|
| 31 |
Created 3/26/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include <sys/types.h> |
| 35 |
#include <time.h> |
| 36 |
#include <algorithm> |
| 37 |
#include <new> |
| 38 |
#include <set> |
| 39 |
|
| 40 |
#include <sql_thd_internal_api.h> |
| 41 |
|
| 42 |
#include "btr0sea.h" |
| 43 |
#include "clone0clone.h" |
| 44 |
#include "current_thd.h" |
| 45 |
#include "dict0dd.h" |
| 46 |
#include "fsp0sysspace.h" |
| 47 |
#include "ha_prototypes.h" |
| 48 |
#include "lock0lock.h" |
| 49 |
#include "log0chkp.h" |
| 50 |
#include "log0write.h" |
| 51 |
#include "os0proc.h" |
| 52 |
#include "que0que.h" |
| 53 |
#include "read0read.h" |
| 54 |
#include "row0mysql.h" |
| 55 |
#include "srv0mon.h" |
| 56 |
#include "srv0srv.h" |
| 57 |
#include "srv0start.h" |
| 58 |
#include "trx0purge.h" |
| 59 |
#include "trx0rec.h" |
| 60 |
#include "trx0roll.h" |
| 61 |
#include "trx0rseg.h" |
| 62 |
#include "trx0trx.h" |
| 63 |
#include "trx0undo.h" |
| 64 |
#include "trx0xa.h" |
| 65 |
#include "usr0sess.h" |
| 66 |
#include "ut0new.h" |
| 67 |
#include "ut0pool.h" |
| 68 |
#include "ut0vec.h" |
| 69 |
|
| 70 |
#include "my_dbug.h" |
| 71 |
#include "mysql/plugin.h" |
| 72 |
#include "sql/clone_handler.h" |
| 73 |
|
| 74 |
static const ulint MAX_DETAILED_ERROR_LEN = 256; |
| 75 |
|
| 76 |
/** Set of table_id */ |
| 77 |
typedef std::set<table_id_t, std::less<table_id_t>, ut::allocator<table_id_t>> |
| 78 |
table_id_set; |
| 79 |
|
| 80 |
/** Map of transactions to affected table_id */ |
| 81 |
typedef std::map<trx_t *, table_id_set, std::less<trx_t *>, |
| 82 |
ut::allocator<std::pair<trx_t *const, table_id_set>>> |
| 83 |
trx_table_map; |
| 84 |
|
| 85 |
/** Map of resurrected transactions to affected table_id */ |
| 86 |
static trx_table_map resurrected_trx_tables; |
| 87 |
|
| 88 |
/* std::vector to store the trx id & table id of tables that needs to be |
| 89 |
* rollbacked. We take SHARED MDL on these tables inside |
| 90 |
* trx_recovery_rollback_thread before letting server accept connections */ |
| 91 |
std::vector<std::pair<trx_id_t, table_id_t>> to_rollback_trx_tables; |
| 92 |
|
| 93 |
/** Dummy session used currently in MySQL interface */ |
| 94 |
sess_t *trx_dummy_sess = nullptr; |
| 95 |
|
| 96 |
/** Constructor */ |
| 97 |
TrxVersion::TrxVersion(trx_t *trx) : m_trx(trx), m_version(trx->version) { |
| 98 |
/* No op */ |
| 99 |
} |
| 100 |
|
| 101 |
/* The following function makes the transaction committed in memory |
| 102 |
and makes its changes to data visible to other transactions. |
| 103 |
In particular it releases implicit and explicit locks held by transaction and |
| 104 |
transitions to the transaction to the TRX_STATE_COMMITTED_IN_MEMORY state. |
| 105 |
NOTE that there is a small discrepancy from the strict formal |
| 106 |
visibility rules here: a human user of the database can see |
| 107 |
modifications made by another transaction T even before the necessary |
| 108 |
log segment has been flushed to the disk. If the database happens to |
| 109 |
crash before the flush, the user has seen modifications from T which |
| 110 |
will never be a committed transaction. However, any transaction T2 |
| 111 |
which sees the modifications of the committing transaction T, and |
| 112 |
which also itself makes modifications to the database, will get an lsn |
| 113 |
larger than the committing transaction T. In the case where the log |
| 114 |
flush fails, and T never gets committed, also T2 will never get |
| 115 |
committed. |
| 116 |
@param[in,out] trx The transaction for which will be committed in |
| 117 |
memory |
| 118 |
@param[in] serialised true if serialisation log was written. Affects the |
| 119 |
list of things we need to clean up during |
| 120 |
trx_erase_lists. |
| 121 |
*/ |
| 122 |
static void trx_release_impl_and_expl_locks(trx_t *trx, bool serialised); |
| 123 |
|
| 124 |
/** Tests the durability settings and flushes logs if needed. |
| 125 |
@param[in,out] trx the transaction to flush the logs for. |
| 126 |
@param[in] lsn the identifier of the transaction to flush. */ |
| 127 |
static void trx_flush_logs(trx_t *trx, lsn_t lsn); |
| 128 |
|
| 129 |
/** Set flush observer for the transaction |
| 130 |
@param[in,out] trx transaction struct |
| 131 |
@param[in] observer flush observer */ |
| 132 |
void trx_set_flush_observer(trx_t *trx, Flush_observer *observer) { |
| 133 |
trx->flush_observer = observer; |
| 134 |
} |
| 135 |
|
| 136 |
/** Set detailed error message for the transaction. |
| 137 |
@param[in] trx Transaction struct |
| 138 |
@param[in] msg Detailed error message */ |
| 139 |
void trx_set_detailed_error(trx_t *trx, const char *msg) { |
| 140 |
ut_strlcpy(trx->detailed_error, msg, MAX_DETAILED_ERROR_LEN); |
| 141 |
} |
| 142 |
|
| 143 |
/** Set detailed error message for the transaction from a file. Note that the |
| 144 |
file is rewinded before reading from it. */ |
| 145 |
void trx_set_detailed_error_from_file( |
| 146 |
trx_t *trx, /*!< in: transaction struct */ |
| 147 |
FILE *file) /*!< in: file to read message from */ |
| 148 |
{ |
| 149 |
os_file_read_string(file, trx->detailed_error, MAX_DETAILED_ERROR_LEN); |
| 150 |
} |
| 151 |
|
| 152 |
/** Initialize transaction object. |
| 153 |
@param trx trx to initialize */ |
| 154 |
static void trx_init(trx_t *trx) { |
| 155 |
/* This is called at the end of commit, do not reset the |
| 156 |
trx_t::state here to NOT_STARTED. The FORCED_ROLLBACK |
| 157 |
status is required for asynchronous handling. */ |
| 158 |
|
| 159 |
trx->id = 0; |
| 160 |
|
| 161 |
trx->no = TRX_ID_MAX; |
| 162 |
|
| 163 |
trx->persists_gtid = false; |
| 164 |
|
| 165 |
trx->skip_lock_inheritance = false; |
| 166 |
|
| 167 |
trx->is_recovered = false; |
| 168 |
|
| 169 |
trx->op_info = ""; |
| 170 |
|
| 171 |
trx->isolation_level = TRX_ISO_REPEATABLE_READ; |
| 172 |
|
| 173 |
trx->check_foreigns = true; |
| 174 |
|
| 175 |
trx->check_unique_secondary = true; |
| 176 |
|
| 177 |
trx->lock.n_rec_locks.store(0); |
| 178 |
|
| 179 |
trx->lock.blocking_trx.store(nullptr); |
| 180 |
|
| 181 |
trx->dict_operation = TRX_DICT_OP_NONE; |
| 182 |
|
| 183 |
trx->ddl_operation = false; |
| 184 |
|
| 185 |
trx->error_state = DB_SUCCESS; |
| 186 |
|
| 187 |
trx->error_key_num = ULINT_UNDEFINED; |
| 188 |
|
| 189 |
trx->undo_no = 0; |
| 190 |
|
| 191 |
trx->rsegs.m_redo.rseg = nullptr; |
| 192 |
|
| 193 |
trx->rsegs.m_noredo.rseg = nullptr; |
| 194 |
|
| 195 |
trx->read_only = false; |
| 196 |
|
| 197 |
trx->auto_commit = false; |
| 198 |
|
| 199 |
trx->will_lock = 0; |
| 200 |
|
| 201 |
trx->lock.inherit_all.store(false); |
| 202 |
|
| 203 |
trx->internal = false; |
| 204 |
|
| 205 |
trx->in_truncate = false; |
| 206 |
#ifdef UNIV_DEBUG |
| 207 |
trx->is_dd_trx = false; |
| 208 |
trx->in_rollback = false; |
| 209 |
trx->lock.in_rollback = false; |
| 210 |
#endif /* UNIV_DEBUG */ |
| 211 |
|
| 212 |
ut_d(trx->start_file = nullptr); |
| 213 |
|
| 214 |
ut_d(trx->start_line = 0); |
| 215 |
|
| 216 |
trx->magic_n = TRX_MAGIC_N; |
| 217 |
|
| 218 |
trx->lock.que_state = TRX_QUE_RUNNING; |
| 219 |
|
| 220 |
trx->last_sql_stat_start.least_undo_no = 0; |
| 221 |
|
| 222 |
ut_ad(!MVCC::is_view_active(trx->read_view)); |
| 223 |
|
| 224 |
trx->lock.rec_cached = 0; |
| 225 |
|
| 226 |
trx->lock.table_cached = 0; |
| 227 |
|
| 228 |
trx->error_index = nullptr; |
| 229 |
|
| 230 |
/* During asynchronous rollback, we should reset forced rollback flag |
| 231 |
only after rollback is complete to avoid race with the thread owning |
| 232 |
the transaction. */ |
| 233 |
|
| 234 |
if (!TrxInInnoDB::is_async_rollback(trx)) { |
| 235 |
trx->killed_by.store(std::thread::id{}); |
| 236 |
|
| 237 |
/* Note: Do not set to 0, the ref count is decremented inside |
| 238 |
the TrxInInnoDB() destructor. We only need to clear the flags. */ |
| 239 |
|
| 240 |
trx->in_innodb &= TRX_FORCE_ROLLBACK_MASK; |
| 241 |
} |
| 242 |
|
| 243 |
trx->flush_observer = nullptr; |
| 244 |
|
| 245 |
++trx->version; |
| 246 |
} |
| 247 |
|
| 248 |
trx_guid_t::trx_guid_t(const trx_t &trx) |
| 249 |
: m_immutable_id(reinterpret_cast<uint64_t>(&trx)), |
| 250 |
m_version(trx.version) {} |
| 251 |
|
| 252 |
/** For managing the life-cycle of the trx_t instance that we get |
| 253 |
from the pool. */ |
| 254 |
struct TrxFactory { |
| 255 |
/** Initializes a transaction object. It must be explicitly started |
| 256 |
with trx_start_if_not_started() before using it. The default isolation |
| 257 |
level is TRX_ISO_REPEATABLE_READ. |
| 258 |
@param trx Transaction instance to initialise */ |
| 259 |
static void init(trx_t *trx) { |
| 260 |
/* Explicitly call the constructor of the already |
| 261 |
allocated object. trx_t objects are allocated by |
| 262 |
ut::zalloc_withkey() in Pool::Pool() which would not call |
| 263 |
the constructors of the trx_t members. */ |
| 264 |
new (trx) trx_t(); |
| 265 |
|
| 266 |
trx_init(trx); |
| 267 |
|
| 268 |
trx->state.store(TRX_STATE_NOT_STARTED, std::memory_order_relaxed); |
| 269 |
|
| 270 |
trx->dict_operation_lock_mode = 0; |
| 271 |
|
| 272 |
ut_a(trx->in_innodb == 0); |
| 273 |
ut_a(trx->in_depth == 0); |
| 274 |
|
| 275 |
trx->xid = ut::new_withkey<xid_t>(UT_NEW_THIS_FILE_PSI_KEY); |
| 276 |
|
| 277 |
trx->detailed_error = reinterpret_cast<char *>( |
| 278 |
ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, MAX_DETAILED_ERROR_LEN)); |
| 279 |
|
| 280 |
trx->lock.lock_heap = |
| 281 |
mem_heap_create(1024, UT_LOCATION_HERE, MEM_HEAP_FOR_LOCK_HEAP); |
| 282 |
|
| 283 |
mutex_create(LATCH_ID_TRX, &trx->mutex); |
| 284 |
mutex_create(LATCH_ID_TRX_UNDO, &trx->undo_mutex); |
| 285 |
|
| 286 |
lock_trx_alloc_locks(trx); |
| 287 |
} |
| 288 |
|
| 289 |
/** Release resources held by the transaction object. |
| 290 |
@param trx the transaction for which to release resources */ |
| 291 |
static void destroy(trx_t *trx) { |
| 292 |
ut_a(trx->magic_n == TRX_MAGIC_N); |
| 293 |
ut_ad(!trx->in_rw_trx_list); |
| 294 |
ut_ad(!trx->in_mysql_trx_list); |
| 295 |
|
| 296 |
ut_a(trx->lock.wait_lock == nullptr); |
| 297 |
ut_a(trx->lock.wait_thr == nullptr); |
| 298 |
ut_a(trx->lock.blocking_trx.load() == nullptr); |
| 299 |
|
| 300 |
ut_a(!trx->has_search_latch); |
| 301 |
|
| 302 |
ut_a(trx->dict_operation_lock_mode == 0); |
| 303 |
|
| 304 |
if (trx->lock.lock_heap != nullptr) { |
| 305 |
mem_heap_free(trx->lock.lock_heap); |
| 306 |
trx->lock.lock_heap = nullptr; |
| 307 |
} |
| 308 |
|
| 309 |
ut_a(UT_LIST_GET_LEN(trx->lock.trx_locks) == 0); |
| 310 |
|
| 311 |
ut::delete_(trx->xid); |
| 312 |
ut::free(trx->detailed_error); |
| 313 |
|
| 314 |
mutex_free(&trx->mutex); |
| 315 |
mutex_free(&trx->undo_mutex); |
| 316 |
|
| 317 |
trx->mod_tables.~trx_mod_tables_t(); |
| 318 |
|
| 319 |
ut_ad(trx->read_view == nullptr); |
| 320 |
|
| 321 |
if (!trx->lock.rec_pool.empty()) { |
| 322 |
/* See lock_trx_alloc_locks() why we only free |
| 323 |
the first element. */ |
| 324 |
|
| 325 |
ut::free(trx->lock.rec_pool[0]); |
| 326 |
} |
| 327 |
|
| 328 |
if (!trx->lock.table_pool.empty()) { |
| 329 |
/* See lock_trx_alloc_locks() why we only free |
| 330 |
the first element. */ |
| 331 |
|
| 332 |
ut::free(trx->lock.table_pool[0]); |
| 333 |
} |
| 334 |
|
| 335 |
trx->lock.rec_pool.~lock_pool_t(); |
| 336 |
|
| 337 |
trx->lock.table_pool.~lock_pool_t(); |
| 338 |
} |
| 339 |
|
| 340 |
/** Enforce any invariants here, this is called before the transaction |
| 341 |
is added to the pool. |
| 342 |
@return true if all OK */ |
| 343 |
static bool debug(const trx_t *trx) { |
| 344 |
ut_a(trx->error_state == DB_SUCCESS); |
| 345 |
|
| 346 |
ut_a(trx->magic_n == TRX_MAGIC_N); |
| 347 |
|
| 348 |
ut_ad(!trx->read_only); |
| 349 |
|
| 350 |
ut_ad(!trx_was_started(trx)); |
| 351 |
|
| 352 |
ut_ad(trx->dict_operation == TRX_DICT_OP_NONE); |
| 353 |
|
| 354 |
ut_ad(trx->mysql_thd == nullptr); |
| 355 |
|
| 356 |
ut_ad(!trx->in_rw_trx_list); |
| 357 |
ut_ad(!trx->in_mysql_trx_list); |
| 358 |
|
| 359 |
ut_a(trx->lock.wait_thr == nullptr); |
| 360 |
ut_a(trx->lock.wait_lock == nullptr); |
| 361 |
ut_a(trx->lock.blocking_trx.load() == nullptr); |
| 362 |
|
| 363 |
ut_a(!trx->has_search_latch); |
| 364 |
|
| 365 |
ut_a(trx->dict_operation_lock_mode == 0); |
| 366 |
|
| 367 |
ut_a(UT_LIST_GET_LEN(trx->lock.trx_locks) == 0); |
| 368 |
|
| 369 |
ut_ad(trx->lock.autoinc_locks == nullptr); |
| 370 |
|
| 371 |
ut_ad(!trx->lock.inherit_all.load()); |
| 372 |
|
| 373 |
ut_ad(!trx->abort); |
| 374 |
|
| 375 |
ut_ad(trx->killed_by == std::thread::id{}); |
| 376 |
|
| 377 |
return (true); |
| 378 |
} |
| 379 |
}; |
| 380 |
|
| 381 |
/** The lock strategy for TrxPool */ |
| 382 |
struct TrxPoolLock { |
| 383 |
TrxPoolLock() = default; |
| 384 |
|
| 385 |
/** Create the mutex */ |
| 386 |
void create() { mutex_create(LATCH_ID_TRX_POOL, &m_mutex); } |
| 387 |
|
| 388 |
/** Acquire the mutex */ |
| 389 |
void enter() { mutex_enter(&m_mutex); } |
| 390 |
|
| 391 |
/** Release the mutex */ |
| 392 |
void exit() { mutex_exit(&m_mutex); } |
| 393 |
|
| 394 |
/** Free the mutex */ |
| 395 |
void destroy() { mutex_free(&m_mutex); } |
| 396 |
|
| 397 |
/** Mutex to use */ |
| 398 |
ib_mutex_t m_mutex; |
| 399 |
}; |
| 400 |
|
| 401 |
/** The lock strategy for the TrxPoolManager */ |
| 402 |
struct TrxPoolManagerLock { |
| 403 |
TrxPoolManagerLock() = default; |
| 404 |
|
| 405 |
/** Create the mutex */ |
| 406 |
void create() { mutex_create(LATCH_ID_TRX_POOL_MANAGER, &m_mutex); } |
| 407 |
|
| 408 |
/** Acquire the mutex */ |
| 409 |
void enter() { mutex_enter(&m_mutex); } |
| 410 |
|
| 411 |
/** Release the mutex */ |
| 412 |
void exit() { mutex_exit(&m_mutex); } |
| 413 |
|
| 414 |
/** Free the mutex */ |
| 415 |
void destroy() { mutex_free(&m_mutex); } |
| 416 |
|
| 417 |
/** Mutex to use */ |
| 418 |
ib_mutex_t m_mutex; |
| 419 |
}; |
| 420 |
|
| 421 |
/** Use explicit mutexes for the trx_t pool and its manager. */ |
| 422 |
typedef Pool<trx_t, TrxFactory, TrxPoolLock> trx_pool_t; |
| 423 |
typedef PoolManager<trx_pool_t, TrxPoolManagerLock> trx_pools_t; |
| 424 |
|
| 425 |
/** The trx_t pool manager */ |
| 426 |
static trx_pools_t *trx_pools; |
| 427 |
|
| 428 |
/** Size of on trx_t pool in bytes. */ |
| 429 |
static const ulint MAX_TRX_BLOCK_SIZE = 1024 * 1024 * 4; |
| 430 |
|
| 431 |
/** Create the trx_t pool */ |
| 432 |
void trx_pool_init() { |
| 433 |
trx_pools = ut::new_withkey<trx_pools_t>(UT_NEW_THIS_FILE_PSI_KEY, |
| 434 |
MAX_TRX_BLOCK_SIZE); |
| 435 |
|
| 436 |
ut_a(trx_pools != nullptr); |
| 437 |
} |
| 438 |
|
| 439 |
/** Destroy the trx_t pool */ |
| 440 |
void trx_pool_close() { |
| 441 |
ut::delete_(trx_pools); |
| 442 |
|
| 443 |
trx_pools = nullptr; |
| 444 |
} |
| 445 |
|
| 446 |
/** @return a trx_t instance from trx_pools. */ |
| 447 |
static trx_t *trx_create_low() { |
| 448 |
trx_t *trx = trx_pools->get(); |
| 449 |
|
| 450 |
assert_trx_is_free(trx); |
| 451 |
|
| 452 |
mem_heap_t *heap; |
| 453 |
ib_alloc_t *alloc; |
| 454 |
|
| 455 |
/* We just got trx from pool, it should be non locking */ |
| 456 |
ut_ad(trx->will_lock == 0); |
| 457 |
|
| 458 |
trx->persists_gtid = false; |
| 459 |
|
| 460 |
trx->api_trx = false; |
| 461 |
|
| 462 |
trx->api_auto_commit = false; |
| 463 |
|
| 464 |
trx->read_write = true; |
| 465 |
|
| 466 |
trx->purge_sys_trx = false; |
| 467 |
|
| 468 |
/* Background trx should not be forced to rollback, |
| 469 |
we will unset the flag for user trx. */ |
| 470 |
trx->in_innodb |= TRX_FORCE_ROLLBACK_DISABLE; |
| 471 |
|
| 472 |
/* Trx state can be TRX_STATE_FORCED_ROLLBACK if |
| 473 |
the trx was forced to rollback before it's reused.*/ |
| 474 |
trx->state.store(TRX_STATE_NOT_STARTED, std::memory_order_relaxed); |
| 475 |
|
| 476 |
heap = mem_heap_create(sizeof(ib_vector_t) + sizeof(void *) * 8, |
| 477 |
UT_LOCATION_HERE); |
| 478 |
|
| 479 |
alloc = ib_heap_allocator_create(heap); |
| 480 |
|
| 481 |
/* Remember to free the vector explicitly in trx_free(). */ |
| 482 |
trx->lock.autoinc_locks = ib_vector_create(alloc, sizeof(void **), 4); |
| 483 |
|
| 484 |
/* Should have been either just initialized or .clear()ed by |
| 485 |
trx_free(). */ |
| 486 |
ut_a(trx->mod_tables.size() == 0); |
| 487 |
|
| 488 |
return (trx); |
| 489 |
} |
| 490 |
|
| 491 |
/** |
| 492 |
Release a trx_t instance back to the pool. |
| 493 |
@param trx the instance to release. */ |
| 494 |
static void trx_free(trx_t *&trx) { |
| 495 |
assert_trx_is_free(trx); |
| 496 |
|
| 497 |
trx->mysql_thd = nullptr; |
| 498 |
|
| 499 |
// FIXME: We need to avoid this heap free/alloc for each commit. |
| 500 |
if (trx->lock.autoinc_locks != nullptr) { |
| 501 |
ut_ad(ib_vector_is_empty(trx->lock.autoinc_locks)); |
| 502 |
/* We allocated a dedicated heap for the vector. */ |
| 503 |
ib_vector_free(trx->lock.autoinc_locks); |
| 504 |
trx->lock.autoinc_locks = nullptr; |
| 505 |
} |
| 506 |
|
| 507 |
trx->mod_tables.clear(); |
| 508 |
|
| 509 |
ut_ad(trx->read_view == nullptr); |
| 510 |
ut_ad(trx->is_dd_trx == false); |
| 511 |
ut_ad(trx->in_depth == 0); |
| 512 |
|
| 513 |
/* trx locking state should have been reset before returning trx |
| 514 |
to pool */ |
| 515 |
ut_ad(trx->will_lock == 0); |
| 516 |
trx->in_innodb &= TRX_FORCE_ROLLBACK_MASK; |
| 517 |
trx_pools->mem_free(trx); |
| 518 |
|
| 519 |
trx = nullptr; |
| 520 |
} |
| 521 |
|
| 522 |
/** Creates a transaction object for background operations by the master thread. |
| 523 |
@return own: transaction object */ |
| 524 |
trx_t *trx_allocate_for_background(void) { |
| 525 |
trx_t *trx; |
| 526 |
|
| 527 |
trx = trx_create_low(); |
| 528 |
|
| 529 |
trx->sess = trx_dummy_sess; |
| 530 |
|
| 531 |
return (trx); |
| 532 |
} |
| 533 |
|
| 534 |
/** Creates a transaction object for MySQL. |
| 535 |
@return own: transaction object */ |
| 536 |
trx_t *trx_allocate_for_mysql(void) { |
| 537 |
trx_t *trx; |
| 538 |
|
| 539 |
trx = trx_allocate_for_background(); |
| 540 |
|
| 541 |
trx_sys_mutex_enter(); |
| 542 |
|
| 543 |
ut_d(trx->in_mysql_trx_list = true); |
| 544 |
UT_LIST_ADD_FIRST(trx_sys->mysql_trx_list, trx); |
| 545 |
|
| 546 |
trx_sys_mutex_exit(); |
| 547 |
|
| 548 |
return (trx); |
| 549 |
} |
| 550 |
|
| 551 |
/** Check state of transaction before freeing it. |
| 552 |
@param[in,out] trx transaction object to validate */ |
| 553 |
static void trx_validate_state_before_free(trx_t *trx) { |
| 554 |
if (trx->declared_to_be_inside_innodb) { |
| 555 |
ib::error(ER_IB_MSG_1202) |
| 556 |
<< "Freeing a trx (" << trx << ", " << trx_get_id_for_print(trx) |
| 557 |
<< ") which is declared" |
| 558 |
" to be processing inside InnoDB"; |
| 559 |
|
| 560 |
trx_print(stderr, trx, 600); |
| 561 |
putc('\n', stderr); |
| 562 |
|
| 563 |
/* This is an error but not a fatal error. We must keep |
| 564 |
the counters like srv_conc_n_threads accurate. */ |
| 565 |
srv_conc_force_exit_innodb(trx); |
| 566 |
} |
| 567 |
|
| 568 |
if (trx->n_mysql_tables_in_use != 0 || trx->mysql_n_tables_locked != 0) { |
| 569 |
ib::error(ER_IB_MSG_1203) |
| 570 |
<< "MySQL is freeing a thd though trx->n_mysql_tables_in_use is " |
| 571 |
<< trx->n_mysql_tables_in_use << " and trx->mysql_n_tables_locked is " |
| 572 |
<< trx->mysql_n_tables_locked << "."; |
| 573 |
|
| 574 |
trx_print(stderr, trx, 600); |
| 575 |
ut_print_buf(stderr, trx, sizeof(trx_t)); |
| 576 |
putc('\n', stderr); |
| 577 |
} |
| 578 |
|
| 579 |
trx->dict_operation = TRX_DICT_OP_NONE; |
| 580 |
assert_trx_is_inactive(trx); |
| 581 |
} |
| 582 |
|
| 583 |
/** Free and initialize a transaction object instantiated during recovery. |
| 584 |
@param[in,out] trx transaction object to free and initialize */ |
| 585 |
void trx_free_resurrected(trx_t *trx) { |
| 586 |
trx_validate_state_before_free(trx); |
| 587 |
|
| 588 |
trx_init(trx); |
| 589 |
|
| 590 |
trx_free(trx); |
| 591 |
} |
| 592 |
|
| 593 |
/** Free a transaction that was allocated by background or user threads. |
| 594 |
@param[in,out] trx transaction object to free */ |
| 595 |
void trx_free_for_background(trx_t *trx) { |
| 596 |
trx_validate_state_before_free(trx); |
| 597 |
|
| 598 |
trx_free(trx); |
| 599 |
} |
| 600 |
|
| 601 |
void trx_free_prepared_or_active_recovered(trx_t *trx) { |
| 602 |
ut_a(trx->magic_n == TRX_MAGIC_N); |
| 603 |
|
| 604 |
bool was_prepared{false}; |
| 605 |
if (trx->state.load(std::memory_order_relaxed) == TRX_STATE_ACTIVE) { |
| 606 |
ut_a(trx_state_eq(trx, TRX_STATE_ACTIVE)); |
| 607 |
ut_a(trx->is_recovered); |
| 608 |
} else { |
| 609 |
ut_a(trx_state_eq(trx, TRX_STATE_PREPARED)); |
| 610 |
was_prepared = true; |
| 611 |
} |
| 612 |
/* A PREPARED transaction which got disconnected often has nonzero will_lock, |
| 613 |
yet trx_free() expects it to be cleared. We clear it at the latest possible |
| 614 |
moment, instead of doing it immediately on disconnect, because this field is |
| 615 |
used to check if this transaction is "non-locking" in various functions which |
| 616 |
might be called either here, or when another client reconnects to XA COMMIT or |
| 617 |
XA ROLLBACK. Usually the field is cleared during rollback or commit, here we |
| 618 |
have to do it ourselves as we neither rollback nor commit, just "free" it. */ |
| 619 |
ut_ad(!trx->will_lock || trx_state_eq(trx, TRX_STATE_PREPARED)); |
| 620 |
assert_trx_in_rw_list(trx); |
| 621 |
|
| 622 |
trx_release_impl_and_expl_locks(trx, false); |
| 623 |
trx_undo_free_trx_with_prepared_or_active_logs(trx, was_prepared); |
| 624 |
|
| 625 |
ut_ad(!trx->in_rw_trx_list); |
| 626 |
ut_a(!trx->read_only); |
| 627 |
|
| 628 |
trx->state.store(TRX_STATE_NOT_STARTED, std::memory_order_relaxed); |
| 629 |
trx->will_lock = 0; |
| 630 |
|
| 631 |
trx_validate_state_before_free(trx); |
| 632 |
trx_free(trx); |
| 633 |
} |
| 634 |
|
| 635 |
/** Disconnect a transaction from MySQL. |
| 636 |
@param[in,out] trx transaction |
| 637 |
@param[in] prepared boolean value to specify whether trx is in |
| 638 |
TRX_STATE_PREPARED state (such as after XA |
| 639 |
PREPARE) and we want to unlink it from the |
| 640 |
mysql_thd object, so it can potentially be |
| 641 |
linked to another session in future. |
| 642 |
*/ |
| 643 |
inline void trx_disconnect_from_mysql(trx_t *trx, bool prepared) { |
| 644 |
trx_sys_mutex_enter(); |
| 645 |
|
| 646 |
ut_ad(trx->in_mysql_trx_list); |
| 647 |
ut_d(trx->in_mysql_trx_list = false); |
| 648 |
|
| 649 |
UT_LIST_REMOVE(trx_sys->mysql_trx_list, trx); |
| 650 |
|
| 651 |
if (trx->read_view != nullptr) { |
| 652 |
trx_sys->mvcc->view_close(trx->read_view, true); |
| 653 |
} |
| 654 |
|
| 655 |
ut_ad(trx_sys_validate_trx_list()); |
| 656 |
|
| 657 |
if (prepared) { |
| 658 |
ut_ad(trx_state_eq(trx, TRX_STATE_PREPARED)); |
| 659 |
|
| 660 |
/* During disconnection there is a short period when the server layer |
| 661 |
already believes this XID is detached from this connection, and thus another |
| 662 |
connection may try to XA COMMIT/ROLLBACK it, yet InnoDB is still processing |
| 663 |
the trx object - another client can be executing innobase_commit_by_xid() in |
| 664 |
parallel to our trx_disconnect_from_mysql(). We use trx->mysql_thd field, |
| 665 |
under protection of trx_sys->mutex to synchronize with trx_get_trx_by_xid() |
| 666 |
and convey if this trx is still attached to this thd or not. */ |
| 667 |
ut_ad(trx->mysql_thd != nullptr); |
| 668 |
trx->mysql_thd = nullptr; |
| 669 |
} |
| 670 |
|
| 671 |
trx_sys_mutex_exit(); |
| 672 |
} |
| 673 |
|
| 674 |
/** Disconnect a transaction from MySQL. |
| 675 |
@param[in,out] trx transaction */ |
| 676 |
inline void trx_disconnect_plain(trx_t *trx) { |
| 677 |
trx_disconnect_from_mysql(trx, false); |
| 678 |
} |
| 679 |
|
| 680 |
/** Disconnect a prepared transaction from MySQL. |
| 681 |
@param[in,out] trx transaction */ |
| 682 |
void trx_disconnect_prepared(trx_t *trx) { |
| 683 |
trx_disconnect_from_mysql(trx, true); |
| 684 |
} |
| 685 |
|
| 686 |
/** Free a transaction object for MySQL. |
| 687 |
@param[in,out] trx transaction */ |
| 688 |
void trx_free_for_mysql(trx_t *trx) { |
| 689 |
trx_disconnect_plain(trx); |
| 690 |
trx_free_for_background(trx); |
| 691 |
} |
| 692 |
|
| 693 |
/** Resurrect the table IDs for a resurrected transaction. |
| 694 |
@param[in] trx resurrected transaction |
| 695 |
@param[in] undo_ptr pointer to undo segment |
| 696 |
@param[in] undo undo log */ |
| 697 |
static void trx_resurrect_table_ids(trx_t *trx, const trx_undo_ptr_t *undo_ptr, |
| 698 |
const trx_undo_t *undo) { |
| 699 |
mtr_t mtr; |
| 700 |
page_t *undo_page; |
| 701 |
trx_undo_rec_t *undo_rec; |
| 702 |
|
| 703 |
ut_ad(undo == undo_ptr->insert_undo || undo == undo_ptr->update_undo); |
| 704 |
|
| 705 |
if (trx_state_eq(trx, TRX_STATE_COMMITTED_IN_MEMORY) || undo->empty) { |
| 706 |
return; |
| 707 |
} |
| 708 |
|
| 709 |
table_id_set empty; |
| 710 |
table_id_set &tables = |
| 711 |
resurrected_trx_tables.insert(trx_table_map::value_type(trx, empty)) |
| 712 |
.first->second; |
| 713 |
|
| 714 |
mtr_start(&mtr); |
| 715 |
|
| 716 |
/* trx_rseg_mem_create() may have acquired an X-latch on this |
| 717 |
page, so we cannot acquire an S-latch. */ |
| 718 |
undo_page = trx_undo_page_get(page_id_t(undo->space, undo->top_page_no), |
| 719 |
undo->page_size, &mtr); |
| 720 |
|
| 721 |
undo_rec = undo_page + undo->top_offset; |
| 722 |
ulong n_undo_recs = 0; |
| 723 |
ulong n_undo_pages = 0; |
| 724 |
ulong n_tables = 0; |
| 725 |
|
| 726 |
auto last_progress_log_time = std::chrono::steady_clock::now(); |
| 727 |
using namespace std::chrono_literals; |
| 728 |
// Since resurrecting a transaction can take a long time, progress is logged |
| 729 |
// at regular intervals to the error log. The debug value is provided for |
| 730 |
// testing |
| 731 |
auto const progress_log_interval = |
| 732 |
DBUG_EVALUATE_IF("resurrect_logs", 1s, 30s); |
| 733 |
|
| 734 |
do { |
| 735 |
ulint type; |
| 736 |
undo_no_t undo_no; |
| 737 |
table_id_t table_id; |
| 738 |
ulint cmpl_info; |
| 739 |
bool updated_extern; |
| 740 |
type_cmpl_t type_cmpl; |
| 741 |
page_t *undo_rec_page = page_align(undo_rec); |
| 742 |
|
| 743 |
if (undo_rec_page != undo_page) { |
| 744 |
n_undo_pages++; |
| 745 |
mtr.release_page(undo_page, MTR_MEMO_PAGE_X_FIX); |
| 746 |
undo_page = undo_rec_page; |
| 747 |
} |
| 748 |
|
| 749 |
trx_undo_rec_get_pars(undo_rec, &type, &cmpl_info, &updated_extern, |
| 750 |
&undo_no, &table_id, type_cmpl); |
| 751 |
if (tables.insert(table_id).second == true) { |
| 752 |
n_tables++; |
| 753 |
ib::info(ER_IB_RESURRECT_IDENTIFY_TABLE_TO_LOCK, ulong(table_id)); |
| 754 |
} |
| 755 |
|
| 756 |
undo_rec = trx_undo_get_prev_rec(undo_rec, undo->hdr_page_no, |
| 757 |
undo->hdr_offset, false, &mtr); |
| 758 |
n_undo_recs++; |
| 759 |
|
| 760 |
auto now = std::chrono::steady_clock::now(); |
| 761 |
auto time_diff = now - last_progress_log_time; |
| 762 |
|
| 763 |
if (time_diff >= progress_log_interval) { |
| 764 |
ib::info(ER_IB_RESURRECT_RECORD_PROGRESS, n_undo_recs, n_undo_pages); |
| 765 |
last_progress_log_time = now; |
| 766 |
} |
| 767 |
} while (undo_rec); |
| 768 |
|
| 769 |
ib::info(ER_IB_RESURRECT_RECORD_COMPLETE, n_undo_recs, n_undo_pages, |
| 770 |
n_tables); |
| 771 |
|
| 772 |
mtr_commit(&mtr); |
| 773 |
} |
| 774 |
|
| 775 |
void trx_resurrect_locks(bool all) { |
| 776 |
for (const auto &element : resurrected_trx_tables) { |
| 777 |
trx_t *trx = element.first; |
| 778 |
|
| 779 |
/* We deal only with recovered transactions. If all is false, |
| 780 |
we skip non dictionary transactions. */ |
| 781 |
if (!trx->is_recovered || (!all && !trx->ddl_operation)) { |
| 782 |
continue; |
| 783 |
} |
| 784 |
|
| 785 |
const bool is_prepared = trx_state_eq(trx, TRX_STATE_PREPARED); |
| 786 |
|
| 787 |
const table_id_set &tables = element.second; |
| 788 |
|
| 789 |
for (auto table_id : tables) { |
| 790 |
auto table = dd_table_open_on_id(table_id, nullptr, nullptr, false, true); |
| 791 |
|
| 792 |
if (table == nullptr) { |
| 793 |
continue; |
| 794 |
} |
| 795 |
|
| 796 |
ut_ad(!table->is_temporary()); |
| 797 |
|
| 798 |
if (table->ibd_file_missing || table->is_temporary()) { |
| 799 |
dict_sys_mutex_enter(); |
| 800 |
dd_table_close(table, nullptr, nullptr, true); |
| 801 |
dict_table_remove_from_cache(table); |
| 802 |
dict_sys_mutex_exit(); |
| 803 |
continue; |
| 804 |
} |
| 805 |
|
| 806 |
if (is_prepared && !dict_table_is_sdi(table->id)) { |
| 807 |
trx->mod_tables.insert(table); |
| 808 |
} |
| 809 |
DICT_TF2_FLAG_SET(table, DICT_TF2_RESURRECT_PREPARED); |
| 810 |
|
| 811 |
/* We don't rollback DDL or XA prepared transaction in background */ |
| 812 |
if (trx->ddl_operation || is_prepared) { |
| 813 |
lock_table_ix_resurrect(table, trx); |
| 814 |
ib::info(ER_IB_RESURRECT_ACQUIRE_TABLE_LOCK, ulong(table->id), |
| 815 |
table->name.m_name); |
| 816 |
} else { |
| 817 |
/* MDL & IX_LOCK is taken inside trx_recovery_rollback_thread */ |
| 818 |
to_rollback_trx_tables.push_back(std::make_pair(trx->id, table_id)); |
| 819 |
} |
| 820 |
|
| 821 |
DBUG_PRINT("ib_trx", ("resurrect" TRX_ID_FMT " table '%s' IX lock", |
| 822 |
trx_get_id_for_print(trx), table->name.m_name)); |
| 823 |
|
| 824 |
dd_table_close(table, nullptr, nullptr, false); |
| 825 |
} |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
void trx_clear_resurrected_table_ids() { resurrected_trx_tables.clear(); } |
| 830 |
|
| 831 |
/** Resurrect the transactions that were doing inserts at the time of the |
| 832 |
crash, they need to be undone. |
| 833 |
@return trx_t instance */ |
| 834 |
static trx_t *trx_resurrect_insert( |
| 835 |
trx_undo_t *undo, /*!< in: entry to UNDO */ |
| 836 |
trx_rseg_t *rseg) /*!< in: rollback segment */ |
| 837 |
{ |
| 838 |
trx_t *trx; |
| 839 |
|
| 840 |
trx = trx_allocate_for_background(); |
| 841 |
|
| 842 |
ut_d(trx->start_file = __FILE__); |
| 843 |
ut_d(trx->start_line = __LINE__); |
| 844 |
|
| 845 |
rseg->trx_ref_count++; |
| 846 |
trx->rsegs.m_redo.rseg = rseg; |
| 847 |
*trx->xid = undo->xid; |
| 848 |
trx->id = undo->trx_id; |
| 849 |
trx_sys_rw_trx_add(trx); |
| 850 |
trx->rsegs.m_redo.insert_undo = undo; |
| 851 |
trx->is_recovered = true; |
| 852 |
|
| 853 |
/* This is single-threaded startup code, we do not need the |
| 854 |
protection of trx->mutex or trx_sys->mutex here. */ |
| 855 |
|
| 856 |
if (undo->state != TRX_UNDO_ACTIVE) { |
| 857 |
/* Prepared transactions are left in the prepared state |
| 858 |
waiting for a commit or abort decision from MySQL */ |
| 859 |
|
| 860 |
if (undo->is_prepared()) { |
| 861 |
ib::info(ER_IB_MSG_1204) << "Transaction " << trx_get_id_for_print(trx) |
| 862 |
<< " was in the XA prepared state."; |
| 863 |
|
| 864 |
if (srv_force_recovery == 0) { |
| 865 |
trx->state.store(TRX_STATE_PREPARED, std::memory_order_relaxed); |
| 866 |
++trx_sys->n_prepared_trx; |
| 867 |
} else { |
| 868 |
ib::info(ER_IB_MSG_1205) << "Since innodb_force_recovery" |
| 869 |
" > 0, we will force a rollback."; |
| 870 |
|
| 871 |
trx->state.store(TRX_STATE_ACTIVE, std::memory_order_relaxed); |
| 872 |
} |
| 873 |
} else { |
| 874 |
trx->state.store(TRX_STATE_COMMITTED_IN_MEMORY, |
| 875 |
std::memory_order_relaxed); |
| 876 |
} |
| 877 |
|
| 878 |
/* We give a dummy value for the trx no; this should have no |
| 879 |
relevance since purge is not interested in committed |
| 880 |
transaction numbers, unless they are in the history |
| 881 |
list, in which case it looks the number from the disk based |
| 882 |
undo log structure */ |
| 883 |
|
| 884 |
trx->no = trx->id; |
| 885 |
|
| 886 |
} else { |
| 887 |
trx->state.store(TRX_STATE_ACTIVE, std::memory_order_relaxed); |
| 888 |
|
| 889 |
/* A running transaction always has the number |
| 890 |
field inited to TRX_ID_MAX */ |
| 891 |
|
| 892 |
trx->no = TRX_ID_MAX; |
| 893 |
} |
| 894 |
|
| 895 |
/* trx_start_low() is not called with resurrect, so need to initialize |
| 896 |
start time here.*/ |
| 897 |
if (trx->state.load(std::memory_order_relaxed) == TRX_STATE_ACTIVE || |
| 898 |
trx->state.load(std::memory_order_relaxed) == TRX_STATE_PREPARED) { |
| 899 |
trx->start_time.store(std::chrono::system_clock::from_time_t(time(nullptr)), |
| 900 |
std::memory_order_relaxed); |
| 901 |
} |
| 902 |
|
| 903 |
trx->ddl_operation = undo->dict_operation; |
| 904 |
|
| 905 |
if (undo->dict_operation) { |
| 906 |
trx_set_dict_operation(trx, TRX_DICT_OP_TABLE); |
| 907 |
} |
| 908 |
|
| 909 |
if (!undo->empty) { |
| 910 |
trx->undo_no = undo->top_undo_no + 1; |
| 911 |
trx->undo_rseg_space = undo->rseg->space_id; |
| 912 |
} |
| 913 |
|
| 914 |
return (trx); |
| 915 |
} |
| 916 |
|
| 917 |
/** Prepared transactions are left in the prepared state waiting for a |
| 918 |
commit or abort decision from MySQL */ |
| 919 |
static void trx_resurrect_update_in_prepared_state( |
| 920 |
trx_t *trx, /*!< in,out: transaction */ |
| 921 |
const trx_undo_t *undo) /*!< in: update UNDO record */ |
| 922 |
{ |
| 923 |
/* This is single-threaded startup code, we do not need the |
| 924 |
protection of trx->mutex or trx_sys->mutex here. */ |
| 925 |
|
| 926 |
if (undo->is_prepared()) { |
| 927 |
ib::info(ER_IB_MSG_1206) << "Transaction " << trx_get_id_for_print(trx) |
| 928 |
<< " was in the XA prepared state."; |
| 929 |
|
| 930 |
ut_ad(trx->state.load(std::memory_order_relaxed) != |
| 931 |
TRX_STATE_FORCED_ROLLBACK); |
| 932 |
|
| 933 |
if (trx_state_eq(trx, TRX_STATE_NOT_STARTED)) { |
| 934 |
++trx_sys->n_prepared_trx; |
| 935 |
} else { |
| 936 |
ut_ad(trx_state_eq(trx, TRX_STATE_PREPARED)); |
| 937 |
} |
| 938 |
|
| 939 |
trx->state.store(TRX_STATE_PREPARED, std::memory_order_relaxed); |
| 940 |
} else { |
| 941 |
trx->state.store(TRX_STATE_COMMITTED_IN_MEMORY, std::memory_order_relaxed); |
| 942 |
} |
| 943 |
} |
| 944 |
|
| 945 |
/** Resurrect the transactions that were doing updates the time of the |
| 946 |
crash, they need to be undone. */ |
| 947 |
static void trx_resurrect_update( |
| 948 |
trx_t *trx, /*!< in/out: transaction */ |
| 949 |
trx_undo_t *undo, /*!< in/out: update UNDO record */ |
| 950 |
trx_rseg_t *rseg) /*!< in/out: rollback segment */ |
| 951 |
{ |
| 952 |
/* This resurected transaction might also have been doing inserts. |
| 953 |
If so, this rseg is already assigned by trx_resurrect_insert(). */ |
| 954 |
if (trx->rsegs.m_redo.rseg != nullptr) { |
| 955 |
ut_a(trx->rsegs.m_redo.rseg == rseg); |
| 956 |
ut_ad(trx->id == undo->trx_id); |
| 957 |
ut_ad(trx->is_recovered); |
| 958 |
/* For GTID persistence, we might have empty update undo for |
| 959 |
insert only transactions. */ |
| 960 |
if (undo->empty && trx_state_eq(trx, TRX_STATE_PREPARED)) { |
| 961 |
undo->set_prepared(trx->xid); |
| 962 |
} |
| 963 |
ut_ad(undo->xid.eq(trx->xid)); |
| 964 |
} else { |
| 965 |
rseg->trx_ref_count++; |
| 966 |
trx->rsegs.m_redo.rseg = rseg; |
| 967 |
*trx->xid = undo->xid; |
| 968 |
trx->id = undo->trx_id; |
| 969 |
trx_sys_rw_trx_add(trx); |
| 970 |
trx->is_recovered = true; |
| 971 |
} |
| 972 |
|
| 973 |
/* Assign the update_undo segment. */ |
| 974 |
ut_a(trx->rsegs.m_redo.update_undo == nullptr); |
| 975 |
trx->rsegs.m_redo.update_undo = undo; |
| 976 |
|
| 977 |
/* This is single-threaded startup code, we do not need the |
| 978 |
protection of trx->mutex or trx_sys->mutex here. */ |
| 979 |
|
| 980 |
if (undo->state != TRX_UNDO_ACTIVE) { |
| 981 |
trx_resurrect_update_in_prepared_state(trx, undo); |
| 982 |
|
| 983 |
/* We give a dummy value for the trx number */ |
| 984 |
|
| 985 |
trx->no = trx->id; |
| 986 |
|
| 987 |
} else { |
| 988 |
trx->state.store(TRX_STATE_ACTIVE, std::memory_order_relaxed); |
| 989 |
|
| 990 |
/* A running transaction always has the number field inited to |
| 991 |
TRX_ID_MAX */ |
| 992 |
|
| 993 |
trx->no = TRX_ID_MAX; |
| 994 |
} |
| 995 |
|
| 996 |
/* trx_start_low() is not called with resurrect, so need to initialize |
| 997 |
start time here.*/ |
| 998 |
if (trx->state.load(std::memory_order_relaxed) == TRX_STATE_ACTIVE || |
| 999 |
trx->state.load(std::memory_order_relaxed) == TRX_STATE_PREPARED) { |
| 1000 |
trx->start_time.store(std::chrono::system_clock::from_time_t(time(nullptr)), |
| 1001 |
std::memory_order_relaxed); |
| 1002 |
} |
| 1003 |
|
| 1004 |
trx->ddl_operation = undo->dict_operation; |
| 1005 |
|
| 1006 |
if (undo->dict_operation) { |
| 1007 |
trx_set_dict_operation(trx, TRX_DICT_OP_TABLE); |
| 1008 |
} |
| 1009 |
|
| 1010 |
if (!undo->empty && undo->top_undo_no >= trx->undo_no) { |
| 1011 |
trx->undo_no = undo->top_undo_no + 1; |
| 1012 |
trx->undo_rseg_space = undo->rseg->space_id; |
| 1013 |
} |
| 1014 |
} |
| 1015 |
|
| 1016 |
/** Resurrect the transactions that were doing inserts and updates at |
| 1017 |
the time of a crash, they need to be undone. |
| 1018 |
@param[in] rseg rollback segment */ |
| 1019 |
static void trx_resurrect(trx_rseg_t *rseg) { |
| 1020 |
ut_ad(rseg != nullptr); |
| 1021 |
ulong ins_trx_count = 0; |
| 1022 |
ulong upd_trx_count = 0; |
| 1023 |
/* Resurrect transactions that were doing inserts. */ |
| 1024 |
for (auto undo : rseg->insert_undo_list) { |
| 1025 |
auto trx = trx_resurrect_insert(undo, rseg); |
| 1026 |
ins_trx_count++; |
| 1027 |
ib::info(ER_IB_RESURRECT_TRX_INSERT, ulong(trx->id)); |
| 1028 |
trx_resurrect_table_ids(trx, &trx->rsegs.m_redo, undo); |
| 1029 |
} |
| 1030 |
if (ins_trx_count > 0) { |
| 1031 |
ib::info(ER_IB_RESURRECT_TRX_INSERT_COMPLETE, ins_trx_count); |
| 1032 |
} |
| 1033 |
/* Ressurrect transactions that were doing updates. */ |
| 1034 |
for (auto undo : rseg->update_undo_list) { |
| 1035 |
/* Check the active_rw_trxs.by_id first. */ |
| 1036 |
|
| 1037 |
trx_t *trx = trx_sys->latch_and_execute_with_active_trx( |
| 1038 |
undo->trx_id, [](trx_t *trx) { return trx; }, UT_LOCATION_HERE); |
| 1039 |
|
| 1040 |
if (trx == nullptr) { |
| 1041 |
trx = trx_allocate_for_background(); |
| 1042 |
|
| 1043 |
ut_d(trx->start_file = __FILE__); |
| 1044 |
ut_d(trx->start_line = __LINE__); |
| 1045 |
} |
| 1046 |
|
| 1047 |
trx_resurrect_update(trx, undo, rseg); |
| 1048 |
upd_trx_count++; |
| 1049 |
ib::info(ER_IB_RESURRECT_TRX_UPDATE, ulong(trx->id)); |
| 1050 |
trx_resurrect_table_ids(trx, &trx->rsegs.m_redo, undo); |
| 1051 |
} |
| 1052 |
if (upd_trx_count > 0) { |
| 1053 |
ib::info(ER_IB_RESURRECT_TRX_UPDATE_COMPLETE, upd_trx_count); |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
/** Adds the transaction to trx_sys->rw_trx_list |
| 1058 |
Requires trx_sys->mutex, unless called in the single threaded startup code. |
| 1059 |
@param[in] trx The transaction assumed to not be in the rw_trx_list yet |
| 1060 |
*/ |
| 1061 |
static inline void trx_add_to_rw_trx_list(trx_t *trx) { |
| 1062 |
ut_ad(srv_is_being_started || trx_sys_mutex_own()); |
| 1063 |
ut_ad(!trx->in_rw_trx_list); |
| 1064 |
UT_LIST_ADD_FIRST(trx_sys->rw_trx_list, trx); |
| 1065 |
ut_d(trx->in_rw_trx_list = true); |
| 1066 |
} |
| 1067 |
|
| 1068 |
/** Removes the transaction from trx_sys->rw_trx_list. |
| 1069 |
Requires trx_sys->mutex, unless called in the single threaded startup code. |
| 1070 |
@param[in] trx The transaction assumed to be in the rw_trx_list |
| 1071 |
*/ |
| 1072 |
static inline void trx_remove_from_rw_trx_list(trx_t *trx) { |
| 1073 |
ut_ad(srv_is_being_started || trx_sys_mutex_own()); |
| 1074 |
ut_ad(trx->in_rw_trx_list); |
| 1075 |
UT_LIST_REMOVE(trx_sys->rw_trx_list, trx); |
| 1076 |
ut_d(trx->in_rw_trx_list = false); |
| 1077 |
} |
| 1078 |
|
| 1079 |
/** Creates trx objects for transactions and initializes the trx list of |
| 1080 |
trx_sys at database start. Rollback segments and undo log lists must |
| 1081 |
already exist when this function is called, because the lists of |
| 1082 |
transactions to be rolled back or cleaned up are built based on the |
| 1083 |
undo log lists. */ |
| 1084 |
void trx_lists_init_at_db_start(void) { |
| 1085 |
ut_a(srv_is_being_started); |
| 1086 |
|
| 1087 |
/* Look through the rollback segments in the TRX_SYS for |
| 1088 |
transaction undo logs. */ |
| 1089 |
for (auto rseg : trx_sys->rsegs) { |
| 1090 |
trx_resurrect(rseg); |
| 1091 |
} |
| 1092 |
|
| 1093 |
/* Look through the rollback segments in each RSEG_ARRAY for |
| 1094 |
transaction undo logs. */ |
| 1095 |
undo::spaces->s_lock(); |
| 1096 |
for (auto undo_space : undo::spaces->m_spaces) { |
| 1097 |
undo_space->rsegs()->s_lock(); |
| 1098 |
for (auto rseg : *undo_space->rsegs()) { |
| 1099 |
trx_resurrect(rseg); |
| 1100 |
} |
| 1101 |
undo_space->rsegs()->s_unlock(); |
| 1102 |
} |
| 1103 |
undo::spaces->s_unlock(); |
| 1104 |
|
| 1105 |
ut::vector<trx_t *> trxs; |
| 1106 |
for (auto &shard : trx_sys->shards) { |
| 1107 |
shard.active_rw_trxs.latch_and_execute( |
| 1108 |
[&](const Trx_by_id_with_min &trx_by_id_with_min) { |
| 1109 |
for (const auto &trx_track : trx_by_id_with_min.by_id()) { |
| 1110 |
trxs.emplace_back(trx_track.second); |
| 1111 |
} |
| 1112 |
}, |
| 1113 |
UT_LOCATION_HERE); |
| 1114 |
} |
| 1115 |
std::sort(trxs.begin(), trxs.end(), |
| 1116 |
[&](trx_t *a, trx_t *b) { return a->id < b->id; }); |
| 1117 |
|
| 1118 |
for (trx_t *trx : trxs) { |
| 1119 |
if (trx->state.load(std::memory_order_relaxed) == TRX_STATE_ACTIVE || |
| 1120 |
trx->state.load(std::memory_order_relaxed) == TRX_STATE_PREPARED) { |
| 1121 |
trx_sys->rw_trx_ids.push_back(trx->id); |
| 1122 |
} |
| 1123 |
trx_add_to_rw_trx_list(trx); |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
/** Get next redo rollback segment in round-robin fashion. |
| 1128 |
While InnoDB is running in multi-threaded mode, the vectors of undo |
| 1129 |
tablespaces and rsegs do not shrink. So they do not need protection |
| 1130 |
to get a pointer to an rseg. |
| 1131 |
If an rseg is not marked for undo tablespace truncation, we assign |
| 1132 |
it to a transaction. We increment trx_ref_count to keep the purge |
| 1133 |
thread from truncating the undo tablespace that contains this rseg |
| 1134 |
until the transaction is done with it. |
| 1135 |
@return assigned rollback segment instance */ |
| 1136 |
static trx_rseg_t *get_next_redo_rseg_from_undo_spaces() { |
| 1137 |
undo::Tablespace *undo_space; |
| 1138 |
|
| 1139 |
/* The number of undo tablespaces cannot be changed while |
| 1140 |
we have this s_lock. */ |
| 1141 |
undo::spaces->s_lock(); |
| 1142 |
|
| 1143 |
/* Use all known undo tablespaces. Some may be inactive. */ |
| 1144 |
ulint target_undo_tablespaces = undo::spaces->size(); |
| 1145 |
|
| 1146 |
ut_ad(target_undo_tablespaces > 0); |
| 1147 |
|
| 1148 |
/* The number of rollback segments may be changed at any instant. |
| 1149 |
So use the value at this instant. Rollback segments are never |
| 1150 |
deleted from an rseg list, so srv_rollback_segments is always |
| 1151 |
less than rsegs->size(). */ |
| 1152 |
ulint target_rollback_segments = srv_rollback_segments; |
| 1153 |
|
| 1154 |
static std::atomic<ulint> rseg_counter{0}; |
| 1155 |
trx_rseg_t *rseg = nullptr; |
| 1156 |
ulint current = rseg_counter; |
| 1157 |
|
| 1158 |
while (rseg == nullptr) { |
| 1159 |
/* Increment the static redo_rseg_slot so the next call from any thread |
| 1160 |
starts with the next rseg. */ |
| 1161 |
rseg_counter.fetch_add(1); |
| 1162 |
|
| 1163 |
/* Traverse the rsegs like this: (space, rseg_id) |
| 1164 |
(0,0), (1,0), ... (n,0), (0,1), (1,1), ... (n,1), ... */ |
| 1165 |
ulint window = |
| 1166 |
current % (target_rollback_segments * target_undo_tablespaces); |
| 1167 |
ulint spaces_slot = window % target_undo_tablespaces; |
| 1168 |
ulint rseg_slot = window / target_undo_tablespaces; |
| 1169 |
|
| 1170 |
current++; |
| 1171 |
|
| 1172 |
undo_space = undo::spaces->at(spaces_slot); |
| 1173 |
|
| 1174 |
/* Avoid any rseg that resides in a tablespace that has been made |
| 1175 |
inactive either explicitly or by being marked for truncate. We do |
| 1176 |
not want to wait here on an x_lock for an rseg in an undo tablespace |
| 1177 |
that is being truncated. So check this first without the latch. |
| 1178 |
It could be set immediately after this, but that is a very short gap |
| 1179 |
and the get_active() call below will use an rseg->s_lock. */ |
| 1180 |
if (!undo_space->is_active_no_latch()) { |
| 1181 |
continue; |
| 1182 |
} |
| 1183 |
|
| 1184 |
/* This is done here because we know the rsegs() pointer is good. */ |
| 1185 |
ut_ad(target_rollback_segments <= undo_space->rsegs()->size()); |
| 1186 |
|
| 1187 |
/* Check again with a shared lock. */ |
| 1188 |
rseg = undo_space->get_active(rseg_slot); |
| 1189 |
if (rseg == nullptr) { |
| 1190 |
continue; |
| 1191 |
} |
| 1192 |
} |
| 1193 |
|
| 1194 |
undo::spaces->s_unlock(); |
| 1195 |
|
| 1196 |
ut_ad(rseg->trx_ref_count > 0); |
| 1197 |
|
| 1198 |
return (rseg); |
| 1199 |
} |
| 1200 |
|
| 1201 |
/** Get the next redo rollback segment in round-robin fashion. |
| 1202 |
The assigned slots may have gaps but the vector does not. |
| 1203 |
@return assigned rollback segment instance */ |
| 1204 |
static trx_rseg_t *get_next_redo_rseg_from_trx_sys() { |
| 1205 |
static std::atomic<ulint> rseg_counter{0}; |
| 1206 |
ulong n_rollback_segments = srv_rollback_segments; |
| 1207 |
|
| 1208 |
/* Versions 5.6 and 5.7 of InnoDB would allow 128 as the max for |
| 1209 |
innodb_rollback_segments but would only use 96 since 32 slots were |
| 1210 |
used for temporary rsegs. Now those rsegs are in trx_sys_t::tmp_rsegs |
| 1211 |
and trx_sys_t::rsegs which each can hold all 128. As a result, |
| 1212 |
an existing system tablespace might have gaps in the slot assignment. |
| 1213 |
The Rsegs vector only contains the rsegs that exist. Since |
| 1214 |
srv_rollback_segments can be set to a smaller number at runtime, |
| 1215 |
it might be smaller than Rsegs::size(). But srv_rollback_segments |
| 1216 |
can never be larger than Rsegs::size() because when the user increases |
| 1217 |
innodb_rollback_segments, the rollback segments are created and rseg |
| 1218 |
objects are added to the vector ready to use before |
| 1219 |
srv_rollback_segments is increased. */ |
| 1220 |
ut_ad(n_rollback_segments <= trx_sys->rsegs.size()); |
| 1221 |
|
| 1222 |
/* Try the next slot that no other thread is looking at */ |
| 1223 |
ulint slot = (rseg_counter.fetch_add(1) + 1) % n_rollback_segments; |
| 1224 |
|
| 1225 |
/* s_lock the vector since it might be sorted when added to. */ |
| 1226 |
trx_sys->rsegs.s_lock(); |
| 1227 |
trx_rseg_t *rseg = trx_sys->rsegs.at(slot); |
| 1228 |
trx_sys->rsegs.s_unlock(); |
| 1229 |
|
| 1230 |
/* It is not necessary to s_lock Rsegs::m_latch here because the |
| 1231 |
system tablespace is never truncated like other undo tablespaces. */ |
| 1232 |
rseg->trx_ref_count++; |
| 1233 |
|
| 1234 |
ut_ad(rseg->space_id == TRX_SYS_SPACE); |
| 1235 |
|
| 1236 |
return (rseg); |
| 1237 |
} |
| 1238 |
|
| 1239 |
/** Get next redo rollback segment in round-robin fashion. |
| 1240 |
We assume that the assigned slots are not contiguous and have gaps. |
| 1241 |
@return assigned rollback segment instance */ |
| 1242 |
static trx_rseg_t *get_next_redo_rseg() { |
| 1243 |
if (!trx_sys->rsegs.is_empty()) { |
| 1244 |
return (get_next_redo_rseg_from_trx_sys()); |
| 1245 |
} else { |
| 1246 |
return (get_next_redo_rseg_from_undo_spaces()); |
| 1247 |
} |
| 1248 |
} |
| 1249 |
|
| 1250 |
/** Get the next noredo rollback segment. |
| 1251 |
@return assigned rollback segment instance */ |
| 1252 |
static trx_rseg_t *get_next_temp_rseg() { |
| 1253 |
static std::atomic<ulint> temp_rseg_counter{0}; |
| 1254 |
ulong n_rollback_segments = srv_rollback_segments; |
| 1255 |
|
| 1256 |
ut_ad(n_rollback_segments <= trx_sys->tmp_rsegs.size()); |
| 1257 |
|
| 1258 |
/* Try the next slot that no other thread is looking at */ |
| 1259 |
ulint slot = (temp_rseg_counter.fetch_add(1) + 1) % n_rollback_segments; |
| 1260 |
|
| 1261 |
/* No need to s_lock the vector since it is only added to at the end, |
| 1262 |
and it is never resized or sorted. */ |
| 1263 |
trx_rseg_t *rseg = trx_sys->tmp_rsegs.at(slot); |
| 1264 |
|
| 1265 |
ut_ad(rseg->id == slot); |
| 1266 |
ut_ad(fsp_is_system_temporary(rseg->space_id)); |
| 1267 |
|
| 1268 |
return (rseg); |
| 1269 |
} |
| 1270 |
|
| 1271 |
/** Assign a durable rollback segment to a transaction in a round-robin |
| 1272 |
fashion. |
| 1273 |
@param[in,out] trx transaction that involves a durable write. */ |
| 1274 |
void trx_assign_rseg_durable(trx_t *trx) { |
| 1275 |
ut_ad(trx->rsegs.m_redo.rseg == nullptr); |
| 1276 |
|
| 1277 |
trx->rsegs.m_redo.rseg = srv_read_only_mode ? nullptr : get_next_redo_rseg(); |
| 1278 |
} |
| 1279 |
|
| 1280 |
/** Assign a temp-tablespace bound rollback-segment to a transaction. |
| 1281 |
@param[in,out] trx transaction that involves write to temp-table. */ |
| 1282 |
void trx_assign_rseg_temp(trx_t *trx) { |
| 1283 |
ut_ad(trx->rsegs.m_noredo.rseg == nullptr); |
| 1284 |
ut_ad(!trx_is_autocommit_non_locking(trx)); |
| 1285 |
|
| 1286 |
trx->rsegs.m_noredo.rseg = |
| 1287 |
srv_read_only_mode ? nullptr : get_next_temp_rseg(); |
| 1288 |
|
| 1289 |
if (trx->id == 0) { |
| 1290 |
trx_sys_mutex_enter(); |
| 1291 |
|
| 1292 |
trx->id = trx_sys_allocate_trx_id(); |
| 1293 |
|
| 1294 |
trx_sys->rw_trx_ids.push_back(trx->id); |
| 1295 |
|
| 1296 |
trx_sys_mutex_exit(); |
| 1297 |
|
| 1298 |
trx_sys_rw_trx_add(trx); |
| 1299 |
} |
| 1300 |
} |
| 1301 |
|
| 1302 |
/** Starts a transaction. */ |
| 1303 |
static void trx_start_low( |
| 1304 |
trx_t *trx, /*!< in: transaction */ |
| 1305 |
bool read_write) /*!< in: true if read-write transaction */ |
| 1306 |
{ |
| 1307 |
ut_ad(!trx->in_rollback); |
| 1308 |
ut_ad(!trx->is_recovered); |
| 1309 |
ut_ad(trx->start_line != 0); |
| 1310 |
ut_ad(trx->start_file != nullptr); |
| 1311 |
ut_ad(trx->roll_limit == 0); |
| 1312 |
ut_ad(!trx->lock.in_rollback); |
| 1313 |
ut_ad(trx->error_state == DB_SUCCESS); |
| 1314 |
ut_ad(trx->rsegs.m_redo.rseg == nullptr); |
| 1315 |
ut_ad(trx->rsegs.m_noredo.rseg == nullptr); |
| 1316 |
ut_ad(trx_state_eq(trx, TRX_STATE_NOT_STARTED)); |
| 1317 |
ut_ad(UT_LIST_GET_LEN(trx->lock.trx_locks) == 0); |
| 1318 |
ut_ad(!(trx->in_innodb & TRX_FORCE_ROLLBACK)); |
| 1319 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 1320 |
|
| 1321 |
++trx->version; |
| 1322 |
|
| 1323 |
/* Check whether it is an AUTOCOMMIT SELECT */ |
| 1324 |
trx->auto_commit = (trx->api_trx && trx->api_auto_commit) || |
| 1325 |
thd_trx_is_auto_commit(trx->mysql_thd); |
| 1326 |
|
| 1327 |
trx->read_only = (trx->api_trx && !trx->read_write) || |
| 1328 |
(!trx->internal && thd_trx_is_read_only(trx->mysql_thd)) || |
| 1329 |
srv_read_only_mode; |
| 1330 |
|
| 1331 |
if (!trx->auto_commit) { |
| 1332 |
++trx->will_lock; |
| 1333 |
} else if (trx->will_lock == 0) { |
| 1334 |
trx->read_only = true; |
| 1335 |
} |
| 1336 |
trx->persists_gtid = false; |
| 1337 |
|
| 1338 |
#ifdef UNIV_DEBUG |
| 1339 |
/* If the transaction is DD attachable trx, it should be AC-NL-RO |
| 1340 |
(AutoCommit-NonLocking-ReadOnly) trx */ |
| 1341 |
if (trx->is_dd_trx) { |
| 1342 |
ut_ad(trx->read_only); |
| 1343 |
ut_ad(trx->auto_commit); |
| 1344 |
ut_ad(trx->isolation_level == TRX_ISO_READ_UNCOMMITTED || |
| 1345 |
trx->isolation_level == TRX_ISO_READ_COMMITTED); |
| 1346 |
} |
| 1347 |
#endif /* UNIV_DEBUG */ |
| 1348 |
|
| 1349 |
/* Note, that trx->start_time is set without std::memory_order_release, |
| 1350 |
and it is possible that trx->state below is set neither within critical |
| 1351 |
section protected by trx_sys->mutex nor, with std::memory_order_release. |
| 1352 |
That is possible for read-only transactions in code further below. |
| 1353 |
This can result in an incorrect message printed to error log inside the |
| 1354 |
buf_pool_resize thread about transaction lasting too long. The decision |
| 1355 |
was to keep this issue for read-only transactions as it was, because |
| 1356 |
providing a fix which would guarantee that state of printed information |
| 1357 |
about such transactions is always consistent, would take much more work. |
| 1358 |
TODO: check performance gain from this micro-optimization on ARM. */ |
| 1359 |
|
| 1360 |
if (trx->mysql_thd != nullptr) { |
| 1361 |
trx->start_time.store(thd_start_time(trx->mysql_thd), |
| 1362 |
std::memory_order_relaxed); |
| 1363 |
if (!trx->ddl_operation) { |
| 1364 |
trx->ddl_operation = thd_is_dd_update_stmt(trx->mysql_thd); |
| 1365 |
} |
| 1366 |
} else { |
| 1367 |
trx->start_time.store(std::chrono::system_clock::from_time_t(time(nullptr)), |
| 1368 |
std::memory_order_relaxed); |
| 1369 |
} |
| 1370 |
|
| 1371 |
/* The initial value for trx->no: TRX_ID_MAX is used in |
| 1372 |
read_view_open_now: */ |
| 1373 |
|
| 1374 |
trx->no = TRX_ID_MAX; |
| 1375 |
|
| 1376 |
ut_a(ib_vector_is_empty(trx->lock.autoinc_locks)); |
| 1377 |
|
| 1378 |
/* This value will only be read by a thread inspecting lock sys queue after |
| 1379 |
the thread which enqueues this trx releases the queue's latch. */ |
| 1380 |
trx->lock.schedule_weight.store(0, std::memory_order_relaxed); |
| 1381 |
|
| 1382 |
/* If this transaction came from trx_allocate_for_mysql(), |
| 1383 |
trx->in_mysql_trx_list would hold. In that case, the trx->state |
| 1384 |
change must be protected by the trx_sys->mutex, so that |
| 1385 |
lock_print_info_all_transactions() will have a consistent view. */ |
| 1386 |
|
| 1387 |
ut_ad(!trx->in_rw_trx_list); |
| 1388 |
|
| 1389 |
/* We tend to over assert and that complicates the code somewhat. |
| 1390 |
e.g., the transaction state can be set earlier but we are forced to |
| 1391 |
set it under the protection of the trx_sys_t::mutex because some |
| 1392 |
trx list assertions are triggered unnecessarily. */ |
| 1393 |
|
| 1394 |
/* By default all transactions are in the read-only list unless they |
| 1395 |
are non-locking auto-commit read only transactions or background |
| 1396 |
(internal) transactions. Note: Transactions marked explicitly as |
| 1397 |
read only can write to temporary tables, we put those on the RO |
| 1398 |
list too. */ |
| 1399 |
|
| 1400 |
if (!trx->read_only && |
| 1401 |
(trx->mysql_thd == nullptr || read_write || trx->ddl_operation)) { |
| 1402 |
trx_assign_rseg_durable(trx); |
| 1403 |
|
| 1404 |
/* Temporary rseg is assigned only if the transaction |
| 1405 |
updates a temporary table */ |
| 1406 |
DEBUG_SYNC_C("trx_sys_before_assign_id"); |
| 1407 |
|
| 1408 |
trx_sys_mutex_enter(); |
| 1409 |
|
| 1410 |
trx->id = trx_sys_allocate_trx_id(); |
| 1411 |
|
| 1412 |
trx_sys->rw_trx_ids.push_back(trx->id); |
| 1413 |
|
| 1414 |
ut_ad(trx->rsegs.m_redo.rseg != nullptr || srv_read_only_mode || |
| 1415 |
srv_force_recovery >= SRV_FORCE_NO_TRX_UNDO); |
| 1416 |
|
| 1417 |
trx_add_to_rw_trx_list(trx); |
| 1418 |
|
| 1419 |
trx->state.store(TRX_STATE_ACTIVE, std::memory_order_relaxed); |
| 1420 |
|
| 1421 |
ut_ad(trx_sys_validate_trx_list()); |
| 1422 |
|
| 1423 |
trx_sys_mutex_exit(); |
| 1424 |
|
| 1425 |
trx_sys_rw_trx_add(trx); |
| 1426 |
|
| 1427 |
} else { |
| 1428 |
trx->id = 0; |
| 1429 |
|
| 1430 |
if (!trx_is_autocommit_non_locking(trx)) { |
| 1431 |
/* If this is a read-only transaction that is writing |
| 1432 |
to a temporary table then it needs a transaction id |
| 1433 |
to write to the temporary table. */ |
| 1434 |
|
| 1435 |
if (read_write) { |
| 1436 |
trx_sys_mutex_enter(); |
| 1437 |
|
| 1438 |
ut_ad(!srv_read_only_mode); |
| 1439 |
|
| 1440 |
trx->state.store(TRX_STATE_ACTIVE, std::memory_order_relaxed); |
| 1441 |
|
| 1442 |
trx->id = trx_sys_allocate_trx_id(); |
| 1443 |
|
| 1444 |
trx_sys->rw_trx_ids.push_back(trx->id); |
| 1445 |
|
| 1446 |
trx_sys_mutex_exit(); |
| 1447 |
|
| 1448 |
trx_sys_rw_trx_add(trx); |
| 1449 |
|
| 1450 |
} else { |
| 1451 |
trx->state.store(TRX_STATE_ACTIVE, std::memory_order_relaxed); |
| 1452 |
} |
| 1453 |
} else { |
| 1454 |
ut_ad(!read_write); |
| 1455 |
trx->state.store(TRX_STATE_ACTIVE, std::memory_order_relaxed); |
| 1456 |
} |
| 1457 |
} |
| 1458 |
|
| 1459 |
ut_a(trx->error_state == DB_SUCCESS); |
| 1460 |
|
| 1461 |
MONITOR_INC(MONITOR_TRX_ACTIVE); |
| 1462 |
} |
| 1463 |
|
| 1464 |
/** Assigns the trx->no and add the transaction to the serialisation_list. |
| 1465 |
Skips adding to the serialisation_list if the transaction is read-only, in |
| 1466 |
which case still the trx->no is assigned. |
| 1467 |
@param[in,out] trx the modified transaction |
| 1468 |
@return true if added to the serialisation_list (non read-only trx) */ |
| 1469 |
static inline bool trx_add_to_serialisation_list(trx_t *trx) { |
| 1470 |
trx_sys_serialisation_mutex_enter(); |
| 1471 |
|
| 1472 |
trx->no = trx_sys_allocate_trx_no(); |
| 1473 |
|
| 1474 |
/* Update the latest transaction number. */ |
| 1475 |
ut_d(trx_sys->rw_max_trx_no = trx->no); |
| 1476 |
|
| 1477 |
if (trx->read_only) { |
| 1478 |
trx_sys_serialisation_mutex_exit(); |
| 1479 |
return false; |
| 1480 |
} |
| 1481 |
|
| 1482 |
UT_LIST_ADD_LAST(trx_sys->serialisation_list, trx); |
| 1483 |
|
| 1484 |
if (UT_LIST_GET_LEN(trx_sys->serialisation_list) == 1) { |
| 1485 |
trx_sys->serialisation_min_trx_no.store(trx->no); |
| 1486 |
} |
| 1487 |
|
| 1488 |
trx_sys_serialisation_mutex_exit(); |
| 1489 |
return true; |
| 1490 |
} |
| 1491 |
|
| 1492 |
/** Erases transaction from the serialisation_list. Caller must have |
| 1493 |
acquired trx_sys->serialisation_mutex prior to calling this function. |
| 1494 |
@param[in,out] trx the transaction to erase */ |
| 1495 |
static inline void trx_erase_from_serialisation_list_low(trx_t *trx) { |
| 1496 |
ut_ad(trx_sys_serialisation_mutex_own()); |
| 1497 |
|
| 1498 |
UT_LIST_REMOVE(trx_sys->serialisation_list, trx); |
| 1499 |
|
| 1500 |
if (UT_LIST_GET_LEN(trx_sys->serialisation_list) > 0) { |
| 1501 |
trx_sys->serialisation_min_trx_no.store( |
| 1502 |
UT_LIST_GET_FIRST(trx_sys->serialisation_list)->no); |
| 1503 |
|
| 1504 |
} else { |
| 1505 |
trx_sys->serialisation_min_trx_no.store(trx_sys_get_next_trx_id_or_no()); |
| 1506 |
} |
| 1507 |
} |
| 1508 |
|
| 1509 |
/** Set the transaction serialisation number. |
| 1510 |
@return true if the transaction number was added to the serialisation_list. */ |
| 1511 |
static bool trx_serialisation_number_get( |
| 1512 |
trx_t *trx, /*!< in/out: transaction */ |
| 1513 |
trx_undo_ptr_t *redo_rseg_undo_ptr, /*!< in/out: Set trx |
| 1514 |
serialisation number in |
| 1515 |
referred undo rseg. */ |
| 1516 |
trx_undo_ptr_t *temp_rseg_undo_ptr) /*!< in/out: Set trx |
| 1517 |
serialisation number in |
| 1518 |
referred undo rseg. */ |
| 1519 |
{ |
| 1520 |
bool added_trx_no; |
| 1521 |
trx_rseg_t *redo_rseg = nullptr; |
| 1522 |
trx_rseg_t *temp_rseg = nullptr; |
| 1523 |
|
| 1524 |
if (redo_rseg_undo_ptr != nullptr) { |
| 1525 |
ut_ad(mutex_own(&redo_rseg_undo_ptr->rseg->mutex)); |
| 1526 |
redo_rseg = redo_rseg_undo_ptr->rseg; |
| 1527 |
} |
| 1528 |
|
| 1529 |
if (temp_rseg_undo_ptr != nullptr) { |
| 1530 |
ut_ad(mutex_own(&temp_rseg_undo_ptr->rseg->mutex)); |
| 1531 |
temp_rseg = temp_rseg_undo_ptr->rseg; |
| 1532 |
} |
| 1533 |
|
| 1534 |
/* If the rollack segment is not empty then the |
| 1535 |
new trx_t::no can't be less than any trx_t::no |
| 1536 |
already in the rollback segment. User threads only |
| 1537 |
produce events when a rollback segment is empty. */ |
| 1538 |
if ((redo_rseg != nullptr && redo_rseg->last_page_no == FIL_NULL) || |
| 1539 |
(temp_rseg != nullptr && temp_rseg->last_page_no == FIL_NULL)) { |
| 1540 |
TrxUndoRsegs elem; |
| 1541 |
|
| 1542 |
if (redo_rseg != nullptr && redo_rseg->last_page_no == FIL_NULL) { |
| 1543 |
elem.insert(redo_rseg); |
| 1544 |
} |
| 1545 |
|
| 1546 |
if (temp_rseg != nullptr && temp_rseg->last_page_no == FIL_NULL) { |
| 1547 |
elem.insert(temp_rseg); |
| 1548 |
} |
| 1549 |
|
| 1550 |
mutex_enter(&purge_sys->pq_mutex); |
| 1551 |
|
| 1552 |
added_trx_no = trx_add_to_serialisation_list(trx); |
| 1553 |
|
| 1554 |
elem.set_trx_no(trx->no); |
| 1555 |
|
| 1556 |
purge_sys->purge_queue->push(std::move(elem)); |
| 1557 |
|
| 1558 |
mutex_exit(&purge_sys->pq_mutex); |
| 1559 |
|
| 1560 |
} else { |
| 1561 |
added_trx_no = trx_add_to_serialisation_list(trx); |
| 1562 |
} |
| 1563 |
|
| 1564 |
return (added_trx_no); |
| 1565 |
} |
| 1566 |
|
| 1567 |
/** Assign the transaction its history serialisation number and write the |
| 1568 |
update UNDO log record to the assigned rollback segment. |
| 1569 |
@return true if a serialisation log was written */ |
| 1570 |
static bool trx_write_serialisation_history( |
| 1571 |
trx_t *trx, /*!< in/out: transaction */ |
| 1572 |
mtr_t *mtr) /*!< in/out: mini-transaction */ |
| 1573 |
{ |
| 1574 |
/* Change the undo log segment states from TRX_UNDO_ACTIVE to some |
| 1575 |
other state: these modifications to the file data structure define |
| 1576 |
the transaction as committed in the file based domain, at the |
| 1577 |
serialization point of the log sequence number lsn obtained below. */ |
| 1578 |
|
| 1579 |
/* We have to hold the rseg mutex because update log headers have |
| 1580 |
to be put to the history list in the (serialisation) order of the |
| 1581 |
UNDO trx number. This is required for the purge in-memory data |
| 1582 |
structures too. */ |
| 1583 |
|
| 1584 |
bool own_redo_rseg_mutex = false; |
| 1585 |
bool own_temp_rseg_mutex = false; |
| 1586 |
|
| 1587 |
/* Get rollback segment mutex. */ |
| 1588 |
if (trx->rsegs.m_redo.rseg != nullptr && trx_is_redo_rseg_updated(trx)) { |
| 1589 |
trx->rsegs.m_redo.rseg->latch(); |
| 1590 |
own_redo_rseg_mutex = true; |
| 1591 |
} |
| 1592 |
|
| 1593 |
mtr_t temp_mtr; |
| 1594 |
|
| 1595 |
if (trx->rsegs.m_noredo.rseg != nullptr && trx_is_temp_rseg_updated(trx)) { |
| 1596 |
trx->rsegs.m_noredo.rseg->latch(); |
| 1597 |
own_temp_rseg_mutex = true; |
| 1598 |
mtr_start(&temp_mtr); |
| 1599 |
temp_mtr.set_log_mode(MTR_LOG_NO_REDO); |
| 1600 |
} |
| 1601 |
|
| 1602 |
/* If transaction involves insert then truncate undo logs. */ |
| 1603 |
if (trx->rsegs.m_redo.insert_undo != nullptr) { |
| 1604 |
trx_undo_set_state_at_finish(trx->rsegs.m_redo.insert_undo, mtr); |
| 1605 |
} |
| 1606 |
|
| 1607 |
if (trx->rsegs.m_noredo.insert_undo != nullptr) { |
| 1608 |
trx_undo_set_state_at_finish(trx->rsegs.m_noredo.insert_undo, &temp_mtr); |
| 1609 |
} |
| 1610 |
|
| 1611 |
bool serialised = false; |
| 1612 |
|
| 1613 |
/* If transaction involves update then add rollback segments |
| 1614 |
to purge queue. */ |
| 1615 |
if (trx->rsegs.m_redo.update_undo != nullptr || |
| 1616 |
trx->rsegs.m_noredo.update_undo != nullptr) { |
| 1617 |
/* Assign the transaction serialisation number and add these |
| 1618 |
rollback segments to purge trx-no sorted priority queue |
| 1619 |
if this is the first UNDO log being written to assigned |
| 1620 |
rollback segments. */ |
| 1621 |
|
| 1622 |
trx_undo_ptr_t *redo_rseg_undo_ptr = |
| 1623 |
trx->rsegs.m_redo.update_undo != nullptr ? &trx->rsegs.m_redo : nullptr; |
| 1624 |
|
| 1625 |
trx_undo_ptr_t *temp_rseg_undo_ptr = |
| 1626 |
trx->rsegs.m_noredo.update_undo != nullptr ? &trx->rsegs.m_noredo |
| 1627 |
: nullptr; |
| 1628 |
|
| 1629 |
/* Will set trx->no and will add rseg to purge queue. */ |
| 1630 |
serialised = trx_serialisation_number_get(trx, redo_rseg_undo_ptr, |
| 1631 |
temp_rseg_undo_ptr); |
| 1632 |
|
| 1633 |
/* It is not necessary to obtain trx->undo_mutex here because |
| 1634 |
only a single OS thread is allowed to do the transaction commit |
| 1635 |
for this transaction. */ |
| 1636 |
if (trx->rsegs.m_redo.update_undo != nullptr) { |
| 1637 |
page_t *undo_hdr_page; |
| 1638 |
|
| 1639 |
undo_hdr_page = |
| 1640 |
trx_undo_set_state_at_finish(trx->rsegs.m_redo.update_undo, mtr); |
| 1641 |
|
| 1642 |
/* Delay update of rseg_history_len if we plan to add |
| 1643 |
non-redo update_undo too. This is to avoid immediate |
| 1644 |
invocation of purge as we need to club these 2 segments |
| 1645 |
with same trx-no as single unit. */ |
| 1646 |
bool update_rseg_len = !(trx->rsegs.m_noredo.update_undo != nullptr); |
| 1647 |
|
| 1648 |
/* Set flag if GTID information need to persist. */ |
| 1649 |
auto undo_ptr = &trx->rsegs.m_redo; |
| 1650 |
trx_undo_gtid_set(trx, undo_ptr->update_undo, false); |
| 1651 |
|
| 1652 |
trx_undo_update_cleanup(trx, undo_ptr, undo_hdr_page, update_rseg_len, |
| 1653 |
(update_rseg_len ? 1 : 0), mtr); |
| 1654 |
} |
| 1655 |
|
| 1656 |
DBUG_EXECUTE_IF("ib_trx_crash_during_commit", DBUG_SUICIDE();); |
| 1657 |
|
| 1658 |
if (trx->rsegs.m_noredo.update_undo != nullptr) { |
| 1659 |
page_t *undo_hdr_page; |
| 1660 |
|
| 1661 |
undo_hdr_page = trx_undo_set_state_at_finish( |
| 1662 |
trx->rsegs.m_noredo.update_undo, &temp_mtr); |
| 1663 |
|
| 1664 |
ulint n_added_logs = (redo_rseg_undo_ptr != nullptr) ? 2 : 1; |
| 1665 |
|
| 1666 |
trx_undo_update_cleanup(trx, &trx->rsegs.m_noredo, undo_hdr_page, true, |
| 1667 |
n_added_logs, &temp_mtr); |
| 1668 |
} |
| 1669 |
} |
| 1670 |
|
| 1671 |
if (own_redo_rseg_mutex) { |
| 1672 |
trx->rsegs.m_redo.rseg->unlatch(); |
| 1673 |
own_redo_rseg_mutex = false; |
| 1674 |
} |
| 1675 |
|
| 1676 |
if (own_temp_rseg_mutex) { |
| 1677 |
trx->rsegs.m_noredo.rseg->unlatch(); |
| 1678 |
own_temp_rseg_mutex = false; |
| 1679 |
mtr_commit(&temp_mtr); |
| 1680 |
} |
| 1681 |
|
| 1682 |
MONITOR_INC(MONITOR_TRX_COMMIT_UNDO); |
| 1683 |
|
| 1684 |
/* Update the latest MySQL binlog name and offset information |
| 1685 |
in trx sys header only if MySQL binary logging is on and clone |
| 1686 |
is has ensured commit order at final stage. */ |
| 1687 |
if (Clone_handler::need_commit_order()) { |
| 1688 |
trx_sys_update_mysql_binlog_offset(trx, mtr); |
| 1689 |
} |
| 1690 |
|
| 1691 |
return (serialised); |
| 1692 |
} |
| 1693 |
|
| 1694 |
/******************************************************************** |
| 1695 |
Finalize a transaction containing updates for a FTS table. */ |
| 1696 |
static void trx_finalize_for_fts_table( |
| 1697 |
fts_trx_table_t *ftt) /* in: FTS trx table */ |
| 1698 |
{ |
| 1699 |
fts_t *fts = ftt->table->fts; |
| 1700 |
fts_doc_ids_t *doc_ids = ftt->added_doc_ids; |
| 1701 |
|
| 1702 |
mutex_enter(&fts->bg_threads_mutex); |
| 1703 |
|
| 1704 |
if (fts->fts_status & BG_THREAD_STOP) { |
| 1705 |
/* The table is about to be dropped, no use |
| 1706 |
adding anything to its work queue. */ |
| 1707 |
|
| 1708 |
mutex_exit(&fts->bg_threads_mutex); |
| 1709 |
} else { |
| 1710 |
mem_heap_t *heap; |
| 1711 |
mutex_exit(&fts->bg_threads_mutex); |
| 1712 |
|
| 1713 |
ut_a(fts->add_wq); |
| 1714 |
|
| 1715 |
heap = static_cast<mem_heap_t *>(doc_ids->self_heap->arg); |
| 1716 |
|
| 1717 |
ib_wqueue_add(fts->add_wq, doc_ids, heap); |
| 1718 |
|
| 1719 |
/* fts_trx_table_t no longer owns the list. */ |
| 1720 |
ftt->added_doc_ids = nullptr; |
| 1721 |
} |
| 1722 |
} |
| 1723 |
|
| 1724 |
/** Finalize a transaction containing updates to FTS tables. */ |
| 1725 |
static void trx_finalize_for_fts( |
| 1726 |
trx_t *trx, /*!< in/out: transaction */ |
| 1727 |
bool is_commit) /*!< in: true if the transaction was |
| 1728 |
committed, false if it was rolled back. */ |
| 1729 |
{ |
| 1730 |
if (is_commit) { |
| 1731 |
const ib_rbt_node_t *node; |
| 1732 |
ib_rbt_t *tables; |
| 1733 |
fts_savepoint_t *savepoint; |
| 1734 |
|
| 1735 |
savepoint = static_cast<fts_savepoint_t *>( |
| 1736 |
ib_vector_last(trx->fts_trx->savepoints)); |
| 1737 |
|
| 1738 |
tables = savepoint->tables; |
| 1739 |
|
| 1740 |
for (node = rbt_first(tables); node; node = rbt_next(tables, node)) { |
| 1741 |
fts_trx_table_t **ftt; |
| 1742 |
|
| 1743 |
ftt = rbt_value(fts_trx_table_t *, node); |
| 1744 |
|
| 1745 |
if ((*ftt)->added_doc_ids) { |
| 1746 |
trx_finalize_for_fts_table(*ftt); |
| 1747 |
} |
| 1748 |
} |
| 1749 |
} |
| 1750 |
|
| 1751 |
fts_trx_free(trx->fts_trx); |
| 1752 |
trx->fts_trx = nullptr; |
| 1753 |
} |
| 1754 |
|
| 1755 |
/** If required, flushes the log to disk based on the value of |
| 1756 |
innodb_flush_log_at_trx_commit. */ |
| 1757 |
static void trx_flush_log_if_needed_low(lsn_t lsn) /*!< in: lsn up to which logs |
| 1758 |
are to be flushed. */ |
| 1759 |
{ |
| 1760 |
#ifdef _WIN32 |
| 1761 |
bool flush = true; |
| 1762 |
#else |
| 1763 |
bool flush = srv_unix_file_flush_method != SRV_UNIX_NOSYNC; |
| 1764 |
#endif /* _WIN32 */ |
| 1765 |
|
| 1766 |
Wait_stats wait_stats; |
| 1767 |
|
| 1768 |
switch (srv_flush_log_at_trx_commit) { |
| 1769 |
case 2: |
| 1770 |
/* Write the log but do not flush it to disk */ |
| 1771 |
flush = false; |
| 1772 |
[[fallthrough]]; |
| 1773 |
case 1: |
| 1774 |
/* Write the log and optionally flush it to disk */ |
| 1775 |
wait_stats = log_write_up_to(*log_sys, lsn, flush); |
| 1776 |
|
| 1777 |
MONITOR_INC_WAIT_STATS(MONITOR_TRX_ON_LOG_, wait_stats); |
| 1778 |
|
| 1779 |
return; |
| 1780 |
case 0: |
| 1781 |
/* Do nothing */ |
| 1782 |
return; |
| 1783 |
} |
| 1784 |
} |
| 1785 |
|
| 1786 |
/** If required, flushes the log to disk based on the value of |
| 1787 |
innodb_flush_log_at_trx_commit. */ |
| 1788 |
static void trx_flush_log_if_needed(lsn_t lsn, /*!< in: lsn up to which logs are |
| 1789 |
to be flushed. */ |
| 1790 |
trx_t *trx) /*!< in/out: transaction */ |
| 1791 |
{ |
| 1792 |
trx->op_info = "flushing log"; |
| 1793 |
|
| 1794 |
DEBUG_SYNC_C("trx_flush_log_if_needed"); |
| 1795 |
|
| 1796 |
if (trx->ddl_operation || trx->ddl_must_flush) { |
| 1797 |
auto wait_stats = log_write_up_to(*log_sys, lsn, true); |
| 1798 |
MONITOR_INC_WAIT_STATS(MONITOR_TRX_ON_LOG_, wait_stats); |
| 1799 |
} else { |
| 1800 |
trx_flush_log_if_needed_low(lsn); |
| 1801 |
} |
| 1802 |
|
| 1803 |
trx->op_info = ""; |
| 1804 |
} |
| 1805 |
|
| 1806 |
/** For each table that has been modified by the given transaction: update |
| 1807 |
its dict_table_t::update_time with the current timestamp. Clear the list |
| 1808 |
of the modified tables at the end. */ |
| 1809 |
static void trx_update_mod_tables_timestamp(trx_t *trx) /*!< in: transaction */ |
| 1810 |
{ |
| 1811 |
ut_ad(trx->id != 0); |
| 1812 |
|
| 1813 |
/* consider using trx->start_time if calling time() is too |
| 1814 |
expensive here */ |
| 1815 |
const auto now = std::chrono::system_clock::from_time_t(time(nullptr)); |
| 1816 |
|
| 1817 |
trx_mod_tables_t::const_iterator end = trx->mod_tables.end(); |
| 1818 |
|
| 1819 |
for (trx_mod_tables_t::const_iterator it = trx->mod_tables.begin(); it != end; |
| 1820 |
++it) { |
| 1821 |
(*it)->update_time = now; |
| 1822 |
} |
| 1823 |
|
| 1824 |
trx->mod_tables.clear(); |
| 1825 |
} |
| 1826 |
|
| 1827 |
/** |
| 1828 |
Erase the transaction from running transaction lists and serialization |
| 1829 |
list. Active RW transaction list of a MVCC snapshot(ReadView::prepare) |
| 1830 |
won't include this transaction after this call. All implicit locks are |
| 1831 |
also released by this call as trx is removed from rw_trx_list. |
| 1832 |
@param[in] trx Transaction to erase, must have an ID > 0 */ |
| 1833 |
static void trx_erase_lists(trx_t *trx) { |
| 1834 |
ut_ad(trx->id > 0); |
| 1835 |
ut_ad(trx_sys_mutex_own()); |
| 1836 |
|
| 1837 |
trx_ids_t::iterator it = std::lower_bound(trx_sys->rw_trx_ids.begin(), |
| 1838 |
trx_sys->rw_trx_ids.end(), trx->id); |
| 1839 |
|
| 1840 |
ut_ad(*it == trx->id); |
| 1841 |
trx_sys->rw_trx_ids.erase(it); |
| 1842 |
|
| 1843 |
if (trx->read_only || trx->rsegs.m_redo.rseg == nullptr) { |
| 1844 |
ut_ad(!trx->in_rw_trx_list); |
| 1845 |
} else { |
| 1846 |
trx_remove_from_rw_trx_list(trx); |
| 1847 |
ut_ad(trx_sys_validate_trx_list()); |
| 1848 |
|
| 1849 |
if (trx->read_view != nullptr) { |
| 1850 |
trx_sys->mvcc->view_close(trx->read_view, true); |
| 1851 |
} |
| 1852 |
} |
| 1853 |
DEBUG_SYNC_C("after_trx_erase_lists"); |
| 1854 |
} |
| 1855 |
|
| 1856 |
static void trx_release_impl_and_expl_locks(trx_t *trx, bool serialised) { |
| 1857 |
check_trx_state(trx); |
| 1858 |
ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE) || |
| 1859 |
trx_state_eq(trx, TRX_STATE_PREPARED)); |
| 1860 |
|
| 1861 |
bool trx_sys_latch_is_needed = |
| 1862 |
(trx->id > 0) || trx_state_eq(trx, TRX_STATE_PREPARED); |
| 1863 |
|
| 1864 |
/* Check and get GTID to be persisted. Do it outside mutex. It must be done |
| 1865 |
before trx->state is changed to TRX_STATE_COMMITTED_IN_MEMORY, because the |
| 1866 |
gtid_persistor.get_gtid_info() calls gtid_persistor.has_gtid() which checks |
| 1867 |
if trx->state is TRX_STATE_PREPARED when thd == nullptr, and updates the thd |
| 1868 |
with thd_get_current_thd() in such case. */ |
| 1869 |
Gtid_desc gtid_desc{}; |
| 1870 |
if (serialised) { |
| 1871 |
auto >id_persistor = clone_sys->get_gtid_persistor(); |
| 1872 |
gtid_persistor.get_gtid_info(trx, gtid_desc); |
| 1873 |
} |
| 1874 |
|
| 1875 |
if (trx_sys_latch_is_needed) { |
| 1876 |
trx_sys_mutex_enter(); |
| 1877 |
} |
| 1878 |
|
| 1879 |
if (trx->id > 0) { |
| 1880 |
/* For consistent snapshot, we need to remove current |
| 1881 |
transaction from running transaction id list for mvcc |
| 1882 |
before doing commit and releasing locks. */ |
| 1883 |
trx_erase_lists(trx); |
| 1884 |
} |
| 1885 |
|
| 1886 |
if (trx_state_eq(trx, TRX_STATE_PREPARED)) { |
| 1887 |
ut_a(trx_sys->n_prepared_trx > 0); |
| 1888 |
--trx_sys->n_prepared_trx; |
| 1889 |
} |
| 1890 |
|
| 1891 |
if (trx_sys_latch_is_needed) { |
| 1892 |
trx_sys_mutex_exit(); |
| 1893 |
} |
| 1894 |
|
| 1895 |
auto state_transition = [&]() { |
| 1896 |
trx_mutex_enter(trx); |
| 1897 |
/* Please consider this particular point in time as the moment the trx's |
| 1898 |
implicit locks become released. |
| 1899 |
This change is protected by both Trx_shard's mutex and trx->mutex. |
| 1900 |
Therefore, there are two secure ways to check if the trx still can hold |
| 1901 |
implicit locks: |
| 1902 |
(1) if you only know id of the trx, then you can obtain Trx_shard's mutex |
| 1903 |
and check if trx is still in the Trx_shard's active_rw_trxs. This works, |
| 1904 |
because the removal from the active_rw_trxs is also protected by the |
| 1905 |
same mutex. We use this approach in lock_rec_convert_impl_to_expl() by |
| 1906 |
using trx_rw_is_active() |
| 1907 |
(2) if you have pointer to trx, and you know it is safe to access (say, you |
| 1908 |
hold reference to this trx which prevents it from being freed) then you |
| 1909 |
can obtain trx->mutex and check if trx->state is equal to |
| 1910 |
TRX_STATE_COMMITTED_IN_MEMORY. We use this approach in |
| 1911 |
lock_rec_convert_impl_to_expl_for_trx() when deciding for the final time |
| 1912 |
if we really want to create explicit lock on behalf of implicit lock |
| 1913 |
holder. */ |
| 1914 |
trx->state.store(TRX_STATE_COMMITTED_IN_MEMORY, std::memory_order_relaxed); |
| 1915 |
trx_mutex_exit(trx); |
| 1916 |
}; |
| 1917 |
if (trx->id > 0) { |
| 1918 |
trx_sys->get_shard_by_trx_id(trx->id).active_rw_trxs.latch_and_execute( |
| 1919 |
[&](Trx_by_id_with_min &trx_by_id_with_min) { |
| 1920 |
state_transition(); |
| 1921 |
ut_d(const size_t trx_shard_no = trx_get_shard_no(trx->id)); |
| 1922 |
ut_ad(trx_get_shard_no(trx_by_id_with_min.min_id()) == trx_shard_no); |
| 1923 |
trx_by_id_with_min.erase(trx->id); |
| 1924 |
ut_ad(trx_get_shard_no(trx_by_id_with_min.min_id()) == trx_shard_no); |
| 1925 |
}, |
| 1926 |
UT_LOCATION_HERE); |
| 1927 |
} else { |
| 1928 |
state_transition(); |
| 1929 |
} |
| 1930 |
|
| 1931 |
/* It is important to remove the transaction from the serialisation list |
| 1932 |
after it is erased from the rw_trx_ids / rw_trx_list (not before!). |
| 1933 |
Otherwise a read-view could be created, which could still pretend that |
| 1934 |
changes of this transaction are invisible, but related undo records could |
| 1935 |
become purged (because trx->no would no longer protect them). */ |
| 1936 |
|
| 1937 |
if (serialised) { |
| 1938 |
trx_sys_serialisation_mutex_enter(); |
| 1939 |
|
| 1940 |
/* Add GTID to be persisted to disk table. It must be done ... |
| 1941 |
1.After the transaction is marked committed in undo. Otherwise |
| 1942 |
GTID might get committed before the transaction commit on disk. |
| 1943 |
2.Before it is removed from serialization list. Otherwise the transaction |
| 1944 |
undo could get purged before persisting GTID on disk table. */ |
| 1945 |
if (gtid_desc.m_is_set) { |
| 1946 |
auto >id_persistor = clone_sys->get_gtid_persistor(); |
| 1947 |
/* The gtid_persistor.add(gtid_desc) might release and re-acquire |
| 1948 |
the trx_sys_serialisation_mutex, so must be called before trx is |
| 1949 |
removed from the serialisation_list - to satisfy [2]. */ |
| 1950 |
gtid_persistor.add(gtid_desc); |
| 1951 |
} |
| 1952 |
|
| 1953 |
trx_erase_from_serialisation_list_low(trx); |
| 1954 |
|
| 1955 |
trx_sys_serialisation_mutex_exit(); |
| 1956 |
} |
| 1957 |
|
| 1958 |
lock_trx_release_locks(trx); |
| 1959 |
} |
| 1960 |
|
| 1961 |
/** Commits a transaction in memory. */ |
| 1962 |
static void trx_commit_in_memory( |
| 1963 |
trx_t *trx, /*!< in/out: transaction */ |
| 1964 |
const mtr_t *mtr, /*!< in: mini-transaction of |
| 1965 |
trx_write_serialisation_history(), or NULL if |
| 1966 |
the transaction did not modify anything */ |
| 1967 |
bool serialised) |
| 1968 |
/*!< in: true if serialisation log was |
| 1969 |
written */ |
| 1970 |
{ |
| 1971 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 1972 |
|
| 1973 |
trx->must_flush_log_later = false; |
| 1974 |
trx->ddl_must_flush = false; |
| 1975 |
|
| 1976 |
if (trx_is_autocommit_non_locking(trx)) { |
| 1977 |
ut_ad(trx->id == 0); |
| 1978 |
ut_ad(trx->read_only); |
| 1979 |
ut_a(!trx->is_recovered); |
| 1980 |
ut_ad(trx->rsegs.m_redo.rseg == nullptr); |
| 1981 |
ut_ad(!trx->in_rw_trx_list); |
| 1982 |
|
| 1983 |
/* Note: We are asserting without holding the locksys latch. But |
| 1984 |
that is OK because this transaction is not waiting and cannot |
| 1985 |
be rolled back and no new locks can (or should not) be added |
| 1986 |
because it is flagged as a non-locking read-only transaction. */ |
| 1987 |
|
| 1988 |
ut_a(UT_LIST_GET_LEN(trx->lock.trx_locks) == 0); |
| 1989 |
|
| 1990 |
/* This state change is not protected by any mutex, therefore |
| 1991 |
there is an inherent race here around state transition during |
| 1992 |
printouts. We ignore this race for the sake of efficiency. |
| 1993 |
However, the trx_sys_t::mutex will protect the trx_t instance |
| 1994 |
and it cannot be removed from the mysql_trx_list and freed |
| 1995 |
without first acquiring the trx_sys_t::mutex. */ |
| 1996 |
|
| 1997 |
ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE)); |
| 1998 |
|
| 1999 |
if (trx->read_view != nullptr) { |
| 2000 |
trx_sys->mvcc->view_close(trx->read_view, false); |
| 2001 |
} |
| 2002 |
|
| 2003 |
MONITOR_INC(MONITOR_TRX_NL_RO_COMMIT); |
| 2004 |
|
| 2005 |
/* AC-NL-RO transactions can't be rolled back asynchronously. */ |
| 2006 |
ut_ad(!trx->abort); |
| 2007 |
ut_ad(!(trx->in_innodb & TRX_FORCE_ROLLBACK)); |
| 2008 |
|
| 2009 |
trx->state.store(TRX_STATE_NOT_STARTED, std::memory_order_relaxed); |
| 2010 |
|
| 2011 |
} else { |
| 2012 |
trx_release_impl_and_expl_locks(trx, serialised); |
| 2013 |
|
| 2014 |
/* Removed the transaction from the list of active transactions. |
| 2015 |
It no longer holds any user locks. */ |
| 2016 |
|
| 2017 |
ut_ad(trx_state_eq(trx, TRX_STATE_COMMITTED_IN_MEMORY)); |
| 2018 |
DEBUG_SYNC_C("after_trx_committed_in_memory"); |
| 2019 |
|
| 2020 |
if (trx->read_only || trx->rsegs.m_redo.rseg == nullptr) { |
| 2021 |
MONITOR_INC(MONITOR_TRX_RO_COMMIT); |
| 2022 |
if (trx->read_view != nullptr) { |
| 2023 |
trx_sys->mvcc->view_close(trx->read_view, false); |
| 2024 |
} |
| 2025 |
|
| 2026 |
} else { |
| 2027 |
ut_ad(trx->id > 0); |
| 2028 |
MONITOR_INC(MONITOR_TRX_RW_COMMIT); |
| 2029 |
} |
| 2030 |
} |
| 2031 |
|
| 2032 |
/* Reset flag that SE persists GTID. */ |
| 2033 |
auto >id_persistor = clone_sys->get_gtid_persistor(); |
| 2034 |
gtid_persistor.set_persist_gtid(trx, false); |
| 2035 |
|
| 2036 |
if (mtr != nullptr) { |
| 2037 |
if (trx->rsegs.m_redo.insert_undo != nullptr) { |
| 2038 |
trx_undo_insert_cleanup(&trx->rsegs.m_redo, false); |
| 2039 |
} |
| 2040 |
|
| 2041 |
if (trx->rsegs.m_noredo.insert_undo != nullptr) { |
| 2042 |
trx_undo_insert_cleanup(&trx->rsegs.m_noredo, true); |
| 2043 |
} |
| 2044 |
|
| 2045 |
/* NOTE that we could possibly make a group commit more |
| 2046 |
efficient here: call std::this_thread::yield() here to allow also other |
| 2047 |
trxs to come to commit! */ |
| 2048 |
|
| 2049 |
/*-------------------------------------*/ |
| 2050 |
|
| 2051 |
/* Depending on the my.cnf options, we may now write the log |
| 2052 |
buffer to the log files, making the transaction durable if |
| 2053 |
the OS does not crash. We may also flush the log files to |
| 2054 |
disk, making the transaction durable also at an OS crash or a |
| 2055 |
power outage. |
| 2056 |
|
| 2057 |
The idea in InnoDB's group commit is that a group of |
| 2058 |
transactions gather behind a trx doing a physical disk write |
| 2059 |
to log files, and when that physical write has been completed, |
| 2060 |
one of those transactions does a write which commits the whole |
| 2061 |
group. Note that this group commit will only bring benefit if |
| 2062 |
there are > 2 users in the database. Then at least 2 users can |
| 2063 |
gather behind one doing the physical log write to disk. |
| 2064 |
|
| 2065 |
If we are calling trx_commit() under prepare_commit_mutex, we |
| 2066 |
will delay possible log write and flush to a separate function |
| 2067 |
trx_commit_complete_for_mysql(), which is only called when the |
| 2068 |
thread has released the mutex. This is to make the |
| 2069 |
group commit algorithm to work. Otherwise, the prepare_commit |
| 2070 |
mutex would serialize all commits and prevent a group of |
| 2071 |
transactions from gathering. */ |
| 2072 |
|
| 2073 |
lsn_t lsn = mtr->commit_lsn(); |
| 2074 |
|
| 2075 |
if (lsn == 0) { |
| 2076 |
/* Nothing to be done. */ |
| 2077 |
} else if (trx->flush_log_later) { |
| 2078 |
/* Do nothing yet */ |
| 2079 |
trx->must_flush_log_later = true; |
| 2080 |
|
| 2081 |
/* Remember current ddl_operation, because trx_init() |
| 2082 |
later will set ddl_operation to false. And the final |
| 2083 |
flush is even later. */ |
| 2084 |
trx->ddl_must_flush = trx->ddl_operation; |
| 2085 |
} else if ((srv_flush_log_at_trx_commit == 0 || |
| 2086 |
thd_requested_durability(trx->mysql_thd) == |
| 2087 |
HA_IGNORE_DURABILITY) && |
| 2088 |
(!trx->ddl_operation)) { |
| 2089 |
/* Do nothing */ |
| 2090 |
} else { |
| 2091 |
trx_flush_log_if_needed(lsn, trx); |
| 2092 |
} |
| 2093 |
|
| 2094 |
trx->commit_lsn = lsn; |
| 2095 |
|
| 2096 |
/* Tell server some activity has happened, since the trx |
| 2097 |
does changes something. Background utility threads like |
| 2098 |
master thread, purge thread or page_cleaner thread might |
| 2099 |
have some work to do. */ |
| 2100 |
srv_active_wake_master_thread(); |
| 2101 |
} |
| 2102 |
|
| 2103 |
/* Do not decrement the reference count before this point. |
| 2104 |
There is a potential issue where a thread attempting to drop |
| 2105 |
an undo tablespace may end up dropping this undo space |
| 2106 |
before this thread can complete the cleanup. |
| 2107 |
While marking a undo space as inactive, the server tries |
| 2108 |
to find if any transaction is actively using the undo log |
| 2109 |
being truncated. A non-zero reference count ensures that the |
| 2110 |
thread attempting to truncate/drop the undo tablespace |
| 2111 |
cannot be successful as the undo log cannot be dropped until |
| 2112 |
is it empty. */ |
| 2113 |
if (trx->rsegs.m_redo.rseg != nullptr) { |
| 2114 |
trx_rseg_t *rseg = trx->rsegs.m_redo.rseg; |
| 2115 |
ut_ad(rseg->trx_ref_count > 0); |
| 2116 |
|
| 2117 |
/* Multiple transactions can simultaneously decrement |
| 2118 |
the atomic counter. */ |
| 2119 |
rseg->trx_ref_count--; |
| 2120 |
|
| 2121 |
trx->rsegs.m_redo.rseg = nullptr; |
| 2122 |
} |
| 2123 |
|
| 2124 |
/* Free all savepoints, starting from the first. */ |
| 2125 |
trx_named_savept_t *savep = UT_LIST_GET_FIRST(trx->trx_savepoints); |
| 2126 |
|
| 2127 |
trx_roll_savepoints_free(trx, savep); |
| 2128 |
|
| 2129 |
if (trx->fts_trx != nullptr) { |
| 2130 |
trx_finalize_for_fts(trx, trx->undo_no != 0); |
| 2131 |
} |
| 2132 |
|
| 2133 |
trx_mutex_enter(trx); |
| 2134 |
trx->dict_operation = TRX_DICT_OP_NONE; |
| 2135 |
|
| 2136 |
/* Because we can rollback transactions asynchronously, we change |
| 2137 |
the state at the last step. trx_t::abort cannot change once commit |
| 2138 |
or rollback has started because we will have released the locks by |
| 2139 |
the time we get here. */ |
| 2140 |
|
| 2141 |
if (trx->abort) { |
| 2142 |
trx->abort = false; |
| 2143 |
trx->state.store(TRX_STATE_FORCED_ROLLBACK, std::memory_order_relaxed); |
| 2144 |
} else { |
| 2145 |
trx->state.store(TRX_STATE_NOT_STARTED, std::memory_order_relaxed); |
| 2146 |
} |
| 2147 |
|
| 2148 |
/* trx->in_mysql_trx_list would hold between |
| 2149 |
trx_allocate_for_mysql() and trx_free_for_mysql(). It does not |
| 2150 |
hold for recovered transactions or system transactions. */ |
| 2151 |
assert_trx_is_free(trx); |
| 2152 |
|
| 2153 |
trx_init(trx); |
| 2154 |
|
| 2155 |
trx_mutex_exit(trx); |
| 2156 |
|
| 2157 |
ut_a(trx->error_state == DB_SUCCESS); |
| 2158 |
} |
| 2159 |
|
| 2160 |
/** Commits a transaction and a mini-transaction. |
| 2161 |
@param[in,out] trx Transaction |
| 2162 |
@param[in,out] mtr Mini-transaction (will be committed), or null if trx made no |
| 2163 |
modifications */ |
| 2164 |
void trx_commit_low(trx_t *trx, mtr_t *mtr) { |
| 2165 |
assert_trx_nonlocking_or_in_list(trx); |
| 2166 |
ut_ad(!trx_state_eq(trx, TRX_STATE_COMMITTED_IN_MEMORY)); |
| 2167 |
ut_ad(!mtr || mtr->is_active()); |
| 2168 |
/* undo_no is non-zero if we're doing the final commit. */ |
| 2169 |
if (trx->fts_trx != nullptr && trx->undo_no != 0 && |
| 2170 |
trx->lock.que_state != TRX_QUE_ROLLING_BACK) { |
| 2171 |
dberr_t error; |
| 2172 |
|
| 2173 |
ut_a(!trx_is_autocommit_non_locking(trx)); |
| 2174 |
|
| 2175 |
error = fts_commit(trx); |
| 2176 |
|
| 2177 |
/* FTS-FIXME: Temporarily tolerate DB_DUPLICATE_KEY |
| 2178 |
instead of dying. This is a possible scenario if there |
| 2179 |
is a crash between insert to DELETED table committing |
| 2180 |
and transaction committing. The fix would be able to |
| 2181 |
return error from this function */ |
| 2182 |
if (error != DB_SUCCESS && error != DB_DUPLICATE_KEY) { |
| 2183 |
/* FTS-FIXME: once we can return values from this |
| 2184 |
function, we should do so and signal an error |
| 2185 |
instead of just dying. */ |
| 2186 |
|
| 2187 |
ut_error; |
| 2188 |
} |
| 2189 |
} |
| 2190 |
|
| 2191 |
bool serialised; |
| 2192 |
|
| 2193 |
if (mtr != nullptr) { |
| 2194 |
mtr->set_sync(); |
| 2195 |
|
| 2196 |
DEBUG_SYNC_C("trx_sys_before_assign_no"); |
| 2197 |
|
| 2198 |
serialised = trx_write_serialisation_history(trx, mtr); |
| 2199 |
|
| 2200 |
/* The following call commits the mini-transaction, making the |
| 2201 |
whole transaction committed in the file-based world, at this |
| 2202 |
log sequence number. The transaction becomes 'durable' when |
| 2203 |
we write the log to disk, but in the logical sense the commit |
| 2204 |
in the file-based data structures (undo logs etc.) happens |
| 2205 |
here. |
| 2206 |
|
| 2207 |
NOTE that transaction numbers, which are assigned only to |
| 2208 |
transactions with an update undo log, do not necessarily come |
| 2209 |
in exactly the same order as commit lsn's, if the transactions |
| 2210 |
have different rollback segments. To get exactly the same |
| 2211 |
order we should hold the kernel mutex up to this point, |
| 2212 |
adding to the contention of the kernel mutex. However, if |
| 2213 |
a transaction T2 is able to see modifications made by |
| 2214 |
a transaction T1, T2 will always get a bigger transaction |
| 2215 |
number and a bigger commit lsn than T1. */ |
| 2216 |
|
| 2217 |
/*--------------*/ |
| 2218 |
|
| 2219 |
DBUG_EXECUTE_IF("trx_commit_to_the_end_of_log_block", { |
| 2220 |
const size_t space_left = mtr->get_expected_log_size(); |
| 2221 |
mtr_commit_mlog_test_filling_block(*log_sys, space_left); |
| 2222 |
}); |
| 2223 |
|
| 2224 |
mtr_commit(mtr); |
| 2225 |
|
| 2226 |
DBUG_PRINT("trx_commit", ("commit lsn at " LSN_PF, mtr->commit_lsn())); |
| 2227 |
|
| 2228 |
DBUG_EXECUTE_IF( |
| 2229 |
"ib_crash_during_trx_commit_in_mem", if (trx_is_rseg_updated(trx)) { |
| 2230 |
log_make_latest_checkpoint(); |
| 2231 |
DBUG_SUICIDE(); |
| 2232 |
}); |
| 2233 |
/*--------------*/ |
| 2234 |
|
| 2235 |
} else { |
| 2236 |
serialised = false; |
| 2237 |
} |
| 2238 |
#ifdef UNIV_DEBUG |
| 2239 |
/* In case of this function is called from a stack executing |
| 2240 |
THD::release_resources -> ... |
| 2241 |
innobase_connection_close() -> |
| 2242 |
trx_rollback_for_mysql... -> . |
| 2243 |
mysql's thd does not seem to have |
| 2244 |
thd->debug_sync_control defined any longer. However the stack |
| 2245 |
is possible only with a prepared trx not updating any data. |
| 2246 |
*/ |
| 2247 |
if (trx->mysql_thd != nullptr && trx_is_redo_rseg_updated(trx)) { |
| 2248 |
DEBUG_SYNC_C("before_trx_state_committed_in_memory"); |
| 2249 |
} |
| 2250 |
#endif |
| 2251 |
|
| 2252 |
trx_commit_in_memory(trx, mtr, serialised); |
| 2253 |
} |
| 2254 |
|
| 2255 |
/** Commits a transaction. */ |
| 2256 |
void trx_commit(trx_t *trx) /*!< in/out: transaction */ |
| 2257 |
{ |
| 2258 |
mtr_t *mtr; |
| 2259 |
mtr_t local_mtr; |
| 2260 |
|
| 2261 |
DBUG_EXECUTE_IF("ib_trx_commit_crash_before_trx_commit_start", |
| 2262 |
DBUG_SUICIDE();); |
| 2263 |
|
| 2264 |
if (trx_is_rseg_updated(trx)) { |
| 2265 |
mtr = &local_mtr; |
| 2266 |
|
| 2267 |
DBUG_EXECUTE_IF("ib_trx_commit_crash_rseg_updated", DBUG_SUICIDE();); |
| 2268 |
|
| 2269 |
mtr_start_sync(mtr); |
| 2270 |
|
| 2271 |
} else { |
| 2272 |
mtr = nullptr; |
| 2273 |
} |
| 2274 |
|
| 2275 |
trx_commit_low(trx, mtr); |
| 2276 |
} |
| 2277 |
|
| 2278 |
/** Cleans up a transaction at database startup. The cleanup is needed if |
| 2279 |
the transaction already got to the middle of a commit when the database |
| 2280 |
crashed, and we cannot roll it back. */ |
| 2281 |
void trx_cleanup_at_db_startup(trx_t *trx) /*!< in: transaction */ |
| 2282 |
{ |
| 2283 |
ut_ad(trx->is_recovered); |
| 2284 |
|
| 2285 |
/* Cleanup any durable undo logs in non-temporary rollback segments. |
| 2286 |
At database start-up there are no active transactions recorded in |
| 2287 |
any rollback segments in the temporary tablespace because all those |
| 2288 |
changes are all lost on restart. */ |
| 2289 |
if (trx->rsegs.m_redo.insert_undo != nullptr) { |
| 2290 |
trx_undo_insert_cleanup(&trx->rsegs.m_redo, false); |
| 2291 |
} |
| 2292 |
|
| 2293 |
memset(&trx->rsegs, 0x0, sizeof(trx->rsegs)); |
| 2294 |
trx->undo_no = 0; |
| 2295 |
trx->undo_rseg_space = 0; |
| 2296 |
trx->last_sql_stat_start.least_undo_no = 0; |
| 2297 |
|
| 2298 |
trx_sys_mutex_enter(); |
| 2299 |
|
| 2300 |
ut_a(!trx->read_only); |
| 2301 |
trx_remove_from_rw_trx_list(trx); |
| 2302 |
|
| 2303 |
trx_sys_mutex_exit(); |
| 2304 |
|
| 2305 |
/* Change the transaction state without mutex protection, now |
| 2306 |
that it no longer is in the trx_list. Recovered transactions |
| 2307 |
are never placed in the mysql_trx_list. */ |
| 2308 |
ut_ad(trx->is_recovered); |
| 2309 |
ut_ad(!trx->in_rw_trx_list); |
| 2310 |
ut_ad(!trx->in_mysql_trx_list); |
| 2311 |
trx->state.store(TRX_STATE_NOT_STARTED, std::memory_order_relaxed); |
| 2312 |
} |
| 2313 |
|
| 2314 |
/** Assigns a read view for a consistent read query. All the consistent reads |
| 2315 |
within the same transaction will get the same read view, which is created |
| 2316 |
when this function is first called for a new started transaction. |
| 2317 |
@return consistent read view */ |
| 2318 |
ReadView *trx_assign_read_view(trx_t *trx) /*!< in/out: active transaction */ |
| 2319 |
{ |
| 2320 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 2321 |
ut_ad(trx->state.load(std::memory_order_relaxed) == TRX_STATE_ACTIVE); |
| 2322 |
|
| 2323 |
if (srv_read_only_mode) { |
| 2324 |
ut_ad(trx->read_view == nullptr); |
| 2325 |
return (nullptr); |
| 2326 |
|
| 2327 |
} else if (!MVCC::is_view_active(trx->read_view)) { |
| 2328 |
trx_sys->mvcc->view_open(trx->read_view, trx); |
| 2329 |
} |
| 2330 |
|
| 2331 |
return (trx->read_view); |
| 2332 |
} |
| 2333 |
|
| 2334 |
/** Prepares a transaction for commit/rollback. */ |
| 2335 |
void trx_commit_or_rollback_prepare(trx_t *trx) /*!< in/out: transaction */ |
| 2336 |
{ |
| 2337 |
/* We are reading trx->state without mutex protection here, |
| 2338 |
because the rollback should either be invoked for: |
| 2339 |
- a running active MySQL transaction associated |
| 2340 |
with the current thread, |
| 2341 |
- or a recovered prepared transaction, |
| 2342 |
- or a transaction which is a victim being killed by HP transaction |
| 2343 |
run by the current thread, in which case it is guaranteed that |
| 2344 |
thread owning the transaction, which is being killed, is not |
| 2345 |
inside InnoDB (thanks to TRX_FORCE_ROLLBACK and TrxInInnoDB::wait()). */ |
| 2346 |
|
| 2347 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 2348 |
|
| 2349 |
switch (trx->state.load(std::memory_order_relaxed)) { |
| 2350 |
case TRX_STATE_NOT_STARTED: |
| 2351 |
case TRX_STATE_FORCED_ROLLBACK: |
| 2352 |
|
| 2353 |
trx_start_low(trx, true); |
| 2354 |
[[fallthrough]]; |
| 2355 |
|
| 2356 |
case TRX_STATE_ACTIVE: |
| 2357 |
case TRX_STATE_PREPARED: |
| 2358 |
|
| 2359 |
/* If the trx is in a lock wait state, moves the waiting |
| 2360 |
query thread to the suspended state */ |
| 2361 |
|
| 2362 |
if (trx->lock.que_state == TRX_QUE_LOCK_WAIT) { |
| 2363 |
ut_a(trx->lock.wait_thr != nullptr); |
| 2364 |
trx->lock.wait_thr->state = QUE_THR_SUSPENDED; |
| 2365 |
trx->lock.wait_thr = nullptr; |
| 2366 |
|
| 2367 |
trx->lock.que_state = TRX_QUE_RUNNING; |
| 2368 |
} |
| 2369 |
|
| 2370 |
ut_a(trx->lock.n_active_thrs == 1); |
| 2371 |
return; |
| 2372 |
|
| 2373 |
case TRX_STATE_COMMITTED_IN_MEMORY: |
| 2374 |
break; |
| 2375 |
} |
| 2376 |
|
| 2377 |
ut_error; |
| 2378 |
} |
| 2379 |
|
| 2380 |
/** Creates a commit command node struct. |
| 2381 |
@return own: commit node struct */ |
| 2382 |
commit_node_t *trx_commit_node_create( |
| 2383 |
mem_heap_t *heap) /*!< in: mem heap where created */ |
| 2384 |
{ |
| 2385 |
commit_node_t *node; |
| 2386 |
|
| 2387 |
node = static_cast<commit_node_t *>(mem_heap_alloc(heap, sizeof(*node))); |
| 2388 |
node->common.type = QUE_NODE_COMMIT; |
| 2389 |
node->state = COMMIT_NODE_SEND; |
| 2390 |
|
| 2391 |
return (node); |
| 2392 |
} |
| 2393 |
|
| 2394 |
/** Performs an execution step for a commit type node in a query graph. |
| 2395 |
@return query thread to run next, or NULL */ |
| 2396 |
que_thr_t *trx_commit_step(que_thr_t *thr) /*!< in: query thread */ |
| 2397 |
{ |
| 2398 |
commit_node_t *node; |
| 2399 |
|
| 2400 |
node = static_cast<commit_node_t *>(thr->run_node); |
| 2401 |
|
| 2402 |
ut_ad(que_node_get_type(node) == QUE_NODE_COMMIT); |
| 2403 |
|
| 2404 |
if (thr->prev_node == que_node_get_parent(node)) { |
| 2405 |
node->state = COMMIT_NODE_SEND; |
| 2406 |
} |
| 2407 |
|
| 2408 |
if (node->state == COMMIT_NODE_SEND) { |
| 2409 |
trx_t *trx; |
| 2410 |
|
| 2411 |
node->state = COMMIT_NODE_WAIT; |
| 2412 |
|
| 2413 |
trx = thr_get_trx(thr); |
| 2414 |
|
| 2415 |
ut_a(trx->lock.wait_thr == nullptr); |
| 2416 |
ut_a(trx->lock.que_state != TRX_QUE_LOCK_WAIT); |
| 2417 |
|
| 2418 |
trx_commit_or_rollback_prepare(trx); |
| 2419 |
|
| 2420 |
trx->lock.que_state = TRX_QUE_COMMITTING; |
| 2421 |
|
| 2422 |
trx_commit(trx); |
| 2423 |
|
| 2424 |
ut_ad(trx->lock.wait_thr == nullptr); |
| 2425 |
|
| 2426 |
trx->lock.que_state = TRX_QUE_RUNNING; |
| 2427 |
|
| 2428 |
thr = nullptr; |
| 2429 |
} else { |
| 2430 |
ut_ad(node->state == COMMIT_NODE_WAIT); |
| 2431 |
|
| 2432 |
node->state = COMMIT_NODE_SEND; |
| 2433 |
|
| 2434 |
thr->run_node = que_node_get_parent(node); |
| 2435 |
} |
| 2436 |
|
| 2437 |
return (thr); |
| 2438 |
} |
| 2439 |
|
| 2440 |
/** Does the transaction commit for MySQL. |
| 2441 |
@return DB_SUCCESS or error number */ |
| 2442 |
dberr_t trx_commit_for_mysql(trx_t *trx) /*!< in/out: transaction */ |
| 2443 |
{ |
| 2444 |
DEBUG_SYNC_C("trx_commit_for_mysql_checks_for_aborted"); |
| 2445 |
TrxInInnoDB trx_in_innodb(trx, true); |
| 2446 |
|
| 2447 |
if (trx_in_innodb.is_aborted() && |
| 2448 |
trx->killed_by != std::this_thread::get_id()) { |
| 2449 |
return (DB_FORCED_ABORT); |
| 2450 |
} |
| 2451 |
|
| 2452 |
/* Because we do not do the commit by sending an Innobase |
| 2453 |
sig to the transaction, we must here make sure that trx has been |
| 2454 |
started. */ |
| 2455 |
|
| 2456 |
dberr_t db_err = DB_SUCCESS; |
| 2457 |
|
| 2458 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 2459 |
|
| 2460 |
switch (trx->state.load(std::memory_order_relaxed)) { |
| 2461 |
case TRX_STATE_NOT_STARTED: |
| 2462 |
case TRX_STATE_FORCED_ROLLBACK: |
| 2463 |
|
| 2464 |
ut_d(trx->start_file = __FILE__); |
| 2465 |
ut_d(trx->start_line = __LINE__); |
| 2466 |
|
| 2467 |
trx_start_low(trx, true); |
| 2468 |
[[fallthrough]]; |
| 2469 |
case TRX_STATE_ACTIVE: |
| 2470 |
case TRX_STATE_PREPARED: |
| 2471 |
trx->op_info = "committing"; |
| 2472 |
|
| 2473 |
/* For GTID persistence we need update undo segment. */ |
| 2474 |
db_err = trx_undo_gtid_add_update_undo(trx, false, false); |
| 2475 |
if (db_err != DB_SUCCESS) { |
| 2476 |
return (db_err); |
| 2477 |
} |
| 2478 |
|
| 2479 |
if (trx->id != 0) { |
| 2480 |
trx_update_mod_tables_timestamp(trx); |
| 2481 |
} |
| 2482 |
|
| 2483 |
trx_commit(trx); |
| 2484 |
|
| 2485 |
MONITOR_DEC(MONITOR_TRX_ACTIVE); |
| 2486 |
trx->op_info = ""; |
| 2487 |
return (DB_SUCCESS); |
| 2488 |
case TRX_STATE_COMMITTED_IN_MEMORY: |
| 2489 |
break; |
| 2490 |
} |
| 2491 |
ut_error; |
| 2492 |
return (DB_CORRUPTION); |
| 2493 |
} |
| 2494 |
|
| 2495 |
/** If required, flushes the log to disk if we called trx_commit_for_mysql() |
| 2496 |
with trx->flush_log_later == true. */ |
| 2497 |
void trx_commit_complete_for_mysql(trx_t *trx) /*!< in/out: transaction */ |
| 2498 |
{ |
| 2499 |
if (trx->id != 0 || !trx->must_flush_log_later || |
| 2500 |
(thd_requested_durability(trx->mysql_thd) == HA_IGNORE_DURABILITY && |
| 2501 |
!trx->ddl_must_flush)) { |
| 2502 |
/* If we removed trx->ddl_must_flush from condition above, we would |
| 2503 |
need to take care of fixing innobase_flush_logs for a scenario in |
| 2504 |
which srv_flush_log_at_trx_commit == 0. */ |
| 2505 |
return; |
| 2506 |
} |
| 2507 |
|
| 2508 |
trx_flush_log_if_needed(trx->commit_lsn, trx); |
| 2509 |
|
| 2510 |
trx->must_flush_log_later = false; |
| 2511 |
trx->ddl_must_flush = false; |
| 2512 |
} |
| 2513 |
|
| 2514 |
/** Marks the latest SQL statement ended. */ |
| 2515 |
void trx_mark_sql_stat_end(trx_t *trx) /*!< in: trx handle */ |
| 2516 |
{ |
| 2517 |
ut_a(trx); |
| 2518 |
|
| 2519 |
lock_on_statement_end(trx); |
| 2520 |
|
| 2521 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 2522 |
|
| 2523 |
switch (trx->state.load(std::memory_order_relaxed)) { |
| 2524 |
case TRX_STATE_PREPARED: |
| 2525 |
case TRX_STATE_COMMITTED_IN_MEMORY: |
| 2526 |
break; |
| 2527 |
case TRX_STATE_NOT_STARTED: |
| 2528 |
case TRX_STATE_FORCED_ROLLBACK: |
| 2529 |
trx->undo_no = 0; |
| 2530 |
trx->undo_rseg_space = 0; |
| 2531 |
[[fallthrough]]; |
| 2532 |
case TRX_STATE_ACTIVE: |
| 2533 |
trx->last_sql_stat_start.least_undo_no = trx->undo_no; |
| 2534 |
|
| 2535 |
if (trx->fts_trx != nullptr) { |
| 2536 |
fts_savepoint_laststmt_refresh(trx); |
| 2537 |
} |
| 2538 |
|
| 2539 |
return; |
| 2540 |
} |
| 2541 |
|
| 2542 |
ut_error; |
| 2543 |
} |
| 2544 |
|
| 2545 |
/** Prints info about a transaction. |
| 2546 |
Caller must hold trx_sys->mutex. */ |
| 2547 |
void trx_print_low(FILE *f, |
| 2548 |
/*!< in: output stream */ |
| 2549 |
const trx_t *trx, |
| 2550 |
/*!< in: transaction */ |
| 2551 |
ulint max_query_len, |
| 2552 |
/*!< in: max query length to print, |
| 2553 |
must be positive */ |
| 2554 |
ulint n_rec_locks, |
| 2555 |
/*!< in: lock_number_of_rows_locked(&trx->lock) */ |
| 2556 |
ulint n_trx_locks, |
| 2557 |
/*!< in: length of trx->lock.trx_locks */ |
| 2558 |
ulint heap_size) |
| 2559 |
/*!< in: mem_heap_get_size(trx->lock.lock_heap) */ |
| 2560 |
{ |
| 2561 |
bool newline; |
| 2562 |
const char *op_info; |
| 2563 |
|
| 2564 |
ut_ad(trx_sys_mutex_own()); |
| 2565 |
|
| 2566 |
fprintf(f, "TRANSACTION " TRX_ID_FMT, trx_get_id_for_print(trx)); |
| 2567 |
|
| 2568 |
const auto trx_state = trx->state.load(std::memory_order_relaxed); |
| 2569 |
|
| 2570 |
switch (trx_state) { |
| 2571 |
case TRX_STATE_NOT_STARTED: |
| 2572 |
fputs(", not started", f); |
| 2573 |
break; |
| 2574 |
case TRX_STATE_FORCED_ROLLBACK: |
| 2575 |
fputs(", forced rollback", f); |
| 2576 |
break; |
| 2577 |
case TRX_STATE_ACTIVE: |
| 2578 |
fprintf(f, ", ACTIVE %lu sec", |
| 2579 |
(ulong)std::chrono::duration_cast<std::chrono::seconds>( |
| 2580 |
std::chrono::system_clock::now() - |
| 2581 |
trx->start_time.load(std::memory_order_relaxed)) |
| 2582 |
.count()); |
| 2583 |
break; |
| 2584 |
case TRX_STATE_PREPARED: |
| 2585 |
fprintf(f, ", ACTIVE (PREPARED) %lu sec", |
| 2586 |
(ulong)std::chrono::duration_cast<std::chrono::seconds>( |
| 2587 |
std::chrono::system_clock::now() - |
| 2588 |
trx->start_time.load(std::memory_order_relaxed)) |
| 2589 |
.count()); |
| 2590 |
break; |
| 2591 |
case TRX_STATE_COMMITTED_IN_MEMORY: |
| 2592 |
fputs(", COMMITTED IN MEMORY", f); |
| 2593 |
break; |
| 2594 |
default: |
| 2595 |
fprintf(f, ", state %lu", static_cast<ulong>(trx_state)); |
| 2596 |
ut_d(ut_error); |
| 2597 |
ut_o(break); |
| 2598 |
} |
| 2599 |
|
| 2600 |
/* prevent a race condition */ |
| 2601 |
op_info = trx->op_info; |
| 2602 |
|
| 2603 |
if (*op_info) { |
| 2604 |
putc(' ', f); |
| 2605 |
fputs(op_info, f); |
| 2606 |
} |
| 2607 |
|
| 2608 |
if (trx->is_recovered) { |
| 2609 |
fputs(" recovered trx", f); |
| 2610 |
} |
| 2611 |
|
| 2612 |
if (trx->declared_to_be_inside_innodb) { |
| 2613 |
fprintf(f, ", thread declared inside InnoDB %lu", |
| 2614 |
(ulong)trx->n_tickets_to_enter_innodb); |
| 2615 |
} |
| 2616 |
|
| 2617 |
putc('\n', f); |
| 2618 |
|
| 2619 |
if (trx->n_mysql_tables_in_use > 0 || trx->mysql_n_tables_locked > 0) { |
| 2620 |
fprintf(f, "mysql tables in use %lu, locked %lu\n", |
| 2621 |
(ulong)trx->n_mysql_tables_in_use, |
| 2622 |
(ulong)trx->mysql_n_tables_locked); |
| 2623 |
} |
| 2624 |
|
| 2625 |
newline = true; |
| 2626 |
|
| 2627 |
/* trx->lock.que_state of an ACTIVE transaction may change |
| 2628 |
while we are not holding trx->mutex. We perform a dirty read |
| 2629 |
for performance reasons. */ |
| 2630 |
|
| 2631 |
switch (trx->lock.que_state) { |
| 2632 |
case TRX_QUE_RUNNING: |
| 2633 |
newline = false; |
| 2634 |
break; |
| 2635 |
case TRX_QUE_LOCK_WAIT: |
| 2636 |
fputs("LOCK WAIT ", f); |
| 2637 |
break; |
| 2638 |
case TRX_QUE_ROLLING_BACK: |
| 2639 |
fputs("ROLLING BACK ", f); |
| 2640 |
break; |
| 2641 |
case TRX_QUE_COMMITTING: |
| 2642 |
fputs("COMMITTING ", f); |
| 2643 |
break; |
| 2644 |
default: |
| 2645 |
fprintf(f, "que state %lu ", (ulong)trx->lock.que_state); |
| 2646 |
} |
| 2647 |
|
| 2648 |
if (n_trx_locks > 0 || heap_size > 400) { |
| 2649 |
newline = true; |
| 2650 |
|
| 2651 |
fprintf(f, |
| 2652 |
"%lu lock struct(s), heap size %lu," |
| 2653 |
" %lu row lock(s)", |
| 2654 |
(ulong)n_trx_locks, (ulong)heap_size, (ulong)n_rec_locks); |
| 2655 |
} |
| 2656 |
|
| 2657 |
if (trx->has_search_latch) { |
| 2658 |
newline = true; |
| 2659 |
fputs(", holds adaptive hash latch", f); |
| 2660 |
} |
| 2661 |
|
| 2662 |
if (trx->undo_no != 0) { |
| 2663 |
newline = true; |
| 2664 |
fprintf(f, ", undo log entries " TRX_ID_FMT, trx->undo_no); |
| 2665 |
} |
| 2666 |
|
| 2667 |
if (newline) { |
| 2668 |
putc('\n', f); |
| 2669 |
} |
| 2670 |
|
| 2671 |
if (trx_state != TRX_STATE_NOT_STARTED && trx->mysql_thd != nullptr) { |
| 2672 |
innobase_mysql_print_thd(f, trx->mysql_thd, |
| 2673 |
static_cast<uint>(max_query_len)); |
| 2674 |
} |
| 2675 |
} |
| 2676 |
|
| 2677 |
void trx_print_latched(FILE *f, const trx_t *trx, ulint max_query_len) { |
| 2678 |
/* We need exclusive access to lock_sys for lock_number_of_rows_locked(), |
| 2679 |
and accessing trx->lock fields without trx->mutex.*/ |
| 2680 |
ut_ad(locksys::owns_exclusive_global_latch()); |
| 2681 |
ut_ad(trx_sys_mutex_own()); |
| 2682 |
|
| 2683 |
trx_print_low(f, trx, max_query_len, lock_number_of_rows_locked(&trx->lock), |
| 2684 |
UT_LIST_GET_LEN(trx->lock.trx_locks), |
| 2685 |
mem_heap_get_size(trx->lock.lock_heap)); |
| 2686 |
} |
| 2687 |
|
| 2688 |
void trx_print(FILE *f, const trx_t *trx, ulint max_query_len) { |
| 2689 |
/* trx_print_latched() requires exclusive global latch */ |
| 2690 |
locksys::Global_exclusive_latch_guard guard{UT_LOCATION_HERE}; |
| 2691 |
mutex_enter(&trx_sys->mutex); |
| 2692 |
trx_print_latched(f, trx, max_query_len); |
| 2693 |
mutex_exit(&trx_sys->mutex); |
| 2694 |
} |
| 2695 |
|
| 2696 |
#ifdef UNIV_DEBUG |
| 2697 |
bool trx_can_be_handled_by_current_thread(const trx_t *trx) { |
| 2698 |
return trx->mysql_thd == nullptr || trx->mysql_thd == current_thd || |
| 2699 |
/* THD::restore_globals() set current_thd to nullptr and it is called |
| 2700 |
in Table_access::~Table_access() before THD::release_resources(). |
| 2701 |
On the other hand THD::release_resources() calls ha_close_connection(), |
| 2702 |
which needs to enter InnoDB (then assertion based on this function |
| 2703 |
wants to validate that the thread has permission to enter). Until |
| 2704 |
Table_access::~Table_access() is fixed, we disable this assertion |
| 2705 |
when current_thd == nullptr. */ |
| 2706 |
current_thd == nullptr; |
| 2707 |
} |
| 2708 |
|
| 2709 |
bool trx_can_be_handled_by_current_thread_or_is_hp_victim(const trx_t *trx) { |
| 2710 |
return trx_can_be_handled_by_current_thread(trx) || |
| 2711 |
trx->killed_by.load() == std::this_thread::get_id(); |
| 2712 |
} |
| 2713 |
|
| 2714 |
/** Asserts that a transaction has been started. |
| 2715 |
The caller must hold trx_sys->mutex. |
| 2716 |
@return true if started */ |
| 2717 |
bool trx_assert_started(const trx_t *trx) /*!< in: transaction */ |
| 2718 |
{ |
| 2719 |
ut_ad(trx_sys_mutex_own()); |
| 2720 |
|
| 2721 |
/* Non-locking autocommits should not hold any locks and this |
| 2722 |
function is only called from the locking code. */ |
| 2723 |
check_trx_state(trx); |
| 2724 |
|
| 2725 |
/* trx->state can change from or to NOT_STARTED while we are holding |
| 2726 |
trx_sys->mutex for non-locking autocommit selects but not for other |
| 2727 |
types of transactions. It may change from ACTIVE to PREPARED. */ |
| 2728 |
|
| 2729 |
switch (trx->state.load(std::memory_order_relaxed)) { |
| 2730 |
case TRX_STATE_PREPARED: |
| 2731 |
return true; |
| 2732 |
|
| 2733 |
case TRX_STATE_ACTIVE: |
| 2734 |
case TRX_STATE_COMMITTED_IN_MEMORY: |
| 2735 |
return true; |
| 2736 |
|
| 2737 |
case TRX_STATE_NOT_STARTED: |
| 2738 |
case TRX_STATE_FORCED_ROLLBACK: |
| 2739 |
break; |
| 2740 |
} |
| 2741 |
|
| 2742 |
ut_error; |
| 2743 |
} |
| 2744 |
|
| 2745 |
/* |
| 2746 |
Interaction between Lock-sys and trx->mutex-es is rather complicated. |
| 2747 |
In particular we allow a thread performing Lock-sys operations to request |
| 2748 |
another trx->mutex even though it already holds one for a different trx. |
| 2749 |
Therefore one has to prove that it is impossible to form a deadlock cycle in the |
| 2750 |
imaginary wait-for-graph in which edges go from thread trying to obtain |
| 2751 |
trx->mutex to a thread which holds it at the moment. |
| 2752 |
|
| 2753 |
In the past it was simple, because Lock-sys was protected by a global mutex, |
| 2754 |
which meant that there was at most one thread which could try to posses more |
| 2755 |
than one trx->mutex - one can not form a cycle in a graph in which only |
| 2756 |
one node has both incoming and outgoing edges. |
| 2757 |
|
| 2758 |
Today it is much harder to prove, because we have sharded the Lock-sys mutex, |
| 2759 |
and now multiple threads can perform Lock-sys operations in parallel, as long |
| 2760 |
as they happen in different shards. |
| 2761 |
|
| 2762 |
Here's my attempt at the proof. |
| 2763 |
|
| 2764 |
Assumption 1. |
| 2765 |
If a thread attempts to acquire more then one trx->mutex, then it either has |
| 2766 |
exclusive global latch, or it attempts to acquire exactly two of them, and at |
| 2767 |
just before calling mutex_enter for the second time it saw |
| 2768 |
1.1 trx1->lock.wait_lock==nullptr or it held the latch for the shard |
| 2769 |
containing the trx1->lock.wait_lock |
| 2770 |
AND |
| 2771 |
1.2. trx2->lock.wait_lock!=nullptr, and it held the latch for the shard |
| 2772 |
containing trx2->lock.wait_lock. |
| 2773 |
|
| 2774 |
@see asserts in trx_before_mutex_enter |
| 2775 |
|
| 2776 |
Assumption 2. |
| 2777 |
The Lock-sys latches are taken before any trx->mutex. |
| 2778 |
|
| 2779 |
@see asserts in sync0debug.cc |
| 2780 |
|
| 2781 |
Assumption 3. |
| 2782 |
Changing trx->lock.wait_lock from NULL to non-NULL requires latching |
| 2783 |
trx->mutex and the shard containing new wait_lock value. |
| 2784 |
|
| 2785 |
@see asserts in lock_set_lock_and_trx_wait() |
| 2786 |
|
| 2787 |
Assumption 4. |
| 2788 |
Changing trx->lock.wait_lock from non-NULL to NULL requires latching the shard |
| 2789 |
containing old wait_lock value. |
| 2790 |
|
| 2791 |
@see asserts in lock_reset_lock_and_trx_wait() |
| 2792 |
|
| 2793 |
Assumption 5. |
| 2794 |
If a thread is latching two Lock-sys shards then it's acquiring and releasing |
| 2795 |
both shards together (that is, without interleaving it with trx->mutex |
| 2796 |
operations). |
| 2797 |
|
| 2798 |
@see Shard_latches_guard |
| 2799 |
|
| 2800 |
Theorem 1. |
| 2801 |
If the Assumptions 1-5 hold, then it's impossible for trx_mutex_enter() call |
| 2802 |
to deadlock. |
| 2803 |
|
| 2804 |
By proving the theorem, and observing that the assertions hold for multiple runs |
| 2805 |
of test suite on debug build, we gain more and more confidence that |
| 2806 |
trx_mutex_enter() calls can not deadlock. |
| 2807 |
|
| 2808 |
The intuitive, albeit imprecise, version of the proof is that by Assumption 1, |
| 2809 |
for each i, the edge of the deadlock cycle caused by thread[i] - which has |
| 2810 |
already latched transaction start[i] and waits to latch transaction end[i] - |
| 2811 |
must have non-NULL end[i]->lock.wait_lock in a shard latched by thread[i], and |
| 2812 |
either start[i]->lock.wait_lock == NULL, or the shard containing this lock is |
| 2813 |
latched by thread[i], in either case no other j!=i can have end[j]=start[i], as |
| 2814 |
that would mean start[i]->lock.wait_lock is non-NULL and in shard latched by |
| 2815 |
thread[j] instead. |
| 2816 |
|
| 2817 |
The difficulty lays in that wait_lock is a field which can be modified over time |
| 2818 |
from several threads, so care must be taken to clarify at which moment in time |
| 2819 |
we make our observations and from whose perspective. |
| 2820 |
|
| 2821 |
We will now formally prove Theorem 1. |
| 2822 |
Assume otherwise, that is that we are in a thread which have just started a call |
| 2823 |
to mutex_enter(trx_a->mutex) and caused a deadlock. |
| 2824 |
|
| 2825 |
Fact 0. There is no thread which possesses exclusive Lock-sys latch, since to |
| 2826 |
form a deadlock one needs at least two threads inside Lock-sys |
| 2827 |
Fact 1. Each thread participating in the deadlock holds one trx mutex and waits |
| 2828 |
for the second one it tried to acquire |
| 2829 |
Fact 2. Thus each thread participating in the deadlock had gone through "else" |
| 2830 |
branch inside trx_before_mutex_enter(), so it verifies Assumption 1. |
| 2831 |
Fact 3. Our thread owns_lock_shard(trx_a->lock.wait_lock) |
| 2832 |
Fact 4. Another thread has latched trx_a->mutex as the first of its two latches |
| 2833 |
|
| 2834 |
Consider the situation from the point of view of this other thread, which is now |
| 2835 |
in the deadlock waiting for mutex_enter(trx_b->mutex) for some trx_b!=trx_a. |
| 2836 |
By Fact 2 and assumption 1, it had to take the "else" branch on the way there, |
| 2837 |
and thus at some moment in time it has saw either |
| 2838 |
a) trx_a->lock.wait_lock == nullptr or |
| 2839 |
b) it held the latch of the shard containing trx_a->lock.wait_lock. |
| 2840 |
This observation was either before or after our observation that |
| 2841 |
trx_a->lock.wait_lock != nullptr and that we hold the latch on this shard |
| 2842 |
(again Fact 2 and Assumption 1). |
| 2843 |
|
| 2844 |
Let us first rule out case b). As both threads are presumably in a deadlock, |
| 2845 |
they are still holding the latches on the lock-sys shards that they had latched, |
| 2846 |
so in case b) both threads hold a latch on a shard which contained the current |
| 2847 |
trx_a->lock.wait_lock value, which implies they must have latched two different |
| 2848 |
shards, which implies they saw two different values of wait_lock field, which in |
| 2849 |
turn means that it has changed, but by Assumption 4 it can not change while |
| 2850 |
somebody holds a latch on its current value's shard. Thus b) is impossible! |
| 2851 |
|
| 2852 |
This leaves us with case a). |
| 2853 |
|
| 2854 |
If our thread observed non-NULL value first, then it means a change from |
| 2855 |
non-NULL to NULL has happened, which by Assumption 4 requires a shard latch, |
| 2856 |
which only our thread posses - and we couldn't manipulate the wait_lock as we |
| 2857 |
are in a deadlock. |
| 2858 |
|
| 2859 |
If the other thread observed NULL first, then it means that the value has |
| 2860 |
changed to non-NULL, which requires trx_a->mutex according to Assumption 3, yet |
| 2861 |
this mutex was held entire time by the other thread, since it observed the NULL |
| 2862 |
just before it deadlock, so it could not change it, either. |
| 2863 |
|
| 2864 |
So, there is no way the value of wait_lock has changed from NULL to non-NULL or |
| 2865 |
vice-versa, yet one thread sees NULL and the other non-NULL - contradiction ends |
| 2866 |
the proof. |
| 2867 |
*/ |
| 2868 |
|
| 2869 |
static thread_local const trx_t *trx_first_latched_trx = nullptr; |
| 2870 |
static thread_local int32_t trx_latched_count = 0; |
| 2871 |
static thread_local bool trx_allowed_two_latches = false; |
| 2872 |
|
| 2873 |
void trx_before_mutex_enter(const trx_t *trx, bool first_of_two) { |
| 2874 |
if (0 == trx_latched_count++) { |
| 2875 |
ut_a(trx_first_latched_trx == nullptr); |
| 2876 |
trx_first_latched_trx = trx; |
| 2877 |
if (first_of_two) { |
| 2878 |
trx_allowed_two_latches = true; |
| 2879 |
} |
| 2880 |
} else { |
| 2881 |
ut_a(!first_of_two); |
| 2882 |
if (!locksys::owns_exclusive_global_latch()) { |
| 2883 |
ut_a(trx_allowed_two_latches); |
| 2884 |
ut_a(trx_latched_count == 2); |
| 2885 |
/* In theory wait_lock can change from non-null to null at any moment |
| 2886 |
unless we indeed hold the wait_lock's shard. Thankfully, this is exactly |
| 2887 |
what we assert. */ |
| 2888 |
ut_a(trx_first_latched_trx->lock.wait_lock == nullptr || |
| 2889 |
locksys::owns_lock_shard(trx_first_latched_trx->lock.wait_lock)); |
| 2890 |
ut_a(trx_first_latched_trx != trx); |
| 2891 |
/* This is not very safe, because to read trx->lock.wait_lock we |
| 2892 |
should already either latch trx->mutex (which we don't) or shard with |
| 2893 |
trx->lock.wait_lock. But our claim is precisely that we have latched |
| 2894 |
this shard, and we want to check that here. */ |
| 2895 |
ut_a(trx->lock.wait_lock != nullptr); |
| 2896 |
ut_a(locksys::owns_lock_shard(trx->lock.wait_lock)); |
| 2897 |
} |
| 2898 |
} |
| 2899 |
} |
| 2900 |
void trx_before_mutex_exit(const trx_t *trx) { |
| 2901 |
ut_a(0 < trx_latched_count); |
| 2902 |
if (0 == --trx_latched_count) { |
| 2903 |
ut_a(trx_first_latched_trx == trx); |
| 2904 |
trx_first_latched_trx = nullptr; |
| 2905 |
trx_allowed_two_latches = false; |
| 2906 |
} |
| 2907 |
} |
| 2908 |
#endif /* UNIV_DEBUG */ |
| 2909 |
|
| 2910 |
/** Compares the "weight" (or size) of two transactions. Transactions that |
| 2911 |
have edited non-transactional tables are considered heavier than ones |
| 2912 |
that have not. |
| 2913 |
@return true if weight(a) >= weight(b) */ |
| 2914 |
bool trx_weight_ge(const trx_t *a, /*!< in: transaction to be compared */ |
| 2915 |
const trx_t *b) /*!< in: transaction to be compared */ |
| 2916 |
{ |
| 2917 |
/* To read TRX_WEIGHT we need a exclusive global lock_sys latch */ |
| 2918 |
ut_ad(locksys::owns_exclusive_global_latch()); |
| 2919 |
|
| 2920 |
/* If mysql_thd is NULL for a transaction we assume that it has |
| 2921 |
not edited non-transactional tables. */ |
| 2922 |
|
| 2923 |
auto a_notrans_edit = |
| 2924 |
a->mysql_thd != nullptr && thd_has_edited_nontrans_tables(a->mysql_thd); |
| 2925 |
|
| 2926 |
auto b_notrans_edit = |
| 2927 |
b->mysql_thd != nullptr && thd_has_edited_nontrans_tables(b->mysql_thd); |
| 2928 |
|
| 2929 |
if (a_notrans_edit != b_notrans_edit) { |
| 2930 |
return (a_notrans_edit); |
| 2931 |
} |
| 2932 |
|
| 2933 |
/* Either both had edited non-transactional tables or both had |
| 2934 |
not, we fall back to comparing the number of altered/locked |
| 2935 |
rows. */ |
| 2936 |
|
| 2937 |
return (TRX_WEIGHT(a) >= TRX_WEIGHT(b)); |
| 2938 |
} |
| 2939 |
|
| 2940 |
/** Prepares a transaction for given rollback segment. |
| 2941 |
@return lsn_t: lsn assigned for commit of scheduled rollback segment */ |
| 2942 |
static lsn_t trx_prepare_low( |
| 2943 |
trx_t *trx, /*!< in/out: transaction */ |
| 2944 |
trx_undo_ptr_t *undo_ptr, /*!< in/out: pointer to rollback |
| 2945 |
segment scheduled for prepare. */ |
| 2946 |
bool noredo_logging) /*!< in: turn-off redo logging. */ |
| 2947 |
{ |
| 2948 |
if (undo_ptr->insert_undo != nullptr || undo_ptr->update_undo != nullptr) { |
| 2949 |
mtr_t mtr; |
| 2950 |
trx_rseg_t *rseg = undo_ptr->rseg; |
| 2951 |
|
| 2952 |
mtr_start_sync(&mtr); |
| 2953 |
|
| 2954 |
if (noredo_logging) { |
| 2955 |
mtr_set_log_mode(&mtr, MTR_LOG_NO_REDO); |
| 2956 |
} |
| 2957 |
|
| 2958 |
/* Change the undo log segment states from TRX_UNDO_ACTIVE to |
| 2959 |
TRX_UNDO_PREPARED: these modifications to the file data |
| 2960 |
structure define the transaction as prepared in the file-based |
| 2961 |
world, at the serialization point of lsn. */ |
| 2962 |
|
| 2963 |
rseg->latch(); |
| 2964 |
|
| 2965 |
if (undo_ptr->insert_undo != nullptr) { |
| 2966 |
/* It is not necessary to obtain trx->undo_mutex here |
| 2967 |
because only a single OS thread is allowed to do the |
| 2968 |
transaction prepare for this transaction. */ |
| 2969 |
trx_undo_set_state_at_prepare(trx, undo_ptr->insert_undo, false, &mtr); |
| 2970 |
} |
| 2971 |
|
| 2972 |
if (undo_ptr->update_undo != nullptr) { |
| 2973 |
if (!noredo_logging) { |
| 2974 |
trx_undo_gtid_set(trx, undo_ptr->update_undo, true); |
| 2975 |
} |
| 2976 |
trx_undo_set_state_at_prepare(trx, undo_ptr->update_undo, false, &mtr); |
| 2977 |
} |
| 2978 |
|
| 2979 |
rseg->unlatch(); |
| 2980 |
|
| 2981 |
/*--------------*/ |
| 2982 |
/* This mtr commit makes the transaction prepared in |
| 2983 |
file-based world. */ |
| 2984 |
mtr_commit(&mtr); |
| 2985 |
/*--------------*/ |
| 2986 |
|
| 2987 |
if (!noredo_logging) { |
| 2988 |
const lsn_t lsn = mtr.commit_lsn(); |
| 2989 |
ut_ad(lsn > 0 || !mtr_t::s_logging.is_enabled()); |
| 2990 |
return lsn; |
| 2991 |
} |
| 2992 |
} |
| 2993 |
|
| 2994 |
return 0; |
| 2995 |
} |
| 2996 |
|
| 2997 |
bool trx_is_mysql_xa(const trx_t *trx) { |
| 2998 |
auto my_xid = trx->xid->get_my_xid(); |
| 2999 |
return (my_xid != 0); |
| 3000 |
} |
| 3001 |
|
| 3002 |
/** Prepares a transaction. |
| 3003 |
@param[in] trx the transction to prepare. */ |
| 3004 |
static void trx_prepare(trx_t *trx) { |
| 3005 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 3006 |
|
| 3007 |
/* This transaction has crossed the point of no return and cannot |
| 3008 |
be rolled back asynchronously now. It must commit or rollback |
| 3009 |
synchronously. */ |
| 3010 |
|
| 3011 |
lsn_t lsn = 0; |
| 3012 |
|
| 3013 |
/* Only fresh user transactions can be prepared. |
| 3014 |
Recovered transactions cannot. */ |
| 3015 |
ut_a(!trx->is_recovered); |
| 3016 |
|
| 3017 |
DBUG_EXECUTE_IF("ib_trx_crash_during_xa_prepare_step", DBUG_SUICIDE();); |
| 3018 |
|
| 3019 |
if (trx->rsegs.m_redo.rseg != nullptr && trx_is_redo_rseg_updated(trx)) { |
| 3020 |
lsn = trx_prepare_low(trx, &trx->rsegs.m_redo, false); |
| 3021 |
} |
| 3022 |
|
| 3023 |
if (trx->rsegs.m_noredo.rseg != nullptr && trx_is_temp_rseg_updated(trx)) { |
| 3024 |
trx_prepare_low(trx, &trx->rsegs.m_noredo, true); |
| 3025 |
} |
| 3026 |
|
| 3027 |
ut_a(trx->state.load(std::memory_order_relaxed) == TRX_STATE_ACTIVE); |
| 3028 |
|
| 3029 |
trx_sys_mutex_enter(); |
| 3030 |
trx->state.store(TRX_STATE_PREPARED, std::memory_order_relaxed); |
| 3031 |
trx_sys->n_prepared_trx++; |
| 3032 |
trx_sys_mutex_exit(); |
| 3033 |
|
| 3034 |
/* Force isolation level to RC and release GAP locks |
| 3035 |
for test purpose. */ |
| 3036 |
DBUG_EXECUTE_IF("ib_force_release_gap_lock_prepare", |
| 3037 |
trx->isolation_level = TRX_ISO_READ_COMMITTED;); |
| 3038 |
|
| 3039 |
/* Release read locks after PREPARE for READ COMMITTED |
| 3040 |
and lower isolation. */ |
| 3041 |
if (trx->releases_gap_locks_at_prepare()) { |
| 3042 |
/* Stop inheriting GAP locks. */ |
| 3043 |
trx->skip_lock_inheritance = true; |
| 3044 |
|
| 3045 |
/* Release only GAP locks for now. */ |
| 3046 |
lock_trx_release_read_locks(trx, true); |
| 3047 |
} |
| 3048 |
|
| 3049 |
if (lsn > 0) { |
| 3050 |
trx_flush_logs(trx, lsn); |
| 3051 |
} |
| 3052 |
} |
| 3053 |
|
| 3054 |
/** Sets the transaction as prepared in the transaction coordinator for |
| 3055 |
the given rollback segment. |
| 3056 |
@param[in,out] trx The rollback segment parent transaction. |
| 3057 |
@param[in] undo_ptr The rollback segment. |
| 3058 |
@return lsn assigned for commit of scheduled rollback segment */ |
| 3059 |
static lsn_t trx_set_prepared_in_tc_low(trx_t *trx, trx_undo_ptr_t *undo_ptr) { |
| 3060 |
if (undo_ptr->insert_undo != nullptr || undo_ptr->update_undo != nullptr) { |
| 3061 |
mtr_t mtr; |
| 3062 |
trx_rseg_t *rseg = undo_ptr->rseg; |
| 3063 |
|
| 3064 |
mtr_start_sync(&mtr); |
| 3065 |
|
| 3066 |
/* Change the undo log segment states from TRX_UNDO_ACTIVE to |
| 3067 |
TRX_UNDO_PREPARED: these modifications to the file data |
| 3068 |
structure define the transaction as prepared in the file-based |
| 3069 |
world, at the serialization point of lsn. */ |
| 3070 |
|
| 3071 |
rseg->latch(); |
| 3072 |
|
| 3073 |
if (undo_ptr->insert_undo != nullptr) { |
| 3074 |
/* It is not necessary to obtain trx->undo_mutex here |
| 3075 |
because only a single OS thread is allowed to do the |
| 3076 |
transaction prepare for this transaction. */ |
| 3077 |
trx_undo_set_prepared_in_tc(trx, undo_ptr->insert_undo, &mtr); |
| 3078 |
} |
| 3079 |
|
| 3080 |
if (undo_ptr->update_undo != nullptr) { |
| 3081 |
trx_undo_gtid_set(trx, undo_ptr->update_undo, true); |
| 3082 |
trx_undo_set_prepared_in_tc(trx, undo_ptr->update_undo, &mtr); |
| 3083 |
} |
| 3084 |
|
| 3085 |
rseg->unlatch(); |
| 3086 |
|
| 3087 |
/*--------------*/ |
| 3088 |
/* This mtr commit makes the transaction prepared in |
| 3089 |
file-based world. */ |
| 3090 |
mtr_commit(&mtr); |
| 3091 |
/*--------------*/ |
| 3092 |
|
| 3093 |
const lsn_t lsn = mtr.commit_lsn(); |
| 3094 |
ut_ad(lsn > 0 || !mtr_t::s_logging.is_enabled()); |
| 3095 |
return lsn; |
| 3096 |
} |
| 3097 |
|
| 3098 |
return 0; |
| 3099 |
} |
| 3100 |
|
| 3101 |
/** Marks a transaction as prepared in the transaction coordinator. |
| 3102 |
@param[in] trx the transction to mark. */ |
| 3103 |
static void trx_set_prepared_in_tc(trx_t *trx) { |
| 3104 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 3105 |
ut_a(trx->state.load(std::memory_order_relaxed) == TRX_STATE_PREPARED); |
| 3106 |
|
| 3107 |
lsn_t lsn = trx_set_prepared_in_tc_low(trx, &trx->rsegs.m_redo); |
| 3108 |
|
| 3109 |
/* Check and get GTID to be persisted. Do it outside trx_sys mutex. */ |
| 3110 |
auto >id_persistor = clone_sys->get_gtid_persistor(); |
| 3111 |
Gtid_desc gtid_desc; |
| 3112 |
gtid_persistor.get_gtid_info(trx, gtid_desc); |
| 3113 |
|
| 3114 |
/* Add GTID to be persisted to disk table, if needed. */ |
| 3115 |
if (gtid_desc.m_is_set) { |
| 3116 |
/* The gtid_persistor.add() might release and re-acquire the mutex. */ |
| 3117 |
trx_sys_serialisation_mutex_enter(); |
| 3118 |
gtid_persistor.add(gtid_desc); |
| 3119 |
trx_sys_serialisation_mutex_exit(); |
| 3120 |
} |
| 3121 |
|
| 3122 |
/* Reset after successfully adding GTID to in memory table. */ |
| 3123 |
trx->persists_gtid = false; |
| 3124 |
|
| 3125 |
if (lsn > 0) { |
| 3126 |
trx_flush_logs(trx, lsn); |
| 3127 |
} |
| 3128 |
} |
| 3129 |
|
| 3130 |
/** |
| 3131 |
Does the transaction prepare for MySQL. |
| 3132 |
@param[in, out] trx Transaction instance to prepare */ |
| 3133 |
dberr_t trx_prepare_for_mysql(trx_t *trx) { |
| 3134 |
trx_start_if_not_started_xa(trx, false, UT_LOCATION_HERE); |
| 3135 |
|
| 3136 |
TrxInInnoDB trx_in_innodb(trx, true); |
| 3137 |
|
| 3138 |
if (trx_in_innodb.is_aborted() && |
| 3139 |
trx->killed_by != std::this_thread::get_id()) { |
| 3140 |
return (DB_FORCED_ABORT); |
| 3141 |
} |
| 3142 |
|
| 3143 |
trx->op_info = "preparing"; |
| 3144 |
|
| 3145 |
trx_prepare(trx); |
| 3146 |
|
| 3147 |
trx->op_info = ""; |
| 3148 |
|
| 3149 |
return (DB_SUCCESS); |
| 3150 |
} |
| 3151 |
|
| 3152 |
/** |
| 3153 |
Get the table name and database name for the given dd_table object. |
| 3154 |
|
| 3155 |
@param[in,out] table Handler table name object pointer. |
| 3156 |
@param[in] dd_table Pointer table name DD object. |
| 3157 |
@param[in] mem_root Mem_root for space allocation. |
| 3158 |
|
| 3159 |
@retval true Error, e.g. Memory allocation failure. |
| 3160 |
@retval false Success |
| 3161 |
*/ |
| 3162 |
|
| 3163 |
static bool get_table_name_info(st_handler_tablename *table, |
| 3164 |
const dict_table_t *dd_table, |
| 3165 |
MEM_ROOT *mem_root) { |
| 3166 |
std::string db_str; |
| 3167 |
std::string table_str; |
| 3168 |
dict_name::get_table(dd_table->name.m_name, db_str, table_str); |
| 3169 |
|
| 3170 |
table->db = strmake_root(mem_root, db_str.c_str(), db_str.size()); |
| 3171 |
if (table->db == nullptr) return true; |
| 3172 |
|
| 3173 |
table->tablename = |
| 3174 |
strmake_root(mem_root, table_str.c_str(), table_str.size()); |
| 3175 |
if (table->tablename == nullptr) return true; |
| 3176 |
|
| 3177 |
return false; |
| 3178 |
} |
| 3179 |
|
| 3180 |
/** |
| 3181 |
Get prepared transaction info from InnoDB data structure. |
| 3182 |
|
| 3183 |
@param[in,out] txn_list Handler layer transaction list. |
| 3184 |
@param[in] trx Innodb transaction info. |
| 3185 |
@param[in] mem_root Mem_root for space allocation. |
| 3186 |
|
| 3187 |
@retval true Error, e.g. Memory allocation failure. |
| 3188 |
@retval false Success |
| 3189 |
*/ |
| 3190 |
|
| 3191 |
static bool get_info_about_prepared_transaction(XA_recover_txn *txn_list, |
| 3192 |
const trx_t *trx, |
| 3193 |
MEM_ROOT *mem_root) { |
| 3194 |
txn_list->id = *trx->xid; |
| 3195 |
txn_list->mod_tables = new (mem_root) List<st_handler_tablename>(); |
| 3196 |
if (!txn_list->mod_tables) return true; |
| 3197 |
|
| 3198 |
for (auto dd_table : trx->mod_tables) { |
| 3199 |
st_handler_tablename *table = new (mem_root) st_handler_tablename(); |
| 3200 |
|
| 3201 |
if (!table || get_table_name_info(table, dd_table, mem_root) || |
| 3202 |
txn_list->mod_tables->push_back(table, mem_root)) |
| 3203 |
return true; |
| 3204 |
} |
| 3205 |
return false; |
| 3206 |
} |
| 3207 |
|
| 3208 |
/** This function is used to find number of prepared transactions and |
| 3209 |
their transaction objects for a recovery. |
| 3210 |
@return number of prepared transactions stored in xid_list */ |
| 3211 |
int trx_recover_for_mysql( |
| 3212 |
XA_recover_txn *txn_list, /*!< in/out: prepared transactions */ |
| 3213 |
ulint len, /*!< in: number of slots in xid_list */ |
| 3214 |
MEM_ROOT *mem_root) /*!< in: memory for table names */ |
| 3215 |
{ |
| 3216 |
ulint count = 0; |
| 3217 |
|
| 3218 |
ut_ad(txn_list); |
| 3219 |
ut_ad(len); |
| 3220 |
|
| 3221 |
/* We should set those transactions which are in the prepared state |
| 3222 |
to the xid_list */ |
| 3223 |
|
| 3224 |
trx_sys_mutex_enter(); |
| 3225 |
|
| 3226 |
for (const trx_t *trx : trx_sys->rw_trx_list) { |
| 3227 |
assert_trx_in_rw_list(trx); |
| 3228 |
|
| 3229 |
/* The state of a read-write transaction cannot change |
| 3230 |
from or to NOT_STARTED while we are holding the |
| 3231 |
trx_sys->mutex. It may change to PREPARED, but not if |
| 3232 |
trx->is_recovered. */ |
| 3233 |
if (trx_state_eq(trx, TRX_STATE_PREPARED)) { |
| 3234 |
if (get_info_about_prepared_transaction(&txn_list[count], trx, mem_root)) |
| 3235 |
break; |
| 3236 |
|
| 3237 |
if (count == 0) { |
| 3238 |
ib::info(ER_IB_MSG_1207) << "Starting recovery for" |
| 3239 |
" XA transactions..."; |
| 3240 |
} |
| 3241 |
|
| 3242 |
ib::info(ER_IB_MSG_1208) << "Transaction " << trx_get_id_for_print(trx) |
| 3243 |
<< " in prepared state after recovery"; |
| 3244 |
|
| 3245 |
ib::info(ER_IB_MSG_1209) |
| 3246 |
<< "Transaction contains changes to " << trx->undo_no << " rows"; |
| 3247 |
|
| 3248 |
count++; |
| 3249 |
|
| 3250 |
if (count == len) { |
| 3251 |
break; |
| 3252 |
} |
| 3253 |
} |
| 3254 |
} |
| 3255 |
|
| 3256 |
trx_sys_mutex_exit(); |
| 3257 |
|
| 3258 |
if (count > 0) { |
| 3259 |
ib::info(ER_IB_MSG_1210) << count |
| 3260 |
<< " transactions in prepared state" |
| 3261 |
" after recovery"; |
| 3262 |
} |
| 3263 |
|
| 3264 |
return (int(count)); |
| 3265 |
} |
| 3266 |
|
| 3267 |
int trx_recover_tc_for_mysql(Xa_state_list &xa_list) { |
| 3268 |
/* We should set those transactions which are in the prepared state |
| 3269 |
to the xid_list */ |
| 3270 |
|
| 3271 |
trx_sys_mutex_enter(); |
| 3272 |
|
| 3273 |
for (trx_t *trx : trx_sys->rw_trx_list) { |
| 3274 |
assert_trx_in_rw_list(trx); |
| 3275 |
|
| 3276 |
/* The state of a read-write transaction cannot change |
| 3277 |
from or to NOT_STARTED while we are holding the |
| 3278 |
trx_sys->mutex. It may change to PREPARED, but not if |
| 3279 |
trx->is_recovered. */ |
| 3280 |
if (trx_state_eq(trx, TRX_STATE_PREPARED)) { |
| 3281 |
if (trx_is_prepared_in_tc(trx)) { |
| 3282 |
/* We found the transaction in 2nd phase of prepare, add to XA |
| 3283 |
transaction state list as PREPARED_IN_TC */ |
| 3284 |
xa_list.add(*trx->xid, enum_ha_recover_xa_state::PREPARED_IN_TC); |
| 3285 |
} else { |
| 3286 |
/* Otherwise, just add as PREPARED_IN_SE */ |
| 3287 |
xa_list.add(*trx->xid, enum_ha_recover_xa_state::PREPARED_IN_SE); |
| 3288 |
} |
| 3289 |
} |
| 3290 |
} |
| 3291 |
|
| 3292 |
trx_sys_mutex_exit(); |
| 3293 |
|
| 3294 |
return 0; |
| 3295 |
} |
| 3296 |
|
| 3297 |
/** This function is used to find one X/Open XA distributed transaction |
| 3298 |
which is in the prepared state |
| 3299 |
@return trx on match, the trx->xid will be invalidated; |
| 3300 |
*/ |
| 3301 |
[[nodiscard]] static trx_t *trx_get_trx_by_xid_low( |
| 3302 |
const XID *xid) /*!< in: X/Open XA transaction |
| 3303 |
identifier */ |
| 3304 |
{ |
| 3305 |
ut_ad(trx_sys_mutex_own()); |
| 3306 |
|
| 3307 |
for (auto trx : trx_sys->rw_trx_list) { |
| 3308 |
assert_trx_in_rw_list(trx); |
| 3309 |
|
| 3310 |
/* Most of the time server layer takes care of synchronizing access to a XID |
| 3311 |
from several connections, but when disconnecting there is a short period in |
| 3312 |
which server allows a new connection to pick up XID still processed by old |
| 3313 |
connection at InnoDB layer. To synchronize with trx_disconnect_from_mysql(), |
| 3314 |
we use trx->mysql_thd under protection of trx_sys->mutex. */ |
| 3315 |
|
| 3316 |
if (trx->mysql_thd == nullptr && trx_state_eq(trx, TRX_STATE_PREPARED) && |
| 3317 |
xid->eq(trx->xid)) { |
| 3318 |
/* Invalidate the XID, so that subsequent calls |
| 3319 |
will not find it. */ |
| 3320 |
trx->xid->reset(); |
| 3321 |
return trx; |
| 3322 |
} |
| 3323 |
} |
| 3324 |
|
| 3325 |
return nullptr; |
| 3326 |
} |
| 3327 |
|
| 3328 |
trx_t *trx_get_trx_by_xid(const XID *xid) { |
| 3329 |
trx_t *trx; |
| 3330 |
|
| 3331 |
if (xid == nullptr) { |
| 3332 |
return (nullptr); |
| 3333 |
} |
| 3334 |
|
| 3335 |
trx_sys_mutex_enter(); |
| 3336 |
|
| 3337 |
/* Recovered/Resurrected transactions are always only on the |
| 3338 |
trx_sys_t::rw_trx_list. */ |
| 3339 |
trx = trx_get_trx_by_xid_low(xid); |
| 3340 |
|
| 3341 |
trx_sys_mutex_exit(); |
| 3342 |
|
| 3343 |
return (trx); |
| 3344 |
} |
| 3345 |
|
| 3346 |
/** Starts the transaction if it is not yet started. |
| 3347 |
@param[in,out] trx Transaction |
| 3348 |
@param[in] read_write True if read write transaction */ |
| 3349 |
void trx_start_if_not_started_xa_low(trx_t *trx, bool read_write) { |
| 3350 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 3351 |
switch (trx->state.load(std::memory_order_relaxed)) { |
| 3352 |
case TRX_STATE_NOT_STARTED: |
| 3353 |
case TRX_STATE_FORCED_ROLLBACK: |
| 3354 |
trx_start_low(trx, read_write); |
| 3355 |
return; |
| 3356 |
|
| 3357 |
case TRX_STATE_ACTIVE: |
| 3358 |
if (trx->id == 0 && read_write) { |
| 3359 |
/* If the transaction is tagged as read-only then |
| 3360 |
it can only write to temp tables and for such |
| 3361 |
transactions we don't want to move them to the |
| 3362 |
trx_sys_t::rw_trx_list. */ |
| 3363 |
if (!trx->read_only) { |
| 3364 |
trx_set_rw_mode(trx); |
| 3365 |
} else if (!srv_read_only_mode) { |
| 3366 |
trx_assign_rseg_temp(trx); |
| 3367 |
} |
| 3368 |
} |
| 3369 |
return; |
| 3370 |
case TRX_STATE_PREPARED: |
| 3371 |
case TRX_STATE_COMMITTED_IN_MEMORY: |
| 3372 |
break; |
| 3373 |
} |
| 3374 |
|
| 3375 |
ut_error; |
| 3376 |
} |
| 3377 |
|
| 3378 |
/** Starts the transaction if it is not yet started. |
| 3379 |
@param[in] trx Transaction |
| 3380 |
@param[in] read_write True if read write transaction */ |
| 3381 |
void trx_start_if_not_started_low(trx_t *trx, bool read_write) { |
| 3382 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 3383 |
switch (trx->state.load(std::memory_order_relaxed)) { |
| 3384 |
case TRX_STATE_NOT_STARTED: |
| 3385 |
case TRX_STATE_FORCED_ROLLBACK: |
| 3386 |
|
| 3387 |
trx_start_low(trx, read_write); |
| 3388 |
return; |
| 3389 |
|
| 3390 |
case TRX_STATE_ACTIVE: |
| 3391 |
|
| 3392 |
if (read_write && trx->id == 0 && !trx->read_only) { |
| 3393 |
trx_set_rw_mode(trx); |
| 3394 |
} |
| 3395 |
return; |
| 3396 |
|
| 3397 |
case TRX_STATE_PREPARED: |
| 3398 |
case TRX_STATE_COMMITTED_IN_MEMORY: |
| 3399 |
break; |
| 3400 |
} |
| 3401 |
|
| 3402 |
ut_error; |
| 3403 |
} |
| 3404 |
|
| 3405 |
/** Starts a transaction for internal processing. */ |
| 3406 |
void trx_start_internal_low(trx_t *trx) /*!< in/out: transaction */ |
| 3407 |
{ |
| 3408 |
/* Ensure it is not flagged as an auto-commit-non-locking |
| 3409 |
transaction. */ |
| 3410 |
|
| 3411 |
trx->will_lock = 1; |
| 3412 |
|
| 3413 |
trx->internal = true; |
| 3414 |
|
| 3415 |
trx_start_low(trx, true); |
| 3416 |
} |
| 3417 |
|
| 3418 |
/** Starts a read-only transaction for internal processing. |
| 3419 |
@param[in,out] trx transaction to be started */ |
| 3420 |
void trx_start_internal_read_only_low(trx_t *trx) { |
| 3421 |
/* Ensure it is not flagged as an auto-commit-non-locking |
| 3422 |
transaction. */ |
| 3423 |
|
| 3424 |
trx->will_lock = 1; |
| 3425 |
|
| 3426 |
trx->internal = true; |
| 3427 |
|
| 3428 |
trx_start_low(trx, false); |
| 3429 |
} |
| 3430 |
|
| 3431 |
/** Set the transaction as a read-write transaction if it is not already |
| 3432 |
tagged as such. Read-only transactions that are writing to temporary |
| 3433 |
tables are assigned an ID and a rollback segment but are not added |
| 3434 |
to the trx read-write list because their updates should not be visible |
| 3435 |
to other transactions and therefore their changes can be ignored by |
| 3436 |
by MVCC. */ |
| 3437 |
void trx_set_rw_mode(trx_t *trx) /*!< in/out: transaction that is RW */ |
| 3438 |
{ |
| 3439 |
ut_ad(trx->rsegs.m_redo.rseg == nullptr); |
| 3440 |
ut_ad(!trx->in_rw_trx_list); |
| 3441 |
ut_ad(!trx_is_autocommit_non_locking(trx)); |
| 3442 |
ut_ad(!trx->read_only); |
| 3443 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 3444 |
|
| 3445 |
if (srv_force_recovery >= SRV_FORCE_NO_TRX_UNDO) { |
| 3446 |
return; |
| 3447 |
} |
| 3448 |
|
| 3449 |
/* Function is promoting existing trx from ro mode to rw mode. |
| 3450 |
In this process it has acquired trx_sys->mutex as it plan to |
| 3451 |
move trx from ro list to rw list. If in future, some other thread |
| 3452 |
looks at this trx object while it is being promoted then ensure |
| 3453 |
that both threads are synced by acquiring trx->mutex to avoid decision |
| 3454 |
based on in-consistent view formed during promotion. */ |
| 3455 |
|
| 3456 |
trx_assign_rseg_durable(trx); |
| 3457 |
|
| 3458 |
ut_ad(trx->rsegs.m_redo.rseg != nullptr); |
| 3459 |
|
| 3460 |
DEBUG_SYNC_C("trx_sys_before_assign_id"); |
| 3461 |
|
| 3462 |
trx_sys_mutex_enter(); |
| 3463 |
|
| 3464 |
ut_ad(trx->id == 0); |
| 3465 |
trx->id = trx_sys_allocate_trx_id(); |
| 3466 |
|
| 3467 |
trx_sys->rw_trx_ids.push_back(trx->id); |
| 3468 |
|
| 3469 |
/* So that we can see our own changes. */ |
| 3470 |
if (MVCC::is_view_active(trx->read_view)) { |
| 3471 |
MVCC::set_view_creator_trx_id(trx->read_view, trx->id); |
| 3472 |
} |
| 3473 |
trx_add_to_rw_trx_list(trx); |
| 3474 |
|
| 3475 |
trx_sys_mutex_exit(); |
| 3476 |
|
| 3477 |
trx_sys_rw_trx_add(trx); |
| 3478 |
} |
| 3479 |
|
| 3480 |
void trx_kill_blocking(trx_t *trx) { |
| 3481 |
if (!trx_is_high_priority(trx)) { |
| 3482 |
return; |
| 3483 |
} |
| 3484 |
hit_list_t hit_list; |
| 3485 |
lock_make_trx_hit_list(trx, hit_list); |
| 3486 |
if (hit_list.empty()) { |
| 3487 |
return; |
| 3488 |
} |
| 3489 |
|
| 3490 |
DEBUG_SYNC_C("trx_kill_blocking_enter"); |
| 3491 |
|
| 3492 |
ulint had_dict_lock = trx->dict_operation_lock_mode; |
| 3493 |
|
| 3494 |
switch (had_dict_lock) { |
| 3495 |
case 0: |
| 3496 |
break; |
| 3497 |
|
| 3498 |
case RW_S_LATCH: |
| 3499 |
/* Release foreign key check latch */ |
| 3500 |
row_mysql_unfreeze_data_dictionary(trx); |
| 3501 |
break; |
| 3502 |
|
| 3503 |
default: |
| 3504 |
/* There should never be a lock wait when the |
| 3505 |
dictionary latch is reserved in X mode. Dictionary |
| 3506 |
transactions should only acquire locks on dictionary |
| 3507 |
tables, not other tables. All access to dictionary |
| 3508 |
tables should be covered by dictionary |
| 3509 |
transactions. */ |
| 3510 |
ut_error; |
| 3511 |
} |
| 3512 |
|
| 3513 |
ut_a(trx->dict_operation_lock_mode == 0); |
| 3514 |
|
| 3515 |
/** Kill the transactions in the lock acquisition order old -> new. */ |
| 3516 |
hit_list_t::reverse_iterator end = hit_list.rend(); |
| 3517 |
|
| 3518 |
for (hit_list_t::reverse_iterator it = hit_list.rbegin(); it != end; ++it) { |
| 3519 |
trx_t *victim_trx = it->m_trx; |
| 3520 |
auto version = it->m_version; |
| 3521 |
|
| 3522 |
/* Shouldn't commit suicide. */ |
| 3523 |
ut_ad(victim_trx != trx); |
| 3524 |
ut_ad(victim_trx->mysql_thd != trx->mysql_thd); |
| 3525 |
|
| 3526 |
if (lock_cancel_if_waiting_and_release(*it)) { |
| 3527 |
continue; |
| 3528 |
} |
| 3529 |
|
| 3530 |
/* Check that the transaction isn't active inside |
| 3531 |
InnoDB code. We have to wait while it is executing |
| 3532 |
in the InnoDB context. This can potentially take a |
| 3533 |
long time */ |
| 3534 |
|
| 3535 |
trx_mutex_enter(victim_trx); |
| 3536 |
ut_ad(version <= victim_trx->version); |
| 3537 |
|
| 3538 |
ulint loop_count = 0; |
| 3539 |
/* start with optimistic sleep time of 20 micro seconds. */ |
| 3540 |
ulint sleep_time = 20; |
| 3541 |
|
| 3542 |
bool exited_innodb = false; |
| 3543 |
|
| 3544 |
while ((victim_trx->in_innodb & TRX_FORCE_ROLLBACK_MASK) > 0 && |
| 3545 |
victim_trx->version == version) { |
| 3546 |
trx_mutex_exit(victim_trx); |
| 3547 |
|
| 3548 |
/* Declare this OS thread to exit InnoDB, before waiting */ |
| 3549 |
if (trx->declared_to_be_inside_innodb) { |
| 3550 |
exited_innodb = true; |
| 3551 |
srv_conc_force_exit_innodb(trx); |
| 3552 |
} |
| 3553 |
|
| 3554 |
loop_count++; |
| 3555 |
/* If the wait is long, don't hog the cpu. */ |
| 3556 |
if (loop_count < 100) { |
| 3557 |
/* 20 microseconds */ |
| 3558 |
sleep_time = 20; |
| 3559 |
} else if (loop_count < 1000) { |
| 3560 |
/* 1 millisecond */ |
| 3561 |
sleep_time = 1000; |
| 3562 |
} else { |
| 3563 |
/* 100 milliseconds */ |
| 3564 |
sleep_time = 100000; |
| 3565 |
} |
| 3566 |
|
| 3567 |
std::this_thread::sleep_for(std::chrono::microseconds(sleep_time)); |
| 3568 |
|
| 3569 |
trx_mutex_enter(victim_trx); |
| 3570 |
} |
| 3571 |
|
| 3572 |
/* Return back inside InnoDB */ |
| 3573 |
if (exited_innodb) { |
| 3574 |
exited_innodb = false; |
| 3575 |
/* Exit transaction mutex before entering Innodb. */ |
| 3576 |
trx_mutex_exit(victim_trx); |
| 3577 |
srv_conc_force_enter_innodb(trx); |
| 3578 |
trx_mutex_enter(victim_trx); |
| 3579 |
} |
| 3580 |
|
| 3581 |
/* Compare the version to check if the transaction has |
| 3582 |
already finished */ |
| 3583 |
if (victim_trx->version != version) { |
| 3584 |
trx_mutex_exit(victim_trx); |
| 3585 |
continue; |
| 3586 |
} |
| 3587 |
|
| 3588 |
/* We should never kill background transactions. */ |
| 3589 |
ut_ad(victim_trx->mysql_thd != nullptr); |
| 3590 |
|
| 3591 |
ut_ad(!(trx->in_innodb & TRX_FORCE_ROLLBACK_DISABLE)); |
| 3592 |
ut_ad(victim_trx->in_innodb & TRX_FORCE_ROLLBACK); |
| 3593 |
ut_ad(victim_trx->killed_by == std::this_thread::get_id()); |
| 3594 |
ut_ad(victim_trx->version == it->m_version); |
| 3595 |
|
| 3596 |
/* We don't kill Read Only, Background or high priority |
| 3597 |
transactions. */ |
| 3598 |
ut_a(!victim_trx->read_only); |
| 3599 |
ut_a(victim_trx->mysql_thd != nullptr); |
| 3600 |
|
| 3601 |
trx_mutex_exit(victim_trx); |
| 3602 |
|
| 3603 |
#ifdef UNIV_DEBUG |
| 3604 |
char buffer[1024]; |
| 3605 |
char *thr_text; |
| 3606 |
trx_id_t id; |
| 3607 |
|
| 3608 |
thr_text = thd_security_context(victim_trx->mysql_thd, buffer, |
| 3609 |
sizeof(buffer), 512); |
| 3610 |
id = victim_trx->id; |
| 3611 |
#endif /* UNIV_DEBUG */ |
| 3612 |
trx_rollback_for_mysql(victim_trx); |
| 3613 |
|
| 3614 |
#ifdef UNIV_DEBUG |
| 3615 |
ib::info(ER_IB_MSG_1211, ulonglong{trx->id}, |
| 3616 |
to_string(std::this_thread::get_id()).c_str(), ulonglong{id}, |
| 3617 |
thr_text); |
| 3618 |
#endif /* UNIV_DEBUG */ |
| 3619 |
trx_mutex_enter(victim_trx); |
| 3620 |
|
| 3621 |
version++; |
| 3622 |
ut_ad(victim_trx->version == version); |
| 3623 |
|
| 3624 |
victim_trx->killed_by.store(std::thread::id{}); |
| 3625 |
|
| 3626 |
victim_trx->in_innodb &= TRX_FORCE_ROLLBACK_MASK; |
| 3627 |
|
| 3628 |
trx_mutex_exit(victim_trx); |
| 3629 |
} |
| 3630 |
|
| 3631 |
if (had_dict_lock) { |
| 3632 |
row_mysql_freeze_data_dictionary(trx, UT_LOCATION_HERE); |
| 3633 |
} |
| 3634 |
} |
| 3635 |
|
| 3636 |
/* To get current session thread default THD */ |
| 3637 |
THD *thd_get_current_thd(); |
| 3638 |
|
| 3639 |
void trx_sys_update_binlog_position(trx_t *trx) { |
| 3640 |
THD *thd = trx->mysql_thd; |
| 3641 |
/* For XA commit/rollback by XID, transaction thd could be null. */ |
| 3642 |
if (thd == nullptr) { |
| 3643 |
thd = thd_get_current_thd(); |
| 3644 |
if (thd == nullptr) { |
| 3645 |
return; |
| 3646 |
} |
| 3647 |
} |
| 3648 |
ulonglong pos; |
| 3649 |
thd_binlog_pos(thd, &trx->mysql_log_file_name, &pos); |
| 3650 |
trx->mysql_log_offset = static_cast<uint64_t>(pos); |
| 3651 |
} |
| 3652 |
|
| 3653 |
bool trx_is_prepared_in_tc(trx_t const *trx) { |
| 3654 |
trx_undo_t *undo{nullptr}; |
| 3655 |
|
| 3656 |
if (trx->rsegs.m_redo.rseg != nullptr && trx_is_redo_rseg_updated(trx)) { |
| 3657 |
if (trx->rsegs.m_redo.insert_undo != nullptr) { |
| 3658 |
undo = trx->rsegs.m_redo.insert_undo; |
| 3659 |
} else { |
| 3660 |
undo = trx->rsegs.m_redo.update_undo; |
| 3661 |
} |
| 3662 |
} |
| 3663 |
|
| 3664 |
return undo != nullptr && (undo->state == TRX_UNDO_PREPARED_80028 || |
| 3665 |
undo->state == TRX_UNDO_PREPARED_IN_TC); |
| 3666 |
} |
| 3667 |
|
| 3668 |
dberr_t trx_set_prepared_in_tc_for_mysql(trx_t *trx) { |
| 3669 |
ut_a(trx->state.load(std::memory_order_relaxed) == TRX_STATE_PREPARED); |
| 3670 |
|
| 3671 |
/* For GTID persistence we need update undo segment. */ |
| 3672 |
auto db_err = trx_undo_gtid_add_update_undo(trx, true, false); |
| 3673 |
if (db_err != DB_SUCCESS) { |
| 3674 |
return (db_err); |
| 3675 |
} |
| 3676 |
|
| 3677 |
trx->op_info = "marking transaction as prepared in TC"; |
| 3678 |
|
| 3679 |
trx_set_prepared_in_tc(trx); |
| 3680 |
|
| 3681 |
trx->op_info = ""; |
| 3682 |
|
| 3683 |
return (DB_SUCCESS); |
| 3684 |
} |
| 3685 |
|
| 3686 |
static void trx_flush_logs(trx_t *trx, lsn_t lsn) { |
| 3687 |
if (lsn == 0) { |
| 3688 |
return; |
| 3689 |
} |
| 3690 |
switch (thd_requested_durability(trx->mysql_thd)) { |
| 3691 |
case HA_IGNORE_DURABILITY: |
| 3692 |
/* We set the HA_IGNORE_DURABILITY during prepare phase of |
| 3693 |
binlog group commit to not flush redo log for every transaction |
| 3694 |
here. So that we can flush prepared records of transactions to |
| 3695 |
redo log in a group right before writing them to binary log |
| 3696 |
during flush stage of binlog group commit. */ |
| 3697 |
break; |
| 3698 |
case HA_REGULAR_DURABILITY: |
| 3699 |
/* Depending on the my.cnf options, we may now write the log |
| 3700 |
buffer to the log files, making the prepared state of the |
| 3701 |
transaction durable if the OS does not crash. We may also |
| 3702 |
flush the log files to disk, making the prepared state of the |
| 3703 |
transaction durable also at an OS crash or a power outage. |
| 3704 |
|
| 3705 |
The idea in InnoDB's group prepare is that a group of |
| 3706 |
transactions gather behind a trx doing a physical disk write |
| 3707 |
to log files, and when that physical write has been completed, |
| 3708 |
one of those transactions does a write which prepares the whole |
| 3709 |
group. Note that this group prepare will only bring benefit if |
| 3710 |
there are > 2 users in the database. Then at least 2 users can |
| 3711 |
gather behind one doing the physical log write to disk. |
| 3712 |
|
| 3713 |
We must not be holding any mutexes or latches here. */ |
| 3714 |
|
| 3715 |
/* We should trust trx->ddl_operation instead of |
| 3716 |
ddl_must_flush here */ |
| 3717 |
trx->ddl_must_flush = false; |
| 3718 |
trx_flush_log_if_needed(lsn, trx); |
| 3719 |
} |
| 3720 |
} |