MySQL  8.0.16
Source Code Documentation
rpl_gtid_persist.h
Go to the documentation of this file.
1 /* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
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"
39 #include "sql/transaction_info.h"
40 #include "sql/xa.h"
41 #include "thr_lock.h"
42 
43 class Field;
44 
46  public:
47  static const LEX_STRING DB_NAME;
48  static const LEX_STRING TABLE_NAME;
49 
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  @return
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  - Reenable 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  @return
80  @retval true failed
81  @retval false success
82  */
83  bool deinit(THD *thd, TABLE *table, bool error, bool need_commit);
84  /**
85  Prepares before opening table.
86  - set flags
87 
88  @param[in] thd Thread requesting to open the table
89  */
90  void before_open(THD *thd);
91  /**
92  Creates a new thread in the bootstrap process or in the mysqld startup,
93  a thread is created in order to be able to access a table. And reset a
94  new "statement".
95 
96  @return
97  @retval THD* Pointer to thread structure
98  */
99  THD *create_thd();
100  void drop_thd(THD *thd);
101 
102  private:
103  /* Pointer to new created THD. */
105  /* Modify the table if it is true. */
107  /* Save the lock info. */
109  /* Save binlog options. */
111  /* Whether or not `THD::set_skip_readonly_check` was invoked during `THD`
112  initialization */
113  bool m_skip_readonly_set{false};
114 
115  /* Prevent user from invoking default assignment function. */
117  /* Prevent user from invoking default constructor function. */
119 };
120 
122  public:
123  static const uint number_fields = 3;
124 
127 
128  /**
129  Insert the gtid into table.
130 
131  @param thd Thread requesting to save gtid into the table
132  @param gtid holds the sidno and the gno.
133 
134  @retval
135  0 OK
136  @retval
137  1 The table was not found.
138  @retval
139  -1 Error
140  */
141  int save(THD *thd, const Gtid *gtid);
142  /**
143  Insert the gtid set into table.
144 
145  @param gtid_set contains a set of gtid, which holds
146  the sidno and the gno.
147 
148  @retval
149  0 OK
150  @retval
151  -1 Error
152  */
153  int save(const Gtid_set *gtid_set);
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  */
209  DBUG_ENTER("Gtid_table_persistor::warn_or_err_on_explicit_modification");
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  DBUG_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  DBUG_RETURN(1);
231  }
232  }
233 
234  DBUG_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  @return
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 fileds.
286  @param sid The source id of the gtid interval.
287  @param gno_start The first GNO of the gtid interval.
288  @param gno_end The last GNO of the gtid interval.
289 
290  @return
291  @retval 0 OK.
292  @retval -1 Error.
293  */
294  int fill_fields(Field **fields, const char *sid, 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 of the gtid interval.
301  @param gno_start The first GNO of the gtid interval.
302  @param gno_end The last GNO of the gtid interval.
303 
304  @return
305  @retval 0 OK.
306  @retval -1 Error.
307  */
308  int write_row(TABLE *table, const char *sid, rpl_gno gno_start,
309  rpl_gno gno_end);
310  /**
311  Update a gtid interval in the gtid_executed table.
312  - locate the gtid interval by primary key (sid, 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 of the gtid interval.
317  @param gno_start The first GNO of the gtid interval.
318  @param new_gno_end The new last GNO of the gtid interval.
319 
320  @return
321  @retval 0 OK.
322  @retval -1 Error.
323  */
324  int update_row(TABLE *table, const char *sid, rpl_gno gno_start,
325  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  @return
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 of the gtid interval.
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, rpl_gno &gno_start,
352  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_ */
unsigned long long int ulonglong
Definition: my_inttypes.h:69
int compress(THD *thd)
Compress the gtid_executed table completely by employing one or more transactions.
Definition: rpl_gtid_persist.cc:490
#define DBUG_RETURN(a1)
Definition: my_dbug.h:84
const Lock_descriptor & lock_descriptor() const
Definition: table.h:3036
char * str
Definition: mysql_lex_string.h:35
Definition: mysql_lex_string.h:34
Gtid_table_access_context()
Definition: rpl_gtid_persist.h:50
int delete_all(TABLE *table)
Delete all rows in the gtid_executed table.
Definition: rpl_gtid_persist.cc:711
void create_compress_gtid_table_thread()
Create the compression thread to compress gtid_executed table.
Definition: rpl_gtid_persist.cc:804
ulonglong m_tmp_disable_binlog__save_options
Definition: rpl_gtid_persist.h:110
static const LEX_STRING DB_NAME
Definition: rpl_gtid_persist.h:47
Some integer typedefs for easier portability.
Definition: rpl_gtid_persist.h:121
void before_open(THD *thd)
Prepares before opening table.
Definition: rpl_gtid_persist.cc:126
void terminate_compress_gtid_table_thread()
Terminate the compression thread.
Definition: rpl_gtid_persist.cc:844
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:217
Definition: field.h:707
Sergei Dialog Client Authentication NULL
Definition: dialog.cc:352
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:279
Transaction_ctx * get_transaction()
Definition: sql_class.h:1699
void drop_thd(THD *thd)
Definition: rpl_gtid_persist.cc:121
const char * table_name
Definition: table.h:3018
Definition: thr_lock.h:72
Storage for backup of Open_tables_state.
Definition: sql_class.h:563
TODO: Move this structure to libbinlogevents/include/control_events.h when we start using C++11...
Definition: rpl_gtid.h:924
static const LEX_STRING TABLE_NAME
Definition: rpl_gtid_persist.h:48
bool m_is_write
Definition: rpl_gtid_persist.h:106
Definition: table.h:1294
THD * m_drop_thd_object
Definition: rpl_gtid_persist.h:104
static const uint number_fields
Definition: rpl_gtid_persist.h:123
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:208
virtual ~Gtid_table_access_context()
Definition: rpl_gtid_persist.h:51
void raise_error_printf(uint code,...)
Raise an exception condition, with a formatted message.
Definition: sql_class.cc:624
#define DBUG_ENTER(a)
Definition: my_dbug.h:80
bool m_skip_readonly_set
Definition: rpl_gtid_persist.h:113
Open_tables_backup m_backup
Definition: rpl_gtid_persist.h:108
Gtid_table_persistor & operator=(const Gtid_table_persistor &info)
Gtid_table_persistor * gtid_table_persistor
Definition: mysqld.cc:1496
Gtid_table_persistor()
Definition: rpl_gtid_persist.h: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:175
virtual ~Gtid_table_persistor()
Definition: rpl_gtid_persist.h:126
Definition: xa.h:363
unsigned int uint
Definition: uca-dump.cc:29
Gtid_table_access_context & operator=(const Gtid_table_access_context &info)
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:108
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:541
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:247
bool init(THD **thd, TABLE **table, bool is_write)
Initialize the gtid_executed table access context as following:
Definition: rpl_gtid_persist.cc:138
std::atomic< int64 > m_atomic_count
Definition: rpl_gtid_persist.h:239
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:665
Represents a set of GTIDs.
Definition: rpl_gtid.h:1302
A base class for accessing a system table.
Definition: rpl_table_access.h:39
XID_STATE * xid_state()
Definition: transaction_info.h:291
thr_lock_type type
Definition: thr_lock.h:99
Definition: rpl_gtid_persist.h:45
void raise_warning_printf(uint code,...)
Raise a completion condition (warning), with a formatted message.
Definition: sql_class.cc:642
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:650
bool has_state(xa_states state) const
Definition: xa.h:405
Log info(cout, "NOTE")
Definition: table.h:2439
int reset(THD *thd)
Delete all rows from the table.
Definition: rpl_gtid_persist.cc:611
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:509
int save(THD *thd, const Gtid *gtid)
Insert the gtid into table.
Definition: rpl_gtid_persist.cc:348
long long int rpl_gno
Type of GNO, the second (numeric) component of GTID.
Definition: rpl_gtid.h:99
std::string encode_gtid_text(TABLE *table)
Encode the current row fetched from the table into gtid text.
Definition: rpl_gtid_persist.cc:632
bool is_operating_gtid_table_implicitly
Definition: sql_class.h:1909
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:776
Log error(cerr, "ERROR")