MySQL 8.0.32
Source Code Documentation
rpl_commit_stage_manager.h
Go to the documentation of this file.
1/* Copyright (c) 2019, 2022, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef RPL_COMMIT_STAGE_MANAGER
24#define RPL_COMMIT_STAGE_MANAGER
25
26#include <atomic>
27#include <utility>
28
29#include "my_dbug.h"
32#include "sql/sql_class.h"
33#include "thr_mutex.h"
34
35class THD;
36
37/**
38 Class for maintaining the commit stages for binary log group commit.
39 */
41 public:
44
45 public:
47
49
50 bool is_empty() const { return m_first == nullptr; }
51
52 /**
53 Append a linked list of threads to the queue.
54
55 @param[in] first Linked list of threads to be appended to queue
56
57 @retval true The queue was empty before this operation.
58 @retval false The queue was non-empty before this operation.
59 */
60 bool append(THD *first);
61
62 /**
63 Fetch the entire queue for a stage. It is a wrapper over
64 fetch_and_empty() and acquires queue lock before fetching
65 and emptying the queue threads.
66
67 @return Pointer to the first session of the queue.
68 */
70
71 /**
72 Fetch the entire queue for a stage. It is a wrapper over
73 fetch_and_empty(). The caller must acquire queue lock before
74 calling this function.
75
76 @return Pointer to the first session of the queue.
77 */
79
80 /**
81 Remove first member from the queue
82
83 @retval Returns std::pair<bool, THD *> object.
84 The first boolean value of pair if true determines queue
85 is not empty, and false determines queue is empty.
86 The second value returns the first removed member.
87 */
88 std::pair<bool, THD *> pop_front();
89
90 /**
91 Get number of elements in the queue.
92
93 @retval Returns number of element in the queue.
94 */
95 inline int32 get_size() { return m_size.load(); }
96
97 /**
98 Fetch the first thread of the queue.
99
100 @return first thread of the queue.
101 */
102 THD *get_leader() { return m_first; }
103
104 void lock() {
107 }
108
110
112
113 private:
114 /**
115 Fetch the entire queue for a stage.
116
117 @retval This will fetch the entire queue in one go.
118 */
120
121 /**
122 Pointer to the first thread in the queue, or nullptr if the queue is
123 empty.
124 */
126
127 /**
128 Pointer to the location holding the end of the queue.
129
130 This is either @c &first, or a pointer to the @c next_to_commit of
131 the last thread that is enqueued.
132 */
134
135 /** size of the queue */
136 std::atomic<int32> m_size;
137
138 /** Lock for protecting the queue. */
140
141 /*
142 This attribute did not have the desired effect, at least not according
143 to -fsanitize=undefined with gcc 5.2.1
144 */
145 }; // MY_ATTRIBUTE((aligned(CPU_LEVEL1_DCACHE_LINESIZE)));
146
147 private:
149
151
153
154 public:
155 /**
156 Fetch Commit_stage_manager class instance.
157
158 @return Reference to the Commit_stage_manager class instance.
159 */
161
162 /**
163 Constants for queues for different stages.
164 */
165 enum StageID {
171 };
172
173 /**
174 Initializes m_stage_cond_binlog, m_stage_cond_commit_order,
175 m_stage_cond_leader condition variables and m_lock_done mutex.
176
177 The binlog follower threads blocks on m_stage_cond_binlog condition
178 variable till signalled to wake up from leader thread. And similarly
179 commit order follower threads blocks on m_stage_cond_commit_order
180 condition variable till signalled to wake up from leader thread.
181
182 The first binlog thread supposed to be leader finds that commit order queue
183 is not empty then it blocks on m_stage_cond_leader till commit order leader
184 signals it to awake and become new leader.
185
186 m_lock_done mutex is shared by all three stages.
187
188 @param key_LOCK_flush_queue mutex instrumentation key
189 @param key_LOCK_sync_queue mutex instrumentation key
190 @param key_LOCK_commit_queue mutex instrumentation key
191 @param key_LOCK_done mutex instrumentation key
192 @param key_LOCK_wait_for_group_turn mutex instrumentation key
193 @param key_COND_done cond instrumentation key
194 @param key_COND_flush_queue cond instrumentation key
195 @param key_COND_wait_for_group_turn cond instrumentation key
196 */
197 void init(PSI_mutex_key key_LOCK_flush_queue,
198 PSI_mutex_key key_LOCK_sync_queue,
199 PSI_mutex_key key_LOCK_commit_queue, PSI_mutex_key key_LOCK_done,
200 PSI_mutex_key key_LOCK_wait_for_group_turn,
201 PSI_cond_key key_COND_done, PSI_cond_key key_COND_flush_queue,
202 PSI_cond_key key_COND_wait_for_group_turn);
203
204 /**
205 Deinitializes m_stage_cond_binlog, m_stage_cond_commit_order,
206 m_stage_cond_leader condition variables and m_lock_done mutex.
207 */
208 void deinit();
209
210 /**
211 Waits for the THD session parameter underlying BGC ticket to become
212 active.
213
214 @param thd The THD session that holds the ticket to wait for.
215 @param update_ticket_manager Indicates whether to mark ticket
216 as consumed by the session (add session to processed sessions)
217 after the ticket is opened for processing.
218 */
219 void wait_for_ticket_turn(THD *thd, bool update_ticket_manager = true);
220
221 /**
222 Appends the given THD session object to the given stage queue. It
223 verifies that the given session's ticket is the active ticket, if not,
224 waits on `m_cond_wait_for_ticket_turn` condition variable until it is.
225
226 @param stage The stage to add the THD parameter to.
227 @param thd The THD session object to queue.
228
229 @return True if the session is a group leader, false otherwise.
230 */
231 bool append_to(StageID stage, THD *thd);
232
233 /**
234 Enroll a set of sessions for a stage.
235
236 This will queue the session thread for writing and flushing.
237
238 If the thread being queued is assigned as stage leader, it will
239 return immediately.
240
241 If wait_if_follower is true the thread is not the stage leader,
242 the thread will be wait for the queue to be processed by the
243 leader before it returns.
244 In DBUG-ON version the follower marks is preempt status as ready.
245
246 The session threads entering this function acquires mutexes, and few of
247 them are not released while exiting based on thread and stage type.
248 - A binlog leader (returning true when stage!=COMMIT_ORDER_FLUSH_STAGE) will
249 acquire the stage mutex in this function and not release it.
250 - A commit order leader of the flush stage (returning true when
251 stage==COMMIT_ORDER_FLUSH_STAGE) will acquire both the stage mutex and the
252 flush queue mutex in this function, and not release them.
253 - A follower (returning false) will release any mutexes it takes, before
254 returning from the function.
255
256 @param[in] stage Stage identifier for the queue to append to.
257 @param[in] first Queue to append.
258 @param[in] stage_mutex
259 Pointer to the currently held stage mutex, or nullptr if we're
260 not in a stage, that will be released when changing stage.
261 @param[in] enter_mutex
262 Pointer to the mutex that will be taken when changing stage.
263
264 @retval true Thread is stage leader.
265 @retval false Thread was not stage leader and processing has been done.
266 */
267 bool enroll_for(StageID stage, THD *first, mysql_mutex_t *stage_mutex,
268 mysql_mutex_t *enter_mutex);
269
270 /**
271 Remove first member from the queue for given stage
272
273 @retval Returns std::pair<bool, THD *> object.
274 The first boolean value of pair if true determines queue
275 is not empty, and false determines queue is empty.
276 The second value returns the first removed member.
277 */
278 std::pair<bool, THD *> pop_front(StageID stage) {
279 return m_queue[stage].pop_front();
280 }
281
282#ifndef NDEBUG
283 /**
284 The method ensures the follower's execution path can be preempted
285 by the leader's thread.
286 Preempt status of @c head follower is checked to engange the leader
287 into waiting when set.
288
289 @param head THD* of a follower thread
290 */
291 void clear_preempt_status(THD *head);
292#endif
293
294 /**
295 Fetch the entire queue and empty it. It acquires queue lock before fetching
296 and emptying the queue threads.
297
298 @param[in] stage Stage identifier for the queue to append to.
299
300 @return Pointer to the first session of the queue.
301 */
303
304 /**
305 Fetch the entire queue and empty it. The caller must acquire queue lock
306 before calling this function.
307
308 @param[in] stage Stage identifier for the queue to append to.
309
310 @return Pointer to the first session of the queue.
311 */
313
314 /**
315 Introduces a wait operation on the executing thread. The
316 waiting is done until the timeout elapses or count is
317 reached (whichever comes first).
318
319 If count == 0, then the session will wait until the timeout
320 elapses. If timeout == 0, then there is no waiting.
321
322 @param usec the number of microseconds to wait.
323 @param count wait for as many as count to join the queue the
324 session is waiting on
325 @param stage which stage queue size to compare count against.
326 */
327 void wait_count_or_timeout(ulong count, long usec, StageID stage);
328
329 /**
330 The function is called after follower thread are processed by leader,
331 to unblock follower threads.
332
333 @param queue the thread list which needs to ne unblocked
334 @param stage Stage identifier current thread belong to.
335 */
337
338 /**
339 Signals threads waiting on their BGC ticket turn.
340
341 @param force Whether or not to force the signaling, despit the state of
342 the ticket manager.
343 */
344 void signal_end_of_ticket(bool force = false);
345 /**
346 Updates the THD session object underlying BGC context.
347
348 @param thd The THD object to update the BGC context for.
349 */
351 /**
352 Adds the given session count to the total of processed sessions in the
353 ticket manager active window, ends the active window if possible and
354 notifies other threads that are waiting for a given ticket to have an
355 active processing window.
356
357 @param sessions_count The number of sessions to add to the ticket
358 manager processed sessions count.
359 @param session_ticket The session ticket (used for validations).
360 */
361 void update_ticket_manager(std::uint64_t sessions_count,
362 const binlog::BgcTicket &session_ticket);
363 /**
364 Waits for the session's ticket, if needed, and resets the session's
365 ticket context.
366
367 @param thd The THD sessions object to finish the ticket's related work.
368 */
369 void finish_session_ticket(THD *thd);
370
371 /**
372 This function gets called after transactions are flushed to the engine
373 i.e. after calling ha_flush_logs, to unblock commit order thread list
374 which are not needed to wait for other stages.
375
376 @param first the thread list which needs to ne unblocked
377 */
379
380 /**
381 Wrapper on Mutex_queue lock(), acquires lock on stage queue.
382
383 @param[in] stage Stage identifier for the queue to append to.
384 */
385 void lock_queue(StageID stage) { m_queue[stage].lock(); }
386
387 /**
388 Wrapper on Mutex_queue unlock(), releases lock on stage queue.
389
390 @param[in] stage Stage identifier for the queue to append to.
391 */
392 void unlock_queue(StageID stage) { m_queue[stage].unlock(); }
393
394 /**
395 Disables the ability for session BGC tickets to be set manually.
396 */
397 static void disable_manual_session_tickets();
398 /**
399 Enables the ability for session BGC tickets to be set manually.
400 */
401 static void enable_manual_session_tickets();
402
403 private:
404 /** check if Commit_stage_manager variables already initialized. */
406
407 /**
408 Queues for sessions.
409
410 We need four queues:
411 - Binlog flush queue: transactions that are going to be flushed to the
412 engine and written to the binary log.
413 - Commit order flush queue: transactions that are not going to write the
414 binlog at all, but participate in the beginning
415 of the group commit, up to and including the
416 engine flush.
417 - Sync queue: transactions that are going to be synced to disk
418 - Commit queue: transactions that are going to to be committed
419 (when binlog_order_commit=1).
420 */
422
423 /**
424 The binlog leader waits on this condition variable till it is indicated
425 to wake up. If binlog flush queue gets first thread in the queue but
426 by then commit order flush queue has already elected leader. The the
427 first thread of binlog queue waits on this condition variable and get
428 signalled to wake up from commit order flush queue leader later.
429 */
431
432 /**
433 Condition variable to indicate that the binlog threads can wake up
434 and continue.
435 */
437
438 /**
439 Condition variable to indicate that the flush to storage engine
440 is done and commit order threads can again wake up and continue.
441 */
443
444 /** Mutex used for the condition variable above */
446
447 /** Mutex used for the stage level locks */
449
450#ifndef NDEBUG
451 /** Save pointer to leader thread which is used later to awake leader */
453
454 /** Flag is set by Leader when it starts waiting for follower's all-clear */
456
457 /** Condition variable to indicate a follower started waiting for commit */
459#endif
460
461 /** Condition variable to wait for a given ticket to become active. */
463 /** Mutex to protect the wait for a given ticket to become active. */
465};
466
467#endif /*RPL_COMMIT_STAGE_MANAGER*/
Definition: rpl_commit_stage_manager.h:42
THD * fetch_and_empty_acquire_lock()
Fetch the entire queue for a stage.
Definition: rpl_commit_stage_manager.cc:389
THD * fetch_and_empty_skip_acquire_lock()
Fetch the entire queue for a stage.
Definition: rpl_commit_stage_manager.cc:396
std::atomic< int32 > m_size
size of the queue
Definition: rpl_commit_stage_manager.h:136
mysql_mutex_t * m_lock
Lock for protecting the queue.
Definition: rpl_commit_stage_manager.h:139
bool is_empty() const
Definition: rpl_commit_stage_manager.h:50
bool append(THD *first)
Append a linked list of threads to the queue.
Definition: rpl_commit_stage_manager.cc:39
void assert_owner()
Definition: rpl_commit_stage_manager.h:111
THD * get_leader()
Fetch the first thread of the queue.
Definition: rpl_commit_stage_manager.h:102
void init(mysql_mutex_t *lock)
Definition: rpl_commit_stage_manager.h:48
int32 get_size()
Get number of elements in the queue.
Definition: rpl_commit_stage_manager.h:95
void unlock()
Definition: rpl_commit_stage_manager.h:109
void lock()
Definition: rpl_commit_stage_manager.h:104
THD ** m_last
Pointer to the location holding the end of the queue.
Definition: rpl_commit_stage_manager.h:133
THD * fetch_and_empty()
Fetch the entire queue for a stage.
Definition: rpl_commit_stage_manager.cc:401
Mutex_queue()
Definition: rpl_commit_stage_manager.h:46
std::pair< bool, THD * > pop_front()
Remove first member from the queue.
Definition: rpl_commit_stage_manager.cc:73
THD * m_first
Pointer to the first thread in the queue, or nullptr if the queue is empty.
Definition: rpl_commit_stage_manager.h:125
Class for maintaining the commit stages for binary log group commit.
Definition: rpl_commit_stage_manager.h:40
const Commit_stage_manager & operator=(const Commit_stage_manager &)=delete
void update_session_ticket_state(THD *thd)
Updates the THD session object underlying BGC context.
Definition: rpl_commit_stage_manager.cc:492
mysql_mutex_t m_lock_done
Mutex used for the condition variable above.
Definition: rpl_commit_stage_manager.h:445
void wait_for_ticket_turn(THD *thd, bool update_ticket_manager=true)
Waits for the THD session parameter underlying BGC ticket to become active.
Definition: rpl_commit_stage_manager.cc:157
std::pair< bool, THD * > pop_front(StageID stage)
Remove first member from the queue for given stage.
Definition: rpl_commit_stage_manager.h:278
mysql_cond_t m_stage_cond_leader
The binlog leader waits on this condition variable till it is indicated to wake up.
Definition: rpl_commit_stage_manager.h:430
void deinit()
Deinitializes m_stage_cond_binlog, m_stage_cond_commit_order, m_stage_cond_leader condition variables...
Definition: rpl_commit_stage_manager.cc:143
void finish_session_ticket(THD *thd)
Waits for the session's ticket, if needed, and resets the session's ticket context.
Definition: rpl_commit_stage_manager.cc:509
bool leader_await_preempt_status
Flag is set by Leader when it starts waiting for follower's all-clear.
Definition: rpl_commit_stage_manager.h:455
mysql_cond_t m_stage_cond_binlog
Condition variable to indicate that the binlog threads can wake up and continue.
Definition: rpl_commit_stage_manager.h:436
void clear_preempt_status(THD *head)
The method ensures the follower's execution path can be preempted by the leader's thread.
Definition: rpl_commit_stage_manager.cc:529
Mutex_queue m_queue[STAGE_COUNTER]
Queues for sessions.
Definition: rpl_commit_stage_manager.h:421
Commit_stage_manager(const Commit_stage_manager &)=delete
void signal_end_of_ticket(bool force=false)
Signals threads waiting on their BGC ticket turn.
Definition: rpl_commit_stage_manager.cc:477
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 ...
Definition: rpl_commit_stage_manager.h:442
mysql_mutex_t m_lock_wait_for_ticket_turn
Mutex to protect the wait for a given ticket to become active.
Definition: rpl_commit_stage_manager.h:464
mysql_cond_t m_cond_wait_for_ticket_turn
Condition variable to wait for a given ticket to become active.
Definition: rpl_commit_stage_manager.h:462
bool append_to(StageID stage, THD *thd)
Appends the given THD session object to the given stage queue.
Definition: rpl_commit_stage_manager.cc:190
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,...
Definition: rpl_commit_stage_manager.cc:501
mysql_mutex_t m_queue_lock[STAGE_COUNTER - 1]
Mutex used for the stage level locks.
Definition: rpl_commit_stage_manager.h:448
StageID
Constants for queues for different stages.
Definition: rpl_commit_stage_manager.h:165
@ COMMIT_ORDER_FLUSH_STAGE
Definition: rpl_commit_stage_manager.h:169
@ COMMIT_STAGE
Definition: rpl_commit_stage_manager.h:168
@ BINLOG_FLUSH_STAGE
Definition: rpl_commit_stage_manager.h:166
@ SYNC_STAGE
Definition: rpl_commit_stage_manager.h:167
@ STAGE_COUNTER
Definition: rpl_commit_stage_manager.h:170
static Commit_stage_manager & get_instance()
Fetch Commit_stage_manager class instance.
Definition: rpl_commit_stage_manager.cc:542
static void enable_manual_session_tickets()
Enables the ability for session BGC tickets to be set manually.
Definition: rpl_commit_stage_manager.cc:524
THD * fetch_queue_acquire_lock(StageID stage)
Fetch the entire queue and empty it.
Definition: rpl_commit_stage_manager.cc:442
bool m_is_initialized
check if Commit_stage_manager variables already initialized.
Definition: rpl_commit_stage_manager.h:405
Commit_stage_manager()
Definition: rpl_commit_stage_manager.h:148
void wait_count_or_timeout(ulong count, long usec, StageID stage)
Introduces a wait operation on the executing thread.
Definition: rpl_commit_stage_manager.cc:419
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_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 a...
Definition: rpl_commit_stage_manager.cc:98
static void disable_manual_session_tickets()
Disables the ability for session BGC tickets to be set manually.
Definition: rpl_commit_stage_manager.cc:517
void lock_queue(StageID stage)
Wrapper on Mutex_queue lock(), acquires lock on stage queue.
Definition: rpl_commit_stage_manager.h:385
THD * leader_thd
Save pointer to leader thread which is used later to awake leader.
Definition: rpl_commit_stage_manager.h:452
void process_final_stage_for_ordered_commit_group(THD *first)
This function gets called after transactions are flushed to the engine i.e.
Definition: rpl_commit_stage_manager.cc:452
THD * fetch_queue_skip_acquire_lock(StageID stage)
Fetch the entire queue and empty it.
Definition: rpl_commit_stage_manager.cc:447
mysql_cond_t m_cond_preempt
Condition variable to indicate a follower started waiting for commit.
Definition: rpl_commit_stage_manager.h:458
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.
Definition: rpl_commit_stage_manager.cc:460
void unlock_queue(StageID stage)
Wrapper on Mutex_queue unlock(), releases lock on stage queue.
Definition: rpl_commit_stage_manager.h:392
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.
Definition: rpl_commit_stage_manager.cc:199
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:33
Represents the Binlog Group Commit Ticket - BGC Ticket.
Definition: bgc_ticket.h:52
#define mysql_mutex_lock(M)
Definition: mysql_mutex.h:49
#define mysql_mutex_unlock(M)
Definition: mysql_mutex.h:56
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
unsigned int PSI_cond_key
Instrumented cond key.
Definition: psi_cond_bits.h:43
unsigned int PSI_mutex_key
Instrumented mutex key.
Definition: psi_mutex_bits.h:51
#define mysql_mutex_assert_not_owner(M)
Wrapper, to use safe_mutex_assert_not_owner with instrumented mutexes.
Definition: mysql_mutex.h:125
#define mysql_mutex_assert_owner(M)
Wrapper, to use safe_mutex_assert_owner with instrumented mutexes.
Definition: mysql_mutex.h:111
int32_t int32
Definition: my_inttypes.h:65
static int count
Definition: myisam_ftdump.cc:42
static QUEUE queue
Definition: myisampack.cc:206
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:78
Instrumentation helpers for conditions.
Instrumentation helpers for mutexes.
An instrumented cond structure.
Definition: mysql_cond_bits.h:49
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
MySQL mutex implementation.