Skip to content

Commit ddfd497

Browse files
Bill QuBill Qu
Bill Qu
authored and
Bill Qu
committed
Bug#18410442 CREATE A BASE CLASS FOR GTID_TABLE_ACCESS_CONTEXT AND RPL_INFO_TABLE_ACCESS
Post-fix: Rename the class 'Table_access' with 'System_table_access', and declare the class 'System_table_access' in a new replication file named 'rpl_table_access.h' and define it in rpl_table_access.cc.
1 parent 8ae55d8 commit ddfd497

10 files changed

+276
-234
lines changed

libmysqld/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ SET(SQL_EMBEDDED_SOURCES
8080
../sql/rpl_gtid_execution.cc
8181
../sql/rpl_gtid_mutex_cond_array.cc
8282
../sql/rpl_gtid_persist.cc
83+
../sql/rpl_table_access.cc
8384
${IMPORTED_SOURCES}
8485
)
8586

sql/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ SET (BINLOG_SOURCE uuid.cc rpl_gtid_misc.cc
309309
rpl_gtid_mutex_cond_array.cc rpl_gtid_persist.cc
310310
log_event.cc log_event_old.cc binlog.cc sql_binlog.cc
311311
rpl_filter.cc rpl_record.cc rpl_record_old.cc
312-
rpl_utility.cc rpl_injector.cc)
312+
rpl_utility.cc rpl_injector.cc rpl_table_access.cc)
313313
ADD_LIBRARY(binlog ${BINLOG_SOURCE})
314314
SET (RPL_SOURCE rpl_handler.cc rpl_tblmap.cc)
315315
ADD_DEPENDENCIES(binlog GenError)

sql/rpl_gtid_persist.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static void deinit_thd(THD *thd)
7171

