| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2014, 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 lock/lock0prdt.cc |
| 29 |
The transaction lock system |
| 30 |
|
| 31 |
Created 9/7/2013 Jimmy Yang |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#define LOCK_MODULE_IMPLEMENTATION |
| 35 |
|
| 36 |
#include <set> |
| 37 |
|
| 38 |
#include "btr0btr.h" |
| 39 |
#include "dict0boot.h" |
| 40 |
#include "dict0mem.h" |
| 41 |
#include "ha_prototypes.h" |
| 42 |
#include "lock0lock.h" |
| 43 |
#include "lock0prdt.h" |
| 44 |
#include "lock0priv.h" |
| 45 |
#include "srv0mon.h" |
| 46 |
#include "trx0purge.h" |
| 47 |
#include "trx0sys.h" |
| 48 |
#include "usr0sess.h" |
| 49 |
#include "ut0vec.h" |
| 50 |
|
| 51 |
/** Get a minimum bounding box from a Predicate |
| 52 |
@return the minimum bounding box */ |
| 53 |
static inline rtr_mbr_t *prdt_get_mbr_from_prdt( |
| 54 |
const lock_prdt_t *prdt) /*!< in: the lock predicate */ |
| 55 |
{ |
| 56 |
rtr_mbr_t *mbr_loc = reinterpret_cast<rtr_mbr_t *>(prdt->data); |
| 57 |
|
| 58 |
return (mbr_loc); |
| 59 |
} |
| 60 |
|
| 61 |
/** Get a predicate from a lock |
| 62 |
@return the predicate */ |
| 63 |
lock_prdt_t *lock_get_prdt_from_lock(const lock_t *lock) /*!< in: the lock */ |
| 64 |
{ |
| 65 |
lock_prdt_t *prdt = |
| 66 |
reinterpret_cast<lock_prdt_t *>(&((reinterpret_cast<byte *>( |
| 67 |
const_cast<lock_t *>(&lock[1])))[UNIV_WORD_SIZE])); |
| 68 |
|
| 69 |
return (prdt); |
| 70 |
} |
| 71 |
|
| 72 |
/** Get a minimum bounding box directly from a lock |
| 73 |
@return the minimum bounding box*/ |
| 74 |
static inline rtr_mbr_t *lock_prdt_get_mbr_from_lock( |
| 75 |
const lock_t *lock) /*!< in: the lock */ |
| 76 |
{ |
| 77 |
ut_ad(lock->type_mode & LOCK_PREDICATE); |
| 78 |
|
| 79 |
lock_prdt_t *prdt = lock_get_prdt_from_lock(lock); |
| 80 |
|
| 81 |
rtr_mbr_t *mbr_loc = prdt_get_mbr_from_prdt(prdt); |
| 82 |
|
| 83 |
return (mbr_loc); |
| 84 |
} |
| 85 |
|
| 86 |
/** Append a predicate to the lock |
| 87 |
@param[in] lock Lock |
| 88 |
@param[in] prdt Predicate */ |
| 89 |
void lock_prdt_set_prdt(lock_t *lock, const lock_prdt_t *prdt) { |
| 90 |
ut_ad(lock->type_mode & LOCK_PREDICATE); |
| 91 |
|
| 92 |
memcpy(&(((byte *)&lock[1])[UNIV_WORD_SIZE]), prdt, sizeof *prdt); |
| 93 |
} |
| 94 |
|
| 95 |
/** Check whether two predicate locks are compatible with each other |
| 96 |
@param[in] prdt1 first predicate lock |
| 97 |
@param[in] prdt2 second predicate lock |
| 98 |
@param[in] op predicate comparison operator |
| 99 |
@param[in] srs Spatial reference system of R-tree |
| 100 |
@return true if consistent */ |
| 101 |
static bool lock_prdt_consistent(lock_prdt_t *prdt1, lock_prdt_t *prdt2, |
| 102 |
ulint op, |
| 103 |
const dd::Spatial_reference_system *srs) { |
| 104 |
bool ret = false; |
| 105 |
rtr_mbr_t *mbr1 = prdt_get_mbr_from_prdt(prdt1); |
| 106 |
rtr_mbr_t *mbr2 = prdt_get_mbr_from_prdt(prdt2); |
| 107 |
ulint action; |
| 108 |
|
| 109 |
if (op) { |
| 110 |
action = op; |
| 111 |
} else { |
| 112 |
if (prdt2->op != 0 && (prdt1->op != prdt2->op)) { |
| 113 |
return (false); |
| 114 |
} |
| 115 |
|
| 116 |
action = prdt1->op; |
| 117 |
} |
| 118 |
|
| 119 |
switch (action) { |
| 120 |
case PAGE_CUR_CONTAIN: |
| 121 |
ret = mbr_contain_cmp(srs, mbr1, mbr2); |
| 122 |
break; |
| 123 |
case PAGE_CUR_DISJOINT: |
| 124 |
ret = mbr_disjoint_cmp(srs, mbr1, mbr2); |
| 125 |
break; |
| 126 |
case PAGE_CUR_MBR_EQUAL: |
| 127 |
/* TBD: Why is it important to use mbr_equal_physically() |
| 128 |
vs mbr_equal_logically() here? */ |
| 129 |
ret = mbr_equal_logically(srs, mbr1, mbr2); |
| 130 |
break; |
| 131 |
case PAGE_CUR_INTERSECT: |
| 132 |
ret = mbr_intersect_cmp(srs, mbr1, mbr2); |
| 133 |
break; |
| 134 |
case PAGE_CUR_WITHIN: |
| 135 |
ret = mbr_within_cmp(srs, mbr1, mbr2); |
| 136 |
break; |
| 137 |
default: |
| 138 |
ib::error(ER_IB_MSG_645) << "invalid operator " << action; |
| 139 |
ut_error; |
| 140 |
} |
| 141 |
|
| 142 |
return (ret); |
| 143 |
} |
| 144 |
|
| 145 |
/** Checks if a predicate lock request for a new lock has to wait for |
| 146 |
another lock. |
| 147 |
@return true if new lock has to wait for lock2 to be released */ |
| 148 |
bool lock_prdt_has_to_wait( |
| 149 |
const trx_t *trx, /*!< in: trx of new lock */ |
| 150 |
ulint type_mode, /*!< in: precise mode of the new lock |
| 151 |
to set: LOCK_S or LOCK_X, possibly |
| 152 |
ORed to LOCK_PREDICATE or LOCK_PRDT_PAGE, |
| 153 |
LOCK_INSERT_INTENTION */ |
| 154 |
lock_prdt_t *prdt, /*!< in: lock predicate to check */ |
| 155 |
const lock_t *lock2) /*!< in: another record lock; NOTE that |
| 156 |
it is assumed that this has a lock bit |
| 157 |
set on the same record as in the new |
| 158 |
lock we are setting */ |
| 159 |
{ |
| 160 |
ut_ad(trx && lock2); |
| 161 |
ut_ad((lock2->type_mode & LOCK_PREDICATE && type_mode & LOCK_PREDICATE) || |
| 162 |
(lock2->type_mode & LOCK_PRDT_PAGE && type_mode & LOCK_PRDT_PAGE)); |
| 163 |
|
| 164 |
ut_ad(type_mode & (LOCK_PREDICATE | LOCK_PRDT_PAGE)); |
| 165 |
|
| 166 |
if (trx != lock2->trx && |
| 167 |
!lock_mode_compatible(static_cast<lock_mode>(LOCK_MODE_MASK & type_mode), |
| 168 |
lock_get_mode(lock2))) { |
| 169 |
const bool is_hp = trx_is_high_priority(trx); |
| 170 |
/* If our trx is High Priority and the existing lock is WAITING and not |
| 171 |
high priority, then we can ignore it. */ |
| 172 |
if (is_hp && lock2->is_waiting() && !trx_is_high_priority(lock2->trx)) { |
| 173 |
return (false); |
| 174 |
} |
| 175 |
|
| 176 |
/* If it is a page lock, then return true (conflict) */ |
| 177 |
if (type_mode & LOCK_PRDT_PAGE) { |
| 178 |
ut_ad(lock2->type_mode & LOCK_PRDT_PAGE); |
| 179 |
|
| 180 |
return (true); |
| 181 |
} |
| 182 |
|
| 183 |
ut_ad(lock2->type_mode & LOCK_PREDICATE); |
| 184 |
|
| 185 |
if (!(type_mode & LOCK_INSERT_INTENTION)) { |
| 186 |
/* PREDICATE locks without LOCK_INSERT_INTENTION flag |
| 187 |
do not need to wait for anything. This is because |
| 188 |
different users can have conflicting lock types |
| 189 |
on predicates. */ |
| 190 |
|
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
if (lock2->type_mode & LOCK_INSERT_INTENTION) { |
| 195 |
/* No lock request needs to wait for an insert |
| 196 |
intention lock to be removed. This makes it similar |
| 197 |
to GAP lock, that allows conflicting insert intention |
| 198 |
locks */ |
| 199 |
return false; |
| 200 |
} |
| 201 |
lock_prdt_t *cur_prdt = lock_get_prdt_from_lock(lock2); |
| 202 |
|
| 203 |
if (!lock_prdt_consistent(cur_prdt, prdt, 0, lock2->index->rtr_srs.get())) { |
| 204 |
return (false); |
| 205 |
} |
| 206 |
|
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
/** Checks if a transaction has a GRANTED stronger or equal predicate lock |
| 214 |
on the page |
| 215 |
@return lock or NULL */ |
| 216 |
static inline lock_t *lock_prdt_has_lock( |
| 217 |
ulint precise_mode, /*!< in: LOCK_S or LOCK_X */ |
| 218 |
ulint type_mode, /*!< in: LOCK_PREDICATE etc. */ |
| 219 |
const buf_block_t *block, /*!< in: buffer block |
| 220 |
containing the record */ |
| 221 |
lock_prdt_t *prdt, /*!< in: The predicate to be |
| 222 |
attached to the new lock */ |
| 223 |
const trx_t *trx) /*!< in: transaction */ |
| 224 |
{ |
| 225 |
ut_ad(locksys::owns_page_shard(block->get_page_id())); |
| 226 |
ut_ad((precise_mode & LOCK_MODE_MASK) == LOCK_S || |
| 227 |
(precise_mode & LOCK_MODE_MASK) == LOCK_X); |
| 228 |
ut_ad(!(precise_mode & LOCK_INSERT_INTENTION)); |
| 229 |
return lock_hash_get(type_mode).find_on_record( |
| 230 |
RecID{block->get_page_id(), PRDT_HEAPNO}, [&](lock_t *lock) { |
| 231 |
ut_ad(lock->type_mode & (LOCK_PREDICATE | LOCK_PRDT_PAGE)); |
| 232 |
|
| 233 |
if (lock->trx == trx && !(lock->type_mode & LOCK_INSERT_INTENTION) && |
| 234 |
!lock_get_wait(lock) && |
| 235 |
lock_mode_stronger_or_eq( |
| 236 |
lock_get_mode(lock), |
| 237 |
static_cast<lock_mode>(precise_mode & LOCK_MODE_MASK))) { |
| 238 |
if (lock->type_mode & LOCK_PRDT_PAGE) { |
| 239 |
return true; |
| 240 |
} |
| 241 |
|
| 242 |
ut_ad(lock->type_mode & LOCK_PREDICATE); |
| 243 |
lock_prdt_t *cur_prdt = lock_get_prdt_from_lock(lock); |
| 244 |
|
| 245 |
/* if the lock predicate operator is the same as the one to look, and |
| 246 |
prdicate test is successful, then we find a lock */ |
| 247 |
if (cur_prdt->op == prdt->op && |
| 248 |
lock_prdt_consistent(cur_prdt, prdt, 0, |
| 249 |
lock->index->rtr_srs.get())) { |
| 250 |
return true; |
| 251 |
} |
| 252 |
} |
| 253 |
return false; |
| 254 |
}); |
| 255 |
} |
| 256 |
|
| 257 |
/** Checks if some other transaction has a conflicting predicate |
| 258 |
lock request in the queue, so that we have to wait. |
| 259 |
@return lock or NULL */ |
| 260 |
static const lock_t *lock_prdt_other_has_conflicting( |
| 261 |
ulint mode, /*!< in: LOCK_S or LOCK_X, |
| 262 |
possibly ORed to LOCK_PREDICATE or |
| 263 |
LOCK_PRDT_PAGE, LOCK_INSERT_INTENTION */ |
| 264 |
const buf_block_t *block, /*!< in: buffer block containing |
| 265 |
the record */ |
| 266 |
lock_prdt_t *prdt, /*!< in: Predicates (currently) |
| 267 |
the Minimum Bounding Rectangle) |
| 268 |
the new lock will be on */ |
| 269 |
const trx_t *trx) /*!< in: our transaction */ |
| 270 |
{ |
| 271 |
ut_ad(locksys::owns_page_shard(block->get_page_id())); |
| 272 |
return lock_hash_get(mode).find_on_record( |
| 273 |
RecID{block, PRDT_HEAPNO}, [&](lock_t *lock) { |
| 274 |
return lock_prdt_has_to_wait(trx, mode, prdt, lock); |
| 275 |
}); |
| 276 |
} |
| 277 |
|
| 278 |
/** Reset the Minimum Bounding Rectangle (to a large area) */ |
| 279 |
static void lock_prdt_enlarge_mbr( |
| 280 |
const lock_t *lock, /*!< in/out: lock to modify */ |
| 281 |
rtr_mbr_t *mbr) /*!< in: Minimum Bounding Rectangle */ |
| 282 |
{ |
| 283 |
rtr_mbr_t *cur_mbr = lock_prdt_get_mbr_from_lock(lock); |
| 284 |
|
| 285 |
if (cur_mbr->xmin > mbr->xmin) { |
| 286 |
cur_mbr->xmin = mbr->xmin; |
| 287 |
} |
| 288 |
|
| 289 |
if (cur_mbr->ymin > mbr->ymin) { |
| 290 |
cur_mbr->ymin = mbr->ymin; |
| 291 |
} |
| 292 |
|
| 293 |
if (cur_mbr->xmax < mbr->xmax) { |
| 294 |
cur_mbr->xmax = mbr->xmax; |
| 295 |
} |
| 296 |
|
| 297 |
if (cur_mbr->ymax < mbr->ymax) { |
| 298 |
cur_mbr->ymax = mbr->ymax; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
/** Reset the predicates to a "covering" (larger) predicates */ |
| 303 |
static void lock_prdt_enlarge_prdt(lock_t *lock, /*!< in/out: lock to modify */ |
| 304 |
lock_prdt_t *prdt) /*!< in: predicate */ |
| 305 |
{ |
| 306 |
rtr_mbr_t *mbr = prdt_get_mbr_from_prdt(prdt); |
| 307 |
|
| 308 |
lock_prdt_enlarge_mbr(lock, mbr); |
| 309 |
} |
| 310 |
|
| 311 |
/** Check two predicates' MBRs are the same |
| 312 |
@return true if they are the same */ |
| 313 |
static bool lock_prdt_is_same( |
| 314 |
lock_prdt_t *prdt1, /*!< in: MBR with the lock */ |
| 315 |
lock_prdt_t *prdt2, /*!< in: MBR with the lock */ |
| 316 |
const dd::Spatial_reference_system *srs) /*!< in: SRS of R-tree */ |
| 317 |
{ |
| 318 |
rtr_mbr_t *mbr1 = prdt_get_mbr_from_prdt(prdt1); |
| 319 |
rtr_mbr_t *mbr2 = prdt_get_mbr_from_prdt(prdt2); |
| 320 |
|
| 321 |
/* TBD: Why is it important to use mbr_equal_physically() |
| 322 |
vs mbr_equal_logically() here? */ |
| 323 |
if (prdt1->op == prdt2->op && mbr_equal_logically(srs, mbr1, mbr2)) { |
| 324 |
return (true); |
| 325 |
} |
| 326 |
|
| 327 |
return (false); |
| 328 |
} |
| 329 |
|
| 330 |
/** Looks for a similar predicate lock struct by the same trx on the same page. |
| 331 |
This can be used to save space when a new record lock should be set on a page: |
| 332 |
no new struct is needed, if a suitable old one is found. |
| 333 |
@return lock or NULL */ |
| 334 |
static lock_t *lock_prdt_find_on_page( |
| 335 |
ulint type_mode, /*!< in: lock type_mode field */ |
| 336 |
const buf_block_t *block, /*!< in: buffer block */ |
| 337 |
lock_prdt_t *prdt, /*!< in: MBR with the lock */ |
| 338 |
const trx_t *trx) /*!< in: transaction */ |
| 339 |
{ |
| 340 |
ut_ad(locksys::owns_page_shard(block->get_page_id())); |
| 341 |
return lock_hash_get(type_mode).find_on_block(block, [&](lock_t *lock) { |
| 342 |
if (lock->trx == trx && lock->type_mode == type_mode) { |
| 343 |
if (lock->type_mode & LOCK_PRDT_PAGE) { |
| 344 |
return true; |
| 345 |
} |
| 346 |
|
| 347 |
ut_ad(lock->type_mode & LOCK_PREDICATE); |
| 348 |
|
| 349 |
return lock_prdt_is_same(lock_get_prdt_from_lock(lock), prdt, |
| 350 |
lock->index->rtr_srs.get()); |
| 351 |
} |
| 352 |
return false; |
| 353 |
}); |
| 354 |
} |
| 355 |
|
| 356 |
/** Adds a predicate lock request in the predicate lock queue. |
| 357 |
@return lock where the bit was set */ |
| 358 |
static lock_t *lock_prdt_add_to_queue( |
| 359 |
ulint type_mode, /*!< in: lock mode, wait, predicate |
| 360 |
etc. flags; type is ignored |
| 361 |
and replaced by LOCK_REC */ |
| 362 |
const buf_block_t *block, /*!< in: buffer block containing |
| 363 |
the record */ |
| 364 |
dict_index_t *index, /*!< in: index of record */ |
| 365 |
trx_t *trx, /*!< in/out: transaction */ |
| 366 |
lock_prdt_t *prdt) /*!< in: Minimum Bounding Rectangle |
| 367 |
the new lock will be on */ |
| 368 |
{ |
| 369 |
ut_ad(locksys::owns_page_shard(block->get_page_id())); |
| 370 |
ut_ad(!index->is_clustered() && !dict_index_is_online_ddl(index)); |
| 371 |
ut_ad(type_mode & (LOCK_PREDICATE | LOCK_PRDT_PAGE)); |
| 372 |
ut_ad(!trx_mutex_own(trx)); |
| 373 |
|
| 374 |
#ifdef UNIV_DEBUG |
| 375 |
switch (type_mode & LOCK_MODE_MASK) { |
| 376 |
case LOCK_X: |
| 377 |
case LOCK_S: |
| 378 |
break; |
| 379 |
default: |
| 380 |
ut_error; |
| 381 |
} |
| 382 |
#endif /* UNIV_DEBUG */ |
| 383 |
|
| 384 |
type_mode |= LOCK_REC; |
| 385 |
|
| 386 |
if (!(type_mode & LOCK_WAIT)) { |
| 387 |
/* Look for a similar record lock on the same page: |
| 388 |
if one is found we can just set the bit */ |
| 389 |
|
| 390 |
lock_t *lock = lock_prdt_find_on_page(type_mode, block, prdt, trx); |
| 391 |
|
| 392 |
if (lock != nullptr) { |
| 393 |
if (lock->type_mode & LOCK_PREDICATE) { |
| 394 |
lock_prdt_enlarge_prdt(lock, prdt); |
| 395 |
} |
| 396 |
|
| 397 |
return (lock); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
RecLock rec_lock(index, block, PRDT_HEAPNO, type_mode); |
| 402 |
|
| 403 |
trx_mutex_enter(trx); |
| 404 |
auto *created_lock = (rec_lock.create(trx, prdt)); |
| 405 |
trx_mutex_exit(trx); |
| 406 |
|
| 407 |
return (created_lock); |
| 408 |
} |
| 409 |
|
| 410 |
/** Checks if locks of other transactions prevent an immediate insert of |
| 411 |
a predicate record. |
| 412 |
@return DB_SUCCESS, DB_LOCK_WAIT, or DB_DEADLOCK |
| 413 |
*/ |
| 414 |
dberr_t lock_prdt_insert_check_and_lock( |
| 415 |
ulint flags, /*!< in: if BTR_NO_LOCKING_FLAG bit is |
| 416 |
set, does nothing */ |
| 417 |
const rec_t *rec, /*!< in: record after which to insert */ |
| 418 |
buf_block_t *block, /*!< in/out: buffer block of rec */ |
| 419 |
dict_index_t *index, /*!< in: index */ |
| 420 |
que_thr_t *thr, /*!< in: query thread */ |
| 421 |
mtr_t *mtr, /*!< in/out: mini-transaction */ |
| 422 |
lock_prdt_t *prdt) /*!< in: Predicates with Minimum Bound |
| 423 |
Rectangle */ |
| 424 |
{ |
| 425 |
ut_ad(block->frame == page_align(rec)); |
| 426 |
|
| 427 |
if (flags & BTR_NO_LOCKING_FLAG) { |
| 428 |
return (DB_SUCCESS); |
| 429 |
} |
| 430 |
|
| 431 |
ut_ad(!index->table->is_temporary()); |
| 432 |
ut_ad(!index->is_clustered()); |
| 433 |
|
| 434 |
trx_t *trx = thr_get_trx(thr); |
| 435 |
|
| 436 |
dberr_t err = DB_SUCCESS; |
| 437 |
{ |
| 438 |
locksys::Shard_latch_guard guard{UT_LOCATION_HERE, block->get_page_id()}; |
| 439 |
|
| 440 |
/* Because this code is invoked for a running transaction by |
| 441 |
the thread that is serving the transaction, it is not necessary |
| 442 |
to hold trx->mutex here. */ |
| 443 |
|
| 444 |
ut_ad(lock_table_has(trx, index->table, LOCK_IX)); |
| 445 |
|
| 446 |
/* Only need to check locks on prdt_hash */ |
| 447 |
|
| 448 |
/* If another transaction has an explicit lock request which locks |
| 449 |
the predicate, waiting or granted, the insert has to wait. |
| 450 |
|
| 451 |
Similar to GAP lock, we do not consider lock from inserts conflicts |
| 452 |
with each other */ |
| 453 |
|
| 454 |
const ulint mode = LOCK_X | LOCK_PREDICATE | LOCK_INSERT_INTENTION; |
| 455 |
|
| 456 |
const lock_t *wait_for = |
| 457 |
lock_prdt_other_has_conflicting(mode, block, prdt, trx); |
| 458 |
|
| 459 |
if (wait_for != nullptr) { |
| 460 |
rtr_mbr_t *mbr = prdt_get_mbr_from_prdt(prdt); |
| 461 |
|
| 462 |
trx_mutex_enter(trx); |
| 463 |
|
| 464 |
/* Allocate MBR on the lock heap */ |
| 465 |
lock_init_prdt_from_mbr(prdt, mbr, 0, trx->lock.lock_heap); |
| 466 |
|
| 467 |
RecLock rec_lock(thr, index, block, PRDT_HEAPNO, mode); |
| 468 |
|
| 469 |
/* Note that we may get DB_SUCCESS also here! */ |
| 470 |
|
| 471 |
err = rec_lock.add_to_waitq(wait_for, prdt); |
| 472 |
|
| 473 |
trx_mutex_exit(trx); |
| 474 |
} |
| 475 |
|
| 476 |
} // release block latch |
| 477 |
|
| 478 |
switch (err) { |
| 479 |
case DB_SUCCESS_LOCKED_REC: |
| 480 |
err = DB_SUCCESS; |
| 481 |
[[fallthrough]]; |
| 482 |
case DB_SUCCESS: |
| 483 |
/* Update the page max trx id field */ |
| 484 |
page_update_max_trx_id(block, buf_block_get_page_zip(block), trx->id, |
| 485 |
mtr); |
| 486 |
default: |
| 487 |
/* We only care about the two return values. */ |
| 488 |
break; |
| 489 |
} |
| 490 |
ut_ad(err == DB_SUCCESS || err == DB_LOCK_WAIT || err == DB_DEADLOCK); |
| 491 |
return (err); |
| 492 |
} |
| 493 |
|
| 494 |
void lock_prdt_update_parent(buf_block_t *left_block, buf_block_t *right_block, |
| 495 |
lock_prdt_t *left_prdt, lock_prdt_t *right_prdt, |
| 496 |
const page_id_t &page_id) { |
| 497 |
/* We will operate on three blocks (left, right, parent). Latching their |
| 498 |
shards without deadlock is easiest using exclusive global latch. */ |
| 499 |
locksys::Global_exclusive_latch_guard guard{UT_LOCATION_HERE}; |
| 500 |
|
| 501 |
/* Get all locks in parent */ |
| 502 |
lock_sys->prdt_hash.find_on_page(page_id, [&](lock_t *lock) { |
| 503 |
lock_prdt_t *lock_prdt; |
| 504 |
ulint op = PAGE_CUR_DISJOINT; |
| 505 |
|
| 506 |
ut_ad(lock->type_mode & LOCK_PREDICATE); |
| 507 |
|
| 508 |
if ((lock->type_mode & LOCK_MODE_MASK) == LOCK_X) { |
| 509 |
return false; |
| 510 |
} |
| 511 |
|
| 512 |
lock_prdt = lock_get_prdt_from_lock(lock); |
| 513 |
|
| 514 |
/* Check each lock in parent to see if it intersects with |
| 515 |
left or right child */ |
| 516 |
if (!lock_prdt_consistent(lock_prdt, left_prdt, op, |
| 517 |
lock->index->rtr_srs.get()) && |
| 518 |
!lock_prdt_find_on_page(lock->type_mode, left_block, lock_prdt, |
| 519 |
lock->trx)) { |
| 520 |
lock_prdt_add_to_queue(lock->type_mode, left_block, lock->index, |
| 521 |
lock->trx, lock_prdt); |
| 522 |
} |
| 523 |
|
| 524 |
if (!lock_prdt_consistent(lock_prdt, right_prdt, op, |
| 525 |
lock->index->rtr_srs.get()) && |
| 526 |
!lock_prdt_find_on_page(lock->type_mode, right_block, lock_prdt, |
| 527 |
lock->trx)) { |
| 528 |
lock_prdt_add_to_queue(lock->type_mode, right_block, lock->index, |
| 529 |
lock->trx, lock_prdt); |
| 530 |
} |
| 531 |
return false; |
| 532 |
}); |
| 533 |
} |
| 534 |
|
| 535 |
/** Update predicate lock when page splits |
| 536 |
@param[in,out] block page to be split |
| 537 |
@param[in,out] new_block the new half page |
| 538 |
@param[in] prdt MBR on the old page |
| 539 |
@param[in] new_prdt MBR on the new page |
| 540 |
@param[in] type_mode LOCK_PREDICATE or LOCK_PRDT_PAGE |
| 541 |
*/ |
| 542 |
static void lock_prdt_update_split_low(buf_block_t *block, |
| 543 |
buf_block_t *new_block, |
| 544 |
lock_prdt_t *prdt, lock_prdt_t *new_prdt, |
| 545 |
ulint type_mode) { |
| 546 |
locksys::Shard_latches_guard guard{UT_LOCATION_HERE, *block, *new_block}; |
| 547 |
lock_hash_get(type_mode).find_on_block(block, [&](lock_t *lock) { |
| 548 |
/* First dealing with Page Lock */ |
| 549 |
if (lock->type_mode & LOCK_PRDT_PAGE) { |
| 550 |
/* Duplicate the lock to new page */ |
| 551 |
|
| 552 |
lock_prdt_add_to_queue(lock->type_mode, new_block, lock->index, lock->trx, |
| 553 |
nullptr); |
| 554 |
|
| 555 |
return false; |
| 556 |
} |
| 557 |
|
| 558 |
/* Now dealing with Predicate Lock */ |
| 559 |
lock_prdt_t *lock_prdt; |
| 560 |
ulint op = PAGE_CUR_DISJOINT; |
| 561 |
|
| 562 |
ut_ad(lock->type_mode & LOCK_PREDICATE); |
| 563 |
|
| 564 |
/* No need to duplicate waiting X locks */ |
| 565 |
if ((lock->type_mode & LOCK_MODE_MASK) == LOCK_X) { |
| 566 |
return false; |
| 567 |
} |
| 568 |
|
| 569 |
lock_prdt = lock_get_prdt_from_lock(lock); |
| 570 |
|
| 571 |
if (lock_prdt_consistent(lock_prdt, prdt, op, lock->index->rtr_srs.get())) { |
| 572 |
if (!lock_prdt_consistent(lock_prdt, new_prdt, op, |
| 573 |
lock->index->rtr_srs.get())) { |
| 574 |
/* Move the lock to new page */ |
| 575 |
|
| 576 |
lock_prdt_add_to_queue(lock->type_mode, new_block, lock->index, |
| 577 |
lock->trx, lock_prdt); |
| 578 |
} |
| 579 |
} else if (!lock_prdt_consistent(lock_prdt, new_prdt, op, |
| 580 |
lock->index->rtr_srs.get())) { |
| 581 |
/* Duplicate the lock to new page */ |
| 582 |
|
| 583 |
lock_prdt_add_to_queue(lock->type_mode, new_block, lock->index, lock->trx, |
| 584 |
lock_prdt); |
| 585 |
} |
| 586 |
return false; |
| 587 |
}); |
| 588 |
} |
| 589 |
|
| 590 |
void lock_prdt_update_split(buf_block_t *block, buf_block_t *new_block, |
| 591 |
lock_prdt_t *prdt, lock_prdt_t *new_prdt) |
| 592 |
|
| 593 |
{ |
| 594 |
lock_prdt_update_split_low(block, new_block, prdt, new_prdt, LOCK_PREDICATE); |
| 595 |
|
| 596 |
lock_prdt_update_split_low(block, new_block, nullptr, nullptr, |
| 597 |
LOCK_PRDT_PAGE); |
| 598 |
} |
| 599 |
|
| 600 |
/** Initiate a Predicate Lock from a MBR */ |
| 601 |
void lock_init_prdt_from_mbr( |
| 602 |
lock_prdt_t *prdt, /*!< in/out: predicate to initialized */ |
| 603 |
rtr_mbr_t *mbr, /*!< in: Minimum Bounding Rectangle */ |
| 604 |
ulint mode, /*!< in: Search mode */ |
| 605 |
mem_heap_t *heap) /*!< in: heap for allocating memory */ |
| 606 |
{ |
| 607 |
memset(prdt, 0, sizeof(*prdt)); |
| 608 |
|
| 609 |
if (heap != nullptr) { |
| 610 |
prdt->data = mem_heap_alloc(heap, sizeof(*mbr)); |
| 611 |
ut_memcpy(prdt->data, mbr, sizeof(*mbr)); |
| 612 |
} else { |
| 613 |
prdt->data = static_cast<void *>(mbr); |
| 614 |
} |
| 615 |
|
| 616 |
prdt->op = static_cast<uint16>(mode); |
| 617 |
} |
| 618 |
|
| 619 |
void lock_prdt_lock(buf_block_t *block, lock_prdt_t *prdt, dict_index_t *index, |
| 620 |
que_thr_t *thr) { |
| 621 |
trx_t *trx = thr_get_trx(thr); |
| 622 |
|
| 623 |
if (trx->read_only || index->table->is_temporary()) { |
| 624 |
return; |
| 625 |
} |
| 626 |
|
| 627 |
ut_ad(!index->is_clustered()); |
| 628 |
ut_ad(!dict_index_is_online_ddl(index)); |
| 629 |
|
| 630 |
/* Another transaction cannot have an implicit lock on the record, |
| 631 |
because when we come here, we already have modified the clustered |
| 632 |
index record, and this would not have been possible if another active |
| 633 |
transaction had modified this secondary index record. */ |
| 634 |
|
| 635 |
locksys::Shard_latch_guard guard{UT_LOCATION_HERE, block->get_page_id()}; |
| 636 |
|
| 637 |
const ulint prdt_mode = LOCK_S | LOCK_PREDICATE; |
| 638 |
|
| 639 |
lock_t *lock = nullptr; |
| 640 |
lock_t *other_lock = |
| 641 |
lock_sys->prdt_hash.find_on_block(block, [&](lock_t *seen) { |
| 642 |
if (lock != nullptr) { |
| 643 |
return true; |
| 644 |
} |
| 645 |
lock = seen; |
| 646 |
return false; |
| 647 |
}); |
| 648 |
|
| 649 |
if (lock == nullptr) { |
| 650 |
RecLock rec_lock(index, block, PRDT_HEAPNO, prdt_mode); |
| 651 |
|
| 652 |
trx_mutex_enter(trx); |
| 653 |
lock = rec_lock.create(trx, prdt); |
| 654 |
trx_mutex_exit(trx); |
| 655 |
|
| 656 |
} else { |
| 657 |
if (other_lock || lock->trx != trx || |
| 658 |
lock->type_mode != (LOCK_REC | prdt_mode) || |
| 659 |
lock_rec_get_n_bits(lock) == 0 || |
| 660 |
((!lock_prdt_consistent(lock_get_prdt_from_lock(lock), prdt, 0, |
| 661 |
lock->index->rtr_srs.get())))) { |
| 662 |
if (!lock_prdt_has_lock(LOCK_S, LOCK_PREDICATE, block, prdt, trx)) { |
| 663 |
lock_prdt_add_to_queue(prdt_mode, block, index, trx, prdt); |
| 664 |
} |
| 665 |
|
| 666 |
} else { |
| 667 |
if (!lock_rec_get_nth_bit(lock, PRDT_HEAPNO)) { |
| 668 |
lock_rec_set_nth_bit(lock, PRDT_HEAPNO); |
| 669 |
lock_prdt_set_prdt(lock, prdt); |
| 670 |
} |
| 671 |
} |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
dberr_t lock_place_prdt_page_lock(const page_id_t &page_id, dict_index_t *index, |
| 676 |
que_thr_t *thr) { |
| 677 |
ut_ad(thr != nullptr); |
| 678 |
ut_ad(!srv_read_only_mode); |
| 679 |
|
| 680 |
ut_ad(!index->is_clustered()); |
| 681 |
ut_ad(!dict_index_is_online_ddl(index)); |
| 682 |
|
| 683 |
/* Another transaction cannot have an implicit lock on the record, |
| 684 |
because when we come here, we already have modified the clustered |
| 685 |
index record, and this would not have been possible if another active |
| 686 |
transaction had modified this secondary index record. */ |
| 687 |
|
| 688 |
RecID rec_id(page_id, PRDT_HEAPNO); |
| 689 |
locksys::Shard_latch_guard guard{UT_LOCATION_HERE, page_id}; |
| 690 |
|
| 691 |
const ulint mode = LOCK_S | LOCK_PRDT_PAGE; |
| 692 |
trx_t *trx = thr_get_trx(thr); |
| 693 |
|
| 694 |
const lock_t *lock = lock_sys->prdt_page_hash.find_on_page( |
| 695 |
page_id, [&](lock_t *lock) { return lock->trx == trx; }); |
| 696 |
|
| 697 |
if (lock == nullptr) { |
| 698 |
RecLock rec_lock(index, rec_id, mode); |
| 699 |
|
| 700 |
trx_mutex_enter(trx); |
| 701 |
rec_lock.create(trx); |
| 702 |
trx_mutex_exit(trx); |
| 703 |
|
| 704 |
#ifdef PRDT_DIAG |
| 705 |
printf("GIS_DIAGNOSTIC: page lock %d\n", (int)page_no); |
| 706 |
#endif /* PRDT_DIAG */ |
| 707 |
} else { |
| 708 |
/* LOCK_PRDT_PAGE do not have a predicate, but have 1 byte (zeroed) bitmap, |
| 709 |
and they always use S mode. They purpose is not so much to conflict with |
| 710 |
each other (they are all S), rather indicate the page is still needed. */ |
| 711 |
ut_ad(lock->type_mode == (mode | LOCK_REC)); |
| 712 |
ut_ad(lock_rec_get_n_bits(lock) != 0); |
| 713 |
} |
| 714 |
|
| 715 |
return (DB_SUCCESS); |
| 716 |
} |
| 717 |
|
| 718 |
bool lock_other_has_prdt_page_lock(const trx_t *trx, const page_id_t &page_id) { |
| 719 |
locksys::Shard_latch_guard guard{UT_LOCATION_HERE, page_id}; |
| 720 |
/* Make sure that the only locks on this page (if any) are ours. */ |
| 721 |
return lock_sys->prdt_page_hash.find_on_page( |
| 722 |
page_id, [&](lock_t *lock) { return lock->trx != trx; }); |
| 723 |
} |
| 724 |
|
| 725 |
/** Moves the locks of a page to another page and resets the lock bits of |
| 726 |
the donating records. */ |
| 727 |
void lock_prdt_rec_move( |
| 728 |
const buf_block_t *receiver, /*!< in: buffer block containing |
| 729 |
the receiving record */ |
| 730 |
const buf_block_t *donator) /*!< in: buffer block containing |
| 731 |
the donating record */ |
| 732 |
{ |
| 733 |
locksys::Shard_latches_guard guard{UT_LOCATION_HERE, *receiver, *donator}; |
| 734 |
lock_sys->prdt_hash.find_on_record( |
| 735 |
RecID{donator, PRDT_HEAPNO}, [&](lock_t *lock) { |
| 736 |
const ulint type_mode = lock->type_mode; |
| 737 |
lock_prdt_t *lock_prdt = lock_get_prdt_from_lock(lock); |
| 738 |
|
| 739 |
lock_rec_clear_request_no_wakeup(lock, PRDT_HEAPNO); |
| 740 |
|
| 741 |
lock_prdt_add_to_queue(type_mode, receiver, lock->index, lock->trx, |
| 742 |
lock_prdt); |
| 743 |
return false; |
| 744 |
}); |
| 745 |
} |
| 746 |
|
| 747 |
/** Removes predicate lock objects set on an index page which is discarded. |
| 748 |
@param[in] block page to be discarded |
| 749 |
@param[in] lock_hash lock hash */ |
| 750 |
void lock_prdt_page_free_from_discard(const buf_block_t *block, |
| 751 |
Locks_hashtable &lock_hash) { |
| 752 |
ut_ad(locksys::owns_page_shard(block->get_page_id())); |
| 753 |
lock_hash.find_on_block(block, [&](lock_t *lock) { |
| 754 |
trx_t *trx = lock->trx; |
| 755 |
trx_mutex_enter(trx); |
| 756 |
lock_rec_discard(lock); |
| 757 |
trx_mutex_exit(trx); |
| 758 |
return false; |
| 759 |
}); |
| 760 |
} |