| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2007, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/lock0priv.h |
| 29 |
Lock module internal structures and methods. |
| 30 |
|
| 31 |
Created July 12, 2007 Vasil Dimov |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef lock0priv_h |
| 35 |
#define lock0priv_h |
| 36 |
|
| 37 |
#ifndef LOCK_MODULE_IMPLEMENTATION |
| 38 |
/* If you need to access members of the structures defined in this |
| 39 |
file, please write appropriate functions that retrieve them and put |
| 40 |
those functions in lock/ */ |
| 41 |
#error Do not include lock0priv.h outside of the lock/ module |
| 42 |
#endif |
| 43 |
|
| 44 |
#include "dict0types.h" |
| 45 |
#include "hash0hash.h" |
| 46 |
#include "trx0types.h" |
| 47 |
#include "univ.i" |
| 48 |
#include "ut0bitset.h" |
| 49 |
|
| 50 |
#include <scope_guard.h> |
| 51 |
#include <utility> |
| 52 |
|
| 53 |
/** A table lock */ |
| 54 |
struct lock_table_t { |
| 55 |
dict_table_t *table; /*!< database table in dictionary |
| 56 |
cache */ |
| 57 |
UT_LIST_NODE_T(lock_t) |
| 58 |
locks; /*!< list of locks on the same |
| 59 |
table */ |
| 60 |
/** Print the table lock into the given output stream |
| 61 |
@param[in,out] out the output stream |
| 62 |
@return the given output stream. */ |
| 63 |
std::ostream &print(std::ostream &out) const; |
| 64 |
}; |
| 65 |
|
| 66 |
/** Print the table lock into the given output stream |
| 67 |
@param[in,out] out the output stream |
| 68 |
@return the given output stream. */ |
| 69 |
inline std::ostream &lock_table_t::print(std::ostream &out) const { |
| 70 |
out << "[lock_table_t: name=" << table->name << "]"; |
| 71 |
return (out); |
| 72 |
} |
| 73 |
|
| 74 |
/** The global output operator is overloaded to conveniently |
| 75 |
print the lock_table_t object into the given output stream. |
| 76 |
@param[in,out] out the output stream |
| 77 |
@param[in] lock the table lock |
| 78 |
@return the given output stream */ |
| 79 |
inline std::ostream &operator<<(std::ostream &out, const lock_table_t &lock) { |
| 80 |
return (lock.print(out)); |
| 81 |
} |
| 82 |
|
| 83 |
/** Record lock for a page */ |
| 84 |
struct lock_rec_t { |
| 85 |
/** The id of the page on which records referenced by this lock's bitmap are |
| 86 |
located. */ |
| 87 |
page_id_t page_id; |
| 88 |
/** number of bits in the lock bitmap; |
| 89 |
Must be divisible by 8. |
| 90 |
NOTE: the lock bitmap is placed immediately after the lock struct */ |
| 91 |
uint32_t n_bits; |
| 92 |
|
| 93 |
/** Print the record lock into the given output stream |
| 94 |
@param[in,out] out the output stream |
| 95 |
@return the given output stream. */ |
| 96 |
std::ostream &print(std::ostream &out) const; |
| 97 |
}; |
| 98 |
|
| 99 |
/** Print the record lock into the given output stream |
| 100 |
@param[in,out] out the output stream |
| 101 |
@return the given output stream. */ |
| 102 |
inline std::ostream &lock_rec_t::print(std::ostream &out) const { |
| 103 |
return out << "[lock_rec_t: page_id=" << page_id << ", n_bits=" << n_bits |
| 104 |
<< "]"; |
| 105 |
} |
| 106 |
|
| 107 |
inline std::ostream &operator<<(std::ostream &out, const lock_rec_t &lock) { |
| 108 |
return (lock.print(out)); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
Checks if the `mode` is LOCK_S or LOCK_X (possibly ORed with LOCK_WAIT or |
| 113 |
LOCK_REC) which means the lock is a |
| 114 |
Next Key Lock, a.k.a. LOCK_ORDINARY, as opposed to Predicate Lock, |
| 115 |
GAP lock, Insert Intention or Record Lock. |
| 116 |
@param mode A mode and flags, of a lock. |
| 117 |
@return true iff the only bits set in `mode` are LOCK_S or LOCK_X and optionally |
| 118 |
LOCK_WAIT or LOCK_REC */ |
| 119 |
static inline bool lock_mode_is_next_key_lock(ulint mode) { |
| 120 |
static_assert(LOCK_ORDINARY == 0, "LOCK_ORDINARY must be 0 (no flags)"); |
| 121 |
ut_ad((mode & LOCK_TABLE) == 0); |
| 122 |
mode &= ~(LOCK_WAIT | LOCK_REC); |
| 123 |
ut_ad((mode & LOCK_WAIT) == 0); |
| 124 |
ut_ad((mode & LOCK_TYPE_MASK) == 0); |
| 125 |
ut_ad(((mode & ~(LOCK_MODE_MASK)) == LOCK_ORDINARY) == |
| 126 |
(mode == LOCK_S || mode == LOCK_X)); |
| 127 |
return (mode & ~(LOCK_MODE_MASK)) == LOCK_ORDINARY; |
| 128 |
} |
| 129 |
|
| 130 |
/** Gets the nth bit of a record lock. |
| 131 |
@param[in] lock record lock |
| 132 |
@param[in] i index of the bit |
| 133 |
@return true if bit set also if i == ULINT_UNDEFINED return false */ |
| 134 |
static inline bool lock_rec_get_nth_bit(const lock_t *lock, ulint i); |
| 135 |
|
| 136 |
/** Lock struct; protected by lock_sys latches */ |
| 137 |
struct alignas(8 /* For efficient Bitmap::find_set */) lock_t { |
| 138 |
/** transaction owning the lock */ |
| 139 |
trx_t *trx; |
| 140 |
|
| 141 |
/** list of the locks of the transaction */ |
| 142 |
UT_LIST_NODE_T(lock_t) trx_locks; |
| 143 |
|
| 144 |
/** Index for a record lock */ |
| 145 |
dict_index_t *index; |
| 146 |
|
| 147 |
/** Hash chain node for a record lock. The link node in a singly |
| 148 |
linked list, used by the hash table. */ |
| 149 |
lock_t *hash; |
| 150 |
|
| 151 |
union { |
| 152 |
/** Table lock */ |
| 153 |
lock_table_t tab_lock; |
| 154 |
|
| 155 |
/** Record lock */ |
| 156 |
lock_rec_t rec_lock; |
| 157 |
}; |
| 158 |
|
| 159 |
#ifdef HAVE_PSI_THREAD_INTERFACE |
| 160 |
#ifdef HAVE_PSI_DATA_LOCK_INTERFACE |
| 161 |
/** Performance schema thread that created the lock. */ |
| 162 |
ulonglong m_psi_internal_thread_id; |
| 163 |
|
| 164 |
/** Performance schema event that created the lock. */ |
| 165 |
ulonglong m_psi_event_id; |
| 166 |
#endif /* HAVE_PSI_DATA_LOCK_INTERFACE */ |
| 167 |
#endif /* HAVE_PSI_THREAD_INTERFACE */ |
| 168 |
|
| 169 |
/** The lock type and mode bit flags. |
| 170 |
LOCK_GAP or LOCK_REC_NOT_GAP, LOCK_INSERT_INTENTION, wait flag, ORed */ |
| 171 |
uint32_t type_mode; |
| 172 |
|
| 173 |
#if defined(UNIV_DEBUG) |
| 174 |
/** Timestamp when it was created. */ |
| 175 |
uint64_t m_seq; |
| 176 |
#endif /* UNIV_DEBUG */ |
| 177 |
|
| 178 |
/** Unlock the GAP Lock part of this Next Key Lock */ |
| 179 |
void unlock_gap_lock() { |
| 180 |
ut_ad(!is_gap()); |
| 181 |
ut_ad(!is_insert_intention()); |
| 182 |
ut_ad(is_next_key_lock()); |
| 183 |
|
| 184 |
type_mode |= LOCK_REC_NOT_GAP; |
| 185 |
} |
| 186 |
|
| 187 |
/** Determine if the lock object is a record lock. |
| 188 |
@return true if record lock, false otherwise. */ |
| 189 |
bool is_record_lock() const { return (type() == LOCK_REC); } |
| 190 |
|
| 191 |
/** Determine if it is predicate lock. |
| 192 |
@return true if predicate lock, false otherwise. */ |
| 193 |
bool is_predicate() const { |
| 194 |
return (type_mode & (LOCK_PREDICATE | LOCK_PRDT_PAGE)); |
| 195 |
} |
| 196 |
|
| 197 |
/** @return true if the lock wait flag is set */ |
| 198 |
bool is_waiting() const { return (type_mode & LOCK_WAIT); } |
| 199 |
|
| 200 |
/** @return true if the gap lock bit is set */ |
| 201 |
bool is_gap() const { return (type_mode & LOCK_GAP); } |
| 202 |
|
| 203 |
/** @return true if the not gap lock bit is set */ |
| 204 |
bool is_record_not_gap() const { return (type_mode & LOCK_REC_NOT_GAP); } |
| 205 |
|
| 206 |
/** @return true iff the lock is a Next Key Lock */ |
| 207 |
bool is_next_key_lock() const { |
| 208 |
return is_record_lock() && lock_mode_is_next_key_lock(type_mode); |
| 209 |
} |
| 210 |
|
| 211 |
/** @return true if the insert intention bit is set */ |
| 212 |
bool is_insert_intention() const { |
| 213 |
return (type_mode & LOCK_INSERT_INTENTION); |
| 214 |
} |
| 215 |
|
| 216 |
/** @return true iff this lock is (at least) on supremum pseudo-record */ |
| 217 |
bool includes_supremum() const { |
| 218 |
return (lock_rec_get_nth_bit(this, PAGE_HEAP_NO_SUPREMUM)); |
| 219 |
} |
| 220 |
|
| 221 |
/** @return the lock mode */ |
| 222 |
uint32_t type() const { return (type_mode & LOCK_TYPE_MASK); } |
| 223 |
|
| 224 |
/** @return the precise lock mode */ |
| 225 |
lock_mode mode() const { |
| 226 |
return (static_cast<lock_mode>(type_mode & LOCK_MODE_MASK)); |
| 227 |
} |
| 228 |
|
| 229 |
/** Get lock hash table |
| 230 |
@return lock hash table */ |
| 231 |
Locks_hashtable &hash_table() const { return lock_hash_get(type_mode); } |
| 232 |
|
| 233 |
/** @return the transaction's query thread state. */ |
| 234 |
trx_que_t trx_que_state() const { return (trx->lock.que_state); } |
| 235 |
|
| 236 |
/** Print the lock object into the given output stream. |
| 237 |
@param[in,out] out the output stream |
| 238 |
@return the given output stream. */ |
| 239 |
std::ostream &print(std::ostream &out) const; |
| 240 |
|
| 241 |
/** Convert the member 'type_mode' into a human readable string. |
| 242 |
@return human readable string */ |
| 243 |
std::string type_mode_string() const; |
| 244 |
|
| 245 |
/* @return the string/text representation of the record type. */ |
| 246 |
const char *type_string() const { |
| 247 |
switch (type_mode & LOCK_TYPE_MASK) { |
| 248 |
case LOCK_REC: |
| 249 |
return ("LOCK_REC"); |
| 250 |
case LOCK_TABLE: |
| 251 |
return ("LOCK_TABLE"); |
| 252 |
default: |
| 253 |
ut_error; |
| 254 |
} |
| 255 |
} |
| 256 |
/** @overload */ |
| 257 |
Bitset<const byte> bitset() const { |
| 258 |
ut_ad(is_record_lock()); |
| 259 |
/* On a 32-bit system alignof(uint64_t) might be 4. Still, the |
| 260 |
Bitmap::find_set goes into slow path if address is not a multiple of 8. */ |
| 261 |
static_assert(8 <= alignof(lock_t), |
| 262 |
"lock_t and thus the bitmap after lock_t should be aligned " |
| 263 |
"for efficient 64-bit access"); |
| 264 |
const byte *bitmap = (const byte *)&this[1]; |
| 265 |
/* static_assert verified the theory, but the actual allocation algorithm |
| 266 |
used could assign a wrong address in practice */ |
| 267 |
ut_ad(reinterpret_cast<uintptr_t>(bitmap) % 8 == 0); |
| 268 |
ut_ad(rec_lock.n_bits % 8 == 0); |
| 269 |
return {bitmap, rec_lock.n_bits / 8}; |
| 270 |
} |
| 271 |
/** Gets access to the LOCK_REC's bitmap, which indicates heap_no-s, which are |
| 272 |
the subject of this lock request. This should be used directly only in the |
| 273 |
lock-sys code. Use lock_rec_bitmap_reset(), lock_rec_reset_nth_bit(), |
| 274 |
lock_rec_set_nth_bit(), and lock_rec_get_nth_bit() wrappers instead. In |
| 275 |
particular this bitset might be shorter than actual number of heap_no-s on the |
| 276 |
page! */ |
| 277 |
Bitset<byte> bitset() { |
| 278 |
auto immutable = const_cast<const ib_lock_t *>(this)->bitset(); |
| 279 |
return {const_cast<byte *>(immutable.data()), immutable.size_bytes()}; |
| 280 |
} |
| 281 |
}; |
| 282 |
|
| 283 |
struct TableLockGetNode { |
| 284 |
/** Functor for accessing the embedded node within a table lock. */ |
| 285 |
static const ut_list_node<lock_t> &get_node(const lock_t &lock) { |
| 286 |
return lock.tab_lock.locks; |
| 287 |
} |
| 288 |
}; |
| 289 |
|
| 290 |
UT_LIST_NODE_GETTER_DEFINITION(lock_t, trx_locks) |
| 291 |
|
| 292 |
/** Convert the member 'type_mode' into a human readable string. |
| 293 |
@return human readable string */ |
| 294 |
inline std::string lock_t::type_mode_string() const { |
| 295 |
std::ostringstream sout; |
| 296 |
sout << type_string(); |
| 297 |
sout << " | " << lock_mode_string(mode()); |
| 298 |
|
| 299 |
if (is_record_not_gap()) { |
| 300 |
sout << " | LOCK_REC_NOT_GAP"; |
| 301 |
} |
| 302 |
|
| 303 |
if (is_waiting()) { |
| 304 |
sout << " | LOCK_WAIT"; |
| 305 |
} |
| 306 |
|
| 307 |
if (is_gap()) { |
| 308 |
sout << " | LOCK_GAP"; |
| 309 |
} |
| 310 |
|
| 311 |
if (is_insert_intention()) { |
| 312 |
sout << " | LOCK_INSERT_INTENTION"; |
| 313 |
} |
| 314 |
return (sout.str()); |
| 315 |
} |
| 316 |
|
| 317 |
inline std::ostream &lock_t::print(std::ostream &out) const { |
| 318 |
out << "[lock_t: type_mode=" << type_mode << "(" << type_mode_string() << ")"; |
| 319 |
|
| 320 |
if (is_record_lock()) { |
| 321 |
out << rec_lock; |
| 322 |
} else { |
| 323 |
out << tab_lock; |
| 324 |
} |
| 325 |
|
| 326 |
out << "]"; |
| 327 |
return (out); |
| 328 |
} |
| 329 |
|
| 330 |
inline std::ostream &operator<<(std::ostream &out, const lock_t &lock) { |
| 331 |
return (lock.print(out)); |
| 332 |
} |
| 333 |
|
| 334 |
#ifdef UNIV_DEBUG |
| 335 |
extern bool lock_print_waits; |
| 336 |
#endif /* UNIV_DEBUG */ |
| 337 |
|
| 338 |
/* Safety margin when creating a new record lock: this many extra records |
| 339 |
can be inserted to the page without need to create a lock with a bigger |
| 340 |
bitmap */ |
| 341 |
|
| 342 |
static const ulint LOCK_PAGE_BITMAP_MARGIN = 64; |
| 343 |
|
| 344 |
/* An explicit record lock affects both the record and the gap before it. |
| 345 |
An implicit x-lock does not affect the gap, it only locks the index |
| 346 |
record from read or update. |
| 347 |
|
| 348 |
If a transaction has modified or inserted an index record, then |
| 349 |
it owns an implicit x-lock on the record. On a secondary index record, |
| 350 |
a transaction has an implicit x-lock also if it has modified the |
| 351 |
clustered index record, the max trx id of the page where the secondary |
| 352 |
index record resides is >= trx id of the transaction (or database recovery |
| 353 |
is running), and there are no explicit non-gap lock requests on the |
| 354 |
secondary index record. |
| 355 |
|
| 356 |
This complicated definition for a secondary index comes from the |
| 357 |
implementation: we want to be able to determine if a secondary index |
| 358 |
record has an implicit x-lock, just by looking at the present clustered |
| 359 |
index record, not at the historical versions of the record. The |
| 360 |
complicated definition can be explained to the user so that there is |
| 361 |
nondeterminism in the access path when a query is answered: we may, |
| 362 |
or may not, access the clustered index record and thus may, or may not, |
| 363 |
bump into an x-lock set there. |
| 364 |
|
| 365 |
Different transaction can have conflicting locks set on the gap at the |
| 366 |
same time. The locks on the gap are purely inhibitive: an insert cannot |
| 367 |
be made, or a select cursor may have to wait if a different transaction |
| 368 |
has a conflicting lock on the gap. An x-lock on the gap does not give |
| 369 |
the right to insert into the gap. |
| 370 |
|
| 371 |
An explicit lock can be placed on a user record or the supremum record of |
| 372 |
a page. The locks on the supremum record are always thought to be of the gap |
| 373 |
type, though the gap bit is not set. When we perform an update of a record |
| 374 |
where the size of the record changes, we may temporarily store its explicit |
| 375 |
locks on the infimum record of the page, though the infimum otherwise never |
| 376 |
carries locks. |
| 377 |
|
| 378 |
A waiting record lock can also be of the gap type. A waiting lock request |
| 379 |
can be granted when there is no conflicting mode lock request by another |
| 380 |
transaction ahead of it in the explicit lock queue. |
| 381 |
|
| 382 |
In version 4.0.5 we added yet another explicit lock type: LOCK_REC_NOT_GAP. |
| 383 |
It only locks the record it is placed on, not the gap before the record. |
| 384 |
This lock type is necessary to emulate an Oracle-like READ COMMITTED isolation |
| 385 |
level. |
| 386 |
|
| 387 |
------------------------------------------------------------------------- |
| 388 |
RULE 1: If there is an implicit x-lock on a record, and there are non-gap |
| 389 |
------- |
| 390 |
lock requests waiting in the queue, then the transaction holding the implicit |
| 391 |
x-lock also has an explicit non-gap record x-lock. Therefore, as locks are |
| 392 |
released, we can grant locks to waiting lock requests purely by looking at |
| 393 |
the explicit lock requests in the queue. |
| 394 |
|
| 395 |
RULE 3: Different transactions cannot have conflicting granted non-gap locks |
| 396 |
------- |
| 397 |
on a record at the same time. However, they can have conflicting granted gap |
| 398 |
locks. |
| 399 |
RULE 4: If a there is a waiting lock request in a queue, no lock request, |
| 400 |
------- |
| 401 |
gap or not, can be inserted ahead of it in the queue. In record deletes |
| 402 |
and page splits new gap type locks can be created by the database manager |
| 403 |
for a transaction, and without rule 4, the waits-for graph of transactions |
| 404 |
might become cyclic without the database noticing it, as the deadlock check |
| 405 |
is only performed when a transaction itself requests a lock! |
| 406 |
------------------------------------------------------------------------- |
| 407 |
|
| 408 |
An insert is allowed to a gap if there are no explicit lock requests by |
| 409 |
other transactions on the next record. It does not matter if these lock |
| 410 |
requests are granted or waiting, gap bit set or not, with the exception |
| 411 |
that a gap type request set by another transaction to wait for |
| 412 |
its turn to do an insert is ignored. On the other hand, an |
| 413 |
implicit x-lock by another transaction does not prevent an insert, which |
| 414 |
allows for more concurrency when using an Oracle-style sequence number |
| 415 |
generator for the primary key with many transactions doing inserts |
| 416 |
concurrently. |
| 417 |
|
| 418 |
A modify of a record is allowed if the transaction has an x-lock on the |
| 419 |
record, or if other transactions do not have any non-gap lock requests on the |
| 420 |
record. |
| 421 |
|
| 422 |
A read of a single user record with a cursor is allowed if the transaction |
| 423 |
has a non-gap explicit, or an implicit lock on the record, or if the other |
| 424 |
transactions have no x-lock requests on the record. At a page supremum a |
| 425 |
read is always allowed. |
| 426 |
|
| 427 |
In summary, an implicit lock is seen as a granted x-lock only on the |
| 428 |
record, not on the gap. An explicit lock with no gap bit set is a lock |
| 429 |
both on the record and the gap. If the gap bit is set, the lock is only |
| 430 |
on the gap. Different transaction cannot own conflicting locks on the |
| 431 |
record at the same time, but they may own conflicting locks on the gap. |
| 432 |
Granted locks on a record give an access right to the record, but gap type |
| 433 |
locks just inhibit operations. |
| 434 |
|
| 435 |
NOTE: Finding out if some transaction has an implicit x-lock on a secondary |
| 436 |
index record can be cumbersome. We may have to look at previous versions of |
| 437 |
the corresponding clustered index record to find out if a delete marked |
| 438 |
secondary index record was delete marked by an active transaction, not by |
| 439 |
a committed one. |
| 440 |
|
| 441 |
FACT A: If a transaction has inserted a row, it can delete it any time |
| 442 |
without need to wait for locks. |
| 443 |
|
| 444 |
PROOF: The transaction has an implicit x-lock on every index record inserted |
| 445 |
for the row, and can thus modify each record without the need to wait. Q.E.D. |
| 446 |
|
| 447 |
FACT B: If a transaction has read some result set with a cursor, it can read |
| 448 |
it again, and retrieves the same result set, if it has not modified the |
| 449 |
result set in the meantime. Hence, there is no phantom problem. If the |
| 450 |
biggest record, in the alphabetical order, touched by the cursor is removed, |
| 451 |
a lock wait may occur, otherwise not. |
| 452 |
|
| 453 |
PROOF: When a read cursor proceeds, it sets an s-lock on each user record |
| 454 |
it passes, and a gap type s-lock on each page supremum. The cursor must |
| 455 |
wait until it has these locks granted. Then no other transaction can |
| 456 |
have a granted x-lock on any of the user records, and therefore cannot |
| 457 |
modify the user records. Neither can any other transaction insert into |
| 458 |
the gaps which were passed over by the cursor. Page splits and merges, |
| 459 |
and removal of obsolete versions of records do not affect this, because |
| 460 |
when a user record or a page supremum is removed, the next record inherits |
| 461 |
its locks as gap type locks, and therefore blocks inserts to the same gap. |
| 462 |
Also, if a page supremum is inserted, it inherits its locks from the successor |
| 463 |
record. When the cursor is positioned again at the start of the result set, |
| 464 |
the records it will touch on its course are either records it touched |
| 465 |
during the last pass or new inserted page supremums. It can immediately |
| 466 |
access all these records, and when it arrives at the biggest record, it |
| 467 |
notices that the result set is complete. If the biggest record was removed, |
| 468 |
lock wait can occur because the next record only inherits a gap type lock, |
| 469 |
and a wait may be needed. Q.E.D. */ |
| 470 |
|
| 471 |
/* If an index record should be changed or a new inserted, we must check |
| 472 |
the lock on the record or the next. When a read cursor starts reading, |
| 473 |
we will set a record level s-lock on each record it passes, except on the |
| 474 |
initial record on which the cursor is positioned before we start to fetch |
| 475 |
records. Our index tree search has the convention that the B-tree |
| 476 |
cursor is positioned BEFORE the first possibly matching record in |
| 477 |
the search. Optimizations are possible here: if the record is searched |
| 478 |
on an equality condition to a unique key, we could actually set a special |
| 479 |
lock on the record, a lock which would not prevent any insert before |
| 480 |
this record. In the next key locking an x-lock set on a record also |
| 481 |
prevents inserts just before that record. |
| 482 |
There are special infimum and supremum records on each page. |
| 483 |
A supremum record can be locked by a read cursor. This records cannot be |
| 484 |
updated but the lock prevents insert of a user record to the end of |
| 485 |
the page. |
| 486 |
Next key locks will prevent the phantom problem where new rows |
| 487 |
could appear to SELECT result sets after the select operation has been |
| 488 |
performed. Prevention of phantoms ensures the serilizability of |
| 489 |
transactions. |
| 490 |
What should we check if an insert of a new record is wanted? |
| 491 |
Only the lock on the next record on the same page, because also the |
| 492 |
supremum record can carry a lock. An s-lock prevents insertion, but |
| 493 |
what about an x-lock? If it was set by a searched update, then there |
| 494 |
is implicitly an s-lock, too, and the insert should be prevented. |
| 495 |
What if our transaction owns an x-lock to the next record, but there is |
| 496 |
a waiting s-lock request on the next record? If this s-lock was placed |
| 497 |
by a read cursor moving in the ascending order in the index, we cannot |
| 498 |
do the insert immediately, because when we finally commit our transaction, |
| 499 |
the read cursor should see also the new inserted record. So we should |
| 500 |
move the read cursor backward from the next record for it to pass over |
| 501 |
the new inserted record. This move backward may be too cumbersome to |
| 502 |
implement. If we in this situation just enqueue a second x-lock request |
| 503 |
for our transaction on the next record, then the deadlock mechanism |
| 504 |
notices a deadlock between our transaction and the s-lock request |
| 505 |
transaction. This seems to be an ok solution. |
| 506 |
We could have the convention that granted explicit record locks, |
| 507 |
lock the corresponding records from changing, and also lock the gaps |
| 508 |
before them from inserting. A waiting explicit lock request locks the gap |
| 509 |
before from inserting. Implicit record x-locks, which we derive from the |
| 510 |
transaction id in the clustered index record, only lock the record itself |
| 511 |
from modification, not the gap before it from inserting. |
| 512 |
How should we store update locks? If the search is done by a unique |
| 513 |
key, we could just modify the record trx id. Otherwise, we could put a record |
| 514 |
x-lock on the record. If the update changes ordering fields of the |
| 515 |
clustered index record, the inserted new record needs no record lock in |
| 516 |
lock table, the trx id is enough. The same holds for a secondary index |
| 517 |
record. Searched delete is similar to update. |
| 518 |
|
| 519 |
PROBLEM: |
| 520 |
What about waiting lock requests? If a transaction is waiting to make an |
| 521 |
update to a record which another modified, how does the other transaction |
| 522 |
know to send the end-lock-wait signal to the waiting transaction? If we have |
| 523 |
the convention that a transaction may wait for just one lock at a time, how |
| 524 |
do we preserve it if lock wait ends? |
| 525 |
|
| 526 |
PROBLEM: |
| 527 |
Checking the trx id label of a secondary index record. In the case of a |
| 528 |
modification, not an insert, is this necessary? A secondary index record |
| 529 |
is modified only by setting or resetting its deleted flag. A secondary index |
| 530 |
record contains fields to uniquely determine the corresponding clustered |
| 531 |
index record. A secondary index record is therefore only modified if we |
| 532 |
also modify the clustered index record, and the trx id checking is done |
| 533 |
on the clustered index record, before we come to modify the secondary index |
| 534 |
record. So, in the case of delete marking or unmarking a secondary index |
| 535 |
record, we do not have to care about trx ids, only the locks in the lock |
| 536 |
table must be checked. In the case of a select from a secondary index, the |
| 537 |
trx id is relevant, and in this case we may have to search the clustered |
| 538 |
index record. |
| 539 |
|
| 540 |
PROBLEM: How to update record locks when page is split or merged, or |
| 541 |
-------------------------------------------------------------------- |
| 542 |
a record is deleted or updated? |
| 543 |
If the size of fields in a record changes, we perform the update by |
| 544 |
a delete followed by an insert. How can we retain the locks set or |
| 545 |
waiting on the record? Because a record lock is indexed in the bitmap |
| 546 |
by the heap number of the record, when we remove the record from the |
| 547 |
record list, it is possible still to keep the lock bits. If the page |
| 548 |
is reorganized, we could make a table of old and new heap numbers, |
| 549 |
and permute the bitmaps in the locks accordingly. We can add to the |
| 550 |
table a row telling where the updated record ended. If the update does |
| 551 |
not require a reorganization of the page, we can simply move the lock |
| 552 |
bits for the updated record to the position determined by its new heap |
| 553 |
number (we may have to allocate a new lock, if we run out of the bitmap |
| 554 |
in the old one). |
| 555 |
A more complicated case is the one where the reinsertion of the |
| 556 |
updated record is done pessimistically, because the structure of the |
| 557 |
tree may change. |
| 558 |
|
| 559 |
PROBLEM: If a supremum record is removed in a page merge, or a record |
| 560 |
--------------------------------------------------------------------- |
| 561 |
removed in a purge, what to do to the waiting lock requests? In a split to |
| 562 |
the right, we just move the lock requests to the new supremum. If a record |
| 563 |
is removed, we could move the waiting lock request to its inheritor, the |
| 564 |
next record in the index. But, the next record may already have lock |
| 565 |
requests on its own queue. A new deadlock check should be made then. Maybe |
| 566 |
it is easier just to release the waiting transactions. They can then enqueue |
| 567 |
new lock requests on appropriate records. |
| 568 |
|
| 569 |
PROBLEM: When a record is inserted, what locks should it inherit from the |
| 570 |
------------------------------------------------------------------------- |
| 571 |
upper neighbor? An insert of a new supremum record in a page split is |
| 572 |
always possible, but an insert of a new user record requires that the upper |
| 573 |
neighbor does not have any lock requests by other transactions, granted or |
| 574 |
waiting, in its lock queue. Solution: We can copy the locks as gap type |
| 575 |
locks, so that also the waiting locks are transformed to granted gap type |
| 576 |
locks on the inserted record. */ |
| 577 |
|
| 578 |
/* LOCK COMPATIBILITY MATRIX |
| 579 |
IS IX S X AI |
| 580 |
IS + + + - + |
| 581 |
IX + + - - + |
| 582 |
S + - + - - |
| 583 |
X - - - - - |
| 584 |
AI + + - - - |
| 585 |
* |
| 586 |
Note that for rows, InnoDB only acquires S or X locks. |
| 587 |
For tables, InnoDB normally acquires IS or IX locks. |
| 588 |
S or X table locks are only acquired for LOCK TABLES. |
| 589 |
Auto-increment (AI) locks are needed because of |
| 590 |
statement-level MySQL binlog. |
| 591 |
See also lock_mode_compatible(). |
| 592 |
*/ |
| 593 |
static const byte lock_compatibility_matrix[5][5] = { |
| 594 |
/** IS IX S X AI */ |
| 595 |
/* IS */ {true, true, true, false, true}, |
| 596 |
/* IX */ {true, true, false, false, true}, |
| 597 |
/* S */ {true, false, true, false, false}, |
| 598 |
/* X */ {false, false, false, false, false}, |
| 599 |
/* AI */ {true, true, false, false, false}}; |
| 600 |
|
| 601 |
/* STRONGER-OR-EQUAL RELATION (mode1=row, mode2=column) |
| 602 |
IS IX S X AI |
| 603 |
IS + - - - - |
| 604 |
IX + + - - - |
| 605 |
S + - + - - |
| 606 |
X + + + + + |
| 607 |
AI - - - - + |
| 608 |
See lock_mode_stronger_or_eq(). |
| 609 |
*/ |
| 610 |
static const byte lock_strength_matrix[5][5] = { |
| 611 |
/** IS IX S X AI */ |
| 612 |
/* IS */ {true, false, false, false, false}, |
| 613 |
/* IX */ {true, true, false, false, false}, |
| 614 |
/* S */ {true, false, true, false, false}, |
| 615 |
/* X */ {true, true, true, true, true}, |
| 616 |
/* AI */ {false, false, false, false, true}}; |
| 617 |
|
| 618 |
/** Maximum depth of the DFS stack. */ |
| 619 |
constexpr uint32_t MAX_STACK_SIZE = 4096; |
| 620 |
|
| 621 |
constexpr uint32_t PRDT_HEAPNO = PAGE_HEAP_NO_INFIMUM; |
| 622 |
/** Record locking request status */ |
| 623 |
enum lock_rec_req_status { |
| 624 |
/** Failed to acquire a lock */ |
| 625 |
LOCK_REC_FAIL, |
| 626 |
/** Succeeded in acquiring a lock (implicit or already acquired) */ |
| 627 |
LOCK_REC_SUCCESS, |
| 628 |
/** Explicitly created a new lock */ |
| 629 |
LOCK_REC_SUCCESS_CREATED |
| 630 |
}; |
| 631 |
|
| 632 |
/** |
| 633 |
Record lock ID */ |
| 634 |
struct RecID { |
| 635 |
/** Constructor |
| 636 |
@param[in] lock Record lock |
| 637 |
@param[in] heap_no Heap number in the page */ |
| 638 |
RecID(const lock_t *lock, ulint heap_no) |
| 639 |
: RecID(lock->rec_lock.page_id, heap_no) { |
| 640 |
ut_ad(lock->is_record_lock()); |
| 641 |
} |
| 642 |
|
| 643 |
/** Constructor |
| 644 |
@param[in] page_id Tablespace ID and page number within space |
| 645 |
@param[in] heap_no Heap number in the page */ |
| 646 |
RecID(page_id_t page_id, uint32_t heap_no) |
| 647 |
: m_page_id(page_id), |
| 648 |
m_heap_no(heap_no), |
| 649 |
m_hash_value(lock_rec_hash_value(page_id)) { |
| 650 |
ut_ad(m_page_id.space() < UINT32_MAX); |
| 651 |
ut_ad(m_page_id.page_no() < UINT32_MAX); |
| 652 |
ut_ad(m_heap_no < UINT32_MAX); |
| 653 |
} |
| 654 |
|
| 655 |
/** Constructor |
| 656 |
@param[in] block Block in a tablespace |
| 657 |
@param[in] heap_no Heap number in the block */ |
| 658 |
RecID(const buf_block_t *block, ulint heap_no) |
| 659 |
: RecID(block->get_page_id(), heap_no) {} |
| 660 |
|
| 661 |
/** |
| 662 |
@return the hashed value of {space, page_no} */ |
| 663 |
uint64_t hash_value() const { return (m_hash_value); } |
| 664 |
|
| 665 |
/** @return true if it's the supremum record */ |
| 666 |
bool is_supremum() const { return (m_heap_no == PAGE_HEAP_NO_SUPREMUM); } |
| 667 |
|
| 668 |
/* Check if the rec id matches the lock instance. |
| 669 |
@param[i] lock Lock to compare with |
| 670 |
@return true if <space, page_no, heap_no> matches the lock. */ |
| 671 |
inline bool matches(const lock_t *lock) const; |
| 672 |
|
| 673 |
const page_id_t &get_page_id() const { return m_page_id; } |
| 674 |
|
| 675 |
/** Tablespace ID and page number within space */ |
| 676 |
page_id_t m_page_id; |
| 677 |
|
| 678 |
/** |
| 679 |
Heap number within the page */ |
| 680 |
uint32_t m_heap_no; |
| 681 |
|
| 682 |
/** |
| 683 |
Hash generated from record's location which will be used to get lock queue for |
| 684 |
this record. */ |
| 685 |
uint64_t m_hash_value; |
| 686 |
}; |
| 687 |
|
| 688 |
/** |
| 689 |
Create record locks */ |
| 690 |
class RecLock { |
| 691 |
public: |
| 692 |
/** |
| 693 |
@param[in,out] thr Transaction query thread requesting the record |
| 694 |
lock |
| 695 |
@param[in] index Index on which record lock requested |
| 696 |
@param[in] rec_id Record lock tuple {space, page_no, heap_no} |
| 697 |
@param[in] mode The lock mode */ |
| 698 |
RecLock(que_thr_t *thr, dict_index_t *index, const RecID &rec_id, ulint mode) |
| 699 |
: m_thr(thr), |
| 700 |
m_trx(thr_get_trx(thr)), |
| 701 |
m_mode(mode), |
| 702 |
m_index(index), |
| 703 |
m_rec_id(rec_id) { |
| 704 |
ut_ad(is_predicate_lock(m_mode)); |
| 705 |
|
| 706 |
init(nullptr); |
| 707 |
} |
| 708 |
|
| 709 |
/** |
| 710 |
@param[in,out] thr Transaction query thread requesting the record |
| 711 |
lock |
| 712 |
@param[in] index Index on which record lock requested |
| 713 |
@param[in] block Buffer page containing record |
| 714 |
@param[in] heap_no Heap number within the block |
| 715 |
@param[in] mode The lock mode */ |
| 716 |
RecLock(que_thr_t *thr, dict_index_t *index, const buf_block_t *block, |
| 717 |
ulint heap_no, ulint mode) |
| 718 |
: m_thr(thr), |
| 719 |
m_trx(thr_get_trx(thr)), |
| 720 |
m_mode(mode), |
| 721 |
m_index(index), |
| 722 |
m_rec_id(block, heap_no) { |
| 723 |
btr_assert_not_corrupted(block, index); |
| 724 |
|
| 725 |
init(block->frame); |
| 726 |
} |
| 727 |
|
| 728 |
/** |
| 729 |
@param[in] index Index on which record lock requested |
| 730 |
@param[in] rec_id Record lock tuple {space, page_no, heap_no} |
| 731 |
@param[in] mode The lock mode */ |
| 732 |
RecLock(dict_index_t *index, const RecID &rec_id, ulint mode) |
| 733 |
: m_thr(), m_trx(), m_mode(mode), m_index(index), m_rec_id(rec_id) { |
| 734 |
ut_ad(is_predicate_lock(m_mode)); |
| 735 |
|
| 736 |
init(nullptr); |
| 737 |
} |
| 738 |
|
| 739 |
/** |
| 740 |
@param[in] index Index on which record lock requested |
| 741 |
@param[in] block Buffer page containing record |
| 742 |
@param[in] heap_no Heap number within block |
| 743 |
@param[in] mode The lock mode */ |
| 744 |
RecLock(dict_index_t *index, const buf_block_t *block, ulint heap_no, |
| 745 |
ulint mode) |
| 746 |
: m_thr(), |
| 747 |
m_trx(), |
| 748 |
m_mode(mode), |
| 749 |
m_index(index), |
| 750 |
m_rec_id(block, heap_no) { |
| 751 |
btr_assert_not_corrupted(block, index); |
| 752 |
|
| 753 |
init(block->frame); |
| 754 |
} |
| 755 |
|
| 756 |
/** |
| 757 |
Enqueue a lock wait for a transaction. If it is a high priority transaction |
| 758 |
(cannot rollback) then try to jump ahead in the record lock wait queue. Also |
| 759 |
check if async rollback was request for our trx. |
| 760 |
@param[in, out] wait_for The lock that the the joining transaction is |
| 761 |
waiting for |
| 762 |
@param[in] prdt Predicate [optional] |
| 763 |
@return DB_LOCK_WAIT, DB_DEADLOCK, or DB_SUCCESS_LOCKED_REC |
| 764 |
@retval DB_DEADLOCK means that async rollback was requested for our trx |
| 765 |
@retval DB_SUCCESS_LOCKED_REC means that we are High Priority transaction and |
| 766 |
we've managed to jump in front of other waiting |
| 767 |
transactions and got the lock granted, so there |
| 768 |
is no need to wait. */ |
| 769 |
dberr_t add_to_waitq(const lock_t *wait_for, |
| 770 |
const lock_prdt_t *prdt = nullptr); |
| 771 |
|
| 772 |
/** |
| 773 |
Create a lock for a transaction and initialise it. |
| 774 |
@param[in, out] trx Transaction requesting the new lock |
| 775 |
@param[in] prdt Predicate lock (optional) |
| 776 |
@return new lock instance */ |
| 777 |
lock_t *create(trx_t *trx, const lock_prdt_t *prdt = nullptr); |
| 778 |
|
| 779 |
/** |
| 780 |
Create the lock instance |
| 781 |
@param[in, out] trx The transaction requesting the lock |
| 782 |
@param[in, out] index Index on which record lock is required |
| 783 |
@param[in] mode The lock mode desired |
| 784 |
@param[in] rec_id The record id |
| 785 |
@param[in] size Size of the lock + bitmap requested |
| 786 |
@return a record lock instance */ |
| 787 |
static lock_t *lock_alloc(trx_t *trx, dict_index_t *index, ulint mode, |
| 788 |
const RecID &rec_id, ulint size); |
| 789 |
|
| 790 |
private: |
| 791 |
/* |
| 792 |
@return the record lock size in bytes */ |
| 793 |
size_t lock_size() const { return (m_size); } |
| 794 |
|
| 795 |
/** |
| 796 |
Do some checks and prepare for creating a new record lock */ |
| 797 |
void prepare() const; |
| 798 |
|
| 799 |
/** |
| 800 |
Setup the requesting transaction state for lock grant |
| 801 |
@param[in,out] lock Lock for which to change state */ |
| 802 |
void set_wait_state(lock_t *lock); |
| 803 |
|
| 804 |
/** |
| 805 |
Add the lock to the record lock hash and the transaction's lock list |
| 806 |
@param[in,out] lock Newly created record lock to add to the |
| 807 |
rec hash and the transaction lock list */ |
| 808 |
void lock_add(lock_t *lock); |
| 809 |
|
| 810 |
/** |
| 811 |
Setup the context from the requirements */ |
| 812 |
void init(const page_t *page) { |
| 813 |
ut_ad(locksys::owns_page_shard(m_rec_id.get_page_id())); |
| 814 |
ut_ad(!srv_read_only_mode); |
| 815 |
ut_ad(m_index->is_clustered() || !dict_index_is_online_ddl(m_index)); |
| 816 |
ut_ad(m_thr == nullptr || m_trx == thr_get_trx(m_thr)); |
| 817 |
|
| 818 |
m_size = is_predicate_lock(m_mode) ? lock_size(m_mode) : lock_size(page); |
| 819 |
|
| 820 |
/** If rec is the supremum record, then we reset the |
| 821 |
gap and LOCK_REC_NOT_GAP bits, as all locks on the |
| 822 |
supremum are automatically of the gap type */ |
| 823 |
|
| 824 |
if (m_rec_id.m_heap_no == PAGE_HEAP_NO_SUPREMUM) { |
| 825 |
ut_ad(!(m_mode & LOCK_REC_NOT_GAP)); |
| 826 |
|
| 827 |
m_mode &= ~(LOCK_GAP | LOCK_REC_NOT_GAP); |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
/** |
| 832 |
Calculate the record lock physical size required for a predicate lock. |
| 833 |
@param[in] mode For predicate locks the lock mode |
| 834 |
@return the size of the lock data structure required in bytes */ |
| 835 |
static size_t lock_size(ulint mode) { |
| 836 |
ut_ad(is_predicate_lock(mode)); |
| 837 |
|
| 838 |
/* The lock is always on PAGE_HEAP_NO_INFIMUM(0), |
| 839 |
so we only need 1 bit (which is rounded up to 1 |
| 840 |
byte) for lock bit setting */ |
| 841 |
|
| 842 |
size_t n_bytes; |
| 843 |
|
| 844 |
if (mode & LOCK_PREDICATE) { |
| 845 |
const ulint align = UNIV_WORD_SIZE - 1; |
| 846 |
|
| 847 |
/* We will attach the predicate structure |
| 848 |
after lock. Make sure the memory is |
| 849 |
aligned on 8 bytes, the mem_heap_alloc |
| 850 |
will align it with MEM_SPACE_NEEDED |
| 851 |
anyway. */ |
| 852 |
|
| 853 |
n_bytes = (1 + sizeof(lock_prdt_t) + align) & ~align; |
| 854 |
|
| 855 |
/* This should hold now */ |
| 856 |
|
| 857 |
ut_ad(n_bytes == sizeof(lock_prdt_t) + UNIV_WORD_SIZE); |
| 858 |
|
| 859 |
} else { |
| 860 |
n_bytes = 1; |
| 861 |
} |
| 862 |
|
| 863 |
return (n_bytes); |
| 864 |
} |
| 865 |
|
| 866 |
/** |
| 867 |
Calculate the record lock physical size required, non-predicate lock. |
| 868 |
@param[in] page For non-predicate locks the buffer page |
| 869 |
@return the size of the lock data structure required in bytes */ |
| 870 |
static size_t lock_size(const page_t *page) { |
| 871 |
ulint n_recs = page_dir_get_n_heap(page); |
| 872 |
|
| 873 |
/* Make lock bitmap bigger by a safety margin */ |
| 874 |
|
| 875 |
return (1 + ((n_recs + LOCK_PAGE_BITMAP_MARGIN) / 8)); |
| 876 |
} |
| 877 |
|
| 878 |
/** |
| 879 |
@return true if the requested lock mode is for a predicate |
| 880 |
or page lock */ |
| 881 |
static bool is_predicate_lock(ulint mode) { |
| 882 |
return (mode & (LOCK_PREDICATE | LOCK_PRDT_PAGE)); |
| 883 |
} |
| 884 |
|
| 885 |
private: |
| 886 |
/** The query thread of the transaction */ |
| 887 |
que_thr_t *m_thr; |
| 888 |
|
| 889 |
/** |
| 890 |
Transaction requesting the record lock */ |
| 891 |
trx_t *m_trx; |
| 892 |
|
| 893 |
/** |
| 894 |
Lock mode requested */ |
| 895 |
ulint m_mode; |
| 896 |
|
| 897 |
/** |
| 898 |
Size of the record lock in bytes */ |
| 899 |
size_t m_size; |
| 900 |
|
| 901 |
/** |
| 902 |
Index on which the record lock is required */ |
| 903 |
dict_index_t *m_index; |
| 904 |
|
| 905 |
/** |
| 906 |
The record lock tuple {space, page_no, heap_no} */ |
| 907 |
RecID m_rec_id; |
| 908 |
}; |
| 909 |
|
| 910 |
#ifdef UNIV_DEBUG |
| 911 |
/** The count of the types of locks. */ |
| 912 |
static const ulint lock_types = UT_ARR_SIZE(lock_compatibility_matrix); |
| 913 |
#endif /* UNIV_DEBUG */ |
| 914 |
|
| 915 |
/** Gets the type of a lock. |
| 916 |
@return LOCK_TABLE or LOCK_REC */ |
| 917 |
static inline uint32_t lock_get_type_low(const lock_t *lock); /*!< in: lock */ |
| 918 |
|
| 919 |
/** Cancels a waiting lock request and releases possible other transactions |
| 920 |
waiting behind it. |
| 921 |
@param[in,out] trx The transaction waiting for a lock */ |
| 922 |
void lock_cancel_waiting_and_release(trx_t *trx); |
| 923 |
|
| 924 |
/** This function is a wrapper around several functions which need to be called |
| 925 |
in particular order to wake up a transaction waiting for a lock. |
| 926 |
You should not call lock_wait_release_thread_if_suspended(thr) directly, |
| 927 |
but rather use this wrapper, as this makes it much easier to reason about all |
| 928 |
possible states in which lock, trx, and thr can be. |
| 929 |
It makes sure that trx is woken up exactly once, and only if it already went to |
| 930 |
sleep. |
| 931 |
@param[in, out] lock The lock for which lock->trx is waiting */ |
| 932 |
void lock_reset_wait_and_release_thread_if_suspended(lock_t *lock); |
| 933 |
|
| 934 |
/** Checks if some transaction has an implicit x-lock on a record in a clustered |
| 935 |
index. |
| 936 |
@param[in] rec User record. |
| 937 |
@param[in] index Clustered index. |
| 938 |
@param[in] offsets rec_get_offsets(rec, index) |
| 939 |
@return transaction id of the transaction which has the x-lock, or 0 */ |
| 940 |
[[nodiscard]] static inline trx_id_t lock_clust_rec_some_has_impl( |
| 941 |
const rec_t *rec, const dict_index_t *index, const ulint *offsets); |
| 942 |
|
| 943 |
/** Gets the number of bits in a record lock bitmap. |
| 944 |
@param[in] lock The record lock |
| 945 |
@return number of bits */ |
| 946 |
static inline uint32_t lock_rec_get_n_bits(const lock_t *lock); |
| 947 |
|
| 948 |
/** Sets the nth bit of a record lock to true. |
| 949 |
@param[in] lock record lock |
| 950 |
@param[in] i index of the bit */ |
| 951 |
static inline void lock_rec_set_nth_bit(lock_t *lock, ulint i); |
| 952 |
|
| 953 |
/** Gets the mode of a lock. |
| 954 |
@return mode */ |
| 955 |
static inline enum lock_mode lock_get_mode(const lock_t *lock); /*!< in: lock */ |
| 956 |
|
| 957 |
/** Calculates if lock mode 1 is compatible with lock mode 2. |
| 958 |
@param[in] mode1 lock mode |
| 959 |
@param[in] mode2 lock mode |
| 960 |
@return nonzero if mode1 compatible with mode2 */ |
| 961 |
static inline ulint lock_mode_compatible(enum lock_mode mode1, |
| 962 |
enum lock_mode mode2); |
| 963 |
|
| 964 |
/** Calculates if lock mode 1 is stronger or equal to lock mode 2. |
| 965 |
@param[in] mode1 lock mode 1 |
| 966 |
@param[in] mode2 lock mode 2 |
| 967 |
@return true iff mode1 stronger or equal to mode2 */ |
| 968 |
static inline bool lock_mode_stronger_or_eq(enum lock_mode mode1, |
| 969 |
enum lock_mode mode2); |
| 970 |
|
| 971 |
/** Gets the wait flag of a lock. |
| 972 |
@return LOCK_WAIT if waiting, 0 if not */ |
| 973 |
static inline ulint lock_get_wait(const lock_t *lock); /*!< in: lock */ |
| 974 |
|
| 975 |
/** Checks if a transaction has the specified table lock, or stronger. This |
| 976 |
function should only be called by the thread that owns the transaction. |
| 977 |
This function acquires trx->mutex which protects trx->lock.trx_locks, but you |
| 978 |
should understand that this only makes it easier to argue against races at the |
| 979 |
level of access to the data structure, yet does not buy us any protection at |
| 980 |
the higher level of making actual decisions based on the result of this call - |
| 981 |
it may happen that another thread is removing a table lock, |
| 982 |
and even though lock_table_has returned true to the caller, the lock is no |
| 983 |
longer in possession of trx once the caller gets to evaluate if/else condition |
| 984 |
based on the result. |
| 985 |
Therefore it is up to caller to make sure that the context of the call to this |
| 986 |
function and making any decisions based on the result is protected from any |
| 987 |
concurrent modifications. This in turn makes the whole trx_mutex_enter/exit |
| 988 |
a bit redundant, but it does not affect performance yet makes the reasoning |
| 989 |
about data structure a bit easier and protects trx->lock.trx_locks data |
| 990 |
structure from corruption in case our high level reasoning about absence of |
| 991 |
parallel modifications turns out wrong. |
| 992 |
@param[in] trx transaction |
| 993 |
@param[in] table table |
| 994 |
@param[in] mode lock mode |
| 995 |
@return lock or NULL */ |
| 996 |
static inline bool lock_table_has(const trx_t *trx, const dict_table_t *table, |
| 997 |
enum lock_mode mode); |
| 998 |
|
| 999 |
/** Handles writing the information about found deadlock to the log files |
| 1000 |
and caches it for future lock_latest_err_file() calls (for example used by |
| 1001 |
SHOW ENGINE INNODB STATUS) |
| 1002 |
@param[in] trxs_on_cycle trxs causing deadlock, i-th waits for i+1-th |
| 1003 |
@param[in] victim_trx the trx from trx_on_cycle which will be rolled back */ |
| 1004 |
void lock_notify_about_deadlock(const ut::vector<const trx_t *> &trxs_on_cycle, |
| 1005 |
const trx_t *victim_trx); |
| 1006 |
|
| 1007 |
#include "lock0priv.ic" |
| 1008 |
|
| 1009 |
namespace locksys { |
| 1010 |
class Unsafe_global_latch_manipulator { |
| 1011 |
public: |
| 1012 |
static void exclusive_unlatch() { lock_sys->latches.global_latch.x_unlock(); } |
| 1013 |
static void exclusive_latch(ut::Location location) { |
| 1014 |
lock_sys->latches.global_latch.x_lock(location); |
| 1015 |
} |
| 1016 |
}; |
| 1017 |
|
| 1018 |
/** Temporarily releases trx->mutex, latches the lock-sys shard containing |
| 1019 |
peeked_lock and latches trx->mutex again and calls f under protection of both |
| 1020 |
latches. The latch on lock-sys shard will be released immediately after f |
| 1021 |
returns. It is a responsibility of the caller to handle shared lock-sys latch, |
| 1022 |
trx->mutex and verify inside f that the trx has not been finished, and the lock |
| 1023 |
was not released meanwhile. |
| 1024 |
@param[in] peeked_lock A lock of the trx. (While trx->mutex is held it can't |
| 1025 |
be freed, but can be released). It is used to |
| 1026 |
determine the lock-sys shard to latch. |
| 1027 |
@param[in] f The callback to call once the lock-sys shard is |
| 1028 |
latched and trx->mutex is relatched. |
| 1029 |
@return The value returned by f. |
| 1030 |
*/ |
| 1031 |
template <typename F> |
| 1032 |
auto latch_peeked_shard_and_do(const lock_t *peeked_lock, F &&f) { |
| 1033 |
ut_ad(locksys::owns_shared_global_latch()); |
| 1034 |
const trx_t *trx = peeked_lock->trx; |
| 1035 |
ut_ad(trx_mutex_own(trx)); |
| 1036 |
ut_ad(peeked_lock->trx == trx); |
| 1037 |
/* peeked_wait_lock points to a lock struct which will not be freed while we |
| 1038 |
hold trx->mutex. Thus it is safe to inspect the peeked_wait_lock's |
| 1039 |
rec_lock.page_id and tab_lock.table. We have to make a copy of them, though, |
| 1040 |
before releasing trx->mutex. */ |
| 1041 |
if (peeked_lock->is_record_lock()) { |
| 1042 |
const auto sharded_by = peeked_lock->rec_lock.page_id; |
| 1043 |
trx_mutex_exit(trx); |
| 1044 |
DEBUG_SYNC_C("try_relatch_trx_and_shard_and_do_noted_expected_version"); |
| 1045 |
locksys::Shard_naked_latch_guard guard{UT_LOCATION_HERE, sharded_by}; |
| 1046 |
trx_mutex_enter_first_of_two(trx); |
| 1047 |
return std::forward<F>(f)(); |
| 1048 |
} else { |
| 1049 |
/*Once we release the trx->mutex, the trx may release locks on table and |
| 1050 |
commit, which in extreme case could lead to freeing the dict_table_t |
| 1051 |
object, so we have to copy its id first. */ |
| 1052 |
const auto sharded_by = peeked_lock->tab_lock.table->id; |
| 1053 |
trx_mutex_exit(trx); |
| 1054 |
locksys::Shard_naked_latch_guard guard{UT_LOCATION_HERE, sharded_by}; |
| 1055 |
trx_mutex_enter_first_of_two(trx); |
| 1056 |
return std::forward<F>(f)(); |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
/** Given a pointer to trx (which the caller guarantees will not be freed) and |
| 1061 |
the expected value of trx->version, will call the provided function f, only if |
| 1062 |
the trx is still in expected version and waiting for a lock, within a critical |
| 1063 |
section which holds latches on the trx, and the shard containing the waiting |
| 1064 |
lock. If the transaction has meanwhile finished waiting for a lock, or committed |
| 1065 |
or rolled back etc. the f will not be called. |
| 1066 |
It may happen that the lock for which the trx is waiting during exectuion of f |
| 1067 |
is not the same as the lock it was waiting at the moment of invocation. |
| 1068 |
@param[in] trx_version The version of the trx that we intend to wake up |
| 1069 |
@param[in] f The callback to call if trx is still waiting for a |
| 1070 |
lock and is still in version trx_version |
| 1071 |
*/ |
| 1072 |
template <typename F> |
| 1073 |
void run_if_waiting(const TrxVersion trx_version, F &&f) { |
| 1074 |
const trx_t *trx = trx_version.m_trx; |
| 1075 |
/* This code would be much simpler with Global_exclusive_latch_guard. |
| 1076 |
Unfortunately, this lead to long semaphore waits when thousands of |
| 1077 |
transactions were taking thousands of locks and timing out. Therefore we use |
| 1078 |
the following tricky code to instead only latch the single shard which |
| 1079 |
contains the trx->lock.wait_lock. This is a bit difficult, because during |
| 1080 |
B-tree reorganization a record lock might be removed from one page and moved |
| 1081 |
to another, temporarily setting wait_lock to nullptr. This should be very |
| 1082 |
rare and short. In most cases this while loop should do just one iteration |
| 1083 |
and proceed along a happy path through all ifs. Another reason wait_lock |
| 1084 |
might become nullptr is because we were granted the lock meanwhile, in which |
| 1085 |
case the trx->lock.blocking_trx is first set to nullptr */ |
| 1086 |
do { |
| 1087 |
if (!trx->lock.wait_lock.load()) { |
| 1088 |
continue; |
| 1089 |
} |
| 1090 |
locksys::Global_shared_latch_guard shared_latch_guard{UT_LOCATION_HERE}; |
| 1091 |
/* We can't use IB_mutex_guard with trx->mutex, as trx_mutex_enter has |
| 1092 |
custom logic. We want to release trx->mutex before ut_delay or return. */ |
| 1093 |
trx_mutex_enter(trx); |
| 1094 |
auto guard = create_scope_guard([trx]() { trx_mutex_exit(trx); }); |
| 1095 |
if (trx->version != trx_version.m_version) { |
| 1096 |
return; |
| 1097 |
} |
| 1098 |
if (const lock_t *peeked_wait_lock = trx->lock.wait_lock.load()) { |
| 1099 |
const bool retry = latch_peeked_shard_and_do(peeked_wait_lock, [&]() { |
| 1100 |
ut_ad(trx_mutex_own(trx)); |
| 1101 |
if (trx->version != trx_version.m_version) { |
| 1102 |
return false; |
| 1103 |
} |
| 1104 |
if (peeked_wait_lock != trx->lock.wait_lock.load()) { |
| 1105 |
/* If wait_lock has changed, then in case of record lock it might have |
| 1106 |
been moved during B-tree reorganization, so we retry. In case of a |
| 1107 |
table lock the wait_lock can not be "moved" so it had to be released |
| 1108 |
permanently and there's no point in retrying.*/ |
| 1109 |
return peeked_wait_lock->is_record_lock(); |
| 1110 |
} |
| 1111 |
std::forward<F>(f)(); |
| 1112 |
ut_ad(trx_mutex_own(trx)); |
| 1113 |
return false; |
| 1114 |
}); |
| 1115 |
if (!retry) { |
| 1116 |
return; |
| 1117 |
} |
| 1118 |
} |
| 1119 |
/* wait_lock appears to be null. If blocking_trx isn't nullptr, then |
| 1120 |
probably the wait_lock will soon be restored, otherwise we can give up */ |
| 1121 |
} while (trx->lock.blocking_trx.load() && ut_delay(10)); |
| 1122 |
} |
| 1123 |
} // namespace locksys |
| 1124 |
|
| 1125 |
template <typename F> |
| 1126 |
lock_t *Locks_hashtable::find_in_cell(size_t cell_id, F &&f) { |
| 1127 |
lock_t *lock = (lock_t *)hash_get_first(ht.get(), cell_id); |
| 1128 |
while (lock != nullptr) { |
| 1129 |
ut_ad(locksys::owns_lock_shard(lock)); |
| 1130 |
// f(lock) might remove the lock from list, so we must save the next pointer |
| 1131 |
lock_t *next = lock->hash; |
| 1132 |
if (std::forward<F>(f)(lock)) { |
| 1133 |
return lock; |
| 1134 |
} |
| 1135 |
lock = next; |
| 1136 |
} |
| 1137 |
return nullptr; |
| 1138 |
} |
| 1139 |
|
| 1140 |
template <typename F> |
| 1141 |
lock_t *Locks_hashtable::find_on_page(page_id_t page_id, F &&f) { |
| 1142 |
ut_ad(locksys::owns_page_shard(page_id)); |
| 1143 |
return find_in_cell(hash_calc_cell_id(lock_rec_hash_value(page_id), ht.get()), |
| 1144 |
[&](lock_t *lock) { |
| 1145 |
return (lock->rec_lock.page_id == page_id) && |
| 1146 |
std::forward<F>(f)(lock); |
| 1147 |
}); |
| 1148 |
} |
| 1149 |
|
| 1150 |
template <typename F> |
| 1151 |
lock_t *Locks_hashtable::find_on_block(const buf_block_t *block, F &&f) { |
| 1152 |
return find_on_page(block->get_page_id(), std::forward<F>(f)); |
| 1153 |
} |
| 1154 |
|
| 1155 |
template <typename F> |
| 1156 |
lock_t *Locks_hashtable::find_on_record(const struct RecID &rec_id, F &&f) { |
| 1157 |
return find_in_cell(hash_calc_cell_id(rec_id.hash_value(), ht.get()), |
| 1158 |
[&](lock_t *lock) { |
| 1159 |
return rec_id.matches(lock) && std::forward<F>(f)(lock); |
| 1160 |
}); |
| 1161 |
} |
| 1162 |
#ifdef UNIV_DEBUG |
| 1163 |
template <typename F> |
| 1164 |
lock_t *Locks_hashtable::find(F &&f) { |
| 1165 |
ut_ad(locksys::owns_exclusive_global_latch()); |
| 1166 |
for (size_t cell_id = lock_sys->rec_hash.get_n_cells(); cell_id--;) { |
| 1167 |
if (auto lock = |
| 1168 |
lock_sys->rec_hash.find_in_cell(cell_id, std::forward<F>(f)); |
| 1169 |
lock != nullptr) { |
| 1170 |
return lock; |
| 1171 |
} |
| 1172 |
} |
| 1173 |
return nullptr; |
| 1174 |
} |
| 1175 |
#endif /*UNIV_DEBUG*/ |
| 1176 |
#endif /* lock0priv_h */ |