| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 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 mtr/mtr0mtr.cc |
| 29 |
Mini-transaction buffer |
| 30 |
|
| 31 |
Created 11/26/1995 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "mtr0mtr.h" |
| 35 |
|
| 36 |
#include "buf0buf.h" |
| 37 |
#include "buf0flu.h" |
| 38 |
#include "clone0api.h" |
| 39 |
#include "fsp0sysspace.h" |
| 40 |
#include "log0meb.h" |
| 41 |
#ifndef UNIV_HOTBACKUP |
| 42 |
#include "clone0clone.h" |
| 43 |
#include "log0buf.h" |
| 44 |
#include "log0chkp.h" |
| 45 |
#include "log0recv.h" |
| 46 |
#include "log0test.h" |
| 47 |
#include "log0write.h" |
| 48 |
#include "mtr0log.h" |
| 49 |
#endif /* !UNIV_HOTBACKUP */ |
| 50 |
#include "my_dbug.h" |
| 51 |
#ifndef UNIV_HOTBACKUP |
| 52 |
#include "page0types.h" |
| 53 |
#include "trx0purge.h" |
| 54 |
#endif /* !UNIV_HOTBACKUP */ |
| 55 |
|
| 56 |
static_assert(static_cast<int>(MTR_MEMO_PAGE_S_FIX) == |
| 57 |
static_cast<int>(RW_S_LATCH), |
| 58 |
""); |
| 59 |
|
| 60 |
static_assert(static_cast<int>(MTR_MEMO_PAGE_X_FIX) == |
| 61 |
static_cast<int>(RW_X_LATCH), |
| 62 |
""); |
| 63 |
|
| 64 |
static_assert(static_cast<int>(MTR_MEMO_PAGE_SX_FIX) == |
| 65 |
static_cast<int>(RW_SX_LATCH), |
| 66 |
""); |
| 67 |
|
| 68 |
/** Iterate over a memo block in reverse. */ |
| 69 |
template <typename Functor> |
| 70 |
struct Iterate { |
| 71 |
/** Release specific object */ |
| 72 |
explicit Iterate(Functor &functor) : m_functor(functor) { /* Do nothing */ |
| 73 |
} |
| 74 |
|
| 75 |
/** @return false if the functor returns false. */ |
| 76 |
bool operator()(mtr_buf_t::block_t *block) { |
| 77 |
const mtr_memo_slot_t *start = |
| 78 |
reinterpret_cast<const mtr_memo_slot_t *>(block->begin()); |
| 79 |
|
| 80 |
mtr_memo_slot_t *slot = reinterpret_cast<mtr_memo_slot_t *>(block->end()); |
| 81 |
|
| 82 |
ut_ad(!(block->used() % sizeof(*slot))); |
| 83 |
|
| 84 |
while (slot-- != start) { |
| 85 |
if (!m_functor(slot)) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
Functor &m_functor; |
| 94 |
}; |
| 95 |
|
| 96 |
/** Find specific object */ |
| 97 |
struct Find { |
| 98 |
/** Constructor */ |
| 99 |
Find(const void *object, ulint type) |
| 100 |
: m_slot(), m_type(type), m_object(object) { |
| 101 |
ut_a(object != nullptr); |
| 102 |
} |
| 103 |
|
| 104 |
/** @return false if the object was found. */ |
| 105 |
bool operator()(mtr_memo_slot_t *slot) { |
| 106 |
if (m_object == slot->object && m_type == slot->type) { |
| 107 |
m_slot = slot; |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
/** Slot if found */ |
| 115 |
mtr_memo_slot_t *m_slot; |
| 116 |
|
| 117 |
/** Type of the object to look for */ |
| 118 |
ulint m_type; |
| 119 |
|
| 120 |
/** The object instance to look for */ |
| 121 |
const void *m_object; |
| 122 |
}; |
| 123 |
|
| 124 |
/** Find a page frame */ |
| 125 |
struct Find_page { |
| 126 |
/** Constructor |
| 127 |
@param[in] ptr pointer to within a page frame |
| 128 |
@param[in] flags MTR_MEMO flags to look for */ |
| 129 |
Find_page(const void *ptr, ulint flags) |
| 130 |
: m_ptr(ptr), m_flags(flags), m_slot(nullptr) { |
| 131 |
/* We can only look for page-related flags. */ |
| 132 |
ut_ad(!(flags & |
| 133 |
~(MTR_MEMO_PAGE_S_FIX | MTR_MEMO_PAGE_X_FIX | MTR_MEMO_PAGE_SX_FIX | |
| 134 |
MTR_MEMO_BUF_FIX | MTR_MEMO_MODIFY))); |
| 135 |
} |
| 136 |
|
| 137 |
/** Visit a memo entry. |
| 138 |
@param[in] slot memo entry to visit |
| 139 |
@retval false if a page was found |
| 140 |
@retval true if the iteration should continue */ |
| 141 |
bool operator()(mtr_memo_slot_t *slot) { |
| 142 |
ut_ad(m_slot == nullptr); |
| 143 |
|
| 144 |
if (!(m_flags & slot->type) || slot->object == nullptr) { |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
buf_block_t *block = reinterpret_cast<buf_block_t *>(slot->object); |
| 149 |
|
| 150 |
if (m_ptr < block->frame || |
| 151 |
m_ptr >= block->frame + block->page.size.logical()) { |
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
m_slot = slot; |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
/** @return the slot that was found */ |
| 160 |
mtr_memo_slot_t *get_slot() const { |
| 161 |
ut_ad(m_slot != nullptr); |
| 162 |
return m_slot; |
| 163 |
} |
| 164 |
/** @return the block that was found */ |
| 165 |
buf_block_t *get_block() const { |
| 166 |
return reinterpret_cast<buf_block_t *>(get_slot()->object); |
| 167 |
} |
| 168 |
|
| 169 |
private: |
| 170 |
/** Pointer inside a page frame to look for */ |
| 171 |
const void *const m_ptr; |
| 172 |
/** MTR_MEMO flags to look for */ |
| 173 |
const ulint m_flags; |
| 174 |
/** The slot corresponding to m_ptr */ |
| 175 |
mtr_memo_slot_t *m_slot; |
| 176 |
}; |
| 177 |
|
| 178 |
struct Mtr_memo_print { |
| 179 |
Mtr_memo_print(std::ostream &out) : m_out(out) {} |
| 180 |
|
| 181 |
bool operator()(mtr_memo_slot_t *slot) { |
| 182 |
slot->print(m_out); |
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
private: |
| 187 |
std::ostream &m_out; |
| 188 |
}; |
| 189 |
|
| 190 |
std::ostream &mtr_t::print_memos(std::ostream &out) const { |
| 191 |
Mtr_memo_print printer(out); |
| 192 |
Iterate<Mtr_memo_print> iterator(printer); |
| 193 |
out << "[mtr_t: this=" << (void *)this << ", "; |
| 194 |
m_impl.m_memo.for_each_block_in_reverse(iterator); |
| 195 |
out << "]" << std::endl; |
| 196 |
return out; |
| 197 |
} |
| 198 |
|
| 199 |
#ifndef UNIV_HOTBACKUP |
| 200 |
#ifdef UNIV_DEBUG |
| 201 |
struct Mtr_memo_contains { |
| 202 |
Mtr_memo_contains(const mtr_t *mtr, mtr_memo_type_t type) |
| 203 |
: m_mtr(mtr), m_type(type) {} |
| 204 |
|
| 205 |
/** Check if the object in the given slot is of the correct type |
| 206 |
and then check if it is contained in the mtr. |
| 207 |
@retval true if the object in the slot is not of required type. |
| 208 |
os is of the required type, but is not contained in the mtr. |
| 209 |
@retval false if the object in the slot is of the required type |
| 210 |
and it is contained in the mtr. */ |
| 211 |
bool operator()(mtr_memo_slot_t *slot) { |
| 212 |
if (slot->type != m_type) { |
| 213 |
return true; |
| 214 |
} |
| 215 |
return !mtr_memo_contains(m_mtr, slot->object, m_type); |
| 216 |
} |
| 217 |
|
| 218 |
private: |
| 219 |
const mtr_t *m_mtr; |
| 220 |
mtr_memo_type_t m_type; |
| 221 |
}; |
| 222 |
|
| 223 |
bool mtr_t::conflicts_with(const mtr_t *mtr2) const { |
| 224 |
Mtr_memo_contains check(mtr2, MTR_MEMO_MODIFY); |
| 225 |
Iterate<Mtr_memo_contains> iterator(check); |
| 226 |
|
| 227 |
bool conflict = !m_impl.m_memo.for_each_block_in_reverse(iterator); |
| 228 |
if (conflict) { |
| 229 |
print_memos(std::cout); |
| 230 |
mtr2->print_memos(std::cout); |
| 231 |
} |
| 232 |
return conflict; |
| 233 |
} |
| 234 |
#endif /* UNIV_DEBUG */ |
| 235 |
#endif /* !UNIV_HOTBACKUP */ |
| 236 |
|
| 237 |
/** Release latches and decrement the buffer fix count. |
| 238 |
@param[in] slot memo slot */ |
| 239 |
static void memo_slot_release(mtr_memo_slot_t *slot) { |
| 240 |
switch (slot->type) { |
| 241 |
#ifndef UNIV_HOTBACKUP |
| 242 |
buf_block_t *block; |
| 243 |
#endif /* !UNIV_HOTBACKUP */ |
| 244 |
|
| 245 |
case MTR_MEMO_BUF_FIX: |
| 246 |
case MTR_MEMO_PAGE_S_FIX: |
| 247 |
case MTR_MEMO_PAGE_SX_FIX: |
| 248 |
case MTR_MEMO_PAGE_X_FIX: |
| 249 |
#ifndef UNIV_HOTBACKUP |
| 250 |
block = reinterpret_cast<buf_block_t *>(slot->object); |
| 251 |
|
| 252 |
buf_page_release_latch(block, slot->type); |
| 253 |
/* The buf_page_release_latch(block,..) call was last action dereferencing |
| 254 |
the `block`, so we can unfix the `block` now, but not sooner.*/ |
| 255 |
buf_block_unfix(block); |
| 256 |
#endif /* !UNIV_HOTBACKUP */ |
| 257 |
break; |
| 258 |
|
| 259 |
case MTR_MEMO_S_LOCK: |
| 260 |
rw_lock_s_unlock(reinterpret_cast<rw_lock_t *>(slot->object)); |
| 261 |
break; |
| 262 |
|
| 263 |
case MTR_MEMO_SX_LOCK: |
| 264 |
rw_lock_sx_unlock(reinterpret_cast<rw_lock_t *>(slot->object)); |
| 265 |
break; |
| 266 |
|
| 267 |
case MTR_MEMO_X_LOCK: |
| 268 |
rw_lock_x_unlock(reinterpret_cast<rw_lock_t *>(slot->object)); |
| 269 |
break; |
| 270 |
|
| 271 |
#ifdef UNIV_DEBUG |
| 272 |
default: |
| 273 |
ut_ad(slot->type == MTR_MEMO_MODIFY); |
| 274 |
#endif /* UNIV_DEBUG */ |
| 275 |
} |
| 276 |
|
| 277 |
slot->object = nullptr; |
| 278 |
} |
| 279 |
|
| 280 |
/** Release the latches and blocks acquired by the mini-transaction. */ |
| 281 |
struct Release_all { |
| 282 |
/** @return true always. */ |
| 283 |
bool operator()(mtr_memo_slot_t *slot) const { |
| 284 |
if (slot->object != nullptr) { |
| 285 |
memo_slot_release(slot); |
| 286 |
} |
| 287 |
|
| 288 |
return true; |
| 289 |
} |
| 290 |
}; |
| 291 |
|
| 292 |
/** Check that all slots have been handled. */ |
| 293 |
struct Debug_check { |
| 294 |
/** @return true always. */ |
| 295 |
bool operator()(const mtr_memo_slot_t *slot) const { |
| 296 |
ut_a(slot->object == nullptr); |
| 297 |
return true; |
| 298 |
} |
| 299 |
}; |
| 300 |
|
| 301 |
#ifdef UNIV_DEBUG |
| 302 |
/** Assure that there are no slots that are latching any resources. Only buffer |
| 303 |
fixing a page is allowed. */ |
| 304 |
struct Debug_check_no_latching { |
| 305 |
/** @return true always. */ |
| 306 |
bool operator()(const mtr_memo_slot_t *slot) const { |
| 307 |
switch (slot->type) { |
| 308 |
case MTR_MEMO_BUF_FIX: |
| 309 |
break; |
| 310 |
default: |
| 311 |
ib::fatal(UT_LOCATION_HERE, ER_MTR_MSG_1, (int)slot->type); |
| 312 |
} |
| 313 |
return true; |
| 314 |
} |
| 315 |
}; |
| 316 |
#endif /* UNIV_DEBUG */ |
| 317 |
|
| 318 |
/** Add blocks modified by the mini-transaction to the flush list. */ |
| 319 |
struct Add_dirty_blocks_to_flush_list { |
| 320 |
/** Constructor. |
| 321 |
@param[in] start_lsn LSN of the first entry that was |
| 322 |
added to REDO by the MTR |
| 323 |
@param[in] end_lsn LSN after the last entry was |
| 324 |
added to REDO by the MTR |
| 325 |
@param[in,out] observer flush observer */ |
| 326 |
Add_dirty_blocks_to_flush_list(lsn_t start_lsn, lsn_t end_lsn, |
| 327 |
Flush_observer *observer); |
| 328 |
|
| 329 |
/** Add the modified page to the buffer flush list. */ |
| 330 |
void add_dirty_page_to_flush_list(mtr_memo_slot_t *slot) const { |
| 331 |
ut_ad(m_end_lsn > m_start_lsn || (m_end_lsn == 0 && m_start_lsn == 0)); |
| 332 |
|
| 333 |
#ifndef UNIV_HOTBACKUP |
| 334 |
buf_block_t *block; |
| 335 |
|
| 336 |
block = reinterpret_cast<buf_block_t *>(slot->object); |
| 337 |
|
| 338 |
buf_flush_note_modification(block, m_start_lsn, m_end_lsn, |
| 339 |
m_flush_observer); |
| 340 |
#endif /* !UNIV_HOTBACKUP */ |
| 341 |
} |
| 342 |
|
| 343 |
/** @return true always. */ |
| 344 |
bool operator()(mtr_memo_slot_t *slot) const { |
| 345 |
if (slot->object != nullptr) { |
| 346 |
if (slot->type == MTR_MEMO_PAGE_X_FIX || |
| 347 |
slot->type == MTR_MEMO_PAGE_SX_FIX) { |
| 348 |
add_dirty_page_to_flush_list(slot); |
| 349 |
|
| 350 |
} else if (slot->type == MTR_MEMO_BUF_FIX) { |
| 351 |
buf_block_t *block; |
| 352 |
block = reinterpret_cast<buf_block_t *>(slot->object); |
| 353 |
if (block->made_dirty_with_no_latch) { |
| 354 |
add_dirty_page_to_flush_list(slot); |
| 355 |
block->made_dirty_with_no_latch = false; |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
return true; |
| 361 |
} |
| 362 |
|
| 363 |
/** Mini-transaction REDO end LSN */ |
| 364 |
const lsn_t m_end_lsn; |
| 365 |
|
| 366 |
/** Mini-transaction REDO start LSN */ |
| 367 |
const lsn_t m_start_lsn; |
| 368 |
|
| 369 |
/** Flush observer */ |
| 370 |
Flush_observer *const m_flush_observer; |
| 371 |
}; |
| 372 |
|
| 373 |
/** Constructor. |
| 374 |
@param[in] start_lsn LSN of the first entry that was added |
| 375 |
to REDO by the MTR |
| 376 |
@param[in] end_lsn LSN after the last entry was added |
| 377 |
to REDO by the MTR |
| 378 |
@param[in,out] observer flush observer */ |
| 379 |
Add_dirty_blocks_to_flush_list::Add_dirty_blocks_to_flush_list( |
| 380 |
lsn_t start_lsn, lsn_t end_lsn, Flush_observer *observer) |
| 381 |
: m_end_lsn(end_lsn), m_start_lsn(start_lsn), m_flush_observer(observer) { |
| 382 |
/* Do nothing */ |
| 383 |
} |
| 384 |
|
| 385 |
class mtr_t::Command { |
| 386 |
public: |
| 387 |
/** Constructor. |
| 388 |
Takes ownership of the mtr->m_impl, is responsible for deleting it. |
| 389 |
@param[in,out] mtr Mini-transaction */ |
| 390 |
explicit Command(mtr_t *mtr) : m_locks_released() { init(mtr); } |
| 391 |
|
| 392 |
void init(mtr_t *mtr) { |
| 393 |
m_impl = &mtr->m_impl; |
| 394 |
m_sync = mtr->m_sync; |
| 395 |
} |
| 396 |
|
| 397 |
/** Destructor */ |
| 398 |
~Command() { ut_ad(m_impl == nullptr); } |
| 399 |
|
| 400 |
/** Write the redo log record, add dirty pages to the flush list and |
| 401 |
release the resources. */ |
| 402 |
void execute(); |
| 403 |
|
| 404 |
/** Add blocks modified in this mini-transaction to the flush list. */ |
| 405 |
void add_dirty_blocks_to_flush_list(lsn_t start_lsn, lsn_t end_lsn); |
| 406 |
|
| 407 |
/** Release both the latches and blocks used in the mini-transaction. */ |
| 408 |
void release_all(); |
| 409 |
|
| 410 |
/** Release the resources */ |
| 411 |
void release_resources(); |
| 412 |
|
| 413 |
private: |
| 414 |
#ifndef UNIV_HOTBACKUP |
| 415 |
/** Prepare to write the mini-transaction log to the redo log buffer. |
| 416 |
@return number of bytes to write in finish_write() */ |
| 417 |
ulint prepare_write(); |
| 418 |
#endif /* !UNIV_HOTBACKUP */ |
| 419 |
|
| 420 |
/** true if it is a sync mini-transaction. */ |
| 421 |
bool m_sync; |
| 422 |
|
| 423 |
/** The mini-transaction state. */ |
| 424 |
mtr_t::Impl *m_impl; |
| 425 |
|
| 426 |
/** Set to 1 after the user thread releases the latches. The log |
| 427 |
writer thread must wait for this to be set to 1. */ |
| 428 |
volatile ulint m_locks_released; |
| 429 |
}; |
| 430 |
|
| 431 |
/* Mode update matrix. The array is indexed as [old mode][new mode]. |
| 432 |
All new modes for a specific old mode are in one horizontal line. |
| 433 |
true : update to new mode |
| 434 |
false: ignore new mode |
| 435 |
A - MTR_LOG_ALL |
| 436 |
N - MTR_LOG_NONE |
| 437 |
NR - MTR_LOG_NO_REDO |
| 438 |
S - MTR_LOG_SHORT_INSERTS */ |
| 439 |
bool mtr_t::s_mode_update[MTR_LOG_MODE_MAX][MTR_LOG_MODE_MAX] = { |
| 440 |
/* | A N NR S */ |
| 441 |
/* A */ {false, true, true, true}, /* A is default and we allow to switch |
| 442 |
to all other modes. */ |
| 443 |
/* N */ {true, false, true, false}, /* For both A & NR, we can shortly |
| 444 |
switch to N and return back*/ |
| 445 |
/* NR*/ {false, true, false, false}, /* Default is NR when global redo is |
| 446 |
disabled. Allow to move to N */ |
| 447 |
/* S */ {true, false, false, false} /* Only allow return back to A after |
| 448 |
short switch from A to S */ |
| 449 |
}; |
| 450 |
#ifdef UNIV_DEBUG |
| 451 |
/* Mode update validity matrix. The array is indexed as [old mode][new mode]. */ |
| 452 |
bool mtr_t::s_mode_update_valid[MTR_LOG_MODE_MAX][MTR_LOG_MODE_MAX] = { |
| 453 |
/* | A N NR S */ |
| 454 |
/* A */ {true, true, true, true}, /* No assert case. */ |
| 455 |
|
| 456 |
/* N */ {true, true, true, true}, |
| 457 |
|
| 458 |
/* NR*/ {true, true, true, true}, /* We generally never return back from |
| 459 |
NR to A but need to allow for LOB |
| 460 |
restarting B-tree mtr. */ |
| 461 |
|
| 462 |
/* S */ {true, false, false, true} /* Short Insert state is set transiently |
| 463 |
and we don't expect N or NR switch. */ |
| 464 |
}; |
| 465 |
#endif /* UNIV_DEBUG */ |
| 466 |
|
| 467 |
#ifndef UNIV_HOTBACKUP |
| 468 |
mtr_t::Logging mtr_t::s_logging; |
| 469 |
#endif /* !UNIV_HOTBACKUP */ |
| 470 |
|
| 471 |
mtr_log_t mtr_t::set_log_mode(mtr_log_t mode) { |
| 472 |
ut_ad(mode < MTR_LOG_MODE_MAX); |
| 473 |
|
| 474 |
const auto old_mode = m_impl.m_log_mode; |
| 475 |
ut_ad(s_mode_update_valid[old_mode][mode]); |
| 476 |
|
| 477 |
#ifdef UNIV_DEBUG |
| 478 |
if (mode == MTR_LOG_NO_REDO && old_mode == MTR_LOG_ALL) { |
| 479 |
/* Should change to no redo mode before generating any redo. */ |
| 480 |
ut_ad(!has_any_log_record()); |
| 481 |
} |
| 482 |
#endif /* UNIV_DEBUG */ |
| 483 |
|
| 484 |
if (s_mode_update[old_mode][mode]) { |
| 485 |
m_impl.m_log_mode = mode; |
| 486 |
} |
| 487 |
|
| 488 |
#ifndef UNIV_HOTBACKUP |
| 489 |
/* If we are explicitly setting no logging, this mtr doesn't need |
| 490 |
logging and we can safely unmark it. */ |
| 491 |
if (mode == MTR_LOG_NO_REDO && mode == old_mode) { |
| 492 |
check_nolog_and_unmark(); |
| 493 |
m_impl.m_log_mode = mode; |
| 494 |
} |
| 495 |
#endif /* !UNIV_HOTBACKUP */ |
| 496 |
|
| 497 |
return old_mode; |
| 498 |
} |
| 499 |
|
| 500 |
#ifndef UNIV_HOTBACKUP |
| 501 |
/** Write the block contents to the REDO log */ |
| 502 |
struct mtr_write_log_t { |
| 503 |
/** Append a block to the redo log buffer. |
| 504 |
@return whether the appending should continue */ |
| 505 |
bool operator()(const mtr_buf_t::block_t *block) { |
| 506 |
lsn_t start_lsn; |
| 507 |
lsn_t end_lsn; |
| 508 |
|
| 509 |
ut_ad(block != nullptr); |
| 510 |
|
| 511 |
if (block->used() == 0) { |
| 512 |
return true; |
| 513 |
} |
| 514 |
|
| 515 |
start_lsn = m_lsn; |
| 516 |
|
| 517 |
end_lsn = |
| 518 |
log_buffer_write(*log_sys, block->begin(), block->used(), start_lsn); |
| 519 |
|
| 520 |
ut_a(end_lsn % OS_FILE_LOG_BLOCK_SIZE < |
| 521 |
OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE); |
| 522 |
|
| 523 |
m_left_to_write -= block->used(); |
| 524 |
|
| 525 |
if (m_left_to_write == 0 |
| 526 |
/* This write was up to the end of record group, |
| 527 |
the last record in group has been written. |
| 528 |
|
| 529 |
Therefore next group of records starts at m_lsn. |
| 530 |
We need to find out, if the next group is the first group, |
| 531 |
that starts in this log block. |
| 532 |
|
| 533 |
In such case we need to set first_rec_group. |
| 534 |
|
| 535 |
Now, we could have two cases: |
| 536 |
1. This group of log records has started in previous block |
| 537 |
to block containing m_lsn. |
| 538 |
2. This group of log records has started in the same block |
| 539 |
as block containing m_lsn. |
| 540 |
|
| 541 |
Only in case 1), the next group of records is the first group |
| 542 |
of log records in block containing m_lsn. */ |
| 543 |
&& m_handle.start_lsn / OS_FILE_LOG_BLOCK_SIZE != |
| 544 |
end_lsn / OS_FILE_LOG_BLOCK_SIZE) { |
| 545 |
log_buffer_set_first_record_group(*log_sys, end_lsn); |
| 546 |
} |
| 547 |
|
| 548 |
log_buffer_write_completed(*log_sys, start_lsn, end_lsn); |
| 549 |
|
| 550 |
m_lsn = end_lsn; |
| 551 |
|
| 552 |
return true; |
| 553 |
} |
| 554 |
|
| 555 |
Log_handle m_handle; |
| 556 |
lsn_t m_lsn; |
| 557 |
ulint m_left_to_write; |
| 558 |
}; |
| 559 |
#endif /* !UNIV_HOTBACKUP */ |
| 560 |
|
| 561 |
#ifdef UNIV_DEBUG |
| 562 |
thread_local ut::unordered_set<const mtr_t *> mtr_t::s_my_thread_active_mtrs; |
| 563 |
#endif |
| 564 |
|
| 565 |
void mtr_t::start(bool sync) { |
| 566 |
ut_ad(m_impl.m_state == MTR_STATE_INIT || |
| 567 |
m_impl.m_state == MTR_STATE_COMMITTED); |
| 568 |
|
| 569 |
UNIV_MEM_INVALID(this, sizeof(*this)); |
| 570 |
IF_DEBUG(UNIV_MEM_VALID(&m_restart_count, sizeof(m_restart_count));); |
| 571 |
|
| 572 |
UNIV_MEM_INVALID(&m_impl, sizeof(m_impl)); |
| 573 |
|
| 574 |
m_sync = sync; |
| 575 |
|
| 576 |
m_commit_lsn = 0; |
| 577 |
|
| 578 |
new (&m_impl.m_log) mtr_buf_t(); |
| 579 |
new (&m_impl.m_memo) mtr_buf_t(); |
| 580 |
|
| 581 |
m_impl.m_mtr = this; |
| 582 |
m_impl.m_log_mode = MTR_LOG_ALL; |
| 583 |
m_impl.m_inside_ibuf = false; |
| 584 |
m_impl.m_modifications = false; |
| 585 |
m_impl.m_n_log_recs = 0; |
| 586 |
m_impl.m_state = MTR_STATE_ACTIVE; |
| 587 |
m_impl.m_flush_observer = nullptr; |
| 588 |
m_impl.m_marked_nolog = false; |
| 589 |
|
| 590 |
#ifndef UNIV_HOTBACKUP |
| 591 |
check_nolog_and_mark(); |
| 592 |
#endif /* !UNIV_HOTBACKUP */ |
| 593 |
ut_d(m_impl.m_magic_n = MTR_MAGIC_N); |
| 594 |
|
| 595 |
#ifdef UNIV_DEBUG |
| 596 |
auto res = s_my_thread_active_mtrs.insert(this); |
| 597 |
/* Assert there are no collisions in thread local context - it would mean |
| 598 |
reusing MTR without committing or destructing it. */ |
| 599 |
ut_a(res.second); |
| 600 |
m_restart_count++; |
| 601 |
#endif /* UNIV_DEBUG */ |
| 602 |
} |
| 603 |
|
| 604 |
#ifndef UNIV_HOTBACKUP |
| 605 |
void mtr_t::check_nolog_and_mark() { |
| 606 |
/* Safe check to make this call idempotent. */ |
| 607 |
if (m_impl.m_marked_nolog) { |
| 608 |
return; |
| 609 |
} |
| 610 |
|
| 611 |
size_t shard_index = default_indexer_t<>::get_rnd_index(); |
| 612 |
m_impl.m_marked_nolog = s_logging.mark_mtr(shard_index); |
| 613 |
|
| 614 |
/* Disable redo logging by this mtr if logging is globally off. */ |
| 615 |
if (m_impl.m_marked_nolog) { |
| 616 |
ut_ad(m_impl.m_log_mode == MTR_LOG_ALL); |
| 617 |
m_impl.m_log_mode = MTR_LOG_NO_REDO; |
| 618 |
m_impl.m_shard_index = shard_index; |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
void mtr_t::check_nolog_and_unmark() { |
| 623 |
if (m_impl.m_marked_nolog) { |
| 624 |
s_logging.unmark_mtr(m_impl.m_shard_index); |
| 625 |
|
| 626 |
m_impl.m_marked_nolog = false; |
| 627 |
m_impl.m_shard_index = 0; |
| 628 |
|
| 629 |
if (m_impl.m_log_mode == MTR_LOG_NO_REDO) { |
| 630 |
/* Reset back to default mode. */ |
| 631 |
m_impl.m_log_mode = MTR_LOG_ALL; |
| 632 |
} |
| 633 |
} |
| 634 |
} |
| 635 |
#endif /* !UNIV_HOTBACKUP */ |
| 636 |
|
| 637 |
/** Release the resources */ |
| 638 |
void mtr_t::Command::release_resources() { |
| 639 |
ut_ad(m_impl->m_magic_n == MTR_MAGIC_N); |
| 640 |
|
| 641 |
/* Currently only used in commit */ |
| 642 |
ut_ad(m_impl->m_state == MTR_STATE_COMMITTING); |
| 643 |
|
| 644 |
#ifdef UNIV_DEBUG |
| 645 |
Debug_check release; |
| 646 |
Iterate<Debug_check> iterator(release); |
| 647 |
|
| 648 |
m_impl->m_memo.for_each_block_in_reverse(iterator); |
| 649 |
#endif /* UNIV_DEBUG */ |
| 650 |
|
| 651 |
/* Reset the mtr buffers */ |
| 652 |
m_impl->m_log.erase(); |
| 653 |
|
| 654 |
m_impl->m_memo.erase(); |
| 655 |
|
| 656 |
m_impl->m_state = MTR_STATE_COMMITTED; |
| 657 |
|
| 658 |
m_impl = nullptr; |
| 659 |
} |
| 660 |
|
| 661 |
/** Commit a mini-transaction. */ |
| 662 |
void mtr_t::commit() { |
| 663 |
ut_ad(is_active()); |
| 664 |
ut_ad(!is_inside_ibuf()); |
| 665 |
ut_ad(m_impl.m_magic_n == MTR_MAGIC_N); |
| 666 |
m_impl.m_state = MTR_STATE_COMMITTING; |
| 667 |
|
| 668 |
DBUG_EXECUTE_IF("mtr_commit_crash", DBUG_SUICIDE();); |
| 669 |
|
| 670 |
Command cmd(this); |
| 671 |
|
| 672 |
if (has_any_log_record() || |
| 673 |
(has_modifications() && m_impl.m_log_mode == MTR_LOG_NO_REDO)) { |
| 674 |
ut_ad(!srv_read_only_mode || m_impl.m_log_mode == MTR_LOG_NO_REDO); |
| 675 |
|
| 676 |
cmd.execute(); |
| 677 |
} else { |
| 678 |
cmd.release_all(); |
| 679 |
cmd.release_resources(); |
| 680 |
} |
| 681 |
#ifndef UNIV_HOTBACKUP |
| 682 |
check_nolog_and_unmark(); |
| 683 |
#endif /* !UNIV_HOTBACKUP */ |
| 684 |
|
| 685 |
ut_d(remove_from_debug_list()); |
| 686 |
} |
| 687 |
|
| 688 |
#ifdef UNIV_DEBUG |
| 689 |
void mtr_t::remove_from_debug_list() const { |
| 690 |
auto it = s_my_thread_active_mtrs.find(this); |
| 691 |
/* We have to find the MTR that is about to be committed in local context. We |
| 692 |
are not sharing MTRs between threads. */ |
| 693 |
ut_a(it != s_my_thread_active_mtrs.end()); |
| 694 |
s_my_thread_active_mtrs.erase(it); |
| 695 |
} |
| 696 |
|
| 697 |
void mtr_t::check_is_not_latching() const { |
| 698 |
Debug_check_no_latching release; |
| 699 |
Iterate<Debug_check_no_latching> iterator(release); |
| 700 |
|
| 701 |
m_impl.m_memo.for_each_block_in_reverse(iterator); |
| 702 |
} |
| 703 |
#endif /* UNIV_DEBUG */ |
| 704 |
|
| 705 |
#ifndef UNIV_HOTBACKUP |
| 706 |
|
| 707 |
/** Acquire a tablespace X-latch. |
| 708 |
NOTE: use mtr_x_lock_space(). |
| 709 |
@param[in] space tablespace instance |
| 710 |
@param[in] location location from where called */ |
| 711 |
void mtr_t::x_lock_space(fil_space_t *space, ut::Location location) { |
| 712 |
ut_ad(m_impl.m_magic_n == MTR_MAGIC_N); |
| 713 |
ut_ad(is_active()); |
| 714 |
|
| 715 |
x_lock(&space->latch, location); |
| 716 |
} |
| 717 |
|
| 718 |
/** Release an object in the memo stack. */ |
| 719 |
void mtr_t::memo_release(const void *object, ulint type) { |
| 720 |
ut_ad(m_impl.m_magic_n == MTR_MAGIC_N); |
| 721 |
ut_ad(is_active()); |
| 722 |
|
| 723 |
/* We cannot release a page that has been written to in the |
| 724 |
middle of a mini-transaction. */ |
| 725 |
ut_ad(!has_modifications() || type != MTR_MEMO_PAGE_X_FIX); |
| 726 |
|
| 727 |
Find find(object, type); |
| 728 |
Iterate<Find> iterator(find); |
| 729 |
|
| 730 |
if (!m_impl.m_memo.for_each_block_in_reverse(iterator)) { |
| 731 |
memo_slot_release(find.m_slot); |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
/** Release a page latch. |
| 736 |
@param[in] ptr pointer to within a page frame |
| 737 |
@param[in] type object type: MTR_MEMO_PAGE_X_FIX, ... */ |
| 738 |
void mtr_t::release_page(const void *ptr, mtr_memo_type_t type) { |
| 739 |
ut_ad(m_impl.m_magic_n == MTR_MAGIC_N); |
| 740 |
ut_ad(is_active()); |
| 741 |
|
| 742 |
/* We cannot release a page that has been written to in the |
| 743 |
middle of a mini-transaction. */ |
| 744 |
ut_ad(!has_modifications() || type != MTR_MEMO_PAGE_X_FIX); |
| 745 |
|
| 746 |
Find_page find(ptr, type); |
| 747 |
Iterate<Find_page> iterator(find); |
| 748 |
|
| 749 |
if (!m_impl.m_memo.for_each_block_in_reverse(iterator)) { |
| 750 |
memo_slot_release(find.get_slot()); |
| 751 |
return; |
| 752 |
} |
| 753 |
|
| 754 |
/* The page was not found! */ |
| 755 |
ut_d(ut_error); |
| 756 |
} |
| 757 |
|
| 758 |
/** Prepare to write the mini-transaction log to the redo log buffer. |
| 759 |
@return number of bytes to write in finish_write() */ |
| 760 |
ulint mtr_t::Command::prepare_write() { |
| 761 |
switch (m_impl->m_log_mode) { |
| 762 |
case MTR_LOG_SHORT_INSERTS: |
| 763 |
ut_d(ut_error); |
| 764 |
/* fall through (write no redo log) */ |
| 765 |
[[fallthrough]]; |
| 766 |
case MTR_LOG_NO_REDO: |
| 767 |
case MTR_LOG_NONE: |
| 768 |
ut_ad(m_impl->m_log.size() == 0); |
| 769 |
return 0; |
| 770 |
case MTR_LOG_ALL: |
| 771 |
break; |
| 772 |
default: |
| 773 |
ut_d(ut_error); |
| 774 |
ut_o(return 0); |
| 775 |
} |
| 776 |
|
| 777 |
/* An ibuf merge could happen when loading page to apply log |
| 778 |
records during recovery. During the ibuf merge mtr is used. */ |
| 779 |
|
| 780 |
ut_a(!recv_recovery_is_on() || !recv_no_ibuf_operations); |
| 781 |
|
| 782 |
ulint len = m_impl->m_log.size(); |
| 783 |
ut_ad(len > 0); |
| 784 |
|
| 785 |
const auto n_recs = m_impl->m_n_log_recs; |
| 786 |
ut_ad(n_recs > 0); |
| 787 |
|
| 788 |
ut_ad(log_sys != nullptr); |
| 789 |
|
| 790 |
/* This was not the first time of dirtying a |
| 791 |
tablespace since the latest checkpoint. */ |
| 792 |
|
| 793 |
if (n_recs <= 1) { |
| 794 |
ut_ad(n_recs == 1); |
| 795 |
|
| 796 |
/* Flag the single log record as the |
| 797 |
only record in this mini-transaction. */ |
| 798 |
|
| 799 |
*m_impl->m_log.front()->begin() |= MLOG_SINGLE_REC_FLAG; |
| 800 |
|
| 801 |
} else { |
| 802 |
/* Because this mini-transaction comprises |
| 803 |
multiple log records, append MLOG_MULTI_REC_END |
| 804 |
at the end. */ |
| 805 |
|
| 806 |
mlog_catenate_ulint(&m_impl->m_log, MLOG_MULTI_REC_END, MLOG_1BYTE); |
| 807 |
++len; |
| 808 |
} |
| 809 |
|
| 810 |
ut_ad(m_impl->m_log_mode == MTR_LOG_ALL); |
| 811 |
ut_ad(m_impl->m_log.size() == len); |
| 812 |
ut_ad(len > 0); |
| 813 |
|
| 814 |
return len; |
| 815 |
} |
| 816 |
#endif /* !UNIV_HOTBACKUP */ |
| 817 |
|
| 818 |
/** Release the latches and blocks acquired by this mini-transaction */ |
| 819 |
void mtr_t::Command::release_all() { |
| 820 |
Release_all release; |
| 821 |
Iterate<Release_all> iterator(release); |
| 822 |
|
| 823 |
m_impl->m_memo.for_each_block_in_reverse(iterator); |
| 824 |
|
| 825 |
/* Note that we have released the latches. */ |
| 826 |
m_locks_released = 1; |
| 827 |
} |
| 828 |
|
| 829 |
/** Add blocks modified in this mini-transaction to the flush list. */ |
| 830 |
void mtr_t::Command::add_dirty_blocks_to_flush_list(lsn_t start_lsn, |
| 831 |
lsn_t end_lsn) { |
| 832 |
Add_dirty_blocks_to_flush_list add_to_flush(start_lsn, end_lsn, |
| 833 |
m_impl->m_flush_observer); |
| 834 |
|
| 835 |
Iterate<Add_dirty_blocks_to_flush_list> iterator(add_to_flush); |
| 836 |
|
| 837 |
m_impl->m_memo.for_each_block_in_reverse(iterator); |
| 838 |
} |
| 839 |
|
| 840 |
/** Write the redo log record, add dirty pages to the flush list and release |
| 841 |
the resources. */ |
| 842 |
void mtr_t::Command::execute() { |
| 843 |
ut_ad(m_impl->m_log_mode != MTR_LOG_NONE); |
| 844 |
|
| 845 |
#ifndef UNIV_HOTBACKUP |
| 846 |
ulint len = prepare_write(); |
| 847 |
|
| 848 |
if (len > 0) { |
| 849 |
mtr_write_log_t write_log; |
| 850 |
|
| 851 |
write_log.m_left_to_write = len; |
| 852 |
|
| 853 |
auto handle = log_buffer_reserve(*log_sys, len); |
| 854 |
|
| 855 |
write_log.m_handle = handle; |
| 856 |
write_log.m_lsn = handle.start_lsn; |
| 857 |
|
| 858 |
m_impl->m_log.for_each_block(write_log); |
| 859 |
|
| 860 |
ut_ad(write_log.m_left_to_write == 0); |
| 861 |
ut_ad(write_log.m_lsn == handle.end_lsn); |
| 862 |
|
| 863 |
log_wait_for_space_in_log_recent_closed(*log_sys, handle.start_lsn); |
| 864 |
|
| 865 |
DEBUG_SYNC_C("mtr_redo_before_add_dirty_blocks"); |
| 866 |
|
| 867 |
add_dirty_blocks_to_flush_list(handle.start_lsn, handle.end_lsn); |
| 868 |
|
| 869 |
log_buffer_close(*log_sys, handle); |
| 870 |
|
| 871 |
m_impl->m_mtr->m_commit_lsn = handle.end_lsn; |
| 872 |
|
| 873 |
} else { |
| 874 |
DEBUG_SYNC_C("mtr_noredo_before_add_dirty_blocks"); |
| 875 |
|
| 876 |
add_dirty_blocks_to_flush_list(0, 0); |
| 877 |
} |
| 878 |
#endif /* !UNIV_HOTBACKUP */ |
| 879 |
|
| 880 |
release_all(); |
| 881 |
release_resources(); |
| 882 |
} |
| 883 |
|
| 884 |
std::ostream &mtr_memo_slot_t::print(std::ostream &out) const { |
| 885 |
buf_block_t *block = nullptr; |
| 886 |
if (!is_lock()) { |
| 887 |
block = reinterpret_cast<buf_block_t *>(object); |
| 888 |
} |
| 889 |
out << "[mtr_memo_slot_t: object=" << object << ", type=" << type << " (" |
| 890 |
<< mtr_memo_type(type) << "), page_id=" |
| 891 |
<< ((block == nullptr) ? page_id_t(FIL_NULL, FIL_NULL) |
| 892 |
: block->get_page_id()) |
| 893 |
<< "]" << std::endl; |
| 894 |
return out; |
| 895 |
} |
| 896 |
|
| 897 |
#ifndef UNIV_HOTBACKUP |
| 898 |
int mtr_t::Logging::enable(THD *thd) { |
| 899 |
if (is_enabled()) { |
| 900 |
return 0; |
| 901 |
} |
| 902 |
/* Allow mtrs to generate redo log. Concurrent clone and redo |
| 903 |
log archiving is still restricted till we reach a recoverable state. */ |
| 904 |
ut_ad(m_state.load() == DISABLED); |
| 905 |
m_state.store(ENABLED_RESTRICT); |
| 906 |
|
| 907 |
/* 1. Wait for all no-log mtrs to finish and add dirty pages to disk.*/ |
| 908 |
auto err = wait_no_log_mtr(thd); |
| 909 |
if (err != 0) { |
| 910 |
m_state.store(DISABLED); |
| 911 |
return err; |
| 912 |
} |
| 913 |
|
| 914 |
/* 2. Wait for dirty pages to flush by forcing checkpoint at current LSN. |
| 915 |
All no-logging page modification are done with the LSN when we stopped |
| 916 |
redo logging. We need to have one write mini-transaction after enabling redo |
| 917 |
to progress the system LSN and take a checkpoint. An easy way is to flush |
| 918 |
the max transaction ID which is generally done at TRX_SYS_TRX_ID_WRITE_MARGIN |
| 919 |
interval but safe to do any time. */ |
| 920 |
trx_sys_mutex_enter(); |
| 921 |
trx_sys_write_max_trx_id(); |
| 922 |
trx_sys_mutex_exit(); |
| 923 |
|
| 924 |
/* It would ensure that the modified page in previous mtr and all other |
| 925 |
pages modified before are flushed to disk. Since there could be large |
| 926 |
number of left over pages from LAD operation, we still don't enable |
| 927 |
double-write at this stage. */ |
| 928 |
log_make_latest_checkpoint(*log_sys); |
| 929 |
m_state.store(ENABLED_DBLWR); |
| 930 |
|
| 931 |
/* 3. Take another checkpoint after enabling double write to ensure any page |
| 932 |
being written without double write are already synced to disk. */ |
| 933 |
log_make_latest_checkpoint(*log_sys); |
| 934 |
|
| 935 |
/* 4. Mark that it is safe to recover from crash. */ |
| 936 |
log_persist_enable(*log_sys); |
| 937 |
|
| 938 |
ib::warn(ER_IB_WRN_REDO_ENABLED); |
| 939 |
m_state.store(ENABLED); |
| 940 |
|
| 941 |
return 0; |
| 942 |
} |
| 943 |
|
| 944 |
int mtr_t::Logging::disable(THD *) { |
| 945 |
if (is_disabled()) { |
| 946 |
return 0; |
| 947 |
} |
| 948 |
|
| 949 |
/* Disallow archiving to start. */ |
| 950 |
ut_ad(m_state.load() == ENABLED); |
| 951 |
m_state.store(ENABLED_RESTRICT); |
| 952 |
|
| 953 |
/* Check if redo log archiving is active. */ |
| 954 |
if (meb::redo_log_archive_is_active()) { |
| 955 |
m_state.store(ENABLED); |
| 956 |
my_error(ER_INNODB_REDO_ARCHIVING_ENABLED, MYF(0)); |
| 957 |
return ER_INNODB_REDO_ARCHIVING_ENABLED; |
| 958 |
} |
| 959 |
|
| 960 |
/* Concurrent clone operation is not supported. */ |
| 961 |
Clone_notify notifier(Clone_notify::Type::SYSTEM_REDO_DISABLE, |
| 962 |
dict_sys_t::s_invalid_space_id, false); |
| 963 |
if (notifier.failed()) { |
| 964 |
m_state.store(ENABLED); |
| 965 |
return notifier.get_error(); |
| 966 |
} |
| 967 |
|
| 968 |
/* Mark that it is unsafe to crash going forward. */ |
| 969 |
if (!srv_read_only_mode) { |
| 970 |
/* We need to check for read-only mode, because InnoDB might be |
| 971 |
restarted in read-only mode on log files for which redo logging |
| 972 |
has been disabled just before the crash. During runtime, InnoDB |
| 973 |
refuses to disable redo logging in read-only mode, but in such |
| 974 |
case, we must pretend redo logging is still disabled. It should |
| 975 |
not matter because in read-only, redo logging isn't used anyway. |
| 976 |
However to preserve old behaviour we call s_logging.disable in |
| 977 |
such case and we only prevent from calling log_persist_disable, |
| 978 |
which in the old code was no-op anyway in such case (read-only).*/ |
| 979 |
log_persist_disable(*log_sys); |
| 980 |
} else { |
| 981 |
ut_ad(srv_is_being_started); |
| 982 |
} |
| 983 |
|
| 984 |
ulonglong current_lsn = log_get_lsn(*log_sys); |
| 985 |
ib::warn(ER_IB_WRN_REDO_DISABLED_INFO, current_lsn); |
| 986 |
m_state.store(DISABLED); |
| 987 |
|
| 988 |
return 0; |
| 989 |
} |
| 990 |
|
| 991 |
int mtr_t::Logging::wait_no_log_mtr(THD *thd) { |
| 992 |
auto wait_cond = [&](bool, bool &result) { |
| 993 |
if (Counter::total(m_count_nologging_mtr) == 0) { |
| 994 |
result = false; |
| 995 |
return 0; |
| 996 |
} |
| 997 |
result = true; |
| 998 |
|
| 999 |
if (thd_killed(thd)) { |
| 1000 |
my_error(ER_QUERY_INTERRUPTED, MYF(0)); |
| 1001 |
return ER_QUERY_INTERRUPTED; |
| 1002 |
} |
| 1003 |
return 0; |
| 1004 |
}; |
| 1005 |
|
| 1006 |
/* Sleep for 1 millisecond */ |
| 1007 |
Clone_Msec sleep_time(10); |
| 1008 |
/* Generate alert message every 5 second. */ |
| 1009 |
Clone_Sec alert_interval(5); |
| 1010 |
/* Wait for 5 minutes. */ |
| 1011 |
Clone_Sec time_out(Clone_Min(5)); |
| 1012 |
|
| 1013 |
bool is_timeout = false; |
| 1014 |
auto err = Clone_Sys::wait(sleep_time, time_out, alert_interval, wait_cond, |
| 1015 |
nullptr, is_timeout); |
| 1016 |
|
| 1017 |
if (err == 0 && is_timeout) { |
| 1018 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 1019 |
"Innodb wait for no-log mtr timed out."); |
| 1020 |
err = ER_INTERNAL_ERROR; |
| 1021 |
ut_d(ut_error); |
| 1022 |
} |
| 1023 |
|
| 1024 |
return err; |
| 1025 |
} |
| 1026 |
|
| 1027 |
#ifdef UNIV_DEBUG |
| 1028 |
/** Check if memo contains the given item. |
| 1029 |
@return true if contains */ |
| 1030 |
bool mtr_t::memo_contains(const mtr_buf_t *memo, const void *object, |
| 1031 |
ulint type) { |
| 1032 |
Find find(object, type); |
| 1033 |
Iterate<Find> iterator(find); |
| 1034 |
|
| 1035 |
return !memo->for_each_block_in_reverse(iterator); |
| 1036 |
} |
| 1037 |
|
| 1038 |
/** Debug check for flags */ |
| 1039 |
struct FlaggedCheck { |
| 1040 |
FlaggedCheck(const void *ptr, ulint flags) : m_ptr(ptr), m_flags(flags) { |
| 1041 |
// Do nothing |
| 1042 |
} |
| 1043 |
|
| 1044 |
bool operator()(const mtr_memo_slot_t *slot) const { |
| 1045 |
if (m_ptr == slot->object && (m_flags & slot->type)) { |
| 1046 |
return false; |
| 1047 |
} |
| 1048 |
|
| 1049 |
return true; |
| 1050 |
} |
| 1051 |
|
| 1052 |
const void *m_ptr; |
| 1053 |
ulint m_flags; |
| 1054 |
}; |
| 1055 |
|
| 1056 |
/** Check if memo contains the given item. |
| 1057 |
@param ptr object to search |
| 1058 |
@param flags specify types of object (can be ORred) of |
| 1059 |
MTR_MEMO_PAGE_S_FIX ... values |
| 1060 |
@return true if contains */ |
| 1061 |
bool mtr_t::memo_contains_flagged(const void *ptr, ulint flags) const { |
| 1062 |
ut_ad(m_impl.m_magic_n == MTR_MAGIC_N); |
| 1063 |
ut_ad(is_committing() || is_active()); |
| 1064 |
|
| 1065 |
FlaggedCheck check(ptr, flags); |
| 1066 |
Iterate<FlaggedCheck> iterator(check); |
| 1067 |
|
| 1068 |
return !m_impl.m_memo.for_each_block_in_reverse(iterator); |
| 1069 |
} |
| 1070 |
|
| 1071 |
/** Check if memo contains the given page. |
| 1072 |
@param[in] ptr pointer to within buffer frame |
| 1073 |
@param[in] flags specify types of object with OR of |
| 1074 |
MTR_MEMO_PAGE_S_FIX... values |
| 1075 |
@return the block |
| 1076 |
@retval NULL if not found */ |
| 1077 |
buf_block_t *mtr_t::memo_contains_page_flagged(const byte *ptr, |
| 1078 |
ulint flags) const { |
| 1079 |
Find_page check(ptr, flags); |
| 1080 |
Iterate<Find_page> iterator(check); |
| 1081 |
|
| 1082 |
return m_impl.m_memo.for_each_block_in_reverse(iterator) ? nullptr |
| 1083 |
: check.get_block(); |
| 1084 |
} |
| 1085 |
|
| 1086 |
/** Mark the given latched page as modified. |
| 1087 |
@param[in] ptr pointer to within buffer frame */ |
| 1088 |
void mtr_t::memo_modify_page(const byte *ptr) { |
| 1089 |
buf_block_t *block = memo_contains_page_flagged( |
| 1090 |
ptr, MTR_MEMO_PAGE_X_FIX | MTR_MEMO_PAGE_SX_FIX); |
| 1091 |
ut_ad(block != nullptr); |
| 1092 |
|
| 1093 |
if (!memo_contains(get_memo(), block, MTR_MEMO_MODIFY)) { |
| 1094 |
memo_push(block, MTR_MEMO_MODIFY); |
| 1095 |
} |
| 1096 |
} |
| 1097 |
|
| 1098 |
/** Print info of an mtr handle. */ |
| 1099 |
void mtr_t::print() const { |
| 1100 |
ib::info(ER_IB_MSG_1275) << "Mini-transaction handle: memo size " |
| 1101 |
<< m_impl.m_memo.size() << " bytes log size " |
| 1102 |
<< get_log()->size() << " bytes"; |
| 1103 |
} |
| 1104 |
|
| 1105 |
lsn_t mtr_commit_mlog_test(size_t payload) { |
| 1106 |
constexpr size_t MAX_PAYLOAD_SIZE = 1024; |
| 1107 |
ut_a(payload <= MAX_PAYLOAD_SIZE); |
| 1108 |
|
| 1109 |
/* Create MLOG_TEST record in the memory. */ |
| 1110 |
byte record[MLOG_TEST_REC_OVERHEAD + MAX_PAYLOAD_SIZE]; |
| 1111 |
|
| 1112 |
byte *record_end = |
| 1113 |
Log_test::create_mlog_rec(record, 1, MLOG_TEST_VALUE, payload); |
| 1114 |
|
| 1115 |
const size_t rec_len = record_end - record; |
| 1116 |
|
| 1117 |
mtr_t mtr; |
| 1118 |
mtr_start(&mtr); |
| 1119 |
|
| 1120 |
/* Copy the created MLOG_TEST to mtr's local buffer. */ |
| 1121 |
byte *dst = nullptr; |
| 1122 |
bool success = mlog_open(&mtr, rec_len, dst); |
| 1123 |
ut_a(success); |
| 1124 |
std::memcpy(dst, record, rec_len); |
| 1125 |
mlog_close(&mtr, dst + rec_len); |
| 1126 |
|
| 1127 |
mtr.added_rec(); |
| 1128 |
|
| 1129 |
ut_ad(mtr.get_expected_log_size() == MLOG_TEST_REC_OVERHEAD + payload); |
| 1130 |
|
| 1131 |
mtr_commit(&mtr); |
| 1132 |
|
| 1133 |
return mtr.commit_lsn(); |
| 1134 |
} |
| 1135 |
|
| 1136 |
static void mtr_commit_mlog_test_filling_block_low(log_t &log, |
| 1137 |
size_t req_space_left, |
| 1138 |
size_t recursive_level) { |
| 1139 |
ut_a(req_space_left <= LOG_BLOCK_DATA_SIZE); |
| 1140 |
|
| 1141 |
/* Compute how much free space we have in current log block. */ |
| 1142 |
const lsn_t current_lsn = log_get_lsn(log); |
| 1143 |
size_t cur_space_left = OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE - |
| 1144 |
current_lsn % OS_FILE_LOG_BLOCK_SIZE; |
| 1145 |
|
| 1146 |
/* Subtract minimum space required for a single MLOG_TEST. */ |
| 1147 |
if (cur_space_left < MLOG_TEST_REC_OVERHEAD) { |
| 1148 |
/* Even the smallest MLOG_TEST was not fitting the left space, |
| 1149 |
so we will need to use the next log block too. */ |
| 1150 |
cur_space_left += LOG_BLOCK_DATA_SIZE; |
| 1151 |
} |
| 1152 |
cur_space_left -= MLOG_TEST_REC_OVERHEAD; |
| 1153 |
|
| 1154 |
/* Compute how big payload is required to leave exactly the provided |
| 1155 |
req_space_left bytes free in last block. */ |
| 1156 |
size_t payload; |
| 1157 |
if (cur_space_left < req_space_left) { |
| 1158 |
/* We requested to leave more free bytes, than we have currently |
| 1159 |
in the last block, we need to use the next log block. */ |
| 1160 |
payload = cur_space_left + LOG_BLOCK_DATA_SIZE - req_space_left; |
| 1161 |
} else { |
| 1162 |
payload = cur_space_left - req_space_left; |
| 1163 |
} |
| 1164 |
|
| 1165 |
/* Check if size of the record fits the maximum allowed size, which |
| 1166 |
is defined by the dyn_buf_t used in mtr_t (mtr_buf_t). */ |
| 1167 |
|
| 1168 |
if (MLOG_TEST_REC_OVERHEAD + payload <= mtr_buf_t::MAX_DATA_SIZE) { |
| 1169 |
mtr_commit_mlog_test(payload); |
| 1170 |
} else { |
| 1171 |
/* It does not fit, so we need to write as much as possible here, |
| 1172 |
but keep in mind that next record will need to take at least |
| 1173 |
MLOG_TEST_REC_OVERHEAD bytes. Fortunately the MAX_DATA_SIZE is |
| 1174 |
always at least twice larger than the MLOG_TEST_REC_OVERHEAD, |
| 1175 |
so the payload has to be larger than MLOG_TEST_REC_OVERHEAD. */ |
| 1176 |
static_assert(mtr_buf_t::MAX_DATA_SIZE >= MLOG_TEST_REC_OVERHEAD * 2); |
| 1177 |
ut_a(payload > MLOG_TEST_REC_OVERHEAD); |
| 1178 |
|
| 1179 |
/* Subtract space which we will occupy by usage of next record. |
| 1180 |
The remaining space is maximum we are allowed to occupy within |
| 1181 |
this record. */ |
| 1182 |
payload -= MLOG_TEST_REC_OVERHEAD; |
| 1183 |
|
| 1184 |
if (MLOG_TEST_REC_OVERHEAD + payload > mtr_buf_t::MAX_DATA_SIZE) { |
| 1185 |
/* We still cannot fit mtr_buf_t::MAX_DATA_SIZE bytes, so write |
| 1186 |
as much as possible within this record. */ |
| 1187 |
payload = mtr_buf_t::MAX_DATA_SIZE - MLOG_TEST_REC_OVERHEAD; |
| 1188 |
} |
| 1189 |
|
| 1190 |
/* Write this MLOG_TEST record. */ |
| 1191 |
mtr_commit_mlog_test(payload); |
| 1192 |
|
| 1193 |
/* Compute upper bound for maximum level of recursion that is ever possible. |
| 1194 |
This is to verify the guarantee that we don't go to deep. |
| 1195 |
|
| 1196 |
We do not want to depend on actual difference between the |
| 1197 |
mtr_buf_t::MAX_DATA_SIZE and LOG_BLOCK_DATA_SIZE. |
| 1198 |
|
| 1199 |
Note that mtr_buf_t::MAX_DATA_SIZE is the maximum size of log record we |
| 1200 |
could add. The LOG_BLOCK_DATA_SIZE consists of LOG_BLOCK_DATA_SIZE / |
| 1201 |
mtr_buf_t::MAX_DATA_SIZE records of mtr_buf_t::MAX_DATA_SIZE size each (0 if |
| 1202 |
MAX_DATA_SIZE is larger than the LOG_BLOCK_DATA_SIZE). If we shifted these |
| 1203 |
records then possibly 2 more records are required at boundaries (beginning |
| 1204 |
and ending) to cover the whole range. If the last record would not end at |
| 1205 |
proper offset, we decrease its payload. If we needed to move its end to even |
| 1206 |
smaller offset from beginning of log block than we reach with payload=0, |
| 1207 |
then we subtract up to MLOG_TEST_REC_OVERHEAD bytes from payload of previous |
| 1208 |
record, which is always possible because: |
| 1209 |
MAX_DATA_SIZE - MLOG_TEST_REC_OVERHEAD >= MLOG_TEST_REC_OVERHEAD. |
| 1210 |
|
| 1211 |
If the initial free space minus MLOG_TEST_REC_OVERHEAD is smaller than the |
| 1212 |
requested free space, then we need to move forward by at most |
| 1213 |
LOG_BLOCK_DATA_SIZE bytes. For that we need at most LOG_BLOCK_DATA_SIZE / |
| 1214 |
mtr_buf_t::MAX_DATA_SIZE + 2 records shifted in the way described above. |
| 1215 |
|
| 1216 |
This solution is reached by the loop of writing MAX_DATA_SIZE records until |
| 1217 |
the distance to target is <= MAX_DATA_SIZE + MLOG_TEST_REC_OVERHEAD, in |
| 1218 |
which case we adjust size of next record to end it exactly |
| 1219 |
MLOG_TEST_REC_OVERHEAD bytes before the target (this is why we subtract |
| 1220 |
MLOG_TEST_REC_OVERHEAD from payload). Then next recursive call will have an |
| 1221 |
easy task of adding record with payload=0. The loop mentioned above is |
| 1222 |
implemented by the recursion. */ |
| 1223 |
constexpr auto MAX_REC_N = |
| 1224 |
LOG_BLOCK_DATA_SIZE / mtr_buf_t::MAX_DATA_SIZE + 2; |
| 1225 |
|
| 1226 |
ut_a(recursive_level + 1 <= MAX_REC_N); |
| 1227 |
|
| 1228 |
/* Write next MLOG_TEST record(s). */ |
| 1229 |
mtr_commit_mlog_test_filling_block_low(log, req_space_left, |
| 1230 |
recursive_level + 1); |
| 1231 |
} |
| 1232 |
} |
| 1233 |
|
| 1234 |
void mtr_commit_mlog_test_filling_block(log_t &log, size_t req_space_left) { |
| 1235 |
mtr_commit_mlog_test_filling_block_low(log, req_space_left, 1); |
| 1236 |
} |
| 1237 |
|
| 1238 |
void mtr_t::wait_for_flush() { |
| 1239 |
ut_ad(commit_lsn() > 0); |
| 1240 |
log_write_up_to(*log_sys, commit_lsn(), true); |
| 1241 |
} |
| 1242 |
|
| 1243 |
#endif /* UNIV_DEBUG */ |
| 1244 |
#endif /* !UNIV_HOTBACKUP */ |