/***************************************************************************** Copyright (c) 1996, 2026, Oracle and/or its affiliates. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2.0, as published by the Free Software Foundation. This program is designed to work with certain software (including but not limited to OpenSSL) that is licensed under separate terms, as designated in a particular file or component or in included license documentation. The authors of MySQL hereby grant you an additional permission to link the program and your derivative works with the separately licensed software that they have either included with the program or referenced in the documentation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *****************************************************************************/ /** @file include/trx0sys.ic Transaction system Created 3/26/1996 Heikki Tuuri *******************************************************/ #include "data0type.h" #include "srv0srv.h" #include "trx0trx.h" #ifndef UNIV_HOTBACKUP #include "mtr0log.h" #include "my_dbug.h" /* The typedef for rseg slot in the file copy */ typedef byte trx_sysf_rseg_t; /* Rollback segment specification slot offsets */ /*-------------------------------------------------------------*/ /** space where the segment header is placed; starting with MySQL/InnoDB 5.1.7, this is UNIV_UNDEFINED if the slot is unused */ constexpr uint32_t TRX_SYS_RSEG_SPACE = 0; /** page number where the segment header is placed; this is FIL_NULL if the slot is unused */ constexpr uint32_t TRX_SYS_RSEG_PAGE_NO = 4; /*-------------------------------------------------------------*/ /* Size of a rollback segment specification slot */ constexpr uint32_t TRX_SYS_RSEG_SLOT_SIZE = 8; /** Writes the value of max_trx_id to the file based trx system header. */ void trx_sys_write_max_trx_id(void); /** Checks if a page address is the trx sys header page. @param[in] page_id page id @return true if trx sys header page */ static inline bool trx_sys_hdr_page(const page_id_t &page_id) { return (page_id.space() == TRX_SYS_SPACE && page_id.page_no() == TRX_SYS_PAGE_NO); } /** Gets a pointer to the transaction system header and x-latches its page. @return pointer to system header, page x-latched. */ static inline trx_sysf_t *trx_sysf_get(mtr_t *mtr) /*!< in: mtr */ { buf_block_t *block; trx_sysf_t *header; ut_ad(mtr); block = buf_page_get(page_id_t(TRX_SYS_SPACE, TRX_SYS_PAGE_NO), univ_page_size, RW_X_LATCH, UT_LOCATION_HERE, mtr); buf_block_dbg_add_level(block, SYNC_TRX_SYS_HEADER); header = TRX_SYS + buf_block_get_frame(block); return (header); } /** Gets the space of the nth rollback segment slot in the trx system file copy. @return space id */ static inline space_id_t trx_sysf_rseg_get_space( trx_sysf_t *sys_header, /*!< in: trx sys header */ ulint slot, /*!< in: slot index == rseg id */ mtr_t *mtr) /*!< in: mtr */ { ut_ad(sys_header); ut_ad(slot < TRX_SYS_N_RSEGS); return (mtr_read_ulint(sys_header + TRX_SYS_RSEGS + slot * TRX_SYS_RSEG_SLOT_SIZE + TRX_SYS_RSEG_SPACE, MLOG_4BYTES, mtr)); } /** Gets the page number of the nth rollback segment slot in the trx system header. @return page number, FIL_NULL if slot unused */ static inline page_no_t trx_sysf_rseg_get_page_no( trx_sysf_t *sys_header, /*!< in: trx system header */ ulint slot, /*!< in: slot index == rseg id */ mtr_t *mtr) /*!< in: mtr */ { ut_ad(sys_header); ut_ad(slot < TRX_SYS_N_RSEGS); return (mtr_read_ulint(sys_header + TRX_SYS_RSEGS + slot * TRX_SYS_RSEG_SLOT_SIZE + TRX_SYS_RSEG_PAGE_NO, MLOG_4BYTES, mtr)); } /** Sets the space id of the nth rollback segment slot in the trx system file copy. */ static inline void trx_sysf_rseg_set_space( trx_sysf_t *sys_header, /*!< in: trx sys file copy */ ulint slot, /*!< in: slot index == rseg id */ space_id_t space, /*!< in: space id */ mtr_t *mtr) /*!< in: mtr */ { ut_ad(sys_header); ut_ad(slot < TRX_SYS_N_RSEGS); mlog_write_ulint(sys_header + TRX_SYS_RSEGS + slot * TRX_SYS_RSEG_SLOT_SIZE + TRX_SYS_RSEG_SPACE, space, MLOG_4BYTES, mtr); } /** Sets the page number of the nth rollback segment slot in the trx system header. */ static inline void trx_sysf_rseg_set_page_no( trx_sysf_t *sys_header, /*!< in: trx sys header */ ulint slot, /*!< in: slot index == rseg id */ page_no_t page_no, /*!< in: page number, FIL_NULL if the slot is reset to unused */ mtr_t *mtr) /*!< in: mtr */ { ut_ad(sys_header); ut_ad(slot < TRX_SYS_N_RSEGS); mlog_write_ulint(sys_header + TRX_SYS_RSEGS + slot * TRX_SYS_RSEG_SLOT_SIZE + TRX_SYS_RSEG_PAGE_NO, page_no, MLOG_4BYTES, mtr); } #endif /* !UNIV_HOTBACKUP */ /** Writes a trx id to an index page. In case that the id size changes in some future version, this function should be used instead of mach_write_... */ static inline void trx_write_trx_id( byte *ptr, /*!< in: pointer to memory where written */ trx_id_t id) /*!< in: id */ { ut_ad(id > 0); mach_write_to_6(ptr, id); } #ifndef UNIV_HOTBACKUP /** Reads a trx id from an index page. In case that the id size changes in some future version, this function should be used instead of mach_read_... @return id */ static inline trx_id_t trx_read_trx_id( const byte *ptr) /*!< in: pointer to memory from where to read */ { return (mach_read_from_6(ptr)); } static inline trx_t *trx_rw_is_active(trx_id_t trx_id, bool do_ref_count) { /* Fast checking without Trx_shard::mutex to see if trx_id can't be in the Trx_shard::active_rw_trxs because it's too small. This works, because whenever we add or remove an element from Trx_by_id_with_min::m_by_id under Trx_shard::mutex, we also modify the m_min_id to be lower or equal to each of ids inside Trx_by_id_with_min::m_by_id in the same critical section. We assume that trx_id is an identifier of a transaction for which the call to trx_sys_rw_trx_add(trx) happened-before the current call to trx_rw_is_active(trx_id,..) - indeed in current implementation of InnoDB, the trx_id comes from: - a recovered transaction (for which trx_sys_rw_trx_add happened before crash) - a header of a row (stored there by an active transaction which must have finished a call to trx_sys_rw_trx_add before) - a prepared transaction (which must have called trx_sys_rw_trx_add before becoming PREPAREd) so clearly trx_sys_rw_trx_add() happened-before. Under this assumption, if trx_id < Trx_shard::min_id, then the value of Trx_shard::min_id is loaded from a store which must have occurred in a critical section protected by Trx_shard::mutex which happened-after the trx_sys_rw_trx_add(trx) for the trx with trx->id==trx_id - it could not be an earlier critical section, by our assumption, and it couldn't be stored during that particular trx_sys_rw_trx_add(trx) because of the invariant for min_id. Further, given that the loaded value is larger than trx_id, it must be the case that during that critical section the trx with trx->id==trx_id no longer belonged to Trx_shard::active_rw_trxs.m_by_id. In other words, this load of min_id synchronizes with a store, which happens-after Trx_shard::mutex acquisition of a critical section in which trx_id was not in active_rw_trxs.m_by_id. So, if this thread were to check the contents of active_rw_trxs.m_by_id after the if's condition evaluated to true, it would first have to acquire Trx_shard::mutex, which could only succeed *after* that critical section, thus it would not find trx_id in it for sure. NOTE: to appreciate why the assumption is important, observe, that if we call the trx_rw_is_active(trx_id,..) with trx_id for which trx_sys_rw_trx_add(trx) wasn't called yet, then it could happen, that trx_id+TRX_SHARDS_N was already assigned and added to Trx_shard::active_rw_trxs.m_by_id, and Trx_shard::min_id was set to larger than trx_id, so we decide to return nullptr, even though, if we were to repeat the call in just a moment we might get a different result if the trx_sys_rw_trx_add() for the trx_id happens meanwhile. */ ut_ad(trx_id < trx_sys_get_next_trx_id_or_no()); auto &shard = trx_sys->get_shard_by_trx_id(trx_id); if (trx_id < shard.active_rw_trxs.peek().min_id()) { return nullptr; } return trx_sys->latch_and_execute_with_active_trx( trx_id, [&](trx_t *trx) { if (trx != nullptr && do_ref_count) { trx_reference(trx); } return trx; }, UT_LOCATION_HERE); } inline trx_id_t trx_sys_get_trx_id_write_margin() { DBUG_EXECUTE_IF("trx_sys_low_trx_id_write_margin", return 8;); return TRX_SYS_TRX_ID_WRITE_MARGIN; } /** Allocates a new transaction id or transaction number. @return new, allocated trx id or trx no */ inline trx_id_t trx_sys_allocate_trx_id_or_no() { ut_ad(trx_sys_mutex_own() || trx_sys_serialisation_mutex_own()); trx_id_t trx_id = trx_sys->next_trx_id_or_no.fetch_add(1); if (trx_id % trx_sys_get_trx_id_write_margin() == 0) { /* Reserve the next range of trx_id values. This thread has acquired either the trx_sys_mutex or the trx_sys_serialisation_mutex. Therefore at least one of these two mutexes, is latched and it stays latched until the call to trx_sys_write_max_trx_id() is finished. Meanwhile other threads could be acquiring the other of these two mutexes, reserving more and more trx_id values, until TRX_SYS_TRX_ID_WRITE_MARGIN next values are reserved, when another trx_sys_write_max_trx_id() would be called. */ trx_sys_write_max_trx_id(); } return trx_id; } inline trx_id_t trx_sys_allocate_trx_id() { ut_ad(trx_sys_mutex_own()); return trx_sys_allocate_trx_id_or_no(); } inline trx_id_t trx_sys_allocate_trx_no() { ut_ad(trx_sys_serialisation_mutex_own()); return trx_sys_allocate_trx_id_or_no(); } /** Reads trx->no up to which all transactions have been serialised. @return minimum value which is still possibly not serialised */ static inline trx_id_t trx_get_serialisation_min_trx_no(void) { return (trx_sys->serialisation_min_trx_no.load()); } inline trx_id_t trx_sys_get_next_trx_id_or_no() { return trx_sys->next_trx_id_or_no.load(); } /** Determine if there are incomplete transactions in the system. @return whether incomplete transactions need rollback */ static inline bool trx_sys_need_rollback() { ulint n_trx; trx_sys_mutex_enter(); n_trx = UT_LIST_GET_LEN(trx_sys->rw_trx_list); ut_ad(n_trx >= trx_sys->n_prepared_trx); n_trx -= trx_sys->n_prepared_trx; trx_sys_mutex_exit(); return (n_trx > 0); } static inline void trx_sys_rw_trx_add(trx_t *trx) { const trx_id_t trx_id = trx->id; ut_ad(trx_id != 0); const auto trx_shard_no = trx_get_shard_no(trx_id); DBUG_EXECUTE_IF("trx_sys_rw_trx_add_rc", { if (trx_shard_no == 123 && (trx_id / TRX_SHARDS_N) % 3 == 0) { std::this_thread::sleep_for(std::chrono::seconds(1)); } }); trx_sys->shards[trx_shard_no].active_rw_trxs.latch_and_execute( [&](Trx_by_id_with_min &trx_by_id_with_min) { trx_by_id_with_min.insert(*trx); ut_ad(trx_by_id_with_min.min_id() <= trx_id); ut_ad(trx_by_id_with_min.min_id() % TRX_SHARDS_N == trx_shard_no); }, UT_LOCATION_HERE); } #endif /* !UNIV_HOTBACKUP */