MySQL 8.4.0
Source Code Documentation
xa.cc File Reference
#include "sql/xa.h"
#include <memory>
#include <new>
#include <string>
#include <unordered_map>
#include <utility>
#include "dig_vec.h"
#include "map_helpers.h"
#include "my_dbug.h"
#include "my_macros.h"
#include "my_psi_config.h"
#include "my_sys.h"
#include "mysql/components/services/bits/mysql_mutex_bits.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql/components/services/bits/psi_mutex_bits.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/my_loglevel.h"
#include "mysql/plugin.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/psi/mysql_transaction.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql/strings/m_ctype.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "scope_guard.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/binlog.h"
#include "sql/clone_handler.h"
#include "sql/debug_sync.h"
#include "sql/handler.h"
#include "sql/item.h"
#include "sql/log.h"
#include "sql/mdl.h"
#include "sql/mdl_context_backup.h"
#include "sql/mysqld.h"
#include "sql/protocol.h"
#include "sql/psi_memory_key.h"
#include "sql/query_options.h"
#include "sql/rpl_context.h"
#include "sql/rpl_gtid.h"
#include "sql/rpl_handler.h"
#include "sql/rpl_replica_commit_order_manager.h"
#include "sql/sql_class.h"
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_plugin.h"
#include "sql/sql_table.h"
#include "sql/system_variables.h"
#include "sql/tc_log.h"
#include "sql/transaction.h"
#include "sql/transaction_info.h"
#include "sql/xa/recovery.h"
#include "sql/xa/sql_xa_commit.h"
#include "sql/xa/transaction_cache.h"
#include "sql_string.h"
#include "string_with_len.h"
#include "template_utils.h"
#include "thr_mutex.h"

Functions

static void attach_native_trx (THD *thd)
 The function restores previously saved storage engine transaction context. More...
 
std::ostream & operator<< (std::ostream &out, struct xid_t const &in)
 
static bool xarecover_create_mdl_backup (XA_recover_txn &txn, MEM_ROOT *mem_root)
 
int ha_recover (Xid_commit_list *commit_list, Xa_state_list *xa_list)
 Stage of the recovery process where information is collected from the storage engines (SE), merged with information from the transaction coordinator (TC) and transactions states are determined and enforced. More...
 
bool xa_trans_force_rollback (THD *thd)
 Rollback the active XA transaction. More...
 
void cleanup_trans_state (THD *thd)
 Reset some transaction state information and delete corresponding Transaction_ctx object from cache. More...
 
std::shared_ptr< Transaction_ctxfind_trn_for_recover_and_check_its_state (THD *thd, xid_t *xid_for_trn_in_recover, XID_STATE *xid_state)
 Find XA transaction in cache by its xid value. More...
 
bool acquire_mandatory_metadata_locks (THD *thd, xid_t *detached_xid)
 Acquire Commit metadata lock and all locks acquired by a prepared XA transaction before server was shutdown or terminated. More...
 
bool applier_reset_xa_trans (THD *thd)
 This is a specific to "slave" applier collection of standard cleanup actions to reset XA transaction state at the end of XA prepare rather than to do it at the transaction commit, see ha_commit_one_phase. More...
 
bool detach_native_trx (THD *thd, plugin_ref plugin, void *)
 The function detaches existing storage engines transaction context from thd. More...
 
bool reattach_native_trx (THD *thd, plugin_ref plugin, void *)
 The function reattaches existing storage engines transaction context to thd. More...
 
bool disconnect_native_trx (THD *thd, plugin_ref plugin, void *)
 Disconnect transaction in SE. More...
 
bool thd_holds_xa_transaction (THD *thd)
 Test if the THD session underlying transaction is an externally coordinated (XA) transaction. More...
 
bool is_xa_prepare (THD *thd)
 Checks whether or not the underlying statement is an XA PREPARE. More...
 
bool is_xa_rollback (THD *thd)
 Checks whether or not the underlying statement is an XA ROLLBACK. More...
 

Variables

static const int MIN_XID_LIST_SIZE = 128
 
static const int MAX_XID_LIST_SIZE = 1024 * 128
 
static const uint MYSQL_XID_PREFIX_LEN = 8
 
static const uint MYSQL_XID_OFFSET = MYSQL_XID_PREFIX_LEN + sizeof(server_id)
 
static const uint MYSQL_XID_GTRID_LEN = MYSQL_XID_OFFSET + sizeof(my_xid)
 

Function Documentation

◆ acquire_mandatory_metadata_locks()

bool acquire_mandatory_metadata_locks ( THD thd,
xid_t detached_xid 
)

Acquire Commit metadata lock and all locks acquired by a prepared XA transaction before server was shutdown or terminated.

Parameters
thdThread context
detached_xidXID value specified by XA COMMIT or XA ROLLBACK that corresponds to a XA transaction generated outside current session context.
Return values
falseSuccess
trueFailure

◆ applier_reset_xa_trans()

bool applier_reset_xa_trans ( THD thd)

This is a specific to "slave" applier collection of standard cleanup actions to reset XA transaction state at the end of XA prepare rather than to do it at the transaction commit, see ha_commit_one_phase.

THD of the slave applier is dissociated from a transaction object in engine that continues to exist there.

Parameters
thdcurrent thread
Returns
the value of is_error()

◆ attach_native_trx()

static void attach_native_trx ( THD thd)
static

The function restores previously saved storage engine transaction context.

Parameters
thdThread context

◆ cleanup_trans_state()

void cleanup_trans_state ( THD thd)

Reset some transaction state information and delete corresponding Transaction_ctx object from cache.

Parameters
thdCurrent thread

◆ detach_native_trx()

