MySQL  8.0.18
Source Code Documentation
xa.cc File Reference
#include "sql/xa.h"
#include <memory>
#include <new>
#include <string>
#include <unordered_map>
#include <utility>
#include "m_ctype.h"
#include "m_string.h"
#include "map_helpers.h"
#include "my_dbug.h"
#include "my_loglevel.h"
#include "my_macros.h"
#include "my_psi_config.h"
#include "my_sys.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/mysql_mutex_bits.h"
#include "mysql/components/services/psi_mutex_bits.h"
#include "mysql/plugin.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/psi/mysql_transaction.h"
#include "mysql/psi/psi_base.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql_com.h"
#include "mysqld_error.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/sql_class.h"
#include "sql/sql_const.h"
#include "sql/sql_error.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_string.h"
#include "template_utils.h"
#include "thr_mutex.h"

Classes

struct  transaction_free_hash
 
struct  xarecover_st
 

Functions

static void attach_native_trx (THD *thd)
 The function restores previously saved storage engine transaction context. More...
 
static std::shared_ptr< Transaction_ctxtransaction_cache_search (XID *xid)
 Search information about XA transaction by a XID value. More...
 
static bool transaction_cache_insert (XID *xid, Transaction_ctx *transaction)
 Insert information about XA transaction into a cache indexed by XID. More...
 
static bool transaction_cache_insert_recovery (XID *xid)
 Insert information about XA transaction being recovered into a cache indexed by XID. More...
 
static bool xacommit_handlerton (THD *, plugin_ref plugin, void *arg)
 
static bool xarollback_handlerton (THD *, plugin_ref plugin, void *arg)
 
static bool ha_commit_or_rollback_by_xid (THD *, XID *xid, bool commit)
 
static bool xarecover_create_mdl_backup (XA_recover_txn &txn, MEM_ROOT *mem_root)
 
static bool xarecover_handlerton (THD *, plugin_ref plugin, void *arg)
 
int ha_recover (const memroot_unordered_set< my_xid > *commit_list)
 
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...
 
static 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...
 
static bool acquire_mandatory_metadata_locks (THD *thd, xid_t *external_xid)
 Acquire Commit metadata lock and all locks acquired by a prepared XA transaction before server was shutdown or terminated. More...
 
static std::string to_string (const XID &xid)
 
static void init_transaction_cache_psi_keys (void)
 
bool transaction_cache_init ()
 Initialize a cache to store Transaction_ctx and a mutex to protect access to the cache. More...
 
void transaction_cache_free ()
 Release resources occupied by transaction cache. More...
 
bool create_and_insert_new_transaction (XID *xid, bool is_binlogged_arg)
 
bool transaction_cache_detach (Transaction_ctx *transaction)
 Transaction is marked in the cache as if it's recovered. More...
 
void transaction_cache_delete (Transaction_ctx *transaction)
 Remove information about transaction from a cache. More...
 
bool applier_reset_xa_trans (THD *thd)
 This is a specific to "slave" applier collection of standard cleanup actions to reset XA transaction states 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...
 

Variables

static const int MIN_XID_LIST_SIZE = 128
 
static const int MAX_XID_LIST_SIZE = 1024 * 128
 
static bool inited = false
 
static mysql_mutex_t LOCK_transaction_cache
 
static malloc_unordered_map< std::string, std::shared_ptr< Transaction_ctx > > transaction_cache {key_memory_XID}
 
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)
 
static PSI_mutex_key key_LOCK_transaction_cache
 
static PSI_mutex_info transaction_cache_mutexes []
 

Function Documentation

◆ acquire_mandatory_metadata_locks()

static bool acquire_mandatory_metadata_locks ( THD thd,
xid_t external_xid 
)
static

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

Parameters
thdThread context
external_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 states at the end of XA prepare rather than to do it at the transaction commit, see ha_commit_one_phase.

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

◆ create_and_insert_new_transaction()

bool create_and_insert_new_transaction ( XID xid,
bool  is_binlogged_arg 
)
inline

◆ 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.

◆ find_trn_for_recover_and_check_its_state()

