mirror of
https://github.com/MariaDB/server.git
synced 2025-01-16 20:12:31 +01:00
4462578a1c
Bug#25422 (Hang with log tables) Bug 17876 (Truncating mysql.slow_log in a SP after using cursor locks the thread) Bug 23044 (Warnings on flush of a log table) Bug 29129 (Resetting general_log while the GLOBAL READ LOCK is set causes a deadlock) Prior to this fix, the server would hang when performing concurrent ALTER TABLE or TRUNCATE TABLE statements against the LOG TABLES, which are mysql.general_log and mysql.slow_log. The root cause traces to the following code: in sql_base.cc, open_table() if (table->in_use != thd) { /* wait_for_condition will unlock LOCK_open for us */ wait_for_condition(thd, &LOCK_open, &COND_refresh); } The problem with this code is that the current implementation of the LOGGER creates 'fake' THD objects, like - Log_to_csv_event_handler::general_log_thd - Log_to_csv_event_handler::slow_log_thd which are not associated to a real thread running in the server, so that waiting for these non-existing threads to release table locks cause the dead lock. In general, the design of Log_to_csv_event_handler does not fit into the general architecture of the server, so that the concept of general_log_thd and slow_log_thd has to be abandoned: - this implementation does not work with table locking - it will not work with commands like SHOW PROCESSLIST - having the log tables always opened does not integrate well with DDL operations / FLUSH TABLES / SET GLOBAL READ_ONLY With this patch, the fundamental design of the LOGGER has been changed to: - always open and close a log table when writing a log - remove totally the usage of fake THD objects - clarify how locking of log tables is implemented in general. See WL#3984 for details related to the new locking design. Additional changes (misc bugs exposed and fixed): 1) mysqldump which would ignore some tables in dump_all_tables_in_db(), but forget to ignore the same in dump_all_views_in_db(). 2) mysqldump would also issue an empty "LOCK TABLE" command when all the tables to lock are to be ignored (numrows == 0), instead of not issuing the query. 3) Internal errors handlers could intercept errors but not warnings (see sql_error.cc). 4) Implementing a nested call to open tables, for the performance schema tables, exposed an existing bug in remove_table_from_cache(), which would perform: in_use->some_tables_deleted=1; against another thread, without any consideration about thread locking. This call inside remove_table_from_cache() was not required anyway, since calling mysql_lock_abort() takes care of aborting -- cleanly -- threads that might hold a lock on a table. This line (in_use->some_tables_deleted=1) has been removed. sql/handler.cc: Moved logic for system / log tables in the SQL layer. sql/handler.h: Moved logic for system / log tables in the SQL layer. sql/lock.cc: Revised locking of log tables sql/log.cc: Major cleanup: changed how log tables are locked / written to. sql/log.h: Major cleanup: changed how log tables are locked / written to. sql/mysql_priv.h: performance schema helpers sql/slave.cc: open_ltable() lock flags sql/sp.cc: open_ltable() lock flags sql/sql_acl.cc: open_ltable() lock flags sql/sql_class.h: performance schema helpers sql/sql_delete.cc: log tables cleanup in TRUNCATE sql/sql_error.cc: Internal handlers can also intercept warnings sql/sql_insert.cc: open_ltable() lock flags sql/sql_parse.cc: performance schema helpers sql/sql_plugin.cc: open_ltable() lock flags sql/sql_rename.cc: log tables cleanup in RENAME sql/sql_servers.cc: open_ltable() lock flags sql/sql_show.cc: Move INFORMATION_SCHEMA_NAME to table.cc sql/sql_table.cc: log tables cleanup (admin operations, ALTER TABLE) sql/sql_udf.cc: open_ltable() lock flags sql/table.cc: Implemented TABLE_CATEGORY. sql/share/errmsg.txt: Changed the wording and name of ER_CANT_READ_LOCK_LOG_TABLE sql/table.h: Implemented TABLE_CATEGORY. storage/csv/ha_tina.cc: Moved logic for system / log tables in the SQL layer. storage/csv/ha_tina.h: Moved logic for system / log tables in the SQL layer. storage/myisam/ha_myisam.cc: Moved logic for system / log tables in the SQL layer. storage/myisam/ha_myisam.h: Moved logic for system / log tables in the SQL layer. client/mysqldump.c: Don't lock tables in the ignore list. Don't issue empty LOCK TABLES queries. sql/sql_base.cc: log tables cleanup performance schema helpers mysql-test/r/ps.result: Adjust test results mysql-test/r/show_check.result: Adjust test results mysql-test/r/status.result: Adjust test results mysql-test/t/log_state.test: Added tests for Bug#29129 mysql-test/t/ps.test: Make the test output deterministic mysql-test/t/show_check.test: Make the test output deterministic mysql-test/r/log_state.result: Changed the default location of the log output to LOG_FILE, for backward compatibility with MySQL 5.0 --- Adjust test results mysql-test/r/log_tables.result: cleanup for -ps-protocol mysql-test/t/log_tables.test: cleanup for -ps-protocol sql/set_var.cc: Changed the default location of the log output to LOG_FILE, for backward compatibility with MySQL 5.0 --- log tables cleanup
360 lines
10 KiB
C++
360 lines
10 KiB
C++
/* Copyright (C) 2000-2006 MySQL AB
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
/*
|
|
Atomic rename of table; RENAME TABLE t1 to t2, tmp to t1 [,...]
|
|
*/
|
|
|
|
#include "mysql_priv.h"
|
|
#include "sql_trigger.h"
|
|
|
|
|
|
static TABLE_LIST *rename_tables(THD *thd, TABLE_LIST *table_list,
|
|
bool skip_error);
|
|
|
|
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list);
|
|
|
|
/*
|
|
Every second entry in the table_list is the original name and every
|
|
second entry is the new name.
|
|
*/
|
|
|
|
bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list, bool silent)
|
|
{
|
|
bool error= 1;
|
|
TABLE_LIST *ren_table= 0;
|
|
int to_table;
|
|
char *rename_log_table[2]= {NULL, NULL};
|
|
DBUG_ENTER("mysql_rename_tables");
|
|
|
|
/*
|
|
Avoid problems with a rename on a table that we have locked or
|
|
if the user is trying to to do this in a transcation context
|
|
*/
|
|
|
|
if (thd->locked_tables || thd->active_transaction())
|
|
{
|
|
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
|
|
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
|
|
DBUG_RETURN(1);
|
|
}
|
|
|
|
if (wait_if_global_read_lock(thd,0,1))
|
|
DBUG_RETURN(1);
|
|
|
|
if (logger.is_log_table_enabled(QUERY_LOG_GENERAL) ||
|
|
logger.is_log_table_enabled(QUERY_LOG_SLOW))
|
|
{
|
|
|
|
/*
|
|
Rules for rename of a log table:
|
|
|
|
IF 1. Log tables are enabled
|
|
AND 2. Rename operates on the log table and nothing is being
|
|
renamed to the log table.
|
|
DO 3. Throw an error message.
|
|
ELSE 4. Perform rename.
|
|
*/
|
|
|
|
for (to_table= 0, ren_table= table_list; ren_table;
|
|
to_table= 1 - to_table, ren_table= ren_table->next_local)
|
|
{
|
|
int log_table_rename= 0;
|
|
|
|
if ((log_table_rename=
|
|
check_if_log_table(ren_table->db_length, ren_table->db,
|
|
ren_table->table_name_length,
|
|
ren_table->table_name, 1)))
|
|
{
|
|
/*
|
|
as we use log_table_rename as an array index, we need it to start
|
|
with 0, while QUERY_LOG_SLOW == 1 and QUERY_LOG_GENERAL == 2.
|
|
So, we shift the value to start with 0;
|
|
*/
|
|
log_table_rename--;
|
|
if (rename_log_table[log_table_rename])
|
|
{
|
|
if (to_table)
|
|
rename_log_table[log_table_rename]= NULL;
|
|
else
|
|
{
|
|
/*
|
|
Two renames of "log_table TO" w/o rename "TO log_table" in
|
|
between.
|
|
*/
|
|
my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), ren_table->table_name,
|
|
ren_table->table_name);
|
|
DBUG_RETURN(1);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (to_table)
|
|
{
|
|
/*
|
|
Attempt to rename a table TO log_table w/o renaming
|
|
log_table TO some table.
|
|
*/
|
|
my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), ren_table->table_name,
|
|
ren_table->table_name);
|
|
DBUG_RETURN(1);
|
|
}
|
|
else
|
|
{
|
|
/* save the name of the log table to report an error */
|
|
rename_log_table[log_table_rename]= ren_table->table_name;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (rename_log_table[0] || rename_log_table[1])
|
|
{
|
|
if (rename_log_table[0])
|
|
my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), rename_log_table[0],
|
|
rename_log_table[0]);
|
|
else
|
|
my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), rename_log_table[1],
|
|
rename_log_table[1]);
|
|
DBUG_RETURN(1);
|
|
}
|
|
}
|
|
|
|
pthread_mutex_lock(&LOCK_open);
|
|
if (lock_table_names_exclusively(thd, table_list))
|
|
{
|
|
pthread_mutex_unlock(&LOCK_open);
|
|
goto err;
|
|
}
|
|
|
|
error=0;
|
|
if ((ren_table=rename_tables(thd,table_list,0)))
|
|
{
|
|
/* Rename didn't succeed; rename back the tables in reverse order */
|
|
TABLE_LIST *table;
|
|
|
|
/* Reverse the table list */
|
|
table_list= reverse_table_list(table_list);
|
|
|
|
/* Find the last renamed table */
|
|
for (table= table_list;
|
|
table->next_local != ren_table ;
|
|
table= table->next_local->next_local) ;
|
|
table= table->next_local->next_local; // Skip error table
|
|
/* Revert to old names */
|
|
rename_tables(thd, table, 1);
|
|
|
|
/* Revert the table list (for prepared statements) */
|
|
table_list= reverse_table_list(table_list);
|
|
|
|
error= 1;
|
|
}
|
|
/*
|
|
An exclusive lock on table names is satisfactory to ensure
|
|
no other thread accesses this table.
|
|
However, NDB assumes that handler::rename_tables is called under
|
|
LOCK_open. And it indeed is, from ALTER TABLE.
|
|
TODO: remove this limitation.
|
|
We still should unlock LOCK_open as early as possible, to provide
|
|
higher concurrency - query_cache_invalidate can take minutes to
|
|
complete.
|
|
*/
|
|
pthread_mutex_unlock(&LOCK_open);
|
|
|
|
/* Lets hope this doesn't fail as the result will be messy */
|
|
if (!silent && !error)
|
|
{
|
|
write_bin_log(thd, TRUE, thd->query, thd->query_length);
|
|
send_ok(thd);
|
|
}
|
|
|
|
if (!error)
|
|
query_cache_invalidate3(thd, table_list, 0);
|
|
|
|
pthread_mutex_lock(&LOCK_open);
|
|
unlock_table_names(thd, table_list, (TABLE_LIST*) 0);
|
|
pthread_mutex_unlock(&LOCK_open);
|
|
|
|
err:
|
|
start_waiting_global_read_lock(thd);
|
|
DBUG_RETURN(error);
|
|
}
|
|
|
|
|
|
/*
|
|
reverse table list
|
|
|
|
SYNOPSIS
|
|
reverse_table_list()
|
|
table_list pointer to table _list
|
|
|
|
RETURN
|
|
pointer to new (reversed) list
|
|
*/
|
|
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list)
|
|
{
|
|
TABLE_LIST *prev= 0;
|
|
|
|
while (table_list)
|
|
{
|
|
TABLE_LIST *next= table_list->next_local;
|
|
table_list->next_local= prev;
|
|
prev= table_list;
|
|
table_list= next;
|
|
}
|
|
return (prev);
|
|
}
|
|
|
|
|
|
/*
|
|
Rename a single table or a view
|
|
|
|
SYNPOSIS
|
|
do_rename()
|
|
thd Thread handle
|
|
ren_table A table/view to be renamed
|
|
new_db The database to which the table to be moved to
|
|
new_table_name The new table/view name
|
|
new_table_alias The new table/view alias
|
|
skip_error Whether to skip error
|
|
|
|
DESCRIPTION
|
|
Rename a single table or a view.
|
|
|
|
RETURN
|
|
false Ok
|
|
true rename failed
|
|
*/
|
|
|
|
bool
|
|
do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db, char *new_table_name,
|
|
char *new_table_alias, bool skip_error)
|
|
{
|
|
int rc= 1;
|
|
char name[FN_REFLEN];
|
|
const char *new_alias, *old_alias;
|
|
frm_type_enum frm_type;
|
|
enum legacy_db_type table_type;
|
|
|
|
DBUG_ENTER("do_rename");
|
|
|
|
if (lower_case_table_names == 2)
|
|
{
|
|
old_alias= ren_table->alias;
|
|
new_alias= new_table_alias;
|
|
}
|
|
else
|
|
{
|
|
old_alias= ren_table->table_name;
|
|
new_alias= new_table_name;
|
|
}
|
|
build_table_filename(name, sizeof(name),
|
|
new_db, new_alias, reg_ext, 0);
|
|
if (!access(name,F_OK))
|
|
{
|
|
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
|
|
DBUG_RETURN(1); // This can't be skipped
|
|
}
|
|
build_table_filename(name, sizeof(name),
|
|
ren_table->db, old_alias, reg_ext, 0);
|
|
|
|
frm_type= mysql_frm_type(thd, name, &table_type);
|
|
switch (frm_type)
|
|
{
|
|
case FRMTYPE_TABLE:
|
|
{
|
|
if (!(rc= mysql_rename_table(ha_resolve_by_legacy_type(thd,
|
|
table_type),
|
|
ren_table->db, old_alias,
|
|
new_db, new_alias, 0)))
|
|
{
|
|
if ((rc= Table_triggers_list::change_table_name(thd, ren_table->db,
|
|
old_alias,
|
|
new_db,
|
|
new_alias)))
|
|
{
|
|
/*
|
|
We've succeeded in renaming table's .frm and in updating
|
|
corresponding handler data, but have failed to update table's
|
|
triggers appropriately. So let us revert operations on .frm
|
|
and handler's data and report about failure to rename table.
|
|
*/
|
|
(void) mysql_rename_table(ha_resolve_by_legacy_type(thd,
|
|
table_type),
|
|
new_db, new_alias,
|
|
ren_table->db, old_alias, 0);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case FRMTYPE_VIEW:
|
|
/* change of schema is not allowed */
|
|
if (strcmp(ren_table->db, new_db))
|
|
my_error(ER_FORBID_SCHEMA_CHANGE, MYF(0), ren_table->db,
|
|
new_db);
|
|
else
|
|
rc= mysql_rename_view(thd, new_alias, ren_table);
|
|
break;
|
|
default:
|
|
DBUG_ASSERT(0); // should never happen
|
|
case FRMTYPE_ERROR:
|
|
my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno);
|
|
break;
|
|
}
|
|
if (rc && !skip_error)
|
|
DBUG_RETURN(1);
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
/*
|
|
Rename all tables in list; Return pointer to wrong entry if something goes
|
|
wrong. Note that the table_list may be empty!
|
|
*/
|
|
|
|
/*
|
|
Rename tables/views in the list
|
|
|
|
SYNPOSIS
|
|
rename_tables()
|
|
thd Thread handle
|
|
table_list List of tables to rename
|
|
skip_error Whether to skip errors
|
|
|
|
DESCRIPTION
|
|
Take a table/view name from and odd list element and rename it to a
|
|
the name taken from list element+1. Note that the table_list may be
|
|
empty.
|
|
|
|
RETURN
|
|
false Ok
|
|
true rename failed
|
|
*/
|
|
|
|
static TABLE_LIST *
|
|
rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error)
|
|
{
|
|
TABLE_LIST *ren_table, *new_table;
|
|
|
|
DBUG_ENTER("rename_tables");
|
|
|
|
for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
|
|
{
|
|
new_table= ren_table->next_local;
|
|
if (do_rename(thd, ren_table, new_table->db, new_table->table_name,
|
|
new_table->alias, skip_error))
|
|
DBUG_RETURN(ren_table);
|
|
}
|
|
DBUG_RETURN(0);
|
|
}
|