| 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 include/trx0sys.h |
| 29 |
Transaction system |
| 30 |
|
| 31 |
Created 3/26/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef trx0sys_h |
| 35 |
#define trx0sys_h |
| 36 |
|
| 37 |
#include "univ.i" |
| 38 |
|
| 39 |
#include "buf0buf.h" |
| 40 |
#include "fil0fil.h" |
| 41 |
#include "trx0types.h" |
| 42 |
#ifndef UNIV_HOTBACKUP |
| 43 |
#include "mem0mem.h" |
| 44 |
#include "mtr0mtr.h" |
| 45 |
#include "page0types.h" |
| 46 |
#include "ut0byte.h" |
| 47 |
#include "ut0class_life_cycle.h" |
| 48 |
#include "ut0guarded.h" |
| 49 |
#include "ut0lst.h" |
| 50 |
#include "ut0mutex.h" |
| 51 |
#endif /* !UNIV_HOTBACKUP */ |
| 52 |
#include <atomic> |
| 53 |
#include <unordered_map> |
| 54 |
#include <vector> |
| 55 |
#include "trx0trx.h" |
| 56 |
|
| 57 |
#ifndef UNIV_HOTBACKUP |
| 58 |
|
| 59 |
// Forward declaration |
| 60 |
class MVCC; |
| 61 |
class ReadView; |
| 62 |
|
| 63 |
/** The transaction system */ |
| 64 |
extern trx_sys_t *trx_sys; |
| 65 |
|
| 66 |
/** Checks if a page address is the trx sys header page. |
| 67 |
@param[in] page_id page id |
| 68 |
@return true if trx sys header page */ |
| 69 |
static inline bool trx_sys_hdr_page(const page_id_t &page_id); |
| 70 |
|
| 71 |
/** Creates and initializes the central memory structures for the transaction |
| 72 |
system. This is called when the database is started. |
| 73 |
@return min binary heap of rsegs to purge */ |
| 74 |
purge_pq_t *trx_sys_init_at_db_start(void); |
| 75 |
/** Creates the trx_sys instance and initializes purge_queue and mutex. */ |
| 76 |
void trx_sys_create(void); |
| 77 |
/** Creates and initializes the transaction system at the database creation. */ |
| 78 |
void trx_sys_create_sys_pages(void); |
| 79 |
|
| 80 |
/** Find the page number in the TRX_SYS page for a given slot/rseg_id |
| 81 |
@param[in] rseg_id slot number in the TRX_SYS page rseg array |
| 82 |
@return page number from the TRX_SYS page rseg array */ |
| 83 |
page_no_t trx_sysf_rseg_find_page_no(ulint rseg_id); |
| 84 |
|
| 85 |
/** Look for a free slot for a rollback segment in the trx system file copy. |
| 86 |
@param[in,out] mtr mtr |
| 87 |
@return slot index or ULINT_UNDEFINED if not found */ |
| 88 |
ulint trx_sysf_rseg_find_free(mtr_t *mtr); |
| 89 |
|
| 90 |
/** Gets a pointer to the transaction system file copy and x-locks its page. |
| 91 |
@return pointer to system file copy, page x-locked */ |
| 92 |
static inline trx_sysf_t *trx_sysf_get(mtr_t *mtr); /*!< in: mtr */ |
| 93 |
|
| 94 |
/** Gets the space of the nth rollback segment slot in the trx system |
| 95 |
file copy. |
| 96 |
@param[in] sys_header trx sys file copy |
| 97 |
@param[in] i slot index == rseg id |
| 98 |
@param[in] mtr mtr |
| 99 |
@return space id */ |
| 100 |
static inline space_id_t trx_sysf_rseg_get_space(trx_sysf_t *sys_header, |
| 101 |
ulint i, mtr_t *mtr); |
| 102 |
|
| 103 |
/** Gets the page number of the nth rollback segment slot in the trx system |
| 104 |
file copy. |
| 105 |
@param[in] sys_header trx sys file copy |
| 106 |
@param[in] i slot index == rseg id |
| 107 |
@param[in] mtr mtr |
| 108 |
@return page number, FIL_NULL if slot unused */ |
| 109 |
static inline page_no_t trx_sysf_rseg_get_page_no(trx_sysf_t *sys_header, |
| 110 |
ulint i, mtr_t *mtr); |
| 111 |
|
| 112 |
/** Sets the space id of the nth rollback segment slot in the trx system |
| 113 |
file copy. |
| 114 |
@param[in] sys_header trx sys file copy |
| 115 |
@param[in] i slot index == rseg id |
| 116 |
@param[in] space space id |
| 117 |
@param[in] mtr mtr */ |
| 118 |
static inline void trx_sysf_rseg_set_space(trx_sysf_t *sys_header, ulint i, |
| 119 |
space_id_t space, mtr_t *mtr); |
| 120 |
|
| 121 |
/** Set the page number of the nth rollback segment slot in the trx system |
| 122 |
file copy. |
| 123 |
@param[in] sys_header trx sys file copy |
| 124 |
@param[in] i slot index == rseg id |
| 125 |
@param[in] page_no page number, FIL_NULL if the slot is reset to |
| 126 |
unused |
| 127 |
@param[in] mtr mtr */ |
| 128 |
static inline void trx_sysf_rseg_set_page_no(trx_sysf_t *sys_header, ulint i, |
| 129 |
page_no_t page_no, mtr_t *mtr); |
| 130 |
|
| 131 |
/** Allocates a new transaction id (for trx->id). Before calling, |
| 132 |
the trx_sys_mutex must be acquired. |
| 133 |
@return new, allocated trx id */ |
| 134 |
inline trx_id_t trx_sys_allocate_trx_id(); |
| 135 |
|
| 136 |
/** Allocates a new transaction number (for trx->no). Before calling, |
| 137 |
the trx_sys_serialisation_mutex must be acquired. |
| 138 |
@return new, allocated trx no */ |
| 139 |
inline trx_id_t trx_sys_allocate_trx_no(); |
| 140 |
|
| 141 |
/** Retrieves a next value that will be allocated if trx_sys_allocate_trx_id() |
| 142 |
or trx_sys_allocate_trx_id_trx_no() was called. |
| 143 |
@return the next trx->id or trx->no that will be allocated */ |
| 144 |
inline trx_id_t trx_sys_get_next_trx_id_or_no(); |
| 145 |
|
| 146 |
#ifdef UNIV_DEBUG |
| 147 |
/* Flag to control TRX_RSEG_N_SLOTS behavior debugging. */ |
| 148 |
extern uint trx_rseg_n_slots_debug; |
| 149 |
#endif |
| 150 |
#endif /* !UNIV_HOTBACKUP */ |
| 151 |
|
| 152 |
/** Writes a trx id to an index page. In case that the id size changes in some |
| 153 |
future version, this function should be used instead of mach_write_... |
| 154 |
@param[in] ptr pointer to memory where written |
| 155 |
@param[in] id id */ |
| 156 |
static inline void trx_write_trx_id(byte *ptr, trx_id_t id); |
| 157 |
|
| 158 |
#ifndef UNIV_HOTBACKUP |
| 159 |
/** Reads a trx id from an index page. In case that the id size changes in |
| 160 |
some future version, this function should be used instead of |
| 161 |
mach_read_... |
| 162 |
@return id */ |
| 163 |
static inline trx_id_t trx_read_trx_id( |
| 164 |
const byte *ptr); /*!< in: pointer to memory from where to read */ |
| 165 |
|
| 166 |
/** Checks if a rw transaction with the given id is active. |
| 167 |
Please note, that positive result means only that the trx was active |
| 168 |
at some moment during the call, but it might have already become |
| 169 |
TRX_STATE_COMMITTED_IN_MEMORY before the call returns to the caller, as this |
| 170 |
transition is protected by trx->mutex and Trx_shard's mutex, but it is |
| 171 |
impossible for the caller to hold any of these mutexes when calling this |
| 172 |
function as the function itself internally acquires Trx_shard's mutex which |
| 173 |
would cause recurrent mutex acquisition if caller already had the same mutex, |
| 174 |
or latching order violation in case of holding trx->mutex. |
| 175 |
@param[in] trx_id trx id of the transaction |
| 176 |
@param[in] do_ref_count if true then increment the trx_t::n_ref_count |
| 177 |
@return transaction instance if active, or NULL; */ |
| 178 |
static inline trx_t *trx_rw_is_active(trx_id_t trx_id, bool do_ref_count); |
| 179 |
|
| 180 |
/** Persist transaction number limit below which all transaction GTIDs |
| 181 |
are persisted to disk table. |
| 182 |
@param[in] gtid_trx_no transaction number */ |
| 183 |
void trx_sys_persist_gtid_num(trx_id_t gtid_trx_no); |
| 184 |
|
| 185 |
/** @return oldest transaction number yet to be committed. */ |
| 186 |
trx_id_t trx_sys_oldest_trx_no(); |
| 187 |
|
| 188 |
/** Get a list of all binlog prepared transactions. |
| 189 |
@param[out] trx_ids all prepared transaction IDs. */ |
| 190 |
void trx_sys_get_binlog_prepared(std::vector<trx_id_t> &trx_ids); |
| 191 |
|
| 192 |
/** Get current binary log positions stored. |
| 193 |
@param[out] file binary log file name |
| 194 |
@param[out] offset binary log file offset */ |
| 195 |
void trx_sys_read_binlog_position(char *file, uint64_t &offset); |
| 196 |
|
| 197 |
/** Update binary log position if not already updated. This is called |
| 198 |
by clone to update any stale binary log position if any transaction |
| 199 |
is yet to update the binary log position in SE. |
| 200 |
@param[in] last_file last noted binary log file name |
| 201 |
@param[in] last_offset last noted binary log offset |
| 202 |
@param[in] file current binary log file name |
| 203 |
@param[in] offset current binary log file offset |
| 204 |
@return true, if binary log position is updated with current. */ |
| 205 |
bool trx_sys_write_binlog_position(const char *last_file, uint64_t last_offset, |
| 206 |
const char *file, uint64_t offset); |
| 207 |
|
| 208 |
/** Updates the offset information about the end of the MySQL binlog entry |
| 209 |
which corresponds to the transaction being committed, external XA transaction |
| 210 |
being prepared or rolled back. In a MySQL replication slave updates the latest |
| 211 |
master binlog position up to which replication has proceeded. |
| 212 |
@param[in] trx Current transaction |
| 213 |
@param[in,out] mtr Mini-transaction for update */ |
| 214 |
void trx_sys_update_mysql_binlog_offset(trx_t *trx, mtr_t *mtr); |
| 215 |
|
| 216 |
/** Shutdown/Close the transaction system. */ |
| 217 |
void trx_sys_close(void); |
| 218 |
|
| 219 |
/** Determine if there are incomplete transactions in the system. |
| 220 |
@return whether incomplete transactions need rollback */ |
| 221 |
static inline bool trx_sys_need_rollback(); |
| 222 |
|
| 223 |
/** Reads number of recovered transactions which have state |
| 224 |
equal to TRX_STATE_ACTIVE (so are not prepared transactions). |
| 225 |
@return number of active recovered transactions */ |
| 226 |
size_t trx_sys_recovered_active_trxs_count(); |
| 227 |
|
| 228 |
/** Validates lists of transactions at the very beginning of the |
| 229 |
pre-dd-shutdown phase. */ |
| 230 |
void trx_sys_before_pre_dd_shutdown_validate(); |
| 231 |
|
| 232 |
/** Validates lists of transactions at the very end of the |
| 233 |
pre-dd-shutdown phase. */ |
| 234 |
void trx_sys_after_pre_dd_shutdown_validate(); |
| 235 |
|
| 236 |
/** Validates lists of transactions after all background threads |
| 237 |
of InnoDB exited during shutdown of MySQL. */ |
| 238 |
void trx_sys_after_background_threads_shutdown_validate(); |
| 239 |
|
| 240 |
/** Add the transaction to the RW transaction set. |
| 241 |
@param trx transaction instance to add */ |
| 242 |
static inline void trx_sys_rw_trx_add(trx_t *trx); |
| 243 |
|
| 244 |
#endif /* !UNIV_HOTBACKUP */ |
| 245 |
|
| 246 |
#ifdef UNIV_DEBUG |
| 247 |
/** Validate the trx_sys_t::rw_trx_list. |
| 248 |
@return true if the list is valid */ |
| 249 |
bool trx_sys_validate_trx_list(); |
| 250 |
#endif /* UNIV_DEBUG */ |
| 251 |
|
| 252 |
/** Initialize trx_sys_undo_spaces, called once during srv_start(). */ |
| 253 |
void trx_sys_undo_spaces_init(); |
| 254 |
|
| 255 |
/** Free the resources occupied by trx_sys_undo_spaces, |
| 256 |
called once during thread de-initialization. */ |
| 257 |
void trx_sys_undo_spaces_deinit(); |
| 258 |
|
| 259 |
/** The automatically created system rollback segment has this id */ |
| 260 |
constexpr uint32_t TRX_SYS_SYSTEM_RSEG_ID = 0; |
| 261 |
|
| 262 |
/** The offset of the transaction system header on the page */ |
| 263 |
constexpr uint32_t TRX_SYS = FSEG_PAGE_DATA; |
| 264 |
|
| 265 |
/** Transaction system header */ |
| 266 |
/*------------------------------------------------------------- @{ */ |
| 267 |
/** the maximum trx id or trx number modulo TRX_SYS_TRX_ID_UPDATE_MARGIN written |
| 268 |
to a file page by any transaction; the assignment of transaction ids |
| 269 |
continues from this number rounded up by TRX_SYS_TRX_ID_UPDATE_MARGIN plus |
| 270 |
TRX_SYS_TRX_ID_UPDATE_MARGIN when the database is started */ |
| 271 |
constexpr uint32_t TRX_SYS_TRX_ID_STORE = 0; |
| 272 |
/** segment header for the tablespace segment the trx system is created into */ |
| 273 |
constexpr uint32_t TRX_SYS_FSEG_HEADER = 8; |
| 274 |
/** the start of the array of rollback segment specification slots */ |
| 275 |
constexpr uint32_t TRX_SYS_RSEGS = 8 + FSEG_HEADER_SIZE; |
| 276 |
/*------------------------------------------------------------- @} */ |
| 277 |
|
| 278 |
/* Originally, InnoDB defined TRX_SYS_N_RSEGS as 256 but created only one |
| 279 |
rollback segment. It initialized some arrays with this number of entries. |
| 280 |
We must remember this limit in order to keep file compatibility. */ |
| 281 |
constexpr size_t TRX_SYS_OLD_N_RSEGS = 256; |
| 282 |
|
| 283 |
/* The system temporary tablespace was originally allocated rseg_id slot |
| 284 |
numbers 1 through 32 in the TRX_SYS page. But those slots were not used |
| 285 |
because those Rollback segments were recreated at startup and after any |
| 286 |
crash. These slots are now used for redo-enabled rollback segments. |
| 287 |
The default number of rollback segments in the temporary tablespace |
| 288 |
remains the same. */ |
| 289 |
constexpr size_t TRX_SYS_OLD_TMP_RSEGS = 32; |
| 290 |
|
| 291 |
/** Maximum length of MySQL binlog file name, in bytes. */ |
| 292 |
constexpr uint32_t TRX_SYS_MYSQL_LOG_NAME_LEN = 512; |
| 293 |
/** Contents of TRX_SYS_MYSQL_LOG_MAGIC_N_FLD */ |
| 294 |
constexpr uint32_t TRX_SYS_MYSQL_LOG_MAGIC_N = 873422344; |
| 295 |
|
| 296 |
static_assert(UNIV_PAGE_SIZE_MIN >= 4096, "UNIV_PAGE_SIZE_MIN < 4096"); |
| 297 |
/** The offset of the MySQL binlog offset info in the trx system header */ |
| 298 |
#define TRX_SYS_MYSQL_LOG_INFO (UNIV_PAGE_SIZE - 1000) |
| 299 |
/** magic number which is TRX_SYS_MYSQL_LOG_MAGIC_N if we have valid data in the |
| 300 |
MySQL binlog info */ |
| 301 |
constexpr uint32_t TRX_SYS_MYSQL_LOG_MAGIC_N_FLD = 0; |
| 302 |
/** high 4 bytes of the offset within that file */ |
| 303 |
constexpr uint32_t TRX_SYS_MYSQL_LOG_OFFSET_HIGH = 4; |
| 304 |
/** low 4 bytes of the offset within that file */ |
| 305 |
constexpr uint32_t TRX_SYS_MYSQL_LOG_OFFSET_LOW = 8; |
| 306 |
/** MySQL log file name */ |
| 307 |
constexpr uint32_t TRX_SYS_MYSQL_LOG_NAME = 12; |
| 308 |
|
| 309 |
/** Reserve next 8 bytes for transaction number up to which GTIDs |
| 310 |
are persisted to table */ |
| 311 |
#define TRX_SYS_TRX_NUM_GTID \ |
| 312 |
(TRX_SYS_MYSQL_LOG_INFO + TRX_SYS_MYSQL_LOG_NAME + TRX_SYS_MYSQL_LOG_NAME_LEN) |
| 313 |
#define TRX_SYS_TRX_NUM_END (TRX_SYS_TRX_NUM_GTID + 8) |
| 314 |
/** Doublewrite buffer */ |
| 315 |
/** @{ */ |
| 316 |
/** The offset of the doublewrite buffer header on the trx system header page */ |
| 317 |
#define TRX_SYS_DOUBLEWRITE (UNIV_PAGE_SIZE - 200) |
| 318 |
/*-------------------------------------------------------------*/ |
| 319 |
/** fseg header of the fseg containing the doublewrite buffer */ |
| 320 |
constexpr uint32_t TRX_SYS_DOUBLEWRITE_FSEG = 0; |
| 321 |
/** 4-byte magic number which shows if we already have created the doublewrite |
| 322 |
buffer */ |
| 323 |
constexpr uint32_t TRX_SYS_DOUBLEWRITE_MAGIC = FSEG_HEADER_SIZE; |
| 324 |
/** page number of the first page in the first sequence of 64 (= |
| 325 |
FSP_EXTENT_SIZE) consecutive pages in the doublewrite buffer */ |
| 326 |
constexpr uint32_t TRX_SYS_DOUBLEWRITE_BLOCK1 = 4 + FSEG_HEADER_SIZE; |
| 327 |
/** page number of the first page in the second sequence of 64 consecutive pages |
| 328 |
in the doublewrite buffer */ |
| 329 |
constexpr uint32_t TRX_SYS_DOUBLEWRITE_BLOCK2 = 8 + FSEG_HEADER_SIZE; |
| 330 |
/** we repeat TRX_SYS_DOUBLEWRITE_MAGIC, TRX_SYS_DOUBLEWRITE_BLOCK1, |
| 331 |
TRX_SYS_DOUBLEWRITE_BLOCK2 so that if the trx sys header is half-written to |
| 332 |
disk, we still may be able to recover the information */ |
| 333 |
constexpr uint32_t TRX_SYS_DOUBLEWRITE_REPEAT = 12; |
| 334 |
/** If this is not yet set to TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED_N, |
| 335 |
we must reset the doublewrite buffer, because starting from 4.1.x the |
| 336 |
space id of a data page is stored into |
| 337 |
FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID. */ |
| 338 |
constexpr uint32_t TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED = 24 + FSEG_HEADER_SIZE; |
| 339 |
|
| 340 |
/*-------------------------------------------------------------*/ |
| 341 |
/** Contents of TRX_SYS_DOUBLEWRITE_MAGIC */ |
| 342 |
constexpr uint32_t TRX_SYS_DOUBLEWRITE_MAGIC_N = 536853855; |
| 343 |
/** Contents of TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED */ |
| 344 |
constexpr uint32_t TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED_N = 1783657386; |
| 345 |
|
| 346 |
/** Size of the doublewrite block in pages */ |
| 347 |
#define TRX_SYS_DOUBLEWRITE_BLOCK_SIZE FSP_EXTENT_SIZE |
| 348 |
/** @} */ |
| 349 |
|
| 350 |
/** List of undo tablespace IDs. */ |
| 351 |
class Space_Ids : public std::vector<space_id_t, ut::allocator<space_id_t>> { |
| 352 |
public: |
| 353 |
void sort() { std::sort(begin(), end()); } |
| 354 |
|
| 355 |
bool contains(space_id_t id) { |
| 356 |
if (size() == 0) { |
| 357 |
return (false); |
| 358 |
} |
| 359 |
|
| 360 |
iterator it = std::find(begin(), end(), id); |
| 361 |
|
| 362 |
return (it != end()); |
| 363 |
} |
| 364 |
|
| 365 |
iterator find(space_id_t id) { return (std::find(begin(), end(), id)); } |
| 366 |
}; |
| 367 |
|
| 368 |
/** Number of shards created for transactions. */ |
| 369 |
constexpr size_t TRX_SHARDS_N = 256; |
| 370 |
|
| 371 |
/** Computes shard number for a given trx_id. |
| 372 |
@param[in] trx_id trx_id for which shard_no should be computed |
| 373 |
@return the computed shard number (number in range 0..TRX_SHARDS_N-1) */ |
| 374 |
inline size_t trx_get_shard_no(trx_id_t trx_id) { |
| 375 |
ut_ad(trx_id != 0); |
| 376 |
return trx_id % TRX_SHARDS_N; |
| 377 |
} |
| 378 |
|
| 379 |
#ifndef UNIV_HOTBACKUP |
| 380 |
class Trx_by_id_with_min { |
| 381 |
struct Trx_track_hash { |
| 382 |
size_t operator()(const trx_id_t &key) const { |
| 383 |
return static_cast<size_t>(key / TRX_SHARDS_N); |
| 384 |
} |
| 385 |
}; |
| 386 |
|
| 387 |
using By_id = std::unordered_map<trx_id_t, trx_t *, Trx_track_hash>; |
| 388 |
By_id m_by_id; |
| 389 |
|
| 390 |
/** For observers which use Trx_shard::mutex protection: each transaction id |
| 391 |
in the m_by_id is guaranteed to be at least m_min_id. |
| 392 |
Writes are protected with Trx_shard::mutex. |
| 393 |
Reads can be performed without any latch before accessing m_by_id, |
| 394 |
but care must be taken to interpret the result - |
| 395 |
@see trx_rw_is_active for details.*/ |
| 396 |
std::atomic<trx_id_t> m_min_id{0}; |
| 397 |
|
| 398 |
public: |
| 399 |
By_id const &by_id() const { return m_by_id; } |
| 400 |
trx_id_t min_id() const { return m_min_id.load(); } |
| 401 |
trx_t *get(trx_id_t trx_id) const { |
| 402 |
const auto it = m_by_id.find(trx_id); |
| 403 |
trx_t *trx = it == m_by_id.end() ? nullptr : it->second; |
| 404 |
/* We remove trx from active_rw_trxs and change state to |
| 405 |
TRX_STATE_COMMITTED_IN_MEMORY in a same critical section protected by |
| 406 |
Trx_shard's mutex, which we happen to hold here, so we expect the state |
| 407 |
of trx to match its presence in that set */ |
| 408 |
ut_ad(trx == nullptr || !trx_state_eq(trx, TRX_STATE_COMMITTED_IN_MEMORY)); |
| 409 |
return trx; |
| 410 |
} |
| 411 |
void insert(trx_t &trx) { |
| 412 |
const trx_id_t trx_id = trx.id; |
| 413 |
ut_ad(0 == m_by_id.count(trx_id)); |
| 414 |
m_by_id.emplace(trx_id, &trx); |
| 415 |
if (m_by_id.size() == 1 || |
| 416 |
trx_id < m_min_id.load(std::memory_order_relaxed)) { |
| 417 |
m_min_id.store(trx_id, std::memory_order_release); |
| 418 |
} |
| 419 |
} |
| 420 |
void erase(trx_id_t trx_id) { |
| 421 |
ut_ad(1 == m_by_id.count(trx_id)); |
| 422 |
m_by_id.erase(trx_id); |
| 423 |
if (m_min_id.load(std::memory_order_relaxed) == trx_id) { |
| 424 |
// We want at most 1 release store, so we use a local variable for the |
| 425 |
// loop. |
| 426 |
trx_id_t new_min = trx_id + TRX_SHARDS_N; |
| 427 |
if (!m_by_id.empty()) { |
| 428 |
#ifdef UNIV_DEBUG |
| 429 |
// These asserts ensure while loop terminates: |
| 430 |
const trx_id_t some_id = m_by_id.begin()->first; |
| 431 |
ut_a(new_min <= some_id); |
| 432 |
ut_a((some_id - new_min) % TRX_SHARDS_N == 0); |
| 433 |
#endif /* UNIV_DEBUG */ |
| 434 |
while (m_by_id.count(new_min) == 0) { |
| 435 |
new_min += TRX_SHARDS_N; |
| 436 |
} |
| 437 |
} |
| 438 |
m_min_id.store(new_min, std::memory_order_release); |
| 439 |
} |
| 440 |
} |
| 441 |
}; |
| 442 |
|
| 443 |
/** Shard for subset of transactions. */ |
| 444 |
struct Trx_shard { |
| 445 |
/** Mapping from trx->id to trx of active rw transactions. |
| 446 |
The peek() interface can only be used safely for the min_id(). |
| 447 |
Use latch_and_execute() interface to access other members. */ |
| 448 |
ut::Cacheline_padded<ut::Guarded<Trx_by_id_with_min, LATCH_ID_TRX_SYS_SHARD>> |
| 449 |
active_rw_trxs; |
| 450 |
}; |
| 451 |
|
| 452 |
/** The transaction system central memory data structure. */ |
| 453 |
struct trx_sys_t { |
| 454 |
/* Members protected by neither trx_sys_t::mutex nor serialisation_mutex. */ |
| 455 |
char pad0[ut::INNODB_CACHE_LINE_SIZE]; |
| 456 |
|
| 457 |
/** @{ */ |
| 458 |
|
| 459 |
/** Multi version concurrency control manager */ |
| 460 |
|
| 461 |
MVCC *mvcc; |
| 462 |
|
| 463 |
/** Vector of pointers to rollback segments. These rsegs are iterated |
| 464 |
and added to the end under a read lock. They are deleted under a write |
| 465 |
lock while the vector is adjusted. They are created and destroyed in |
| 466 |
single-threaded mode. */ |
| 467 |
Rsegs rsegs; |
| 468 |
|
| 469 |
/** Vector of pointers to rollback segments within the temp tablespace; |
| 470 |
This vector is created and destroyed in single-threaded mode so it is not |
| 471 |
protected by any mutex because it is read-only during multi-threaded |
| 472 |
operation. */ |
| 473 |
Rsegs tmp_rsegs; |
| 474 |
|
| 475 |
/** Length of the TRX_RSEG_HISTORY list (update undo logs for committed |
| 476 |
transactions). */ |
| 477 |
std::atomic<uint64_t> rseg_history_len; |
| 478 |
|
| 479 |
/** @} */ |
| 480 |
|
| 481 |
/* Members protected by either trx_sys_t::mutex or serialisation_mutex. */ |
| 482 |
char pad1[ut::INNODB_CACHE_LINE_SIZE]; |
| 483 |
|
| 484 |
/** @{ */ |
| 485 |
|
| 486 |
/** The smallest number not yet assigned as a transaction id |
| 487 |
or transaction number. This is declared as atomic because it |
| 488 |
can be accessed without holding any mutex during AC-NL-RO |
| 489 |
view creation. When it is used for assignment of the trx->id, |
| 490 |
it is synchronized by the trx_sys_t::mutex. When it is used |
| 491 |
for assignment of the trx->no, it is synchronized by the |
| 492 |
trx_sys_t::serialisation_mutex. Note: it might be in parallel |
| 493 |
used for both trx->id and trx->no assignments (for different |
| 494 |
trx_t objects). */ |
| 495 |
std::atomic<trx_id_t> next_trx_id_or_no; |
| 496 |
|
| 497 |
/** @} */ |
| 498 |
|
| 499 |
/* Members protected by serialisation_mutex. */ |
| 500 |
char pad2[ut::INNODB_CACHE_LINE_SIZE]; |
| 501 |
|
| 502 |
/** @{ */ |
| 503 |
|
| 504 |
/** Mutex to protect serialisation_list. */ |
| 505 |
TrxSysMutex serialisation_mutex; |
| 506 |
|
| 507 |
/** Tracks minimal transaction id which has received trx->no, but has |
| 508 |
not yet finished commit for the mtr writing the trx commit. Protected |
| 509 |
by the serialisation_mutex. Ordered on the trx->no field. */ |
| 510 |
UT_LIST_BASE_NODE_T(trx_t, no_list) serialisation_list; |
| 511 |
|
| 512 |
#ifdef UNIV_DEBUG |
| 513 |
/** Max trx number of read-write transactions added for purge. */ |
| 514 |
trx_id_t rw_max_trx_no; |
| 515 |
#endif /* UNIV_DEBUG */ |
| 516 |
|
| 517 |
char pad3[ut::INNODB_CACHE_LINE_SIZE]; |
| 518 |
|
| 519 |
/* The minimum trx->no inside the serialisation_list. Protected by |
| 520 |
the serialisation_mutex. Might be read without the mutex. */ |
| 521 |
std::atomic<trx_id_t> serialisation_min_trx_no; |
| 522 |
|
| 523 |
/** @} */ |
| 524 |
|
| 525 |
/* Members protected by the trx_sys_t::mutex. */ |
| 526 |
char pad4[ut::INNODB_CACHE_LINE_SIZE]; |
| 527 |
|
| 528 |
/** @{ */ |
| 529 |
|
| 530 |
/** Mutex protecting most fields in this structure (the default one). */ |
| 531 |
TrxSysMutex mutex; |
| 532 |
|
| 533 |
char pad5[ut::INNODB_CACHE_LINE_SIZE]; |
| 534 |
|
| 535 |
/** List of active and committed in memory read-write transactions, sorted |
| 536 |
on trx id, biggest first. Recovered transactions are always on this list. */ |
| 537 |
UT_LIST_BASE_NODE_T(trx_t, trx_list) rw_trx_list; |
| 538 |
|
| 539 |
char pad6[ut::INNODB_CACHE_LINE_SIZE]; |
| 540 |
|
| 541 |
/** List of transactions created for MySQL. All user transactions are |
| 542 |
on mysql_trx_list. The rw_trx_list can contain system transactions and |
| 543 |
recovered transactions that will not be in the mysql_trx_list. |
| 544 |
Additionally, mysql_trx_list may contain transactions that have not yet |
| 545 |
been started in InnoDB. */ |
| 546 |
UT_LIST_BASE_NODE_T(trx_t, mysql_trx_list) mysql_trx_list; |
| 547 |
|
| 548 |
/** Array of Read write transaction IDs for MVCC snapshot. A ReadView would |
| 549 |
take a snapshot of these transactions whose changes are not visible to it. |
| 550 |
We should remove transactions from the list before committing in memory and |
| 551 |
releasing locks to ensure right order of removal and consistent snapshot. */ |
| 552 |
trx_ids_t rw_trx_ids; |
| 553 |
|
| 554 |
char pad7[ut::INNODB_CACHE_LINE_SIZE]; |
| 555 |
|
| 556 |
/** Mapping from transaction id to transaction instance. */ |
| 557 |
Trx_shard shards[TRX_SHARDS_N]; |
| 558 |
|
| 559 |
/** Number of transactions currently in the XA PREPARED state. */ |
| 560 |
ulint n_prepared_trx; |
| 561 |
|
| 562 |
/** True if XA PREPARED trxs are found. */ |
| 563 |
bool found_prepared_trx; |
| 564 |
|
| 565 |
/** @} */ |
| 566 |
|
| 567 |
char pad_after[ut::INNODB_CACHE_LINE_SIZE]; |
| 568 |
|
| 569 |
Trx_shard &get_shard_by_trx_id(trx_id_t trx_id) { |
| 570 |
return trx_sys->shards[trx_get_shard_no(trx_id)]; |
| 571 |
} |
| 572 |
template <typename F> |
| 573 |
auto latch_and_execute_with_active_trx(trx_id_t trx_id, F &&f, |
| 574 |
const ut::Location &loc) { |
| 575 |
return get_shard_by_trx_id(trx_id).active_rw_trxs.latch_and_execute( |
| 576 |
[&](Trx_by_id_with_min &trx_by_id_with_min) { |
| 577 |
return std::forward<F>(f)(trx_by_id_with_min.get(trx_id)); |
| 578 |
}, |
| 579 |
loc); |
| 580 |
} |
| 581 |
}; |
| 582 |
|
| 583 |
#endif /* !UNIV_HOTBACKUP */ |
| 584 |
|
| 585 |
/** A list of undo tablespace IDs found in the TRX_SYS page. |
| 586 |
This cannot be part of the trx_sys_t object because it is initialized before |
| 587 |
that object is created. These are the old type of undo tablespaces that do not |
| 588 |
have space_IDs in the reserved range nor contain an RSEG_ARRAY page. */ |
| 589 |
extern Space_Ids *trx_sys_undo_spaces; |
| 590 |
|
| 591 |
#ifndef UNIV_HOTBACKUP |
| 592 |
|
| 593 |
/** When a trx id which is zero modulo this number (which must be a power of |
| 594 |
two) is assigned, the field TRX_SYS_TRX_ID_STORE on the transaction system |
| 595 |
page is updated */ |
| 596 |
constexpr trx_id_t TRX_SYS_TRX_ID_WRITE_MARGIN = 256; |
| 597 |
|
| 598 |
/** Acquire the trx_sys->mutex. */ |
| 599 |
static inline void trx_sys_mutex_enter() { mutex_enter(&trx_sys->mutex); } |
| 600 |
|
| 601 |
/** Release the trx_sys->mutex. */ |
| 602 |
static inline void trx_sys_mutex_exit() { trx_sys->mutex.exit(); } |
| 603 |
|
| 604 |
#ifdef UNIV_DEBUG |
| 605 |
|
| 606 |
/** Test if trx_sys->mutex is owned. */ |
| 607 |
static inline bool trx_sys_mutex_own() { return trx_sys->mutex.is_owned(); } |
| 608 |
|
| 609 |
/** Test if trx_sys->serialisation_mutex is owned. */ |
| 610 |
static inline bool trx_sys_serialisation_mutex_own() { |
| 611 |
return trx_sys->serialisation_mutex.is_owned(); |
| 612 |
} |
| 613 |
#endif |
| 614 |
|
| 615 |
/** Acquire the trx_sys->serialisation_mutex. */ |
| 616 |
static inline void trx_sys_serialisation_mutex_enter() { |
| 617 |
mutex_enter(&trx_sys->serialisation_mutex); |
| 618 |
} |
| 619 |
|
| 620 |
/** Release the trx_sys->serialisation_mutex. */ |
| 621 |
static inline void trx_sys_serialisation_mutex_exit() { |
| 622 |
trx_sys->serialisation_mutex.exit(); |
| 623 |
} |
| 624 |
|
| 625 |
#endif /* !UNIV_HOTBACKUP */ |
| 626 |
|
| 627 |
#include "trx0sys.ic" |
| 628 |
|
| 629 |
#endif |