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