MySQL 8.0.30
Source Code Documentation
rpl_gtid_persist.h
Go to the documentation of this file.
1/* Copyright (c) 2013, 2022, 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 = 3;
121
123 virtual ~Gtid_table_persistor() = default;
124
125 /**
126 Insert the gtid into table.
127
128 @param thd Thread requesting to save gtid into the table
129 @param gtid holds the sidno and the gno.
130
131 @retval
132 0 OK
133 @retval
134 1 The table was not found.
135 @retval
136 -1 Error
137 */
138 int save(THD *thd, const Gtid *gtid);
139 /**
140 Insert the gtid set into table.
141
142 @param gtid_set contains a set of gtid, which holds
143 the sidno and the gno.
144
145 @param compress notify to compress gtid_executed table
146
147 @retval
148 0 OK
149 @retval
150 -1 Error
151 */
152 int save(const Gtid_set *gtid_set, bool compress = true);
153 /**
154 Delete all rows from the table.
155
156 @param thd Thread requesting to reset the table
157
158 @retval
159 0 OK
160 @retval
161 1 The table was not found.
162 @retval
163 -1 Error
164 */
165 int reset(THD *thd);
166
167 /**
168 Fetch gtids from gtid_executed table and store them into
169 gtid_executed set.
170
171 @param[out] gtid_set store gtids fetched from the gtid_executed table.
172
173 @retval
174 0 OK
175 @retval
176 1 The table was not found.
177 @retval
178 -1 Error
179 */
180 int fetch_gtids(Gtid_set *gtid_set);
181 /**
182 Compress the gtid_executed table completely by employing one
183 or more transactions.
184
185 @param thd Thread requesting to compress the table
186
187 @retval
188 0 OK
189 @retval
190 1 The table was not found.
191 @retval
192 -1 Error
193 */
194 int compress(THD *thd);
195 /**
196 Push a warning to client if user is modifying the gtid_executed
197 table explicitly by a non-XA transaction. Push an error to client
198 if user is modifying it explicitly by a XA transaction.
199
200 @param thd Thread requesting to access the table
201 @param table The table is being accessed.
202
203 @retval 0 No warning or error was pushed to the client.
204 @retval 1 Push a warning to client.
205 @retval 2 Push an error to client.
206 */
209
213 if (thd->get_transaction()->xid_state()->has_state(
215 /*
216 Push an error to client if user is modifying the gtid_executed
217 table explicitly by a XA transaction.
218 */
219 thd->raise_error_printf(ER_ERROR_ON_MODIFYING_GTID_EXECUTED_TABLE,
220 table->table_name);
221 return 2;
222 } else {
223 /*
224 Push a warning to client if user is modifying the gtid_executed
225 table explicitly by a non-XA transaction.
226 */
227 thd->raise_warning_printf(ER_WARN_ON_MODIFYING_GTID_EXECUTED_TABLE,
228 table->table_name);
229 return 1;
230 }
231 }
232
233 return 0;
234 }
235
236 private:
237 /* Count the append size of the table */
238 std::atomic<int64> m_atomic_count{0};
239 /**
240 Compress the gtid_executed table, read each row by the
241 PK(sid, gno_start) in increasing order, compress the first
242 consecutive range of gtids within a single transaction.
243
244 @param thd Thread requesting to compress the table
245 @param[out] is_complete True if the gtid_executed table is
246 compressd completely.
247
248 @retval
249 0 OK
250 @retval
251 1 The table was not found.
252 @retval
253 -1 Error
254 */
255 int compress_in_single_transaction(THD *thd, bool &is_complete);
256 /**
257 Read each row by the PK(sid, gno_start) in increasing order,
258 compress the first consecutive range of gtids.
259 For example,
260 1 1
261 2 2
262 3 3
263 6 6
264 7 7
265 8 8
266 After the compression, the gtids in the table is compressed as following:
267 1 3
268 6 6
269 7 7
270 8 8
271
272 @param table Reference to a table object.
273 @param[out] is_complete True if the gtid_executed table
274 is compressd completely.
275
276 @retval 0 OK.
277 @retval -1 Error.
278 */
279 int compress_first_consecutive_range(TABLE *table, bool &is_complete);
280 /**
281 Fill a gtid interval into fields of the gtid_executed table.
282
283 @param fields Reference to table fields.
284 @param sid The source id of the gtid interval.
285 @param gno_start The first GNO of the gtid interval.
286 @param gno_end The last GNO of the gtid interval.
287
288 @retval 0 OK.
289 @retval -1 Error.
290 */
291 int fill_fields(Field **fields, const char *sid, rpl_gno gno_start,
292 rpl_gno gno_end);
293 /**
294 Write a gtid interval into the gtid_executed table.
295
296 @param table Reference to a table object.
297 @param sid The source id of the gtid interval.
298 @param gno_start The first GNO of the gtid interval.
299 @param gno_end The last GNO of the gtid interval.
300
301 @retval 0 OK.
302 @retval -1 Error.
303 */
304 int write_row(TABLE *table, const char *sid, rpl_gno gno_start,
305 rpl_gno gno_end);
306 /**
307 Update a gtid interval in the gtid_executed table.
308 - locate the gtid interval by primary key (sid, gno_start)
309 to update it with the new_gno_end.
310
311 @param table Reference to a table object.
312 @param sid The source id of the gtid interval.
313 @param gno_start The first GNO of the gtid interval.
314 @param new_gno_end The new last GNO of the gtid interval.
315
316 @retval 0 OK.
317 @retval -1 Error.
318 */
319 int update_row(TABLE *table, const char *sid, rpl_gno gno_start,
320 rpl_gno new_gno_end);
321 /**
322 Delete all rows in the gtid_executed table.
323
324 @param table Reference to a table object.
325
326 @retval 0 OK.
327 @retval -1 Error.
328 */
329 int delete_all(TABLE *table);
330 /**
331 Encode the current row fetched from the table into gtid text.
332
333 @param table Reference to a table object.
334 @retval Return the encoded gtid text.
335 */
336 std::string encode_gtid_text(TABLE *table);
337 /**
338 Get gtid interval from the the current row of the table.
339
340 @param table Reference to a table object.
341 @param [out] sid The source id of the gtid interval.
342 @param [out] gno_start The first GNO of the gtid interval.
343 @param [out] gno_end The last GNO of the gtid interval.
344 */
345 void get_gtid_interval(TABLE *table, std::string &sid, rpl_gno &gno_start,
346 rpl_gno &gno_end);
347 /**
348 Insert the gtid set into table.
349
350 @param table The gtid_executed table.
351 @param gtid_set Contains a set of gtid, which holds
352 the sidno and the gno.
353
354 @retval
355 0 OK
356 @retval
357 -1 Error
358 */
359 int save(TABLE *table, const Gtid_set *gtid_set);
360 /* Prevent user from invoking default assignment function. */
362 /* Prevent user from invoking default constructor function. */
364};
365
369
370#endif /* RPL_GTID_PERSIST_H_ */
Definition: field.h:573
Represents a set of GTIDs.
Definition: rpl_gtid.h:1454
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:238
int save(THD *thd, const Gtid *gtid)
Insert the gtid into table.
Definition: rpl_gtid_persist.cc:339
int warn_or_err_on_explicit_modification(THD *thd, TABLE_LIST *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:207
Gtid_table_persistor & operator=(const Gtid_table_persistor &info)
static const uint number_fields
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:480
std::string encode_gtid_text(TABLE *table)
Encode the current row fetched from the table into gtid text.
Definition: rpl_gtid_persist.cc:625
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:208
int reset(THD *thd)
Delete all rows from the table.
Definition: rpl_gtid_persist.cc:601
int delete_all(TABLE *table)
Delete all rows in the gtid_executed table.
Definition: rpl_gtid_persist.cc:703
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:270
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:499
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:531
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:643
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:657
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:238
Storage for backup of Open_tables_state.
Definition: sql_class.h:680
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_class.h:922
void raise_error_printf(uint code,...)
Raise an exception condition, with a formatted message.
Definition: sql_class.cc:927
bool is_operating_gtid_table_implicitly
Definition: sql_class.h:2231
void raise_warning_printf(uint code,...)
Raise a completion condition (warning), with a formatted message.
Definition: sql_class.cc:944
Transaction_ctx * get_transaction()
Definition: sql_class.h:2005
XID_STATE * xid_state()
Definition: transaction_info.h:280
@ XA_ACTIVE
Definition: xa.h:298
bool has_state(xa_states state) const
Definition: xa.h:346
DBUG_TRACE
Definition: do_ctype.cc:46
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
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:102
void create_compress_gtid_table_thread()
Create the compression thread to compress gtid_executed table.
Definition: rpl_gtid_persist.cc:801
void terminate_compress_gtid_table_thread()
Terminate the compression thread.
Definition: rpl_gtid_persist.cc:841
Gtid_table_persistor * gtid_table_persistor
Definition: mysqld.cc:1806
TODO: Move this structure to libbinlogevents/include/control_events.h when we start using C++11.
Definition: rpl_gtid.h:1065
thr_lock_type type
Definition: thr_lock.h:99
Definition: mysql_lex_string.h:39
const char * str
Definition: mysql_lex_string.h:40
Definition: table.h:2684
const Lock_descriptor & lock_descriptor() const
Definition: table.h:3395
const char * table_name
Definition: table.h:3378
Definition: table.h:1394
@ TL_WRITE_ALLOW_WRITE
Definition: thr_lock.h:72
unsigned int uint
Definition: uca-dump.cc:29