| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file trx/trx0undo.cc |
| 29 |
Transaction undo log |
| 30 |
|
| 31 |
Created 3/26/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include <stddef.h> |
| 35 |
|
| 36 |
#include <sql_thd_internal_api.h> |
| 37 |
|
| 38 |
#include "fsp0fsp.h" |
| 39 |
#include "ha_prototypes.h" |
| 40 |
#include "trx0undo.h" |
| 41 |
|
| 42 |
#include "my_dbug.h" |
| 43 |
|
| 44 |
#ifndef UNIV_HOTBACKUP |
| 45 |
#include "clone0clone.h" |
| 46 |
#include "current_thd.h" |
| 47 |
#include "dict0dd.h" |
| 48 |
#include "fil0fil.h" |
| 49 |
#include "log0chkp.h" |
| 50 |
#include "log0write.h" |
| 51 |
#include "mach0data.h" |
| 52 |
#include "mtr0log.h" |
| 53 |
#include "srv0mon.h" |
| 54 |
#include "srv0srv.h" |
| 55 |
#include "srv0start.h" |
| 56 |
#include "trx0purge.h" |
| 57 |
#include "trx0rec.h" |
| 58 |
#include "trx0rseg.h" |
| 59 |
#include "trx0trx.h" |
| 60 |
|
| 61 |
/* How should the old versions in the history list be managed? |
| 62 |
---------------------------------------------------------- |
| 63 |
If each transaction is given a whole page for its update undo log, file |
| 64 |
space consumption can be 10 times higher than necessary. Therefore, |
| 65 |
partly filled update undo log pages should be reusable. But then there |
| 66 |
is no way individual pages can be ordered so that the ordering agrees |
| 67 |
with the serialization numbers of the transactions on the pages. Thus, |
| 68 |
the history list must be formed of undo logs, not their header pages as |
| 69 |
it was in the old implementation. |
| 70 |
However, on a single header page the transactions are placed in |
| 71 |
the order of their serialization numbers. As old versions are purged, we |
| 72 |
may free the page when the last transaction on the page has been purged. |
| 73 |
A problem is that the purge has to go through the transactions |
| 74 |
in the serialization order. This means that we have to look through all |
| 75 |
rollback segments for the one that has the smallest transaction number |
| 76 |
in its history list. |
| 77 |
When should we do a purge? A purge is necessary when space is |
| 78 |
running out in any of the rollback segments. Then we may have to purge |
| 79 |
also old version which might be needed by some consistent read. How do |
| 80 |
we trigger the start of a purge? When a transaction writes to an undo log, |
| 81 |
it may notice that the space is running out. When a read view is closed, |
| 82 |
it may make some history superfluous. The server can have an utility which |
| 83 |
periodically checks if it can purge some history. |
| 84 |
In a parallelized purge we have the problem that a query thread |
| 85 |
can remove a delete marked clustered index record before another query |
| 86 |
thread has processed an earlier version of the record, which cannot then |
| 87 |
be done because the row cannot be constructed from the clustered index |
| 88 |
record. To avoid this problem, we will store in the update and delete mark |
| 89 |
undo record also the columns necessary to construct the secondary index |
| 90 |
entries which are modified. |
| 91 |
We can latch the stack of versions of a single clustered index record |
| 92 |
by taking a latch on the clustered index page. As long as the latch is held, |
| 93 |
no new versions can be added and no versions removed by undo. But, a purge |
| 94 |
can still remove old versions from the bottom of the stack. */ |
| 95 |
|
| 96 |
/* How to protect rollback segments, undo logs, and history lists with |
| 97 |
------------------------------------------------------------------- |
| 98 |
latches? |
| 99 |
------- |
| 100 |
The contention of the trx_sys_t::mutex should be minimized. When a transaction |
| 101 |
does its first insert or modify in an index, an undo log is assigned for it. |
| 102 |
Then we must have an x-latch to the rollback segment header. |
| 103 |
When the transaction does more modifications or rolls back, the undo log |
| 104 |
is protected with undo_mutex in the transaction. |
| 105 |
When the transaction commits, its insert undo log is either reset and |
| 106 |
cached for a fast reuse, or freed. In these cases we must have an x-latch on |
| 107 |
the rollback segment page. The update undo log is put to the history list. If |
| 108 |
it is not suitable for reuse, its slot in the rollback segment is reset. In |
| 109 |
both cases, an x-latch must be acquired on the rollback segment. |
| 110 |
The purge operation steps through the history list without modifying |
| 111 |
it until a truncate operation occurs, which can remove undo logs from the end |
| 112 |
of the list and release undo log segments. In stepping through the list, |
| 113 |
s-latches on the undo log pages are enough, but in a truncate, x-latches must |
| 114 |
be obtained on the rollback segment and individual pages. */ |
| 115 |
#endif /* !UNIV_HOTBACKUP */ |
| 116 |
|
| 117 |
/** Initializes the fields in an undo log segment page. */ |
| 118 |
static void trx_undo_page_init( |
| 119 |
page_t *undo_page, /*!< in: undo log segment page */ |
| 120 |
ulint type, /*!< in: undo log segment type */ |
| 121 |
mtr_t *mtr); /*!< in: mtr */ |
| 122 |
|
| 123 |
#ifndef UNIV_HOTBACKUP |
| 124 |
/** Creates and initializes an undo log memory object. |
| 125 |
@param[in] rseg rollback segment memory object |
| 126 |
@param[in] id slot index within rseg |
| 127 |
@param[in] type type of the log: TRX_UNDO_INSERT or TRX_UNDO_UPDATE |
| 128 |
@param[in] trx_id id of the trx for which the undo log is created |
| 129 |
@param[in] xid X/Open XA transaction identification |
| 130 |
@param[in] page_no undo log header page number |
| 131 |
@param[in] offset undo log header byte offset on page |
| 132 |
@return own: the undo log memory object */ |
| 133 |
static trx_undo_t *trx_undo_mem_create(trx_rseg_t *rseg, ulint id, ulint type, |
| 134 |
trx_id_t trx_id, const XID *xid, |
| 135 |
page_no_t page_no, ulint offset); |
| 136 |
#endif /* !UNIV_HOTBACKUP */ |
| 137 |
/** Initializes a cached insert undo log header page for new use. NOTE that this |
| 138 |
function has its own log record type MLOG_UNDO_HDR_REUSE. You must NOT change |
| 139 |
the operation of this function! |
| 140 |
@return undo log header byte offset on page */ |
| 141 |
static ulint trx_undo_insert_header_reuse( |
| 142 |
page_t *undo_page, /*!< in/out: insert undo log segment |
| 143 |
header page, x-latched */ |
| 144 |
trx_id_t trx_id, /*!< in: transaction id */ |
| 145 |
mtr_t *mtr); /*!< in: mtr */ |
| 146 |
|
| 147 |
#ifndef UNIV_HOTBACKUP |
| 148 |
/** Gets the previous record in an undo log from the previous page. |
| 149 |
@return undo log record, the page s-latched, NULL if none */ |
| 150 |
static trx_undo_rec_t *trx_undo_get_prev_rec_from_prev_page( |
| 151 |
trx_undo_rec_t *rec, /*!< in: undo record */ |
| 152 |
page_no_t page_no, /*!< in: undo log header page number */ |
| 153 |
ulint offset, /*!< in: undo log header offset on page */ |
| 154 |
bool shared, /*!< in: true=S-latch, false=X-latch */ |
| 155 |
mtr_t *mtr) /*!< in: mtr */ |
| 156 |
{ |
| 157 |
space_id_t space; |
| 158 |
page_no_t prev_page_no; |
| 159 |
page_t *prev_page; |
| 160 |
page_t *undo_page; |
| 161 |
|
| 162 |
undo_page = page_align(rec); |
| 163 |
|
| 164 |
prev_page_no = flst_get_prev_addr( |
| 165 |
undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_NODE, mtr) |
| 166 |
.page; |
| 167 |
|
| 168 |
if (prev_page_no == FIL_NULL) { |
| 169 |
return (nullptr); |
| 170 |
} |
| 171 |
|
| 172 |
space = page_get_space_id(undo_page); |
| 173 |
|
| 174 |
bool found; |
| 175 |
const page_size_t &page_size = fil_space_get_page_size(space, &found); |
| 176 |
|
| 177 |
ut_ad(found); |
| 178 |
|
| 179 |
buf_block_t *block = |
| 180 |
buf_page_get(page_id_t(space, prev_page_no), page_size, |
| 181 |
shared ? RW_S_LATCH : RW_X_LATCH, UT_LOCATION_HERE, mtr); |
| 182 |
|
| 183 |
buf_block_dbg_add_level(block, SYNC_TRX_UNDO_PAGE); |
| 184 |
|
| 185 |
prev_page = buf_block_get_frame(block); |
| 186 |
|
| 187 |
return (trx_undo_page_get_last_rec(prev_page, page_no, offset)); |
| 188 |
} |
| 189 |
|
| 190 |
/** Gets the previous record in an undo log. |
| 191 |
@return undo log record, the page s-latched, NULL if none */ |
| 192 |
trx_undo_rec_t *trx_undo_get_prev_rec( |
| 193 |
trx_undo_rec_t *rec, /*!< in: undo record */ |
| 194 |
page_no_t page_no, /*!< in: undo log header page number */ |
| 195 |
ulint offset, /*!< in: undo log header offset on page */ |
| 196 |
bool shared, /*!< in: true=S-latch, false=X-latch */ |
| 197 |
mtr_t *mtr) /*!< in: mtr */ |
| 198 |
{ |
| 199 |
trx_undo_rec_t *prev_rec; |
| 200 |
|
| 201 |
prev_rec = trx_undo_page_get_prev_rec(rec, page_no, offset); |
| 202 |
|
| 203 |
if (prev_rec) { |
| 204 |
return (prev_rec); |
| 205 |
} |
| 206 |
|
| 207 |
/* We have to go to the previous undo log page to look for the |
| 208 |
previous record */ |
| 209 |
|
| 210 |
return ( |
| 211 |
trx_undo_get_prev_rec_from_prev_page(rec, page_no, offset, shared, mtr)); |
| 212 |
} |
| 213 |
|
| 214 |
/** Gets the next record in an undo log from the next page. |
| 215 |
@param[in] space Undo log header space |
| 216 |
@param[in] page_size Page size |
| 217 |
@param[in] undo_page Undo log page |
| 218 |
@param[in] page_no Undo log header page number |
| 219 |
@param[in] offset Undo log header offset on page |
| 220 |
@param[in] mode Latch mode: RW_S_LATCH or RW_X_LATCH |
| 221 |
@param[in,out] mtr Mini-transaction |
| 222 |
@return undo log record, the page latched, NULL if none */ |
| 223 |
static trx_undo_rec_t *trx_undo_get_next_rec_from_next_page( |
| 224 |
space_id_t space, const page_size_t &page_size, const page_t *undo_page, |
| 225 |
page_no_t page_no, ulint offset, ulint mode, mtr_t *mtr) { |
| 226 |
const trx_ulogf_t *log_hdr; |
| 227 |
page_no_t next_page_no; |
| 228 |
page_t *next_page; |
| 229 |
ulint next; |
| 230 |
|
| 231 |
if (page_no == page_get_page_no(undo_page)) { |
| 232 |
log_hdr = undo_page + offset; |
| 233 |
next = mach_read_from_2(log_hdr + TRX_UNDO_NEXT_LOG); |
| 234 |
|
| 235 |
if (next != 0) { |
| 236 |
return (nullptr); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
next_page_no = flst_get_next_addr( |
| 241 |
undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_NODE, mtr) |
| 242 |
.page; |
| 243 |
if (next_page_no == FIL_NULL) { |
| 244 |
return (nullptr); |
| 245 |
} |
| 246 |
|
| 247 |
const page_id_t next_page_id(space, next_page_no); |
| 248 |
|
| 249 |
if (mode == RW_S_LATCH) { |
| 250 |
next_page = trx_undo_page_get_s_latched(next_page_id, page_size, mtr); |
| 251 |
} else { |
| 252 |
ut_ad(mode == RW_X_LATCH); |
| 253 |
next_page = trx_undo_page_get(next_page_id, page_size, mtr); |
| 254 |
} |
| 255 |
|
| 256 |
return (trx_undo_page_get_first_rec(next_page, page_no, offset)); |
| 257 |
} |
| 258 |
|
| 259 |
/** Gets the next record in an undo log. |
| 260 |
@return undo log record, the page s-latched, NULL if none */ |
| 261 |
trx_undo_rec_t *trx_undo_get_next_rec( |
| 262 |
trx_undo_rec_t *rec, /*!< in: undo record */ |
| 263 |
page_no_t page_no, /*!< in: undo log header page number */ |
| 264 |
ulint offset, /*!< in: undo log header offset on page */ |
| 265 |
mtr_t *mtr) /*!< in: mtr */ |
| 266 |
{ |
| 267 |
space_id_t space; |
| 268 |
trx_undo_rec_t *next_rec; |
| 269 |
|
| 270 |
next_rec = trx_undo_page_get_next_rec(rec, page_no, offset); |
| 271 |
|
| 272 |
if (next_rec) { |
| 273 |
return (next_rec); |
| 274 |
} |
| 275 |
|
| 276 |
space = page_get_space_id(page_align(rec)); |
| 277 |
|
| 278 |
bool found; |
| 279 |
const page_size_t &page_size = fil_space_get_page_size(space, &found); |
| 280 |
|
| 281 |
ut_ad(found); |
| 282 |
|
| 283 |
return (trx_undo_get_next_rec_from_next_page( |
| 284 |
space, page_size, page_align(rec), page_no, offset, RW_S_LATCH, mtr)); |
| 285 |
} |
| 286 |
|
| 287 |
/** Gets the first record in an undo log. |
| 288 |
@param[out] modifier_trx_id The modifier trx identifier. |
| 289 |
@param[in] space Undo log header space |
| 290 |
@param[in] page_size Page size |
| 291 |
@param[in] page_no Undo log header page number |
| 292 |
@param[in] offset Undo log header offset on page |
| 293 |
@param[in] mode Latching mode: RW_S_LATCH or RW_X_LATCH |
| 294 |
@param[in,out] mtr Mini-transaction |
| 295 |
@return undo log record, the page latched, NULL if none */ |
| 296 |
trx_undo_rec_t *trx_undo_get_first_rec(trx_id_t *modifier_trx_id, |
| 297 |
space_id_t space, |
| 298 |
const page_size_t &page_size, |
| 299 |
page_no_t page_no, ulint offset, |
| 300 |
ulint mode, mtr_t *mtr) { |
| 301 |
page_t *undo_page; |
| 302 |
trx_undo_rec_t *rec; |
| 303 |
|
| 304 |
const page_id_t page_id(space, page_no); |
| 305 |
|
| 306 |
if (mode == RW_S_LATCH) { |
| 307 |
undo_page = trx_undo_page_get_s_latched(page_id, page_size, mtr); |
| 308 |
} else { |
| 309 |
undo_page = trx_undo_page_get(page_id, page_size, mtr); |
| 310 |
} |
| 311 |
|
| 312 |
if (modifier_trx_id != nullptr) { |
| 313 |
trx_ulogf_t *undo_header = undo_page + offset; |
| 314 |
*modifier_trx_id = mach_read_from_8(undo_header + TRX_UNDO_TRX_ID); |
| 315 |
} |
| 316 |
|
| 317 |
rec = trx_undo_page_get_first_rec(undo_page, page_no, offset); |
| 318 |
|
| 319 |
if (rec) { |
| 320 |
return (rec); |
| 321 |
} |
| 322 |
|
| 323 |
return (trx_undo_get_next_rec_from_next_page(space, page_size, undo_page, |
| 324 |
page_no, offset, mode, mtr)); |
| 325 |
} |
| 326 |
|
| 327 |
/*============== UNDO LOG FILE COPY CREATION AND FREEING ==================*/ |
| 328 |
|
| 329 |
/** Writes the mtr log entry of an undo log page initialization. */ |
| 330 |
static inline void trx_undo_page_init_log( |
| 331 |
page_t *undo_page, /*!< in: undo log page */ |
| 332 |
ulint type, /*!< in: undo log type */ |
| 333 |
mtr_t *mtr) /*!< in: mtr */ |
| 334 |
{ |
| 335 |
mlog_write_initial_log_record(undo_page, MLOG_UNDO_INIT, mtr); |
| 336 |
|
| 337 |
mlog_catenate_ulint_compressed(mtr, type); |
| 338 |
} |
| 339 |
#else /* !UNIV_HOTBACKUP */ |
| 340 |
#define trx_undo_page_init_log(undo_page, type, mtr) ((void)0) |
| 341 |
#endif /* !UNIV_HOTBACKUP */ |
| 342 |
|
| 343 |
/** Parses the redo log entry of an undo log page initialization. |
| 344 |
@return end of log record or NULL */ |
| 345 |
byte *trx_undo_parse_page_init(const byte *ptr, /*!< in: buffer */ |
| 346 |
const byte *end_ptr, /*!< in: buffer end */ |
| 347 |
page_t *page, /*!< in: page or NULL */ |
| 348 |
mtr_t *mtr) /*!< in: mtr or NULL */ |
| 349 |
{ |
| 350 |
ulint type; |
| 351 |
|
| 352 |
type = mach_parse_compressed(&ptr, end_ptr); |
| 353 |
|
| 354 |
if (ptr == nullptr) { |
| 355 |
return (nullptr); |
| 356 |
} |
| 357 |
|
| 358 |
if (page) { |
| 359 |
trx_undo_page_init(page, type, mtr); |
| 360 |
} |
| 361 |
|
| 362 |
return (const_cast<byte *>(ptr)); |
| 363 |
} |
| 364 |
|
| 365 |
/** Initializes the fields in an undo log segment page. */ |
| 366 |
static void trx_undo_page_init( |
| 367 |
page_t *undo_page, /*!< in: undo log segment page */ |
| 368 |
ulint type, /*!< in: undo log segment type */ |
| 369 |
mtr_t *mtr) /*!< in: mtr */ |
| 370 |
{ |
| 371 |
trx_upagef_t *page_hdr; |
| 372 |
|
| 373 |
page_hdr = undo_page + TRX_UNDO_PAGE_HDR; |
| 374 |
|
| 375 |
mach_write_to_2(page_hdr + TRX_UNDO_PAGE_TYPE, type); |
| 376 |
|
| 377 |
mach_write_to_2(page_hdr + TRX_UNDO_PAGE_START, |
| 378 |
TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_HDR_SIZE); |
| 379 |
mach_write_to_2(page_hdr + TRX_UNDO_PAGE_FREE, |
| 380 |
TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_HDR_SIZE); |
| 381 |
|
| 382 |
fil_page_set_type(undo_page, FIL_PAGE_UNDO_LOG); |
| 383 |
|
| 384 |
trx_undo_page_init_log(undo_page, type, mtr); |
| 385 |
} |
| 386 |
|
| 387 |
#ifndef UNIV_HOTBACKUP |
| 388 |
/** Creates a new undo log segment in file. |
| 389 |
@return DB_SUCCESS if page creation OK possible error codes are: |
| 390 |
DB_TOO_MANY_CONCURRENT_TRXS DB_OUT_OF_FILE_SPACE */ |
| 391 |
[[nodiscard]] static dberr_t trx_undo_seg_create( |
| 392 |
trx_rseg_t *rseg [[maybe_unused]], /*!< in: rollback segment */ |
| 393 |
trx_rsegf_t *rseg_hdr, /*!< in: rollback segment header, |
| 394 |
page x-latched */ |
| 395 |
ulint type, /*!< in: type of the segment: |
| 396 |
TRX_UNDO_INSERT or TRX_UNDO_UPDATE */ |
| 397 |
ulint *id, /*!< out: slot index within rseg header */ |
| 398 |
page_t **undo_page, |
| 399 |
/*!< out: segment header page x-latched, NULL |
| 400 |
if there was an error */ |
| 401 |
mtr_t *mtr) /*!< in: mtr */ |
| 402 |
{ |
| 403 |
ulint slot_no = ULINT_UNDEFINED; |
| 404 |
space_id_t space; |
| 405 |
buf_block_t *block; |
| 406 |
trx_upagef_t *page_hdr; |
| 407 |
trx_usegf_t *seg_hdr; |
| 408 |
ulint n_reserved; |
| 409 |
bool success; |
| 410 |
dberr_t err = DB_SUCCESS; |
| 411 |
|
| 412 |
ut_ad(mtr != nullptr); |
| 413 |
ut_ad(id != nullptr); |
| 414 |
ut_ad(rseg_hdr != nullptr); |
| 415 |
ut_ad(mutex_own(&(rseg->mutex))); |
| 416 |
|
| 417 |
#ifdef UNIV_DEBUG |
| 418 |
if (!srv_inject_too_many_concurrent_trxs) |
| 419 |
#endif |
| 420 |
{ |
| 421 |
slot_no = trx_rsegf_undo_find_free(rseg_hdr, mtr); |
| 422 |
} |
| 423 |
if (slot_no == ULINT_UNDEFINED) { |
| 424 |
ib::error(ER_IB_MSG_1212) |
| 425 |
<< "Cannot find a free slot for an undo log." |
| 426 |
" You may have too many active transactions running concurrently." |
| 427 |
" Please add more rollback segments or undo tablespaces."; |
| 428 |
|
| 429 |
return (DB_TOO_MANY_CONCURRENT_TRXS); |
| 430 |
} |
| 431 |
|
| 432 |
space = page_get_space_id(page_align(rseg_hdr)); |
| 433 |
|
| 434 |
success = fsp_reserve_free_extents(&n_reserved, space, 2, FSP_UNDO, mtr); |
| 435 |
if (!success) { |
| 436 |
return (DB_OUT_OF_FILE_SPACE); |
| 437 |
} |
| 438 |
|
| 439 |
/* Allocate a new file segment for the undo log */ |
| 440 |
block = fseg_create_general(space, 0, TRX_UNDO_SEG_HDR + TRX_UNDO_FSEG_HEADER, |
| 441 |
true, mtr); |
| 442 |
|
| 443 |
fil_space_release_free_extents(space, n_reserved); |
| 444 |
|
| 445 |
if (block == nullptr) { |
| 446 |
/* No space left */ |
| 447 |
|
| 448 |
return (DB_OUT_OF_FILE_SPACE); |
| 449 |
} |
| 450 |
|
| 451 |
buf_block_dbg_add_level(block, SYNC_TRX_UNDO_PAGE); |
| 452 |
|
| 453 |
*undo_page = buf_block_get_frame(block); |
| 454 |
|
| 455 |
page_hdr = *undo_page + TRX_UNDO_PAGE_HDR; |
| 456 |
seg_hdr = *undo_page + TRX_UNDO_SEG_HDR; |
| 457 |
|
| 458 |
trx_undo_page_init(*undo_page, type, mtr); |
| 459 |
|
| 460 |
mlog_write_ulint(page_hdr + TRX_UNDO_PAGE_FREE, |
| 461 |
TRX_UNDO_SEG_HDR + TRX_UNDO_SEG_HDR_SIZE, MLOG_2BYTES, mtr); |
| 462 |
|
| 463 |
mlog_write_ulint(seg_hdr + TRX_UNDO_LAST_LOG, 0, MLOG_2BYTES, mtr); |
| 464 |
|
| 465 |
flst_init(seg_hdr + TRX_UNDO_PAGE_LIST, mtr); |
| 466 |
|
| 467 |
flst_add_last(seg_hdr + TRX_UNDO_PAGE_LIST, page_hdr + TRX_UNDO_PAGE_NODE, |
| 468 |
mtr); |
| 469 |
|
| 470 |
trx_rsegf_set_nth_undo(rseg_hdr, slot_no, page_get_page_no(*undo_page), mtr); |
| 471 |
*id = slot_no; |
| 472 |
|
| 473 |
MONITOR_INC(MONITOR_NUM_UNDO_SLOT_USED); |
| 474 |
|
| 475 |
return (err); |
| 476 |
} |
| 477 |
|
| 478 |
/** Writes the mtr log entry of an undo log header initialization. */ |
| 479 |
static inline void trx_undo_header_create_log( |
| 480 |
const page_t *undo_page, /*!< in: undo log header page */ |
| 481 |
trx_id_t trx_id, /*!< in: transaction id */ |
| 482 |
mtr_t *mtr) /*!< in: mtr */ |
| 483 |
{ |
| 484 |
mlog_write_initial_log_record(undo_page, MLOG_UNDO_HDR_CREATE, mtr); |
| 485 |
|
| 486 |
mlog_catenate_ull_compressed(mtr, trx_id); |
| 487 |
} |
| 488 |
#else /* !UNIV_HOTBACKUP */ |
| 489 |
#define trx_undo_header_create_log(undo_page, trx_id, mtr) ((void)0) |
| 490 |
#endif /* !UNIV_HOTBACKUP */ |
| 491 |
|
| 492 |
/** Creates a new undo log header in file. NOTE that this function has its own |
| 493 |
log record type MLOG_UNDO_HDR_CREATE. You must NOT change the operation of |
| 494 |
this function! |
| 495 |
@return header byte offset on page */ |
| 496 |
static ulint trx_undo_header_create( |
| 497 |
page_t *undo_page, /*!< in/out: undo log segment |
| 498 |
header page, x-latched; it is |
| 499 |
assumed that there is |
| 500 |
TRX_UNDO_LOG_HDR_SIZE bytes |
| 501 |
free space on it */ |
| 502 |
trx_id_t trx_id, /*!< in: transaction id */ |
| 503 |
mtr_t *mtr) /*!< in: mtr */ |
| 504 |
{ |
| 505 |
trx_upagef_t *page_hdr; |
| 506 |
trx_usegf_t *seg_hdr; |
| 507 |
trx_ulogf_t *log_hdr; |
| 508 |
ulint prev_log; |
| 509 |
ulint free; |
| 510 |
ulint new_free; |
| 511 |
|
| 512 |
ut_ad(mtr && undo_page); |
| 513 |
|
| 514 |
page_hdr = undo_page + TRX_UNDO_PAGE_HDR; |
| 515 |
seg_hdr = undo_page + TRX_UNDO_SEG_HDR; |
| 516 |
|
| 517 |
free = mach_read_from_2(page_hdr + TRX_UNDO_PAGE_FREE); |
| 518 |
|
| 519 |
log_hdr = undo_page + free; |
| 520 |
|
| 521 |
new_free = free + TRX_UNDO_LOG_OLD_HDR_SIZE; |
| 522 |
|
| 523 |
ut_a(free + TRX_UNDO_LOG_GTID_XA_HDR_SIZE < UNIV_PAGE_SIZE - 100); |
| 524 |
|
| 525 |
mach_write_to_2(page_hdr + TRX_UNDO_PAGE_START, new_free); |
| 526 |
|
| 527 |
mach_write_to_2(page_hdr + TRX_UNDO_PAGE_FREE, new_free); |
| 528 |
|
| 529 |
mach_write_to_2(seg_hdr + TRX_UNDO_STATE, TRX_UNDO_ACTIVE); |
| 530 |
|
| 531 |
prev_log = mach_read_from_2(seg_hdr + TRX_UNDO_LAST_LOG); |
| 532 |
|
| 533 |
if (prev_log != 0) { |
| 534 |
trx_ulogf_t *prev_log_hdr; |
| 535 |
|
| 536 |
prev_log_hdr = undo_page + prev_log; |
| 537 |
|
| 538 |
mach_write_to_2(prev_log_hdr + TRX_UNDO_NEXT_LOG, free); |
| 539 |
} |
| 540 |
|
| 541 |
mach_write_to_2(seg_hdr + TRX_UNDO_LAST_LOG, free); |
| 542 |
|
| 543 |
log_hdr = undo_page + free; |
| 544 |
|
| 545 |
mach_write_to_2(log_hdr + TRX_UNDO_DEL_MARKS, true); |
| 546 |
|
| 547 |
mach_write_to_8(log_hdr + TRX_UNDO_TRX_ID, trx_id); |
| 548 |
mach_write_to_2(log_hdr + TRX_UNDO_LOG_START, new_free); |
| 549 |
|
| 550 |
mach_write_to_1(log_hdr + TRX_UNDO_FLAGS, 0); |
| 551 |
mach_write_to_1(log_hdr + TRX_UNDO_DICT_TRANS, false); |
| 552 |
|
| 553 |
mach_write_to_2(log_hdr + TRX_UNDO_NEXT_LOG, 0); |
| 554 |
mach_write_to_2(log_hdr + TRX_UNDO_PREV_LOG, prev_log); |
| 555 |
|
| 556 |
/* Write the log record about the header creation */ |
| 557 |
trx_undo_header_create_log(undo_page, trx_id, mtr); |
| 558 |
|
| 559 |
return (free); |
| 560 |
} |
| 561 |
|
| 562 |
#ifndef UNIV_HOTBACKUP |
| 563 |
/** Write X/Open XA Transaction Identification (XID) to undo log header */ |
| 564 |
static void trx_undo_write_xid( |
| 565 |
trx_ulogf_t *log_hdr, /*!< in: undo log header */ |
| 566 |
const XID *xid, /*!< in: X/Open XA Transaction Identification */ |
| 567 |
mtr_t *mtr) /*!< in: mtr */ |
| 568 |
{ |
| 569 |
mlog_write_ulint(log_hdr + TRX_UNDO_XA_FORMAT, |
| 570 |
static_cast<ulint>(xid->get_format_id()), MLOG_4BYTES, mtr); |
| 571 |
|
| 572 |
mlog_write_ulint(log_hdr + TRX_UNDO_XA_TRID_LEN, |
| 573 |
static_cast<ulint>(xid->get_gtrid_length()), MLOG_4BYTES, |
| 574 |
mtr); |
| 575 |
|
| 576 |
mlog_write_ulint(log_hdr + TRX_UNDO_XA_BQUAL_LEN, |
| 577 |
static_cast<ulint>(xid->get_bqual_length()), MLOG_4BYTES, |
| 578 |
mtr); |
| 579 |
|
| 580 |
mlog_write_string(log_hdr + TRX_UNDO_XA_XID, |
| 581 |
reinterpret_cast<const byte *>(xid->get_data()), |
| 582 |
XIDDATASIZE, mtr); |
| 583 |
} |
| 584 |
|
| 585 |
dberr_t trx_undo_gtid_add_update_undo(trx_t *trx, bool prepare, bool rollback) { |
| 586 |
ut_ad(!(prepare && rollback)); |
| 587 |
/* Check if GTID persistence is needed. */ |
| 588 |
auto >id_persistor = clone_sys->get_gtid_persistor(); |
| 589 |
|
| 590 |
/* Caller could request GTID persistence explicitly for non-Innodb operations |
| 591 |
using an empty transaction. We also allocate undo for such cases. */ |
| 592 |
bool gtid_explicit = false; |
| 593 |
|
| 594 |
bool alloc = |
| 595 |
gtid_persistor.trx_check_set(trx, prepare, rollback, gtid_explicit); |
| 596 |
|
| 597 |
auto undo_ptr = &trx->rsegs.m_redo; |
| 598 |
|
| 599 |
/* If update undo is already allocated, nothing to do. */ |
| 600 |
if (!alloc || undo_ptr->is_update()) { |
| 601 |
return (DB_SUCCESS); |
| 602 |
} |
| 603 |
|
| 604 |
/* For GTID persistence we need update undo segment. Allocate update |
| 605 |
undo segment here if it is insert only transaction. If no undo segment |
| 606 |
is allocated yet, then transaction didn't do any modification and |
| 607 |
no GTID would be allotted to it. One exception is the explicit GTID |
| 608 |
request. */ |
| 609 |
dberr_t db_err = DB_SUCCESS; |
| 610 |
|
| 611 |
if (undo_ptr->is_insert_only() || gtid_explicit) { |
| 612 |
mutex_enter(&trx->undo_mutex); |
| 613 |
db_err = trx_undo_assign_undo(trx, undo_ptr, TRX_UNDO_UPDATE); |
| 614 |
mutex_exit(&trx->undo_mutex); |
| 615 |
} |
| 616 |
/* In rare cases we might find no available update undo segment for insert |
| 617 |
only transactions. It is still fine to return error at prepare stage. |
| 618 |
Cannot do it earlier as GTID information is not known before. Keep the |
| 619 |
debug assert to know if it really happens ever. */ |
| 620 |
if (db_err != DB_SUCCESS) { |
| 621 |
trx->persists_gtid = false; |
| 622 |
ib::error(ER_IB_CLONE_GTID_PERSIST) |
| 623 |
<< "Could not allocate undo segment" |
| 624 |
<< " slot for persisting GTID. DB Error: " << db_err; |
| 625 |
ut_d(ut_error); |
| 626 |
} |
| 627 |
return (db_err); |
| 628 |
} |
| 629 |
|
| 630 |
bool trx_undo_t::gtid_allocated(bool is_prepare) const { |
| 631 |
if (is_prepare) { |
| 632 |
return m_gtid_storage == Gtid_storage::PREPARE_AND_COMMIT; |
| 633 |
} |
| 634 |
return m_gtid_storage == Gtid_storage::COMMIT || |
| 635 |
m_gtid_storage == Gtid_storage::PREPARE_AND_COMMIT; |
| 636 |
} |
| 637 |
|
| 638 |
std::tuple<int, size_t> trx_undo_t::gtid_get_details(bool is_prepare) const { |
| 639 |
int flag = is_prepare ? TRX_UNDO_FLAG_XA_PREPARE_GTID : TRX_UNDO_FLAG_GTID; |
| 640 |
size_t size = is_prepare ? TRX_UNDO_LOG_GTID_XA : TRX_UNDO_LOG_GTID; |
| 641 |
|
| 642 |
return std::make_tuple(flag, size); |
| 643 |
} |
| 644 |
|
| 645 |
void trx_undo_gtid_set(trx_t *trx, trx_undo_t *undo, bool is_xa_prepare) { |
| 646 |
int gtid_flag; |
| 647 |
/* For XA prepare we store the GTID separately. */ |
| 648 |
std::tie(gtid_flag, std::ignore) = undo->gtid_get_details(is_xa_prepare); |
| 649 |
|
| 650 |
/* Reset GTID flag */ |
| 651 |
undo->flag &= ~gtid_flag; |
| 652 |
|
| 653 |
if (!trx->persists_gtid) { |
| 654 |
return; |
| 655 |
} |
| 656 |
|
| 657 |
/* Verify that we have allocated for GTID */ |
| 658 |
if (!undo->gtid_allocated(is_xa_prepare)) { |
| 659 |
ib::error(ER_IB_CLONE_GTID_PERSIST) |
| 660 |
<< "Could not persist GTID as space for GTID is not allocated."; |
| 661 |
ut_d(ut_error); |
| 662 |
ut_o(return); |
| 663 |
} |
| 664 |
undo->flag |= gtid_flag; |
| 665 |
} |
| 666 |
|
| 667 |
void trx_undo_gtid_read_and_persist(trx_ulogf_t *undo_header) { |
| 668 |
/* Check if undo log has GTID. */ |
| 669 |
auto flag = mach_read_ulint(undo_header + TRX_UNDO_FLAGS, MLOG_1BYTE); |
| 670 |
|
| 671 |
/* Extract and add GTID information of the transaction to the persister. */ |
| 672 |
Gtid_desc gtid_desc; |
| 673 |
|
| 674 |
/* Get GTID persister */ |
| 675 |
auto >id_persistor = clone_sys->get_gtid_persistor(); |
| 676 |
|
| 677 |
/* Extract and add XA prepare GTID, if there and if and only if the |
| 678 |
transaction is in PREPARED_IN_TC state, otherwise, there is no assurance |
| 679 |
that the transaction will be kept in prepared state. */ |
| 680 |
if ((flag & TRX_UNDO_FLAG_XA_PREPARE_GTID) != 0) { |
| 681 |
/* Get GTID format version. */ |
| 682 |
gtid_desc.m_version = static_cast<uint32_t>( |
| 683 |
mach_read_from_1(undo_header + TRX_UNDO_LOG_GTID_VERSION)); |
| 684 |
|
| 685 |
/* Get GTID information string. */ |
| 686 |
memcpy(>id_desc.m_info[0], undo_header + TRX_UNDO_LOG_GTID_XA, |
| 687 |
TRX_UNDO_LOG_GTID_LEN); |
| 688 |
/* Mark GTID valid. */ |
| 689 |
gtid_desc.m_is_set = true; |
| 690 |
|
| 691 |
/* No concurrency is involved during recovery but satisfy |
| 692 |
the interface requirement. */ |
| 693 |
trx_sys_serialisation_mutex_enter(); |
| 694 |
gtid_persistor.add(gtid_desc); |
| 695 |
trx_sys_serialisation_mutex_exit(); |
| 696 |
} |
| 697 |
|
| 698 |
if ((flag & TRX_UNDO_FLAG_GTID) == 0) { |
| 699 |
return; |
| 700 |
} |
| 701 |
|
| 702 |
/* Get GTID format version. */ |
| 703 |
gtid_desc.m_version = static_cast<uint32_t>( |
| 704 |
mach_read_from_1(undo_header + TRX_UNDO_LOG_GTID_VERSION)); |
| 705 |
|
| 706 |
/* Get GTID information string. */ |
| 707 |
memcpy(>id_desc.m_info[0], undo_header + TRX_UNDO_LOG_GTID, |
| 708 |
TRX_UNDO_LOG_GTID_LEN); |
| 709 |
/* Mark GTID valid. */ |
| 710 |
gtid_desc.m_is_set = true; |
| 711 |
|
| 712 |
/* No concurrency is involved during recovery but satisfy |
| 713 |
the interface requirement. */ |
| 714 |
trx_sys_serialisation_mutex_enter(); |
| 715 |
gtid_persistor.add(gtid_desc); |
| 716 |
trx_sys_serialisation_mutex_exit(); |
| 717 |
} |
| 718 |
|
| 719 |
void trx_undo_gtid_write(trx_t *trx, trx_ulogf_t *undo_header, trx_undo_t *undo, |
| 720 |
mtr_t *mtr, bool is_xa_prepare) { |
| 721 |
int gtid_flag; |
| 722 |
size_t gtid_offset; |
| 723 |
|
| 724 |
std::tie(gtid_flag, gtid_offset) = undo->gtid_get_details(is_xa_prepare); |
| 725 |
|
| 726 |
if ((undo->flag & gtid_flag) == 0) { |
| 727 |
return; |
| 728 |
} |
| 729 |
|
| 730 |
/* Reset GTID flag */ |
| 731 |
undo->flag &= ~gtid_flag; |
| 732 |
|
| 733 |
/* We must have allocated for GTID but add a safe check. */ |
| 734 |
if (!undo->gtid_allocated(is_xa_prepare)) { |
| 735 |
ut_d(ut_error); |
| 736 |
ut_o(return); |
| 737 |
} |
| 738 |
|
| 739 |
Gtid_desc gtid_desc; |
| 740 |
auto >id_persistor = clone_sys->get_gtid_persistor(); |
| 741 |
|
| 742 |
gtid_persistor.get_gtid_info(trx, gtid_desc); |
| 743 |
|
| 744 |
if (gtid_desc.m_is_set) { |
| 745 |
/* Persist GTID version */ |
| 746 |
mlog_write_ulint(undo_header + TRX_UNDO_LOG_GTID_VERSION, |
| 747 |
gtid_desc.m_version, MLOG_1BYTE, mtr); |
| 748 |
/* Persist fixed length GTID */ |
| 749 |
static_assert(TRX_UNDO_LOG_GTID_LEN == GTID_INFO_SIZE); |
| 750 |
mlog_write_string(undo_header + gtid_offset, >id_desc.m_info[0], |
| 751 |
TRX_UNDO_LOG_GTID_LEN, mtr); |
| 752 |
undo->flag |= gtid_flag; |
| 753 |
} |
| 754 |
mlog_write_ulint(undo_header + TRX_UNDO_FLAGS, undo->flag, MLOG_1BYTE, mtr); |
| 755 |
} |
| 756 |
|
| 757 |
/** Read X/Open XA Transaction Identification (XID) from undo log header */ |
| 758 |
static void trx_undo_read_xid( |
| 759 |
trx_ulogf_t *log_hdr, /*!< in: undo log header */ |
| 760 |
XID *xid) /*!< out: X/Open XA Transaction Identification */ |
| 761 |
{ |
| 762 |
xid->set_format_id( |
| 763 |
static_cast<long>(mach_read_from_4(log_hdr + TRX_UNDO_XA_FORMAT))); |
| 764 |
|
| 765 |
xid->set_gtrid_length( |
| 766 |
static_cast<long>(mach_read_from_4(log_hdr + TRX_UNDO_XA_TRID_LEN))); |
| 767 |
|
| 768 |
xid->set_bqual_length( |
| 769 |
static_cast<long>(mach_read_from_4(log_hdr + TRX_UNDO_XA_BQUAL_LEN))); |
| 770 |
|
| 771 |
xid->set_data(log_hdr + TRX_UNDO_XA_XID, XIDDATASIZE); |
| 772 |
} |
| 773 |
|
| 774 |
/** Adds space for the XA XID after an undo log old-style header. |
| 775 |
@param[in,out] undo_page Undo log segment header page |
| 776 |
@param[in,out] log_hdr Undo log header |
| 777 |
@param[in,out] mtr Mini-transaction |
| 778 |
@param[in] gtid_storage GTID storage type */ |
| 779 |
static void trx_undo_header_add_space_for_xid( |
| 780 |
page_t *undo_page, trx_ulogf_t *log_hdr, mtr_t *mtr, |
| 781 |
trx_undo_t::Gtid_storage gtid_storage) { |
| 782 |
trx_upagef_t *page_hdr = undo_page + TRX_UNDO_PAGE_HDR; |
| 783 |
|
| 784 |
ulint free = mach_read_from_2(page_hdr + TRX_UNDO_PAGE_FREE); |
| 785 |
|
| 786 |
/* free is now the end offset of the old style undo log header */ |
| 787 |
ut_a(free == (ulint)(log_hdr - undo_page) + TRX_UNDO_LOG_OLD_HDR_SIZE); |
| 788 |
|
| 789 |
ulint new_limit = TRX_UNDO_LOG_XA_HDR_SIZE; |
| 790 |
|
| 791 |
switch (gtid_storage) { |
| 792 |
case trx_undo_t::Gtid_storage::COMMIT: |
| 793 |
new_limit = TRX_UNDO_LOG_GTID_HDR_SIZE; |
| 794 |
break; |
| 795 |
|
| 796 |
case trx_undo_t::Gtid_storage::PREPARE_AND_COMMIT: |
| 797 |
new_limit = TRX_UNDO_LOG_GTID_XA_HDR_SIZE; |
| 798 |
break; |
| 799 |
|
| 800 |
case trx_undo_t::Gtid_storage::NONE: |
| 801 |
new_limit = TRX_UNDO_LOG_XA_HDR_SIZE; |
| 802 |
break; |
| 803 |
|
| 804 |
default: |
| 805 |
ut_d(ut_error); |
| 806 |
} |
| 807 |
|
| 808 |
ulint new_free = free + (new_limit - TRX_UNDO_LOG_OLD_HDR_SIZE); |
| 809 |
|
| 810 |
/* Add space for a XID after the header, update the free offset |
| 811 |
fields on the undo log page and in the undo log header */ |
| 812 |
|
| 813 |
mlog_write_ulint(page_hdr + TRX_UNDO_PAGE_START, new_free, MLOG_2BYTES, mtr); |
| 814 |
|
| 815 |
mlog_write_ulint(page_hdr + TRX_UNDO_PAGE_FREE, new_free, MLOG_2BYTES, mtr); |
| 816 |
|
| 817 |
mlog_write_ulint(log_hdr + TRX_UNDO_LOG_START, new_free, MLOG_2BYTES, mtr); |
| 818 |
} |
| 819 |
|
| 820 |
/** Writes the mtr log entry of an undo log header reuse. */ |
| 821 |
static inline void trx_undo_insert_header_reuse_log( |
| 822 |
const page_t *undo_page, /*!< in: undo log header page */ |
| 823 |
trx_id_t trx_id, /*!< in: transaction id */ |
| 824 |
mtr_t *mtr) /*!< in: mtr */ |
| 825 |
{ |
| 826 |
mlog_write_initial_log_record(undo_page, MLOG_UNDO_HDR_REUSE, mtr); |
| 827 |
|
| 828 |
mlog_catenate_ull_compressed(mtr, trx_id); |
| 829 |
} |
| 830 |
#else /* !UNIV_HOTBACKUP */ |
| 831 |
#define trx_undo_insert_header_reuse_log(undo_page, trx_id, mtr) ((void)0) |
| 832 |
#endif /* !UNIV_HOTBACKUP */ |
| 833 |
|
| 834 |
/** Parse the redo log entry of an undo log page header create or reuse. |
| 835 |
@param[in] type MLOG_UNDO_HDR_CREATE or MLOG_UNDO_HDR_REUSE |
| 836 |
@param[in] ptr Redo log record |
| 837 |
@param[in] end_ptr End of log buffer |
| 838 |
@param[in,out] page Page frame or NULL |
| 839 |
@param[in,out] mtr Mini-transaction or NULL |
| 840 |
@return end of log record or NULL */ |
| 841 |
byte *trx_undo_parse_page_header(mlog_id_t type, const byte *ptr, |
| 842 |
const byte *end_ptr, page_t *page, |
| 843 |
mtr_t *mtr) { |
| 844 |
trx_id_t trx_id = mach_u64_parse_compressed(&ptr, end_ptr); |
| 845 |
|
| 846 |
if (ptr != nullptr && page != nullptr) { |
| 847 |
switch (type) { |
| 848 |
case MLOG_UNDO_HDR_CREATE: |
| 849 |
trx_undo_header_create(page, trx_id, mtr); |
| 850 |
return (const_cast<byte *>(ptr)); |
| 851 |
case MLOG_UNDO_HDR_REUSE: |
| 852 |
trx_undo_insert_header_reuse(page, trx_id, mtr); |
| 853 |
return (const_cast<byte *>(ptr)); |
| 854 |
default: |
| 855 |
break; |
| 856 |
} |
| 857 |
ut_d(ut_error); |
| 858 |
} |
| 859 |
|
| 860 |
return (const_cast<byte *>(ptr)); |
| 861 |
} |
| 862 |
|
| 863 |
/** Initializes a cached insert undo log header page for new use. NOTE that this |
| 864 |
function has its own log record type MLOG_UNDO_HDR_REUSE. You must NOT change |
| 865 |
the operation of this function! |
| 866 |
@return undo log header byte offset on page */ |
| 867 |
static ulint trx_undo_insert_header_reuse( |
| 868 |
page_t *undo_page, /*!< in/out: insert undo log segment |
| 869 |
header page, x-latched */ |
| 870 |
trx_id_t trx_id, /*!< in: transaction id */ |
| 871 |
mtr_t *mtr) /*!< in: mtr */ |
| 872 |
{ |
| 873 |
trx_upagef_t *page_hdr; |
| 874 |
trx_usegf_t *seg_hdr; |
| 875 |
trx_ulogf_t *log_hdr; |
| 876 |
ulint free; |
| 877 |
ulint new_free; |
| 878 |
|
| 879 |
ut_ad(mtr && undo_page); |
| 880 |
|
| 881 |
page_hdr = undo_page + TRX_UNDO_PAGE_HDR; |
| 882 |
seg_hdr = undo_page + TRX_UNDO_SEG_HDR; |
| 883 |
|
| 884 |
free = TRX_UNDO_SEG_HDR + TRX_UNDO_SEG_HDR_SIZE; |
| 885 |
|
| 886 |
ut_a(free + TRX_UNDO_LOG_GTID_XA_HDR_SIZE < UNIV_PAGE_SIZE - 100); |
| 887 |
|
| 888 |
log_hdr = undo_page + free; |
| 889 |
|
| 890 |
new_free = free + TRX_UNDO_LOG_OLD_HDR_SIZE; |
| 891 |
|
| 892 |
/* Insert undo data is not needed after commit: we may free all |
| 893 |
the space on the page */ |
| 894 |
|
| 895 |
ut_a(mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_TYPE) == |
| 896 |
TRX_UNDO_INSERT); |
| 897 |
|
| 898 |
mach_write_to_2(page_hdr + TRX_UNDO_PAGE_START, new_free); |
| 899 |
|
| 900 |
mach_write_to_2(page_hdr + TRX_UNDO_PAGE_FREE, new_free); |
| 901 |
|
| 902 |
mach_write_to_2(seg_hdr + TRX_UNDO_STATE, TRX_UNDO_ACTIVE); |
| 903 |
|
| 904 |
log_hdr = undo_page + free; |
| 905 |
|
| 906 |
mach_write_to_8(log_hdr + TRX_UNDO_TRX_ID, trx_id); |
| 907 |
mach_write_to_2(log_hdr + TRX_UNDO_LOG_START, new_free); |
| 908 |
|
| 909 |
mach_write_to_1(log_hdr + TRX_UNDO_FLAGS, 0); |
| 910 |
mach_write_to_1(log_hdr + TRX_UNDO_DICT_TRANS, false); |
| 911 |
|
| 912 |
/* Write the log record MLOG_UNDO_HDR_REUSE */ |
| 913 |
trx_undo_insert_header_reuse_log(undo_page, trx_id, mtr); |
| 914 |
|
| 915 |
return (free); |
| 916 |
} |
| 917 |
|
| 918 |
#ifndef UNIV_HOTBACKUP |
| 919 |
/** Tries to add a page to the undo log segment where the undo log is placed. |
| 920 |
@return X-latched block if success, else NULL */ |
| 921 |
buf_block_t *trx_undo_add_page( |
| 922 |
trx_t *trx, /*!< in: transaction */ |
| 923 |
trx_undo_t *undo, /*!< in: undo log memory object */ |
| 924 |
trx_undo_ptr_t *undo_ptr, /*!< in: assign undo log from |
| 925 |
referred rollback segment. */ |
| 926 |
mtr_t *mtr) /*!< in: mtr which does not have |
| 927 |
a latch to any undo log page; |
| 928 |
the caller must have reserved |
| 929 |
the rollback segment mutex */ |
| 930 |
{ |
| 931 |
page_t *header_page; |
| 932 |
buf_block_t *new_block; |
| 933 |
page_t *new_page; |
| 934 |
trx_rseg_t *rseg; |
| 935 |
ulint n_reserved; |
| 936 |
|
| 937 |
ut_ad(mutex_own(&(trx->undo_mutex))); |
| 938 |
ut_ad(mutex_own(&(undo_ptr->rseg->mutex))); |
| 939 |
|
| 940 |
rseg = undo_ptr->rseg; |
| 941 |
|
| 942 |
if (rseg->get_curr_size() == rseg->max_size) { |
| 943 |
return (nullptr); |
| 944 |
} |
| 945 |
|
| 946 |
header_page = trx_undo_page_get(page_id_t(undo->space, undo->hdr_page_no), |
| 947 |
undo->page_size, mtr); |
| 948 |
|
| 949 |
if (!fsp_reserve_free_extents(&n_reserved, undo->space, 1, FSP_UNDO, mtr)) { |
| 950 |
return (nullptr); |
| 951 |
} |
| 952 |
|
| 953 |
new_block = fseg_alloc_free_page_general( |
| 954 |
TRX_UNDO_SEG_HDR + TRX_UNDO_FSEG_HEADER + header_page, |
| 955 |
undo->top_page_no + 1, FSP_UP, true, mtr, mtr); |
| 956 |
|
| 957 |
fil_space_release_free_extents(undo->space, n_reserved); |
| 958 |
|
| 959 |
if (new_block == nullptr) { |
| 960 |
/* No space left */ |
| 961 |
|
| 962 |
return (nullptr); |
| 963 |
} |
| 964 |
|
| 965 |
ut_ad(rw_lock_get_x_lock_count(&new_block->lock) == 1); |
| 966 |
buf_block_dbg_add_level(new_block, SYNC_TRX_UNDO_PAGE); |
| 967 |
undo->last_page_no = new_block->page.id.page_no(); |
| 968 |
|
| 969 |
new_page = buf_block_get_frame(new_block); |
| 970 |
|
| 971 |
trx_undo_page_init(new_page, undo->type, mtr); |
| 972 |
|
| 973 |
flst_add_last(header_page + TRX_UNDO_SEG_HDR + TRX_UNDO_PAGE_LIST, |
| 974 |
new_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_NODE, mtr); |
| 975 |
undo->size++; |
| 976 |
rseg->incr_curr_size(); |
| 977 |
|
| 978 |
return (new_block); |
| 979 |
} |
| 980 |
|
| 981 |
/** Frees an undo log page that is not the header page. |
| 982 |
@return last page number in remaining log */ |
| 983 |
static page_no_t trx_undo_free_page( |
| 984 |
trx_rseg_t *rseg, /*!< in: rollback segment */ |
| 985 |
bool in_history, /*!< in: true if the undo log is in the history |
| 986 |
list */ |
| 987 |
space_id_t space, /*!< in: space */ |
| 988 |
page_no_t hdr_page_no, /*!< in: header page number */ |
| 989 |
page_no_t page_no, /*!< in: page number to free: must not be the |
| 990 |
header page */ |
| 991 |
mtr_t *mtr) /*!< in: mtr which does not have a latch to any |
| 992 |
undo log page; the caller must have reserved |
| 993 |
the rollback segment mutex */ |
| 994 |
{ |
| 995 |
page_t *header_page; |
| 996 |
page_t *undo_page; |
| 997 |
fil_addr_t last_addr; |
| 998 |
trx_rsegf_t *rseg_header; |
| 999 |
ulint hist_size; |
| 1000 |
|
| 1001 |
ut_a(hdr_page_no != page_no); |
| 1002 |
ut_ad(mutex_own(&(rseg->mutex))); |
| 1003 |
|
| 1004 |
undo_page = |
| 1005 |
trx_undo_page_get(page_id_t(space, page_no), rseg->page_size, mtr); |
| 1006 |
|
| 1007 |
header_page = |
| 1008 |
trx_undo_page_get(page_id_t(space, hdr_page_no), rseg->page_size, mtr); |
| 1009 |
|
| 1010 |
flst_remove(header_page + TRX_UNDO_SEG_HDR + TRX_UNDO_PAGE_LIST, |
| 1011 |
undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_NODE, mtr); |
| 1012 |
|
| 1013 |
fseg_free_page(header_page + TRX_UNDO_SEG_HDR + TRX_UNDO_FSEG_HEADER, space, |
| 1014 |
page_no, false, mtr); |
| 1015 |
|
| 1016 |
last_addr = |
| 1017 |
flst_get_last(header_page + TRX_UNDO_SEG_HDR + TRX_UNDO_PAGE_LIST, mtr); |
| 1018 |
|
| 1019 |
rseg->decr_curr_size(); |
| 1020 |
|
| 1021 |
if (in_history) { |
| 1022 |
rseg_header = trx_rsegf_get(space, rseg->page_no, rseg->page_size, mtr); |
| 1023 |
|
| 1024 |
hist_size = |
| 1025 |
mtr_read_ulint(rseg_header + TRX_RSEG_HISTORY_SIZE, MLOG_4BYTES, mtr); |
| 1026 |
ut_ad(hist_size > 0); |
| 1027 |
mlog_write_ulint(rseg_header + TRX_RSEG_HISTORY_SIZE, hist_size - 1, |
| 1028 |
MLOG_4BYTES, mtr); |
| 1029 |
} |
| 1030 |
|
| 1031 |
return (last_addr.page); |
| 1032 |
} |
| 1033 |
|
| 1034 |
void trx_undo_free_last_page_func(IF_DEBUG(const trx_t *trx, ) trx_undo_t *undo, |
| 1035 |
mtr_t *mtr) { |
| 1036 |
ut_ad(mutex_own(&trx->undo_mutex)); |
| 1037 |
ut_ad(undo->hdr_page_no != undo->last_page_no); |
| 1038 |
ut_ad(undo->size > 0); |
| 1039 |
|
| 1040 |
undo->last_page_no = |
| 1041 |
trx_undo_free_page(undo->rseg, false, undo->space, undo->hdr_page_no, |
| 1042 |
undo->last_page_no, mtr); |
| 1043 |
|
| 1044 |
undo->size--; |
| 1045 |
} |
| 1046 |
|
| 1047 |
/** Empties an undo log header page of undo records for that undo log. |
| 1048 |
Other undo logs may still have records on that page, if it is an update |
| 1049 |
undo log. |
| 1050 |
@param[in] space_id Tablespace ID |
| 1051 |
@param[in] page_size Page size |
| 1052 |
@param[in] hdr_page_no Header page number |
| 1053 |
@param[in] hdr_offset Header offset |
| 1054 |
@param[in,out] mtr Mini-transaction */ |
| 1055 |
static void trx_undo_empty_header_page(space_id_t space_id, |
| 1056 |
const page_size_t &page_size, |
| 1057 |
page_no_t hdr_page_no, ulint hdr_offset, |
| 1058 |
mtr_t *mtr) { |
| 1059 |
page_t *header_page; |
| 1060 |
trx_ulogf_t *log_hdr; |
| 1061 |
ulint end; |
| 1062 |
|
| 1063 |
header_page = |
| 1064 |
trx_undo_page_get(page_id_t(space_id, hdr_page_no), page_size, mtr); |
| 1065 |
|
| 1066 |
log_hdr = header_page + hdr_offset; |
| 1067 |
|
| 1068 |
end = trx_undo_page_get_end(header_page, hdr_page_no, hdr_offset); |
| 1069 |
|
| 1070 |
mlog_write_ulint(log_hdr + TRX_UNDO_LOG_START, end, MLOG_2BYTES, mtr); |
| 1071 |
} |
| 1072 |
|
| 1073 |
/** Get page offset up to which undo logs can be truncated. |
| 1074 |
There are three possibilities. |
| 1075 |
1. Truncate nothing on this page. Return -1 |
| 1076 |
2. Truncate part of the page. Return the offset |
| 1077 |
3. Truncate the whole page. Return 0 |
| 1078 |
@param[in] undo undo log to truncate |
| 1079 |
@param[in] undo_page undo log page to check |
| 1080 |
@param[in] limit limit up to which undo logs to be truncated |
| 1081 |
@return page offset to truncate to, 0 for whole page, -1 for nothing. */ |
| 1082 |
int trx_undo_page_truncate_offset(trx_undo_t *undo, page_t *undo_page, |
| 1083 |
undo_no_t limit) { |
| 1084 |
auto rec = trx_undo_page_get_last_rec(undo_page, undo->hdr_page_no, |
| 1085 |
undo->hdr_offset); |
| 1086 |
trx_undo_rec_t *trunc_rec = nullptr; |
| 1087 |
|
| 1088 |
while (rec != nullptr) { |
| 1089 |
/* Check if current record has gone below the limit. */ |
| 1090 |
if (trx_undo_rec_get_undo_no(rec) < limit) { |
| 1091 |
/* If this is the first record on the page, don't truncate anything */ |
| 1092 |
if (trunc_rec == nullptr) { |
| 1093 |
return (-1); |
| 1094 |
} |
| 1095 |
|
| 1096 |
/* Return an offset within the page. */ |
| 1097 |
return (trunc_rec - undo_page); |
| 1098 |
} |
| 1099 |
|
| 1100 |
/* Truncate at least up to this record, maybe more */ |
| 1101 |
trunc_rec = rec; |
| 1102 |
rec = trx_undo_page_get_prev_rec(rec, undo->hdr_page_no, undo->hdr_offset); |
| 1103 |
} |
| 1104 |
|
| 1105 |
/* All records on the page are >= limit */ |
| 1106 |
if (undo->last_page_no == undo->hdr_page_no) { |
| 1107 |
/* This is the header page. Return an offset |
| 1108 |
if there are any records on the page. */ |
| 1109 |
if (trunc_rec != nullptr) { |
| 1110 |
return (trunc_rec - undo_page); |
| 1111 |
} |
| 1112 |
|
| 1113 |
/* Header page is empty. Do not truncate anything. */ |
| 1114 |
return (-1); |
| 1115 |
} |
| 1116 |
|
| 1117 |
/* Truncate the whole page. */ |
| 1118 |
return (0); |
| 1119 |
} |
| 1120 |
|
| 1121 |
/** Truncates an undo log from the end. This function is used during a rollback |
| 1122 |
to free space from an undo log. |
| 1123 |
@param[in] trx transaction for this undo log |
| 1124 |
@param[in] undo undo log |
| 1125 |
@param[in] limit all undo records with undo number; |
| 1126 |
This value should be truncated. */ |
| 1127 |
void trx_undo_truncate_end_func(IF_DEBUG(const trx_t *trx, ) trx_undo_t *undo, |
| 1128 |
undo_no_t limit) { |
| 1129 |
ut_ad(mutex_own(&trx->undo_mutex)); |
| 1130 |
ut_ad(mutex_own(&undo->rseg->mutex)); |
| 1131 |
|
| 1132 |
mtr_t mtr; |
| 1133 |
|
| 1134 |
for (;;) { |
| 1135 |
mtr.start(); |
| 1136 |
|
| 1137 |
/* Set NO_REDO for temporary undo logs. */ |
| 1138 |
if (fsp_is_system_temporary(undo->rseg->space_id)) { |
| 1139 |
ut_ad(trx->rsegs.m_noredo.rseg == undo->rseg); |
| 1140 |
mtr.set_log_mode(MTR_LOG_NO_REDO); |
| 1141 |
} else { |
| 1142 |
ut_ad(trx->rsegs.m_redo.rseg == undo->rseg); |
| 1143 |
} |
| 1144 |
|
| 1145 |
const page_id_t page_id(undo->space, undo->last_page_no); |
| 1146 |
|
| 1147 |
auto undo_page = trx_undo_page_get(page_id, undo->page_size, &mtr); |
| 1148 |
|
| 1149 |
int trunc_offset = trx_undo_page_truncate_offset(undo, undo_page, limit); |
| 1150 |
|
| 1151 |
/* If offset is within the page, truncate part of the page and quit.*/ |
| 1152 |
if (trunc_offset > 0) { |
| 1153 |
mlog_write_ulint(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE, |
| 1154 |
trunc_offset, MLOG_2BYTES, &mtr); |
| 1155 |
break; |
| 1156 |
} |
| 1157 |
|
| 1158 |
/* If all recs are < limit, don't truncate anything. */ |
| 1159 |
if (trunc_offset < 0) { |
| 1160 |
break; |
| 1161 |
} |
| 1162 |
|
| 1163 |
/* Free the last page and move on to the next. */ |
| 1164 |
ut_ad(undo->last_page_no != undo->hdr_page_no); |
| 1165 |
trx_undo_free_last_page_func(IF_DEBUG(trx, ) undo, &mtr); |
| 1166 |
|
| 1167 |
mtr.commit(); |
| 1168 |
} |
| 1169 |
|
| 1170 |
mtr.commit(); |
| 1171 |
} |
| 1172 |
|
| 1173 |
/** Truncate the head of an undo log. |
| 1174 |
NOTE that only whole pages are freed; the header page is not |
| 1175 |
freed, but emptied, if all the records there are below the limit. |
| 1176 |
@param[in,out] rseg rollback segment |
| 1177 |
@param[in] hdr_page_no header page number |
| 1178 |
@param[in] hdr_offset header offset on the page |
| 1179 |
@param[in] limit first undo number to preserve |
| 1180 |
(everything below the limit will be truncated) */ |
| 1181 |
void trx_undo_truncate_start(trx_rseg_t *rseg, page_no_t hdr_page_no, |
| 1182 |
ulint hdr_offset, undo_no_t limit) { |
| 1183 |
page_t *undo_page; |
| 1184 |
trx_undo_rec_t *rec; |
| 1185 |
trx_undo_rec_t *last_rec; |
| 1186 |
page_no_t page_no; |
| 1187 |
mtr_t mtr; |
| 1188 |
|
| 1189 |
ut_ad(mutex_own(&(rseg->mutex))); |
| 1190 |
|
| 1191 |
if (!limit) { |
| 1192 |
return; |
| 1193 |
} |
| 1194 |
loop: |
| 1195 |
mtr.start(); |
| 1196 |
|
| 1197 |
if (fsp_is_system_temporary(rseg->space_id)) { |
| 1198 |
mtr.set_log_mode(MTR_LOG_NO_REDO); |
| 1199 |
} |
| 1200 |
|
| 1201 |
rec = trx_undo_get_first_rec(nullptr, rseg->space_id, rseg->page_size, |
| 1202 |
hdr_page_no, hdr_offset, RW_X_LATCH, &mtr); |
| 1203 |
if (rec == nullptr) { |
| 1204 |
/* Already empty */ |
| 1205 |
|
| 1206 |
mtr.commit(); |
| 1207 |
|
| 1208 |
return; |
| 1209 |
} |
| 1210 |
|
| 1211 |
undo_page = page_align(rec); |
| 1212 |
|
| 1213 |
last_rec = trx_undo_page_get_last_rec(undo_page, hdr_page_no, hdr_offset); |
| 1214 |
if (trx_undo_rec_get_undo_no(last_rec) >= limit) { |
| 1215 |
mtr.commit(); |
| 1216 |
|
| 1217 |
return; |
| 1218 |
} |
| 1219 |
|
| 1220 |
page_no = page_get_page_no(undo_page); |
| 1221 |
|
| 1222 |
if (page_no == hdr_page_no) { |
| 1223 |
trx_undo_empty_header_page(rseg->space_id, rseg->page_size, hdr_page_no, |
| 1224 |
hdr_offset, &mtr); |
| 1225 |
} else { |
| 1226 |
trx_undo_free_page(rseg, true, rseg->space_id, hdr_page_no, page_no, &mtr); |
| 1227 |
} |
| 1228 |
|
| 1229 |
mtr.commit(); |
| 1230 |
|
| 1231 |
goto loop; |
| 1232 |
} |
| 1233 |
|
| 1234 |
/** Frees an undo log segment which is not in the history list. |
| 1235 |
@param[in] undo undo log |
| 1236 |
@param[in] noredo whether the undo tablespace is redo logged */ |
| 1237 |
static void trx_undo_seg_free(const trx_undo_t *undo, bool noredo) { |
| 1238 |
bool finished; |
| 1239 |
|
| 1240 |
auto rseg = undo->rseg; |
| 1241 |
|
| 1242 |
do { |
| 1243 |
mtr_t mtr; |
| 1244 |
mtr.start(); |
| 1245 |
|
| 1246 |
if (noredo) { |
| 1247 |
mtr.set_log_mode(MTR_LOG_NO_REDO); |
| 1248 |
} |
| 1249 |
|
| 1250 |
rseg->latch(); |
| 1251 |
|
| 1252 |
auto seg_header = |
| 1253 |
trx_undo_page_get(page_id_t(undo->space, undo->hdr_page_no), |
| 1254 |
undo->page_size, &mtr) + |
| 1255 |
TRX_UNDO_SEG_HDR; |
| 1256 |
|
| 1257 |
auto file_seg = seg_header + TRX_UNDO_FSEG_HEADER; |
| 1258 |
|
| 1259 |
finished = fseg_free_step(file_seg, false, &mtr); |
| 1260 |
|
| 1261 |
if (finished) { |
| 1262 |
/* Update the rseg header */ |
| 1263 |
auto rseg_header = |
| 1264 |
trx_rsegf_get(rseg->space_id, rseg->page_no, rseg->page_size, &mtr); |
| 1265 |
trx_rsegf_set_nth_undo(rseg_header, undo->id, FIL_NULL, &mtr); |
| 1266 |
|
| 1267 |
rseg->decr_curr_size(undo->size); |
| 1268 |
MONITOR_DEC(MONITOR_NUM_UNDO_SLOT_USED); |
| 1269 |
} |
| 1270 |
|
| 1271 |
rseg->unlatch(); |
| 1272 |
mtr.commit(); |
| 1273 |
} while (!finished); |
| 1274 |
} |
| 1275 |
|
| 1276 |
/*========== UNDO LOG MEMORY COPY INITIALIZATION =====================*/ |
| 1277 |
|
| 1278 |
/** Creates and initializes an undo log memory object for a newly created |
| 1279 |
rseg. The memory object is inserted in the appropriate list in the rseg. |
| 1280 |
@return own: the undo log memory object */ |
| 1281 |
static trx_undo_t *trx_undo_mem_init( |
| 1282 |
trx_rseg_t *rseg, /*!< in: rollback segment memory object */ |
| 1283 |
ulint id, /*!< in: slot index within rseg */ |
| 1284 |
page_no_t page_no, /*!< in: undo log segment page number */ |
| 1285 |
mtr_t *mtr) /*!< in: mtr */ |
| 1286 |
{ |
| 1287 |
page_t *undo_page; |
| 1288 |
trx_upagef_t *page_header; |
| 1289 |
trx_usegf_t *seg_header; |
| 1290 |
trx_ulogf_t *undo_header; |
| 1291 |
trx_undo_t *undo; |
| 1292 |
ulint type; |
| 1293 |
ulint state; |
| 1294 |
trx_id_t trx_id; |
| 1295 |
ulint offset; |
| 1296 |
fil_addr_t last_addr; |
| 1297 |
page_t *last_page; |
| 1298 |
trx_undo_rec_t *rec; |
| 1299 |
XID xid; |
| 1300 |
|
| 1301 |
ut_a(id < TRX_RSEG_N_SLOTS); |
| 1302 |
|
| 1303 |
undo_page = trx_undo_page_get(page_id_t(rseg->space_id, page_no), |
| 1304 |
rseg->page_size, mtr); |
| 1305 |
|
| 1306 |
page_header = undo_page + TRX_UNDO_PAGE_HDR; |
| 1307 |
|
| 1308 |
type = mtr_read_ulint(page_header + TRX_UNDO_PAGE_TYPE, MLOG_2BYTES, mtr); |
| 1309 |
seg_header = undo_page + TRX_UNDO_SEG_HDR; |
| 1310 |
|
| 1311 |
state = mach_read_from_2(seg_header + TRX_UNDO_STATE); |
| 1312 |
|
| 1313 |
offset = mach_read_from_2(seg_header + TRX_UNDO_LAST_LOG); |
| 1314 |
|
| 1315 |
undo_header = undo_page + offset; |
| 1316 |
|
| 1317 |
trx_id = mach_read_from_8(undo_header + TRX_UNDO_TRX_ID); |
| 1318 |
|
| 1319 |
auto flag = mtr_read_ulint(undo_header + TRX_UNDO_FLAGS, MLOG_1BYTE, mtr); |
| 1320 |
|
| 1321 |
bool xid_exists = ((flag & TRX_UNDO_FLAG_XID) != 0); |
| 1322 |
|
| 1323 |
/* Read X/Open XA transaction identification if it exists, or |
| 1324 |
set it to NULL. */ |
| 1325 |
xid.reset(); |
| 1326 |
|
| 1327 |
if (xid_exists) { |
| 1328 |
trx_undo_read_xid(undo_header, &xid); |
| 1329 |
} |
| 1330 |
|
| 1331 |
undo = trx_undo_mem_create(rseg, id, type, trx_id, &xid, page_no, offset); |
| 1332 |
|
| 1333 |
undo->dict_operation = |
| 1334 |
mtr_read_ulint(undo_header + TRX_UNDO_DICT_TRANS, MLOG_1BYTE, mtr); |
| 1335 |
|
| 1336 |
undo->flag = flag; |
| 1337 |
|
| 1338 |
undo->m_gtid_storage = trx_undo_t::Gtid_storage::NONE; |
| 1339 |
|
| 1340 |
if ((flag & TRX_UNDO_FLAG_XA_PREPARE_GTID) != 0) { |
| 1341 |
/* Prepare GTID implies space is allocated for both |
| 1342 |
prepare and commit GTID. */ |
| 1343 |
undo->m_gtid_storage = trx_undo_t::Gtid_storage::PREPARE_AND_COMMIT; |
| 1344 |
} else if ((flag & TRX_UNDO_FLAG_GTID) != 0) { |
| 1345 |
/* Space is allocated for only commit GTID. */ |
| 1346 |
undo->m_gtid_storage = trx_undo_t::Gtid_storage::COMMIT; |
| 1347 |
} |
| 1348 |
|
| 1349 |
undo->state = state; |
| 1350 |
undo->size = flst_get_len(seg_header + TRX_UNDO_PAGE_LIST); |
| 1351 |
|
| 1352 |
/* If the log segment is being freed, the page list is inconsistent! */ |
| 1353 |
if (state == TRX_UNDO_TO_FREE) { |
| 1354 |
goto add_to_list; |
| 1355 |
} |
| 1356 |
|
| 1357 |
last_addr = flst_get_last(seg_header + TRX_UNDO_PAGE_LIST, mtr); |
| 1358 |
|
| 1359 |
undo->last_page_no = last_addr.page; |
| 1360 |
undo->top_page_no = last_addr.page; |
| 1361 |
|
| 1362 |
last_page = trx_undo_page_get(page_id_t(rseg->space_id, undo->last_page_no), |
| 1363 |
rseg->page_size, mtr); |
| 1364 |
|
| 1365 |
rec = trx_undo_page_get_last_rec(last_page, page_no, offset); |
| 1366 |
|
| 1367 |
if (rec == nullptr) { |
| 1368 |
undo->empty = true; |
| 1369 |
} else { |
| 1370 |
undo->empty = false; |
| 1371 |
undo->top_offset = rec - last_page; |
| 1372 |
undo->top_undo_no = trx_undo_rec_get_undo_no(rec); |
| 1373 |
} |
| 1374 |
add_to_list: |
| 1375 |
if (type == TRX_UNDO_INSERT) { |
| 1376 |
if (state != TRX_UNDO_CACHED) { |
| 1377 |
UT_LIST_ADD_LAST(rseg->insert_undo_list, undo); |
| 1378 |
} else { |
| 1379 |
UT_LIST_ADD_LAST(rseg->insert_undo_cached, undo); |
| 1380 |
|
| 1381 |
MONITOR_INC(MONITOR_NUM_UNDO_SLOT_CACHED); |
| 1382 |
} |
| 1383 |
} else { |
| 1384 |
ut_ad(type == TRX_UNDO_UPDATE); |
| 1385 |
if (state != TRX_UNDO_CACHED) { |
| 1386 |
UT_LIST_ADD_LAST(rseg->update_undo_list, undo); |
| 1387 |
/* For XA prepared transaction and XA rolled back transaction, we |
| 1388 |
could have GTID to be persisted. */ |
| 1389 |
if (undo->is_prepared() || state == TRX_UNDO_ACTIVE) { |
| 1390 |
trx_undo_gtid_read_and_persist(undo_header); |
| 1391 |
} |
| 1392 |
} else { |
| 1393 |
UT_LIST_ADD_LAST(rseg->update_undo_cached, undo); |
| 1394 |
|
| 1395 |
MONITOR_INC(MONITOR_NUM_UNDO_SLOT_CACHED); |
| 1396 |
} |
| 1397 |
} |
| 1398 |
|
| 1399 |
return (undo); |
| 1400 |
} |
| 1401 |
|
| 1402 |
/** Initializes the undo log lists for a rollback segment memory copy. This |
| 1403 |
function is only called when the database is started or a new rollback |
| 1404 |
segment is created. |
| 1405 |
@return the combined size of undo log segments in pages */ |
| 1406 |
ulint trx_undo_lists_init( |
| 1407 |
trx_rseg_t *rseg) /*!< in: rollback segment memory object */ |
| 1408 |
{ |
| 1409 |
ulint size = 0; |
| 1410 |
trx_rsegf_t *rseg_header; |
| 1411 |
ulint i; |
| 1412 |
mtr_t mtr; |
| 1413 |
|
| 1414 |
mtr.start(); |
| 1415 |
|
| 1416 |
rseg_header = |
| 1417 |
trx_rsegf_get_new(rseg->space_id, rseg->page_no, rseg->page_size, &mtr); |
| 1418 |
|
| 1419 |
for (i = 0; i < TRX_RSEG_N_SLOTS; i++) { |
| 1420 |
page_no_t page_no; |
| 1421 |
|
| 1422 |
page_no = trx_rsegf_get_nth_undo(rseg_header, i, &mtr); |
| 1423 |
|
| 1424 |
/* In forced recovery: try to avoid operations which look |
| 1425 |
at database pages; undo logs are rapidly changing data, and |
| 1426 |
the probability that they are in an inconsistent state is |
| 1427 |
high */ |
| 1428 |
|
| 1429 |
if (page_no != FIL_NULL && |
| 1430 |
srv_force_recovery < SRV_FORCE_NO_UNDO_LOG_SCAN) { |
| 1431 |
trx_undo_t *undo; |
| 1432 |
|
| 1433 |
undo = trx_undo_mem_init(rseg, i, page_no, &mtr); |
| 1434 |
|
| 1435 |
size += undo->size; |
| 1436 |
|
| 1437 |
mtr.commit(); |
| 1438 |
|
| 1439 |
mtr.start(); |
| 1440 |
|
| 1441 |
rseg_header = |
| 1442 |
trx_rsegf_get(rseg->space_id, rseg->page_no, rseg->page_size, &mtr); |
| 1443 |
|
| 1444 |
/* Found a used slot */ |
| 1445 |
MONITOR_INC(MONITOR_NUM_UNDO_SLOT_USED); |
| 1446 |
} |
| 1447 |
} |
| 1448 |
|
| 1449 |
mtr.commit(); |
| 1450 |
|
| 1451 |
return (size); |
| 1452 |
} |
| 1453 |
|
| 1454 |
/** Creates and initializes an undo log memory object. |
| 1455 |
@param[in] rseg rollback segment memory object |
| 1456 |
@param[in] id slot index within rseg |
| 1457 |
@param[in] type type of the log: TRX_UNDO_INSERT or TRX_UNDO_UPDATE |
| 1458 |
@param[in] trx_id id of the trx for which the undo log is created |
| 1459 |
@param[in] xid X/Open XA transaction identification |
| 1460 |
@param[in] page_no undo log header page number |
| 1461 |
@param[in] offset undo log header byte offset on page |
| 1462 |
@return own: the undo log memory object */ |
| 1463 |
static trx_undo_t *trx_undo_mem_create(trx_rseg_t *rseg, ulint id, ulint type, |
| 1464 |
trx_id_t trx_id, const XID *xid, |
| 1465 |
page_no_t page_no, ulint offset) { |
| 1466 |
trx_undo_t *undo; |
| 1467 |
|
| 1468 |
ut_a(id < TRX_RSEG_N_SLOTS); |
| 1469 |
|
| 1470 |
undo = static_cast<trx_undo_t *>( |
| 1471 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*undo))); |
| 1472 |
|
| 1473 |
if (undo == nullptr) { |
| 1474 |
return (nullptr); |
| 1475 |
} |
| 1476 |
|
| 1477 |
undo->id = id; |
| 1478 |
undo->type = type; |
| 1479 |
undo->state = TRX_UNDO_ACTIVE; |
| 1480 |
undo->del_marks = false; |
| 1481 |
undo->trx_id = trx_id; |
| 1482 |
undo->xid = *xid; |
| 1483 |
|
| 1484 |
undo->dict_operation = false; |
| 1485 |
undo->flag = 0; |
| 1486 |
undo->m_gtid_storage = trx_undo_t::Gtid_storage::NONE; |
| 1487 |
|
| 1488 |
undo->rseg = rseg; |
| 1489 |
|
| 1490 |
undo->space = rseg->space_id; |
| 1491 |
undo->page_size.copy_from(rseg->page_size); |
| 1492 |
undo->hdr_page_no = page_no; |
| 1493 |
undo->hdr_offset = offset; |
| 1494 |
undo->last_page_no = page_no; |
| 1495 |
undo->size = 1; |
| 1496 |
|
| 1497 |
undo->empty = true; |
| 1498 |
undo->top_page_no = page_no; |
| 1499 |
undo->guess_block = nullptr; |
| 1500 |
|
| 1501 |
return (undo); |
| 1502 |
} |
| 1503 |
|
| 1504 |
/** Initializes a cached undo log object for new use. */ |
| 1505 |
static void trx_undo_mem_init_for_reuse( |
| 1506 |
trx_undo_t *undo, /*!< in: undo log to init */ |
| 1507 |
trx_id_t trx_id, /*!< in: id of the trx for which the undo log |
| 1508 |
is created */ |
| 1509 |
const XID *xid, /*!< in: X/Open XA transaction identification*/ |
| 1510 |
ulint offset) /*!< in: undo log header byte offset on page */ |
| 1511 |
{ |
| 1512 |
ut_ad(mutex_own(&((undo->rseg)->mutex))); |
| 1513 |
|
| 1514 |
ut_a(undo->id < TRX_RSEG_N_SLOTS); |
| 1515 |
|
| 1516 |
undo->state = TRX_UNDO_ACTIVE; |
| 1517 |
undo->del_marks = false; |
| 1518 |
undo->trx_id = trx_id; |
| 1519 |
undo->xid = *xid; |
| 1520 |
|
| 1521 |
undo->dict_operation = false; |
| 1522 |
undo->flag = 0; |
| 1523 |
undo->m_gtid_storage = trx_undo_t::Gtid_storage::NONE; |
| 1524 |
|
| 1525 |
undo->hdr_offset = offset; |
| 1526 |
undo->empty = true; |
| 1527 |
} |
| 1528 |
|
| 1529 |
/** Frees an undo log memory copy. */ |
| 1530 |
void trx_undo_mem_free(trx_undo_t *undo) /*!< in: the undo object to be freed */ |
| 1531 |
{ |
| 1532 |
ut_a(undo->id < TRX_RSEG_N_SLOTS); |
| 1533 |
|
| 1534 |
ut::free(undo); |
| 1535 |
} |
| 1536 |
|
| 1537 |
/** Create a new undo log in the given rollback segment. |
| 1538 |
@param[in] rseg rollback segment memory copy |
| 1539 |
@param[in] type type of the log: TRX_UNDO_INSERT or TRX_UNDO_UPDATE |
| 1540 |
@param[in] trx_id id of the trx for which the undo log is created |
| 1541 |
@param[in] xid X/Open transaction identification |
| 1542 |
@param[in] gtid_storage GTID storage type |
| 1543 |
@param[out] undo the new undo log object, undefined if did not succeed |
| 1544 |
@param[in] mtr mini-transation |
| 1545 |
@retval DB_SUCCESS if successful in creating the new undo lob object, |
| 1546 |
@retval DB_TOO_MANY_CONCURRENT_TRXS |
| 1547 |
@retval DB_OUT_OF_FILE_SPACE |
| 1548 |
@retval DB_OUT_OF_MEMORY */ |
| 1549 |
[[nodiscard]] static dberr_t trx_undo_create( |
| 1550 |
trx_rseg_t *rseg, ulint type, trx_id_t trx_id, const XID *xid, |
| 1551 |
trx_undo_t::Gtid_storage gtid_storage, trx_undo_t **undo, mtr_t *mtr) { |
| 1552 |
trx_rsegf_t *rseg_header; |
| 1553 |
page_no_t page_no; |
| 1554 |
ulint offset; |
| 1555 |
ulint id; |
| 1556 |
page_t *undo_page; |
| 1557 |
|
| 1558 |
ut_ad(mutex_own(&(rseg->mutex))); |
| 1559 |
|
| 1560 |
if (rseg->get_curr_size() == rseg->max_size) { |
| 1561 |
return DB_OUT_OF_FILE_SPACE; |
| 1562 |
} |
| 1563 |
|
| 1564 |
rseg->incr_curr_size(); |
| 1565 |
|
| 1566 |
rseg_header = |
| 1567 |
trx_rsegf_get(rseg->space_id, rseg->page_no, rseg->page_size, mtr); |
| 1568 |
|
| 1569 |
auto err = trx_undo_seg_create(rseg, rseg_header, type, &id, &undo_page, mtr); |
| 1570 |
|
| 1571 |
if (err != DB_SUCCESS) { |
| 1572 |
/* Did not succeed */ |
| 1573 |
rseg->decr_curr_size(); |
| 1574 |
return err; |
| 1575 |
} |
| 1576 |
|
| 1577 |
page_no = page_get_page_no(undo_page); |
| 1578 |
|
| 1579 |
offset = trx_undo_header_create(undo_page, trx_id, mtr); |
| 1580 |
|
| 1581 |
/* GTID storage is needed only for update undo log. */ |
| 1582 |
if (type != TRX_UNDO_UPDATE) { |
| 1583 |
gtid_storage = trx_undo_t::Gtid_storage::NONE; |
| 1584 |
} |
| 1585 |
|
| 1586 |
trx_undo_header_add_space_for_xid(undo_page, undo_page + offset, mtr, |
| 1587 |
gtid_storage); |
| 1588 |
|
| 1589 |
*undo = trx_undo_mem_create(rseg, id, type, trx_id, xid, page_no, offset); |
| 1590 |
if (*undo == nullptr) { |
| 1591 |
return DB_OUT_OF_MEMORY; |
| 1592 |
} |
| 1593 |
|
| 1594 |
(*undo)->m_gtid_storage = gtid_storage; |
| 1595 |
return DB_SUCCESS; |
| 1596 |
} |
| 1597 |
|
| 1598 |
/*================ UNDO LOG ASSIGNMENT AND CLEANUP =====================*/ |
| 1599 |
|
| 1600 |
/** Reuses a cached undo log. |
| 1601 |
@param[in,out] rseg Rollback segment memory object |
| 1602 |
@param[in] type Type of the log: TRX_UNDO_INSERT or TRX_UNDO_UPDATE |
| 1603 |
@param[in] trx_id Id of the trx for which the undo log is used |
| 1604 |
@param[in] xid X/Open XA transaction identification |
| 1605 |
@param[in] gtid_storage GTID storage type |
| 1606 |
@param[in,out] mtr Mini-transaction |
| 1607 |
@return the undo log memory object, NULL if none cached */ |
| 1608 |
static trx_undo_t *trx_undo_reuse_cached(trx_rseg_t *rseg, ulint type, |
| 1609 |
trx_id_t trx_id, const XID *xid, |
| 1610 |
trx_undo_t::Gtid_storage gtid_storage, |
| 1611 |
mtr_t *mtr) { |
| 1612 |
trx_undo_t *undo; |
| 1613 |
|
| 1614 |
ut_ad(mutex_own(&(rseg->mutex))); |
| 1615 |
|
| 1616 |
if (type == TRX_UNDO_INSERT) { |
| 1617 |
undo = UT_LIST_GET_FIRST(rseg->insert_undo_cached); |
| 1618 |
if (undo == nullptr) { |
| 1619 |
return (nullptr); |
| 1620 |
} |
| 1621 |
|
| 1622 |
UT_LIST_REMOVE(rseg->insert_undo_cached, undo); |
| 1623 |
|
| 1624 |
MONITOR_DEC(MONITOR_NUM_UNDO_SLOT_CACHED); |
| 1625 |
} else { |
| 1626 |
ut_ad(type == TRX_UNDO_UPDATE); |
| 1627 |
|
| 1628 |
undo = UT_LIST_GET_FIRST(rseg->update_undo_cached); |
| 1629 |
if (undo == nullptr) { |
| 1630 |
return (nullptr); |
| 1631 |
} |
| 1632 |
|
| 1633 |
UT_LIST_REMOVE(rseg->update_undo_cached, undo); |
| 1634 |
|
| 1635 |
MONITOR_DEC(MONITOR_NUM_UNDO_SLOT_CACHED); |
| 1636 |
} |
| 1637 |
|
| 1638 |
ut_ad(undo->size == 1); |
| 1639 |
ut_a(undo->id < TRX_RSEG_N_SLOTS); |
| 1640 |
|
| 1641 |
auto undo_page = trx_undo_page_get(page_id_t(undo->space, undo->hdr_page_no), |
| 1642 |
undo->page_size, mtr); |
| 1643 |
ulint offset; |
| 1644 |
|
| 1645 |
if (type == TRX_UNDO_INSERT) { |
| 1646 |
offset = trx_undo_insert_header_reuse(undo_page, trx_id, mtr); |
| 1647 |
gtid_storage = trx_undo_t::Gtid_storage::NONE; |
| 1648 |
|
| 1649 |
} else { |
| 1650 |
ut_a(mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_TYPE) == |
| 1651 |
TRX_UNDO_UPDATE); |
| 1652 |
|
| 1653 |
offset = trx_undo_header_create(undo_page, trx_id, mtr); |
| 1654 |
} |
| 1655 |
|
| 1656 |
trx_undo_header_add_space_for_xid(undo_page, undo_page + offset, mtr, |
| 1657 |
gtid_storage); |
| 1658 |
|
| 1659 |
trx_undo_mem_init_for_reuse(undo, trx_id, xid, offset); |
| 1660 |
|
| 1661 |
undo->m_gtid_storage = gtid_storage; |
| 1662 |
|
| 1663 |
return (undo); |
| 1664 |
} |
| 1665 |
|
| 1666 |
/** Marks an undo log header as a header of a data dictionary operation |
| 1667 |
transaction. */ |
| 1668 |
static void trx_undo_mark_as_dict_operation( |
| 1669 |
trx_undo_t *undo, /*!< in: assigned undo log */ |
| 1670 |
mtr_t *mtr) /*!< in: mtr */ |
| 1671 |
{ |
| 1672 |
page_t *hdr_page; |
| 1673 |
|
| 1674 |
hdr_page = trx_undo_page_get(page_id_t(undo->space, undo->hdr_page_no), |
| 1675 |
undo->page_size, mtr); |
| 1676 |
|
| 1677 |
mlog_write_ulint(hdr_page + undo->hdr_offset + TRX_UNDO_DICT_TRANS, true, |
| 1678 |
MLOG_1BYTE, mtr); |
| 1679 |
|
| 1680 |
undo->dict_operation = true; |
| 1681 |
} |
| 1682 |
|
| 1683 |
/** Assigns an undo log for a transaction. A new undo log is created or a cached |
| 1684 |
undo log reused. |
| 1685 |
@return DB_SUCCESS if undo log assign successful, possible error codes |
| 1686 |
are: DB_TOO_MANY_CONCURRENT_TRXS DB_OUT_OF_FILE_SPACE DB_READ_ONLY |
| 1687 |
DB_OUT_OF_MEMORY */ |
| 1688 |
dberr_t trx_undo_assign_undo( |
| 1689 |
trx_t *trx, /*!< in: transaction */ |
| 1690 |
trx_undo_ptr_t *undo_ptr, /*!< in: assign undo log from |
| 1691 |
referred rollback segment. */ |
| 1692 |
ulint type) /*!< in: TRX_UNDO_INSERT or |
| 1693 |
TRX_UNDO_UPDATE */ |
| 1694 |
{ |
| 1695 |
trx_rseg_t *rseg; |
| 1696 |
trx_undo_t *undo; |
| 1697 |
mtr_t mtr; |
| 1698 |
dberr_t err = DB_SUCCESS; |
| 1699 |
|
| 1700 |
ut_ad(trx); |
| 1701 |
|
| 1702 |
/* In case of read-only scenario trx->rsegs.m_redo.rseg can be NULL but |
| 1703 |
still request for assigning undo logs is valid as temporary tables |
| 1704 |
can be updated in read-only mode. |
| 1705 |
If there is no rollback segment assigned to trx and still there is |
| 1706 |
object being updated there is something wrong and so this condition |
| 1707 |
check. */ |
| 1708 |
ut_ad(trx_is_rseg_assigned(trx)); |
| 1709 |
|
| 1710 |
rseg = undo_ptr->rseg; |
| 1711 |
|
| 1712 |
ut_ad(mutex_own(&(trx->undo_mutex))); |
| 1713 |
|
| 1714 |
bool no_redo = (&trx->rsegs.m_noredo == undo_ptr); |
| 1715 |
|
| 1716 |
/* If none of the undo pointers are assigned then this is |
| 1717 |
first time transaction is allocating undo segment. */ |
| 1718 |
bool is_first = undo_ptr->is_empty(); |
| 1719 |
|
| 1720 |
/* If any undo segment is assigned it is guaranteed that |
| 1721 |
Innodb would persist GTID. Call it before any undo segment |
| 1722 |
is assigned for transaction. We allocate space for GTID |
| 1723 |
only if GTID is persisted. */ |
| 1724 |
trx_undo_t::Gtid_storage gtid_storage{trx_undo_t::Gtid_storage::NONE}; |
| 1725 |
|
| 1726 |
if (!no_redo) { |
| 1727 |
auto >id_persistor = clone_sys->get_gtid_persistor(); |
| 1728 |
if (is_first) { |
| 1729 |
gtid_persistor.set_persist_gtid(trx, true); |
| 1730 |
} |
| 1731 |
/* Check if the undo segment needs to allocate for GTID. */ |
| 1732 |
gtid_storage = gtid_persistor.persists_gtid(trx); |
| 1733 |
} |
| 1734 |
|
| 1735 |
mtr.start(); |
| 1736 |
if (no_redo) { |
| 1737 |
mtr.set_log_mode(MTR_LOG_NO_REDO); |
| 1738 |
} else { |
| 1739 |
ut_ad(&trx->rsegs.m_redo == undo_ptr); |
| 1740 |
} |
| 1741 |
|
| 1742 |
rseg->latch(); |
| 1743 |
|
| 1744 |
DBUG_EXECUTE_IF("ib_create_table_fail_too_many_trx", |
| 1745 |
err = DB_TOO_MANY_CONCURRENT_TRXS; |
| 1746 |
goto func_exit;); |
| 1747 |
undo = |
| 1748 |
#ifdef UNIV_DEBUG |
| 1749 |
srv_inject_too_many_concurrent_trxs |
| 1750 |
? nullptr |
| 1751 |
: |
| 1752 |
#endif |
| 1753 |
trx_undo_reuse_cached(rseg, type, trx->id, trx->xid, gtid_storage, |
| 1754 |
&mtr); |
| 1755 |
|
| 1756 |
if (undo == nullptr) { |
| 1757 |
err = trx_undo_create(rseg, type, trx->id, trx->xid, gtid_storage, &undo, |
| 1758 |
&mtr); |
| 1759 |
if (err != DB_SUCCESS) { |
| 1760 |
goto func_exit; |
| 1761 |
} |
| 1762 |
} |
| 1763 |
|
| 1764 |
if (type == TRX_UNDO_INSERT) { |
| 1765 |
UT_LIST_ADD_FIRST(rseg->insert_undo_list, undo); |
| 1766 |
ut_ad(undo_ptr->insert_undo == nullptr); |
| 1767 |
undo_ptr->insert_undo = undo; |
| 1768 |
} else { |
| 1769 |
UT_LIST_ADD_FIRST(rseg->update_undo_list, undo); |
| 1770 |
ut_ad(undo_ptr->update_undo == nullptr); |
| 1771 |
undo_ptr->update_undo = undo; |
| 1772 |
} |
| 1773 |
|
| 1774 |
if (trx->mysql_thd && !trx->ddl_operation && |
| 1775 |
thd_is_dd_update_stmt(trx->mysql_thd)) { |
| 1776 |
trx->ddl_operation = true; |
| 1777 |
} |
| 1778 |
|
| 1779 |
if (trx->ddl_operation || trx_get_dict_operation(trx) != TRX_DICT_OP_NONE) { |
| 1780 |
trx_undo_mark_as_dict_operation(undo, &mtr); |
| 1781 |
} |
| 1782 |
|
| 1783 |
/* For GTID persistence we might add undo segment to prepared transaction. If |
| 1784 |
the transaction is in prepared state, we need to set XA properties. */ |
| 1785 |
if (trx_state_eq(trx, TRX_STATE_PREPARED)) { |
| 1786 |
ut_ad(!is_first); |
| 1787 |
undo->set_prepared(trx->xid); |
| 1788 |
} |
| 1789 |
|
| 1790 |
func_exit: |
| 1791 |
rseg->unlatch(); |
| 1792 |
mtr.commit(); |
| 1793 |
|
| 1794 |
return (err); |
| 1795 |
} |
| 1796 |
|
| 1797 |
/** Sets the state of the undo log segment at a transaction finish. |
| 1798 |
@return undo log segment header page, x-latched */ |
| 1799 |
page_t *trx_undo_set_state_at_finish( |
| 1800 |
trx_undo_t *undo, /*!< in: undo log memory copy */ |
| 1801 |
mtr_t *mtr) /*!< in: mtr */ |
| 1802 |
{ |
| 1803 |
trx_usegf_t *seg_hdr; |
| 1804 |
trx_upagef_t *page_hdr; |
| 1805 |
page_t *undo_page; |
| 1806 |
ulint state; |
| 1807 |
|
| 1808 |
ut_a(undo->id < TRX_RSEG_N_SLOTS); |
| 1809 |
|
| 1810 |
undo_page = trx_undo_page_get(page_id_t(undo->space, undo->hdr_page_no), |
| 1811 |
undo->page_size, mtr); |
| 1812 |
|
| 1813 |
seg_hdr = undo_page + TRX_UNDO_SEG_HDR; |
| 1814 |
page_hdr = undo_page + TRX_UNDO_PAGE_HDR; |
| 1815 |
|
| 1816 |
if (undo->size == 1 && mach_read_from_2(page_hdr + TRX_UNDO_PAGE_FREE) < |
| 1817 |
TRX_UNDO_PAGE_REUSE_LIMIT) { |
| 1818 |
state = TRX_UNDO_CACHED; |
| 1819 |
|
| 1820 |
} else if (undo->type == TRX_UNDO_INSERT) { |
| 1821 |
state = TRX_UNDO_TO_FREE; |
| 1822 |
} else { |
| 1823 |
state = TRX_UNDO_TO_PURGE; |
| 1824 |
} |
| 1825 |
|
| 1826 |
undo->state = state; |
| 1827 |
|
| 1828 |
mlog_write_ulint(seg_hdr + TRX_UNDO_STATE, state, MLOG_2BYTES, mtr); |
| 1829 |
|
| 1830 |
return (undo_page); |
| 1831 |
} |
| 1832 |
|
| 1833 |
/** Set the state of the undo log segment at a XA PREPARE or XA ROLLBACK. |
| 1834 |
@param[in,out] trx Transaction |
| 1835 |
@param[in,out] undo Insert_undo or update_undo log |
| 1836 |
@param[in] rollback false=XA PREPARE, true=XA ROLLBACK |
| 1837 |
@param[in,out] mtr Mini-transaction |
| 1838 |
@return undo log segment header page, x-latched */ |
| 1839 |
page_t *trx_undo_set_state_at_prepare(trx_t *trx, trx_undo_t *undo, |
| 1840 |
bool rollback, mtr_t *mtr) { |
| 1841 |
trx_usegf_t *seg_hdr; |
| 1842 |
trx_ulogf_t *undo_header; |
| 1843 |
page_t *undo_page; |
| 1844 |
ulint offset; |
| 1845 |
|
| 1846 |
ut_ad(trx && undo && mtr); |
| 1847 |
|
| 1848 |
ut_a(undo->id < TRX_RSEG_N_SLOTS); |
| 1849 |
|
| 1850 |
undo_page = trx_undo_page_get(page_id_t(undo->space, undo->hdr_page_no), |
| 1851 |
undo->page_size, mtr); |
| 1852 |
|
| 1853 |
seg_hdr = undo_page + TRX_UNDO_SEG_HDR; |
| 1854 |
|
| 1855 |
offset = mach_read_from_2(seg_hdr + TRX_UNDO_LAST_LOG); |
| 1856 |
undo_header = undo_page + offset; |
| 1857 |
|
| 1858 |
if (rollback) { |
| 1859 |
ut_ad(undo->is_prepared()); |
| 1860 |
|
| 1861 |
/* Write GTID information if there. */ |
| 1862 |
trx_undo_gtid_write(trx, undo_header, undo, mtr, !rollback); |
| 1863 |
|
| 1864 |
mlog_write_ulint(seg_hdr + TRX_UNDO_STATE, TRX_UNDO_ACTIVE, MLOG_2BYTES, |
| 1865 |
mtr); |
| 1866 |
return (undo_page); |
| 1867 |
} |
| 1868 |
|
| 1869 |
ut_ad(undo->state == TRX_UNDO_ACTIVE); |
| 1870 |
undo->set_prepared(trx->xid); |
| 1871 |
|
| 1872 |
mlog_write_ulint(seg_hdr + TRX_UNDO_STATE, undo->state, MLOG_2BYTES, mtr); |
| 1873 |
|
| 1874 |
mlog_write_ulint(undo_header + TRX_UNDO_FLAGS, undo->flag, MLOG_1BYTE, mtr); |
| 1875 |
|
| 1876 |
trx_undo_write_xid(undo_header, &undo->xid, mtr); |
| 1877 |
|
| 1878 |
return (undo_page); |
| 1879 |
} |
| 1880 |
|
| 1881 |
page_t *trx_undo_set_prepared_in_tc(trx_t *trx, trx_undo_t *undo, mtr_t *mtr) { |
| 1882 |
trx_usegf_t *seg_hdr; |
| 1883 |
trx_ulogf_t *undo_header; |
| 1884 |
page_t *undo_page; |
| 1885 |
ulint offset; |
| 1886 |
|
| 1887 |
ut_ad(trx && undo && mtr); |
| 1888 |
|
| 1889 |
ut_a(undo->id < TRX_RSEG_N_SLOTS); |
| 1890 |
|
| 1891 |
undo_page = trx_undo_page_get(page_id_t(undo->space, undo->hdr_page_no), |
| 1892 |
undo->page_size, mtr); |
| 1893 |
|
| 1894 |
seg_hdr = undo_page + TRX_UNDO_SEG_HDR; |
| 1895 |
|
| 1896 |
offset = mach_read_from_2(seg_hdr + TRX_UNDO_LAST_LOG); |
| 1897 |
undo_header = undo_page + offset; |
| 1898 |
|
| 1899 |
ut_ad(undo->state == TRX_UNDO_PREPARED_80028 || |
| 1900 |
undo->state == TRX_UNDO_PREPARED); |
| 1901 |
|
| 1902 |
/* Write GTID information if there. */ |
| 1903 |
trx_undo_gtid_write(trx, undo_header, undo, mtr, true); |
| 1904 |
|
| 1905 |
undo->set_prepared_in_tc(); |
| 1906 |
|
| 1907 |
mlog_write_ulint(seg_hdr + TRX_UNDO_STATE, undo->state, MLOG_2BYTES, mtr); |
| 1908 |
|
| 1909 |
return (undo_page); |
| 1910 |
} |
| 1911 |
|
| 1912 |
/** Adds the update undo log header as the first in the history list, and |
| 1913 |
frees the memory object, or puts it to the list of cached update undo log |
| 1914 |
segments. |
| 1915 |
@param[in] trx Trx owning the update undo log |
| 1916 |
@param[in] undo_ptr Update undo log. |
| 1917 |
@param[in] undo_page Update undo log header page, x-latched |
| 1918 |
@param[in] update_rseg_history_len If true: update rseg history len else |
| 1919 |
skip updating it. |
| 1920 |
@param[in] n_added_logs Number of logs added |
| 1921 |
@param[in] mtr Mini-transaction */ |
| 1922 |
void trx_undo_update_cleanup(trx_t *trx, trx_undo_ptr_t *undo_ptr, |
| 1923 |
page_t *undo_page, bool update_rseg_history_len, |
| 1924 |
|
| 1925 |
ulint n_added_logs, mtr_t *mtr) { |
| 1926 |
trx_rseg_t *rseg; |
| 1927 |
trx_undo_t *undo; |
| 1928 |
|
| 1929 |
undo = undo_ptr->update_undo; |
| 1930 |
rseg = undo_ptr->rseg; |
| 1931 |
|
| 1932 |
ut_ad(mutex_own(&(rseg->mutex))); |
| 1933 |
|
| 1934 |
trx_purge_add_update_undo_to_history( |
| 1935 |
trx, undo_ptr, undo_page, update_rseg_history_len, n_added_logs, mtr); |
| 1936 |
|
| 1937 |
UT_LIST_REMOVE(rseg->update_undo_list, undo); |
| 1938 |
|
| 1939 |
undo_ptr->update_undo = nullptr; |
| 1940 |
|
| 1941 |
if (undo->state == TRX_UNDO_CACHED) { |
| 1942 |
UT_LIST_ADD_FIRST(rseg->update_undo_cached, undo); |
| 1943 |
|
| 1944 |
MONITOR_INC(MONITOR_NUM_UNDO_SLOT_CACHED); |
| 1945 |
} else { |
| 1946 |
ut_ad(undo->state == TRX_UNDO_TO_PURGE); |
| 1947 |
|
| 1948 |
trx_undo_mem_free(undo); |
| 1949 |
} |
| 1950 |
} |
| 1951 |
|
| 1952 |
/** Frees an insert undo log after a transaction commit or rollback. |
| 1953 |
Knowledge of inserts is not needed after a commit or rollback, therefore |
| 1954 |
the data can be discarded. |
| 1955 |
@param[in,out] undo_ptr undo log to clean up |
| 1956 |
@param[in] noredo whether the undo tablespace is redo logged */ |
| 1957 |
void trx_undo_insert_cleanup(trx_undo_ptr_t *undo_ptr, bool noredo) { |
| 1958 |
trx_undo_t *undo; |
| 1959 |
trx_rseg_t *rseg; |
| 1960 |
|
| 1961 |
undo = undo_ptr->insert_undo; |
| 1962 |
ut_ad(undo != nullptr); |
| 1963 |
|
| 1964 |
rseg = undo_ptr->rseg; |
| 1965 |
|
| 1966 |
ut_ad(noredo == fsp_is_system_temporary(rseg->space_id)); |
| 1967 |
|
| 1968 |
rseg->latch(); |
| 1969 |
|
| 1970 |
UT_LIST_REMOVE(rseg->insert_undo_list, undo); |
| 1971 |
undo_ptr->insert_undo = nullptr; |
| 1972 |
|
| 1973 |
if (undo->state == TRX_UNDO_CACHED) { |
| 1974 |
UT_LIST_ADD_FIRST(rseg->insert_undo_cached, undo); |
| 1975 |
|
| 1976 |
MONITOR_INC(MONITOR_NUM_UNDO_SLOT_CACHED); |
| 1977 |
} else { |
| 1978 |
ut_ad(undo->state == TRX_UNDO_TO_FREE); |
| 1979 |
|
| 1980 |
/* Delete first the undo log segment in the file */ |
| 1981 |
|
| 1982 |
rseg->unlatch(); |
| 1983 |
|
| 1984 |
DEBUG_SYNC_C("innodb_commit_wait_for_truncate"); |
| 1985 |
trx_undo_seg_free(undo, noredo); |
| 1986 |
|
| 1987 |
rseg->latch(); |
| 1988 |
|
| 1989 |
trx_undo_mem_free(undo); |
| 1990 |
} |
| 1991 |
|
| 1992 |
rseg->unlatch(); |
| 1993 |
} |
| 1994 |
|
| 1995 |
void trx_undo_free_trx_with_prepared_or_active_logs(trx_t *trx, bool prepared) { |
| 1996 |
ut_a(srv_shutdown_state.load() == SRV_SHUTDOWN_EXIT_THREADS); |
| 1997 |
|
| 1998 |
if (trx->rsegs.m_redo.update_undo) { |
| 1999 |
ut_a(trx->rsegs.m_redo.update_undo->is_prepared() == prepared); |
| 2000 |
|
| 2001 |
UT_LIST_REMOVE(trx->rsegs.m_redo.rseg->update_undo_list, |
| 2002 |
trx->rsegs.m_redo.update_undo); |
| 2003 |
trx_undo_mem_free(trx->rsegs.m_redo.update_undo); |
| 2004 |
|
| 2005 |
trx->rsegs.m_redo.update_undo = nullptr; |
| 2006 |
} |
| 2007 |
|
| 2008 |
if (trx->rsegs.m_redo.insert_undo) { |
| 2009 |
ut_a(trx->rsegs.m_redo.insert_undo->is_prepared() == prepared); |
| 2010 |
|
| 2011 |
UT_LIST_REMOVE(trx->rsegs.m_redo.rseg->insert_undo_list, |
| 2012 |
trx->rsegs.m_redo.insert_undo); |
| 2013 |
trx_undo_mem_free(trx->rsegs.m_redo.insert_undo); |
| 2014 |
|
| 2015 |
trx->rsegs.m_redo.insert_undo = nullptr; |
| 2016 |
} |
| 2017 |
|
| 2018 |
if (trx->rsegs.m_noredo.update_undo) { |
| 2019 |
ut_a(trx->rsegs.m_noredo.update_undo->is_prepared() == prepared); |
| 2020 |
|
| 2021 |
UT_LIST_REMOVE(trx->rsegs.m_noredo.rseg->update_undo_list, |
| 2022 |
trx->rsegs.m_noredo.update_undo); |
| 2023 |
trx_undo_mem_free(trx->rsegs.m_noredo.update_undo); |
| 2024 |
|
| 2025 |
trx->rsegs.m_noredo.update_undo = nullptr; |
| 2026 |
} |
| 2027 |
if (trx->rsegs.m_noredo.insert_undo) { |
| 2028 |
ut_a(trx->rsegs.m_noredo.insert_undo->is_prepared() == prepared); |
| 2029 |
|
| 2030 |
UT_LIST_REMOVE(trx->rsegs.m_noredo.rseg->insert_undo_list, |
| 2031 |
trx->rsegs.m_noredo.insert_undo); |
| 2032 |
trx_undo_mem_free(trx->rsegs.m_noredo.insert_undo); |
| 2033 |
|
| 2034 |
trx->rsegs.m_noredo.insert_undo = nullptr; |
| 2035 |
} |
| 2036 |
} |
| 2037 |
|
| 2038 |
bool trx_undo_truncate_tablespace(undo::Tablespace *marked_space) { |
| 2039 |
#ifdef UNIV_DEBUG |
| 2040 |
static undo::Inject_failure_once injector("ib_undo_trunc_fail_truncate"); |
| 2041 |
if (injector.should_fail()) { |
| 2042 |
return (false); |
| 2043 |
}; |
| 2044 |
#endif /* UNIV_DEBUG */ |
| 2045 |
|
| 2046 |
bool is_encrypted; |
| 2047 |
|
| 2048 |
auto old_space_id = marked_space->id(); |
| 2049 |
auto space_num = undo::id2num(old_space_id); |
| 2050 |
auto marked_rsegs = marked_space->rsegs(); |
| 2051 |
|
| 2052 |
undo::unuse_space_id(old_space_id); |
| 2053 |
|
| 2054 |
auto new_space_id = undo::use_next_space_id(space_num); |
| 2055 |
|
| 2056 |
auto space = fil_space_get(old_space_id); |
| 2057 |
|
| 2058 |
auto n_pages = UNDO_INITIAL_SIZE_IN_PAGES; |
| 2059 |
|
| 2060 |
/* If the default extend amount has been increased greater than the default |
| 2061 |
and it has been less than 1 second since the last time the file was extended, |
| 2062 |
then we consider the undo tablespace to be growing aggressively. */ |
| 2063 |
if (space != nullptr && space->m_undo_extend > UNDO_INITIAL_SIZE_IN_PAGES && |
| 2064 |
space->m_last_extended.elapsed() < 1000) { |
| 2065 |
/* UNDO is being extended aggressively, don't reduce size to default. */ |
| 2066 |
n_pages = fil_space_get_size(old_space_id) / 4; |
| 2067 |
} |
| 2068 |
|
| 2069 |
/* Step-1: Delete the old tablespace. */ |
| 2070 |
/* Tablespace might have been attempted to be deleted before. If it was |
| 2071 |
removed, then possibly only new space creation failed, or old space file |
| 2072 |
deletion failed.*/ |
| 2073 |
if (space != nullptr) { |
| 2074 |
is_encrypted = FSP_FLAGS_GET_ENCRYPTION(space->flags); |
| 2075 |
|
| 2076 |
if (fil_delete_tablespace(old_space_id, BUF_REMOVE_NONE) != DB_SUCCESS) { |
| 2077 |
return false; |
| 2078 |
} |
| 2079 |
} else { |
| 2080 |
/* For example on Windows the file deletion can fail if the file |
| 2081 |
is being used. Just try again to remove it if it still exists. */ |
| 2082 |
os_file_delete_if_exists(innodb_data_file_key, marked_space->file_name(), |
| 2083 |
nullptr); |
| 2084 |
|
| 2085 |
/* We don't know if the undo was encrypted or not, just use the |
| 2086 |
srv_undo_log_encrypt value. */ |
| 2087 |
is_encrypted = true; |
| 2088 |
} |
| 2089 |
|
| 2090 |
/* Step-2: Re-create tablespace with new file. */ |
| 2091 |
ulint flags = fsp_flags_init(univ_page_size, false, false, false, false); |
| 2092 |
|
| 2093 |
/* Create the new UNDO tablespace. */ |
| 2094 |
if (fil_ibd_create(new_space_id, marked_space->space_name(), |
| 2095 |
marked_space->file_name(), flags, n_pages) != DB_SUCCESS) { |
| 2096 |
return false; |
| 2097 |
} |
| 2098 |
|
| 2099 |
ut_d(undo::inject_crash("ib_undo_trunc_empty_file")); |
| 2100 |
|
| 2101 |
/* This undo tablespace is unused. Lock the Rsegs before the |
| 2102 |
file_space because SYNC_RSEGS > SYNC_FSP. */ |
| 2103 |
marked_rsegs->x_lock(); |
| 2104 |
|
| 2105 |
/* Step-3: Re-initialize tablespace header. */ |
| 2106 |
log_free_check(); |
| 2107 |
|
| 2108 |
mtr_t mtr; |
| 2109 |
|
| 2110 |
mtr.start(); |
| 2111 |
|
| 2112 |
fsp_header_init(new_space_id, n_pages, &mtr); |
| 2113 |
|
| 2114 |
/* If tablespace is to be encrypted, encrypt it now */ |
| 2115 |
if (is_encrypted && srv_undo_log_encrypt) { |
| 2116 |
ut_d(bool ret =) set_undo_tablespace_encryption(new_space_id, &mtr); |
| 2117 |
/* Don't expect any error here (unless keyring plugin is uninstalled). In |
| 2118 |
that case too, continue truncation processing of tablespace. */ |
| 2119 |
ut_ad(!ret); |
| 2120 |
} |
| 2121 |
|
| 2122 |
/* Step-4: Add the RSEG_ARRAY page. */ |
| 2123 |
trx_rseg_array_create(new_space_id, &mtr); |
| 2124 |
|
| 2125 |
mtr.commit(); |
| 2126 |
|
| 2127 |
/* Step-5: Add rollback segment header pages. |
| 2128 |
This is different from trx_rseg_add_rollback_segments() in that the |
| 2129 |
undo::Tablespace::m_rsegs already exist and we are assigning a new |
| 2130 |
space_id to each rseg as we create the rseg header page. */ |
| 2131 |
|
| 2132 |
ut_d(undo::inject_crash("ib_undo_trunc_before_rsegs")); |
| 2133 |
|
| 2134 |
for (auto rseg : *marked_rsegs) { |
| 2135 |
log_free_check(); |
| 2136 |
|
| 2137 |
mtr.start(); |
| 2138 |
|
| 2139 |
mtr_x_lock(fil_space_get_latch(new_space_id), &mtr, UT_LOCATION_HERE); |
| 2140 |
|
| 2141 |
rseg->space_id = new_space_id; |
| 2142 |
|
| 2143 |
rseg->page_no = trx_rseg_header_create(new_space_id, univ_page_size, |
| 2144 |
PAGE_NO_MAX, rseg->id, &mtr); |
| 2145 |
|
| 2146 |
ut_a(rseg->page_no != FIL_NULL); |
| 2147 |
|
| 2148 |
auto rseg_header = |
| 2149 |
trx_rsegf_get_new(new_space_id, rseg->page_no, rseg->page_size, &mtr); |
| 2150 |
|
| 2151 |
/* Before re-initialization ensure that we free the existing |
| 2152 |
structure. There can't be any active transactions. */ |
| 2153 |
ut_a(UT_LIST_GET_LEN(rseg->update_undo_list) == 0); |
| 2154 |
ut_a(UT_LIST_GET_LEN(rseg->insert_undo_list) == 0); |
| 2155 |
|
| 2156 |
for (auto undo : rseg->update_undo_cached.removable()) { |
| 2157 |
UT_LIST_REMOVE(rseg->update_undo_cached, undo); |
| 2158 |
MONITOR_DEC(MONITOR_NUM_UNDO_SLOT_CACHED); |
| 2159 |
trx_undo_mem_free(undo); |
| 2160 |
} |
| 2161 |
|
| 2162 |
for (auto undo : rseg->insert_undo_cached.removable()) { |
| 2163 |
UT_LIST_REMOVE(rseg->insert_undo_cached, undo); |
| 2164 |
MONITOR_DEC(MONITOR_NUM_UNDO_SLOT_CACHED); |
| 2165 |
trx_undo_mem_free(undo); |
| 2166 |
} |
| 2167 |
|
| 2168 |
rseg->update_undo_list.clear(); |
| 2169 |
rseg->update_undo_cached.clear(); |
| 2170 |
rseg->insert_undo_list.clear(); |
| 2171 |
rseg->insert_undo_cached.clear(); |
| 2172 |
|
| 2173 |
rseg->max_size = |
| 2174 |
mtr_read_ulint(rseg_header + TRX_RSEG_MAX_SIZE, MLOG_4BYTES, &mtr); |
| 2175 |
|
| 2176 |
/* Initialize the undo log lists according to the rseg header */ |
| 2177 |
rseg->set_curr_size( |
| 2178 |
mtr_read_ulint(rseg_header + TRX_RSEG_HISTORY_SIZE, MLOG_4BYTES, &mtr) + |
| 2179 |
1); |
| 2180 |
|
| 2181 |
mtr.commit(); |
| 2182 |
|
| 2183 |
ut_ad(rseg->get_curr_size() == 1); |
| 2184 |
ut_ad(rseg->trx_ref_count == 0); |
| 2185 |
|
| 2186 |
rseg->last_page_no = FIL_NULL; |
| 2187 |
rseg->last_offset = 0; |
| 2188 |
rseg->last_trx_no = 0; |
| 2189 |
rseg->last_del_marks = false; |
| 2190 |
} |
| 2191 |
|
| 2192 |
marked_rsegs->x_unlock(); |
| 2193 |
|
| 2194 |
/* Increment the space ID for this undo space now so that if anyone refers |
| 2195 |
to this space, it is completely initialized. */ |
| 2196 |
marked_space->set_space_id(new_space_id); |
| 2197 |
|
| 2198 |
/* Set the amount in which an undo tablespace grows. */ |
| 2199 |
fil_space_set_undo_size(new_space_id, true); |
| 2200 |
|
| 2201 |
return true; |
| 2202 |
} |
| 2203 |
|
| 2204 |
#endif /* !UNIV_HOTBACKUP */ |