mariadb/sql/lock.cc

1355 lines
41 KiB
C++
Raw Normal View History

/* Copyright (C) 2000-2006 MySQL AB
2000-07-31 21:29:14 +02:00
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.
2000-07-31 21:29:14 +02:00
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.
2000-07-31 21:29:14 +02:00
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 */
/* locking functions for mysql */
/*
Because of the new concurrent inserts, we must first get external locks
before getting internal locks. If we do it in the other order, the status
information is not up to date when called from the lock handler.
GENERAL DESCRIPTION OF LOCKING
When not using LOCK TABLES:
- For each SQL statement mysql_lock_tables() is called for all involved
tables.
- mysql_lock_tables() will call
table_handler->external_lock(thd,locktype) for each table.
This is followed by a call to thr_multi_lock() for all tables.
- When statement is done, we call mysql_unlock_tables().
This will call thr_multi_unlock() followed by
table_handler->external_lock(thd, F_UNLCK) for each table.
- Note that mysql_unlock_tables() may be called several times as
MySQL in some cases can free some tables earlier than others.
- The above is true both for normal and temporary tables.
- Temporary non transactional tables are never passed to thr_multi_lock()
and we never call external_lock(thd, F_UNLOCK) on these.
When using LOCK TABLES:
- LOCK TABLE will call mysql_lock_tables() for all tables.
mysql_lock_tables() will call
table_handler->external_lock(thd,locktype) for each table.
This is followed by a call to thr_multi_lock() for all tables.
- For each statement, we will call table_handler->start_stmt(THD)
to inform the table handler that we are using the table.
The tables used can only be tables used in LOCK TABLES or a
temporary table.
- When statement is done, we will call ha_commit_stmt(thd);
- When calling UNLOCK TABLES we call mysql_unlock_tables() for all
tables used in LOCK TABLES
2000-07-31 21:29:14 +02:00
TODO:
Change to use my_malloc() ONLY when using LOCK TABLES command or when
we are forced to use mysql_lock_merge.
*/
#include "mysql_priv.h"
#include <hash.h>
#include <assert.h>
2000-07-31 21:29:14 +02:00
extern HASH open_cache;
/* flags for get_lock_data */
#define GET_LOCK_UNLOCK 1
#define GET_LOCK_STORE_LOCKS 2
2000-07-31 21:29:14 +02:00
static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table,uint count,
uint flags, TABLE **write_locked);
static void reset_lock_data(MYSQL_LOCK *sql_lock);
static int lock_external(THD *thd, TABLE **table,uint count);
2000-07-31 21:29:14 +02:00
static int unlock_external(THD *thd, TABLE **table,uint count);
static void print_lock_error(int error, const char *);
2000-07-31 21:29:14 +02:00
/*
Lock tables.
SYNOPSIS
mysql_lock_tables()
thd The current thread.
tables An array of pointers to the tables to lock.
count The number of tables to lock.
flags Options:
MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK Ignore a global read lock
MYSQL_LOCK_IGNORE_FLUSH Ignore a flush tables.
MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN Instead of reopening altered
or dropped tables by itself,
mysql_lock_tables() should
notify upper level and rely
on caller doing this.
need_reopen Out parameter, TRUE if some tables were altered
or deleted and should be reopened by caller.
RETURN
A lock structure pointer on success.
NULL on error or if some tables should be reopen.
*/
/* Map the return value of thr_lock to an error from errmsg.txt */
2005-07-28 20:39:24 +02:00
static int thr_lock_errno_to_mysql[]=
{ 0, 1, ER_LOCK_WAIT_TIMEOUT, ER_LOCK_DEADLOCK };
MYSQL_LOCK *mysql_lock_tables(THD *thd, TABLE **tables, uint count,
uint flags, bool *need_reopen)
2000-07-31 21:29:14 +02:00
{
MYSQL_LOCK *sql_lock;
TABLE *write_lock_used;
int rc;
2000-07-31 21:29:14 +02:00
DBUG_ENTER("mysql_lock_tables");
*need_reopen= FALSE;
2000-07-31 21:29:14 +02:00
for (;;)
{
if (! (sql_lock= get_lock_data(thd, tables, count, GET_LOCK_STORE_LOCKS,
&write_lock_used)))
2000-07-31 21:29:14 +02:00
break;
if (global_read_lock && write_lock_used &&
! (flags & MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK))
2000-07-31 21:29:14 +02:00
{
/*
Someone has issued LOCK ALL TABLES FOR READ and we want a write lock
Wait until the lock is gone
*/
if (wait_if_global_read_lock(thd, 1, 1))
2000-07-31 21:29:14 +02:00
{
/* Clear the lock type of all lock data to avoid reusage. */
reset_lock_data(sql_lock);
2000-07-31 21:29:14 +02:00
my_free((gptr) sql_lock,MYF(0));
sql_lock=0;
break;
}
if (thd->version != refresh_version)
2000-07-31 21:29:14 +02:00
{
/* Clear the lock type of all lock data to avoid reusage. */
reset_lock_data(sql_lock);
2000-07-31 21:29:14 +02:00
my_free((gptr) sql_lock,MYF(0));
goto retry;
}
}
Prevent bugs by making DBUG_* expressions syntactically equivalent to a single statement. --- Bug#24795: SHOW PROFILE Profiling is only partially functional on some architectures. Where there is no getrusage() system call, presently Null values are returned where it would be required. Notably, Windows needs some love applied to make it as useful. Syntax this adds: SHOW PROFILES SHOW PROFILE [types] [FOR QUERY n] [OFFSET n] [LIMIT n] where "n" is an integer and "types" is zero or many (comma-separated) of "CPU" "MEMORY" (not presently supported) "BLOCK IO" "CONTEXT SWITCHES" "PAGE FAULTS" "IPC" "SWAPS" "SOURCE" "ALL" It also adds a session variable (boolean) "profiling", set to "no" by default, and (integer) profiling_history_size, set to 15 by default. This patch abstracts setting THDs' "proc_info" behind a macro that can be used as a hook into the profiling code when profiling support is compiled in. All future code in this line should use that mechanism for setting thd->proc_info. --- Tests are now set to omit the statistics. --- Adds an Information_schema table, "profiling" for access to "show profile" data. --- Merge zippy.cornsilk.net:/home/cmiller/work/mysql/mysql-5.0-community-3--bug24795 into zippy.cornsilk.net:/home/cmiller/work/mysql/mysql-5.0-community --- Fix merge problems. --- Fixed one bug in the query_source being NULL. Updated test results. --- Include more thorough profiling tests. Improve support for prepared statements. Use session-specific query IDs, starting at zero. --- Selecting from I_S.profiling is no longer quashed in profiling, as requested by Giuseppe. Limit the size of captured query text. No longer log queries that are zero length.
2007-02-22 16:03:08 +01:00
thd_proc_info(thd, "System lock");
2007-07-27 14:37:29 +02:00
if (sql_lock->table_count && lock_external(thd, sql_lock->table,
sql_lock->table_count))
2000-07-31 21:29:14 +02:00
{
/* Clear the lock type of all lock data to avoid reusage. */
reset_lock_data(sql_lock);
2000-07-31 21:29:14 +02:00
my_free((gptr) sql_lock,MYF(0));
sql_lock=0;
break;
}
Prevent bugs by making DBUG_* expressions syntactically equivalent to a single statement. --- Bug#24795: SHOW PROFILE Profiling is only partially functional on some architectures. Where there is no getrusage() system call, presently Null values are returned where it would be required. Notably, Windows needs some love applied to make it as useful. Syntax this adds: SHOW PROFILES SHOW PROFILE [types] [FOR QUERY n] [OFFSET n] [LIMIT n] where "n" is an integer and "types" is zero or many (comma-separated) of "CPU" "MEMORY" (not presently supported) "BLOCK IO" "CONTEXT SWITCHES" "PAGE FAULTS" "IPC" "SWAPS" "SOURCE" "ALL" It also adds a session variable (boolean) "profiling", set to "no" by default, and (integer) profiling_history_size, set to 15 by default. This patch abstracts setting THDs' "proc_info" behind a macro that can be used as a hook into the profiling code when profiling support is compiled in. All future code in this line should use that mechanism for setting thd->proc_info. --- Tests are now set to omit the statistics. --- Adds an Information_schema table, "profiling" for access to "show profile" data. --- Merge zippy.cornsilk.net:/home/cmiller/work/mysql/mysql-5.0-community-3--bug24795 into zippy.cornsilk.net:/home/cmiller/work/mysql/mysql-5.0-community --- Fix merge problems. --- Fixed one bug in the query_source being NULL. Updated test results. --- Include more thorough profiling tests. Improve support for prepared statements. Use session-specific query IDs, starting at zero. --- Selecting from I_S.profiling is no longer quashed in profiling, as requested by Giuseppe. Limit the size of captured query text. No longer log queries that are zero length.
2007-02-22 16:03:08 +01:00
thd_proc_info(thd, "Table lock");
2000-07-31 21:29:14 +02:00
thd->locked=1;
/* Copy the lock data array. thr_multi_lock() reorders its contens. */
memcpy(sql_lock->locks + sql_lock->lock_count, sql_lock->locks,
sql_lock->lock_count * sizeof(*sql_lock->locks));
/* Lock on the copied half of the lock data array. */
rc= thr_lock_errno_to_mysql[(int) thr_multi_lock(sql_lock->locks +
sql_lock->lock_count,
sql_lock->lock_count,
thd->lock_id)];
if (rc > 1) /* a timeout or a deadlock */
{
if (sql_lock->table_count)
VOID(unlock_external(thd, sql_lock->table, sql_lock->table_count));
my_error(rc, MYF(0));
my_free((gptr) sql_lock,MYF(0));
sql_lock= 0;
break;
}
else if (rc == 1) /* aborted */
2000-07-31 21:29:14 +02:00
{
/*
reset_lock_data is required here. If thr_multi_lock fails it
resets lock type for tables, which were locked before (and
including) one that caused error. Lock type for other tables
preserved.
*/
reset_lock_data(sql_lock);
2000-07-31 21:29:14 +02:00
thd->some_tables_deleted=1; // Try again
sql_lock->lock_count= 0; // Locks are already freed
2000-07-31 21:29:14 +02:00
}
else if (!thd->some_tables_deleted || (flags & MYSQL_LOCK_IGNORE_FLUSH))
2000-07-31 21:29:14 +02:00
{
thd->locked=0;
break;
}
else if (!thd->open_tables)
{
// Only using temporary tables, no need to unlock
thd->some_tables_deleted=0;
thd->locked=0;
break;
}
Prevent bugs by making DBUG_* expressions syntactically equivalent to a single statement. --- Bug#24795: SHOW PROFILE Profiling is only partially functional on some architectures. Where there is no getrusage() system call, presently Null values are returned where it would be required. Notably, Windows needs some love applied to make it as useful. Syntax this adds: SHOW PROFILES SHOW PROFILE [types] [FOR QUERY n] [OFFSET n] [LIMIT n] where "n" is an integer and "types" is zero or many (comma-separated) of "CPU" "MEMORY" (not presently supported) "BLOCK IO" "CONTEXT SWITCHES" "PAGE FAULTS" "IPC" "SWAPS" "SOURCE" "ALL" It also adds a session variable (boolean) "profiling", set to "no" by default, and (integer) profiling_history_size, set to 15 by default. This patch abstracts setting THDs' "proc_info" behind a macro that can be used as a hook into the profiling code when profiling support is compiled in. All future code in this line should use that mechanism for setting thd->proc_info. --- Tests are now set to omit the statistics. --- Adds an Information_schema table, "profiling" for access to "show profile" data. --- Merge zippy.cornsilk.net:/home/cmiller/work/mysql/mysql-5.0-community-3--bug24795 into zippy.cornsilk.net:/home/cmiller/work/mysql/mysql-5.0-community --- Fix merge problems. --- Fixed one bug in the query_source being NULL. Updated test results. --- Include more thorough profiling tests. Improve support for prepared statements. Use session-specific query IDs, starting at zero. --- Selecting from I_S.profiling is no longer quashed in profiling, as requested by Giuseppe. Limit the size of captured query text. No longer log queries that are zero length.
2007-02-22 16:03:08 +01:00
thd_proc_info(thd, 0);
2000-07-31 21:29:14 +02:00
/* some table was altered or deleted. reopen tables marked deleted */
mysql_unlock_tables(thd,sql_lock);
thd->locked=0;
retry:
sql_lock=0;
if (flags & MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN)
{
*need_reopen= TRUE;
break;
}
2000-07-31 21:29:14 +02:00
if (wait_for_tables(thd))
break; // Couldn't open tables
}
Prevent bugs by making DBUG_* expressions syntactically equivalent to a single statement. --- Bug#24795: SHOW PROFILE Profiling is only partially functional on some architectures. Where there is no getrusage() system call, presently Null values are returned where it would be required. Notably, Windows needs some love applied to make it as useful. Syntax this adds: SHOW PROFILES SHOW PROFILE [types] [FOR QUERY n] [OFFSET n] [LIMIT n] where "n" is an integer and "types" is zero or many (comma-separated) of "CPU" "MEMORY" (not presently supported) "BLOCK IO" "CONTEXT SWITCHES" "PAGE FAULTS" "IPC" "SWAPS" "SOURCE" "ALL" It also adds a session variable (boolean) "profiling", set to "no" by default, and (integer) profiling_history_size, set to 15 by default. This patch abstracts setting THDs' "proc_info" behind a macro that can be used as a hook into the profiling code when profiling support is compiled in. All future code in this line should use that mechanism for setting thd->proc_info. --- Tests are now set to omit the statistics. --- Adds an Information_schema table, "profiling" for access to "show profile" data. --- Merge zippy.cornsilk.net:/home/cmiller/work/mysql/mysql-5.0-community-3--bug24795 into zippy.cornsilk.net:/home/cmiller/work/mysql/mysql-5.0-community --- Fix merge problems. --- Fixed one bug in the query_source being NULL. Updated test results. --- Include more thorough profiling tests. Improve support for prepared statements. Use session-specific query IDs, starting at zero. --- Selecting from I_S.profiling is no longer quashed in profiling, as requested by Giuseppe. Limit the size of captured query text. No longer log queries that are zero length.
2007-02-22 16:03:08 +01:00
thd_proc_info(thd, 0);
2000-07-31 21:29:14 +02:00
if (thd->killed)
{
thd->send_kill_message();
2000-07-31 21:29:14 +02:00
if (sql_lock)
{
mysql_unlock_tables(thd,sql_lock);
sql_lock=0;
}
}
thd->lock_time();
2000-07-31 21:29:14 +02:00
DBUG_RETURN (sql_lock);
}
static int lock_external(THD *thd, TABLE **tables, uint count)
2000-07-31 21:29:14 +02:00
{
reg1 uint i;
int lock_type,error;
DBUG_ENTER("lock_external");
for (i=1 ; i <= count ; i++, tables++)
{
DBUG_ASSERT((*tables)->reginfo.lock_type >= TL_READ);
2000-07-31 21:29:14 +02:00
lock_type=F_WRLCK; /* Lock exclusive */
if ((*tables)->db_stat & HA_READ_ONLY ||
((*tables)->reginfo.lock_type >= TL_READ &&
(*tables)->reginfo.lock_type <= TL_READ_NO_INSERT))
lock_type=F_RDLCK;
2007-07-27 14:37:29 +02:00
if ((error= (*tables)->file->ha_external_lock(thd,lock_type)))
2000-07-31 21:29:14 +02:00
{
print_lock_error(error, (*tables)->file->table_type());
for (; i-- ; tables--)
2000-07-31 21:29:14 +02:00
{
2007-07-27 14:37:29 +02:00
(*tables)->file->ha_external_lock(thd, F_UNLCK);
2000-07-31 21:29:14 +02:00
(*tables)->current_lock=F_UNLCK;
}
DBUG_RETURN(error);
}
else
{
(*tables)->db_stat &= ~ HA_BLOCK_LOCK;
(*tables)->current_lock= lock_type;
}
}
DBUG_RETURN(0);
}
void mysql_unlock_tables(THD *thd, MYSQL_LOCK *sql_lock)
{
DBUG_ENTER("mysql_unlock_tables");
if (sql_lock->lock_count)
thr_multi_unlock(sql_lock->locks,sql_lock->lock_count);
if (sql_lock->table_count)
VOID(unlock_external(thd,sql_lock->table,sql_lock->table_count));
2000-07-31 21:29:14 +02:00
my_free((gptr) sql_lock,MYF(0));
DBUG_VOID_RETURN;
}
/*
Unlock some of the tables locked by mysql_lock_tables
This will work even if get_lock_data fails (next unlock will free all)
*/
void mysql_unlock_some_tables(THD *thd, TABLE **table,uint count)
{
MYSQL_LOCK *sql_lock;
TABLE *write_lock_used;
if ((sql_lock= get_lock_data(thd, table, count, GET_LOCK_UNLOCK,
&write_lock_used)))
2000-07-31 21:29:14 +02:00
mysql_unlock_tables(thd, sql_lock);
}
/*
** unlock all tables locked for read.
*/
void mysql_unlock_read_tables(THD *thd, MYSQL_LOCK *sql_lock)
{
uint i,found;
DBUG_ENTER("mysql_unlock_read_tables");
/* Move all write locks first */
THR_LOCK_DATA **lock=sql_lock->locks;
for (i=found=0 ; i < sql_lock->lock_count ; i++)
{
if (sql_lock->locks[i]->type >= TL_WRITE_ALLOW_READ)
{
swap_variables(THR_LOCK_DATA *, *lock, sql_lock->locks[i]);
2000-07-31 21:29:14 +02:00
lock++;
found++;
}
}
/* unlock the read locked tables */
if (i != found)
{
thr_multi_unlock(lock,i-found);
sql_lock->lock_count= found;
2000-07-31 21:29:14 +02:00
}
/* Then do the same for the external locks */
2000-07-31 21:29:14 +02:00
/* Move all write locked tables first */
TABLE **table=sql_lock->table;
for (i=found=0 ; i < sql_lock->table_count ; i++)
{
DBUG_ASSERT(sql_lock->table[i]->lock_position == i);
2000-07-31 21:29:14 +02:00
if ((uint) sql_lock->table[i]->reginfo.lock_type >= TL_WRITE_ALLOW_READ)
{
swap_variables(TABLE *, *table, sql_lock->table[i]);
2000-07-31 21:29:14 +02:00
table++;
found++;
}
}
/* Unlock all read locked tables */
if (i != found)
{
VOID(unlock_external(thd,table,i-found));
sql_lock->table_count=found;
2000-07-31 21:29:14 +02:00
}
/* Fix the lock positions in TABLE */
table= sql_lock->table;
found= 0;
for (i= 0; i < sql_lock->table_count; i++)
{
TABLE *tbl= *table;
tbl->lock_position= (uint) (table - sql_lock->table);
tbl->lock_data_start= found;
found+= tbl->lock_count;
table++;
}
2000-07-31 21:29:14 +02:00
DBUG_VOID_RETURN;
}
2007-07-27 14:37:29 +02:00
/**
Try to find the table in the list of locked tables.
In case of success, unlock the table and remove it from this list.
2000-07-31 21:29:14 +02:00
2007-07-27 14:37:29 +02:00
@note This function has a legacy side effect: the table is
unlocked even if it is not found in the locked list.
It's not clear if this side effect is intentional or still
desirable. It might lead to unmatched calls to
unlock_external(). Moreover, a discrepancy can be left
unnoticed by the storage engine, because in
unlock_external() we call handler::external_lock(F_UNLCK) only
if table->current_lock is not F_UNLCK.
@param always_unlock specify explicitly if the legacy side
effect is desired.
*/
void mysql_lock_remove(THD *thd, MYSQL_LOCK *locked,TABLE *table,
bool always_unlock)
2000-07-31 21:29:14 +02:00
{
2007-07-27 14:37:29 +02:00
if (always_unlock == TRUE)
mysql_unlock_some_tables(thd, &table, /* table count */ 1);
2000-07-31 21:29:14 +02:00
if (locked)
{
reg1 uint i;
for (i=0; i < locked->table_count; i++)
{
if (locked->table[i] == table)
{
uint j, removed_locks, old_tables;
TABLE *tbl;
uint lock_data_end;
DBUG_ASSERT(table->lock_position == i);
2007-07-27 14:37:29 +02:00
/* Unlock if not yet unlocked */
if (always_unlock == FALSE)
mysql_unlock_some_tables(thd, &table, /* table count */ 1);
/* Decrement table_count in advance, making below expressions easier */
old_tables= --locked->table_count;
/* The table has 'removed_locks' lock data elements in locked->locks */
removed_locks= table->lock_count;
/* Move down all table pointers above 'i'. */
2000-07-31 21:29:14 +02:00
bmove((char*) (locked->table+i),
(char*) (locked->table+i+1),
(old_tables - i) * sizeof(TABLE*));
lock_data_end= table->lock_data_start + table->lock_count;
/* Move down all lock data pointers above 'table->lock_data_end-1' */
bmove((char*) (locked->locks + table->lock_data_start),
(char*) (locked->locks + lock_data_end),
(locked->lock_count - lock_data_end) *
sizeof(THR_LOCK_DATA*));
/*
Fix moved table elements.
lock_position is the index in the 'locked->table' array,
it must be fixed by one.
table->lock_data_start is pointer to the lock data for this table
in the 'locked->locks' array, they must be fixed by 'removed_locks',
the lock data count of the removed table.
*/
for (j= i ; j < old_tables; j++)
{
tbl= locked->table[j];
tbl->lock_position--;
DBUG_ASSERT(tbl->lock_position == j);
tbl->lock_data_start-= removed_locks;
}
/* Finally adjust lock_count. */
locked->lock_count-= removed_locks;
2000-07-31 21:29:14 +02:00
break;
}
}
}
}
/* abort all other threads waiting to get lock in table */
void mysql_lock_abort(THD *thd, TABLE *table)
{
MYSQL_LOCK *locked;
TABLE *write_lock_used;
if ((locked= get_lock_data(thd, &table, 1, GET_LOCK_UNLOCK,
&write_lock_used)))
2000-07-31 21:29:14 +02:00
{
for (uint i=0; i < locked->lock_count; i++)
thr_abort_locks(locked->locks[i]->lock);
my_free((gptr) locked,MYF(0));
}
}
/*
Abort one thread / table combination
SYNOPSIS
mysql_lock_abort_for_thread()
thd Thread handler
table Table that should be removed from lock queue
RETURN
0 Table was not locked by another thread
1 Table was locked by at least one other thread
*/
bool mysql_lock_abort_for_thread(THD *thd, TABLE *table)
{
MYSQL_LOCK *locked;
TABLE *write_lock_used;
bool result= FALSE;
DBUG_ENTER("mysql_lock_abort_for_thread");
if ((locked= get_lock_data(thd, &table, 1, GET_LOCK_UNLOCK,
&write_lock_used)))
{
for (uint i=0; i < locked->lock_count; i++)
{
if (thr_abort_locks_for_thread(locked->locks[i]->lock,
table->in_use->real_id))
result= TRUE;
}
my_free((gptr) locked,MYF(0));
}
DBUG_RETURN(result);
}
2000-07-31 21:29:14 +02:00
MYSQL_LOCK *mysql_lock_merge(MYSQL_LOCK *a,MYSQL_LOCK *b)
{
MYSQL_LOCK *sql_lock;
TABLE **table, **end_table;
2000-07-31 21:29:14 +02:00
DBUG_ENTER("mysql_lock_merge");
2000-07-31 21:29:14 +02:00
if (!(sql_lock= (MYSQL_LOCK*)
my_malloc(sizeof(*sql_lock)+
sizeof(THR_LOCK_DATA*)*(a->lock_count+b->lock_count)+
sizeof(TABLE*)*(a->table_count+b->table_count),MYF(MY_WME))))
DBUG_RETURN(0); // Fatal error
sql_lock->lock_count=a->lock_count+b->lock_count;
sql_lock->table_count=a->table_count+b->table_count;
sql_lock->locks=(THR_LOCK_DATA**) (sql_lock+1);
sql_lock->table=(TABLE**) (sql_lock->locks+sql_lock->lock_count);
memcpy(sql_lock->locks,a->locks,a->lock_count*sizeof(*a->locks));
memcpy(sql_lock->locks+a->lock_count,b->locks,
b->lock_count*sizeof(*b->locks));
memcpy(sql_lock->table,a->table,a->table_count*sizeof(*a->table));
memcpy(sql_lock->table+a->table_count,b->table,
b->table_count*sizeof(*b->table));
/*
Now adjust lock_position and lock_data_start for all objects that was
moved in 'b' (as there is now all objects in 'a' before these).
*/
for (table= sql_lock->table + a->table_count,
end_table= table + b->table_count;
table < end_table;
table++)
{
(*table)->lock_position+= a->table_count;
(*table)->lock_data_start+= a->lock_count;
}
/* Delete old, not needed locks */
2000-07-31 21:29:14 +02:00
my_free((gptr) a,MYF(0));
my_free((gptr) b,MYF(0));
DBUG_RETURN(sql_lock);
}
/*
Find duplicate lock in tables.
SYNOPSIS
mysql_lock_have_duplicate()
thd The current thread.
needle The table to check for duplicate lock.
haystack The list of tables to search for the dup lock.
NOTE
This is mainly meant for MERGE tables in INSERT ... SELECT
situations. The 'real', underlying tables can be found only after
the MERGE tables are opened. This function assumes that the tables are
already locked.
Temporary tables are ignored here like they are ignored in
get_lock_data(). If we allow two opens on temporary tables later,
both functions should be checked.
RETURN
NULL No duplicate lock found.
! NULL First table from 'haystack' that matches a lock on 'needle'.
*/
TABLE_LIST *mysql_lock_have_duplicate(THD *thd, TABLE_LIST *needle,
TABLE_LIST *haystack)
{
MYSQL_LOCK *mylock;
TABLE **lock_tables;
TABLE *table;
TABLE *table2;
THR_LOCK_DATA **lock_locks;
THR_LOCK_DATA **table_lock_data;
THR_LOCK_DATA **end_data;
THR_LOCK_DATA **lock_data2;
THR_LOCK_DATA **end_data2;
DBUG_ENTER("mysql_lock_have_duplicate");
/*
Table may not be defined for derived or view tables.
Table may not be part of a lock for delayed operations.
*/
if (! (table= needle->table) || ! table->lock_count)
goto end;
/* A temporary table does not have locks. */
if (table->s->tmp_table == NON_TRANSACTIONAL_TMP_TABLE)
goto end;
/* Get command lock or LOCK TABLES lock. Maybe empty for INSERT DELAYED. */
if (! (mylock= thd->lock ? thd->lock : thd->locked_tables))
goto end;
/* If we have less than two tables, we cannot have duplicates. */
if (mylock->table_count < 2)
goto end;
lock_locks= mylock->locks;
lock_tables= mylock->table;
/* Prepare table related variables that don't change in loop. */
DBUG_ASSERT((table->lock_position < mylock->table_count) &&
(table == lock_tables[table->lock_position]));
table_lock_data= lock_locks + table->lock_data_start;
end_data= table_lock_data + table->lock_count;
for (; haystack; haystack= haystack->next_global)
{
Bug#8407 (Stored functions/triggers ignore exception handler) Bug 18914 (Calling certain SPs from triggers fail) Bug 20713 (Functions will not not continue for SQLSTATE VALUE '42S02') Bug 21825 (Incorrect message error deleting records in a table with a trigger for inserting) Bug 22580 (DROP TABLE in nested stored procedure causes strange dependency error) Bug 25345 (Cursors from Functions) This fix resolves a long standing issue originally reported with bug 8407, which affect the behavior of Stored Procedures, Stored Functions and Trigger in many different ways, causing symptoms reported by all the bugs listed. In all cases, the root cause of the problem traces back to 8407 and how the server locks tables involved with sub statements. Prior to this fix, the implementation of stored routines would: - compute the transitive closure of all the tables referenced by a top level statement - open and lock all the tables involved - execute the top level statement "transitive closure of tables" means collecting: - all the tables, - all the stored functions, - all the views, - all the table triggers - all the stored procedures involved, and recursively inspect these objects definition to find more references to more objects, until the list of every object referenced does not grow any more. This mechanism is known as "pre-locking" tables before execution. The motivation for locking all the tables (possibly) used at once is to prevent dead locks. One problem with this approach is that, if the execution path the code really takes during runtime does not use a given table, and if the table is missing, the server would not execute the statement. This in particular has a major impact on triggers, since a missing table referenced by an update/delete trigger would prevent an insert trigger to run. Another problem is that stored routines might define SQL exception handlers to deal with missing tables, but the server implementation would never give user code a chance to execute this logic, since the routine is never executed when a missing table cause the pre-locking code to fail. With this fix, the internal implementation of the pre-locking code has been relaxed of some constraints, so that failure to open a table does not necessarily prevent execution of a stored routine. In particular, the pre-locking mechanism is now behaving as follows: 1) the first step, to compute the transitive closure of all the tables possibly referenced by a statement, is unchanged. 2) the next step, which is to open all the tables involved, only attempts to open the tables added by the pre-locking code, but silently fails without reporting any error or invoking any exception handler is the table is not present. This is achieved by trapping internal errors with Prelock_error_handler 3) the locking step only locks tables that were successfully opened. 4) when executing sub statements, the list of tables used by each statements is evaluated as before. The tables needed by the sub statement are expected to be already opened and locked. Statement referencing tables that were not opened in step 2) will fail to find the table in the open list, and only at this point will execution of the user code fail. 5) when a runtime exception is raised at 4), the instruction continuation destination (the next instruction to execute in case of SQL continue handlers) is evaluated. This is achieved with sp_instr::exec_open_and_lock_tables() 6) if a user exception handler is present in the stored routine, that handler is invoked as usual, so that ER_NO_SUCH_TABLE exceptions can be trapped by stored routines. If no handler exists, then the runtime execution will fail as expected. With all these changes, a side effect is that view security is impacted, in two different ways. First, a view defined as "select stored_function()", where the stored function references a table that may not exist, is considered valid. The rationale is that, because the stored function might trap exceptions during execution and still return a valid result, there is no way to decide when the view is created if a missing table really cause the view to be invalid. Secondly, testing for existence of tables is now done later during execution. View security, which consist of trapping errors and return a generic ER_VIEW_INVALID (to prevent disclosing information) was only implemented at very specific phases covering *opening* tables, but not covering the runtime execution. Because of this existing limitation, errors that were previously trapped and converted into ER_VIEW_INVALID are not trapped, causing table names to be reported to the user. This change is exposing an existing problem, which is independent and will be resolved separately.
2007-03-06 03:42:07 +01:00
if (haystack->placeholder())
continue;
table2= haystack->table;
if (table2->s->tmp_table == NON_TRANSACTIONAL_TMP_TABLE)
continue;
/* All tables in list must be in lock. */
DBUG_ASSERT((table2->lock_position < mylock->table_count) &&
(table2 == lock_tables[table2->lock_position]));
for (lock_data2= lock_locks + table2->lock_data_start,
end_data2= lock_data2 + table2->lock_count;
lock_data2 < end_data2;
lock_data2++)
{
THR_LOCK_DATA **lock_data;
THR_LOCK *lock2= (*lock_data2)->lock;
for (lock_data= table_lock_data;
lock_data < end_data;
lock_data++)
{
if ((*lock_data)->lock == lock2)
{
DBUG_PRINT("info", ("haystack match: '%s'", haystack->table_name));
DBUG_RETURN(haystack);
}
}
}
}
end:
DBUG_PRINT("info", ("no duplicate found"));
DBUG_RETURN(NULL);
}
2000-07-31 21:29:14 +02:00
/* unlock a set of external */
static int unlock_external(THD *thd, TABLE **table,uint count)
{
int error,error_code;
DBUG_ENTER("unlock_external");
error_code=0;
do
2000-07-31 21:29:14 +02:00
{
if ((*table)->current_lock != F_UNLCK)
{
(*table)->current_lock = F_UNLCK;
2007-07-27 14:37:29 +02:00
if ((error= (*table)->file->ha_external_lock(thd, F_UNLCK)))
{
2000-07-31 21:29:14 +02:00
error_code=error;
print_lock_error(error_code, (*table)->file->table_type());
}
2000-07-31 21:29:14 +02:00
}
table++;
} while (--count);
2000-07-31 21:29:14 +02:00
DBUG_RETURN(error_code);
}
/*
Get lock structures from table structs and initialize locks
SYNOPSIS
get_lock_data()
thd Thread handler
table_ptr Pointer to tables that should be locks
flags One of:
GET_LOCK_UNLOCK: If we should send TL_IGNORE to
store lock
GET_LOCK_STORE_LOCKS: Store lock info in TABLE
write_lock_used Store pointer to last table with WRITE_ALLOW_WRITE
2000-07-31 21:29:14 +02:00
*/
static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count,
uint flags, TABLE **write_lock_used)
2000-07-31 21:29:14 +02:00
{
uint i,tables,lock_count;
MYSQL_LOCK *sql_lock;
THR_LOCK_DATA **locks, **locks_buf, **locks_start;
TABLE **to, **table_buf;
DBUG_ENTER("get_lock_data");
2000-07-31 21:29:14 +02:00
*write_lock_used=0;
for (i=tables=lock_count=0 ; i < count ; i++)
{
if (table_ptr[i]->s->tmp_table != NON_TRANSACTIONAL_TMP_TABLE)
2000-07-31 21:29:14 +02:00
{
tables+=table_ptr[i]->file->lock_count();
lock_count++;
}
/*
To be able to open and lock for reading system tables like 'mysql.proc',
when we already have some tables opened and locked, and avoid deadlocks
we have to disallow write-locking of these tables with any other tables.
*/
if (table_ptr[i]->s->system_table &&
table_ptr[i]->reginfo.lock_type >= TL_WRITE_ALLOW_WRITE &&
count != 1)
{
my_error(ER_WRONG_LOCK_OF_SYSTEM_TABLE, MYF(0), table_ptr[i]->s->db,
table_ptr[i]->s->table_name);
2008-03-29 16:50:46 +01:00
DBUG_RETURN(0);
}
2000-07-31 21:29:14 +02:00
}
/*
Allocating twice the number of pointers for lock data for use in
thr_mulit_lock(). This function reorders the lock data, but cannot
update the table values. So the second part of the array is copied
from the first part immediately before calling thr_multi_lock().
*/
2000-07-31 21:29:14 +02:00
if (!(sql_lock= (MYSQL_LOCK*)
my_malloc(sizeof(*sql_lock) +
sizeof(THR_LOCK_DATA*) * tables * 2 +
sizeof(table_ptr) * lock_count,
2000-07-31 21:29:14 +02:00
MYF(0))))
DBUG_RETURN(0);
locks= locks_buf= sql_lock->locks= (THR_LOCK_DATA**) (sql_lock + 1);
to= table_buf= sql_lock->table= (TABLE**) (locks + tables * 2);
2000-07-31 21:29:14 +02:00
sql_lock->table_count=lock_count;
sql_lock->lock_count=tables;
for (i=0 ; i < count ; i++)
{
TABLE *table;
enum thr_lock_type lock_type;
if ((table=table_ptr[i])->s->tmp_table == NON_TRANSACTIONAL_TMP_TABLE)
2000-07-31 21:29:14 +02:00
continue;
lock_type= table->reginfo.lock_type;
DBUG_ASSERT (lock_type != TL_WRITE_DEFAULT);
2000-07-31 21:29:14 +02:00
if (lock_type >= TL_WRITE_ALLOW_WRITE)
{
*write_lock_used=table;
if (table->db_stat & HA_READ_ONLY)
{
my_error(ER_OPEN_AS_READONLY,MYF(0),table->alias);
/* Clear the lock type of the lock data that are stored already. */
sql_lock->lock_count= (uint) (locks - sql_lock->locks);
reset_lock_data(sql_lock);
2000-07-31 21:29:14 +02:00
my_free((gptr) sql_lock,MYF(0));
DBUG_RETURN(0);
2000-07-31 21:29:14 +02:00
}
}
THR_LOCK_DATA **org_locks = locks;
locks_start= locks;
locks= table->file->store_lock(thd, locks,
(flags & GET_LOCK_UNLOCK) ? TL_IGNORE :
lock_type);
if (flags & GET_LOCK_STORE_LOCKS)
{
table->lock_position= (uint) (to - table_buf);
table->lock_data_start= (uint) (locks_start - locks_buf);
table->lock_count= (uint) (locks - locks_start);
}
*to++= table;
if (locks)
for ( ; org_locks != locks ; org_locks++)
(*org_locks)->debug_print_param= (void *) table;
2000-07-31 21:29:14 +02:00
}
DBUG_RETURN(sql_lock);
2000-07-31 21:29:14 +02:00
}
/*
Reset lock type in lock data.
SYNOPSIS
reset_lock_data()
sql_lock The MySQL lock.
DESCRIPTION
After a locking error we want to quit the locking of the table(s).
The test case in the bug report for Bug #18544 has the following
cases: 1. Locking error in lock_external() due to InnoDB timeout.
2. Locking error in get_lock_data() due to missing write permission.
3. Locking error in wait_if_global_read_lock() due to lock conflict.
In all these cases we have already set the lock type into the lock
data of the open table(s). If the table(s) are in the open table
cache, they could be reused with the non-zero lock type set. This
could lead to ignoring a different lock type with the next lock.
Clear the lock type of all lock data. This ensures that the next
lock request will set its lock type properly.
RETURN
void
*/
static void reset_lock_data(MYSQL_LOCK *sql_lock)
{
THR_LOCK_DATA **ldata;
THR_LOCK_DATA **ldata_end;
for (ldata= sql_lock->locks, ldata_end= ldata + sql_lock->lock_count;
ldata < ldata_end;
ldata++)
{
/* Reset lock type. */
(*ldata)->type= TL_UNLOCK;
}
}
/*****************************************************************************
Lock table based on the name.
This is used when we need total access to a closed, not open table
*****************************************************************************/
/*
Lock and wait for the named lock.
SYNOPSIS
lock_and_wait_for_table_name()
thd Thread handler
table_list Lock first table in this list
NOTES
Works together with global read lock.
RETURN
0 ok
1 error
*/
int lock_and_wait_for_table_name(THD *thd, TABLE_LIST *table_list)
{
int lock_retcode;
int error= -1;
DBUG_ENTER("lock_and_wait_for_table_name");
if (wait_if_global_read_lock(thd, 0, 1))
DBUG_RETURN(1);
VOID(pthread_mutex_lock(&LOCK_open));
if ((lock_retcode = lock_table_name(thd, table_list)) < 0)
goto end;
if (lock_retcode && wait_for_locked_table_names(thd, table_list))
{
unlock_table_name(thd, table_list);
goto end;
}
error=0;
end:
pthread_mutex_unlock(&LOCK_open);
start_waiting_global_read_lock(thd);
DBUG_RETURN(error);
}
/*
Put a not open table with an old refresh version in the table cache.
SYNPOSIS
lock_table_name()
thd Thread handler
table_list Lock first table in this list
WARNING
If you are going to update the table, you should use
lock_and_wait_for_table_name instead of this function as this works
together with 'FLUSH TABLES WITH READ LOCK'
NOTES
This will force any other threads that uses the table to release it
as soon as possible.
REQUIREMENTS
One must have a lock on LOCK_open !
RETURN:
< 0 error
== 0 table locked
> 0 table locked, but someone is using it
*/
int lock_table_name(THD *thd, TABLE_LIST *table_list)
{
TABLE *table;
char key[MAX_DBKEY_LENGTH];
2004-03-15 11:53:27 +01:00
char *db= table_list->db;
uint key_length;
HASH_SEARCH_STATE state;
2000-08-29 11:31:01 +02:00
DBUG_ENTER("lock_table_name");
DBUG_PRINT("enter",("db: %s name: %s", db, table_list->table_name));
safe_mutex_assert_owner(&LOCK_open);
2000-08-29 11:31:01 +02:00
Fix for: Bug #20662 "Infinite loop in CREATE TABLE IF NOT EXISTS ... SELECT with locked tables" Bug #20903 "Crash when using CREATE TABLE .. SELECT and triggers" Bug #24738 "CREATE TABLE ... SELECT is not isolated properly" Bug #24508 "Inconsistent results of CREATE TABLE ... SELECT when temporary table exists" Deadlock occured when one tried to execute CREATE TABLE IF NOT EXISTS ... SELECT statement under LOCK TABLES which held read lock on target table. Attempt to execute the same statement for already existing target table with triggers caused server crashes. Also concurrent execution of CREATE TABLE ... SELECT statement and other statements involving target table suffered from various races (some of which might've led to deadlocks). Finally, attempt to execute CREATE TABLE ... SELECT in case when a temporary table with same name was already present led to the insertion of data into this temporary table and creation of empty non-temporary table. All above problems stemmed from the old implementation of CREATE TABLE ... SELECT in which we created, opened and locked target table without any special protection in a separate step and not with the rest of tables used by this statement. This underminded deadlock-avoidance approach used in server and created window for races. It also excluded target table from prelocking causing problems with trigger execution. The patch solves these problems by implementing new approach to handling of CREATE TABLE ... SELECT for base tables. We try to open and lock table to be created at the same time as the rest of tables used by this statement. If such table does not exist at this moment we create and place in the table cache special placeholder for it which prevents its creation or any other usage by other threads. We still use old approach for creation of temporary tables. Also note that we decided to postpone introduction of some tests for concurrent behaviour of CREATE TABLE ... SELECT till 5.1. The main reason for this is absence in 5.0 ability to set @@debug variable at runtime, which can be circumvented only by using several test files with individual .opt files. Since the latter is likely to slowdown test-suite unnecessary we chose not to push this tests into 5.0, but run them manually for this version and later push their optimized version into 5.1
2007-05-11 18:33:13 +02:00
key_length= (uint)(strmov(strmov(key, db) + 1, table_list->table_name) -
key) + 1;
/* Only insert the table if we haven't insert it already */
for (table=(TABLE*) hash_first(&open_cache, (byte*)key, key_length, &state);
table ;
table = (TABLE*) hash_next(&open_cache, (byte*)key, key_length, &state))
if (table->in_use == thd)
2000-08-29 11:31:01 +02:00
DBUG_RETURN(0);
Fix for: Bug #20662 "Infinite loop in CREATE TABLE IF NOT EXISTS ... SELECT with locked tables" Bug #20903 "Crash when using CREATE TABLE .. SELECT and triggers" Bug #24738 "CREATE TABLE ... SELECT is not isolated properly" Bug #24508 "Inconsistent results of CREATE TABLE ... SELECT when temporary table exists" Deadlock occured when one tried to execute CREATE TABLE IF NOT EXISTS ... SELECT statement under LOCK TABLES which held read lock on target table. Attempt to execute the same statement for already existing target table with triggers caused server crashes. Also concurrent execution of CREATE TABLE ... SELECT statement and other statements involving target table suffered from various races (some of which might've led to deadlocks). Finally, attempt to execute CREATE TABLE ... SELECT in case when a temporary table with same name was already present led to the insertion of data into this temporary table and creation of empty non-temporary table. All above problems stemmed from the old implementation of CREATE TABLE ... SELECT in which we created, opened and locked target table without any special protection in a separate step and not with the rest of tables used by this statement. This underminded deadlock-avoidance approach used in server and created window for races. It also excluded target table from prelocking causing problems with trigger execution. The patch solves these problems by implementing new approach to handling of CREATE TABLE ... SELECT for base tables. We try to open and lock table to be created at the same time as the rest of tables used by this statement. If such table does not exist at this moment we create and place in the table cache special placeholder for it which prevents its creation or any other usage by other threads. We still use old approach for creation of temporary tables. Also note that we decided to postpone introduction of some tests for concurrent behaviour of CREATE TABLE ... SELECT till 5.1. The main reason for this is absence in 5.0 ability to set @@debug variable at runtime, which can be circumvented only by using several test files with individual .opt files. Since the latter is likely to slowdown test-suite unnecessary we chose not to push this tests into 5.0, but run them manually for this version and later push their optimized version into 5.1
2007-05-11 18:33:13 +02:00
if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
2000-08-29 11:31:01 +02:00
DBUG_RETURN(-1);
Fix for: Bug #20662 "Infinite loop in CREATE TABLE IF NOT EXISTS ... SELECT with locked tables" Bug #20903 "Crash when using CREATE TABLE .. SELECT and triggers" Bug #24738 "CREATE TABLE ... SELECT is not isolated properly" Bug #24508 "Inconsistent results of CREATE TABLE ... SELECT when temporary table exists" Deadlock occured when one tried to execute CREATE TABLE IF NOT EXISTS ... SELECT statement under LOCK TABLES which held read lock on target table. Attempt to execute the same statement for already existing target table with triggers caused server crashes. Also concurrent execution of CREATE TABLE ... SELECT statement and other statements involving target table suffered from various races (some of which might've led to deadlocks). Finally, attempt to execute CREATE TABLE ... SELECT in case when a temporary table with same name was already present led to the insertion of data into this temporary table and creation of empty non-temporary table. All above problems stemmed from the old implementation of CREATE TABLE ... SELECT in which we created, opened and locked target table without any special protection in a separate step and not with the rest of tables used by this statement. This underminded deadlock-avoidance approach used in server and created window for races. It also excluded target table from prelocking causing problems with trigger execution. The patch solves these problems by implementing new approach to handling of CREATE TABLE ... SELECT for base tables. We try to open and lock table to be created at the same time as the rest of tables used by this statement. If such table does not exist at this moment we create and place in the table cache special placeholder for it which prevents its creation or any other usage by other threads. We still use old approach for creation of temporary tables. Also note that we decided to postpone introduction of some tests for concurrent behaviour of CREATE TABLE ... SELECT till 5.1. The main reason for this is absence in 5.0 ability to set @@debug variable at runtime, which can be circumvented only by using several test files with individual .opt files. Since the latter is likely to slowdown test-suite unnecessary we chose not to push this tests into 5.0, but run them manually for this version and later push their optimized version into 5.1
2007-05-11 18:33:13 +02:00
table_list->table= table;
/* Return 1 if table is in use */
2005-07-28 20:39:24 +02:00
DBUG_RETURN(test(remove_table_from_cache(thd, db, table_list->table_name,
RTFC_NO_FLAG)));
}
void unlock_table_name(THD *thd, TABLE_LIST *table_list)
{
if (table_list->table)
{
hash_delete(&open_cache, (byte*) table_list->table);
broadcast_refresh();
}
}
static bool locked_named_table(THD *thd, TABLE_LIST *table_list)
{
2004-07-16 00:15:55 +02:00
for (; table_list ; table_list=table_list->next_local)
{
2000-08-29 11:31:01 +02:00
if (table_list->table && table_is_used(table_list->table,0))
return 1;
}
return 0; // All tables are locked
}
bool wait_for_locked_table_names(THD *thd, TABLE_LIST *table_list)
{
bool result=0;
2000-08-29 11:31:01 +02:00
DBUG_ENTER("wait_for_locked_table_names");
safe_mutex_assert_owner(&LOCK_open);
while (locked_named_table(thd,table_list))
{
if (thd->killed)
{
result=1;
break;
}
wait_for_refresh(thd);
pthread_mutex_lock(&LOCK_open);
}
2000-08-29 11:31:01 +02:00
DBUG_RETURN(result);
}
/*
Lock all tables in list with a name lock
SYNOPSIS
lock_table_names()
thd Thread handle
table_list Names of tables to lock
NOTES
If you are just locking one table, you should use
lock_and_wait_for_table_name().
REQUIREMENTS
One must have a lock on LOCK_open when calling this
RETURN
0 ok
1 Fatal error (end of memory ?)
*/
bool lock_table_names(THD *thd, TABLE_LIST *table_list)
{
bool got_all_locks=1;
TABLE_LIST *lock_table;
2004-07-16 00:15:55 +02:00
for (lock_table= table_list; lock_table; lock_table= lock_table->next_local)
{
int got_lock;
if ((got_lock=lock_table_name(thd,lock_table)) < 0)
goto end; // Fatal error
if (got_lock)
got_all_locks=0; // Someone is using table
}
/* If some table was in use, wait until we got the lock */
if (!got_all_locks && wait_for_locked_table_names(thd, table_list))
goto end;
return 0;
end:
unlock_table_names(thd, table_list, lock_table);
return 1;
}
/*
Unlock all tables in list with a name lock
SYNOPSIS
unlock_table_names()
thd Thread handle
table_list Names of tables to unlock
last_table Don't unlock any tables after this one.
(default 0, which will unlock all tables)
NOTES
One must have a lock on LOCK_open when calling this.
This function will broadcast refresh signals to inform other threads
that the name locks are removed.
RETURN
0 ok
1 Fatal error (end of memory ?)
*/
void unlock_table_names(THD *thd, TABLE_LIST *table_list,
TABLE_LIST *last_table)
{
2004-07-16 00:15:55 +02:00
for (TABLE_LIST *table= table_list;
table != last_table;
table= table->next_local)
unlock_table_name(thd,table);
broadcast_refresh();
}
static void print_lock_error(int error, const char *table)
{
int textno;
DBUG_ENTER("print_lock_error");
switch (error) {
case HA_ERR_LOCK_WAIT_TIMEOUT:
textno=ER_LOCK_WAIT_TIMEOUT;
break;
case HA_ERR_READ_ONLY_TRANSACTION:
textno=ER_READ_ONLY_TRANSACTION;
break;
case HA_ERR_LOCK_DEADLOCK:
textno=ER_LOCK_DEADLOCK;
break;
case HA_ERR_WRONG_COMMAND:
textno=ER_ILLEGAL_HA;
break;
default:
textno=ER_CANT_LOCK;
break;
}
if ( textno == ER_ILLEGAL_HA )
my_error(textno, MYF(ME_BELL+ME_OLDWIN+ME_WAITTANG), table);
else
my_error(textno, MYF(ME_BELL+ME_OLDWIN+ME_WAITTANG), error);
DBUG_VOID_RETURN;
}
/****************************************************************************
Handling of global read locks
Taking the global read lock is TWO steps (2nd step is optional; without
it, COMMIT of existing transactions will be allowed):
lock_global_read_lock() THEN make_global_read_lock_block_commit().
The global locks are handled through the global variables:
global_read_lock
count of threads which have the global read lock (i.e. have completed at
least the first step above)
global_read_lock_blocks_commit
count of threads which have the global read lock and block
commits (i.e. are in or have completed the second step above)
waiting_for_read_lock
count of threads which want to take a global read lock but cannot
protect_against_global_read_lock
count of threads which have set protection against global read lock.
2005-04-04 00:50:05 +02:00
access to them is protected with a mutex LOCK_global_read_lock
(XXX: one should never take LOCK_open if LOCK_global_read_lock is
taken, otherwise a deadlock may occur. Other mutexes could be a
problem too - grep the code for global_read_lock if you want to use
any other mutex here) Also one must not hold LOCK_open when calling
wait_if_global_read_lock(). When the thread with the global read lock
tries to close its tables, it needs to take LOCK_open in
close_thread_table().
2005-04-04 00:50:05 +02:00
How blocking of threads by global read lock is achieved: that's
advisory. Any piece of code which should be blocked by global read lock must
be designed like this:
- call to wait_if_global_read_lock(). When this returns 0, no global read
lock is owned; if argument abort_on_refresh was 0, none can be obtained.
- job
- if abort_on_refresh was 0, call to start_waiting_global_read_lock() to
allow other threads to get the global read lock. I.e. removal of the
protection.
(Note: it's a bit like an implementation of rwlock).
[ I am sorry to mention some SQL syntaxes below I know I shouldn't but found
no better descriptive way ]
Why does FLUSH TABLES WITH READ LOCK need to block COMMIT: because it's used
to read a non-moving SHOW MASTER STATUS, and a COMMIT writes to the binary
log.
Why getting the global read lock is two steps and not one. Because FLUSH
TABLES WITH READ LOCK needs to insert one other step between the two:
flushing tables. So the order is
1) lock_global_read_lock() (prevents any new table write locks, i.e. stalls
all new updates)
2) close_cached_tables() (the FLUSH TABLES), which will wait for tables
currently opened and being updated to close (so it's possible that there is
a moment where all new updates of server are stalled *and* FLUSH TABLES WITH
READ LOCK is, too).
3) make_global_read_lock_block_commit().
If we have merged 1) and 3) into 1), we would have had this deadlock:
imagine thread 1 and 2, in non-autocommit mode, thread 3, and an InnoDB
table t.
thd1: SELECT * FROM t FOR UPDATE;
thd2: UPDATE t SET a=1; # blocked by row-level locks of thd1
thd3: FLUSH TABLES WITH READ LOCK; # blocked in close_cached_tables() by the
table instance of thd2
thd1: COMMIT; # blocked by thd3.
thd1 blocks thd2 which blocks thd3 which blocks thd1: deadlock.
2005-04-04 00:50:05 +02:00
Note that we need to support that one thread does
FLUSH TABLES WITH READ LOCK; and then COMMIT;
(that's what innobackup does, for some good reason).
So in this exceptional case the COMMIT should not be blocked by the FLUSH
TABLES WITH READ LOCK.
****************************************************************************/
volatile uint global_read_lock=0;
volatile uint global_read_lock_blocks_commit=0;
static volatile uint protect_against_global_read_lock=0;
static volatile uint waiting_for_read_lock=0;
#define GOT_GLOBAL_READ_LOCK 1
#define MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT 2
bool lock_global_read_lock(THD *thd)
{
DBUG_ENTER("lock_global_read_lock");
if (!thd->global_read_lock)
{
const char *old_message;
(void) pthread_mutex_lock(&LOCK_global_read_lock);
old_message=thd->enter_cond(&COND_global_read_lock, &LOCK_global_read_lock,
"Waiting to get readlock");
DBUG_PRINT("info",
("waiting_for: %d protect_against: %d",
waiting_for_read_lock, protect_against_global_read_lock));
waiting_for_read_lock++;
while (protect_against_global_read_lock && !thd->killed)
pthread_cond_wait(&COND_global_read_lock, &LOCK_global_read_lock);
waiting_for_read_lock--;
if (thd->killed)
{
thd->exit_cond(old_message);
DBUG_RETURN(1);
}
thd->global_read_lock= GOT_GLOBAL_READ_LOCK;
global_read_lock++;
2005-04-04 00:50:05 +02:00
thd->exit_cond(old_message); // this unlocks LOCK_global_read_lock
}
/*
We DON'T set global_read_lock_blocks_commit now, it will be set after
tables are flushed (as the present function serves for FLUSH TABLES WITH
READ LOCK only). Doing things in this order is necessary to avoid
deadlocks (we must allow COMMIT until all tables are closed; we should not
forbid it before, or we can have a 3-thread deadlock if 2 do SELECT FOR
UPDATE and one does FLUSH TABLES WITH READ LOCK).
*/
DBUG_RETURN(0);
}
void unlock_global_read_lock(THD *thd)
{
uint tmp;
DBUG_ENTER("unlock_global_read_lock");
DBUG_PRINT("info",
("global_read_lock: %u global_read_lock_blocks_commit: %u",
global_read_lock, global_read_lock_blocks_commit));
pthread_mutex_lock(&LOCK_global_read_lock);
tmp= --global_read_lock;
if (thd->global_read_lock == MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT)
--global_read_lock_blocks_commit;
pthread_mutex_unlock(&LOCK_global_read_lock);
/* Send the signal outside the mutex to avoid a context switch */
if (!tmp)
{
DBUG_PRINT("signal", ("Broadcasting COND_global_read_lock"));
pthread_cond_broadcast(&COND_global_read_lock);
}
thd->global_read_lock= 0;
DBUG_VOID_RETURN;
}
#define must_wait (global_read_lock && \
(is_not_commit || \
global_read_lock_blocks_commit))
bool wait_if_global_read_lock(THD *thd, bool abort_on_refresh,
bool is_not_commit)
{
const char *old_message;
bool result= 0, need_exit_cond;
DBUG_ENTER("wait_if_global_read_lock");
LINT_INIT(old_message);
/*
Assert that we do not own LOCK_open. If we would own it, other
threads could not close their tables. This would make a pretty
deadlock.
*/
safe_mutex_assert_not_owner(&LOCK_open);
(void) pthread_mutex_lock(&LOCK_global_read_lock);
if ((need_exit_cond= must_wait))
{
if (thd->global_read_lock) // This thread had the read locks
{
if (is_not_commit)
2004-11-12 14:36:31 +01:00
my_message(ER_CANT_UPDATE_WITH_READLOCK,
ER(ER_CANT_UPDATE_WITH_READLOCK), MYF(0));
(void) pthread_mutex_unlock(&LOCK_global_read_lock);
/*
We allow FLUSHer to COMMIT; we assume FLUSHer knows what it does.
This allowance is needed to not break existing versions of innobackup
which do a BEGIN; INSERT; FLUSH TABLES WITH READ LOCK; COMMIT.
*/
DBUG_RETURN(is_not_commit);
}
old_message=thd->enter_cond(&COND_global_read_lock, &LOCK_global_read_lock,
"Waiting for release of readlock");
while (must_wait && ! thd->killed &&
(!abort_on_refresh || thd->version == refresh_version))
{
DBUG_PRINT("signal", ("Waiting for COND_global_read_lock"));
(void) pthread_cond_wait(&COND_global_read_lock, &LOCK_global_read_lock);
DBUG_PRINT("signal", ("Got COND_global_read_lock"));
}
if (thd->killed)
result=1;
}
if (!abort_on_refresh && !result)
protect_against_global_read_lock++;
/*
The following is only true in case of a global read locks (which is rare)
and if old_message is set
*/
2005-04-04 00:50:05 +02:00
if (unlikely(need_exit_cond))
thd->exit_cond(old_message); // this unlocks LOCK_global_read_lock
else
pthread_mutex_unlock(&LOCK_global_read_lock);
DBUG_RETURN(result);
}
void start_waiting_global_read_lock(THD *thd)
{
bool tmp;
DBUG_ENTER("start_waiting_global_read_lock");
if (unlikely(thd->global_read_lock))
DBUG_VOID_RETURN;
(void) pthread_mutex_lock(&LOCK_global_read_lock);
tmp= (!--protect_against_global_read_lock &&
(waiting_for_read_lock || global_read_lock_blocks_commit));
(void) pthread_mutex_unlock(&LOCK_global_read_lock);
if (tmp)
pthread_cond_broadcast(&COND_global_read_lock);
DBUG_VOID_RETURN;
}
bool make_global_read_lock_block_commit(THD *thd)
{
bool error;
const char *old_message;
DBUG_ENTER("make_global_read_lock_block_commit");
/*
If we didn't succeed lock_global_read_lock(), or if we already suceeded
make_global_read_lock_block_commit(), do nothing.
*/
if (thd->global_read_lock != GOT_GLOBAL_READ_LOCK)
DBUG_RETURN(0);
pthread_mutex_lock(&LOCK_global_read_lock);
/* increment this BEFORE waiting on cond (otherwise race cond) */
global_read_lock_blocks_commit++;
/* For testing we set up some blocking, to see if we can be killed */
DBUG_EXECUTE_IF("make_global_read_lock_block_commit_loop",
protect_against_global_read_lock++;);
old_message= thd->enter_cond(&COND_global_read_lock, &LOCK_global_read_lock,
"Waiting for all running commits to finish");
while (protect_against_global_read_lock && !thd->killed)
pthread_cond_wait(&COND_global_read_lock, &LOCK_global_read_lock);
DBUG_EXECUTE_IF("make_global_read_lock_block_commit_loop",
protect_against_global_read_lock--;);
2005-08-11 14:58:15 +02:00
if ((error= test(thd->killed)))
global_read_lock_blocks_commit--; // undo what we did
else
thd->global_read_lock= MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT;
2005-04-04 00:50:05 +02:00
thd->exit_cond(old_message); // this unlocks LOCK_global_read_lock
DBUG_RETURN(error);
}
/*
Broadcast COND_refresh and COND_global_read_lock.
SYNOPSIS
broadcast_refresh()
void No parameters.
DESCRIPTION
Due to a bug in a threading library it could happen that a signal
did not reach its target. A condition for this was that the same
condition variable was used with different mutexes in
pthread_cond_wait(). Some time ago we changed LOCK_open to
LOCK_global_read_lock in global read lock handling. So COND_refresh
was used with LOCK_open and LOCK_global_read_lock.
We did now also change from COND_refresh to COND_global_read_lock
in global read lock handling. But now it is necessary to signal
both conditions at the same time.
NOTE
When signalling COND_global_read_lock within the global read lock
handling, it is not necessary to also signal COND_refresh.
RETURN
void
*/
void broadcast_refresh(void)
{
VOID(pthread_cond_broadcast(&COND_refresh));
VOID(pthread_cond_broadcast(&COND_global_read_lock));
}