bool detach_native_trx ( THD thd,
plugin_ref  plugin,
void *   
)

The function detaches existing storage engines transaction context from thd.

Backup area to save it is provided to low level storage engine function.

is invoked by plugin_foreach() after trans_xa_start() for each storage engine.

Parameters
[in,out]thdThread context
pluginReference to handlerton
Returns
false on success, true otherwise.

◆ disconnect_native_trx()

bool disconnect_native_trx ( THD thd,
plugin_ref  plugin,
void *   
)

Disconnect transaction in SE.

This the same action which is performed by SE when disconnecting a connection which has a prepared XA transaction, when xa_detach_on_prepare is OFF. Signature matches that required by plugin_foreach.

◆ find_trn_for_recover_and_check_its_state()

std::shared_ptr< Transaction_ctx > find_trn_for_recover_and_check_its_state ( THD thd,
xid_t xid_for_trn_in_recover,
XID_STATE xid_state 
)

Find XA transaction in cache by its xid value.

Parameters
thdThread context
xid_for_trn_in_recoverxid value to look for in transaction cache
xid_stateState of XA transaction in current session
Returns
Pointer to an instance of Transaction_ctx corresponding to a xid in argument. If XA transaction not found returns nullptr and sets an error in DA to specify a reason of search failure.

◆ ha_recover()

int ha_recover ( Xid_commit_list commit_list = nullptr,
Xa_state_list xa_state_list = nullptr 
)

Stage of the recovery process where information is collected from the storage engines (SE), merged with information from the transaction coordinator (TC) and transactions states are determined and enforced.

Implemented heuristics is as follows:

  1. The commit_list parameter contains the set of internally coordinated transactions that the TC ensures were marked as committed.
  2. The xa_state_list parameter contains the list of externally coordinated transactions and their states, as recorded by the TC.
  3. For each SE: a. Collect list of transactions found in PREPARED_IN_TC state in the SE and merge it with the information collected from the TC, in xa_state_list. b. Retrieve the list of transactions found in prepared state in the SE.

    c. For each internally coordinated transactions found in prepared state:

    1. If the transaction is found in commit_list, commit it.
    2. If the transaction is NOT found in commit_list but tc_heuristic_recover = TC_HEURISTIC_RECOVER_COMMIT, commit it.
    3. Otherwise, roll it back.

    d. For each externally coordinated transactions found in prepared state:

    1. If the transaction isn't found in xa_state_list, roll it back.
    2. If the transaction is found in xa_state_list in COMMITTED state, commit it.
    3. If the transaction is found in xa_state_list in ROLLEDBACK state, roll it back.
    4. If the transaction is found in xa_state_list in PREPARED state, ensure that the transaction state in the SE is PREPARED_IN_TC.
Parameters
commit_listSet of XIDs of internally coordinated transactions found as been committed in the transaction coordinator state.
xa_state_listMap between XIDs and states of externally coordinated transactions as found in the internal transaction coordinator state.
Returns
0 if recovery was successful, non-zero otherwise.

◆ is_xa_prepare()

bool is_xa_prepare ( THD thd)

Checks whether or not the underlying statement is an XA PREPARE.

Parameters
thdTHD session object.
Returns
true if the underlying statement is an XA PREPARE, false if not

◆ is_xa_rollback()

bool is_xa_rollback ( THD thd)

Checks whether or not the underlying statement is an XA ROLLBACK.

Parameters
thdTHD session object.
Returns
true if the underlying statement is an XA ROLLBACK, false if not

◆ operator<<()

std::ostream & operator<< ( std::ostream &  out,
struct xid_t const &  in 
)
Parameters
outThe stream to write the XID representation to
inThe XID for which the string representation should be written
Returns
The reference for the stream passed on as parameter.

◆ reattach_native_trx()

bool reattach_native_trx ( THD thd,
plugin_ref  plugin,
void *   
)

The function reattaches existing storage engines transaction context to thd.

Backup area to save it is provided to low level storage engine function.

is invoked by plugin_foreach() after trans_xa_prepare() for each storage engine.

Parameters
[in,out]thdThread context
pluginReference to handlerton
Returns
false on success, true otherwise.

◆ thd_holds_xa_transaction()

bool thd_holds_xa_transaction ( THD thd)

Test if the THD session underlying transaction is an externally coordinated (XA) transaction.

Parameters
thdThe session THD object holding the transaction to be tested.
Returns
true if the session underlying transaction is an XA transaction, false otherwise.

◆ xa_trans_force_rollback()

bool xa_trans_force_rollback ( THD thd)

Rollback the active XA transaction.

Note
Resets rm_error before calling ha_rollback(), so the thd->transaction.xid structure gets reset by ha_rollback() / THD::transaction::cleanup().
Returns
true if the rollback failed, false otherwise.

◆ xarecover_create_mdl_backup()

static bool xarecover_create_mdl_backup ( XA_recover_txn txn,
MEM_ROOT mem_root 
)
static

Variable Documentation

◆ MAX_XID_LIST_SIZE

const int MAX_XID_LIST_SIZE = 1024 * 128
static

◆ MIN_XID_LIST_SIZE

const int MIN_XID_LIST_SIZE = 128
static

◆ MYSQL_XID_GTRID_LEN

const uint MYSQL_XID_GTRID_LEN = MYSQL_XID_OFFSET + sizeof(my_xid)
static

◆ MYSQL_XID_OFFSET

const uint MYSQL_XID_OFFSET = MYSQL_XID_PREFIX_LEN + sizeof(server_id)
static

◆ MYSQL_XID_PREFIX_LEN

const uint MYSQL_XID_PREFIX_LEN = 8
static