| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2012, Facebook Inc. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 8 |
Free Software Foundation. |
| 9 |
|
| 10 |
This program is designed to work with certain software (including |
| 11 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 12 |
as designated in a particular file or component or in included license |
| 13 |
documentation. The authors of MySQL hereby grant you an additional |
| 14 |
permission to link the program and your derivative works with the |
| 15 |
separately licensed software that they have either included with |
| 16 |
the program or referenced in the documentation. |
| 17 |
|
| 18 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 19 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 20 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 21 |
for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License along with |
| 24 |
this program; if not, write to the Free Software Foundation, Inc., |
| 25 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 |
|
| 27 |
*****************************************************************************/ |
| 28 |
|
| 29 |
/** @file include/mtr0mtr.h |
| 30 |
Mini-transaction buffer |
| 31 |
|
| 32 |
Created 11/26/1995 Heikki Tuuri |
| 33 |
*******************************************************/ |
| 34 |
|
| 35 |
#ifndef mtr0mtr_h |
| 36 |
#define mtr0mtr_h |
| 37 |
|
| 38 |
#include <stddef.h> |
| 39 |
|
| 40 |
#include "univ.i" |
| 41 |
|
| 42 |
#include "buf0types.h" |
| 43 |
#include "dyn0buf.h" |
| 44 |
#include "fil0fil.h" |
| 45 |
#include "mtr0types.h" |
| 46 |
#include "srv0srv.h" |
| 47 |
#include "trx0types.h" |
| 48 |
|
| 49 |
/** Start a mini-transaction. */ |
| 50 |
#define mtr_start(m) (m)->start() |
| 51 |
|
| 52 |
/** Start a synchronous mini-transaction */ |
| 53 |
#define mtr_start_sync(m) (m)->start(true) |
| 54 |
|
| 55 |
/** Start an asynchronous read-only mini-transaction */ |
| 56 |
#define mtr_start_ro(m) (m)->start(true, true) |
| 57 |
|
| 58 |
/** Commit a mini-transaction. */ |
| 59 |
#define mtr_commit(m) (m)->commit() |
| 60 |
|
| 61 |
/** Set and return a savepoint in mtr. |
| 62 |
@return savepoint */ |
| 63 |
#define mtr_set_savepoint(m) (m)->get_savepoint() |
| 64 |
|
| 65 |
/** Release the (index tree) s-latch stored in an mtr memo after a |
| 66 |
savepoint. */ |
| 67 |
#define mtr_release_s_latch_at_savepoint(m, s, l) \ |
| 68 |
(m)->release_s_latch_at_savepoint((s), (l)) |
| 69 |
|
| 70 |
/** Get the logging mode of a mini-transaction. |
| 71 |
@return logging mode: MTR_LOG_NONE, ... */ |
| 72 |
#define mtr_get_log_mode(m) (m)->get_log_mode() |
| 73 |
|
| 74 |
/** Change the logging mode of a mini-transaction. |
| 75 |
@return old mode */ |
| 76 |
#define mtr_set_log_mode(m, d) (m)->set_log_mode((d)) |
| 77 |
|
| 78 |
/** Get the flush observer of a mini-transaction. |
| 79 |
@return flush observer object */ |
| 80 |
#define mtr_get_flush_observer(m) (m)->get_flush_observer() |
| 81 |
|
| 82 |
/** Set the flush observer of a mini-transaction. */ |
| 83 |
#define mtr_set_flush_observer(m, d) (m)->set_flush_observer((d)) |
| 84 |
|
| 85 |
/** Read 1 - 4 bytes from a file page buffered in the buffer pool. |
| 86 |
@return value read */ |
| 87 |
#define mtr_read_ulint(p, t, m) (m)->read_ulint((p), (t)) |
| 88 |
|
| 89 |
/** Release an object in the memo stack. |
| 90 |
@return true if released */ |
| 91 |
#define mtr_memo_release(m, o, t) (m)->memo_release((o), (t)) |
| 92 |
|
| 93 |
#ifdef UNIV_DEBUG |
| 94 |
|
| 95 |
/** Check if memo contains the given item ignore if table is intrinsic |
| 96 |
@return true if contains or table is intrinsic. */ |
| 97 |
#define mtr_is_block_fix(m, o, t, table) \ |
| 98 |
(mtr_memo_contains(m, o, t) || table->is_intrinsic()) |
| 99 |
|
| 100 |
/** Check if memo contains the given page ignore if table is intrinsic |
| 101 |
@return true if contains or table is intrinsic. */ |
| 102 |
#define mtr_is_page_fix(m, p, t, table) \ |
| 103 |
(mtr_memo_contains_page(m, p, t) || table->is_intrinsic()) |
| 104 |
|
| 105 |
/** Check if memo contains the given item. |
| 106 |
@return true if contains */ |
| 107 |
#define mtr_memo_contains(m, o, t) (m)->memo_contains((m)->get_memo(), (o), (t)) |
| 108 |
|
| 109 |
/** Check if memo contains the given page. |
| 110 |
@return true if contains */ |
| 111 |
#define mtr_memo_contains_page(m, p, t) \ |
| 112 |
(m)->memo_contains_page_flagged((p), (t)) |
| 113 |
#endif /* UNIV_DEBUG */ |
| 114 |
|
| 115 |
/** Print info of an mtr handle. */ |
| 116 |
#define mtr_print(m) (m)->print() |
| 117 |
|
| 118 |
/** Return the log object of a mini-transaction buffer. |
| 119 |
@return log */ |
| 120 |
#define mtr_get_log(m) (m)->get_log() |
| 121 |
|
| 122 |
/** Push an object to an mtr memo stack. */ |
| 123 |
#define mtr_memo_push(m, o, t) (m)->memo_push(o, t) |
| 124 |
|
| 125 |
/** Lock an rw-lock in s-mode. */ |
| 126 |
#define mtr_s_lock(l, m, loc) (m)->s_lock((l), loc) |
| 127 |
|
| 128 |
/** Lock an rw-lock in x-mode. */ |
| 129 |
#define mtr_x_lock(l, m, loc) (m)->x_lock((l), loc) |
| 130 |
|
| 131 |
/** Lock a tablespace in x-mode. */ |
| 132 |
#define mtr_x_lock_space(s, m) (m)->x_lock_space((s), UT_LOCATION_HERE) |
| 133 |
|
| 134 |
/** Lock an rw-lock in sx-mode. */ |
| 135 |
#define mtr_sx_lock(l, m, loc) (m)->sx_lock((l), loc) |
| 136 |
|
| 137 |
#define mtr_memo_contains_flagged(m, p, l) (m)->memo_contains_flagged((p), (l)) |
| 138 |
|
| 139 |
#define mtr_memo_contains_page_flagged(m, p, l) \ |
| 140 |
(m)->memo_contains_page_flagged((p), (l)) |
| 141 |
|
| 142 |
#define mtr_release_block_at_savepoint(m, s, b) \ |
| 143 |
(m)->release_block_at_savepoint((s), (b)) |
| 144 |
|
| 145 |
#define mtr_block_sx_latch_at_savepoint(m, s, b) \ |
| 146 |
(m)->sx_latch_at_savepoint((s), (b)) |
| 147 |
|
| 148 |
#define mtr_block_x_latch_at_savepoint(m, s, b) \ |
| 149 |
(m)->x_latch_at_savepoint((s), (b)) |
| 150 |
|
| 151 |
/** Forward declaration of a tablespace object */ |
| 152 |
struct fil_space_t; |
| 153 |
|
| 154 |
/** Mini-transaction memo stack slot. */ |
| 155 |
struct mtr_memo_slot_t { |
| 156 |
/** Pointer to the object - either buf_block_t or rw_lock_t */ |
| 157 |
void *object; |
| 158 |
|
| 159 |
/** type of the stored object (MTR_MEMO_S_LOCK, ...) */ |
| 160 |
ulint type; |
| 161 |
|
| 162 |
/** Check if the object stored in this slot is a lock (rw_lock_t). |
| 163 |
@return true if it is a lock object, false otherwise. */ |
| 164 |
bool is_lock() const { |
| 165 |
return type == MTR_MEMO_S_LOCK || type == MTR_MEMO_X_LOCK || |
| 166 |
type == MTR_MEMO_SX_LOCK; |
| 167 |
} |
| 168 |
|
| 169 |
std::ostream &print(std::ostream &out) const; |
| 170 |
}; |
| 171 |
|
| 172 |
inline std::ostream &operator<<(std::ostream &out, const mtr_memo_slot_t &obj) { |
| 173 |
return obj.print(out); |
| 174 |
} |
| 175 |
|
| 176 |
/** Mini-transaction handle and buffer */ |
| 177 |
struct mtr_t { |
| 178 |
/** State variables of the mtr */ |
| 179 |
struct Impl { |
| 180 |
/** memo stack for locks etc. */ |
| 181 |
mtr_buf_t m_memo; |
| 182 |
|
| 183 |
/** mini-transaction log */ |
| 184 |
mtr_buf_t m_log; |
| 185 |
|
| 186 |
/** true if inside ibuf changes */ |
| 187 |
bool m_inside_ibuf; |
| 188 |
|
| 189 |
/** true if the mini-transaction might have modified buffer pool pages */ |
| 190 |
bool m_modifications; |
| 191 |
|
| 192 |
/** true if mtr is forced to NO_LOG mode because redo logging is |
| 193 |
disabled globally. In this case, mtr increments the global counter |
| 194 |
at ::start and must decrement it back at ::commit. */ |
| 195 |
bool m_marked_nolog; |
| 196 |
|
| 197 |
/** Shard index used for incrementing global counter at ::start. We need |
| 198 |
to use the same shard while decrementing counter at ::commit. */ |
| 199 |
size_t m_shard_index; |
| 200 |
|
| 201 |
/** Count of how many page initial log records have been |
| 202 |
written to the mtr log */ |
| 203 |
uint32_t m_n_log_recs; |
| 204 |
|
| 205 |
/** specifies which operations should be logged; default |
| 206 |
value MTR_LOG_ALL */ |
| 207 |
mtr_log_t m_log_mode; |
| 208 |
|
| 209 |
/** State of the transaction */ |
| 210 |
mtr_state_t m_state; |
| 211 |
|
| 212 |
/** Flush Observer */ |
| 213 |
Flush_observer *m_flush_observer; |
| 214 |
|
| 215 |
#ifdef UNIV_DEBUG |
| 216 |
/** For checking corruption. */ |
| 217 |
ulint m_magic_n; |
| 218 |
|
| 219 |
#endif /* UNIV_DEBUG */ |
| 220 |
|
| 221 |
/** Owning mini-transaction */ |
| 222 |
mtr_t *m_mtr; |
| 223 |
}; |
| 224 |
|
| 225 |
#ifndef UNIV_HOTBACKUP |
| 226 |
/** mtr global logging */ |
| 227 |
class Logging { |
| 228 |
public: |
| 229 |
/** mtr global redo logging state. |
| 230 |
Enable Logging : |
| 231 |
[ENABLED] -> [ENABLED_RESTRICT] -> [DISABLED] |
| 232 |
|
| 233 |
Disable Logging : |
| 234 |
[DISABLED] -> [ENABLED_RESTRICT] -> [ENABLED_DBLWR] -> [ENABLED] */ |
| 235 |
|
| 236 |
enum State : uint32_t { |
| 237 |
/* Redo Logging is enabled. Server is crash safe. */ |
| 238 |
ENABLED, |
| 239 |
/* Redo logging is enabled. All non-logging mtr are finished with the |
| 240 |
pages flushed to disk. Double write is enabled. Some pages could be |
| 241 |
still getting written to disk without double-write. Not safe to crash. */ |
| 242 |
ENABLED_DBLWR, |
| 243 |
/* Redo logging is enabled but there could be some mtrs still running |
| 244 |
in no logging mode. Redo archiving and clone are not allowed to start. |
| 245 |
No double-write */ |
| 246 |
ENABLED_RESTRICT, |
| 247 |
/* Redo logging is disabled and all new mtrs would not generate any redo. |
| 248 |
Redo archiving and clone are not allowed. */ |
| 249 |
DISABLED |
| 250 |
}; |
| 251 |
|
| 252 |
/** Initialize logging state at server start up. */ |
| 253 |
void init() { |
| 254 |
m_state.store(ENABLED); |
| 255 |
/* We use sharded counter and force sequentially consistent counting |
| 256 |
which is the general default for c++ atomic operation. If we try to |
| 257 |
optimize it further specific to current operations, we could use |
| 258 |
Release-Acquire ordering i.e. std::memory_order_release during counting |
| 259 |
and std::memory_order_acquire while checking for the count. However, |
| 260 |
sharding looks to be good enough for now and we should go for non default |
| 261 |
memory ordering only with some visible proof for improvement. */ |
| 262 |
m_count_nologging_mtr.set_order(std::memory_order_seq_cst); |
| 263 |
Counter::clear(m_count_nologging_mtr); |
| 264 |
} |
| 265 |
|
| 266 |
/** Disable mtr redo logging. Server is crash unsafe without logging. |
| 267 |
@param[in] thd server connection THD |
| 268 |
@return mysql error code. */ |
| 269 |
int disable(THD *thd); |
| 270 |
|
| 271 |
/** Enable mtr redo logging. Ensure that the server is crash safe |
| 272 |
before returning. |
| 273 |
@param[in] thd server connection THD |
| 274 |
@return mysql error code. */ |
| 275 |
int enable(THD *thd); |
| 276 |
|
| 277 |
/** Mark a no-logging mtr to indicate that it would not generate redo log |
| 278 |
and system is crash unsafe. |
| 279 |
@return true iff logging is disabled and mtr is marked. */ |
| 280 |
bool mark_mtr(size_t index) { |
| 281 |
/* Have initial check to avoid incrementing global counter for regular |
| 282 |
case when redo logging is enabled. */ |
| 283 |
if (is_disabled()) { |
| 284 |
/* Increment counter to restrict state change DISABLED to ENABLED. */ |
| 285 |
Counter::inc(m_count_nologging_mtr, index); |
| 286 |
|
| 287 |
/* Check if the no-logging is still disabled. At this point, if we |
| 288 |
find the state disabled, it is no longer possible for the state move |
| 289 |
back to enabled till the mtr finishes and we unmark the mtr. */ |
| 290 |
if (is_disabled()) { |
| 291 |
return (true); |
| 292 |
} |
| 293 |
Counter::dec(m_count_nologging_mtr, index); |
| 294 |
} |
| 295 |
return (false); |
| 296 |
} |
| 297 |
|
| 298 |
/** unmark a no logging mtr. */ |
| 299 |
void unmark_mtr(size_t index) { |
| 300 |
ut_ad(!is_enabled()); |
| 301 |
ut_ad(Counter::total(m_count_nologging_mtr) > 0); |
| 302 |
Counter::dec(m_count_nologging_mtr, index); |
| 303 |
} |
| 304 |
|
| 305 |
/* @return flush loop count for faster response when logging is disabled. */ |
| 306 |
uint32_t get_nolog_flush_loop() const { return (NOLOG_MAX_FLUSH_LOOP); } |
| 307 |
|
| 308 |
/** @return true iff redo logging is enabled and server is crash safe. */ |
| 309 |
bool is_enabled() const { return (m_state.load() == ENABLED); } |
| 310 |
|
| 311 |
/** @return true iff redo logging is disabled and new mtrs are not going |
| 312 |
to generate redo log. */ |
| 313 |
bool is_disabled() const { return (m_state.load() == DISABLED); } |
| 314 |
|
| 315 |
/** @return true iff we can skip data page double write. */ |
| 316 |
bool dblwr_disabled() const { |
| 317 |
auto state = m_state.load(); |
| 318 |
return (state == DISABLED || state == ENABLED_RESTRICT); |
| 319 |
} |
| 320 |
|
| 321 |
/* Force faster flush loop for quicker adaptive flush response when logging |
| 322 |
is disabled. When redo logging is disabled the system operates faster with |
| 323 |
dirty pages generated at much faster rate. */ |
| 324 |
static constexpr uint32_t NOLOG_MAX_FLUSH_LOOP = 5; |
| 325 |
|
| 326 |
private: |
| 327 |
/** Wait till all no-logging mtrs are finished. |
| 328 |
@return mysql error code. */ |
| 329 |
int wait_no_log_mtr(THD *thd); |
| 330 |
|
| 331 |
private: |
| 332 |
/** Global redo logging state. */ |
| 333 |
std::atomic<State> m_state; |
| 334 |
|
| 335 |
using Shards = Counter::Shards<128>; |
| 336 |
|
| 337 |
/** Number of no logging mtrs currently running. */ |
| 338 |
Shards m_count_nologging_mtr; |
| 339 |
}; |
| 340 |
|
| 341 |
/** Check if redo logging is disabled globally and mark |
| 342 |
the global counter till mtr ends. */ |
| 343 |
void check_nolog_and_mark(); |
| 344 |
|
| 345 |
/** Check if the mtr has marked the global no log counter and |
| 346 |
unmark it. */ |
| 347 |
void check_nolog_and_unmark(); |
| 348 |
#endif /* !UNIV_HOTBACKUP */ |
| 349 |
|
| 350 |
mtr_t() { |
| 351 |
m_impl.m_state = MTR_STATE_INIT; |
| 352 |
m_impl.m_marked_nolog = false; |
| 353 |
m_impl.m_shard_index = 0; |
| 354 |
} |
| 355 |
|
| 356 |
~mtr_t() { |
| 357 |
#ifdef UNIV_DEBUG |
| 358 |
switch (m_impl.m_state) { |
| 359 |
case MTR_STATE_ACTIVE: |
| 360 |
ut_ad(m_impl.m_memo.size() == 0); |
| 361 |
ut_d(remove_from_debug_list()); |
| 362 |
break; |
| 363 |
case MTR_STATE_INIT: |
| 364 |
case MTR_STATE_COMMITTED: |
| 365 |
break; |
| 366 |
case MTR_STATE_COMMITTING: |
| 367 |
ut_error; |
| 368 |
} |
| 369 |
#endif /* UNIV_DEBUG */ |
| 370 |
#ifndef UNIV_HOTBACKUP |
| 371 |
/* Safety check in case mtr is not committed. */ |
| 372 |
if (m_impl.m_state != MTR_STATE_INIT) { |
| 373 |
check_nolog_and_unmark(); |
| 374 |
} |
| 375 |
#endif /* !UNIV_HOTBACKUP */ |
| 376 |
} |
| 377 |
|
| 378 |
#ifdef UNIV_DEBUG |
| 379 |
/** Removed the MTR from the s_my_thread_active_mtrs list. */ |
| 380 |
void remove_from_debug_list() const; |
| 381 |
|
| 382 |
/** Assure that there are no slots that are latching any resources. Only |
| 383 |
buffer fixing a page is allowed. */ |
| 384 |
void check_is_not_latching() const; |
| 385 |
#endif /* UNIV_DEBUG */ |
| 386 |
|
| 387 |
/** Start a mini-transaction. |
| 388 |
@param sync true if it is a synchronous mini-transaction */ |
| 389 |
void start(bool sync = true); |
| 390 |
|
| 391 |
/** @return whether this is an asynchronous mini-transaction. */ |
| 392 |
bool is_async() const { return (!m_sync); } |
| 393 |
|
| 394 |
/** Request a future commit to be synchronous. */ |
| 395 |
void set_sync() { m_sync = true; } |
| 396 |
|
| 397 |
/** Commit the mini-transaction. */ |
| 398 |
void commit(); |
| 399 |
|
| 400 |
/** Return current size of the buffer. |
| 401 |
@return savepoint */ |
| 402 |
[[nodiscard]] ulint get_savepoint() const { |
| 403 |
ut_ad(is_active()); |
| 404 |
ut_ad(m_impl.m_magic_n == MTR_MAGIC_N); |
| 405 |
|
| 406 |
return (m_impl.m_memo.size()); |
| 407 |
} |
| 408 |
|
| 409 |
/** Release the (index tree) s-latch stored in an mtr memo after a |
| 410 |
savepoint. |
| 411 |
@param savepoint value returned by @see set_savepoint. |
| 412 |
@param lock latch to release */ |
| 413 |
inline void release_s_latch_at_savepoint(ulint savepoint, rw_lock_t *lock); |
| 414 |
|
| 415 |
/** Release the block in an mtr memo after a savepoint. */ |
| 416 |
inline void release_block_at_savepoint(ulint savepoint, buf_block_t *block); |
| 417 |
|
| 418 |
/** SX-latch a not yet latched block after a savepoint. */ |
| 419 |
inline void sx_latch_at_savepoint(ulint savepoint, buf_block_t *block); |
| 420 |
|
| 421 |
/** X-latch a not yet latched block after a savepoint. */ |
| 422 |
inline void x_latch_at_savepoint(ulint savepoint, buf_block_t *block); |
| 423 |
|
| 424 |
/** Get the logging mode. |
| 425 |
@return logging mode */ |
| 426 |
[[nodiscard]] inline mtr_log_t get_log_mode() const; |
| 427 |
|
| 428 |
/** Change the logging mode. |
| 429 |
@param mode logging mode |
| 430 |
@return old mode */ |
| 431 |
mtr_log_t set_log_mode(mtr_log_t mode); |
| 432 |
|
| 433 |
/** Read 1 - 4 bytes from a file page buffered in the buffer pool. |
| 434 |
@param ptr pointer from where to read |
| 435 |
@param type MLOG_1BYTE, MLOG_2BYTES, MLOG_4BYTES |
| 436 |
@return value read */ |
| 437 |
[[nodiscard]] inline uint32_t read_ulint(const byte *ptr, |
| 438 |
mlog_id_t type) const; |
| 439 |
|
| 440 |
/** Locks a rw-latch in S mode. |
| 441 |
NOTE: use mtr_s_lock(). |
| 442 |
@param lock rw-lock |
| 443 |
@param location location from where called */ |
| 444 |
inline void s_lock(rw_lock_t *lock, ut::Location location); |
| 445 |
|
| 446 |
/** Locks a rw-latch in X mode. |
| 447 |
NOTE: use mtr_x_lock(). |
| 448 |
@param lock rw-lock |
| 449 |
@param location location where name from where called */ |
| 450 |
inline void x_lock(rw_lock_t *lock, ut::Location location); |
| 451 |
|
| 452 |
/** Locks a rw-latch in X mode. |
| 453 |
NOTE: use mtr_sx_lock(). |
| 454 |
@param lock rw-lock |
| 455 |
@param location location from where called */ |
| 456 |
inline void sx_lock(rw_lock_t *lock, ut::Location location); |
| 457 |
|
| 458 |
/** Acquire a tablespace X-latch. |
| 459 |
NOTE: use mtr_x_lock_space(). |
| 460 |
@param[in] space tablespace instance |
| 461 |
@param[in] location location from where called */ |
| 462 |
void x_lock_space(fil_space_t *space, ut::Location location); |
| 463 |
|
| 464 |
/** Release an object in the memo stack. |
| 465 |
@param object object |
| 466 |
@param type object type: MTR_MEMO_S_LOCK, ... */ |
| 467 |
void memo_release(const void *object, ulint type); |
| 468 |
|
| 469 |
/** Release a page latch. |
| 470 |
@param[in] ptr pointer to within a page frame |
| 471 |
@param[in] type object type: MTR_MEMO_PAGE_X_FIX, ... */ |
| 472 |
void release_page(const void *ptr, mtr_memo_type_t type); |
| 473 |
|
| 474 |
/** Note that the mini-transaction might have modified a buffer pool page. |
| 475 |
As it's called from mlog_open(), which is called from fil_op_write_log() and |
| 476 |
perhaps other places which do not modify any page, this can be a false |
| 477 |
positive. */ |
| 478 |
void set_modified() { m_impl.m_modifications = true; } |
| 479 |
|
| 480 |
/** Checks if this mtr has modified any buffer pool page. |
| 481 |
It errs on the safe side: may return true even if it didn't modify any page. |
| 482 |
This is used in MTR_LOG_NO_REDO mode to detect that pages should be added to |
| 483 |
flush lists during commit() even though no redo log will be produced. |
| 484 |
@return true if the mini-transaction might have modified buffer pool pages. */ |
| 485 |
[[nodiscard]] bool has_modifications() const { |
| 486 |
return m_impl.m_modifications; |
| 487 |
} |
| 488 |
|
| 489 |
/** Get the LSN of commit(). |
| 490 |
@return the commit LSN |
| 491 |
@retval 0 if the transaction only modified temporary tablespaces or logging |
| 492 |
is disabled globally. */ |
| 493 |
[[nodiscard]] lsn_t commit_lsn() const { |
| 494 |
ut_ad(has_committed()); |
| 495 |
ut_ad(m_impl.m_log_mode == MTR_LOG_ALL); |
| 496 |
return (m_commit_lsn); |
| 497 |
} |
| 498 |
|
| 499 |
/** Note that we are inside the change buffer code. */ |
| 500 |
void enter_ibuf() { m_impl.m_inside_ibuf = true; } |
| 501 |
|
| 502 |
/** Note that we have exited from the change buffer code. */ |
| 503 |
void exit_ibuf() { m_impl.m_inside_ibuf = false; } |
| 504 |
|
| 505 |
/** @return true if we are inside the change buffer code */ |
| 506 |
[[nodiscard]] bool is_inside_ibuf() const { return (m_impl.m_inside_ibuf); } |
| 507 |
|
| 508 |
/* |
| 509 |
@return true if the mini-transaction is active */ |
| 510 |
[[nodiscard]] bool is_active() const { |
| 511 |
return (m_impl.m_state == MTR_STATE_ACTIVE); |
| 512 |
} |
| 513 |
|
| 514 |
/** Get flush observer |
| 515 |
@return flush observer */ |
| 516 |
[[nodiscard]] Flush_observer *get_flush_observer() const { |
| 517 |
return (m_impl.m_flush_observer); |
| 518 |
} |
| 519 |
|
| 520 |
/** Set flush observer |
| 521 |
@param[in] observer flush observer */ |
| 522 |
void set_flush_observer(Flush_observer *observer) { |
| 523 |
ut_ad(observer == nullptr || m_impl.m_log_mode == MTR_LOG_NO_REDO); |
| 524 |
|
| 525 |
m_impl.m_flush_observer = observer; |
| 526 |
} |
| 527 |
|
| 528 |
/** Print the memo objects (mtr_memo_slot_t) of mtr_t to the given output |
| 529 |
stream. |
| 530 |
@param[in] out the output stream for printing |
| 531 |
@return the output stream. */ |
| 532 |
std::ostream &print_memos(std::ostream &out) const; |
| 533 |
|
| 534 |
#ifdef UNIV_DEBUG |
| 535 |
/** Check if memo contains the given item. |
| 536 |
@param memo memo stack |
| 537 |
@param object object to search |
| 538 |
@param type type of object |
| 539 |
@return true if contains */ |
| 540 |
[[nodiscard]] static bool memo_contains(const mtr_buf_t *memo, |
| 541 |
const void *object, ulint type); |
| 542 |
|
| 543 |
/** Check if memo contains the given item. |
| 544 |
@param ptr object to search |
| 545 |
@param flags specify types of object (can be ORred) of |
| 546 |
MTR_MEMO_PAGE_S_FIX ... values |
| 547 |
@return true if contains */ |
| 548 |
[[nodiscard]] bool memo_contains_flagged(const void *ptr, ulint flags) const; |
| 549 |
|
| 550 |
/** Check if memo contains the given page. |
| 551 |
@param[in] ptr pointer to within buffer frame |
| 552 |
@param[in] flags specify types of object with OR of |
| 553 |
MTR_MEMO_PAGE_S_FIX... values |
| 554 |
@return the block |
| 555 |
@retval NULL if not found */ |
| 556 |
[[nodiscard]] buf_block_t *memo_contains_page_flagged(const byte *ptr, |
| 557 |
ulint flags) const; |
| 558 |
|
| 559 |
/** Mark the given latched page as modified. |
| 560 |
@param[in] ptr pointer to within buffer frame */ |
| 561 |
void memo_modify_page(const byte *ptr); |
| 562 |
|
| 563 |
/** Print info of an mtr handle. */ |
| 564 |
void print() const; |
| 565 |
|
| 566 |
/** @return true if the mini-transaction has committed */ |
| 567 |
[[nodiscard]] bool has_committed() const { |
| 568 |
return (m_impl.m_state == MTR_STATE_COMMITTED); |
| 569 |
} |
| 570 |
|
| 571 |
/** @return true if the mini-transaction is committing */ |
| 572 |
bool is_committing() const { |
| 573 |
return (m_impl.m_state == MTR_STATE_COMMITTING); |
| 574 |
} |
| 575 |
|
| 576 |
/** Check if the changes done in this mtr conflicts with changes done |
| 577 |
in the given mtr. Two mtrs are said to conflict with each other, if |
| 578 |
they modify the same buffer block. |
| 579 |
@param[in] mtr2 the given mtr. |
| 580 |
@return true if there is conflict, false otherwise. */ |
| 581 |
[[nodiscard]] bool conflicts_with(const mtr_t *mtr2) const; |
| 582 |
|
| 583 |
/** @return the memo stack */ |
| 584 |
[[nodiscard]] const mtr_buf_t *get_memo() const { return (&m_impl.m_memo); } |
| 585 |
|
| 586 |
/** @return the memo stack */ |
| 587 |
[[nodiscard]] mtr_buf_t *get_memo() { return (&m_impl.m_memo); } |
| 588 |
|
| 589 |
/** Computes the number of bytes that would be written to the redo |
| 590 |
log if mtr was committed right now (excluding headers of log blocks). |
| 591 |
@return number of bytes of the collected log records increased |
| 592 |
by 1 if MLOG_MULTI_REC_END would already be required */ |
| 593 |
size_t get_expected_log_size() const { |
| 594 |
return (m_impl.m_log.size() + (m_impl.m_n_log_recs > 1 ? 1 : 0)); |
| 595 |
} |
| 596 |
|
| 597 |
void wait_for_flush(); |
| 598 |
#endif /* UNIV_DEBUG */ |
| 599 |
|
| 600 |
/** Note that a record has been added to the log */ |
| 601 |
void added_rec() { ++m_impl.m_n_log_recs; } |
| 602 |
|
| 603 |
/** Checks if this mtr has generated any redo log records which should be |
| 604 |
written to the redo log during commit(). |
| 605 |
Note: If redo logging is disabled by set_log_mode(MTR_LOG_NONE) or |
| 606 |
set_log_mode(MTR_LOG_NO_REDO) or globally by s_logging.disable(..), then it |
| 607 |
will return false, even if set_modified() was called. |
| 608 |
Note: Redo log records can be generated for things other than page |
| 609 |
modifications, for example for tablespace rename, or other metadata updates. |
| 610 |
Note: Redo log records can be generated for modifications of pages which were |
| 611 |
already marked as dirty in BP. |
| 612 |
@return true iff there is at least one redo log record generated */ |
| 613 |
bool has_any_log_record() { return 0 < m_impl.m_n_log_recs; } |
| 614 |
|
| 615 |
/** Get the buffered redo log of this mini-transaction. |
| 616 |
@return redo log */ |
| 617 |
[[nodiscard]] const mtr_buf_t *get_log() const { |
| 618 |
ut_ad(m_impl.m_magic_n == MTR_MAGIC_N); |
| 619 |
|
| 620 |
return (&m_impl.m_log); |
| 621 |
} |
| 622 |
|
| 623 |
/** Get the buffered redo log of this mini-transaction. |
| 624 |
@return redo log */ |
| 625 |
[[nodiscard]] mtr_buf_t *get_log() { |
| 626 |
ut_ad(m_impl.m_magic_n == MTR_MAGIC_N); |
| 627 |
|
| 628 |
return (&m_impl.m_log); |
| 629 |
} |
| 630 |
|
| 631 |
/** Push an object to an mtr memo stack. |
| 632 |
@param object object |
| 633 |
@param type object type: MTR_MEMO_S_LOCK, ... */ |
| 634 |
inline void memo_push(void *object, mtr_memo_type_t type); |
| 635 |
|
| 636 |
#ifdef UNIV_DEBUG |
| 637 |
/** Iterate all MTRs created in this thread to assure they are not latching |
| 638 |
any resources. Violating this could lead to deadlocks under |
| 639 |
log_free_check(). */ |
| 640 |
static void check_my_thread_mtrs_are_not_latching() { |
| 641 |
for (auto &it : s_my_thread_active_mtrs) { |
| 642 |
it->check_is_not_latching(); |
| 643 |
} |
| 644 |
} |
| 645 |
/** This method is useful to detect if the thread is already inside an mtr. |
| 646 |
We should not do the log_free_check() in the child mtrs if a thread is |
| 647 |
already inside an mtr. |
| 648 |
@return true if thread is not inside an mtr, false otherwise */ |
| 649 |
static bool is_this_thread_inside_mtr() { |
| 650 |
return !s_my_thread_active_mtrs.empty(); |
| 651 |
} |
| 652 |
#endif |
| 653 |
|
| 654 |
/** Matrix to check if a mode update request should be ignored. */ |
| 655 |
static bool s_mode_update[MTR_LOG_MODE_MAX][MTR_LOG_MODE_MAX]; |
| 656 |
|
| 657 |
#ifdef UNIV_DEBUG |
| 658 |
/** For checking invalid mode update requests. */ |
| 659 |
static bool s_mode_update_valid[MTR_LOG_MODE_MAX][MTR_LOG_MODE_MAX]; |
| 660 |
|
| 661 |
/** Count the number of times the same mtr object has been committed and |
| 662 |
restarted. */ |
| 663 |
size_t m_restart_count{}; |
| 664 |
#endif /* UNIV_DEBUG */ |
| 665 |
|
| 666 |
#ifndef UNIV_HOTBACKUP |
| 667 |
/** Instance level logging information for all mtrs. */ |
| 668 |
static Logging s_logging; |
| 669 |
#endif /* !UNIV_HOTBACKUP */ |
| 670 |
|
| 671 |
private: |
| 672 |
Impl m_impl; |
| 673 |
|
| 674 |
/** LSN at commit time */ |
| 675 |
lsn_t m_commit_lsn; |
| 676 |
|
| 677 |
/** true if it is synchronous mini-transaction */ |
| 678 |
bool m_sync; |
| 679 |
|
| 680 |
#ifdef UNIV_DEBUG |
| 681 |
/** List of all non-committed MTR instances created in this thread. Used for |
| 682 |
debug purposes in the log_free_check(). */ |
| 683 |
static thread_local ut::unordered_set<const mtr_t *> s_my_thread_active_mtrs; |
| 684 |
#endif |
| 685 |
|
| 686 |
class Command; |
| 687 |
|
| 688 |
friend class Command; |
| 689 |
}; |
| 690 |
|
| 691 |
#ifndef UNIV_HOTBACKUP |
| 692 |
#ifdef UNIV_DEBUG |
| 693 |
|
| 694 |
/** Reserves space in the log buffer and writes a single MLOG_TEST. |
| 695 |
@param[in] payload number of extra bytes within the record, |
| 696 |
not greater than 1024 |
| 697 |
@return end_lsn pointing to the first byte after the written record */ |
| 698 |
lsn_t mtr_commit_mlog_test(size_t payload = 0); |
| 699 |
|
| 700 |
/** Reserves space in the log buffer and writes a single MLOG_TEST. |
| 701 |
Adjusts size of the payload in the record, in order to fill the current |
| 702 |
block up to its boundary. If nothing else is happening in parallel, |
| 703 |
we could expect to see afterwards: |
| 704 |
(cur_lsn + space_left) % OS_FILE_LOG_BLOCK_SIZE == LOG_BLOCK_HDR_SIZE, |
| 705 |
where cur_lsn = log_get_lsn(log). |
| 706 |
@param[in,out] log redo log |
| 707 |
@param[in] space_left extra bytes left to the boundary of block, |
| 708 |
must be not greater than 496 */ |
| 709 |
void mtr_commit_mlog_test_filling_block(log_t &log, size_t space_left = 0); |
| 710 |
|
| 711 |
#endif /* UNIV_DEBUG */ |
| 712 |
#endif /* !UNIV_HOTBACKUP */ |
| 713 |
|
| 714 |
#include "mtr0mtr.ic" |
| 715 |
|
| 716 |
#endif /* mtr0mtr_h */ |