Commit 11eb7b94 authored by Konstantin Osipov's avatar Konstantin Osipov

Backport of:

---------------------------------------------
2630.7.3 Konstantin Osipov       2008-06-02
        Various style changes preceding the removal of reopen_table().
(Post-review fixes for WL#3726).
parent 8486ae21
......@@ -554,7 +554,7 @@ Event_db_repository::open_event_table(THD *thd, enum thr_lock_type lock_type,
TABLE_LIST tables;
DBUG_ENTER("Event_db_repository::open_event_table");
tables.init_one_table("mysql", "event", lock_type);
tables.init_one_table("mysql", "event", "event", lock_type);
alloc_mdl_locks(&tables, thd->mem_root);
if (simple_open_n_lock_tables(thd, &tables))
......@@ -1109,7 +1109,7 @@ Event_db_repository::check_system_tables(THD *thd)
/* Check mysql.db */
tables.init_one_table("mysql", "db", TL_READ);
tables.init_one_table("mysql", "db", "db", TL_READ);
alloc_mdl_locks(&tables, thd->mem_root);
if (simple_open_n_lock_tables(thd, &tables))
......@@ -1127,7 +1127,7 @@ Event_db_repository::check_system_tables(THD *thd)
close_thread_tables(thd);
}
/* Check mysql.user */
tables.init_one_table("mysql", "user", TL_READ);
tables.init_one_table("mysql", "user", "user", TL_READ);
alloc_mdl_locks(&tables, thd->mem_root);
if (simple_open_n_lock_tables(thd, &tables))
......@@ -1148,7 +1148,7 @@ Event_db_repository::check_system_tables(THD *thd)
close_thread_tables(thd);
}
/* Check mysql.event */
tables.init_one_table("mysql", "event", TL_READ);
tables.init_one_table("mysql", "event", "event", TL_READ);
alloc_mdl_locks(&tables, thd->mem_root);
if (simple_open_n_lock_tables(thd, &tables))
......
......@@ -1033,8 +1033,6 @@ bool check_dup(const char *db, const char *name, TABLE_LIST *tables);
bool compare_record(TABLE *table);
bool append_file_to_dir(THD *thd, const char **filename_ptr,
const char *table_name);
bool wait_while_table_is_used(THD *thd, TABLE *table,
enum ha_extra_function function);
bool table_def_init(void);
void table_def_free(void);
void assign_new_table_id(TABLE_SHARE *share);
......@@ -1390,6 +1388,9 @@ void add_join_on(TABLE_LIST *b,Item *expr);
void add_join_natural(TABLE_LIST *a,TABLE_LIST *b,List<String> *using_fields,
SELECT_LEX *lex);
bool add_proc_to_list(THD *thd, Item *item);
bool close_cached_table(THD *thd, TABLE *table);
bool wait_while_table_is_used(THD *thd, TABLE *table,
enum ha_extra_function function);
void unlink_open_table(THD *thd, TABLE *find, bool unlock);
void drop_open_table(THD *thd, TABLE *table, const char *db_name,
const char *table_name);
......
......@@ -2200,6 +2200,85 @@ static void unlink_open_merge(THD *thd, TABLE *table, TABLE ***prev_pp)
}
/**
Force all other threads to stop using the table by upgrading
metadata lock on it and remove unused TABLE instances from cache.
@param thd Thread handler
@param table Table to remove from cache
@param function HA_EXTRA_PREPARE_FOR_DROP if table is to be deleted
HA_EXTRA_FORCE_REOPEN if table is not be used
HA_EXTRA_PREPARE_FOR_RENAME if table is to be renamed
@note When returning, the table will be unusable for other threads
until metadata lock is downgraded.
@retval FALSE Success.
@retval TRUE Failure (e.g. because thread was killed).
*/
bool wait_while_table_is_used(THD *thd, TABLE *table,
enum ha_extra_function function)
{
enum thr_lock_type old_lock_type;
DBUG_ENTER("wait_while_table_is_used");
DBUG_PRINT("enter", ("table: '%s' share: 0x%lx db_stat: %u version: %lu",
table->s->table_name.str, (ulong) table->s,
table->db_stat, table->s->version));
(void) table->file->extra(function);
old_lock_type= table->reginfo.lock_type;
mysql_lock_abort(thd, table, TRUE); /* end threads waiting on lock */
if (mdl_upgrade_shared_lock_to_exclusive(&thd->mdl_context,
table->mdl_lock_data))
{
mysql_lock_downgrade_write(thd, table, old_lock_type);
DBUG_RETURN(TRUE);
}
pthread_mutex_lock(&LOCK_open);
expel_table_from_cache(thd, table->s->db.str, table->s->table_name.str);
pthread_mutex_unlock(&LOCK_open);
DBUG_RETURN(FALSE);
}
/**
Upgrade metadata lock on the table and close all its instances.
@param thd Thread handler
@param table Table to remove from cache
@retval FALSE Success.
@retval TRUE Failure (e.g. because thread was killed).
*/
bool close_cached_table(THD *thd, TABLE *table)
{
DBUG_ENTER("close_cached_table");
/* FIXME: check if we pass proper parameters everywhere. */
if (wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN))
DBUG_RETURN(TRUE);
/* Close lock if this is not got with LOCK TABLES */
if (thd->lock)
{
mysql_unlock_tables(thd, thd->lock);
thd->lock=0; // Start locked threads
}
pthread_mutex_lock(&LOCK_open);
/* Close all copies of 'table'. This also frees all LOCK TABLES lock */
unlink_open_table(thd, table, TRUE);
pthread_mutex_unlock(&LOCK_open);
DBUG_RETURN(FALSE);
}
/**
Remove all instances of table from thread's open list and
table cache.
......
......@@ -53,7 +53,6 @@ static bool
mysql_prepare_alter_table(THD *thd, TABLE *table,
HA_CREATE_INFO *create_info,
Alter_info *alter_info);
static bool close_cached_table(THD *thd, TABLE *table);
#ifndef DBUG_OFF
......@@ -4295,84 +4294,6 @@ mysql_rename_table(handlerton *base, const char *old_db,
}
/**
Force all other threads to stop using the table by upgrading
metadata lock on it and remove unused TABLE instances from cache.
@param thd Thread handler
@param table Table to remove from cache
@param function HA_EXTRA_PREPARE_FOR_DROP if table is to be deleted
HA_EXTRA_FORCE_REOPEN if table is not be used
HA_EXTRA_PREPARE_FOR_RENAME if table is to be renamed
@note When returning, the table will be unusable for other threads
until metadata lock is downgraded.
@retval FALSE Success.
@retval TRUE Failure (e.g. because thread was killed).
*/
bool wait_while_table_is_used(THD *thd, TABLE *table,
enum ha_extra_function function)
{
enum thr_lock_type old_lock_type;
DBUG_ENTER("wait_while_table_is_used");
DBUG_PRINT("enter", ("table: '%s' share: 0x%lx db_stat: %u version: %lu",
table->s->table_name.str, (ulong) table->s,
table->db_stat, table->s->version));
(void) table->file->extra(function);
old_lock_type= table->reginfo.lock_type;
mysql_lock_abort(thd, table, TRUE); /* end threads waiting on lock */
if (mdl_upgrade_shared_lock_to_exclusive(&thd->mdl_context,
table->mdl_lock_data))
{
mysql_lock_downgrade_write(thd, table, old_lock_type);
DBUG_RETURN(TRUE);
}
pthread_mutex_lock(&LOCK_open);
expel_table_from_cache(thd, table->s->db.str, table->s->table_name.str);
pthread_mutex_unlock(&LOCK_open);
DBUG_RETURN(FALSE);
}
/**
Upgrade metadata lock on the table and close all its instances.
@param thd Thread handler
@param table Table to remove from cache
@retval FALSE Success.
@retval TRUE Failure (e.g. because thread was killed).
*/
static bool close_cached_table(THD *thd, TABLE *table)
{
DBUG_ENTER("close_cached_table");
/* FIXME: check if we pass proper parameters everywhere. */
if (wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN))
DBUG_RETURN(TRUE);
/* Close lock if this is not got with LOCK TABLES */
if (thd->lock)
{
mysql_unlock_tables(thd, thd->lock);
thd->lock=0; // Start locked threads
}
pthread_mutex_lock(&LOCK_open);
/* Close all copies of 'table'. This also frees all LOCK TABLES lock */
unlink_open_table(thd, table, TRUE);
pthread_mutex_unlock(&LOCK_open);
DBUG_RETURN(FALSE);
}
static int send_check_errmsg(THD *thd, TABLE_LIST* table,
const char* operator_name, const char* errmsg)
......
......@@ -1121,11 +1121,13 @@ struct TABLE_LIST
*/
inline void init_one_table(const char *db_name_arg,
const char *table_name_arg,
const char *alias_arg,
enum thr_lock_type lock_type_arg)
{
bzero((char*) this, sizeof(*this));
db= (char*) db_name_arg;
table_name= alias= (char*) table_name_arg;
table_name= (char*) table_name_arg;
alias= (char*) alias_arg;
lock_type= lock_type_arg;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment