MySQL 26.10.0
Source Code Documentation
transaction_cache.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
25#ifndef XA_TRANSACTION_CACHE_H_INCLUDED
26#define XA_TRANSACTION_CACHE_H_INCLUDED
27
28#include <sys/types.h>
29#include <functional>
30#include <memory>
31#include <mutex>
32#include <string>
33#include <utility>
34#include <vector>
35
36#include "lex_string.h"
37#include "map_helpers.h" // malloc_unordered_map
38#include "my_dbug.h"
39#include "my_inttypes.h"
40#include "my_sqlcommand.h"
41#include "sql/malloc_allocator.h" // Malloc_allocator
42#include "sql/psi_memory_key.h" // key_memory_xa_recovered_transactions
43#include "sql/xa.h" // XID
44#include "sql/xa_aux.h" // serialize_xid
45
46class Transaction_ctx;
47
48namespace xa {
49
50/**
51 @class Transaction_cache
52
53 Class responsible for managing a cache of `Transaction_ctx` objects
54 associated with XA transactions.
55
56 The cache is used during the recovery stage of an XA transaction.
57
58 @note this class is a singleton class.
59 */
61 public:
62 using transaction_ptr = std::shared_ptr<Transaction_ctx>;
63 using filter_predicate_t = std::function<bool(transaction_ptr const &)>;
64
65 virtual ~Transaction_cache() = default;
66
67 // Disallow copy/move semantics
72
73 /**
74 Transaction is marked in the cache as if it's recovered.
75 The method allows to sustain prepared transaction disconnection.
76
77 @param transaction
78 Pointer to Transaction object that is replaced.
79
80 @return operation result
81 @retval false success or a cache already contains XID_STATE
82 for this XID value
83 @retval true failure
84 */
85 static bool detach(Transaction_ctx *transaction);
86 /**
87 Remove information about transaction from a cache.
88
89 @param transaction Pointer to a Transaction_ctx that has to be removed
90 from a cache.
91 */
92 static void remove(Transaction_ctx *transaction);
93 /// Mark a transaction visible through SQL XA RECOVER without changing its SQL
94 /// XA state.
95 ///
96 /// This is used by XA PREPARE before the prepare GTID is made visible, and
97 /// again when SQL state changes to XA_PREPARED. The second call is
98 /// idempotent and preserves the ordinary prepared-implies-recover-visible
99 /// contract for paths without GTID externalization.
100 ///
101 /// @param transaction Transaction context to show in SQL XA RECOVER.
103 Transaction_ctx *transaction);
104 /// Mark a transaction finalized for SQL XA RECOVER without releasing its XID
105 /// reservation from the cache.
106 ///
107 /// This is used after XA COMMIT/XA ROLLBACK has completed in engines, but
108 /// before the statement GTID is made visible. The cache entry must remain
109 /// present for two reasons:
110 /// 1. To keep the XID reserved until per-XID cleanup, such as MDL context
111 /// backup deletion, is done.
112 /// 2. To keep the cached detached transaction identity available for
113 /// concurrent same-XID second-phase checks until final cleanup removes the
114 /// entry.
115 ///
116 /// @param transaction Transaction context to hide from SQL XA RECOVER.
117 static void mark_finalized_for_recover(Transaction_ctx *transaction);
118 /**
119 Inserts a transaction context identified by a given XID.
120
121 @param xid The XID of the transaction.
122 @param transaction The object containing the context of the transaction.
123
124 @return false if the pair was successfully inserted, true otherwise.
125 */
126 static bool insert(XID *xid, Transaction_ctx *transaction);
127 /**
128 Creates a new transaction context for the recovering transaction
129 identified by a given XID.
130
131 @param xid The XID of the transaction being recovered.
132
133 @return false if the pair was successfully inserted, true otherwise.
134 */
135 static bool insert(XID *xid);
136 /**
137 Searches the cache for the transaction context identified by the given
138 XID.
139
140 An additional filtering predicate can be provided, to allow for further
141 validations on values for mathching XID. The predicate is evaluated
142 while holding the necessary locks to ensure the validaty of the
143 `Transaction_ctx` shared pointer.
144
145 A non-null value is returned if and only if:
146
147 1. The value is found in the underlying map
148 2. The found value underlying XID
149 (`Transaction_ctx::xid_state()::get_xid()`) equals to the parameter
150 `xid`. This validation is necessary since the XID representation for
151 the key used in the underlying map isn't an exact match for the full
152 XID representation.
153 3. If a predicate parameter is provided, the evaluation of passing the
154 value as a predicate parameter must be `true`.
155
156 @param xid The XID of the transaction to search the context for.
157 @param filter A predicate to be evaluated when an value for `xid` is
158 found. If predicate returns false, the found element is
159 filtered out.
160
161 @return The transaction context if found and valid, nullptr otherwise.
162 */
163 static transaction_ptr find(XID *xid, filter_predicate_t filter = nullptr);
164 /// Retrieves XID snapshots visible through SQL XA RECOVER.
165 ///
166 /// Entries finalized for recover are skipped here, but remain in the cache
167 /// as XID reservations until final cleanup removes them.
168 ///
169 /// @return XID value snapshots for transactions visible through SQL
170 /// XA RECOVER.
171 static std::vector<XID> get_xids_visible_to_xa_recover();
172 /**
173 Initializes the transaction cache underlying resources.
174 */
175 static void initialize();
176 /**
177 Disposes of the transaction cache allocated resources.
178 */
179 static void dispose();
180
181 private:
183
184 /// Cached transaction context and its visibility through SQL XA RECOVER.
186 public:
187 /// @param transaction Transaction context stored by the cache entry.
188 /// @param visibility Whether the transaction is visible through SQL
189 /// XA RECOVER.
191 : m_transaction(std::move(transaction)),
192 m_xa_recover_visibility(visibility) {}
193
194 /// @return Transaction context stored by the cache entry.
195 const transaction_ptr &transaction() const { return m_transaction; }
196
197 /// @retval true The transaction can be listed by SQL XA RECOVER.
198 /// @retval false The transaction XID remains reserved, but hidden from SQL
199 /// XA RECOVER.
202 }
203
204 /// Mark visible through SQL XA RECOVER without changing SQL XA state.
207 }
208
209 /// Mark finalized for SQL XA RECOVER while keeping the XID reserved.
212 }
213
214 private:
217 };
218
220
221 /** A lock to serialize the access to `m_transaction_cache` */
223#ifdef HAVE_PSI_INTERFACE
224 /** The PSI key for the above lock */
226 /** The PSI configuration of the above lock and key */
228 {&m_key_LOCK_transaction_cache, "LOCK_transaction_cache",
230#endif
231 /** A map holding the cached transaction context, indexed by XID */
233
234 /**
235 Class constructor.
236
237 It's declared private since this class is a singleton class.
238 */
240
241 /**
242 Initialize a cache to store Transaction_ctx and a mutex to protect access
243 to the cache
244
245 @return The initialized class instance.
246 */
247 static Transaction_cache &instance();
248 /**
249 Creates a new transaction context for the transaction with the given
250 XID and adds it to the cache.
251
252 @param xid The XID of the transaction to create and add.
253 @param is_binlogged_arg Whether or not the transaction has already been
254 binlogged.
255 @param src The transaction context and info to be added to the newly
256 created cache item.
257
258 @return false if the pair was successfully inserted, true otherwise.
259 */
260 static bool create_and_insert_new_transaction(XID *xid, bool is_binlogged_arg,
261 const Transaction_ctx *src);
262};
263} // namespace xa
264#endif // XA_TRANSACTION_CACHE_H_INCLUDED
Definition: transaction_info.h:67
Cached transaction context and its visibility through SQL XA RECOVER.
Definition: transaction_cache.h:185
Xa_recover_visibility m_xa_recover_visibility
Definition: transaction_cache.h:216
const transaction_ptr & transaction() const
Definition: transaction_cache.h:195
transaction_ptr m_transaction
Definition: transaction_cache.h:215
void mark_prepared_visible_for_xa_recover()
Mark visible through SQL XA RECOVER without changing SQL XA state.
Definition: transaction_cache.h:205
bool is_visible_to_xa_recover() const
Definition: transaction_cache.h:200
void mark_finalized_for_recover()
Mark finalized for SQL XA RECOVER while keeping the XID reserved.
Definition: transaction_cache.h:210
Cache_entry(transaction_ptr transaction, Xa_recover_visibility visibility)
Definition: transaction_cache.h:190
Class responsible for managing a cache of Transaction_ctx objects associated with XA transactions.
Definition: transaction_cache.h:60
Transaction_cache()
Class constructor.
Definition: transaction_cache.cc:83
static void mark_prepared_visible_for_xa_recover(Transaction_ctx *transaction)
Mark a transaction visible through SQL XA RECOVER without changing its SQL XA state.
Definition: transaction_cache.cc:131
PSI_mutex_key m_key_LOCK_transaction_cache
The PSI key for the above lock.
Definition: transaction_cache.h:225
Xa_recover_visibility
Definition: transaction_cache.h:182
std::shared_ptr< Transaction_ctx > transaction_ptr
Definition: transaction_cache.h:62
static void dispose()
Disposes of the transaction cache allocated resources.
Definition: transaction_cache.cc:227
static Transaction_cache & instance()
Initialize a cache to store Transaction_ctx and a mutex to protect access to the cache.
Definition: transaction_cache.cc:232
static bool detach(Transaction_ctx *transaction)
Transaction is marked in the cache as if it's recovered.
Definition: transaction_cache.cc:99
virtual ~Transaction_cache()=default
unordered_map m_transaction_cache
A map holding the cached transaction context, indexed by XID.
Definition: transaction_cache.h:232
static void remove(Transaction_ctx *transaction)
Remove information about transaction from a cache.
Definition: transaction_cache.cc:120
static bool insert(XID *xid, Transaction_ctx *transaction)
Inserts a transaction context identified by a given XID.
Definition: transaction_cache.cc:161
PSI_mutex_info m_transaction_cache_mutexes[1]
The PSI configuration of the above lock and key.
Definition: transaction_cache.h:227
std::function< bool(transaction_ptr const &)> filter_predicate_t
Definition: transaction_cache.h:63
static transaction_ptr find(XID *xid, filter_predicate_t filter=nullptr)
Searches the cache for the transaction context identified by the given XID.
Definition: transaction_cache.cc:199
static void initialize()
Initializes the transaction cache underlying resources.
Definition: transaction_cache.cc:225
Transaction_cache & operator=(Transaction_cache const &)=delete
static void mark_finalized_for_recover(Transaction_ctx *transaction)
Mark a transaction finalized for SQL XA RECOVER without releasing its XID reservation from the cache.
Definition: transaction_cache.cc:146
Transaction_cache(Transaction_cache &&)=delete
static std::vector< XID > get_xids_visible_to_xa_recover()
Retrieves XID snapshots visible through SQL XA RECOVER.
Definition: transaction_cache.cc:211
mysql_mutex_t m_LOCK_transaction_cache
A lock to serialize the access to m_transaction_cache
Definition: transaction_cache.h:222
Transaction_cache & operator=(Transaction_cache &&)=delete
Transaction_cache(Transaction_cache const &)=delete
static bool create_and_insert_new_transaction(XID *xid, bool is_binlogged_arg, const Transaction_ctx *src)
Creates a new transaction context for the transaction with the given XID and adds it to the cache.
Definition: transaction_cache.cc:237
#define PSI_DOCUMENT_ME
Definition: component_common.h:29
#define PSI_FLAG_SINGLETON
Singleton flag.
Definition: component_common.h:35
unsigned int PSI_mutex_key
Instrumented mutex key.
Definition: psi_mutex_bits.h:52
Some integer typedefs for easier portability.
Define std::hash<Gtid>.
Definition: gtid.h:355
Definition: recovery.h:39
Mutex information.
Definition: psi_mutex_bits.h:73
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50
struct xid_t is binary compatible with the XID structure as in the X/Open CAE Specification,...
Definition: xa.h:83