MySQL  8.0.19
Source Code Documentation
consistency_manager.h
Go to the documentation of this file.
1 /* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
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 CONSISTENCY_MANAGER_INCLUDED
24 #define CONSISTENCY_MANAGER_INCLUDED
25 
26 #define CONSISTENCY_INFO_OUTCOME_OK 0
27 #define CONSISTENCY_INFO_OUTCOME_ERROR 1
28 #define CONSISTENCY_INFO_OUTCOME_COMMIT 2
29 
32 #include <atomic>
33 #include <list>
34 #include <map>
35 #include <utility>
36 
37 #include "my_dbug.h"
42 
43 /**
44  @class Transaction_consistency_info
45 
46  The consistency information of a transaction, including its
47  configuration and state.
48 */
50  public:
51  /**
52  Constructor
53 
54  @param[in] thread_id the thread that is executing the transaction
55  @param[in] local_transaction true if this transaction did originate from
56  this server
57  @param[in] sid transaction sid
58  @param[in] sidno transaction sidno
59  @param[in] gno transaction gno
60  @param[in] consistency_level the transaction consistency
61  @param[in] members_that_must_prepare_the_transaction
62  list of the members that must prepare the
63  transaction before it is allowed to commit
64  */
66  my_thread_id thread_id, bool local_transaction, const rpl_sid *sid,
67  rpl_sidno sidno, rpl_gno gno,
69  std::list<Gcs_member_identifier>
70  *members_that_must_prepare_the_transaction);
71 
73 
74  /**
75  Get the thread id that is executing the transaction.
76 
77  @return the thread id
78  */
80 
81  /**
82  Is the transaction from this server?
83 
84  @return true yes
85  false otherwise
86  */
87  bool is_local_transaction();
88 
89  /**
90  Is the transaction prepared locally?
91 
92  @return true yes
93  false otherwise
94  */
96 
97  /**
98  Get the transaction sidno.
99 
100  @return the sidno
101  */
103 
104  /**
105  Get the transaction gno.
106 
107  @return the gno
108  */
109  rpl_gno get_gno();
110 
111  /**
112  Get the transaction consistency.
113 
114  @return the consistency
115  */
117 
118  /**
119  Is this transaction running on a single member group?
120 
121  @return true yes
122  false otherwise
123  */
125 
126  /**
127  Did all other ONLINE members already prepared the transaction?
128 
129  @return true yes
130  false otherwise
131  */
133 
134  /**
135  Call action after this transaction being prepared on this member
136  applier.
137 
138  @param[in] thread_id the applier thread id
139  @param[in] member_status this member status
140 
141  @return Operation status
142  @retval 0 OK
143  @retval !=0 error
144  */
148 
149  /**
150  Call action after this transaction being prepared by other member.
151 
152  @param[in] gcs_member_id the member id
153 
154  @return Operation status
155  @retval CONSISTENCY_INFO_OUTCOME_OK OK
156  @retval CONSISTENCY_INFO_OUTCOME_ERROR error
157  @retval CONSISTENCY_INFO_OUTCOME_COMMIT transaction must proceeded to
158  commit
159  */
160  int handle_remote_prepare(const Gcs_member_identifier &gcs_member_id);
161 
162  /**
163  Call action after members leave the group.
164  If any of these members are on the prepare wait list, they will
165  be removed. If the lists becomes empty, the transaction will proceed
166  to commit.
167 
168  @param[in] leaving_members the members that left
169 
170  @return Operation status
171  @retval 0 OK
172  @retval !=0 error
173  */
175  const std::vector<Gcs_member_identifier> &leaving_members);
176 
177  private:
180  const bool m_sid_specified;
183  const rpl_gno m_gno;
185  std::list<Gcs_member_identifier> *m_members_that_must_prepare_the_transaction;
188 };
189 
190 typedef std::pair<rpl_sidno, rpl_gno> Transaction_consistency_manager_key;
191 typedef std::pair<Transaction_consistency_manager_key,
194 typedef std::map<Transaction_consistency_manager_key,
197 
198 /**
199  @class Transaction_consistency_manager
200 
201  The consistency information of all ongoing transactions which have
202  consistency GROUP_REPLICATION_CONSISTENCY_BEFORE,
203  GROUP_REPLICATION_CONSISTENCY_AFTER or
204  GROUP_REPLICATION_CONSISTENCY_BEFORE_AND_AFTER.
205 */
207  public:
208  /**
209  Constructor.
210  */
212 
214 
215  /**
216  Clear all information.
217  */
218  void clear();
219 
220  /**
221  Call action after a transaction is certified.
222  The transaction coordination among the members will start on
223  this point.
224 
225  @param[in] transaction_info the transaction info
226 
227  @return Operation status
228  @retval 0 OK
229  @retval !=0 error
230  */
231  int after_certification(Transaction_consistency_info *transaction_info);
232 
233  /**
234  Call action after a transaction being prepared on this member
235  applier.
236 
237  @param[in] sidno the transaction sidno
238  @param[in] gno the transaction gno
239  @param[in] thread_id the applier thread id
240  @param[in] member_status this member status
241 
242  @return Operation status
243  @retval 0 OK
244  @retval !=0 error
245  */
249 
250  /**
251  Call action after a transaction being prepared by other member.
252 
253  If this sid is NULL that means this transaction sid is the group
254  name.
255 
256  @param[in] sid the transaction sid
257  @param[in] gno the transaction gno
258  @param[in] gcs_member_id the member id
259 
260  @return Operation status
261  @retval 0 OK
262  @retval !=0 error
263  */
264  int handle_remote_prepare(const rpl_sid *sid, rpl_gno gno,
265  const Gcs_member_identifier &gcs_member_id);
266 
267  /**
268  Call action after members leave the group.
269  If any of these members are on the prepare wait lists, they will
270  be removed. If any those lists become empty, those transactions
271  proceed to commit.
272 
273  @param[in] leaving_members the members that left
274 
275  @return Operation status
276  @retval 0 OK
277  @retval !=0 error
278  */
280  const std::vector<Gcs_member_identifier> &leaving_members);
281 
282  /**
283  Call action after commit a transaction on this member.
284  If new transactions are waiting for this prepared transaction
285  to be committed, they will be released.
286 
287  @param[in] thread_id the transaction thread id
288  @param[in] sidno the transaction sidno
289  @param[in] gno the transaction gno
290 
291  @return Operation status
292  @retval 0 OK
293  @retval !=0 error
294  */
295  virtual int after_commit(my_thread_id thread_id, rpl_sidno sidno,
296  rpl_gno gno);
297 
298  /**
299  Call action before a transaction starts.
300  It will handle transactions with
301  GROUP_REPLICATION_CONSISTENCY_BEFORE consistency and any others
302  that need to wait for preceding prepared transactions to
303  commit.
304 
305  @param[in] thread_id the thread that is executing the
306  transaction
307  @param[in] gr_consistency_level the transaction consistency
308  @param[in] timeout maximum time to wait
309  @param[in] rpl_channel_type type of the channel that receives the
310  transaction
311 
312  @return Operation status
313  @retval 0 OK
314  @retval !=0 error
315  */
317  ulong gr_consistency_level,
318  ulong timeout,
319  enum_rpl_channel_type rpl_channel_type);
320 
321  /**
322  Call action once a Sync_before_execution_message is received,
323  this will allow fetch the group transactions set ordered with
324  the message order.
325 
326  @param[in] thread_id the thread that is executing the
327  transaction
328  @param[in] gcs_member_id the member id
329 
330  @return Operation status
331  @retval 0 OK
332  @retval !=0 error
333  */
335  my_thread_id thread_id, const Gcs_member_identifier &gcs_member_id) const;
336 
337  /**
338  Are there local prepared transactions waiting for prepare
339  acknowledge from other members?
340 
341  @return true yes
342  false otherwise
343  */
345 
346  /**
347  Schedule a View_change_log_event log into the relay to after
348  the local prepared transactions are complete, since those
349  transactions belong to the previous view and as such must be
350  logged before this view.
351 
352  @param[in] pevent the pipeline event that contains
353  the View_change_log_event
354 
355  @return Operation status
356  @retval 0 OK
357  @retval !=0 error
358  */
360 
361  /**
362  Inform that plugin did start.
363  */
364  void plugin_started();
365 
366  /**
367  Inform that plugin is stopping.
368  New consistent transactions are not allowed to start.
369  On after_applier_prepare the transactions do not wait
370  for other prepares.
371  */
372  void plugin_is_stopping();
373 
374  /**
375  Register an observer for transactions
376  */
378 
379  /**
380  Unregister the observer for transactions
381  */
383 
384  virtual int before_commit(
387 
388  virtual int before_rollback(
391 
393 
394  private:
395  /**
396  Help method called by transaction begin action that, for
397  transactions with consistency GROUP_REPLICATION_CONSISTENCY_BEFORE
398  or GROUP_REPLICATION_CONSISTENCY_BEFORE_AND_AFTER will:
399  1) send a message to all members;
400  2) when that message is received and processed in-order,
401  w.r.t. the message stream, will fetch the Group Replication
402  applier RECEIVED_TRANSACTION_SET, the set of remote
403  transactions that were allowed to commit;
404  3) wait until all the transactions on Group Replication applier
405  RECEIVED_TRANSACTION_SET are committed.
406 
407  @param[in] thread_id the thread that is executing the
408  transaction
409  @param[in] consistency_level the transaction consistency
410  @param[in] timeout maximum time to wait
411 
412  @return Operation status
413  @retval 0 OK
414  @retval !=0 error
415  */
419  ulong timeout) const;
420 
421  /**
422  Help method called by transaction begin action that, if there are
423  precedent prepared transactions with consistency
424  GROUP_REPLICATION_CONSISTENCY_AFTER or
425  GROUP_REPLICATION_CONSISTENCY_BEFORE_AND_AFTER, will hold the
426  this transaction until the prepared are committed.
427 
428  @param[in] thread_id the thread that is executing the
429  transaction
430  @param[in] timeout maximum time to wait
431 
432  @return Operation status
433  @retval 0 OK
434  @retval !=0 error
435  */
437  ulong timeout);
438 
439  /**
440  Help method that cleans prepared transactions and releases
441  transactions waiting on them.
442 
443  @param[in] key the transaction key
444 
445  @return Operation status
446  @retval 0 OK
447  @retval !=0 error
448  */
450 
453 
455  std::list<Transaction_consistency_manager_key>
457  std::list<my_thread_id> m_new_transactions_waiting;
458  std::list<Pipeline_event *> m_delayed_view_change_events;
459 
460  std::atomic<bool> m_plugin_stopping;
461 };
462 
463 #endif /* CONSISTENCY_MANAGER_INCLUDED */
Transaction_consistency_manager::register_transaction_observer
void register_transaction_observer()
Register an observer for transactions.
Definition: consistency_manager.cc:824
pipeline_interfaces.h
Pipeline_event
Definition: pipeline_interfaces.h:122
thread_id
static my_thread_id thread_id
Definition: my_thr_init.cc:62
Transaction_consistency_info::m_consistency_level
const enum_group_replication_consistency_level m_consistency_level
Definition: consistency_manager.h:184
Transaction_consistency_info::is_a_single_member_group
bool is_a_single_member_group()
Is this transaction running on a single member group?
Definition: consistency_manager.cc:91
Transaction_consistency_info::handle_remote_prepare
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:161
Transaction_consistency_info::after_applier_prepare
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:100
my_dbug.h
Transaction_consistency_manager::m_plugin_stopping
std::atomic< bool > m_plugin_stopping
Definition: consistency_manager.h:460
Transaction_consistency_info::is_the_transaction_prepared_remotely
bool is_the_transaction_prepared_remotely()
Did all other ONLINE members already prepared the transaction?
Definition: consistency_manager.cc:95
Transaction_consistency_manager::before_transaction_begin
virtual int before_transaction_begin(my_thread_id thread_id, ulong gr_consistency_level, ulong timeout, enum_rpl_channel_type rpl_channel_type)
Call action before a transaction starts.
Definition: consistency_manager.cc:536
Transaction_consistency_manager::Transaction_consistency_manager
Transaction_consistency_manager()
Constructor.
Definition: consistency_manager.cc:220
Transaction_consistency_manager::m_new_transactions_waiting
std::list< my_thread_id > m_new_transactions_waiting
Definition: consistency_manager.h:457
Transaction_consistency_manager::transaction_begin_sync_prepared_transactions
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:662
rpl_sidno
int rpl_sidno
Type of SIDNO (source ID number, first component of GTID)
Definition: rpl_gtid.h:94
Transaction_consistency_manager::after_certification
int after_certification(Transaction_consistency_info *transaction_info)
Call action after a transaction is certified.
Definition: consistency_manager.cc:267
Transaction_consistency_manager::has_local_prepared_transactions
bool has_local_prepared_transactions()
Are there local prepared transactions waiting for prepare acknowledge from other members?
Definition: consistency_manager.cc:721
key
static const char * key
Definition: suite_stubs.c:14
Transaction_consistency_info::m_local_transaction
const bool m_local_transaction
Definition: consistency_manager.h:179
Transaction_consistency_info::m_transaction_prepared_remotely
bool m_transaction_prepared_remotely
Definition: consistency_manager.h:187
Transaction_consistency_manager::m_delayed_view_change_events
std::list< Pipeline_event * > m_delayed_view_change_events
Definition: consistency_manager.h:458
Transaction_consistency_info
Definition: consistency_manager.h:49
Transaction_consistency_manager_pair
std::pair< Transaction_consistency_manager_key, Transaction_consistency_info * > Transaction_consistency_manager_pair
Definition: consistency_manager.h:193
Transaction_consistency_manager::clear
void clear()
Clear all information.
Definition: consistency_manager.cc:244
Transaction_consistency_info::m_thread_id
my_thread_id m_thread_id
Definition: consistency_manager.h:178
Transaction_consistency_manager::handle_member_leave
int handle_member_leave(const std::vector< Gcs_member_identifier > &leaving_members)
Call action after members leave the group.
Definition: consistency_manager.cc:491
Transaction_consistency_manager::handle_remote_prepare
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:407
Transaction_consistency_manager::m_prepared_transactions_on_my_applier
std::list< Transaction_consistency_manager_key > m_prepared_transactions_on_my_applier
Definition: consistency_manager.h:456
group_transaction_observation_manager.h
Gcs_member_identifier
Definition: gcs_member_identifier.h:39
Transaction_consistency_manager::transaction_begin_sync_before_execution
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:582
Transaction_consistency_manager::m_prepared_transactions_on_my_applier_lock
Checkable_rwlock * m_prepared_transactions_on_my_applier_lock
Definition: consistency_manager.h:454
Transaction_consistency_info::is_local_transaction
bool is_local_transaction()
Is the transaction from this server?
Definition: consistency_manager.cc:74
Transaction_consistency_manager::unregister_transaction_observer
void unregister_transaction_observer()
Unregister the observer for transactions.
Definition: consistency_manager.cc:828
rpl_gno
long long int rpl_gno
Type of GNO, the second (numeric) component of GTID.
Definition: rpl_gtid.h:100
Transaction_consistency_manager::~Transaction_consistency_manager
virtual ~Transaction_consistency_manager()
Definition: consistency_manager.cc:235
Transaction_consistency_info::m_sidno
const rpl_sidno m_sidno
Definition: consistency_manager.h:182
Transaction_consistency_info::is_transaction_prepared_locally
bool is_transaction_prepared_locally()
Is the transaction prepared locally?
Definition: consistency_manager.cc:78
Transaction_consistency_info::m_transaction_prepared_locally
bool m_transaction_prepared_locally
Definition: consistency_manager.h:186
Transaction_consistency_manager::m_map
Transaction_consistency_manager_map m_map
Definition: consistency_manager.h:452
Transaction_consistency_info::m_gno
const rpl_gno m_gno
Definition: consistency_manager.h:183
Transaction_consistency_info::handle_member_leave
int handle_member_leave(const std::vector< Gcs_member_identifier > &leaving_members)
Call action after members leave the group.
Definition: consistency_manager.cc:195
Transaction_consistency_info::get_consistency_level
enum_group_replication_consistency_level get_consistency_level()
Get the transaction consistency.
Definition: consistency_manager.cc:87
Transaction_consistency_manager::after_commit
virtual int after_commit(my_thread_id thread_id, rpl_sidno sidno, rpl_gno gno)
Call action after commit a transaction on this member.
Definition: consistency_manager.cc:517
Transaction_consistency_manager::plugin_is_stopping
void plugin_is_stopping()
Inform that plugin is stopping.
Definition: consistency_manager.cc:820
Transaction_consistency_info::~Transaction_consistency_info
virtual ~Transaction_consistency_info()
Definition: consistency_manager.cc:66
Transaction_consistency_manager::handle_sync_before_execution_message
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:643
my_thread_id
uint32 my_thread_id
Definition: my_thread_local.h:33
enum_rpl_channel_type
enum_rpl_channel_type
Type of replication channel thread/transaction might be associated to.
Definition: rpl_context.h:37
Transaction_consistency_manager
Definition: consistency_manager.h:206
Checkable_rwlock
This has the functionality of mysql_rwlock_t, with two differences:
Definition: rpl_gtid.h:430
Transaction_consistency_manager::remove_prepared_transaction
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:755
Transaction_consistency_info::get_gno
rpl_gno get_gno()
Get the transaction gno.
Definition: consistency_manager.cc:84
Transaction_consistency_manager_map
std::map< Transaction_consistency_manager_key, Transaction_consistency_info * > Transaction_consistency_manager_map
Definition: consistency_manager.h:196
Transaction_consistency_manager::plugin_started
void plugin_started()
Inform that plugin did start.
Definition: consistency_manager.cc:816
Transaction_consistency_manager_key
std::pair< rpl_sidno, rpl_gno > Transaction_consistency_manager_key
Definition: consistency_manager.h:190
Transaction_consistency_manager::after_rollback
virtual int after_rollback(my_thread_id thread_id)
Executed after rollback.
Definition: consistency_manager.cc:847
plugin_group_replication.h
Transaction_consistency_manager::before_commit
virtual int before_commit(my_thread_id thread_id, Group_transaction_listener::enum_transaction_origin origin)
Executed before commit.
Definition: consistency_manager.cc:837
Transaction_consistency_manager::schedule_view_change_event
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:742
ulong
unsigned long ulong
Definition: my_inttypes.h:48
Transaction_consistency_info::m_sid
rpl_sid m_sid
Definition: consistency_manager.h:181
Transaction_consistency_manager::m_map_lock
Checkable_rwlock * m_map_lock
Definition: consistency_manager.h:451
gcs_member_identifier.h
Transaction_consistency_manager::after_applier_prepare
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:317
member_info.h
binary_log::Uuid
Definition: uuid.h:60
enum_group_replication_consistency_level
enum_group_replication_consistency_level
Definition: plugin_group_replication.h:34
Transaction_consistency_info::Transaction_consistency_info
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, std::list< Gcs_member_identifier > *members_that_must_prepare_the_transaction)
Constructor.
Definition: consistency_manager.cc:32
Group_transaction_listener
Listener for transaction life cycle events.
Definition: group_transaction_observation_manager.h:32
Group_transaction_listener::enum_transaction_origin
enum_transaction_origin
Enum for transaction origins.
Definition: group_transaction_observation_manager.h:35
Transaction_consistency_info::m_sid_specified
const bool m_sid_specified
Definition: consistency_manager.h:180
Group_member_info::Group_member_status
Group_member_status
Definition: member_info.h:151
Transaction_consistency_info::get_thread_id
my_thread_id get_thread_id()
Get the thread id that is executing the transaction.
Definition: consistency_manager.cc:70
Transaction_consistency_manager::before_rollback
virtual int before_rollback(my_thread_id thread_id, Group_transaction_listener::enum_transaction_origin origin)
Executed before rollback.
Definition: consistency_manager.cc:842
Transaction_consistency_info::m_members_that_must_prepare_the_transaction
std::list< Gcs_member_identifier > * m_members_that_must_prepare_the_transaction
Definition: consistency_manager.h:185
group_replication_priv.h
Transaction_consistency_info::get_sidno
rpl_sidno get_sidno()
Get the transaction sidno.
Definition: consistency_manager.cc:82