MySQL 8.0.39
Source Code Documentation
consistency_manager.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef CONSISTENCY_MANAGER_INCLUDED
25#define CONSISTENCY_MANAGER_INCLUDED
26
27#define CONSISTENCY_INFO_OUTCOME_OK 0
28#define CONSISTENCY_INFO_OUTCOME_ERROR 1
29#define CONSISTENCY_INFO_OUTCOME_COMMIT 2
30
33#include <atomic>
34#include <list>
35#include <map>
36#include <utility>
37
43
44/**
45 @class Transaction_consistency_info
46
47 The consistency information of a transaction, including its
48 configuration and state.
49*/
51 public:
52 /*
53 Allocate memory on the heap with instrumented memory allocation, so
54 that memory consumption can be tracked.
55
56 @param[in] size memory size to be allocated
57 @param[in] nothrow When the nothrow constant is passed as second parameter
58 to operator new, operator new returns a null-pointer on
59 failure instead of throwing a bad_alloc exception.
60
61 @return pointer to the allocated memory, or NULL if memory could not
62 be allocated.
63 */
64 void *operator new(size_t size, const std::nothrow_t &) noexcept {
65 /*
66 Call my_malloc() with the MY_WME flag to make sure that it will
67 write an error message if the memory could not be allocated.
68 */
70 }
71
72 /*
73 Deallocate memory on the heap with instrumented memory allocation, so
74 that memory consumption can be tracked.
75
76 @param[in] ptr pointer to the allocated memory
77 @param[in] nothrow When the nothrow constant is passed as second parameter
78 to operator new, operator new returns a null-pointer on
79 failure instead of throwing a bad_alloc exception.
80 */
81 void operator delete(void *ptr, const std::nothrow_t &) noexcept {
82 my_free(ptr);
83 }
84
85 /**
86 Allocate memory on the heap with instrumented memory allocation, so
87 that memory consumption can be tracked.
88
89 @param[in] size memory size to be allocated
90
91 @return pointer to the allocated memory, or NULL if memory could not
92 be allocated.
93 */
94 void *operator new(size_t size) noexcept {
95 /*
96 Call my_malloc() with the MY_WME flag to make sure that it will
97 write an error message if the memory could not be allocated.
98 */
100 }
101
102 /**
103 Deallocate memory on the heap with instrumented memory allocation, so
104 that memory consumption can be tracked.
105
106 @param[in] ptr pointer to the allocated memory
107 */
108 void operator delete(void *ptr) noexcept { my_free(ptr); }
109
110 /**
111 Constructor
112
113 @param[in] thread_id the thread that is executing the transaction
114 @param[in] local_transaction true if this transaction did originate from
115 this server
116 @param[in] sid transaction sid
117 @param[in] sidno transaction sidno
118 @param[in] gno transaction gno
119 @param[in] consistency_level the transaction consistency
120 @param[in] members_that_must_prepare_the_transaction
121 list of the members that must prepare the
122 transaction before it is allowed to commit
123 */
125 my_thread_id thread_id, bool local_transaction, const rpl_sid *sid,
126 rpl_sidno sidno, rpl_gno gno,
128 Members_list *members_that_must_prepare_the_transaction);
129
131
132 /**
133 Get the thread id that is executing the transaction.
134
135 @return the thread id
136 */
138
139 /**
140 Is the transaction from this server?
141
142 @return true yes
143 false otherwise
144 */
146
147 /**
148 Is the transaction prepared locally?
149
150 @return true yes
151 false otherwise
152 */
154
155 /**
156 Get the transaction sidno.
157
158 @return the sidno
159 */
161
162 /**
163 Get the transaction gno.
164
165 @return the gno
166 */
168
169 /**
170 Get the transaction consistency.
171
172 @return the consistency
173 */
175
176 /**
177 Is this transaction running on a single member group?
178
179 @return true yes
180 false otherwise
181 */
183
184 /**
185 Did all other ONLINE members already prepared the transaction?
186
187 @return true yes
188 false otherwise
189 */
191
192 /**
193 Call action after this transaction being prepared on this member
194 applier.
195
196 @param[in] thread_id the applier thread id
197 @param[in] member_status this member status
198
199 @return Operation status
200 @retval 0 OK
201 @retval !=0 error
202 */
206
207 /**
208 Call action after this transaction being prepared by other member.
209
210 @param[in] gcs_member_id the member id
211
212 @return Operation status
213 @retval CONSISTENCY_INFO_OUTCOME_OK OK
214 @retval CONSISTENCY_INFO_OUTCOME_ERROR error
215 @retval CONSISTENCY_INFO_OUTCOME_COMMIT transaction must proceeded to
216 commit
217 */
218 int handle_remote_prepare(const Gcs_member_identifier &gcs_member_id);
219
220 /**
221 Call action after members leave the group.
222 If any of these members are on the prepare wait list, they will
223 be removed. If the lists becomes empty, the transaction will proceed
224 to commit.
225
226 @param[in] leaving_members the members that left
227
228 @return Operation status
229 @retval 0 OK
230 @retval !=0 error
231 */
233 const std::vector<Gcs_member_identifier> &leaving_members);
234
235 private:
238 const bool m_sid_specified;
244 std::unique_ptr<Checkable_rwlock>
248};
249
250typedef std::pair<rpl_sidno, rpl_gno> Transaction_consistency_manager_key;
254typedef std::pair<Pipeline_event *, Transaction_consistency_manager_key>
256typedef std::map<
258 std::less<Transaction_consistency_manager_key>,
262
263/**
264 @class Transaction_consistency_manager
265
266 The consistency information of all ongoing transactions which have
267 consistency GROUP_REPLICATION_CONSISTENCY_BEFORE,
268 GROUP_REPLICATION_CONSISTENCY_AFTER or
269 GROUP_REPLICATION_CONSISTENCY_BEFORE_AND_AFTER.
270*/
272 public:
273 /**
274 Constructor.
275 */
277
279
280 /**
281 Clear all information.
282 */
283 void clear();
284
285 /**
286 Call action after a transaction is certified.
287 The transaction coordination among the members will start on
288 this point.
289
290 @param[in] transaction_info the transaction info
291
292 @return Operation status
293 @retval 0 OK
294 @retval !=0 error
295 */
297
298 /**
299 Call action after a transaction being prepared on this member
300 applier.
301
302 @param[in] sidno the transaction sidno
303 @param[in] gno the transaction gno
304 @param[in] thread_id the applier thread id
305 @param[in] member_status this member status
306
307 @return Operation status
308 @retval 0 OK
309 @retval !=0 error
310 */
314
315 /**
316 Call action after a transaction being prepared by other member.
317
318 If this sid is NULL that means this transaction sid is the group
319 name.
320
321 @param[in] sid the transaction sid
322 @param[in] gno the transaction gno
323 @param[in] gcs_member_id the member id
324
325 @return Operation status
326 @retval 0 OK
327 @retval !=0 error
328 */
329 int handle_remote_prepare(const rpl_sid *sid, rpl_gno gno,
330 const Gcs_member_identifier &gcs_member_id);
331
332 /**
333 Call action after members leave the group.
334 If any of these members are on the prepare wait lists, they will
335 be removed. If any those lists become empty, those transactions
336 proceed to commit.
337
338 @param[in] leaving_members the members that left
339
340 @return Operation status
341 @retval 0 OK
342 @retval !=0 error
343 */
345 const std::vector<Gcs_member_identifier> &leaving_members);
346
347 /**
348 Call action after commit a transaction on this member.
349 If new transactions are waiting for this prepared transaction
350 to be committed, they will be released.
351
352 @param[in] thread_id the transaction thread id
353 @param[in] sidno the transaction sidno
354 @param[in] gno the transaction gno
355
356 @return Operation status
357 @retval 0 OK
358 @retval !=0 error
359 */
361 rpl_gno gno) override;
362
363 /**
364 Call action before a transaction starts.
365 It will handle transactions with
366 GROUP_REPLICATION_CONSISTENCY_BEFORE consistency and any others
367 that need to wait for preceding prepared transactions to
368 commit.
369
370 @param[in] thread_id the thread that is executing the
371 transaction
372 @param[in] gr_consistency_level the transaction consistency
373 @param[in] timeout maximum time to wait
374 @param[in] rpl_channel_type type of the channel that receives the
375 transaction
376
377 @return Operation status
378 @retval 0 OK
379 @retval !=0 error
380 */
382 ulong gr_consistency_level, ulong timeout,
383 enum_rpl_channel_type rpl_channel_type) override;
384
385 /**
386 Call action once a Sync_before_execution_message is received,
387 this will allow fetch the group transactions set ordered with
388 the message order.
389
390 @param[in] thread_id the thread that is executing the
391 transaction
392 @param[in] gcs_member_id the member id
393
394 @return Operation status
395 @retval 0 OK
396 @retval !=0 error
397 */
399 my_thread_id thread_id, const Gcs_member_identifier &gcs_member_id) const;
400
401 /**
402 Are there local prepared transactions waiting for prepare
403 acknowledge from other members?
404
405 @return true yes
406 false otherwise
407 */
409
410 /**
411 Schedule a View_change_log_event log into the relay to after
412 the local prepared transactions are complete, since those
413 transactions belong to the previous view and as such must be
414 logged before this view.
415
416 @param[in] pevent the pipeline event that contains
417 the View_change_log_event
418
419 @return Operation status
420 @retval 0 OK
421 @retval !=0 error
422 */
424
425 /**
426 Inform that plugin did start.
427 */
428 void plugin_started();
429
430 /**
431 Inform that plugin is stopping.
432 New consistent transactions are not allowed to start.
433 On after_applier_prepare the transactions do not wait
434 for other prepares.
435 */
436 void plugin_is_stopping();
437
438 /**
439 Register an observer for transactions
440 */
442
443 /**
444 Unregister the observer for transactions
445 */
447
448 int before_commit(
451
452 int before_rollback(
455
457
458 /**
459 Tells the consistency manager that a primary election is running so it
460 shall enable primary election checks
461 */
463
464 /**
465 Tells the consistency manager that a primary election ended so it
466 shall disable primary election checks
467 */
469
470 private:
471 /**
472 Help method called by transaction begin action that, for
473 transactions with consistency GROUP_REPLICATION_CONSISTENCY_BEFORE
474 or GROUP_REPLICATION_CONSISTENCY_BEFORE_AND_AFTER will:
475 1) send a message to all members;
476 2) when that message is received and processed in-order,
477 w.r.t. the message stream, will fetch the Group Replication
478 applier RECEIVED_TRANSACTION_SET, the set of remote
479 transactions that were allowed to commit;
480 3) wait until all the transactions on Group Replication applier
481 RECEIVED_TRANSACTION_SET are committed.
482
483 @param[in] thread_id the thread that is executing the
484 transaction
485 @param[in] consistency_level the transaction consistency
486 @param[in] timeout maximum time to wait
487
488 @return Operation status
489 @retval 0 OK
490 @retval !=0 error
491 */
495 ulong timeout) const;
496
497 /**
498 Help method called by transaction begin action that, if there are
499 precedent prepared transactions with consistency
500 GROUP_REPLICATION_CONSISTENCY_AFTER or
501 GROUP_REPLICATION_CONSISTENCY_BEFORE_AND_AFTER, will hold the
502 this transaction until the prepared are committed.
503
504 @param[in] thread_id the thread that is executing the
505 transaction
506 @param[in] timeout maximum time to wait
507
508 @return Operation status
509 @retval 0 OK
510 @retval !=0 error
511 */
513 ulong timeout);
514
515 /**
516 Help method that cleans prepared transactions and releases
517 transactions waiting on them.
518
519 @param[in] key the transaction key
520
521 @return Operation status
522 @retval 0 OK
523 @retval !=0 error
524 */
526
529
531
535 std::list<my_thread_id, Malloc_allocator<my_thread_id>>
541
542 std::atomic<bool> m_plugin_stopping;
543 std::atomic<bool> m_primary_election_active;
544
545 /** Hold transaction mechanism */
547};
548
549#endif /* CONSISTENCY_MANAGER_INCLUDED */
This has the functionality of mysql_rwlock_t, with two differences:
Definition: rpl_gtid.h:309
It represents the identity of a group member within a certain group.
Definition: gcs_member_identifier.h:40
Group_member_status
Definition: member_info.h:169
Listener for transaction life cycle events.
Definition: group_transaction_observation_manager.h:33
enum_transaction_origin
Enum for transaction origins.
Definition: group_transaction_observation_manager.h:36
Class that contains the logic to hold transactions when group_replication_consistency is set to BEFOR...
Definition: hold_transactions.h:36
Malloc_allocator is a C++ STL memory allocator based on my_malloc/my_free.
Definition: malloc_allocator.h:63
A wrapper for log events/packets.
Definition: pipeline_interfaces.h:128
The consistency information of a transaction, including its configuration and state.
Definition: consistency_manager.h:50
my_thread_id m_thread_id
Definition: consistency_manager.h:236
bool m_transaction_prepared_locally
Definition: consistency_manager.h:246
bool is_transaction_prepared_locally()
Is the transaction prepared locally?
Definition: consistency_manager.cc:87
const bool m_local_transaction
Definition: consistency_manager.h:237
const rpl_sidno m_sidno
Definition: consistency_manager.h:240
int handle_member_leave(const std::vector< Gcs_member_identifier > &leaving_members)
Call action after members leave the group.
Definition: consistency_manager.cc:220
Transaction_consistency_info(my_thread_id thread_id, bool local_transaction, const rpl_sid *sid, rpl_sidno sidno, rpl_gno gno, enum_group_replication_consistency_level consistency_level, Members_list *members_that_must_prepare_the_transaction)
Constructor.
Definition: consistency_manager.cc:33
const enum_group_replication_consistency_level m_consistency_level
Definition: consistency_manager.h:242
rpl_sid m_sid
Definition: consistency_manager.h:239
const bool m_sid_specified
Definition: consistency_manager.h:238
int handle_remote_prepare(const Gcs_member_identifier &gcs_member_id)
Call action after this transaction being prepared by other member.
Definition: consistency_manager.cc:181
const rpl_gno m_gno
Definition: consistency_manager.h:241
int after_applier_prepare(my_thread_id thread_id, Group_member_info::Group_member_status member_status)
Call action after this transaction being prepared on this member applier.
Definition: consistency_manager.cc:113
virtual ~Transaction_consistency_info()
Definition: consistency_manager.cc:75
std::unique_ptr< Checkable_rwlock > m_members_that_must_prepare_the_transaction_lock
Definition: consistency_manager.h:245
bool m_transaction_prepared_remotely
Definition: consistency_manager.h:247
enum_group_replication_consistency_level get_consistency_level()
Get the transaction consistency.
Definition: consistency_manager.cc:96
bool is_the_transaction_prepared_remotely()
Did all other ONLINE members already prepared the transaction?
Definition: consistency_manager.cc:106
rpl_gno get_gno()
Get the transaction gno.
Definition: consistency_manager.cc:93
my_thread_id get_thread_id()
Get the thread id that is executing the transaction.
Definition: consistency_manager.cc:79
rpl_sidno get_sidno()
Get the transaction sidno.
Definition: consistency_manager.cc:91
bool is_a_single_member_group()
Is this transaction running on a single member group?
Definition: consistency_manager.cc:100
bool is_local_transaction()
Is the transaction from this server?
Definition: consistency_manager.cc:83
Members_list * m_members_that_must_prepare_the_transaction
Definition: consistency_manager.h:243
The consistency information of all ongoing transactions which have consistency GROUP_REPLICATION_CONS...
Definition: consistency_manager.h:271
Hold_transactions m_hold_transactions
Hold transaction mechanism.
Definition: consistency_manager.h:546
int after_applier_prepare(rpl_sidno sidno, rpl_gno gno, my_thread_id thread_id, Group_member_info::Group_member_status member_status)
Call action after a transaction being prepared on this member applier.
Definition: consistency_manager.cc:370
int transaction_begin_sync_prepared_transactions(my_thread_id thread_id, ulong timeout)
Help method called by transaction begin action that, if there are precedent prepared transactions wit...
Definition: consistency_manager.cc:764
Checkable_rwlock * m_prepared_transactions_on_my_applier_lock
Definition: consistency_manager.h:530
bool has_local_prepared_transactions()
Are there local prepared transactions waiting for prepare acknowledge from other members?
Definition: consistency_manager.cc:823
Transaction_consistency_manager_map m_map
Definition: consistency_manager.h:528
void disable_primary_election_checks()
Tells the consistency manager that a primary election ended so it shall disable primary election chec...
Definition: consistency_manager.cc:923
int handle_sync_before_execution_message(my_thread_id thread_id, const Gcs_member_identifier &gcs_member_id) const
Call action once a Sync_before_execution_message is received, this will allow fetch the group transac...
Definition: consistency_manager.cc:745
int handle_member_leave(const std::vector< Gcs_member_identifier > &leaving_members)
Call action after members leave the group.
Definition: consistency_manager.cc:575
void register_transaction_observer()
Register an observer for transactions.
Definition: consistency_manager.cc:910
int remove_prepared_transaction(Transaction_consistency_manager_key key)
Help method that cleans prepared transactions and releases transactions waiting on them.
Definition: consistency_manager.cc:857
~Transaction_consistency_manager() override
Definition: consistency_manager.cc:274
void plugin_is_stopping()
Inform that plugin is stopping.
Definition: consistency_manager.cc:906
std::list< Transaction_consistency_manager_pevent_pair, Malloc_allocator< Transaction_consistency_manager_pevent_pair > > m_delayed_view_change_events
Definition: consistency_manager.h:539
int transaction_begin_sync_before_execution(my_thread_id thread_id, enum_group_replication_consistency_level consistency_level, ulong timeout) const
Help method called by transaction begin action that, for transactions with consistency GROUP_REPLICAT...
Definition: consistency_manager.cc:674
std::list< my_thread_id, Malloc_allocator< my_thread_id > > m_new_transactions_waiting
Definition: consistency_manager.h:536
int handle_remote_prepare(const rpl_sid *sid, rpl_gno gno, const Gcs_member_identifier &gcs_member_id)
Call action after a transaction being prepared by other member.
Definition: consistency_manager.cc:460
std::list< Transaction_consistency_manager_key, Malloc_allocator< Transaction_consistency_manager_key > > m_prepared_transactions_on_my_applier
Definition: consistency_manager.h:534
Transaction_consistency_manager_key m_last_local_transaction
Definition: consistency_manager.h:540
void unregister_transaction_observer()
Unregister the observer for transactions.
Definition: consistency_manager.cc:914
int after_rollback(my_thread_id thread_id) override
Executed after rollback.
Definition: consistency_manager.cc:942
int before_transaction_begin(my_thread_id thread_id, ulong gr_consistency_level, ulong timeout, enum_rpl_channel_type rpl_channel_type) override
Call action before a transaction starts.
Definition: consistency_manager.cc:620
void clear()
Clear all information.
Definition: consistency_manager.cc:283
int after_certification(Transaction_consistency_info *transaction_info)
Call action after a transaction is certified.
Definition: consistency_manager.cc:307
int before_rollback(my_thread_id thread_id, Group_transaction_listener::enum_transaction_origin origin) override
Executed before rollback.
Definition: consistency_manager.cc:937
std::atomic< bool > m_plugin_stopping
Definition: consistency_manager.h:542
int after_commit(my_thread_id thread_id, rpl_sidno sidno, rpl_gno gno) override
Call action after commit a transaction on this member.
Definition: consistency_manager.cc:601
Checkable_rwlock * m_map_lock
Definition: consistency_manager.h:527
std::atomic< bool > m_primary_election_active
Definition: consistency_manager.h:543
int schedule_view_change_event(Pipeline_event *pevent)
Schedule a View_change_log_event log into the relay to after the local prepared transactions are comp...
Definition: consistency_manager.cc:844
Transaction_consistency_manager()
Constructor.
Definition: consistency_manager.cc:246
void enable_primary_election_checks()
Tells the consistency manager that a primary election is running so it shall enable primary election ...
Definition: consistency_manager.cc:918
int before_commit(my_thread_id thread_id, Group_transaction_listener::enum_transaction_origin origin) override
Executed before commit.
Definition: consistency_manager.cc:932
void plugin_started()
Inform that plugin did start.
Definition: consistency_manager.cc:902
std::pair< Transaction_consistency_manager_key, Transaction_consistency_info * > Transaction_consistency_manager_pair
Definition: consistency_manager.h:253
std::map< Transaction_consistency_manager_key, Transaction_consistency_info *, std::less< Transaction_consistency_manager_key >, Malloc_allocator< std::pair< const Transaction_consistency_manager_key, Transaction_consistency_info * > > > Transaction_consistency_manager_map
Definition: consistency_manager.h:261
std::pair< Pipeline_event *, Transaction_consistency_manager_key > Transaction_consistency_manager_pevent_pair
Definition: consistency_manager.h:255
std::pair< rpl_sidno, rpl_gno > Transaction_consistency_manager_key
Definition: consistency_manager.h:250
#define MY_WME
Definition: my_sys.h:123
#define MYF(v)
Definition: my_inttypes.h:97
void * my_malloc(PSI_memory_key key, size_t size, int flags)
Allocates size bytes of memory.
Definition: my_memory.cc:57
void my_free(void *ptr)
Frees the memory pointed by the ptr.
Definition: my_memory.cc:81
static my_thread_id thread_id
Definition: my_thr_init.cc:63
uint32 my_thread_id
Definition: my_thread_local.h:34
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:496
std::map< Key, Value, Compare, ut::allocator< std::pair< const Key, Value > > > map
Specialization of map which uses ut_allocator.
Definition: ut0new.h:2892
std::list< T, ut::allocator< T > > list
Specialization of list which uses ut_allocator.
Definition: ut0new.h:2878
std::list< Gcs_member_identifier, Malloc_allocator< Gcs_member_identifier > > Members_list
Definition: pipeline_interfaces.h:40
API for Group Replication plugin.
enum_group_replication_consistency_level
Definition: plugin_group_replication.h:35
PSI_memory_key key_consistent_transactions
Definition: plugin_psi.h:245
required string key
Definition: replication_asynchronous_connection_failover.proto:60
enum_rpl_channel_type
Type of replication channel thread/transaction might be associated to.
Definition: rpl_context.h:49
int rpl_sidno
Type of SIDNO (source ID number, first component of GTID)
Definition: rpl_gtid.h:96
binary_log::gtids::gno_t rpl_gno
GNO, the second (numeric) component of a GTID, is an alias of binary_log::gtids::gno_t.
Definition: rpl_gtid.h:103
This is a POD.
Definition: uuid.h:61