MySQL  8.0.17
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_CSTRING DB_NAME;
48  static const LEX_CSTRING 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  @param compress notify to compress gtid_executed table
149 
150  @retval
151  0 OK
152  @retval
153  -1 Error
154  */
155  int save(const Gtid_set *gtid_set, bool compress = true);
156  /**
157  Delete all rows from the table.
158 
159  @param thd Thread requesting to reset the table
160 
161  @retval
162  0 OK
163  @retval
164  1 The table was not found.
165  @retval
166  -1 Error
167  */
168  int reset(THD *thd);
169 
170  /**
171  Fetch gtids from gtid_executed table and store them into
172  gtid_executed set.
173 
174  @param[out] gtid_set store gtids fetched from the gtid_executed table.
175 
176  @retval
177  0 OK
178  @retval
179  1 The table was not found.
180  @retval
181  -1 Error
182  */
183  int fetch_gtids(Gtid_set *gtid_set);
184  /**
185  Compress the gtid_executed table completely by employing one
186  or more transactions.
187 
188  @param thd Thread requesting to compress the table
189 
190  @retval
191  0 OK
192  @retval
193  1 The table was not found.
194  @retval
195  -1 Error
196  */
197  int compress(THD *thd);
198  /**
199  Push a warning to client if user is modifying the gtid_executed
200  table explicitly by a non-XA transaction. Push an error to client
201  if user is modifying it explicitly by a XA transaction.
202 
203  @param thd Thread requesting to access the table
204  @param table The table is being accessed.
205 
206  @retval 0 No warning or error was pushed to the client.
207  @retval 1 Push a warning to client.
208  @retval 2 Push an error to client.
209  */
211  DBUG_ENTER("Gtid_table_persistor::warn_or_err_on_explicit_modification");
212 
216  if (thd->get_transaction()->xid_state()->has_state(
218  /*
219  Push an error to client if user is modifying the gtid_executed
220  table explicitly by a XA transaction.
221  */
222  thd->raise_error_printf(ER_ERROR_ON_MODIFYING_GTID_EXECUTED_TABLE,
223  table->table_name);
224  DBUG_RETURN(2);
225  } else {
226  /*
227  Push a warning to client if user is modifying the gtid_executed
228  table explicitly by a non-XA transaction.
229  */
230  thd->raise_warning_printf(ER_WARN_ON_MODIFYING_GTID_EXECUTED_TABLE,
231  table->table_name);
232  DBUG_RETURN(1);
233  }
234  }
235 
236  DBUG_RETURN(0);
237  }
238 
239  private:
240  /* Count the append size of the table */
241  std::atomic<int64> m_atomic_count{0};
242  /**
243  Compress the gtid_executed table, read each row by the
244  PK(sid, gno_start) in increasing order, compress the first
245  consecutive range of gtids within a single transaction.
246 
247  @param thd Thread requesting to compress the table
248  @param[out] is_complete True if the gtid_executed table is
249  compressd completely.
250 
251  @retval
252  0 OK
253  @retval
254  1 The table was not found.
255  @retval
256  -1 Error
257  */
258  int compress_in_single_transaction(THD *thd, bool &is_complete);
259  /**
260  Read each row by the PK(sid, gno_start) in increasing order,
261  compress the first consecutive range of gtids.
262  For example,
263  1 1
264  2 2
265  3 3
266  6 6
267  7 7
268  8 8
269  After the compression, the gtids in the table is compressed as following:
270  1 3
271  6 6
272  7 7
273  8 8
274 
275  @param table Reference to a table object.
276  @param[out] is_complete True if the gtid_executed table
277  is compressd completely.
278 
279  @return
280  @retval 0 OK.
281  @retval -1 Error.
282  */
283  int compress_first_consecutive_range(TABLE *table, bool &is_complete);
284  /**
285  Fill a gtid interval into fields of the gtid_executed table.
286 
287  @param fields Reference to table fileds.
288  @param sid The source id of the gtid interval.
289  @param gno_start The first GNO of the gtid interval.
290  @param gno_end The last GNO of the gtid interval.
291 
292  @return
293  @retval 0 OK.
294  @retval -1 Error.
295  */
296  int fill_fields(Field **fields, const char *sid, rpl_gno gno_start,
297  rpl_gno gno_end);
298  /**
299  Write a gtid interval into the gtid_executed table.
300 
301  @param table Reference to a table object.
302  @param sid The source id of the gtid interval.
303  @param gno_start The first GNO of the gtid interval.
304  @param gno_end The last GNO of the gtid interval.
305 
306  @return
307  @retval 0 OK.
308  @retval -1 Error.
309  */
310  int write_row(TABLE *table, const char *sid, rpl_gno gno_start,
311  rpl_gno gno_end);
312  /**
313  Update a gtid interval in the gtid_executed table.
314  - locate the gtid interval by primary key (sid, gno_start)
315  to update it with the new_gno_end.
316 
317  @param table Reference to a table object.
318  @param sid The source id of the gtid interval.
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  @return
323  @retval 0 OK.
324  @retval -1 Error.
325  */
326  int update_row(TABLE *table, const char *sid, rpl_gno gno_start,
327  rpl_gno new_gno_end);
328  /**
329  Delete all rows in the gtid_executed table.
330 
331  @param table Reference to a table object.
332 
333  @return
334  @retval 0 OK.
335  @retval -1 Error.
336  */
337  int delete_all(TABLE *table);
338  /**
339  Encode the current row fetched from the table into gtid text.
340 
341  @param table Reference to a table object.
342  @retval Return the encoded gtid text.
343  */
344  std::string encode_gtid_text(TABLE *table);
345  /**
346  Get gtid interval from the the current row of the table.
347 
348  @param table Reference to a table object.
349  @param [out] sid The source id of the gtid interval.
350  @param [out] gno_start The first GNO of the gtid interval.
351  @param [out] gno_end The last GNO of the gtid interval.
352  */
353  void get_gtid_interval(TABLE *table, std::string &sid, rpl_gno &gno_start,
354  rpl_gno &gno_end);
355  /**
356  Insert the gtid set into table.
357 
358  @param table The gtid_executed table.
359  @param gtid_set Contains a set of gtid, which holds
360  the sidno and the gno.
361 
362  @retval
363  0 OK
364  @retval
365  -1 Error
366  */
367  int save(TABLE *table, const Gtid_set *gtid_set);
368  /* Prevent user from invoking default assignment function. */
370  /* Prevent user from invoking default constructor function. */
372 };
373 
377 
378 #endif /* RPL_GTID_PERSIST_H_ */
unsigned long long int ulonglong
Definition: my_inttypes.h:74
int compress(THD *thd)
Compress the gtid_executed table completely by employing one or more transactions.
Definition: rpl_gtid_persist.cc:491
#define DBUG_RETURN(a1)
Definition: my_dbug.h:139
const Lock_descriptor & lock_descriptor() const
Definition: table.h:3047
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:715
void create_compress_gtid_table_thread()
Create the compression thread to compress gtid_executed table.
Definition: rpl_gtid_persist.cc:808
ulonglong m_tmp_disable_binlog__save_options
Definition: rpl_gtid_persist.h:110
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:848
const char * str
Definition: mysql_lex_string.h:40
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:709
Definition: mysql_lex_string.h:39
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:1700
void drop_thd(THD *thd)
Definition: rpl_gtid_persist.cc:121
static const LEX_CSTRING DB_NAME
Definition: rpl_gtid_persist.h:47
const char * table_name
Definition: table.h:3029
Definition: thr_lock.h:72
Storage for backup of Open_tables_state.
Definition: sql_class.h:564
TODO: Move this structure to libbinlogevents/include/control_events.h when we start using C++11...
Definition: rpl_gtid.h:924
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
static const LEX_CSTRING TABLE_NAME
Definition: rpl_gtid_persist.h:48
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:210
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:135
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:1586
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:542
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:241
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:669
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:287
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:654
bool has_state(xa_states state) const
Definition: xa.h:405
Log info(cout, "NOTE")
Definition: table.h:2442
int reset(THD *thd)
Delete all rows from the table.
Definition: rpl_gtid_persist.cc:612
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:510
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:636
bool is_operating_gtid_table_implicitly
Definition: sql_class.h:1910
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:777
Log error(cerr, "ERROR")