diff options
-rw-r--r-- | include/thr_lock.h | 2 | ||||
-rw-r--r-- | mysys/thr_lock.c | 7 | ||||
-rw-r--r-- | sql/handler.cc | 2 | ||||
-rw-r--r-- | sql/lock.cc | 23 | ||||
-rw-r--r-- | sql/mysql_priv.h | 8 | ||||
-rw-r--r-- | sql/sql_base.cc | 122 | ||||
-rw-r--r-- | sql/sql_table.cc | 35 | ||||
-rw-r--r-- | tests/mysql_client_test.c | 45 |
8 files changed, 168 insertions, 76 deletions
diff --git a/include/thr_lock.h b/include/thr_lock.h index 8c6540d83e2..ef4e6a7e97b 100644 --- a/include/thr_lock.h +++ b/include/thr_lock.h @@ -144,7 +144,7 @@ enum enum_thr_lock_result thr_multi_lock(THR_LOCK_DATA **data, uint count, THR_LOCK_OWNER *owner); void thr_multi_unlock(THR_LOCK_DATA **data,uint count); void thr_abort_locks(THR_LOCK *lock); -void thr_abort_locks_for_thread(THR_LOCK *lock, pthread_t thread); +bool thr_abort_locks_for_thread(THR_LOCK *lock, pthread_t thread); void thr_print_locks(void); /* For debugging */ my_bool thr_upgrade_write_delay_lock(THR_LOCK_DATA *data); my_bool thr_reschedule_write_lock(THR_LOCK_DATA *data); diff --git a/mysys/thr_lock.c b/mysys/thr_lock.c index b12f8234c26..f3179fee994 100644 --- a/mysys/thr_lock.c +++ b/mysys/thr_lock.c @@ -1048,9 +1048,10 @@ void thr_abort_locks(THR_LOCK *lock) This is used to abort all locks for a specific thread */ -void thr_abort_locks_for_thread(THR_LOCK *lock, pthread_t thread) +bool thr_abort_locks_for_thread(THR_LOCK *lock, pthread_t thread) { THR_LOCK_DATA *data; + bool found= FALSE; DBUG_ENTER("thr_abort_locks_for_thread"); pthread_mutex_lock(&lock->mutex); @@ -1061,6 +1062,7 @@ void thr_abort_locks_for_thread(THR_LOCK *lock, pthread_t thread) DBUG_PRINT("info",("Aborting read-wait lock")); data->type= TL_UNLOCK; /* Mark killed */ /* It's safe to signal the cond first: we're still holding the mutex. */ + found= TRUE; pthread_cond_signal(data->cond); data->cond= 0; /* Removed from list */ @@ -1076,6 +1078,7 @@ void thr_abort_locks_for_thread(THR_LOCK *lock, pthread_t thread) { DBUG_PRINT("info",("Aborting write-wait lock")); data->type= TL_UNLOCK; + found= TRUE; pthread_cond_signal(data->cond); data->cond= 0; @@ -1086,7 +1089,7 @@ void thr_abort_locks_for_thread(THR_LOCK *lock, pthread_t thread) } } pthread_mutex_unlock(&lock->mutex); - DBUG_VOID_RETURN; + DBUG_RETURN(found); } diff --git a/sql/handler.cc b/sql/handler.cc index 0d0f9a75e52..2731cb8480d 100644 --- a/sql/handler.cc +++ b/sql/handler.cc @@ -2101,6 +2101,8 @@ int ha_discover(THD *thd, const char *db, const char *name, int error= -1; // Table does not exist in any handler DBUG_ENTER("ha_discover"); DBUG_PRINT("enter", ("db: %s, name: %s", db, name)); + if (is_prefix(name,tmp_file_prefix)) /* skip temporary tables */ + DBUG_RETURN(error); #ifdef HAVE_NDBCLUSTER_DB if (have_ndbcluster == SHOW_OPTION_YES) error= ndbcluster_discover(thd, db, name, frmblob, frmlen); diff --git a/sql/lock.cc b/sql/lock.cc index aa162a23b40..376a582ae32 100644 --- a/sql/lock.cc +++ b/sql/lock.cc @@ -350,20 +350,25 @@ void mysql_lock_abort(THD *thd, TABLE *table) /* Abort one thread / table combination */ -void mysql_lock_abort_for_thread(THD *thd, TABLE *table) +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,1,&write_lock_used))) { for (uint i=0; i < locked->lock_count; i++) - thr_abort_locks_for_thread(locked->locks[i]->lock, - table->in_use->real_id); + { + bool found; + found= thr_abort_locks_for_thread(locked->locks[i]->lock, + table->in_use->real_id); + result|= found; + } my_free((gptr) locked,MYF(0)); } - DBUG_VOID_RETURN; + DBUG_RETURN(result); } @@ -606,8 +611,14 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list) my_free((gptr) table,MYF(0)); DBUG_RETURN(-1); } - if (remove_table_from_cache(thd, db, table_list->table_name, 0)) - DBUG_RETURN(1); // Table is in use + + { + if (remove_table_from_cache(thd, db, + table_list->table_name, RTFC_NO_FLAG)) + { + DBUG_RETURN(1); // Table is in use + } + } DBUG_RETURN(0); } diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 7bd8d76f25d..3db56a2a9d1 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -951,8 +951,12 @@ bool rename_temporary_table(THD* thd, TABLE *table, const char *new_db, const char *table_name); void remove_db_from_cache(const char *db); void flush_tables(); +#define RTFC_NO_FLAG 0x0000 +#define RTFC_OWNED_BY_THD_FLAG 0x0001 +#define RTFC_WAIT_OTHER_THREAD_FLAG 0x0002 +#define RTFC_CHECK_KILLED_FLAG 0x0004 bool remove_table_from_cache(THD *thd, const char *db, const char *table, - bool return_if_owned_by_thd); + uint flags); bool close_cached_tables(THD *thd, bool wait_for_refresh, TABLE_LIST *tables); void copy_field_from_tmp_record(Field *field,int offset); bool fill_record(THD *thd, Field **field, List<Item> &values, @@ -1215,7 +1219,7 @@ void mysql_unlock_read_tables(THD *thd, MYSQL_LOCK *sql_lock); void mysql_unlock_some_tables(THD *thd, TABLE **table,uint count); void mysql_lock_remove(THD *thd, MYSQL_LOCK *locked,TABLE *table); void mysql_lock_abort(THD *thd, TABLE *table); -void mysql_lock_abort_for_thread(THD *thd, TABLE *table); +bool mysql_lock_abort_for_thread(THD *thd, TABLE *table); MYSQL_LOCK *mysql_lock_merge(MYSQL_LOCK *a,MYSQL_LOCK *b); bool lock_global_read_lock(THD *thd); void unlock_global_read_lock(THD *thd); diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 576f5a503f0..04badbd9945 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -292,7 +292,8 @@ bool close_cached_tables(THD *thd, bool if_wait_for_refresh, bool found=0; for (TABLE_LIST *table= tables; table; table= table->next_local) { - if (remove_table_from_cache(thd, table->db, table->table_name, 1)) + if (remove_table_from_cache(thd, table->db, table->table_name, + RTFC_OWNED_BY_THD_FLAG)) found=1; } if (!found) @@ -4088,62 +4089,99 @@ void flush_tables() */ bool remove_table_from_cache(THD *thd, const char *db, const char *table_name, - bool return_if_owned_by_thd) + uint flags) { char key[MAX_DBKEY_LENGTH]; uint key_length; TABLE *table; - bool result=0; + bool result=0, signalled= 0; DBUG_ENTER("remove_table_from_cache"); key_length=(uint) (strmov(strmov(key,db)+1,table_name)-key)+1; - for (table=(TABLE*) hash_search(&open_cache,(byte*) key,key_length) ; - table; - table = (TABLE*) hash_next(&open_cache,(byte*) key,key_length)) + for (;;) { - THD *in_use; - table->s->version= 0L; /* Free when thread is ready */ - if (!(in_use=table->in_use)) - { - DBUG_PRINT("info",("Table was not in use")); - relink_unused(table); - } - else if (in_use != thd) + result= signalled= 0; + + for (table=(TABLE*) hash_search(&open_cache,(byte*) key,key_length) ; + table; + table = (TABLE*) hash_next(&open_cache,(byte*) key,key_length)) { - in_use->some_tables_deleted=1; - if (table->db_stat) - result=1; - /* Kill delayed insert threads */ - if ((in_use->system_thread & SYSTEM_THREAD_DELAYED_INSERT) && - ! in_use->killed) + THD *in_use; + table->s->version=0L; /* Free when thread is ready */ + if (!(in_use=table->in_use)) { - in_use->killed= THD::KILL_CONNECTION; - pthread_mutex_lock(&in_use->mysys_var->mutex); - if (in_use->mysys_var->current_cond) - { - pthread_mutex_lock(in_use->mysys_var->current_mutex); - pthread_cond_broadcast(in_use->mysys_var->current_cond); - pthread_mutex_unlock(in_use->mysys_var->current_mutex); - } - pthread_mutex_unlock(&in_use->mysys_var->mutex); + DBUG_PRINT("info",("Table was not in use")); + relink_unused(table); } - /* - Now we must abort all tables locks used by this thread - as the thread may be waiting to get a lock for another table - */ - for (TABLE *thd_table= in_use->open_tables; - thd_table ; - thd_table= thd_table->next) + else if (in_use != thd) { - if (thd_table->db_stat) // If table is open - mysql_lock_abort_for_thread(thd, thd_table); + in_use->some_tables_deleted=1; + if (table->db_stat) + result=1; + /* Kill delayed insert threads */ + if ((in_use->system_thread & SYSTEM_THREAD_DELAYED_INSERT) && + ! in_use->killed) + { + in_use->killed= THD::KILL_CONNECTION; + pthread_mutex_lock(&in_use->mysys_var->mutex); + if (in_use->mysys_var->current_cond) + { + pthread_mutex_lock(in_use->mysys_var->current_mutex); + signalled= 1; + pthread_cond_broadcast(in_use->mysys_var->current_cond); + pthread_mutex_unlock(in_use->mysys_var->current_mutex); + } + pthread_mutex_unlock(&in_use->mysys_var->mutex); + } + /* + Now we must abort all tables locks used by this thread + as the thread may be waiting to get a lock for another table + */ + for (TABLE *thd_table= in_use->open_tables; + thd_table ; + thd_table= thd_table->next) + { + if (thd_table->db_stat) // If table is open + signalled|= mysql_lock_abort_for_thread(thd, thd_table); + } } + else + result= result || (flags & RTFC_OWNED_BY_THD_FLAG); } - else - result= result || return_if_owned_by_thd; + while (unused_tables && !unused_tables->version) + VOID(hash_delete(&open_cache,(byte*) unused_tables)); + if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG)) + { + if (!(flags & RTFC_CHECK_KILLED_FLAG) || !thd->killed) + { + if (likely(signalled)) + { + dropping_tables++; + (void) pthread_cond_wait(&COND_refresh, &LOCK_open); + dropping_tables--; + continue; + } + else + { + /* + It can happen that another thread has opened the + table but has not yet locked any table at all. Since + it can be locked waiting for a table that our thread + has done LOCK TABLE x WRITE on previously, we need to + ensure that the thread actually hears our signal + before we go to sleep. Thus we wait for a short time + and then we retry another loop in the + remove_table_from_cache routine. + */ + pthread_mutex_unlock(&LOCK_open); + my_sleep(10); + pthread_mutex_lock(&LOCK_open); + continue; + } + } + } + break; } - while (unused_tables && !unused_tables->s->version) - VOID(hash_delete(&open_cache,(byte*) unused_tables)); DBUG_RETURN(result); } diff --git a/sql/sql_table.cc b/sql/sql_table.cc index e3f85f05c17..e91142d450a 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -229,6 +229,7 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists, for (table= tables; table; table= table->next_local) { char *db=table->db; + uint flags; mysql_ha_flush(thd, table, MYSQL_HA_CLOSE_FINAL); if (!close_temporary_table(thd, db, table->table_name)) { @@ -240,13 +241,8 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists, if (!drop_temporary) { abort_locked_tables(thd,db,table->table_name); - while (remove_table_from_cache(thd, db, table->table_name, 0) && - !thd->killed) - { - dropping_tables++; - (void) pthread_cond_wait(&COND_refresh,&LOCK_open); - dropping_tables--; - } + flags= RTFC_WAIT_OTHER_THREAD_FLAG | RTFC_CHECK_KILLED_FLAG; + remove_table_from_cache(thd,db,table->table_name,flags); drop_locked_tables(thd,db,table->table_name); if (thd->killed) { @@ -1870,12 +1866,8 @@ static void wait_while_table_is_used(THD *thd,TABLE *table, mysql_lock_abort(thd, table); // end threads waiting on lock /* Wait until all there are no other threads that has this table open */ - while (remove_table_from_cache(thd, table->s->db, table->s->table_name, 0)) - { - dropping_tables++; - (void) pthread_cond_wait(&COND_refresh,&LOCK_open); - dropping_tables--; - } + remove_table_from_cache(thd, table->s->db, + table->s->table_name, RTFC_WAIT_OTHER_THREAD_FLAG); DBUG_VOID_RETURN; } @@ -2258,18 +2250,14 @@ static bool mysql_admin_table(THD* thd, TABLE_LIST* tables, /* Close all instances of the table to allow repair to rename files */ if (lock_type == TL_WRITE && table->table->s->version) { + uint flags; pthread_mutex_lock(&LOCK_open); const char *old_message=thd->enter_cond(&COND_refresh, &LOCK_open, "Waiting to get writelock"); mysql_lock_abort(thd,table->table); - while (remove_table_from_cache(thd, table->table->s->db, - table->table->s->table_name, 0) && - ! thd->killed) - { - dropping_tables++; - (void) pthread_cond_wait(&COND_refresh,&LOCK_open); - dropping_tables--; - } + flags= RTFC_WAIT_OTHER_THREAD_FLAG | RTFC_CHECK_KILLED_FLAG; + remove_table_from_cache(thd, table->table->s->db, + table->table->s->table_name, flags); thd->exit_cond(old_message); if (thd->killed) goto err; @@ -2414,7 +2402,7 @@ send_result_message: { pthread_mutex_lock(&LOCK_open); remove_table_from_cache(thd, table->table->s->db, - table->table->s->table_name, 0); + table->table->s->table_name, RTFC_NO_FLAG); pthread_mutex_unlock(&LOCK_open); /* May be something modified consequently we have to invalidate cache */ query_cache_invalidate3(thd, table->table, 0); @@ -3741,7 +3729,8 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, if (table) { VOID(table->file->extra(HA_EXTRA_FORCE_REOPEN)); // Use new file - remove_table_from_cache(thd,db,table_name, 0); // Mark in-use copies old + remove_table_from_cache(thd,db,table_name,RTFC_NO_FLAG); + // Mark in-use copies old mysql_lock_abort(thd,table); // end threads waiting on lock } VOID(quick_rm_table(old_db_type,db,old_name)); diff --git a/tests/mysql_client_test.c b/tests/mysql_client_test.c index 78948548716..53708a7a741 100644 --- a/tests/mysql_client_test.c +++ b/tests/mysql_client_test.c @@ -13888,6 +13888,50 @@ static void test_bug10760() mysql_autocommit(mysql, TRUE); /* restore default */ } +static void test_bug12001() +{ + MYSQL *mysql_local; + MYSQL_RES *result; + const char *query= "DROP TABLE IF EXISTS test_table;" + "CREATE TABLE test_table(id INT);" + "INSERT INTO test_table VALUES(10);" + "UPDATE test_table SET id=20 WHERE id=10;" + "SELECT * FROM test_table;" + "INSERT INTO non_existent_table VALUES(11);"; + int rc, res; + + myheader("test_bug12001"); + + if (!(mysql_local= mysql_init(NULL))) + { + fprintf(stdout, "\n mysql_init() failed"); + exit(1); + } + + /* Create connection that supports multi statements */ + if (!mysql_real_connect(mysql_local, opt_host, opt_user, + opt_password, current_db, opt_port, + opt_unix_socket, CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS)) { + fprintf(stdout, "\n mysql_real_connect() failed"); + exit(1); + } + + rc= mysql_query(mysql_local, query); + myquery(rc); + + do { + if (mysql_field_count(mysql_local) && (result= mysql_use_result(mysql_local))) { + mysql_free_result(result); + } + } while (!(res= mysql_next_result(mysql_local))); + + rc= mysql_query(mysql_local, "DROP TABLE IF EXISTS test_table"); + myquery(rc); + + mysql_close(mysql_local); + DIE_UNLESS(res==1); +} + /* Read and parse arguments and MySQL options from my.cnf */ @@ -14133,6 +14177,7 @@ static struct my_tests_st my_tests[]= { { "test_bug11183", test_bug11183 }, { "test_bug11037", test_bug11037 }, { "test_bug10760", test_bug10760 }, + { "test_bug12001", test_bug12001 }, { 0, 0 } }; |