MySQL  8.0.20
Source Code Documentation
Commit_order_manager Class Reference

#include <rpl_slave_commit_order_manager.h>

Classes

struct  worker_info
 

Public Member Functions

 Commit_order_manager (uint32 worker_numbers)
 
 ~Commit_order_manager ()
 
void register_trx (Slave_worker *worker)
 Register the worker into commit order queue when coordinator dispatches a transaction to the worker. More...
 

Static Public Member Functions

static void check_and_report_deadlock (THD *thd_self, THD *thd_wait_for)
 Check if order commit deadlock happens. More...
 
static bool wait (THD *thd)
 Wait for its turn to commit or unregister. More...
 
static void wait_and_finish (THD *thd, bool error)
 Wait for its turn to unregister and signal the next one to go ahead. More...
 
static bool get_rollback_status (THD *thd)
 Get transaction rollback status. More...
 
static void finish_one (THD *thd)
 Unregister the thread from the commit order queue and signal the next thread to awake. More...
 
static bool wait_for_its_turn_before_flush_stage (THD *thd)
 Determines whether current thread needs to wait for its turn to commit and unregister from the commit order queue. More...
 

Private Types

enum  enum_transaction_stage { enum_transaction_stage::REGISTERED, enum_transaction_stage::WAITED, enum_transaction_stage::FINISHED }
 

Private Member Functions

bool wait (Slave_worker *worker)
 Wait for its turn to commit or unregister. More...
 
void finish_one (Slave_worker *worker)
 Unregister the thread from the commit order queue and signal the next thread to awake. More...
 
void finish (Slave_worker *worker)
 Unregister the transaction from the commit order queue and signal the next one to go ahead. More...
 
void reset_server_status (THD *first_thd)
 Reset server_status value of the commit group. More...
 
bool get_rollback_status ()
 Get rollback status. More...
 
void set_rollback_status ()
 Set rollback status to true. More...
 
void unset_rollback_status ()
 Unset rollback status to false. More...
 
void report_deadlock (Slave_worker *worker)
 
bool queue_empty ()
 
void queue_pop ()
 
void queue_push (uint32 index)
 
uint32 queue_front ()
 
 Commit_order_manager (const Commit_order_manager &)
 
Commit_order_manageroperator= (const Commit_order_manager &)
 
void flush_engine_and_signal_threads (Slave_worker *worker)
 Flush record of transactions for all the waiting threads and then awake them from their wait. More...
 

Private Attributes

mysql_mutex_t m_mutex
 
std::atomic< boolm_rollback_trx
 
std::vector< worker_infom_workers
 
uint32 queue_head
 
uint32 queue_tail
 

Static Private Attributes

static const uint32 QUEUE_EOF = 0xFFFFFFFF
 

Member Enumeration Documentation

◆ enum_transaction_stage

Enumerator
REGISTERED 
WAITED 
FINISHED 

Constructor & Destructor Documentation

◆ Commit_order_manager() [1/2]

Commit_order_manager::Commit_order_manager ( uint32  worker_numbers)

◆ ~Commit_order_manager()

Commit_order_manager::~Commit_order_manager ( )

◆ Commit_order_manager() [2/2]

Commit_order_manager::Commit_order_manager ( const Commit_order_manager )
private

Member Function Documentation

◆ check_and_report_deadlock()

void Commit_order_manager::check_and_report_deadlock ( THD thd_self,
THD thd_wait_for 
)
static

Check if order commit deadlock happens.

