MySQL 8.4.2
Source Code Documentation
|
Class for maintaining the commit stages for binary log group commit. More...
#include <rpl_commit_stage_manager.h>
Classes | |
class | Mutex_queue |
Public Types | |
enum | StageID { BINLOG_FLUSH_STAGE , SYNC_STAGE , COMMIT_STAGE , AFTER_COMMIT_STAGE , COMMIT_ORDER_FLUSH_STAGE , STAGE_COUNTER } |
Constants for queues for different stages. More... | |
Public Member Functions | |
void | init (PSI_mutex_key key_LOCK_flush_queue, PSI_mutex_key key_LOCK_sync_queue, PSI_mutex_key key_LOCK_commit_queue, PSI_mutex_key key_LOCK_after_commit_queue, PSI_mutex_key key_LOCK_done, PSI_mutex_key key_LOCK_wait_for_group_turn, PSI_cond_key key_COND_done, PSI_cond_key key_COND_flush_queue, PSI_cond_key key_COND_wait_for_group_turn) |
Initializes m_stage_cond_binlog, m_stage_cond_commit_order, m_stage_cond_leader condition variables and m_lock_done mutex. More... | |
void | deinit () |
Deinitializes m_stage_cond_binlog, m_stage_cond_commit_order, m_stage_cond_leader condition variables and m_lock_done mutex. More... | |
void | wait_for_ticket_turn (THD *thd, bool update_ticket_manager=true) |
Waits for the THD session parameter underlying BGC ticket to become active. More... | |
bool | append_to (StageID stage, THD *thd) |
Appends the given THD session object to the given stage queue. More... | |
bool | enroll_for (StageID stage, THD *first, mysql_mutex_t *stage_mutex, mysql_mutex_t *enter_mutex) |
Enroll a set of sessions for a stage. More... | |
std::pair< bool, THD * > | pop_front (StageID stage) |
Remove first member from the queue for given stage. More... | |
void | clear_preempt_status (THD *head) |
The method ensures the follower's execution path can be preempted by the leader's thread. More... | |
THD * | fetch_queue_acquire_lock (StageID stage) |
Fetch the entire queue and empty it. More... | |
THD * | fetch_queue_skip_acquire_lock (StageID stage) |
Fetch the entire queue and empty it. More... | |
void | wait_count_or_timeout (ulong count, long usec, StageID stage) |
Introduces a wait operation on the executing thread. More... | |
void | signal_done (THD *queue, StageID stage=BINLOG_FLUSH_STAGE) |
The function is called after follower thread are processed by leader, to unblock follower threads. More... | |
void | signal_end_of_ticket (bool force=false) |
Signals threads waiting on their BGC ticket turn. More... | |
void | update_session_ticket_state (THD *thd) |
Updates the THD session object underlying BGC context. More... | |
void | update_ticket_manager (std::uint64_t sessions_count, const binlog::BgcTicket &session_ticket) |
Adds the given session count to the total of processed sessions in the ticket manager active window, ends the active window if possible and notifies other threads that are waiting for a given ticket to have an active processing window. More... | |
void | finish_session_ticket (THD *thd) |
Waits for the session's ticket, if needed, and resets the session's ticket context. More... | |
void | process_final_stage_for_ordered_commit_group (THD *first) |
This function gets called after transactions are flushed to the engine i.e. More... | |
void | lock_queue (StageID stage) |
Wrapper on Mutex_queue lock(), acquires lock on stage queue. More... | |
void | unlock_queue (StageID stage) |
Wrapper on Mutex_queue unlock(), releases lock on stage queue. More... | |
Static Public Member Functions | |
static Commit_stage_manager & | get_instance () |
Fetch Commit_stage_manager class instance. More... | |
static void | disable_manual_session_tickets () |
Disables the ability for session BGC tickets to be set manually. More... | |
static void | enable_manual_session_tickets () |
Enables the ability for session BGC tickets to be set manually. More... | |
Private Member Functions | |
Commit_stage_manager () | |
Commit_stage_manager (const Commit_stage_manager &)=delete | |
const Commit_stage_manager & | operator= (const Commit_stage_manager &)=delete |
Private Attributes | |
bool | m_is_initialized |
check if Commit_stage_manager variables already initialized. More... | |
Mutex_queue | m_queue [STAGE_COUNTER] |
Queues for sessions. More... | |
mysql_cond_t | m_stage_cond_leader |
The binlog leader waits on this condition variable till it is indicated to wake up. More... | |
mysql_cond_t | m_stage_cond_binlog |
Condition variable to indicate that the binlog threads can wake up and continue. More... | |
mysql_cond_t | m_stage_cond_commit_order |
Condition variable to indicate that the flush to storage engine is done and commit order threads can again wake up and continue. More... | |
mysql_mutex_t | m_lock_done |
Mutex used for the condition variable above. More... | |
mysql_mutex_t | m_queue_lock [STAGE_COUNTER - 1] |
Mutex used for the stage level locks. More... | |
THD * | leader_thd |
Save pointer to leader thread which is used later to awake leader. More... | |
bool | leader_await_preempt_status |
Flag is set by Leader when it starts waiting for follower's all-clear. More... | |
mysql_cond_t | m_cond_preempt |
Condition variable to indicate a follower started waiting for commit. More... | |
mysql_cond_t | m_cond_wait_for_ticket_turn |
Condition variable to wait for a given ticket to become active. More... | |
mysql_mutex_t | m_lock_wait_for_ticket_turn |
Mutex to protect the wait for a given ticket to become active. More... | |
Class for maintaining the commit stages for binary log group commit.
|
inlineprivate |
|
privatedelete |
Appends the given THD session object to the given stage queue.
It verifies that the given session's ticket is the active ticket, if not, waits on m_cond_wait_for_ticket_turn
condition variable until it is.
stage | The stage to add the THD parameter to. |
thd | The THD session object to queue. |
void Commit_stage_manager::clear_preempt_status | ( | THD * | head | ) |
The method ensures the follower's execution path can be preempted by the leader's thread.
Preempt status of head
follower is checked to engange the leader into waiting when set.
head | THD* of a follower thread |
void Commit_stage_manager::deinit | ( | ) |
Deinitializes m_stage_cond_binlog, m_stage_cond_commit_order, m_stage_cond_leader condition variables and m_lock_done mutex.
|
static |
Disables the ability for session BGC tickets to be set manually.
|
static |
Enables the ability for session BGC tickets to be set manually.
bool Commit_stage_manager::enroll_for | ( | StageID | stage, |
THD * | first, | ||
mysql_mutex_t * | stage_mutex, | ||
mysql_mutex_t * | enter_mutex | ||
) |
Enroll a set of sessions for a stage.
This will queue the session thread for writing and flushing.
If the thread being queued is assigned as stage leader, it will return immediately.
If wait_if_follower is true the thread is not the stage leader, the thread will be wait for the queue to be processed by the leader before it returns. In DBUG-ON version the follower marks is preempt status as ready.
The session threads entering this function acquires mutexes, and few of them are not released while exiting based on thread and stage type.
[in] | stage | Stage identifier for the queue to append to. |
[in] | first | Queue to append. |
[in] | stage_mutex | Pointer to the currently held stage mutex, or nullptr if we're not in a stage, that will be released when changing stage. |
[in] | enter_mutex | Pointer to the mutex that will be taken when changing stage. |
true | Thread is stage leader. |
false | Thread was not stage leader and processing has been done. |
Fetch the entire queue and empty it.
It acquires queue lock before fetching and emptying the queue threads.
[in] | stage | Stage identifier for the queue to append to. |
Fetch the entire queue and empty it.
The caller must acquire queue lock before calling this function.
[in] | stage | Stage identifier for the queue to append to. |
void Commit_stage_manager::finish_session_ticket | ( | THD * | thd | ) |
Waits for the session's ticket, if needed, and resets the session's ticket context.
thd | The THD sessions object to finish the ticket's related work. |
|
static |
Fetch Commit_stage_manager class instance.
void Commit_stage_manager::init | ( | PSI_mutex_key | key_LOCK_flush_queue, |
PSI_mutex_key | key_LOCK_sync_queue, | ||
PSI_mutex_key | key_LOCK_commit_queue, | ||
PSI_mutex_key | key_LOCK_after_commit_queue, | ||
PSI_mutex_key | key_LOCK_done, | ||
PSI_mutex_key | key_LOCK_wait_for_group_turn, | ||
PSI_cond_key | key_COND_done, | ||
PSI_cond_key | key_COND_flush_queue, | ||
PSI_cond_key | key_COND_wait_for_group_turn | ||
) |
Initializes m_stage_cond_binlog, m_stage_cond_commit_order, m_stage_cond_leader condition variables and m_lock_done mutex.
The binlog follower threads blocks on m_stage_cond_binlog condition variable till signalled to wake up from leader thread. And similarly commit order follower threads blocks on m_stage_cond_commit_order condition variable till signalled to wake up from leader thread.
The first binlog thread supposed to be leader finds that commit order queue is not empty then it blocks on m_stage_cond_leader till commit order leader signals it to awake and become new leader.
m_lock_done mutex is shared by all three stages.
key_LOCK_flush_queue | mutex instrumentation key |
key_LOCK_sync_queue | mutex instrumentation key |
key_LOCK_commit_queue | mutex instrumentation key |
key_LOCK_after_commit_queue | mutex instrumentation key |
key_LOCK_done | mutex instrumentation key |
key_LOCK_wait_for_group_turn | mutex instrumentation key |
key_COND_done | cond instrumentation key |
key_COND_flush_queue | cond instrumentation key |
key_COND_wait_for_group_turn | cond instrumentation key |
reuse key_COND_done 'cos a new PSI object would be wasteful in !NDEBUG
Initialize mutex for flush, sync, commit and after commit stage queue. The binlog flush stage and commit order flush stage share same mutex.
|
inline |
Wrapper on Mutex_queue lock(), acquires lock on stage queue.
[in] | stage | Stage identifier for the queue to append to. |
|
privatedelete |
Remove first member from the queue for given stage.
Returns | std::pair<bool, THD *> object. The first boolean value of pair if true determines queue is not empty, and false determines queue is empty. The second value returns the first removed member. |
void Commit_stage_manager::process_final_stage_for_ordered_commit_group | ( | THD * | first | ) |
This function gets called after transactions are flushed to the engine i.e.
after calling ha_flush_logs, to unblock commit order thread list which are not needed to wait for other stages.
first | the thread list which needs to ne unblocked |
void Commit_stage_manager::signal_done | ( | THD * | queue, |
StageID | stage = BINLOG_FLUSH_STAGE |
||
) |
The function is called after follower thread are processed by leader, to unblock follower threads.
queue | the thread list which needs to ne unblocked |
stage | Stage identifier current thread belong to. |
void Commit_stage_manager::signal_end_of_ticket | ( | bool | force = false | ) |
Signals threads waiting on their BGC ticket turn.
force | Whether or not to force the signaling, despit the state of the ticket manager. |
|
inline |
Wrapper on Mutex_queue unlock(), releases lock on stage queue.
[in] | stage | Stage identifier for the queue to append to. |
void Commit_stage_manager::update_session_ticket_state | ( | THD * | thd | ) |
Updates the THD session object underlying BGC context.
thd | The THD object to update the BGC context for. |
void Commit_stage_manager::update_ticket_manager | ( | std::uint64_t | sessions_count, |
const binlog::BgcTicket & | session_ticket | ||
) |
Adds the given session count to the total of processed sessions in the ticket manager active window, ends the active window if possible and notifies other threads that are waiting for a given ticket to have an active processing window.
sessions_count | The number of sessions to add to the ticket manager processed sessions count. |
session_ticket | The session ticket (used for validations). |
void Commit_stage_manager::wait_count_or_timeout | ( | ulong | count, |
long | usec, | ||
StageID | stage | ||
) |
Introduces a wait operation on the executing thread.
The waiting is done until the timeout elapses or count is reached (whichever comes first).
If count == 0, then the session will wait until the timeout elapses. If timeout == 0, then there is no waiting.
usec | the number of microseconds to wait. |
count | wait for as many as count to join the queue the session is waiting on |
stage | which stage queue size to compare count against. |
void Commit_stage_manager::wait_for_ticket_turn | ( | THD * | thd, |
bool | update_ticket_manager = true |
||
) |
Waits for the THD session parameter underlying BGC ticket to become active.
thd | The THD session that holds the ticket to wait for. |
update_ticket_manager | Indicates whether to mark ticket as consumed by the session (add session to processed sessions) after the ticket is opened for processing. |
|
private |
Flag is set by Leader when it starts waiting for follower's all-clear.
|
private |
Save pointer to leader thread which is used later to awake leader.
|
private |
Condition variable to indicate a follower started waiting for commit.
|
private |
Condition variable to wait for a given ticket to become active.
|
private |
check if Commit_stage_manager variables already initialized.
|
private |
Mutex used for the condition variable above.
|
private |
Mutex to protect the wait for a given ticket to become active.
|
private |
Queues for sessions.
We need five queues:
|
private |
Mutex used for the stage level locks.
|
private |
Condition variable to indicate that the binlog threads can wake up and continue.
|
private |
Condition variable to indicate that the flush to storage engine is done and commit order threads can again wake up and continue.
|
private |
The binlog leader waits on this condition variable till it is indicated to wake up.
If binlog flush queue gets first thread in the queue but by then commit order flush queue has already elected leader. The the first thread of binlog queue waits on this condition variable and get signalled to wake up from commit order flush queue leader later.