MySQL 8.0.37
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 = 3;
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
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, 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, 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 of the gtid interval.
286 @param gno_start The first GNO of the gtid interval.
287 @param gno_end The last GNO of the gtid interval.
288
289 @retval 0 OK.
290 @retval -1 Error.
291 */
292 int fill_fields(Field **fields, const char *sid, rpl_gno gno_start,
293 rpl_gno gno_end);
294 /**
295 Write a gtid interval into the gtid_executed table.
296
297 @param table Reference to a table object.
298 @param sid The source id of the gtid interval.
299 @param gno_start The first GNO of the gtid interval.
300 @param gno_end The last GNO of the gtid interval.
301
302 @retval 0 OK.
303 @retval -1 Error.
304 */
305 int write_row(TABLE *table, const char *sid, rpl_gno gno_start,
306 rpl_gno gno_end);
307 /**
308 Update a gtid interval in the gtid_executed table.
309 - locate the gtid interval by primary key (sid, gno_start)
310 to update it with the new_gno_end.
311
312 @param table Reference to a table object.
313 @param sid The source id of the gtid interval.
314 @param gno_start The first GNO of the gtid interval.
315 @param new_gno_end The new last GNO of the gtid interval.
316
317 @retval 0 OK.
318 @retval -1 Error.
319 */
320 int update_row(TABLE *table, const char *sid, rpl_gno gno_start,
321 rpl_gno new_gno_end);
322 /**
323 Delete all rows in the gtid_executed table.
324
325 @param table Reference to a table object.
326
327 @retval 0 OK.
328 @retval -1 Error.
329 */
330 int delete_all(TABLE *table);
331 /**
332 Encode the current row fetched from the table into gtid text.
333
334 @param table Reference to a table object.
335 @retval Return the encoded gtid text.
336 */
337 std::string encode_gtid_text(TABLE *table);
338 /**
339 Get gtid interval from the the current row of the table.
340
341 @param table Reference to a table object.
342 @param [out] sid The source id of the gtid interval.
343 @param [out] gno_start The first GNO of the gtid interval.
344 @param [out] gno_end The last GNO of the gtid interval.
345 */
346 void get_gtid_interval(TABLE *table, std::string &sid, rpl_gno &gno_start,
347 rpl_gno &gno_end);
348 /**
349 Insert the gtid set into table.
350
351 @param table The gtid_executed table.
352 @param gtid_set Contains a set of gtid, which holds
353 the sidno and the gno.
354
355 @retval
356 0 OK
357 @retval
358 -1 Error
359 */
360 int save(TABLE *table, const Gtid_set *gtid_set);
361 /* Prevent user from invoking default assignment function. */
363 /* Prevent user from invoking default constructor function. */
365};
366
370
371#endif /* RPL_GTID_PERSIST_H_ */
Definition: field.h:575
Represents a set of GTIDs.
Definition: rpl_gtid.h:1455
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:239
int save(THD *thd, const Gtid *gtid)
Insert the gtid into table.
Definition: rpl_gtid_persist.cc:340
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
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:481
std::string encode_gtid_text(TABLE *table)
Encode the current row fetched from the table into gtid text.
Definition: rpl_gtid_persist.cc:626
int fill_fields(Field **fields, const char *sid, 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 reset(THD *thd)
Delete all rows from the table.
Definition: rpl_gtid_persist.cc:602
int delete_all(TABLE *table)
Delete all rows in the gtid_executed table.
Definition: rpl_gtid_persist.cc:704
int update_row(TABLE *table, const char *sid, rpl_gno gno_start, rpl_gno new_gno_end)
Update a gtid interval in the gtid_executed table.
Definition: rpl_gtid_persist.cc:271
Gtid_table_persistor(const Gtid_table_persistor &info)
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, gno_start) in increasing order,...
Definition: rpl_gtid_persist.cc:500
virtual ~Gtid_table_persistor()=default
int compress_first_consecutive_range(TABLE *table, bool &is_complete)
Read each row by the PK(sid, gno_start) in increasing order, compress the first consecutive range of ...
Definition: rpl_gtid_persist.cc:532
void get_gtid_interval(TABLE *table, std::string &sid, rpl_gno &gno_start, rpl_gno &gno_end)
Get gtid interval from the the current row of the table.
Definition: rpl_gtid_persist.cc:644
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:658
int write_row(TABLE *table, const char *sid, rpl_gno gno_start, rpl_gno gno_end)
Write a gtid interval into the gtid_executed table.
Definition: rpl_gtid_persist.cc:239
Storage for backup of Open_tables_state.
Definition: sql_class.h:688
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:34
void raise_error_printf(uint code,...)
Raise an exception condition, with a formatted message.
Definition: sql_class.cc:924
bool is_operating_gtid_table_implicitly
Definition: sql_class.h:2271
void raise_warning_printf(uint code,...)
Raise a completion condition (warning), with a formatted message.
Definition: sql_class.cc:941
Transaction_ctx * get_transaction()
Definition: sql_class.h:2045
Definition: table.h:2790
const Lock_descriptor & lock_descriptor() const
Definition: table.h:3502
const char * table_name
Definition: table.h:3485
XID_STATE * xid_state()
Definition: transaction_info.h:281
@ XA_ACTIVE
Definition: xa.h:299
bool has_state(xa_states state) const
Definition: xa.h:347
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
#define DBUG_TRACE
Definition: my_dbug.h:146
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
Log info(cout, "NOTE")
binary_log::gtids::gno_t rpl_gno
GNO, the second (numeric) component of a GTID, is an alias of binary_log::gtids::gno_t.
Definition: rpl_gtid.h:103
void create_compress_gtid_table_thread()
Create the compression thread to compress gtid_executed table.
Definition: rpl_gtid_persist.cc:802
void terminate_compress_gtid_table_thread()
Terminate the compression thread.
Definition: rpl_gtid_persist.cc:842
Gtid_table_persistor * gtid_table_persistor
Definition: mysqld.cc:1835
TODO: Move this structure to libbinlogevents/include/control_events.h when we start using C++11.
Definition: rpl_gtid.h:1066
thr_lock_type type
Definition: thr_lock.h:100
Definition: mysql_lex_string.h:40
const char * str
Definition: mysql_lex_string.h:41
Definition: table.h:1398
@ TL_WRITE_ALLOW_WRITE
Definition: thr_lock.h:73
unsigned int uint
Definition: uca9-dump.cc:75