MySQL  8.0.27
Source Code Documentation
rpl_transaction_write_set_ctx.h
Go to the documentation of this file.
1 /* Copyright (c) 2014, 2021, 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_TRANSACTION_WRITE_SET_CTX_H
24 #define RPL_TRANSACTION_WRITE_SET_CTX_H
25 
26 #include <stddef.h>
27 #include <atomic>
28 #include <list>
29 #include <map>
30 #include <string>
31 #include <vector>
32 
33 #include "my_inttypes.h"
34 
35 /**
36  Thread class responsible for the collection of write sets associated
37  to a transaction.
38 
39  It also includes suport for save points where information will be discarded
40  on rollbacks to a savepoint.
41 
42  Write set and flags are reset on
43  Rpl_transaction_write_set_ctx::reset_state().
44 
45  The write set collection by an executing transaction is capped to a limit.
46  The limit can be "soft" or "hard":
47  - when a writeset grows above a "soft" limit, the transaction is allowed to
48  execute and commit, but the write set is discarded, and the transaction
49  declared to not have a usable write set.
50  - when a write set grows above a "hard" limit, the transaction is forced
51  to abort and rollback.
52 
53  We cannot use a soft limit for transactions that will be certified in GR,
54  since a writeset is required for correct certification. But when GR is
55  disabled, we can use a soft limit, because the writeset is only used to
56  compute transaction dependencies, and we can pessimistically mark the
57  transaction as conflicting with all other transcations when the writeset
58  is discarded.
59  A soft limit can also be used for transactions executed by the GR recovery
60  channel, since they will not be certified, and the GR applier channel,
61  since those transactions have already passed the certification stage.
62 
63  For the soft limit, we use
64  - binlog_transaction_dependency_history_size.
65  Transactions bigger than that cannot be added to the writeset history
66  since they do not fit, and therefore are marked as conflicting with all
67  *subsequent* transactions anyways.
68  Therefore much of the parallelization for the transaction is already
69  destroyed, and it is unlikely that also marking it as conflicting with
70  *previous* transactions makes a significant difference.
71 
72  For the hard limit, when using Group Replication, we use
73  - group_replication_transaction_size_limit.
74  Since the writeset is a subset of the transaction, and the transaction
75  is limited to this size anyways, a transaction whose writeset exceeds
76  this limit will fail anyways.
77  So failing it when generating the writeset is merely a fail-fast mechanism,
78  and it is not a restriction to apply this limit to the writeset of all
79  transactions for which the full transaction data is also subject to the limit.
80  The transactions that are subject to this limit are exactly those executed
81  when GR is enabled, except by the GR applier channel and GR recovery channel.
82 
83  We expose the following interfaces so that components such as GR
84  can control the limit.
85  There is an interface to *globally*
86  disable/enable the soft limit:
87  static void set_global_require_full_write_set(bool requires_ws);
88  set/alter/remove a hard limit:
89  static void set_global_write_set_memory_size_limit(uint64 limit)
90  static void update_global_write_set_memory_size_limit(uint64 limit);
91 
92  There is another interface to override the global limits for a thread:
93  void set_local_ignore_write_set_memory_limit(bool ignore_limit);
94  void set_local_allow_drop_write_set(bool allow_drop_write_set);
95 
96  The local methods are used for example for Group Replication applier and
97  recovery threads as group_replication_transaction_size_limit only applies
98  to client sessions and non group replication replica threads.
99 */
101  public:
103  virtual ~Rpl_transaction_write_set_ctx() = default;
104 
105  /**
106  Function to add the write set of the hash of the PKE in the std::vector
107  in the transaction_ctx object.
108 
109  @param[in] hash - the uint64 type hash value of the PKE.
110 
111  @return true if it can't add the write set entry, false if successful
112  */
113  bool add_write_set(uint64 hash);
114 
115  /*
116  Function to get the pointer of the write set vector in the
117  transaction_ctx object.
118  */
119  std::vector<uint64> *get_write_set();
120 
121  /**
122  Reset the object so it can be used for a new transaction.
123  */
124  void reset_state();
125 
126  /*
127  mark transactions that include tables with no pk
128  */
129  void set_has_missing_keys();
130 
131  /*
132  check if the transaction was marked as having missing keys.
133 
134  @retval true The transaction accesses tables with no PK.
135  @retval false All tables referenced in transaction have PK.
136  */
137  bool get_has_missing_keys();
138 
139  /*
140  mark transactions that include tables referenced by foreign keys
141  */
143 
144  /*
145  function to check if the transaction was marked as having missing keys.
146 
147  @retval true If the transaction was marked as being referenced by a foreign
148  key
149  */
151 
152  /**
153  Identifies situations where the limit for number of write set entries
154  already exceeded the configure limit.
155 
156  @retval true if too many write set entries exist, false otherwise
157  */
159 
160  /**
161  @returns the size of the write_set field in bytes
162  */
163  size_t write_set_memory_size();
164 
165  /**
166  Function to add a new SAVEPOINT identifier in the savepoint map in the
167  transaction_ctx object.
168 
169  @param[in] name - the identifier name of the SAVEPOINT.
170  */
171  void add_savepoint(char *name);
172 
173  /**
174  Function to delete a SAVEPOINT identifier in the savepoint map in the
175  transaction_ctx object.
176 
177  @param[in] name - the identifier name of the SAVEPOINT.
178  */
179  void del_savepoint(char *name);
180 
181  /**
182  Function to delete all data added to write set and savepoint since
183  SAVEPOINT identifier was added to savepoinbt in the transaction_ctx object.
184 
185  @param[in] name - the identifier name of the SAVEPOINT.
186  */
187  void rollback_to_savepoint(char *name);
188 
189  /**
190  Function to push savepoint data to a list and clear the savepoint map in
191  order to create another identifier context, needed on functions ant trigger.
192  */
193  void reset_savepoint_list();
194 
195  /**
196  Restore previous savepoint map context, called after executed trigger or
197  function.
198  */
199  void restore_savepoint_list();
200 
201  /**
202  Adds a memory limit for write sets.
203 
204  @note currently only one component can set this limit a time.
205 
206  @param limit the limit to be added
207  */
209 
210  /**
211  Updates the memory limit for write sets.
212 
213  @note Using the value 0 disables the limit
214 
215  @param limit the limit to be added
216  */
218 
219  /**
220  Prevent or allow this class to discard writesets exceeding a size limit
221  If true, a transaction will never discard its write sets
222 
223  @param requires_ws if who invoked the method needs or not write sets
224  */
225  static void set_global_require_full_write_set(bool requires_ws);
226 
227  /**
228  Set if the thread shall ignore any configured memory limit
229  for write set collection
230 
231  @param ignore_limit if the limit should be ignored
232  */
233  void set_local_ignore_write_set_memory_limit(bool ignore_limit);
234 
235  /**
236  Set if the thread shall if needed discard write sets
237 
238  @param allow_drop_write_set if full write sets are not critical
239  */
240  void set_local_allow_drop_write_set(bool allow_drop_write_set);
241 
242  private:
243  /*
244  Clear the vector that stores the PKEs, and clear the savepoints, but do not
245  restore all the flags. Outside transaction cleanup, this is used when
246  discarding a writeset of excessive size, without aborting the transaction.
247  */
248  void clear_write_set();
249 
250  std::vector<uint64> write_set;
253 
254  /**
255  Contains information related to SAVEPOINTs. The key on map is the
256  identifier and the value is the size of write set when command was
257  executed.
258  */
259  std::map<std::string, size_t> savepoint;
260 
261  /**
262  Create a savepoint context hierarchy to support encapsulation of
263  identifier name when function or trigger are executed.
264  */
265  std::list<std::map<std::string, size_t>> savepoint_list;
266 
267  // Write set restriction variables
268 
269  /** There is a component requiring write sets on transactions */
270  static std::atomic<bool> m_global_component_requires_write_sets;
271  /** Memory size limit enforced for write set collection */
272  static std::atomic<uint64> m_global_write_set_memory_size_limit;
273 
274  /**
275  If the thread should or not ignore the set limit for
276  write set collection
277  */
279  /**
280  Even if a component says all transactions require write sets,
281  this variable says this thread should discard them when they are
282  bigger than m_opt_max_history_size
283  */
285 
286  /** True if the write set size is over the configure limit */
288 };
289 
290 #endif /* RPL_TRANSACTION_WRITE_SET_CTX_H */
Thread class responsible for the collection of write sets associated to a transaction.
Definition: rpl_transaction_write_set_ctx.h:100
void reset_savepoint_list()
Function to push savepoint data to a list and clear the savepoint map in order to create another iden...
Definition: rpl_transaction_write_set_ctx.cc:307
std::vector< uint64 > write_set
Definition: rpl_transaction_write_set_ctx.h:250
bool get_has_related_foreign_keys()
Definition: rpl_transaction_write_set_ctx.cc:131
static std::atomic< bool > m_global_component_requires_write_sets
There is a component requiring write sets on transactions.
Definition: rpl_transaction_write_set_ctx.h:270
size_t write_set_memory_size()
Definition: rpl_transaction_write_set_ctx.cc:141
std::list< std::map< std::string, size_t > > savepoint_list
Create a savepoint context hierarchy to support encapsulation of identifier name when function or tri...
Definition: rpl_transaction_write_set_ctx.h:265
std::vector< uint64 > * get_write_set()
Definition: rpl_transaction_write_set_ctx.cc:97
bool add_write_set(uint64 hash)
Function to add the write set of the hash of the PKE in the std::vector in the transaction_ctx object...
Definition: rpl_transaction_write_set_ctx.cc:63
bool m_local_has_reached_write_set_limit
True if the write set size is over the configure limit.
Definition: rpl_transaction_write_set_ctx.h:287
bool m_local_allow_drop_write_set
Even if a component says all transactions require write sets, this variable says this thread should d...
Definition: rpl_transaction_write_set_ctx.h:284
static void set_global_write_set_memory_size_limit(uint64 limit)
Adds a memory limit for write sets.
Definition: rpl_transaction_write_set_ctx.cc:156
bool m_has_missing_keys
Definition: rpl_transaction_write_set_ctx.h:251
static void set_global_require_full_write_set(bool requires_ws)
Prevent or allow this class to discard writesets exceeding a size limit If true, a transaction will n...
Definition: rpl_transaction_write_set_ctx.cc:146
void rollback_to_savepoint(char *name)
Function to delete all data added to write set and savepoint since SAVEPOINT identifier was added to ...
Definition: rpl_transaction_write_set_ctx.cc:263
void set_has_related_foreign_keys()
Definition: rpl_transaction_write_set_ctx.cc:126
void set_local_ignore_write_set_memory_limit(bool ignore_limit)
Set if the thread shall ignore any configured memory limit for write set collection.
Definition: rpl_transaction_write_set_ctx.cc:177
void add_savepoint(char *name)
Function to add a new SAVEPOINT identifier in the savepoint map in the transaction_ctx object.
Definition: rpl_transaction_write_set_ctx.cc:222
void reset_state()
Reset the object so it can be used for a new transaction.
Definition: rpl_transaction_write_set_ctx.cc:102
std::map< std::string, size_t > savepoint
Contains information related to SAVEPOINTs.
Definition: rpl_transaction_write_set_ctx.h:259
void del_savepoint(char *name)
Function to delete a SAVEPOINT identifier in the savepoint map in the transaction_ctx object.
Definition: rpl_transaction_write_set_ctx.cc:251
void restore_savepoint_list()
Restore previous savepoint map context, called after executed trigger or function.
Definition: rpl_transaction_write_set_ctx.cc:314
virtual ~Rpl_transaction_write_set_ctx()=default
void set_has_missing_keys()
Definition: rpl_transaction_write_set_ctx.cc:116
static void update_global_write_set_memory_size_limit(uint64 limit)
Updates the memory limit for write sets.
Definition: rpl_transaction_write_set_ctx.cc:162
bool was_write_set_limit_reached()
Identifies situations where the limit for number of write set entries already exceeded the configure ...
Definition: rpl_transaction_write_set_ctx.cc:136
bool m_has_related_foreign_keys
Definition: rpl_transaction_write_set_ctx.h:252
void clear_write_set()
Definition: rpl_transaction_write_set_ctx.cc:109
static std::atomic< uint64 > m_global_write_set_memory_size_limit
Memory size limit enforced for write set collection.
Definition: rpl_transaction_write_set_ctx.h:272
void set_local_allow_drop_write_set(bool allow_drop_write_set)
Set if the thread shall if needed discard write sets.
Definition: rpl_transaction_write_set_ctx.cc:182
bool get_has_missing_keys()
Definition: rpl_transaction_write_set_ctx.cc:121
Rpl_transaction_write_set_ctx()
Definition: rpl_transaction_write_set_ctx.cc:47
bool m_ignore_write_set_memory_limit
If the thread should or not ignore the set limit for write set collection.
Definition: rpl_transaction_write_set_ctx.h:278
Some integer typedefs for easier portability.
uint64_t uint64
Definition: my_inttypes.h:68
case opt name
Definition: sslopt-case.h:32