MySQL 8.0.39
Source Code Documentation
sql_xa_second_phase.h
Go to the documentation of this file.
1/* Copyright (c) 2022, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef XA_SQL_CMD_XA_SECOND_PHASE
25#define XA_SQL_CMD_XA_SECOND_PHASE
26
27#include "sql/mdl_context_backup.h" // MDL_context_backup_manager
28#include "sql/sql_cmd.h" // Sql_cmd
29#include "sql/xa.h" // xid_t
30
31/**
32 @class Sql_cmd_xa_second_phase
33
34 This class abstracts some functionality used by XA statements involved in
35 the second phase of the the XA two-phase commit, specifically, `XA
36 COMMIT` and `XA ROLLBACK` SQL statements.
37
38 Common usage of the available methods target the process of detached XA
39 transactions, code pattern looks like:
40
41 bool Sql_cmd_xa_statement::process_detached_xa_statement(THD *thd) {
42 DBUG_TRACE;
43
44 if (this->find_and_initialize_xa_context(thd)) return true;
45 if (this->acquire_locks(thd)) return true;
46 raii::Sentry<> xa_lock_guard{
47 [this]() -> void { this->release_locks(); }};
48 this->setup_thd_context(thd);
49 if (this->enter_commit_order(thd)) return true;
50
51 this->assign_xid_to_thd(thd);
52
53 // SPECIFIC STATEMENT EXECUTION HERE
54 this->m_result = exec_statement(thd);
55
56 this->exit_commit_order(thd);
57 this->cleanup_context(thd);
58
59 return this->m_result;
60 }
61
62 @see Sql_cmd
63*/
65 public:
66 /**
67 Class constructor.
68
69 @param xid_arg XID of the XA transacation about to be committed
70 */
72 virtual ~Sql_cmd_xa_second_phase() override = default;
73
74 protected:
75 /** The XID associated with the underlying XA transaction. */
76 xid_t *m_xid{nullptr};
77 /** The MDL savepoint used to rollback the MDL context when transient errors
78 occur */
80 /** The detached transaction context, retrieved from the transaction cache */
81 std::shared_ptr<Transaction_ctx> m_detached_trx_context{nullptr};
82 /** Whether or not the initialization of GTIDs returned an error */
83 bool m_gtid_error{false};
84 /** Whether or not the OWNED_GTID related structures need to be cleaned */
86 /** The incremental success of the several initialization and deinitialization
87 steps. Is mainly used to steer some of the deinitialization calls */
88 bool m_result{false};
89
90 /**
91 Tries to find and initialize the `Transaction_ctx` for the underlying
92 detached XA transaction.
93
94 Execution is as follows:
95 1. Find transaction in the transaction cache.
96 2. Ensure that the underlying state regards the detached XA
97 transaction.
98
99 @param thd The THD session object used to process the detached XA
100 transaction.
101
102 @return false if the transaction context was successfully initialized,
103 true otherwise.
104 */
106 /**
107 Tries to acquire the locks necessary to finalize the underlying
108 detached XA transaction. By function exit, all locks have been acquired
109 or none.
110
111 Execution is as follows:
112 1. An MDL savepoint is set, in order to allow the rollback to this
113 point if a timeout/locking error is triggered.
114 2. XID_STATE::m_xa_lock is acquired to prevent concurrent finalization
115 of the transaction (triggered from different client connections, for
116 instance).
117 3. Ensure that between steps 1. and 2. some other session didn't
118 finalize the transaction, by confirming that the transaction is in
119 the transaction cache and still in prepared state.
120 4. Acquire MDL commit locks.
121
122 @param thd The THD session object used to process the detached XA
123 transaction.
124
125 @return false if all necessary locks were acquired, true otherwise.
126 */
127 bool acquire_locks(THD *thd);
128 /**
129 Release any locks acquires in `acquire_locks` still needing
130 to be released.
131 */
132 void release_locks() const;
133 /**
134 Initializes the necessary parts of the `thd` parameter, transferring
135 some of the detached XA transaction context to the active session. This
136 is necessary to use other parts of the infra-structure that rely on
137 having the active THD session properly initialized.
138
139 Execution is as follows:
140 1. Determine if GTID infra-structure is consistent and ready to
141 finalize the transaction.
142 2. Check the detached transaction status.
143 3. Transfer to the THD session object the state of the detached XA
144 transaction w.r.t whether or not the transaction has already been
145 binlogged.
146
147 @param thd The THD session object used to process the detached XA
148 transaction.
149 */
150 void setup_thd_context(THD *thd);
151 /**
152 For replica applier threads, enters the wait on the commit order.
153
154 Execution is as follows:
155 1. Enters the wait on the commit order.
156 2. If the wait fails (timeout or possible deadlock found), resets the
157 overall state to a point where a retry is possible:
158 a. Resets the GTID infra-structure.
159 b. Rolls back the MDL context to the recorded savepoint.
160 c. Resets the transaction binlogging state.
161
162 @param thd The THD session object used to process the detached XA
163 transaction.
164
165 @return false if the commit order wait was successful, true otherwise.
166 */
167 bool enter_commit_order(THD *thd);
168 /**
169 Sets the XID_STATE of the THD session object parameter as in detached
170 state and copies into it the XID of the detached XA transaction.
171
172 @param thd The THD session object used to process the detached XA
173 transaction.
174 */
175 void assign_xid_to_thd(THD *thd) const;
176 /**
177 For replica applier threads, finishes the wait on the commit order and
178 allows other threads to proceed.
179
180 @param thd The THD session object used to process the detached XA
181 transaction.
182 */
183 void exit_commit_order(THD *thd) const;
184 /**
185 Cleans up the THD context in order to prepare it for re-use.
186
187 Execution is as follows:
188 1. The active THD session binlogging state is cleared.
189 2. Any MDL context backup, associated with the detached transaction, is
190 deleted.
191 3. The detached transaction context is deleted from the transaction
192 cache.
193 4. GTID state is finalized, either committing or rolling back the GTID
194 information.
195
196 @param thd The THD session object used to process the detached XA
197 transaction.
198 */
199 void cleanup_context(THD *thd) const;
200 /**
201 Disposes of member variables that need it, because destructors for `Sql_cmd`
202 classes aren't invoked (since they are created in the internal memory pool,
203 memory is disposed as a all block).
204 */
205 void dispose();
206};
207
208#endif // XA_SQL_CMD_XA_SECOND_PHASE
Savepoint for MDL context.
Definition: mdl.h:1316
This class abstracts some functionality used by XA statements involved in the second phase of the the...
Definition: sql_xa_second_phase.h:64
void release_locks() const
Release any locks acquires in acquire_locks still needing to be released.
Definition: sql_xa_second_phase.cc:110
MDL_savepoint m_mdl_savepoint
The MDL savepoint used to rollback the MDL context when transient errors occur.
Definition: sql_xa_second_phase.h:79
bool find_and_initialize_xa_context(THD *thd)
Tries to find and initialize the Transaction_ctx for the underlying detached XA transaction.
Definition: sql_xa_second_phase.cc:38
bool m_result
The incremental success of the several initialization and deinitialization steps.
Definition: sql_xa_second_phase.h:88
xid_t * m_xid
The XID associated with the underlying XA transaction.
Definition: sql_xa_second_phase.h:76
Sql_cmd_xa_second_phase(xid_t *xid_arg)
Class constructor.
Definition: sql_xa_second_phase.cc:35
void assign_xid_to_thd(THD *thd) const
Sets the XID_STATE of the THD session object parameter as in detached state and copies into it the XI...
Definition: sql_xa_second_phase.cc:157
bool m_gtid_error
Whether or not the initialization of GTIDs returned an error.
Definition: sql_xa_second_phase.h:83
void exit_commit_order(THD *thd) const
For replica applier threads, finishes the wait on the commit order and allows other threads to procee...
Definition: sql_xa_second_phase.cc:163
virtual ~Sql_cmd_xa_second_phase() override=default
void setup_thd_context(THD *thd)
Initializes the necessary parts of the thd parameter, transferring some of the detached XA transactio...
Definition: sql_xa_second_phase.cc:116
bool m_need_clear_owned_gtid
Whether or not the OWNED_GTID related structures need to be cleaned.
Definition: sql_xa_second_phase.h:85
bool acquire_locks(THD *thd)
Tries to acquire the locks necessary to finalize the underlying detached XA transaction.
Definition: sql_xa_second_phase.cc:51
void dispose()
Disposes of member variables that need it, because destructors for Sql_cmd classes aren't invoked (si...
Definition: sql_xa_second_phase.cc:185
std::shared_ptr< Transaction_ctx > m_detached_trx_context
The detached transaction context, retrieved from the transaction cache.
Definition: sql_xa_second_phase.h:81
void cleanup_context(THD *thd) const
Cleans up the THD context in order to prepare it for re-use.
Definition: sql_xa_second_phase.cc:169
bool enter_commit_order(THD *thd)
For replica applier threads, enters the wait on the commit order.
Definition: sql_xa_second_phase.cc:138
Representation of an SQL command.
Definition: sql_cmd.h:65
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
Representation of an SQL command.
struct xid_t is binary compatible with the XID structure as in the X/Open CAE Specification,...
Definition: xa.h:83