static 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 
)
static

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_commit_or_rollback_by_xid()

static bool ha_commit_or_rollback_by_xid ( THD ,
XID xid,
bool  commit 
)
static

◆ ha_recover()

int ha_recover ( const memroot_unordered_set< my_xid > *  commit_list)

◆ init_transaction_cache_psi_keys()

static void init_transaction_cache_psi_keys ( void  )
static

◆ 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.

◆ to_string()

static std::string to_string ( const XID xid)
inlinestatic

◆ transaction_cache_delete()

void transaction_cache_delete ( Transaction_ctx transaction)

Remove information about transaction from a cache.

Parameters
transactionPointer to a Transaction_ctx that has to be removed from a cache.

◆ transaction_cache_detach()

bool transaction_cache_detach ( Transaction_ctx transaction)

Transaction is marked in the cache as if it's recovered.

The method allows to sustain prepared transaction disconnection.

Parameters
transactionPointer to Transaction object that is replaced.
Returns
operation result
Return values
falsesuccess or a cache already contains XID_STATE for this XID value
truefailure

◆ transaction_cache_free()

void transaction_cache_free ( )

Release resources occupied by transaction cache.

◆ transaction_cache_init()

bool transaction_cache_init ( )

Initialize a cache to store Transaction_ctx and a mutex to protect access to the cache.

Returns
result of initialization
Return values
falsesuccess
truefailure

◆ transaction_cache_insert()

bool transaction_cache_insert ( XID xid,
Transaction_ctx transaction 
)
static

Insert information about XA transaction into a cache indexed by XID.

Parameters
xidPointer to a XID structure that identifies a XA transaction.
transactionPointer to Transaction object that is inserted.
Returns
operation result
Return values
falsesuccess or a cache already contains XID_STATE for this XID value
truefailure

◆ transaction_cache_insert_recovery()

bool transaction_cache_insert_recovery ( XID xid)
static

Insert information about XA transaction being recovered into a cache indexed by XID.

Parameters
xidPointer to a XID structure that identifies a XA transaction.
Returns
operation result
Return values
falsesuccess or a cache already contains Transaction_ctx for this XID value
truefailure

◆ transaction_cache_search()

static std::shared_ptr< Transaction_ctx > transaction_cache_search ( XID xid)
static

Search information about XA transaction by a XID value.

Parameters
xidPointer to a XID structure that identifies a XA transaction.
Returns
pointer to a Transaction_ctx that describes the whole transaction including XA-specific information (XID_STATE).
Return values
NULLfailure
!=NULL success

◆ 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.

◆ xacommit_handlerton()

static bool xacommit_handlerton ( THD ,
plugin_ref  plugin,
void *  arg 
)
static

◆ xarecover_create_mdl_backup()

static bool xarecover_create_mdl_backup ( XA_recover_txn txn,
MEM_ROOT mem_root 
)
static

◆ xarecover_handlerton()

static bool xarecover_handlerton ( THD ,
plugin_ref  plugin,
void *  arg 
)
static

◆ xarollback_handlerton()

static bool xarollback_handlerton ( THD ,
plugin_ref  plugin,
void *  arg 
)
static

Variable Documentation

◆ inited

bool inited = false
static

◆ key_LOCK_transaction_cache

PSI_mutex_key key_LOCK_transaction_cache
static

◆ LOCK_transaction_cache

mysql_mutex_t LOCK_transaction_cache
static

◆ 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

◆ transaction_cache

malloc_unordered_map<std::string, std::shared_ptr<Transaction_ctx> > transaction_cache {key_memory_XID}
static

◆ transaction_cache_mutexes

PSI_mutex_info transaction_cache_mutexes[]
static
Initial value:
= {
{&key_LOCK_transaction_cache, "LOCK_transaction_cache", PSI_FLAG_SINGLETON,
static PSI_mutex_key key_LOCK_transaction_cache
Definition: xa.cc:1379
#define PSI_FLAG_SINGLETON
Singleton flag.
Definition: psi_base.h:54
#define PSI_DOCUMENT_ME
Definition: psi_base.h:46