| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2016, 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 |
#include <scope_guard.h> |
| 29 |
#include "lob0del.h" |
| 30 |
#include "lob0first.h" |
| 31 |
#include "lob0index.h" |
| 32 |
#include "lob0inf.h" |
| 33 |
#include "lob0lob.h" |
| 34 |
#include "log0buf.h" |
| 35 |
#include "row0purge.h" |
| 36 |
#include "row0upd.h" |
| 37 |
#include "trx0purge.h" |
| 38 |
#include "trx0rec.h" |
| 39 |
#include "zlob0first.h" |
| 40 |
#include "zlob0index.h" |
| 41 |
#include "zlob0read.h" |
| 42 |
|
| 43 |
namespace lob { |
| 44 |
|
| 45 |
/** Rollback from undo log information. |
| 46 |
@param[in] ctx the delete operation context. |
| 47 |
@param[in] index the clustered index to which LOB belongs. |
| 48 |
@param[in] uf the update vector of concerned field. */ |
| 49 |
static void rollback_from_undolog(DeleteContext *ctx, dict_index_t *index, |
| 50 |
const upd_field_t *uf) { |
| 51 |
DBUG_TRACE; |
| 52 |
|
| 53 |
dberr_t err = apply_undolog(ctx->get_mtr(), index, ctx->m_blobref, uf); |
| 54 |
ut_a(err == DB_SUCCESS); |
| 55 |
} |
| 56 |
|
| 57 |
/** Rollback modification of a uncompressed LOB. |
| 58 |
@param[in] ctx the delete operation context information. |
| 59 |
@param[in] index clustered index in which LOB is present |
| 60 |
@param[in] trxid the transaction that is being rolled back. |
| 61 |
@param[in] undo_no during rollback to savepoint, rollback only |
| 62 |
up to this undo number. |
| 63 |
@param[in] rec_type undo record type. |
| 64 |
@param[in] uf update vector of the concerned field. */ |
| 65 |
static void rollback(DeleteContext *ctx, dict_index_t *index, trx_id_t trxid, |
| 66 |
undo_no_t undo_no, ulint rec_type, const upd_field_t *uf) { |
| 67 |
DBUG_TRACE; |
| 68 |
|
| 69 |
ref_t &ref = ctx->m_blobref; |
| 70 |
|
| 71 |
ut_ad(ctx->m_rollback); |
| 72 |
|
| 73 |
if (uf != nullptr && uf->lob_diffs != nullptr && uf->lob_diffs->size() > 0) { |
| 74 |
/* Undo log contains changes done to the LOB. This must have |
| 75 |
been a small change done to LOB. Apply the undo log on the |
| 76 |
LOB.*/ |
| 77 |
rollback_from_undolog(ctx, index, uf); |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
/* Our local_mtr needs to use the same mode as the ctx's mtr, as for example |
| 82 |
built-in tables do not expect redo logging, so we should respect that */ |
| 83 |
mtr_log_t parent_mtr_log_mode = ctx->get_mtr()->get_log_mode(); |
| 84 |
|
| 85 |
mtr_t local_mtr; |
| 86 |
mtr_start(&local_mtr); |
| 87 |
local_mtr.set_log_mode(parent_mtr_log_mode); |
| 88 |
|
| 89 |
ctx->x_latch_rec_page(&local_mtr); |
| 90 |
|
| 91 |
#ifdef UNIV_DEBUG |
| 92 |
const ulint lob_size = ref.length(); |
| 93 |
#endif /* UNIV_DEBUG */ |
| 94 |
|
| 95 |
/* We mark the LOB as partially deleted here, so that if we crash during the |
| 96 |
while() loop below, then during recovery we will know that the remaining LOB |
| 97 |
data should not be read. OTOH we do not ref.set_page_no(FIL_NULL, &local_mtr) |
| 98 |
until we delete all the pages, so that the recovery can use the reference to |
| 99 |
find the remaining parts of the LOB. */ |
| 100 |
ref.set_length(0, &local_mtr); |
| 101 |
|
| 102 |
page_no_t first_page_no = ref.page_no(); |
| 103 |
page_id_t page_id(ref.space_id(), first_page_no); |
| 104 |
page_size_t page_size(dict_table_page_size(index->table)); |
| 105 |
|
| 106 |
first_page_t first(&local_mtr, index); |
| 107 |
first.load_x(page_id, page_size); |
| 108 |
|
| 109 |
flst_base_node_t *flst = first.index_list(); |
| 110 |
fil_addr_t node_loc = flst_get_first(flst, &local_mtr); |
| 111 |
|
| 112 |
#ifdef UNIV_DEBUG |
| 113 |
size_t iteration = 0; |
| 114 |
#endif /* UNIV_DEBUG */ |
| 115 |
|
| 116 |
while (!fil_addr_is_null(node_loc)) { |
| 117 |
flst_node_t *node = first.addr2ptr_x(node_loc); |
| 118 |
index_entry_t cur_entry(node, &local_mtr, index); |
| 119 |
if (cur_entry.can_rollback(trxid, undo_no)) { |
| 120 |
node_loc = cur_entry.make_old_version_current(index, first); |
| 121 |
} else { |
| 122 |
node_loc = cur_entry.get_next(); |
| 123 |
} |
| 124 |
|
| 125 |
#ifdef UNIV_DEBUG |
| 126 |
++iteration; |
| 127 |
const ulint index_len = flst_get_len(first.index_list()); |
| 128 |
DBUG_EXECUTE_IF("lob_rollback_print_index_size", { |
| 129 |
ib::info(ER_IB_LOB_ROLLBACK_INDEX_LEN, ulonglong{trxid}, |
| 130 |
ulonglong{undo_no}, ulonglong{index_len}, ulonglong{iteration}); |
| 131 |
}); |
| 132 |
#endif /* UNIV_DEBUG */ |
| 133 |
|
| 134 |
/* Ensure that the parent mtr (btr_mtr) and the child mtr (lob_mtr) |
| 135 |
do not make conflicting modifications. */ |
| 136 |
ut_ad(!local_mtr.conflicts_with(ctx->get_mtr())); |
| 137 |
mtr_commit(&local_mtr); |
| 138 |
|
| 139 |
DBUG_INJECT_CRASH_WITH_LOG_FLUSH("crash_middle_of_lob_rollback", |
| 140 |
static_cast<unsigned>(iteration)); |
| 141 |
|
| 142 |
mtr_start(&local_mtr); |
| 143 |
local_mtr.set_log_mode(parent_mtr_log_mode); |
| 144 |
/* We need to reacquire the first page, because in the next iteration of |
| 145 |
the loop we might access not only the FIL_PAGE_TYPE_LOB_INDEX page which |
| 146 |
contains node_loc, but also the FIL_PAGE_TYPE_LOB_FIRST which contains |
| 147 |
the crucial entry index lists base nodes */ |
| 148 |
first.load_x(page_id, page_size); |
| 149 |
} |
| 150 |
|
| 151 |
if (rec_type == TRX_UNDO_INSERT_REC || first.is_empty()) { |
| 152 |
if (dict_index_is_online_ddl(index)) { |
| 153 |
row_log_table_blob_free(index, ref.page_no()); |
| 154 |
} |
| 155 |
|
| 156 |
first.destroy(); |
| 157 |
|
| 158 |
} else { |
| 159 |
ut_ad(first.validate()); |
| 160 |
#ifdef UNIV_DEBUG |
| 161 |
/* We set ref length to 0 at the beginning of a rollback(), so seeing 0 |
| 162 |
indicates a crash might have happened in the middle of a previous rollback() |
| 163 |
and we are now cleaning the left-overs during recovery. */ |
| 164 |
if (lob_size != 0) { |
| 165 |
fil_addr_t first_node_loc = flst_get_first(flst, &local_mtr); |
| 166 |
ut_ad(validate_size(lob_size, index, first_node_loc, &local_mtr)); |
| 167 |
} |
| 168 |
#endif /* UNIV_DEBUG */ |
| 169 |
} |
| 170 |
|
| 171 |
DBUG_INJECT_CRASH_WITH_LOG_FLUSH("crash_almost_end_of_lob_rollback", 0); |
| 172 |
/* We are done with cleaning up index entries for the given version, so now we |
| 173 |
can modify the reference, so that it is no longer reachable. */ |
| 174 |
ctx->x_latch_rec_page(&local_mtr); |
| 175 |
ref.set_page_no(FIL_NULL, &local_mtr); |
| 176 |
ut_ad(ref.length() == 0); |
| 177 |
|
| 178 |
/* Ensure that the parent mtr (btr_mtr) and the child mtr (lob_mtr) |
| 179 |
do not make conflicting modifications. */ |
| 180 |
ut_ad(!local_mtr.conflicts_with(ctx->get_mtr())); |
| 181 |
mtr_commit(&local_mtr); |
| 182 |
|
| 183 |
DBUG_INJECT_CRASH_WITH_LOG_FLUSH("crash_end_of_lob_rollback", 0); |
| 184 |
} |
| 185 |
|
| 186 |
/** Rollback modification of a compressed LOB. |
| 187 |
@param[in] ctx the delete operation context information. |
| 188 |
@param[in] index clustered index in which LOB is present |
| 189 |
@param[in] trxid the transaction that is being rolled back. |
| 190 |
@param[in] undo_no during rollback to savepoint, rollback only |
| 191 |
up to this undo number. |
| 192 |
@param[in] rec_type undo record type. */ |
| 193 |
static void z_rollback(DeleteContext *ctx, dict_index_t *index, trx_id_t trxid, |
| 194 |
undo_no_t undo_no, ulint rec_type) { |
| 195 |
ut_ad(ctx->m_rollback); |
| 196 |
|
| 197 |
ref_t &ref = ctx->m_blobref; |
| 198 |
|
| 199 |
mtr_t local_mtr; |
| 200 |
mtr_start(&local_mtr); |
| 201 |
|
| 202 |
ctx->x_latch_rec_page(&local_mtr); |
| 203 |
/* We mark the LOB as partially deleted here, so that if we crash during the |
| 204 |
while() loop below, then during recovery we will know that the remaining LOB |
| 205 |
data should not be read. OTOH we do not ref.set_page_no(FIL_NULL, 0) |
| 206 |
until we delete all the pages, so that the recovery can use the reference to |
| 207 |
find the remaining parts of the LOB. */ |
| 208 |
ref.set_length(0, nullptr); |
| 209 |
ctx->zblob_write_blobref(ctx->m_field_no, &local_mtr); |
| 210 |
|
| 211 |
page_no_t first_page_no = ref.page_no(); |
| 212 |
page_id_t page_id(ref.space_id(), first_page_no); |
| 213 |
page_size_t page_size(dict_table_page_size(index->table)); |
| 214 |
|
| 215 |
z_first_page_t first(&local_mtr, index); |
| 216 |
first.load_x(page_id, page_size); |
| 217 |
|
| 218 |
flst_base_node_t *flst = first.index_list(); |
| 219 |
fil_addr_t node_loc = flst_get_first(flst, &local_mtr); |
| 220 |
|
| 221 |
#ifdef UNIV_DEBUG |
| 222 |
size_t iteration = 0; |
| 223 |
#endif /* UNIV_DEBUG */ |
| 224 |
|
| 225 |
while (!fil_addr_is_null(node_loc)) { |
| 226 |
flst_node_t *node = first.addr2ptr_x(node_loc); |
| 227 |
z_index_entry_t cur_entry(node, &local_mtr, index); |
| 228 |
|
| 229 |
if (cur_entry.can_rollback(trxid, undo_no)) { |
| 230 |
node_loc = cur_entry.make_old_version_current(index, first); |
| 231 |
} else { |
| 232 |
node_loc = cur_entry.get_next(); |
| 233 |
} |
| 234 |
|
| 235 |
#ifdef UNIV_DEBUG |
| 236 |
++iteration; |
| 237 |
const ulint index_len = flst_get_len(first.index_list()); |
| 238 |
DBUG_EXECUTE_IF("lob_rollback_print_index_size", { |
| 239 |
ib::info(ER_IB_LOB_ROLLBACK_INDEX_LEN, ulonglong{trxid}, |
| 240 |
ulonglong{undo_no}, ulonglong{index_len}, ulonglong{iteration}); |
| 241 |
}); |
| 242 |
#endif /* UNIV_DEBUG */ |
| 243 |
|
| 244 |
/* Ensure that the parent mtr (btr_mtr) and the child mtr (lob_mtr) |
| 245 |
do not make conflicting modifications. */ |
| 246 |
ut_ad(!local_mtr.conflicts_with(ctx->get_mtr())); |
| 247 |
mtr_commit(&local_mtr); |
| 248 |
|
| 249 |
DBUG_INJECT_CRASH_WITH_LOG_FLUSH("crash_middle_of_lob_rollback", |
| 250 |
static_cast<unsigned>(iteration)); |
| 251 |
|
| 252 |
mtr_start(&local_mtr); |
| 253 |
first.load_x(page_id, page_size); |
| 254 |
} |
| 255 |
|
| 256 |
if (rec_type == TRX_UNDO_INSERT_REC || first.is_empty()) { |
| 257 |
if (dict_index_is_online_ddl(index)) { |
| 258 |
row_log_table_blob_free(index, ref.page_no()); |
| 259 |
} |
| 260 |
|
| 261 |
first.destroy(); |
| 262 |
|
| 263 |
} else { |
| 264 |
ut_ad(first.validate()); |
| 265 |
} |
| 266 |
|
| 267 |
DBUG_INJECT_CRASH_WITH_LOG_FLUSH("crash_almost_end_of_lob_rollback", 0); |
| 268 |
ut_ad(ctx->get_page_zip() != nullptr); |
| 269 |
/* We are done with cleaning up index entries for the given version, so now we |
| 270 |
can modify the reference, so that it is no longer reachable. */ |
| 271 |
ref.set_page_no(FIL_NULL, nullptr); |
| 272 |
ut_ad(ref.length() == 0); |
| 273 |
ctx->x_latch_rec_page(&local_mtr); |
| 274 |
ctx->zblob_write_blobref(ctx->m_field_no, &local_mtr); |
| 275 |
|
| 276 |
mtr_commit(&local_mtr); |
| 277 |
|
| 278 |
DBUG_INJECT_CRASH_WITH_LOG_FLUSH("crash_end_of_lob_rollback", 0); |
| 279 |
} |
| 280 |
|
| 281 |
/** Purge a compressed LOB. |
| 282 |
@param[in] ctx The delete operation context information. |
| 283 |
@param[in] index Clustered index in which LOB is present |
| 284 |
@param[in] trxid The transaction that is being purged. |
| 285 |
@param[in] undo_no During rollback to savepoint, purge only up to |
| 286 |
this undo number. |
| 287 |
@param[in] rec_type Undo record type. |
| 288 |
@param[in,out] purge_node if nullptr, free the complete LOB. Otherwise, |
| 289 |
save the first page of LOB in this purge node.*/ |
| 290 |
static void z_purge(DeleteContext *ctx, dict_index_t *index, trx_id_t trxid, |
| 291 |
undo_no_t undo_no, ulint rec_type, |
| 292 |
purge_node_t *purge_node) { |
| 293 |
const bool is_rollback = ctx->m_rollback; |
| 294 |
ref_t &ref = ctx->m_blobref; |
| 295 |
const page_size_t page_size(dict_table_page_size(index->table)); |
| 296 |
|
| 297 |
if (is_rollback) { |
| 298 |
z_rollback(ctx, index, trxid, undo_no, rec_type); |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
mtr_t *mtr = ctx->get_mtr(); |
| 303 |
page_no_t first_page_no = ref.page_no(); |
| 304 |
page_id_t page_id(ref.space_id(), first_page_no); |
| 305 |
|
| 306 |
/* Hold exclusive access to LOB */ |
| 307 |
z_first_page_t btr_first(mtr, index); |
| 308 |
btr_first.load_x(page_id, page_size); |
| 309 |
|
| 310 |
const trx_id_t last_trx_id = btr_first.get_last_trx_id(); |
| 311 |
const undo_no_t last_undo_no = btr_first.get_last_trx_undo_no(); |
| 312 |
|
| 313 |
bool ok_to_free_2 = (rec_type == TRX_UNDO_UPD_EXIST_REC || |
| 314 |
rec_type == TRX_UNDO_UPD_DEL_REC) && |
| 315 |
!btr_first.can_be_partially_updated() && |
| 316 |
(last_trx_id == trxid) && (last_undo_no == undo_no); |
| 317 |
|
| 318 |
if (rec_type == TRX_UNDO_DEL_MARK_REC || ok_to_free_2) { |
| 319 |
if (dict_index_is_online_ddl(index)) { |
| 320 |
row_log_table_blob_free(index, ref.page_no()); |
| 321 |
} |
| 322 |
|
| 323 |
if (purge_node == nullptr) { |
| 324 |
btr_first.destroy(); |
| 325 |
} else { |
| 326 |
/* In this case, the LOB is left with only the first page. Subsequently |
| 327 |
the LOB first page number in the LOB reference is set to FIL_NULL. |
| 328 |
This means that the LOB page is only accessible via an in-memory |
| 329 |
reference held in the purge node. If a crash happens after the btr_mtr |
| 330 |
commit and before freeing the LOB first page, then the LOB first page |
| 331 |
will be leaked. We need to come up with a mechanism to avoid this leak.*/ |
| 332 |
btr_first.make_empty(); |
| 333 |
purge_node->add_lob_page(index, page_id); |
| 334 |
} |
| 335 |
|
| 336 |
if (ctx->get_page_zip() != nullptr) { |
| 337 |
ref.set_page_no(FIL_NULL, nullptr); |
| 338 |
ref.set_length(0, nullptr); |
| 339 |
ctx->zblob_write_blobref(ctx->m_field_no, mtr); |
| 340 |
} else { |
| 341 |
/* Only purge operation should reach this else block. */ |
| 342 |
ut_ad(purge_node != nullptr); |
| 343 |
|
| 344 |
/* Note that page_zip will be NULL in |
| 345 |
row_purge_upd_exist_or_extern(). */ |
| 346 |
ref.set_page_no(FIL_NULL, mtr); |
| 347 |
ref.set_length(0, mtr); |
| 348 |
} |
| 349 |
|
| 350 |
return; |
| 351 |
} |
| 352 |
|
| 353 |
mtr_t lob_mtr; |
| 354 |
mtr_start(&lob_mtr); |
| 355 |
lob_mtr.set_log_mode(mtr->get_log_mode()); |
| 356 |
|
| 357 |
z_first_page_t first(&lob_mtr, index); |
| 358 |
first.load_x(first_page_no); |
| 359 |
|
| 360 |
ut_ad(first.validate()); |
| 361 |
|
| 362 |
ut_ad(first.get_page_type() == FIL_PAGE_TYPE_ZLOB_FIRST); |
| 363 |
|
| 364 |
flst_base_node_t *flst = first.index_list(); |
| 365 |
flst_base_node_t *free_list = first.free_list(); |
| 366 |
fil_addr_t node_loc = flst_get_first(flst, &lob_mtr); |
| 367 |
|
| 368 |
z_index_entry_t cur_entry(&lob_mtr, index); |
| 369 |
|
| 370 |
while (!fil_addr_is_null(node_loc)) { |
| 371 |
flst_node_t *node = first.addr2ptr_x(node_loc); |
| 372 |
cur_entry.reset(node); |
| 373 |
|
| 374 |
flst_base_node_t *vers = cur_entry.get_versions_list(); |
| 375 |
fil_addr_t ver_loc = flst_get_first(vers, &lob_mtr); |
| 376 |
|
| 377 |
/* Scan the older versions. */ |
| 378 |
while (!fil_addr_is_null(ver_loc)) { |
| 379 |
flst_node_t *ver_node = first.addr2ptr_x(ver_loc); |
| 380 |
z_index_entry_t vers_entry(ver_node, &lob_mtr, index); |
| 381 |
|
| 382 |
if (vers_entry.can_be_purged(trxid, undo_no)) { |
| 383 |
ver_loc = vers_entry.purge_version(index, first, vers, free_list); |
| 384 |
} else { |
| 385 |
ver_loc = vers_entry.get_next(); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
node_loc = cur_entry.get_next(); |
| 390 |
cur_entry.reset(nullptr); |
| 391 |
|
| 392 |
ut_ad(!lob_mtr.conflicts_with(mtr)); |
| 393 |
mtr_commit(&lob_mtr); |
| 394 |
mtr_start(&lob_mtr); |
| 395 |
lob_mtr.set_log_mode(mtr->get_log_mode()); |
| 396 |
first.load_x(first_page_no); |
| 397 |
} |
| 398 |
|
| 399 |
ut_ad(!lob_mtr.conflicts_with(mtr)); |
| 400 |
mtr_commit(&lob_mtr); |
| 401 |
|
| 402 |
if (ctx->get_page_zip() != nullptr) { |
| 403 |
ref.set_page_no(FIL_NULL, nullptr); |
| 404 |
ref.set_length(0, nullptr); |
| 405 |
ctx->zblob_write_blobref(ctx->m_field_no, mtr); |
| 406 |
} else { |
| 407 |
/* Note that page_zip will be NULL in |
| 408 |
row_purge_upd_exist_or_extern(). */ |
| 409 |
ref.set_page_no(FIL_NULL, mtr); |
| 410 |
ref.set_length(0, mtr); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
void purge(DeleteContext *ctx, dict_index_t *index, trx_id_t trxid, |
| 415 |
undo_no_t undo_no, ulint rec_type, const upd_field_t *uf, |
| 416 |
purge_node_t *purge_node) { |
| 417 |
DBUG_TRACE; |
| 418 |
mtr_t lob_mtr; |
| 419 |
|
| 420 |
ref_t &ref = ctx->m_blobref; |
| 421 |
mtr_t *mtr = ctx->get_mtr(); |
| 422 |
|
| 423 |
#ifdef UNIV_DEBUG |
| 424 |
/* Ensure that the btr_mtr is not restarted. */ |
| 425 |
const auto restart_count = mtr->m_restart_count; |
| 426 |
auto guard = create_scope_guard( |
| 427 |
[mtr, restart_count]() { ut_ad(restart_count == mtr->m_restart_count); }); |
| 428 |
#endif /* UNIV_DEBUG */ |
| 429 |
|
| 430 |
const mtr_log_t log_mode = mtr->get_log_mode(); |
| 431 |
const bool is_rollback = ctx->m_rollback; |
| 432 |
|
| 433 |
/* Update the context object based on the persistent cursor. */ |
| 434 |
if (ctx->need_recalc()) { |
| 435 |
ctx->recalc(); |
| 436 |
} |
| 437 |
|
| 438 |
if (ref.is_null()) { |
| 439 |
/* In the rollback, we may encounter a clustered index |
| 440 |
record with some unwritten off-page columns. There is |
| 441 |
nothing to free then. */ |
| 442 |
ut_a(ctx->m_rollback); |
| 443 |
return; |
| 444 |
} |
| 445 |
|
| 446 |
/* In case ref.length()==0, the LOB might be partially deleted (for example |
| 447 |
a crash has happened during a rollback() of insert operation) and we want |
| 448 |
to make sure we delete the remaining parts of the LOB so we don't exit here. |
| 449 |
OTOH, if the reason for ref.length()==0, is because of ref.is_null_relaxed(), |
| 450 |
then we should exit.*/ |
| 451 |
if (!ref.is_owner() || ref.page_no() == FIL_NULL || ref.is_null_relaxed() || |
| 452 |
(ctx->m_rollback && ref.is_inherited())) { |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
if (!is_rollback && uf != nullptr && uf->lob_diffs != nullptr && |
| 457 |
uf->lob_diffs->size() > 0) { |
| 458 |
/* Undo record contains LOB diffs. So purge shouldn't look |
| 459 |
at the LOB. */ |
| 460 |
return; |
| 461 |
} |
| 462 |
|
| 463 |
if (!ctx->is_ref_valid()) { |
| 464 |
/* The LOB reference has changed. Don't proceed. */ |
| 465 |
return; |
| 466 |
} |
| 467 |
|
| 468 |
/* If rec_type is 0, it is not the purge operation. */ |
| 469 |
if (!is_rollback && rec_type != 0 && !ctx->is_delete_marked()) { |
| 470 |
/* This is the purge operation. The delete marked clustered record has been |
| 471 |
reused. Purge shouldn't proceed. */ |
| 472 |
return; |
| 473 |
} |
| 474 |
|
| 475 |
space_id_t space_id = ref.space_id(); |
| 476 |
ut_ad(space_id == index->space_id()); |
| 477 |
page_no_t first_page_no = ref.page_no(); |
| 478 |
page_id_t page_id(space_id, first_page_no); |
| 479 |
page_size_t page_size(dict_table_page_size(index->table)); |
| 480 |
|
| 481 |
page_type_t page_type = |
| 482 |
first_page_t::get_page_type(index, page_id, page_size); |
| 483 |
|
| 484 |
if (page_type == FIL_PAGE_TYPE_ZBLOB || |
| 485 |
page_type == FIL_PAGE_TYPE_ZBLOB2 || /* Partially purged ZBLOB */ |
| 486 |
page_type == FIL_PAGE_TYPE_BLOB || page_type == FIL_PAGE_SDI_BLOB || |
| 487 |
page_type == FIL_PAGE_SDI_ZBLOB) { |
| 488 |
lob::Deleter free_blob(*ctx); |
| 489 |
free_blob.destroy(); |
| 490 |
return; |
| 491 |
} |
| 492 |
|
| 493 |
if (page_type == FIL_PAGE_TYPE_ZLOB_FIRST) { |
| 494 |
z_purge(ctx, index, trxid, undo_no, rec_type, purge_node); |
| 495 |
return; |
| 496 |
} |
| 497 |
|
| 498 |
ut_a(page_type == FIL_PAGE_TYPE_LOB_FIRST); |
| 499 |
|
| 500 |
if (is_rollback) { |
| 501 |
rollback(ctx, index, trxid, undo_no, rec_type, uf); |
| 502 |
return; |
| 503 |
} |
| 504 |
|
| 505 |
/* Hold exclusive access to LOB */ |
| 506 |
first_page_t btr_first(mtr, index); |
| 507 |
btr_first.load_x(page_id, page_size); |
| 508 |
const trx_id_t last_trx_id = btr_first.get_last_trx_id(); |
| 509 |
const undo_no_t last_undo_no = btr_first.get_last_trx_undo_no(); |
| 510 |
|
| 511 |
/* Check if the LOB has to be destroyed. */ |
| 512 |
bool ok_to_free = (rec_type == TRX_UNDO_UPD_EXIST_REC || |
| 513 |
rec_type == TRX_UNDO_UPD_DEL_REC) && |
| 514 |
!btr_first.can_be_partially_updated() && |
| 515 |
(last_trx_id == trxid) && (last_undo_no == undo_no); |
| 516 |
|
| 517 |
if (rec_type == TRX_UNDO_DEL_MARK_REC || ok_to_free) { |
| 518 |
ut_ad(btr_first.get_page_type() == FIL_PAGE_TYPE_LOB_FIRST); |
| 519 |
|
| 520 |
if (dict_index_is_online_ddl(index)) { |
| 521 |
row_log_table_blob_free(index, ref.page_no()); |
| 522 |
} |
| 523 |
if (purge_node == nullptr) { |
| 524 |
/* During rollback, when a record has multiple blobs, freeing the first |
| 525 |
page of one blob in btr_mtr and then attempting to free the next blob |
| 526 |
in a local_mtr will cause mtr conflict between btr_mtr and local_mtr. |
| 527 |
To avoid this problem, free the first page of blobs later. */ |
| 528 |
btr_first.make_empty(); |
| 529 |
ctx->add_lob_block(btr_first.get_block()); |
| 530 |
} else { |
| 531 |
/* In this case, the LOB is left with only the first page. Subsequently |
| 532 |
the LOB first page number in the LOB reference is set to FIL_NULL. |
| 533 |
This means that the LOB page is only accessible via an in-memory |
| 534 |
reference held in the purge node. If a crash happens after the btr_mtr |
| 535 |
commit and before freeing the LOB first page, then the LOB first page |
| 536 |
will be leaked. We need to come up with a mechanism to avoid this leak.*/ |
| 537 |
btr_first.make_empty(); |
| 538 |
purge_node->add_lob_page(index, page_id); |
| 539 |
} |
| 540 |
|
| 541 |
ref.set_page_no(FIL_NULL, mtr); |
| 542 |
ref.set_length(0, mtr); |
| 543 |
|
| 544 |
return; |
| 545 |
} |
| 546 |
|
| 547 |
mtr_start(&lob_mtr); |
| 548 |
lob_mtr.set_log_mode(log_mode); |
| 549 |
|
| 550 |
/* The current entry - it is the latest version. */ |
| 551 |
index_entry_t cur_entry(&lob_mtr, index); |
| 552 |
first_page_t first(&lob_mtr, index); |
| 553 |
first.load_x(page_id, page_size); |
| 554 |
|
| 555 |
flst_base_node_t *flst = first.index_list(); |
| 556 |
flst_base_node_t *free_list = first.free_list(); |
| 557 |
fil_addr_t node_loc = flst_get_first(flst, &lob_mtr); |
| 558 |
|
| 559 |
while (!fil_addr_is_null(node_loc)) { |
| 560 |
flst_node_t *node = first.addr2ptr_x(node_loc); |
| 561 |
cur_entry.reset(node); |
| 562 |
|
| 563 |
flst_base_node_t *vers = cur_entry.get_versions_list(); |
| 564 |
fil_addr_t ver_loc = flst_get_first(vers, &lob_mtr); |
| 565 |
|
| 566 |
/* Scan the older versions. */ |
| 567 |
while (!fil_addr_is_null(ver_loc)) { |
| 568 |
flst_node_t *ver_node = first.addr2ptr_x(ver_loc); |
| 569 |
index_entry_t vers_entry(ver_node, &lob_mtr, index); |
| 570 |
|
| 571 |
if (vers_entry.can_be_purged(trxid, undo_no)) { |
| 572 |
ver_loc = vers_entry.purge_version(index, vers, free_list); |
| 573 |
} else { |
| 574 |
ver_loc = vers_entry.get_next(); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
node_loc = cur_entry.get_next(); |
| 579 |
cur_entry.reset(nullptr); |
| 580 |
|
| 581 |
/* Ensure that the parent mtr (btr_mtr) and the child mtr (lob_mtr) |
| 582 |
do not make conflicting modifications. */ |
| 583 |
ut_ad(!lob_mtr.conflicts_with(mtr)); |
| 584 |
mtr_commit(&lob_mtr); |
| 585 |
mtr_start(&lob_mtr); |
| 586 |
lob_mtr.set_log_mode(log_mode); |
| 587 |
first.load_x(page_id, page_size); |
| 588 |
} |
| 589 |
|
| 590 |
/* Ensure that the parent mtr (btr_mtr) and the child mtr (lob_mtr) |
| 591 |
do not make conflicting modifications. */ |
| 592 |
ut_ad(!lob_mtr.conflicts_with(mtr)); |
| 593 |
mtr_commit(&lob_mtr); |
| 594 |
|
| 595 |
ref.set_page_no(FIL_NULL, mtr); |
| 596 |
ref.set_length(0, mtr); |
| 597 |
} |
| 598 |
|
| 599 |
} /* namespace lob */ |