Worker1(trx1) Worker2(trx2) ============= ============= ... ... Engine acquires lock A ... Engine acquires lock A(waiting for trx1 to release it. COMMIT(waiting for trx2 to commit first).

Currently, there are two corner cases can cause the deadlock.

  • Case 1 CREATE TABLE t1(c1 INT PRIMARY KEY, c2 INT, INDEX(c2)) ENGINE = InnoDB; INSERT INTO t1 VALUES(1, NULL),(2, 2), (3, NULL), (4, 4), (5, NULL), (6, 6)

    INSERT INTO t1 VALUES(7, NULL); DELETE FROM t1 WHERE c2 <= 3;

  • Case 2 ANALYZE TABLE t1; INSERT INTO t2 SELECT * FROM mysql.innodb_table_stats

Since this is not a real lock deadlock, it could not be handled by engine. slave need to handle it separately. Worker1(trx1) Worker2(trx2) ============= ============= ... ... Engine acquires lock A ... Engine acquires lock A.

  1. found trx1 is holding the lock.
  2. report the lock wait to server code by calling thd_report_row_lock_wait(). Then this function is called to check if it causes a order commit deadlock. Report the deadlock to worker1.
  3. waiting for trx1 to release it. COMMIT(waiting for trx2 to commit first). Found the deadlock flag set by worker2 and then return with ER_LOCK_DEADLOCK.

Rollback the transaction Get lock A and go ahead. ... Retry the transaction

To conclude, The transaction A which is waiting for transaction B to commit and is holding a lock which is required by transaction B will be rolled back and try again later.

Parameters
[in]thd_selfThe THD object of self session which is acquiring a lock hold by another session.
[in]thd_wait_forThe THD object of a session which is holding a lock being acquired by current session.

◆ finish()

void Commit_order_manager::finish ( Slave_worker worker)
private

Unregister the transaction from the commit order queue and signal the next one to go ahead.

Parameters
[in]workerThe worker which is executing the transaction.

◆ finish_one() [1/2]

void Commit_order_manager::finish_one ( Slave_worker worker)
private

Unregister the thread from the commit order queue and signal the next thread to awake.

Parameters
[in]workerThe worker which is executing the transaction.

◆ finish_one() [2/2]

void Commit_order_manager::finish_one ( THD thd)
static

Unregister the thread from the commit order queue and signal the next thread to awake.

Parameters
[in]thdThe THD object of current thread.

◆ flush_engine_and_signal_threads()

void Commit_order_manager::flush_engine_and_signal_threads ( Slave_worker worker)
private

Flush record of transactions for all the waiting threads and then awake them from their wait.

It also calls gtid_state->update_commit_group() which updates both the THD and the Gtid_state for whole commit group to reflect that the transaction set of transactions has ended.

Parameters
[in]workerThe worker which is executing the transaction.

◆ get_rollback_status() [1/2]

bool Commit_order_manager::get_rollback_status ( )
private

Get rollback status.

Return values
trueTransactions in the queue should rollback.
falseTransactions in the queue shouldn't rollback.

◆ get_rollback_status() [2/2]

bool Commit_order_manager::get_rollback_status ( THD thd)
static

Get transaction rollback status.

Parameters
[in]thdThe THD object of current thread.
Return values
trueCurrent transaction should rollback.
falseCurrent transaction shouldn't rollback.

◆ operator=()

Commit_order_manager& Commit_order_manager::operator= ( const Commit_order_manager )
private

◆ queue_empty()

bool Commit_order_manager::queue_empty ( )
inlineprivate

◆ queue_front()

uint32 Commit_order_manager::queue_front ( )
inlineprivate

◆ queue_pop()

void Commit_order_manager::queue_pop ( )
inlineprivate

◆ queue_push()

void Commit_order_manager::queue_push ( uint32  index)
inlineprivate

◆ register_trx()

void Commit_order_manager::register_trx ( Slave_worker worker)

Register the worker into commit order queue when coordinator dispatches a transaction to the worker.

Parameters
[in]workerThe worker which the transaction will be dispatched to.

◆ report_deadlock()

void Commit_order_manager::report_deadlock ( Slave_worker worker)
private

◆ reset_server_status()

void Commit_order_manager::reset_server_status ( THD first_thd)
private

Reset server_status value of the commit group.

Parameters
[in]first_thdThe first thread of the commit group that needs server_status to be updated.

◆ set_rollback_status()

void Commit_order_manager::set_rollback_status ( )
private

Set rollback status to true.

◆ unset_rollback_status()

void Commit_order_manager::unset_rollback_status ( )
private

Unset rollback status to false.

◆ wait() [1/2]

bool Commit_order_manager::wait ( Slave_worker worker)
private

Wait for its turn to commit or unregister.

Parameters
[in]workerThe worker which is executing the transaction.
Return values
falseAll previous transactions succeed, so this transaction can go ahead and commit.
trueOne or more previous transactions rollback, so this transaction should rollback.

◆ wait() [2/2]

bool Commit_order_manager::wait ( THD thd)
static

Wait for its turn to commit or unregister.

Parameters
[in]thdThe THD object of current thread.
Return values
falseAll previous transactions succeed, so this transaction can go ahead and commit.
trueThe transaction is marked to rollback.

◆ wait_and_finish()

void Commit_order_manager::wait_and_finish ( THD thd,
bool  error 
)
static

Wait for its turn to unregister and signal the next one to go ahead.

In case error happens while processing transaction, notify the following transaction to rollback.

Parameters
[in]thdThe THD object of current thread.
[in]errorIf true failure in transaction execution

◆ wait_for_its_turn_before_flush_stage()

bool Commit_order_manager::wait_for_its_turn_before_flush_stage ( THD thd)
static

Determines whether current thread needs to wait for its turn to commit and unregister from the commit order queue.

The sql commands ALTER TABLE, DROP TABLE, DROP DB, OPTIMIZE TABLE, ANALYZE TABLE and REPAIR TABLE are allowed to wait for its turn to commit and unregister from the commit order queue as exception in MYSQL_BIN_LOG::ordered_commit(), as these transactions have multiple commits and so not determined if the call is ending transaction.

Parameters
[in]thdThe THD object of current thread.
Return values
trueAllow thread to wait for it turn
falseDo not allow thread to wait for it turn

Member Data Documentation

◆ m_mutex

mysql_mutex_t Commit_order_manager::m_mutex
private

◆ m_rollback_trx

std::atomic<bool> Commit_order_manager::m_rollback_trx
private

◆ m_workers

std::vector<worker_info> Commit_order_manager::m_workers
private

◆ QUEUE_EOF

const uint32 Commit_order_manager::QUEUE_EOF = 0xFFFFFFFF
staticprivate

◆ queue_head

uint32 Commit_order_manager::queue_head
private

◆ queue_tail

uint32 Commit_order_manager::queue_tail
private

The documentation for this class was generated from the following files: