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