MySQL 8.3.0
Source Code Documentation
rpl_gtid_persist.h
Go to the documentation of this file.
1/* Copyright (c) 2013, 2023, 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_GTID_PERSIST_H_
24#define RPL_GTID_PERSIST_H_
25
26#include <string.h>
27#include <sys/types.h>
28#include <atomic>
29#include <string>
30
31#include "lex_string.h"
32#include "my_dbug.h"
33#include "my_inttypes.h"
34#include "mysqld_error.h"
35#include "sql/rpl_gtid.h"
36#include "sql/rpl_table_access.h" // System_table_access
37#include "sql/sql_class.h" // Open_tables_backup
38#include "sql/table.h"
40#include "sql/xa.h"
41#include "thr_lock.h"
42
43class Field;
44
46 public:
47 static const LEX_CSTRING DB_NAME;
48 static const LEX_CSTRING TABLE_NAME;
49
51 ~Gtid_table_access_context() override = default;
52
53 /**
54 Initialize the gtid_executed table access context as following:
55 - Create a new THD if current_thd is NULL
56 - Disable binlog temporarily if we are going to modify the table
57 - Open and lock a table.
58
59 @param[in,out] thd Thread requesting to open the table
60 @param[out] table We will store the open table here
61 @param[in] is_write If true, the access will be for modifying the table
62
63 @retval true failed
64 @retval false success
65 */
66 bool init(THD **thd, TABLE **table, bool is_write);
67 /**
68 De-initialize the gtid_executed table access context as following:
69 - Close the table
70 - Re-enable binlog if needed
71 - Destroy the created THD if needed.
72
73 @param thd Thread requesting to close the table
74 @param table Table to be closed
75 @param error If there was an error while updating the table
76 @param need_commit Need to commit current transaction if it is true
77
78 @retval true failed
79 @retval false success
80 */
81 bool deinit(THD *thd, TABLE *table, bool error, bool need_commit);
82 /**
83 Prepares before opening table.
84 - set flags
85
86 @param[in] thd Thread requesting to open the table
87 */
88 void before_open(THD *thd) override;
89 /**
90 Creates a new thread in the bootstrap process or in the mysqld startup,
91 a thread is created in order to be able to access a table. And reset a
92 new "statement".
93
94 @returns THD* Pointer to thread structure
95 */
96 THD *create_thd();
97 void drop_thd(THD *thd);
98
99 private:
100 /* Pointer to new created THD. */
102 /* Modify the table if it is true. */
104 /* Save the lock info. */
106 /* Save binlog options. */
108 /* Whether or not `THD::set_skip_readonly_check` was invoked during `THD`
109 initialization */
111
112 /* Prevent user from invoking default assignment function. */
114 /* Prevent user from invoking default constructor function. */
116};
117
119 public:
120 static const uint number_fields =
121 4; ///< the number of fields in mysql.gtid_executed
122
124 virtual ~Gtid_table_persistor() = default;
125
126 /**
127 Insert the gtid into table.
128
129 @param thd Thread requesting to save gtid into the table
130 @param gtid holds the sidno and the gno.
131
132 @retval
133 0 OK
134 @retval
135 1 The table was not found.
136 @retval
137 -1 Error
138 */
139 int save(THD *thd, const Gtid *gtid);
140 /**
141 Insert the gtid set into table.
142
143 @param gtid_set contains a set of gtid, which holds
144 the sidno and the gno.
145
146 @param compress notify to compress gtid_executed table
147
148 @retval
149 0 OK
150 @retval
151 -1 Error
152 */
153 int save(const Gtid_set *gtid_set, bool compress = true);
154 /**
155 Delete all rows from the table.
156
157 @param thd Thread requesting to reset the table
158
159 @retval
160 0 OK
161 @retval
162 1 The table was not found.
163 @retval
164 -1 Error
165 */
166 int reset(THD *thd);
167
168 /**
169 Fetch gtids from gtid_executed table and store them into
170 gtid_executed set.
171
172 @param[out] gtid_set store gtids fetched from the gtid_executed table.
173
174 @retval
175 0 OK
176 @retval
177 1 The table was not found.
178 @retval
179 -1 Error
180 */
181 int fetch_gtids(Gtid_set *gtid_set);
182 /**
183 Compress the gtid_executed table completely by employing one
184 or more transactions.
185
186 @param thd Thread requesting to compress the table
187
188 @retval
189 0 OK
190 @retval
191 1 The table was not found.
192 @retval
193 -1 Error
194 */
195 int compress(THD *thd);
196 /**
197 Push a warning to client if user is modifying the gtid_executed
198 table explicitly by a non-XA transaction. Push an error to client
199 if user is modifying it explicitly by a XA transaction.
200
201 @param thd Thread requesting to access the table
202 @param table The table is being accessed.
203
204 @retval 0 No warning or error was pushed to the client.
205 @retval 1 Push a warning to client.
206 @retval 2 Push an error to client.
207 */
210
212 table->lock_descriptor().type >= TL_WRITE_ALLOW_WRITE &&
213 !strcmp(table->table_name, Gtid_table_access_context::TABLE_NAME.str)) {
214 if (thd->get_transaction()->xid_state()->has_state(
216 /*
217 Push an error to client if user is modifying the gtid_executed
218 table explicitly by a XA transaction.
219 */
220 thd->raise_error_printf(ER_ERROR_ON_MODIFYING_GTID_EXECUTED_TABLE,
221 table->table_name);
222 return 2;
223 } else {
224 /*
225 Push a warning to client if user is modifying the gtid_executed
226 table explicitly by a non-XA transaction.
227 */
228 thd->raise_warning_printf(ER_WARN_ON_MODIFYING_GTID_EXECUTED_TABLE,
229 table->table_name);
230 return 1;
231 }
232 }
233
234 return 0;
235 }
236
237 private:
238 /* Count the append size of the table */
239 std::atomic<int64> m_atomic_count{0};
240 /**
241 Compress the gtid_executed table, read each row by the
242 PK(sid, tag, gno_start) in increasing order, compress the first
243 consecutive range of gtids within a single transaction.
244
245 @param thd Thread requesting to compress the table
246 @param[out] is_complete True if the gtid_executed table is
247 compressd completely.
248
249 @retval
250 0 OK
251 @retval
252 1 The table was not found.
253 @retval
254 -1 Error
255 */
256 int compress_in_single_transaction(THD *thd, bool &is_complete);
257 /**
258 Read each row by the PK(sid, tag, gno_start) in increasing order,
259 compress the first consecutive range of gtids.
260 For example,
261 1 1
262 2 2
263 3 3
264 6 6
265 7 7
266 8 8
267 After the compression, the gtids in the table is compressed as following:
268 1 3
269 6 6
270 7 7
271 8 8
272
273 @param table Reference to a table object.
274 @param[out] is_complete True if the gtid_executed table
275 is compressd completely.
276
277 @retval 0 OK.
278 @retval -1 Error.
279 */
280 int compress_first_consecutive_range(TABLE *table, bool &is_complete);
281 /**
282 Fill a gtid interval into fields of the gtid_executed table.
283
284 @param fields Reference to table fields.
285 @param sid The source id component of TSID (UUID)
286 @param tag Tag component of a TSID
287 @param gno_start The first GNO of the gtid interval.
288 @param gno_end The last GNO of the gtid interval.
289
290 @retval 0 OK.
291 @retval -1 Error.
292 */
293 [[NODISCARD]] int fill_fields(Field **fields, const char *sid,
294 const char *tag, rpl_gno gno_start,
295 rpl_gno gno_end);
296 /**
297 Write a gtid interval into the gtid_executed table.
298
299 @param table Reference to a table object.
300 @param sid The source id component of TSID (UUID)
301 @param tag Tag component of a TSID
302 @param gno_start The first GNO of the gtid interval.
303 @param gno_end The last GNO of the gtid interval.
304
305 @retval 0 OK.
306 @retval -1 Error.
307 */
308 [[NODISCARD]] int write_row(TABLE *table, const char *sid, const char *tag,
309 rpl_gno gno_start, rpl_gno gno_end);
310 /**
311 Update a gtid interval in the gtid_executed table.
312 - locate the gtid interval by primary key (sid, tag, gno_start)
313 to update it with the new_gno_end.
314
315 @param table Reference to a table object.
316 @param sid The source id component of TSID (UUID)
317 @param tag Tag component of a TSID
318 @param gno_start The first GNO of the gtid interval.
319 @param new_gno_end The new last GNO of the gtid interval.
320
321 @retval 0 OK.
322 @retval -1 Error.
323 */
324 [[NODISCARD]] int update_row(TABLE *table, const char *sid, const char *tag,
325 rpl_gno gno_start, rpl_gno new_gno_end);
326 /**
327 Delete all rows in the gtid_executed table.
328
329 @param table Reference to a table object.
330
331 @retval 0 OK.
332 @retval -1 Error.
333 */
334 int delete_all(TABLE *table);
335 /**
336 Encode the current row fetched from the table into gtid text.
337
338 @param table Reference to a table object.
339 @retval Return the encoded gtid text.
340 */
341 std::string encode_gtid_text(TABLE *table);
342 /**
343 Get gtid interval from the the current row of the table.
344
345 @param table Reference to a table object.
346 @param [out] sid The source id component of TSID (UUID)
347 @param [out] tag tag component of a TSID
348 @param [out] gno_start The first GNO of the gtid interval.
349 @param [out] gno_end The last GNO of the gtid interval.
350 */
351 void get_gtid_interval(TABLE *table, std::string &sid, std::string &tag,
352 rpl_gno &gno_start, rpl_gno &gno_end);
353 /**
354 Insert the gtid set into table.
355
356 @param table The gtid_executed table.
357 @param gtid_set Contains a set of gtid, which holds
358 the sidno and the gno.
359
360 @retval
361 0 OK
362 @retval
363 -1 Error
364 */
365 int save(TABLE *table, const Gtid_set *gtid_set);
366 /* Prevent user from invoking default assignment function. */
368 /* Prevent user from invoking default constructor function. */
370};
371
375
376#endif /* RPL_GTID_PERSIST_H_ */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:250
Definition: field.h:574
Represents a set of GTIDs.
Definition: rpl_gtid.h:1555
Definition: rpl_gtid_persist.h:45
void before_open(THD *thd) override
Prepares before opening table.
Definition: rpl_gtid_persist.cc:124
bool deinit(THD *thd, TABLE *table, bool error, bool need_commit)
De-initialize the gtid_executed table access context as following:
Definition: rpl_gtid_persist.cc:172
Gtid_table_access_context & operator=(const Gtid_table_access_context &info)
bool m_is_write
Definition: rpl_gtid_persist.h:103
static const LEX_CSTRING DB_NAME
Definition: rpl_gtid_persist.h:47
THD * m_drop_thd_object
Definition: rpl_gtid_persist.h:101
static const LEX_CSTRING TABLE_NAME
Definition: rpl_gtid_persist.h:48
Gtid_table_access_context()
Definition: rpl_gtid_persist.h:50
ulonglong m_tmp_disable_binlog__save_options
Definition: rpl_gtid_persist.h:107
~Gtid_table_access_context() override=default
bool init(THD **thd, TABLE **table, bool is_write)
Initialize the gtid_executed table access context as following:
Definition: rpl_gtid_persist.cc:135
void drop_thd(THD *thd)
Definition: rpl_gtid_persist.cc:119
THD * create_thd()
Creates a new thread in the bootstrap process or in the mysqld startup, a thread is created in order ...
Definition: rpl_gtid_persist.cc:106
Gtid_table_access_context(const Gtid_table_access_context &info)
Open_tables_backup m_backup
Definition: rpl_gtid_persist.h:105
bool m_skip_readonly_set
Definition: rpl_gtid_persist.h:110
Definition: rpl_gtid_persist.h:118
std::atomic< int64 > m_atomic_count
Definition: rpl_gtid_persist.h:239
int save(THD *thd, const Gtid *gtid)
Insert the gtid into table.
Definition: rpl_gtid_persist.cc:359
void get_gtid_interval(TABLE *table, std::string &sid, std::string &tag, rpl_gno &gno_start, rpl_gno &gno_end)
Get gtid interval from the the current row of the table.
Definition: rpl_gtid_persist.cc:673
int warn_or_err_on_explicit_modification(THD *thd, Table_ref *table)
Push a warning to client if user is modifying the gtid_executed table explicitly by a non-XA transact...
Definition: rpl_gtid_persist.h:208
Gtid_table_persistor & operator=(const Gtid_table_persistor &info)
static const uint number_fields
the number of fields in mysql.gtid_executed
Definition: rpl_gtid_persist.h:120
int compress(THD *thd)
Compress the gtid_executed table completely by employing one or more transactions.
Definition: rpl_gtid_persist.cc:500
std::string encode_gtid_text(TABLE *table)
Encode the current row fetched from the table into gtid text.
Definition: rpl_gtid_persist.cc:651
int reset(THD *thd)
Delete all rows from the table.
Definition: rpl_gtid_persist.cc:627
int delete_all(TABLE *table)
Delete all rows in the gtid_executed table.
Definition: rpl_gtid_persist.cc:738
Gtid_table_persistor(const Gtid_table_persistor &info)
int write_row(TABLE *table, const char *sid, const char *tag, rpl_gno gno_start, rpl_gno gno_end)
Write a gtid interval into the gtid_executed table.
Definition: rpl_gtid_persist.cc:247
Gtid_table_persistor()=default
int compress_in_single_transaction(THD *thd, bool &is_complete)
Compress the gtid_executed table, read each row by the PK(sid, tag, gno_start) in increasing order,...
Definition: rpl_gtid_persist.cc:519
virtual ~Gtid_table_persistor()=default
int compress_first_consecutive_range(TABLE *table, bool &is_complete)
Read each row by the PK(sid, tag, gno_start) in increasing order, compress the first consecutive rang...
Definition: rpl_gtid_persist.cc:551
int fill_fields(Field **fields, const char *sid, const char *tag, rpl_gno gno_start, rpl_gno gno_end)
Fill a gtid interval into fields of the gtid_executed table.
Definition: rpl_gtid_persist.cc:208
int fetch_gtids(Gtid_set *gtid_set)
Fetch gtids from gtid_executed table and store them into gtid_executed set.
Definition: rpl_gtid_persist.cc:692
int update_row(TABLE *table, const char *sid, const char *tag, rpl_gno gno_start, rpl_gno new_gno_end)
Update a gtid interval in the gtid_executed table.
Definition: rpl_gtid_persist.cc:280
Storage for backup of Open_tables_state.
Definition: sql_class.h:692
A base class for accessing a system table.
Definition: rpl_table_access.h:39
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:35
void raise_error_printf(uint code,...)
Raise an exception condition, with a formatted message.
Definition: sql_class.cc:937
bool is_operating_gtid_table_implicitly
Definition: sql_class.h:2289
void raise_warning_printf(uint code,...)
Raise a completion condition (warning), with a formatted message.
Definition: sql_class.cc:954
Transaction_ctx * get_transaction()
Definition: sql_class.h:2063
Definition: table.h:2853
XID_STATE * xid_state()
Definition: transaction_info.h:281
@ XA_ACTIVE
Definition: xa.h:298
bool has_state(xa_states state) const
Definition: xa.h:346
#define DBUG_TRACE
Definition: my_dbug.h:145
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
static PFS_engine_table_share_proxy table
Definition: pfs.cc:60
#define NODISCARD
The function attribute [[NODISCARD]] is a replacement for [[nodiscard]] to workaround a gcc bug.
Definition: nodiscard.h:46
mysql::gtid::gno_t rpl_gno
GNO, the second (numeric) component of a GTID, is an alias of mysql::gtid::gno_t.
Definition: rpl_gtid.h:111
void create_compress_gtid_table_thread()
Create the compression thread to compress gtid_executed table.
Definition: rpl_gtid_persist.cc:836
void terminate_compress_gtid_table_thread()
Terminate the compression thread.
Definition: rpl_gtid_persist.cc:876
Gtid_table_persistor * gtid_table_persistor
Definition: mysqld.cc:1841
TODO: Move this structure to mysql/binlog/event/control_events.h when we start using C++11.
Definition: rpl_gtid.h:1099
Definition: mysql_lex_string.h:39
const char * str
Definition: mysql_lex_string.h:40
Definition: table.h:1403
@ TL_WRITE_ALLOW_WRITE
Definition: thr_lock.h:72