7272
THD *Gtid_table_access_context::create_thd()
7373
{
74-
THD *thd= Table_access::create_thd();
74+
THD *thd= System_table_access::create_thd();
7575
thd->system_thread= SYSTEM_THREAD_COMPRESS_GTID_TABLE;
7676
/*
7777
This is equivalent to a new "statement". For that reason, we call

sql/rpl_gtid_persist.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
#define RPL_GTID_PERSIST_H_
2020

2121
#include <string>
22-
#include "sql_base.h"
22+
#include "rpl_table_access.h"
2323
using std::string;
2424

2525
class Open_tables_backup;
2626

2727

28-
class Gtid_table_access_context : public Table_access
28+
class Gtid_table_access_context : public System_table_access
2929
{
3030

3131
public:

sql/rpl_info_table_access.cc

+3-4
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ void Rpl_info_table_access::close_table(THD *thd, TABLE* table,
6666
bool error)
6767
{
6868
DBUG_ENTER("Rpl_info_table_access::close_table");
69-
70-
Table_access::close_table(thd, table, backup, error, thd_created);
69+
System_table_access::close_table(thd, table, backup, error, thd_created);
7170
DBUG_VOID_RETURN;
7271
}
7372

@@ -327,7 +326,7 @@ THD *Rpl_info_table_access::create_thd()
327326

328327
if (!thd)
329328
{
330-
thd= Table_access::create_thd();
329+
thd= System_table_access::create_thd();
331330
thd->system_thread= SYSTEM_THREAD_INFO_REPOSITORY;
332331
thd_created= true;
333332
}
@@ -347,7 +346,7 @@ void Rpl_info_table_access::drop_thd(THD *thd)
347346

348347
if (thd_created)
349348
{
350-
Table_access::drop_thd(thd);
349+
System_table_access::drop_thd(thd);
351350
thd_created= false;
352351
}
353352

sql/rpl_info_table_access.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
#include "sql_priv.h"
2020
#include <table.h>
2121
#include <key.h>
22-
#include <sql_base.h>
22+
#include "rpl_table_access.h"
2323
#include "rpl_info_handler.h"
2424
#include "rpl_info_values.h"
2525

2626
enum enum_return_id { FOUND_ID= 1, NOT_FOUND_ID, ERROR_ID };
2727

28-
class Rpl_info_table_access : public Table_access
28+
class Rpl_info_table_access : public System_table_access
2929
{
3030
public:
3131
Rpl_info_table_access(): thd_created(false) { };

sql/rpl_table_access.cc

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2+
3+
This program is free software; you can redistribute it and/or
4+
modify it under the terms of the GNU General Public License as
5+
published by the Free Software Foundation; version 2 of the
6+
License.
7+
8+
This program is distributed in the hope that it will be useful, but
9+
WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with this program; if not, write to the Free Software
15+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16+
02110-1301 USA */
17+
18+
#include "log.h"
19+
#include "rpl_table_access.h"
20+
21+
bool System_table_access::open_table(THD* thd, const LEX_STRING dbstr,
22+
const LEX_STRING tbstr,
23+
uint max_num_field,
24+
enum thr_lock_type lock_type,
25+
TABLE** table,
26+
Open_tables_backup* backup)
27+
{
28+
TABLE_LIST tables;
29+
Query_tables_list query_tables_list_backup;
30+
31+
DBUG_ENTER("System_table_access::open_table");
32+
before_open(thd);
33+
34+
/*
35+
We need to use new Open_tables_state in order not to be affected
36+
by LOCK TABLES/prelocked mode.
37+
Also in order not to break execution of current statement we also
38+
have to backup/reset/restore Query_tables_list part of LEX, which
39+
is accessed and updated in the process of opening and locking
40+
tables.
41+
*/
42+
thd->lex->reset_n_backup_query_tables_list(&query_tables_list_backup);
43+
thd->reset_n_backup_open_tables_state(backup);
44+
45+
tables.init_one_table(dbstr.str, dbstr.length, tbstr.str, tbstr.length,
46+
tbstr.str, lock_type);
47+
48+
tables.open_strategy= TABLE_LIST::OPEN_IF_EXISTS;
49+
50+
if (!open_n_lock_single_table(thd, &tables, tables.lock_type, m_flags))
51+
{
52+
close_thread_tables(thd);
53+
thd->restore_backup_open_tables_state(backup);
54+
thd->lex->restore_backup_query_tables_list(&query_tables_list_backup);
55+
if (thd->is_operating_gtid_table)
56+
sql_print_warning("Gtid table is not ready to be used. Table '%s.%s' "
57+
"cannot be opened.", dbstr.str, tbstr.str);
58+
else
59+
my_error(ER_NO_SUCH_TABLE, MYF(0), dbstr.str, tbstr.str);
60+
DBUG_RETURN(true);
61+
}
62+
63+
if (tables.table->s->fields < max_num_field)
64+
{
65+
/*
66+
Safety: this can only happen if someone started the server and then
67+
altered the table.
68+
*/
69+
ha_rollback_trans(thd, false);
70+
close_thread_tables(thd);
71+
thd->restore_backup_open_tables_state(backup);
72+
thd->lex->restore_backup_query_tables_list(&query_tables_list_backup);
73+
my_error(ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2, MYF(0),
74+
tables.table->s->db.str, tables.table->s->table_name.str,
75+
max_num_field, tables.table->s->fields);
76+
DBUG_RETURN(true);
77+
}
78+
79+
thd->lex->restore_backup_query_tables_list(&query_tables_list_backup);
80+
81+
*table= tables.table;
82+
tables.table->use_all_columns();
83+
DBUG_RETURN(false);
84+
}
85+
86+
87+
void System_table_access::close_table(THD *thd, TABLE* table,
88+
Open_tables_backup *backup,
89+
bool error, bool need_commit)
90+
{
91+
Query_tables_list query_tables_list_backup;
92+
93+
DBUG_ENTER("System_table_access::close_table");
94+
95+
if (table)
96+
{
97+
if (error)
98+
ha_rollback_trans(thd, false);
99+
else
100+
{
101+
/*
102+
To make the commit not to block with global read lock set
103+
"ignore_global_read_lock" flag to true.
104+
*/
105+
ha_commit_trans(thd, false, true);
106+
}
107+
if (need_commit)
108+
{
109+
if (error)
110+
ha_rollback_trans(thd, true);
111+
else
112+
{
113+
/*
114+
To make the commit not to block with global read lock set
115+
"ignore_global_read_lock" flag to true.
116+
*/
117+
ha_commit_trans(thd, true, true);
118+
}
119+
}
120+
/*
121+
In order not to break execution of current statement we have to
122+
backup/reset/restore Query_tables_list part of LEX, which is
123+
accessed and updated in the process of closing tables.
124+
*/
125+
thd->lex->reset_n_backup_query_tables_list(&query_tables_list_backup);
126+
close_thread_tables(thd);
127+
thd->lex->restore_backup_query_tables_list(&query_tables_list_backup);
128+
thd->restore_backup_open_tables_state(backup);
129+
}
130+
131+
DBUG_VOID_RETURN;
132+
}
133+
134+
135+
THD *System_table_access::create_thd()
136+
{
137+
THD *thd= NULL;
138+
thd= new THD;
139+
thd->thread_stack= (char*) &thd;
140+
thd->store_globals();
141+
thd->security_ctx->skip_grants();
142+
143+
return(thd);
144+
}
145+
146+
147+
void System_table_access::drop_thd(THD *thd)
148+
{
149+
DBUG_ENTER("System_table_access::drop_thd");
150+
151+
delete thd;
152+
my_pthread_setspecific_ptr(THR_THD, NULL);
153+
154+
DBUG_VOID_RETURN;
155+
}
156+

sql/rpl_table_access.h

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2+
3+
This program is free software; you can redistribute it and/or
4+
modify it under the terms of the GNU General Public License as
5+
published by the Free Software Foundation; version 2 of the
6+
License.
7+
8+
This program is distributed in the hope that it will be useful, but
9+
WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with this program; if not, write to the Free Software
15+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16+
02110-1301 USA */
17+
18+
#ifndef RPL_TABLE_ACCESS_H_
19+
#define RPL_TABLE_ACCESS_H_
20+
21+
#include "sql_base.h"
22+
23+
/**
24+
A base class for accessing a system table.
25+
*/
26+
27+
class System_table_access
28+
{
29+
public:
30+
virtual ~System_table_access() { }
31+
32+
/**
33+
Opens and locks a system table.
34+
35+
It's assumed that the caller knows what they are doing:
36+
- whether it was necessary to reset-and-backup the open tables state
37+
- whether the requested lock does not lead to a deadlock
38+
- whether this open mode would work under LOCK TABLES, or inside a
39+
stored function or trigger.
40+
41+
Note that if the table can't be locked successfully this operation will
42+
close it. Therefore it provides guarantee that it either opens and locks
43+
table or fails without leaving any tables open.
44+
45+
@param[in] thd Thread requesting to open the table
46+
@param[in] dbstr Database where the table resides
47+
@param[in] tbstr Table to be openned
48+
@param[in] max_num_field Maximum number of fields
49+
@param[in] lock_type How to lock the table
50+
@param[out] table We will store the open table here
51+
@param[out] backup Save the lock info. here
52+
53+
@return
54+
@retval TRUE open and lock failed - an error message is pushed into the
55+
stack
56+
@retval FALSE success
57+
*/
58+
bool open_table(THD* thd, const LEX_STRING dbstr, const LEX_STRING tbstr,
59+
uint max_num_field, enum thr_lock_type lock_type,
60+
TABLE** table, Open_tables_backup* backup);
61+
/**
62+
Prepares before opening table.
63+
64+
@param[in] thd Thread requesting to open the table
65+
*/
66+
virtual void before_open(THD* thd)= 0;
67+
/**
68+
Commits the changes, unlocks the table and closes it. This method
69+
needs to be called even if the open_table fails, in order to ensure
70+
the lock info is properly restored.
71+
72+
@param[in] thd Thread requesting to close the table
73+
@param[in] table Table to be closed
74+
@param[in] backup Restore the lock info from here
75+
@param[in] error If there was an error while updating
76+
the table
77+
@param[in] need_commit Need to commit current transaction
78+
if it is true.
79+
80+
If there is an error, rolls back the current statement. Otherwise,
81+
commits it. However, if a new thread was created and there is an
82+
error, the transaction must be rolled back. Otherwise, it must be
83+
committed. In this case, the changes were not done on behalf of
84+
any user transaction and if not finished, there would be pending
85+
changes.
86+
*/
87+
void close_table(THD *thd, TABLE* table, Open_tables_backup *backup,
88+
bool error, bool need_commit);
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.
92+
93+
@return
94+
@retval THD* Pointer to thread structure
95+
*/
96+
THD *create_thd();
97+
/**
98+
Destroys the created thread and restores the
99+
system_thread information.
100+
101+
@param thd Thread requesting to be destroyed
102+
*/
103+
void drop_thd(THD *thd);
104+
105+
/* Flags for opening table */
106+
uint m_flags;
107+
};
108+
109+
110+
#endif /* RPL_TABLE_ACCESS_H_ */

0 commit comments

Comments
 (0)