| 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 read/read0read.cc |
| 29 |
Cursor read |
| 30 |
|
| 31 |
Created 2/16/1997 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "read0read.h" |
| 35 |
#include "clone0clone.h" |
| 36 |
|
| 37 |
#include "srv0srv.h" |
| 38 |
#include "trx0sys.h" |
| 39 |
|
| 40 |
/* |
| 41 |
------------------------------------------------------------------------------- |
| 42 |
FACT A: Cursor read view on a secondary index sees only committed versions |
| 43 |
------- |
| 44 |
of the records in the secondary index or those versions of rows created |
| 45 |
by transaction which created a cursor before cursor was created even |
| 46 |
if transaction which created the cursor has changed that clustered index page. |
| 47 |
|
| 48 |
PROOF: We must show that read goes always to the clustered index record |
| 49 |
to see that record is visible in the cursor read view. Consider e.g. |
| 50 |
following table and SQL-clauses: |
| 51 |
|
| 52 |
create table t1(a int not null, b int, primary key(a), index(b)); |
| 53 |
insert into t1 values (1,1),(2,2); |
| 54 |
commit; |
| 55 |
|
| 56 |
Now consider that we have a cursor for a query |
| 57 |
|
| 58 |
select b from t1 where b >= 1; |
| 59 |
|
| 60 |
This query will use secondary key on the table t1. Now after the first fetch |
| 61 |
on this cursor if we do a update: |
| 62 |
|
| 63 |
update t1 set b = 5 where b = 2; |
| 64 |
|
| 65 |
Now second fetch of the cursor should not see record (2,5) instead it should |
| 66 |
see record (2,2). |
| 67 |
|
| 68 |
We also should show that if we have delete t1 where b = 5; we still |
| 69 |
can see record (2,2). |
| 70 |
|
| 71 |
When we access a secondary key record maximum transaction id is fetched |
| 72 |
from this record and this trx_id is compared to up_limit_id in the view. |
| 73 |
If trx_id in the record is greater or equal than up_limit_id in the view |
| 74 |
cluster record is accessed. Because trx_id of the creating |
| 75 |
transaction is stored when this view was created to the list of |
| 76 |
trx_ids not seen by this read view previous version of the |
| 77 |
record is requested to be built. This is build using clustered record. |
| 78 |
If the secondary key record is delete-marked, its corresponding |
| 79 |
clustered record can be already be purged only if records |
| 80 |
trx_id < low_limit_no. Purge can't remove any record deleted by a |
| 81 |
transaction which was active when cursor was created. But, we still |
| 82 |
may have a deleted secondary key record but no clustered record. But, |
| 83 |
this is not a problem because this case is handled in |
| 84 |
row_sel_get_clust_rec() function which is called |
| 85 |
whenever we note that this read view does not see trx_id in the |
| 86 |
record. Thus, we see correct version. Q. E. D. |
| 87 |
|
| 88 |
------------------------------------------------------------------------------- |
| 89 |
FACT B: Cursor read view on a clustered index sees only committed versions |
| 90 |
------- |
| 91 |
of the records in the clustered index or those versions of rows created |
| 92 |
by transaction which created a cursor before cursor was created even |
| 93 |
if transaction which created the cursor has changed that clustered index page. |
| 94 |
|
| 95 |
PROOF: Consider e.g.following table and SQL-clauses: |
| 96 |
|
| 97 |
create table t1(a int not null, b int, primary key(a)); |
| 98 |
insert into t1 values (1),(2); |
| 99 |
commit; |
| 100 |
|
| 101 |
Now consider that we have a cursor for a query |
| 102 |
|
| 103 |
select a from t1 where a >= 1; |
| 104 |
|
| 105 |
This query will use clustered key on the table t1. Now after the first fetch |
| 106 |
on this cursor if we do a update: |
| 107 |
|
| 108 |
update t1 set a = 5 where a = 2; |
| 109 |
|
| 110 |
Now second fetch of the cursor should not see record (5) instead it should |
| 111 |
see record (2). |
| 112 |
|
| 113 |
We also should show that if we have execute delete t1 where a = 5; after |
| 114 |
the cursor is opened we still can see record (2). |
| 115 |
|
| 116 |
When accessing clustered record we always check if this read view sees |
| 117 |
trx_id stored to clustered record. By default we don't see any changes |
| 118 |
if record trx_id >= low_limit_id i.e. change was made transaction |
| 119 |
which started after transaction which created the cursor. If row |
| 120 |
was changed by the future transaction a previous version of the |
| 121 |
clustered record is created. Thus we see only committed version in |
| 122 |
this case. We see all changes made by committed transactions i.e. |
| 123 |
record trx_id < up_limit_id. In this case we don't need to do anything, |
| 124 |
we already see correct version of the record. We don't see any changes |
| 125 |
made by active transaction except creating transaction. We have stored |
| 126 |
trx_id of creating transaction to list of trx_ids when this view was |
| 127 |
created. Thus we can easily see if this record was changed by the |
| 128 |
creating transaction. Because we already have clustered record we can |
| 129 |
access roll_ptr. Using this roll_ptr we can fetch undo record. |
| 130 |
We can now check that undo_no of the undo record is less than undo_no of the |
| 131 |
trancaction which created a view when cursor was created. We see this |
| 132 |
clustered record only in case when record undo_no is less than undo_no |
| 133 |
in the view. If this is not true we build based on undo_rec previous |
| 134 |
version of the record. This record is found because purge can't remove |
| 135 |
records accessed by active transaction. Thus we see correct version. Q. E. D. |
| 136 |
------------------------------------------------------------------------------- |
| 137 |
FACT C: Purge does not remove any delete-marked row that is visible |
| 138 |
------- |
| 139 |
in any cursor read view. |
| 140 |
|
| 141 |
PROOF: We know that: |
| 142 |
1: Currently active read views in trx_sys_t::view_list are ordered by |
| 143 |
ReadView::low_limit_no in descending order, that is, |
| 144 |
newest read view first. |
| 145 |
|
| 146 |
2: Purge clones the oldest read view and uses that to determine whether there |
| 147 |
are any active transactions that can see the to be purged records. |
| 148 |
|
| 149 |
Therefore any joining or active transaction will not have a view older |
| 150 |
than the purge view, according to 1. |
| 151 |
|
| 152 |
When purge needs to remove a delete-marked row from a secondary index, |
| 153 |
it will first check that the DB_TRX_ID value of the corresponding |
| 154 |
record in the clustered index is older than the purge view. It will |
| 155 |
also check if there is a newer version of the row (clustered index |
| 156 |
record) that is not delete-marked in the secondary index. If such a |
| 157 |
row exists and is collation-equal to the delete-marked secondary index |
| 158 |
record then purge will not remove the secondary index record. |
| 159 |
|
| 160 |
Delete-marked clustered index records will be removed by |
| 161 |
row_purge_remove_clust_if_poss(), unless the clustered index record |
| 162 |
(and its DB_ROLL_PTR) has been updated. Every new version of the |
| 163 |
clustered index record will update DB_ROLL_PTR, pointing to a new UNDO |
| 164 |
log entry that allows the old version to be reconstructed. The |
| 165 |
DB_ROLL_PTR in the oldest remaining version in the old-version chain |
| 166 |
may be pointing to garbage (an undo log record discarded by purge), |
| 167 |
but it will never be dereferenced, because the purge view is older |
| 168 |
than any active transaction. |
| 169 |
|
| 170 |
For details see: row_vers_old_has_index_entry() and row_purge_poss_sec() |
| 171 |
|
| 172 |
Some additional issues: |
| 173 |
|
| 174 |
What if trx_sys->view_list == NULL and some transaction T1 and Purge both |
| 175 |
try to open read_view at same time. Only one can acquire trx_sys->mutex. |
| 176 |
In which order will the views be opened? Should it matter? If no, why? |
| 177 |
|
| 178 |
The order does not matter. No new transactions can be created and no running |
| 179 |
RW transaction can commit or rollback (or free views). AC-NL-RO transactions |
| 180 |
will mark their views as closed but not actually free their views. |
| 181 |
*/ |
| 182 |
|
| 183 |
/** Minimum number of elements to reserve in ReadView::ids_t */ |
| 184 |
static const ulint MIN_TRX_IDS = 32; |
| 185 |
|
| 186 |
#ifdef UNIV_DEBUG |
| 187 |
/** Functor to validate the view list. */ |
| 188 |
struct ViewCheck { |
| 189 |
ViewCheck() : m_prev_view() {} |
| 190 |
|
| 191 |
void operator()(const ReadView *view) { |
| 192 |
ut_a(m_prev_view == nullptr || view->is_closed() || view->le(m_prev_view)); |
| 193 |
|
| 194 |
m_prev_view = view; |
| 195 |
} |
| 196 |
|
| 197 |
const ReadView *m_prev_view; |
| 198 |
}; |
| 199 |
|
| 200 |
/** |
| 201 |
Validates a read view list. */ |
| 202 |
|
| 203 |
bool MVCC::validate() const { |
| 204 |
ViewCheck check; |
| 205 |
|
| 206 |
ut_ad(trx_sys_mutex_own()); |
| 207 |
|
| 208 |
ut_list_map(m_views, check); |
| 209 |
|
| 210 |
return (true); |
| 211 |
} |
| 212 |
#endif /* UNIV_DEBUG */ |
| 213 |
|
| 214 |
/** Try and increase the size of the array. Old elements are copied across. It |
| 215 |
is a no-op if n is < current size. |
| 216 |
@param n Make space for n elements */ |
| 217 |
void ReadView::ids_t::reserve(ulint n) { |
| 218 |
if (n <= capacity()) { |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
/** Keep a minimum threshold */ |
| 223 |
if (n < MIN_TRX_IDS) { |
| 224 |
n = MIN_TRX_IDS; |
| 225 |
} |
| 226 |
|
| 227 |
value_type *p = m_ptr; |
| 228 |
|
| 229 |
m_ptr = |
| 230 |
ut::new_arr_withkey<value_type>(UT_NEW_THIS_FILE_PSI_KEY, ut::Count{n}); |
| 231 |
|
| 232 |
m_reserved = n; |
| 233 |
|
| 234 |
ut_ad(size() < capacity()); |
| 235 |
|
| 236 |
if (p != nullptr) { |
| 237 |
::memmove(m_ptr, p, size() * sizeof(value_type)); |
| 238 |
|
| 239 |
ut::delete_arr(p); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
Copy and overwrite this array contents |
| 245 |
@param start Source array |
| 246 |
@param end Pointer to end of array */ |
| 247 |
|
| 248 |
void ReadView::ids_t::assign(const value_type *start, const value_type *end) { |
| 249 |
ut_ad(end >= start); |
| 250 |
|
| 251 |
ulint n = end - start; |
| 252 |
|
| 253 |
/* No need to copy the old contents across during reserve(). */ |
| 254 |
clear(); |
| 255 |
|
| 256 |
/* Create extra space if required. */ |
| 257 |
reserve(n); |
| 258 |
|
| 259 |
resize(n); |
| 260 |
|
| 261 |
ut_ad(size() == n); |
| 262 |
|
| 263 |
::memmove(m_ptr, start, size() * sizeof(value_type)); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
Append a value to the array. |
| 268 |
@param value the value to append */ |
| 269 |
|
| 270 |
void ReadView::ids_t::push_back(value_type value) { |
| 271 |
if (capacity() <= size()) { |
| 272 |
reserve(size() * 2); |
| 273 |
} |
| 274 |
|
| 275 |
m_ptr[m_size++] = value; |
| 276 |
ut_ad(size() <= capacity()); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
Insert the value in the correct slot, preserving the order. Doesn't |
| 281 |
check for duplicates. */ |
| 282 |
|
| 283 |
void ReadView::ids_t::insert(value_type value) { |
| 284 |
ut_ad(value > 0); |
| 285 |
|
| 286 |
reserve(size() + 1); |
| 287 |
|
| 288 |
if (empty() || back() < value) { |
| 289 |
push_back(value); |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
value_type *end = data() + size(); |
| 294 |
value_type *ub = std::upper_bound(data(), end, value); |
| 295 |
|
| 296 |
if (ub == end) { |
| 297 |
push_back(value); |
| 298 |
} else { |
| 299 |
ut_ad(ub < end); |
| 300 |
|
| 301 |
ulint n_elems = std::distance(ub, end); |
| 302 |
ulint n = n_elems * sizeof(value_type); |
| 303 |
|
| 304 |
/* Note: Copying overlapped memory locations. */ |
| 305 |
::memmove(ub + 1, ub, n); |
| 306 |
|
| 307 |
*ub = value; |
| 308 |
|
| 309 |
resize(size() + 1); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
ReadView constructor */ |
| 315 |
ReadView::ReadView() |
| 316 |
: m_low_limit_id(), |
| 317 |
m_up_limit_id(), |
| 318 |
m_creator_trx_id(), |
| 319 |
m_ids(), |
| 320 |
m_low_limit_no() { |
| 321 |
ut_d(::memset(&m_view_list, 0x0, sizeof(m_view_list))); |
| 322 |
ut_d(m_view_low_limit_no = 0); |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
ReadView destructor */ |
| 327 |
ReadView::~ReadView() { |
| 328 |
// Do nothing |
| 329 |
} |
| 330 |
|
| 331 |
/** Constructor |
| 332 |
@param size Number of views to pre-allocate */ |
| 333 |
MVCC::MVCC(ulint size) : m_free(), m_views() { |
| 334 |
for (ulint i = 0; i < size; ++i) { |
| 335 |
ReadView *view = ut::new_withkey<ReadView>(UT_NEW_THIS_FILE_PSI_KEY); |
| 336 |
|
| 337 |
UT_LIST_ADD_FIRST(m_free, view); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
MVCC::~MVCC() { |
| 342 |
while (ReadView *view = UT_LIST_GET_FIRST(m_free)) { |
| 343 |
UT_LIST_REMOVE(m_free, view); |
| 344 |
|
| 345 |
ut::delete_(view); |
| 346 |
} |
| 347 |
|
| 348 |
ut_a(UT_LIST_GET_LEN(m_views) == 0); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
Copy the transaction ids from the source vector */ |
| 353 |
|
| 354 |
void ReadView::copy_trx_ids(const trx_ids_t &trx_ids) { |
| 355 |
ut_ad(trx_sys_mutex_own()); |
| 356 |
|
| 357 |
ulint size = trx_ids.size(); |
| 358 |
|
| 359 |
if (m_creator_trx_id > 0) { |
| 360 |
ut_ad(size > 0); |
| 361 |
--size; |
| 362 |
} |
| 363 |
|
| 364 |
if (size == 0) { |
| 365 |
m_ids.clear(); |
| 366 |
return; |
| 367 |
} |
| 368 |
|
| 369 |
m_ids.reserve(size); |
| 370 |
m_ids.resize(size); |
| 371 |
|
| 372 |
ids_t::value_type *p = m_ids.data(); |
| 373 |
|
| 374 |
/* Copy all the trx_ids except the creator trx id */ |
| 375 |
|
| 376 |
if (m_creator_trx_id > 0) { |
| 377 |
/* Note: We go through all this trouble because it is |
| 378 |
unclear whether std::vector::resize() will cause an |
| 379 |
overhead or not. We should test this extensively and |
| 380 |
if the vector to vector copy is fast enough then get |
| 381 |
rid of this code and replace it with more readable |
| 382 |
and obvious code. The code below does exactly one copy, |
| 383 |
and filters out the creator's trx id. */ |
| 384 |
|
| 385 |
trx_ids_t::const_iterator it = |
| 386 |
std::lower_bound(trx_ids.begin(), trx_ids.end(), m_creator_trx_id); |
| 387 |
|
| 388 |
ut_ad(it != trx_ids.end() && *it == m_creator_trx_id); |
| 389 |
|
| 390 |
ulint i = std::distance(trx_ids.begin(), it); |
| 391 |
ulint n = i * sizeof(trx_ids_t::value_type); |
| 392 |
|
| 393 |
::memmove(p, &trx_ids[0], n); |
| 394 |
|
| 395 |
n = (trx_ids.size() - i - 1) * sizeof(trx_ids_t::value_type); |
| 396 |
|
| 397 |
ut_ad(i + (n / sizeof(trx_ids_t::value_type)) == m_ids.size()); |
| 398 |
|
| 399 |
if (n > 0) { |
| 400 |
::memmove(p + i, &trx_ids[i + 1], n); |
| 401 |
} |
| 402 |
} else { |
| 403 |
ulint n = size * sizeof(trx_ids_t::value_type); |
| 404 |
|
| 405 |
::memmove(p, &trx_ids[0], n); |
| 406 |
} |
| 407 |
|
| 408 |
m_up_limit_id = m_ids.front(); |
| 409 |
|
| 410 |
#ifdef UNIV_DEBUG |
| 411 |
/* The check is done randomly from time to time, because the check adds |
| 412 |
a kind of extra synchronization which itself could hide existing bugs. */ |
| 413 |
if (ut::random_from_interval_fast(0, 99) == 0) { |
| 414 |
/* Assert that all transaction ids in list are active. */ |
| 415 |
for (auto trx_id : trx_ids) { |
| 416 |
while (trx_sys->latch_and_execute_with_active_trx( |
| 417 |
trx_id, |
| 418 |
[](trx_t *trx) { |
| 419 |
if (trx != nullptr) { |
| 420 |
const auto trx_state = trx->state.load(std::memory_order_relaxed); |
| 421 |
/* Transaction in active_rw_trxs might only be ACTIVE or |
| 422 |
PREPARED, before it becomes COMMITTED it is removed from |
| 423 |
active_rw_trxs. */ |
| 424 |
ut_ad(trx_state == TRX_STATE_ACTIVE || |
| 425 |
trx_state == TRX_STATE_PREPARED); |
| 426 |
return false; |
| 427 |
} |
| 428 |
return true; |
| 429 |
}, |
| 430 |
UT_LOCATION_HERE)) { |
| 431 |
/* It might happen that transaction became added to rw_trx_ids, |
| 432 |
then trx_sys mutex has been released and thread become scheduled |
| 433 |
out before the call to trx_sys_rw_trx_add(trx). We need to wait, |
| 434 |
it will come (if we hang forever in this loop - it's a bug). */ |
| 435 |
std::this_thread::sleep_for(std::chrono::microseconds(10)); |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
#endif /* UNIV_DEBUG */ |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
Opens a read view where exactly the transactions serialized before this |
| 444 |
point in time are seen in the view. |
| 445 |
@param id Creator transaction id */ |
| 446 |
|
| 447 |
void ReadView::prepare(trx_id_t id) { |
| 448 |
ut_ad(trx_sys_mutex_own()); |
| 449 |
|
| 450 |
m_creator_trx_id = id; |
| 451 |
|
| 452 |
m_low_limit_no = trx_get_serialisation_min_trx_no(); |
| 453 |
|
| 454 |
m_low_limit_id = trx_sys_get_next_trx_id_or_no(); |
| 455 |
|
| 456 |
ut_a(m_low_limit_no <= m_low_limit_id); |
| 457 |
|
| 458 |
if (!trx_sys->rw_trx_ids.empty()) { |
| 459 |
copy_trx_ids(trx_sys->rw_trx_ids); |
| 460 |
} else { |
| 461 |
m_ids.clear(); |
| 462 |
} |
| 463 |
|
| 464 |
/* The first active transaction has the smallest id. */ |
| 465 |
m_up_limit_id = !m_ids.empty() ? m_ids.front() : m_low_limit_id; |
| 466 |
|
| 467 |
ut_a(m_up_limit_id <= m_low_limit_id); |
| 468 |
|
| 469 |
ut_d(m_view_low_limit_no = m_low_limit_no); |
| 470 |
m_closed = false; |
| 471 |
} |
| 472 |
|
| 473 |
/** |
| 474 |
Find a free view from the active list, if none found then allocate |
| 475 |
a new view. |
| 476 |
@return a view to use */ |
| 477 |
|
| 478 |
ReadView *MVCC::get_view() { |
| 479 |
ut_ad(trx_sys_mutex_own()); |
| 480 |
|
| 481 |
ReadView *view; |
| 482 |
|
| 483 |
if (UT_LIST_GET_LEN(m_free) > 0) { |
| 484 |
view = UT_LIST_GET_FIRST(m_free); |
| 485 |
UT_LIST_REMOVE(m_free, view); |
| 486 |
} else { |
| 487 |
view = ut::new_withkey<ReadView>(UT_NEW_THIS_FILE_PSI_KEY); |
| 488 |
|
| 489 |
if (view == nullptr) { |
| 490 |
ib::error(ER_IB_MSG_918) << "Failed to allocate MVCC view"; |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
return (view); |
| 495 |
} |
| 496 |
|
| 497 |
/** |
| 498 |
Release a view that is inactive but not closed. Caller must own |
| 499 |
the trx_sys_t::mutex. |
| 500 |
@param view View to release */ |
| 501 |
void MVCC::view_release(ReadView *&view) { |
| 502 |
ut_ad(!srv_read_only_mode); |
| 503 |
ut_ad(trx_sys_mutex_own()); |
| 504 |
|
| 505 |
uintptr_t p = reinterpret_cast<uintptr_t>(view); |
| 506 |
|
| 507 |
ut_a(p & 0x1); |
| 508 |
|
| 509 |
view = reinterpret_cast<ReadView *>(p & ~1); |
| 510 |
|
| 511 |
ut_ad(view->m_closed); |
| 512 |
|
| 513 |
/** RW transactions should not free their views here. Their views |
| 514 |
should freed using view_close_view() */ |
| 515 |
|
| 516 |
ut_ad(view->m_creator_trx_id == 0); |
| 517 |
|
| 518 |
UT_LIST_REMOVE(m_views, view); |
| 519 |
|
| 520 |
UT_LIST_ADD_LAST(m_free, view); |
| 521 |
|
| 522 |
view = nullptr; |
| 523 |
} |
| 524 |
|
| 525 |
/** Allocate and create a view. |
| 526 |
@param view View owned by this class created for the caller. Must be |
| 527 |
freed by calling view_close() |
| 528 |
@param trx Transaction instance of caller */ |
| 529 |
void MVCC::view_open(ReadView *&view, trx_t *trx) { |
| 530 |
ut_ad(!srv_read_only_mode); |
| 531 |
|
| 532 |
/** If no new RW transaction has been started since the last view |
| 533 |
was created then reuse the the existing view. */ |
| 534 |
if (view != nullptr) { |
| 535 |
uintptr_t p = reinterpret_cast<uintptr_t>(view); |
| 536 |
|
| 537 |
view = reinterpret_cast<ReadView *>(p & ~1); |
| 538 |
|
| 539 |
ut_ad(view->m_closed); |
| 540 |
|
| 541 |
/* NOTE: This can be optimised further, for now we only |
| 542 |
reuse the view if there are no active RW transactions. |
| 543 |
|
| 544 |
There is an inherent race here between purge and this |
| 545 |
thread. Purge will skip views that are marked as closed. |
| 546 |
Therefore we must set the low limit id after we reset the |
| 547 |
closed status after the check. */ |
| 548 |
|
| 549 |
if (trx_is_autocommit_non_locking(trx) && view->empty()) { |
| 550 |
view->m_closed = false; |
| 551 |
|
| 552 |
if (view->m_low_limit_id == trx_sys_get_next_trx_id_or_no()) { |
| 553 |
return; |
| 554 |
} else { |
| 555 |
view->m_closed = true; |
| 556 |
} |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
trx_sys_mutex_enter(); |
| 561 |
|
| 562 |
if (view != nullptr) { |
| 563 |
UT_LIST_REMOVE(m_views, view); |
| 564 |
|
| 565 |
} else { |
| 566 |
view = get_view(); |
| 567 |
} |
| 568 |
|
| 569 |
if (view != nullptr) { |
| 570 |
view->prepare(trx->id); |
| 571 |
|
| 572 |
UT_LIST_ADD_FIRST(m_views, view); |
| 573 |
|
| 574 |
ut_ad(!view->is_closed()); |
| 575 |
|
| 576 |
ut_ad(validate()); |
| 577 |
} |
| 578 |
|
| 579 |
trx_sys_mutex_exit(); |
| 580 |
} |
| 581 |
|
| 582 |
ReadView *MVCC::get_view_created_by_trx_id(trx_id_t trx_id) const { |
| 583 |
ReadView *view; |
| 584 |
|
| 585 |
ut_ad(trx_sys_mutex_own()); |
| 586 |
|
| 587 |
for (view = UT_LIST_GET_LAST(m_views); view != nullptr; |
| 588 |
view = UT_LIST_GET_PREV(m_view_list, view)) { |
| 589 |
if (view->is_closed()) { |
| 590 |
continue; |
| 591 |
} |
| 592 |
|
| 593 |
if (view->m_creator_trx_id == trx_id) { |
| 594 |
break; |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
return (view); |
| 599 |
} |
| 600 |
|
| 601 |
/** |
| 602 |
Get the oldest (active) view in the system. |
| 603 |
@return oldest view if found or NULL */ |
| 604 |
|
| 605 |
ReadView *MVCC::get_oldest_view() const { |
| 606 |
ReadView *view; |
| 607 |
|
| 608 |
ut_ad(trx_sys_mutex_own()); |
| 609 |
|
| 610 |
for (view = UT_LIST_GET_LAST(m_views); view != nullptr; |
| 611 |
view = UT_LIST_GET_PREV(m_view_list, view)) { |
| 612 |
if (!view->is_closed()) { |
| 613 |
break; |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
return (view); |
| 618 |
} |
| 619 |
|
| 620 |
/** |
| 621 |
Copy state from another view. Must call copy_complete() to finish. |
| 622 |
@param other view to copy from */ |
| 623 |
|
| 624 |
void ReadView::copy_prepare(const ReadView &other) { |
| 625 |
ut_ad(&other != this); |
| 626 |
|
| 627 |
if (!other.m_ids.empty()) { |
| 628 |
const ids_t::value_type *p = other.m_ids.data(); |
| 629 |
|
| 630 |
m_ids.assign(p, p + other.m_ids.size()); |
| 631 |
} else { |
| 632 |
m_ids.clear(); |
| 633 |
} |
| 634 |
|
| 635 |
m_up_limit_id = other.m_up_limit_id; |
| 636 |
|
| 637 |
m_low_limit_no = other.m_low_limit_no; |
| 638 |
|
| 639 |
ut_d(m_view_low_limit_no = other.m_view_low_limit_no); |
| 640 |
|
| 641 |
m_low_limit_id = other.m_low_limit_id; |
| 642 |
|
| 643 |
m_creator_trx_id = other.m_creator_trx_id; |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
Complete the copy, insert the creator transaction id into the |
| 648 |
m_ids too and adjust the m_up_limit_id, if required */ |
| 649 |
|
| 650 |
void ReadView::copy_complete() { |
| 651 |
ut_ad(!trx_sys_mutex_own()); |
| 652 |
|
| 653 |
if (m_creator_trx_id > 0) { |
| 654 |
m_ids.insert(m_creator_trx_id); |
| 655 |
} |
| 656 |
|
| 657 |
if (!m_ids.empty()) { |
| 658 |
/* The last active transaction has the smallest id. */ |
| 659 |
m_up_limit_id = std::min(m_ids.front(), m_up_limit_id); |
| 660 |
} |
| 661 |
|
| 662 |
ut_ad(m_up_limit_id <= m_low_limit_id); |
| 663 |
|
| 664 |
/* We added the creator transaction ID to the m_ids. */ |
| 665 |
m_creator_trx_id = 0; |
| 666 |
} |
| 667 |
|
| 668 |
/** Clones the oldest view and stores it in view. No need to |
| 669 |
call view_close(). The caller owns the view that is passed in. |
| 670 |
It will also move the closed views from the m_views list to the |
| 671 |
m_free list. This function is called by Purge to determine whether it should |
| 672 |
purge the delete marked record or not. |
| 673 |
@param view Preallocated view, owned by the caller */ |
| 674 |
void MVCC::clone_oldest_view(ReadView *view) { |
| 675 |
trx_sys_mutex_enter(); |
| 676 |
|
| 677 |
ReadView *oldest_view = get_oldest_view(); |
| 678 |
|
| 679 |
if (oldest_view == nullptr) { |
| 680 |
view->prepare(0); |
| 681 |
|
| 682 |
trx_sys_mutex_exit(); |
| 683 |
|
| 684 |
} else { |
| 685 |
view->copy_prepare(*oldest_view); |
| 686 |
|
| 687 |
trx_sys_mutex_exit(); |
| 688 |
|
| 689 |
view->copy_complete(); |
| 690 |
} |
| 691 |
/* Update view to block purging transaction till GTID is persisted. */ |
| 692 |
auto >id_persistor = clone_sys->get_gtid_persistor(); |
| 693 |
auto gtid_oldest_trxno = gtid_persistor.get_oldest_trx_no(); |
| 694 |
view->reduce_low_limit(gtid_oldest_trxno); |
| 695 |
} |
| 696 |
|
| 697 |
/** |
| 698 |
@return the number of active views */ |
| 699 |
|
| 700 |
ulint MVCC::size() const { |
| 701 |
trx_sys_mutex_enter(); |
| 702 |
|
| 703 |
ulint size = 0; |
| 704 |
|
| 705 |
for (const ReadView *view : m_views) { |
| 706 |
if (!view->is_closed()) { |
| 707 |
++size; |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
trx_sys_mutex_exit(); |
| 712 |
|
| 713 |
return (size); |
| 714 |
} |
| 715 |
|
| 716 |
/** |
| 717 |
Close a view created by the above function. |
| 718 |
@param view view allocated by trx_open. |
| 719 |
@param own_mutex true if caller owns trx_sys_t::mutex */ |
| 720 |
|
| 721 |
void MVCC::view_close(ReadView *&view, bool own_mutex) { |
| 722 |
uintptr_t p = reinterpret_cast<uintptr_t>(view); |
| 723 |
|
| 724 |
/* Note: The assumption here is that AC-NL-RO transactions will |
| 725 |
call this function with own_mutex == false. */ |
| 726 |
if (!own_mutex) { |
| 727 |
/* Sanitise the pointer first. */ |
| 728 |
ReadView *ptr = reinterpret_cast<ReadView *>(p & ~1); |
| 729 |
|
| 730 |
/* Note this can be called for a read view that |
| 731 |
was already closed. */ |
| 732 |
ptr->m_closed = true; |
| 733 |
|
| 734 |
/* Set the view as closed. */ |
| 735 |
view = reinterpret_cast<ReadView *>(p | 0x1); |
| 736 |
} else { |
| 737 |
view = reinterpret_cast<ReadView *>(p & ~1); |
| 738 |
|
| 739 |
view->close(); |
| 740 |
|
| 741 |
UT_LIST_REMOVE(m_views, view); |
| 742 |
UT_LIST_ADD_LAST(m_free, view); |
| 743 |
|
| 744 |
ut_ad(validate()); |
| 745 |
|
| 746 |
view = nullptr; |
| 747 |
} |
| 748 |
} |