From b14045660153fc48065774a9bc8a955b2eba093d Mon Sep 17 00:00:00 2001 From: Konstantin Osipov Date: Fri, 11 Jun 2010 19:28:18 +0400 Subject: WL#5419 "LOCK_open scalability: make tdc_refresh_version an atomic counter" Split the large LOCK_open section in open_table(). Do not call open_table_from_share() under LOCK_open. Remove thd->version. This fixes Bug#50589 "Server hang on a query evaluated using a temporary table" Bug#51557 "LOCK_open and kernel_mutex are not happy together" Bug#49463 "LOCK_table and innodb are not nice when handler instances are created". This patch has effect on storage engines that rely on ha_open() PSEA method being called under LOCK_open. In particular: 1) NDB is broken and left unfixed. NDB relies on LOCK_open being kept as part of ha_open(), since it uses auto-discovery. While previously the NDB open code was race-prone, now it simply fails on asserts. 2) HEAP engine had a race in ha_heap::open() when a share for the same table could be added twice to the list of shares, or a dangling reference to a share stored in HEAP handler. This patch aims to address this problem by 'pinning' the newly created share in the internal HEAP engine share list until at least one handler instance is created using that share. --- sql/lock.cc | 3 +- sql/repl_failsafe.cc | 1 - sql/sql_base.cc | 104 +++++++++++++++++++++++---------------------------- sql/sql_base.h | 1 + sql/sql_class.cc | 2 +- sql/sql_class.h | 10 ----- sql/sql_plugin.cc | 9 +---- sql/sql_udf.cc | 2 +- sql/table.h | 1 - sql/tztime.cc | 5 ++- 10 files changed, 56 insertions(+), 82 deletions(-) (limited to 'sql') diff --git a/sql/lock.cc b/sql/lock.cc index d35f2c359fb..52d97a2422b 100644 --- a/sql/lock.cc +++ b/sql/lock.cc @@ -1298,7 +1298,8 @@ wait_if_global_read_lock(THD *thd, bool abort_on_refresh, 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)) + (!abort_on_refresh || !thd->open_tables || + thd->open_tables->s->version == refresh_version)) { DBUG_PRINT("signal", ("Waiting for COND_global_read_lock")); mysql_cond_wait(&COND_global_read_lock, &LOCK_global_read_lock); diff --git a/sql/repl_failsafe.cc b/sql/repl_failsafe.cc index cddf798aac0..81366d55fc6 100644 --- a/sql/repl_failsafe.cc +++ b/sql/repl_failsafe.cc @@ -102,7 +102,6 @@ static int init_failsafe_rpl_thread(THD* thd) thd->mem_root->free= thd->mem_root->used= 0; thd_proc_info(thd, "Thread initialized"); - thd->version=refresh_version; thd->set_time(); DBUG_RETURN(0); } diff --git a/sql/sql_base.cc b/sql/sql_base.cc index c227fd12f5b..78862985e97 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -1589,10 +1589,18 @@ bool close_thread_table(THD *thd, TABLE **table_ptr) *table_ptr=table->next; mysql_mutex_unlock(&thd->LOCK_thd_data); + if (! table->needs_reopen()) + { + /* Avoid having MERGE tables with attached children in unused_tables. */ + table->file->extra(HA_EXTRA_DETACH_CHILDREN); + /* Free memory and reset for next loop. */ + free_field_buffers_larger_than(table, MAX_TDC_BLOB_SIZE); + table->file->ha_reset(); + } + mysql_mutex_lock(&LOCK_open); - if (table->s->needs_reopen() || - thd->version != refresh_version || table->needs_reopen() || + if (table->s->needs_reopen() || table->needs_reopen() || table_def_shutdown_in_progress) { free_cache_entry(table); @@ -1600,14 +1608,7 @@ bool close_thread_table(THD *thd, TABLE **table_ptr) } else { - /* Avoid to have MERGE tables with attached children in unused_tables. */ DBUG_ASSERT(table->file); - table->file->extra(HA_EXTRA_DETACH_CHILDREN); - - /* Free memory and reset for next loop */ - free_field_buffers_larger_than(table,MAX_TDC_BLOB_SIZE); - - table->file->ha_reset(); table_def_unuse_table(table); /* We free the least used table, not the subject table, @@ -2305,7 +2306,7 @@ void wait_for_condition(THD *thd, mysql_mutex_t *mutex, mysql_cond_t *cond) @param[out] exists Out parameter which is set to TRUE if table exists and to FALSE otherwise. - @note This function assumes that caller owns LOCK_open mutex. + @note This function acquires LOCK_open internally. It also assumes that the fact that there are no exclusive metadata locks on the table was checked beforehand. @@ -2313,28 +2314,30 @@ void wait_for_condition(THD *thd, mysql_mutex_t *mutex, mysql_cond_t *cond) of engines (e.g. it was created on another node of NDB cluster) this function will fetch and create proper .FRM file for it. - @retval TRUE Some error occured + @retval TRUE Some error occurred @retval FALSE No error. 'exists' out parameter set accordingly. */ bool check_if_table_exists(THD *thd, TABLE_LIST *table, bool *exists) { char path[FN_REFLEN + 1]; - int rc; + int rc= 0; DBUG_ENTER("check_if_table_exists"); - mysql_mutex_assert_owner(&LOCK_open); + mysql_mutex_assert_not_owner(&LOCK_open); *exists= TRUE; + mysql_mutex_lock(&LOCK_open); + if (get_cached_table_share(table->db, table->table_name)) - DBUG_RETURN(FALSE); + goto end; build_table_filename(path, sizeof(path) - 1, table->db, table->table_name, reg_ext, 0); if (!access(path, F_OK)) - DBUG_RETURN(FALSE); + goto end; /* .FRM file doesn't exist. Check if some engine can provide it. */ @@ -2344,19 +2347,17 @@ bool check_if_table_exists(THD *thd, TABLE_LIST *table, bool *exists) { /* Table does not exists in engines as well. */ *exists= FALSE; - DBUG_RETURN(FALSE); - } - else if (!rc) - { - /* Table exists in some engine and .FRM for it was created. */ - DBUG_RETURN(FALSE); + rc= 0; } - else /* (rc > 0) */ + else if (rc) { my_printf_error(ER_UNKNOWN_ERROR, "Failed to open '%-.64s', error while " "unpacking from engine", MYF(0), table->table_name); - DBUG_RETURN(TRUE); } + +end: + mysql_mutex_unlock(&LOCK_open); + DBUG_RETURN(test(rc)); } @@ -2651,7 +2652,7 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, if (thd->global_read_lock.wait_if_global_read_lock(thd, 1, 1)) DBUG_RETURN(TRUE); - if (thd->version != refresh_version) + if (thd->open_tables && thd->open_tables->s->version != refresh_version) { (void) ot_ctx->request_backoff_action(Open_table_context::OT_WAIT_TDC, NULL); @@ -2848,48 +2849,24 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, } hash_value= my_calc_hash(&table_def_cache, (uchar*) key, key_length); - mysql_mutex_lock(&LOCK_open); - /* - If it's the first table from a list of tables used in a query, - remember refresh_version (the version of open_cache state). - If the version changes while we're opening the remaining tables, - we will have to back off, close all the tables opened-so-far, - and try to reopen them. - Note: refresh_version is currently changed only during FLUSH TABLES. - */ - if (!thd->open_tables) - thd->version=refresh_version; - else if ((thd->version != refresh_version) && - ! (flags & MYSQL_OPEN_IGNORE_FLUSH)) - { - /* Someone did a refresh while thread was opening tables */ - mysql_mutex_unlock(&LOCK_open); - (void) ot_ctx->request_backoff_action(Open_table_context::OT_WAIT_TDC, - NULL); - DBUG_RETURN(TRUE); - } if (table_list->open_strategy == TABLE_LIST::OPEN_IF_EXISTS) { bool exists; if (check_if_table_exists(thd, table_list, &exists)) - goto err_unlock2; + DBUG_RETURN(TRUE); if (!exists) - { - mysql_mutex_unlock(&LOCK_open); DBUG_RETURN(FALSE); - } + /* Table exists. Let us try to open it. */ } else if (table_list->open_strategy == TABLE_LIST::OPEN_STUB) - { - mysql_mutex_unlock(&LOCK_open); DBUG_RETURN(FALSE); - } + mysql_mutex_lock(&LOCK_open); #ifdef DISABLED_UNTIL_GRL_IS_MADE_PART_OF_MDL if (!(share= (TABLE_SHARE *) mdl_ticket->get_cached_object())) #endif @@ -2911,7 +2888,7 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, my_error(ER_WRONG_MRG_TABLE, MYF(0)); goto err_unlock; } - + /* This table is a view. Validate its metadata version: in particular, that it was a view when the statement was prepared. @@ -2980,7 +2957,15 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, } #endif - if (share->needs_reopen()) + + /* + If the version changes while we're opening the tables, + we have to back off, close all the tables opened-so-far, + and try to reopen them. Note: refresh_version is currently + changed only during FLUSH TABLES. + */ + if (share->needs_reopen() || + (thd->open_tables && thd->open_tables->s->version != share->version)) { if (!(flags & MYSQL_OPEN_IGNORE_FLUSH)) { @@ -3000,8 +2985,6 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, NULL); DBUG_RETURN(TRUE); } - /* Force close at once after usage */ - thd->version= share->version; } if (!share->free_tables.is_empty()) @@ -3017,9 +3000,11 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, while (table_cache_count > table_cache_size && unused_tables) free_cache_entry(unused_tables); + mysql_mutex_unlock(&LOCK_open); + /* make a new table */ if (!(table=(TABLE*) my_malloc(sizeof(*table),MYF(MY_WME)))) - goto err_unlock; + goto err_lock; error= open_table_from_share(thd, share, alias, (uint) (HA_OPEN_KEYFILE | @@ -3041,16 +3026,17 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, (void) ot_ctx->request_backoff_action(Open_table_context::OT_REPAIR, table_list); - goto err_unlock; + goto err_lock; } if (open_table_entry_fini(thd, share, table)) { closefrm(table, 0); my_free((uchar*)table, MYF(0)); - goto err_unlock; + goto err_lock; } + mysql_mutex_lock(&LOCK_open); /* Add table to the share's used tables list. */ table_def_add_used_table(thd, table); } @@ -3112,6 +3098,8 @@ bool open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, table->file->extra(HA_EXTRA_DETACH_CHILDREN); DBUG_RETURN(FALSE); +err_lock: + mysql_mutex_lock(&LOCK_open); err_unlock: release_table_share(share); err_unlock2: diff --git a/sql/sql_base.h b/sql/sql_base.h index 38194f94a37..20a068e27d7 100644 --- a/sql/sql_base.h +++ b/sql/sql_base.h @@ -267,6 +267,7 @@ TABLE *find_table_for_mdl_upgrade(TABLE *list, const char *db, const char *table_name, bool no_error); void mark_tmp_table_for_reuse(TABLE *table); +bool check_if_table_exists(THD *thd, TABLE_LIST *table, bool *exists); extern uint table_cache_count; extern TABLE *unused_tables; diff --git a/sql/sql_class.cc b/sql/sql_class.cc index b5d38bebd73..03d365115cf 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -592,7 +592,7 @@ THD::THD() *scramble= '\0'; /* Call to init() below requires fully initialized Open_tables_state. */ - init_open_tables_state(this, refresh_version); + reset_open_tables_state(this); init(); #if defined(ENABLED_PROFILING) diff --git a/sql/sql_class.h b/sql/sql_class.h index 6cc22b22d6c..44af0b7d66e 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -1006,7 +1006,6 @@ public: of the main statement is called. */ enum enum_locked_tables_mode locked_tables_mode; - ulong version; uint current_tablenr; enum enum_flags { @@ -1025,15 +1024,6 @@ public: */ Open_tables_state() : state_flags(0U) { } - /** - Prepare Open_tables_state instance for operations dealing with tables. - */ - void init_open_tables_state(THD *thd, ulong version_arg) - { - reset_open_tables_state(thd); - version= version_arg; - } - void set_open_tables_state(Open_tables_state *state) { *this= *state; diff --git a/sql/sql_plugin.cc b/sql/sql_plugin.cc index 0282053ce7e..bd6ff3a951d 100644 --- a/sql/sql_plugin.cc +++ b/sql/sql_plugin.cc @@ -261,11 +261,6 @@ static plugin_ref intern_plugin_lock(LEX *lex, plugin_ref plugin static void intern_plugin_unlock(LEX *lex, plugin_ref plugin); static void reap_plugins(void); -#ifdef EMBEDDED_LIBRARY -/* declared in sql_base.cc */ -extern bool check_if_table_exists(THD *thd, TABLE_LIST *table, bool *exists); -#endif /* EMBEDDED_LIBRARY */ - static void report_error(int where_to, uint error, ...) { va_list args; @@ -1475,10 +1470,8 @@ static void plugin_load(MEM_ROOT *tmp_root, int *argc, char **argv) When building an embedded library, if the mysql.plugin table does not exist, we silently ignore the missing table */ - mysql_mutex_lock(&LOCK_open); if (check_if_table_exists(new_thd, &tables, &table_exists)) table_exists= FALSE; - mysql_mutex_unlock(&LOCK_open); if (!table_exists) goto end; #endif /* EMBEDDED_LIBRARY */ @@ -1519,7 +1512,7 @@ static void plugin_load(MEM_ROOT *tmp_root, int *argc, char **argv) if (error > 0) sql_print_error(ER(ER_GET_ERRNO), my_errno); end_read_record(&read_record_info); - new_thd->version--; // Force close to free memory + table->m_needs_reopen= TRUE; // Force close to free memory end: close_thread_tables(new_thd); /* Remember that we don't have a THD */ diff --git a/sql/sql_udf.cc b/sql/sql_udf.cc index 9ec17a67533..3d197303fb1 100644 --- a/sql/sql_udf.cc +++ b/sql/sql_udf.cc @@ -248,7 +248,7 @@ void udf_init() if (error > 0) sql_print_error("Got unknown error: %d", my_errno); end_read_record(&read_record_info); - new_thd->version--; // Force close to free memory + table->m_needs_reopen= TRUE; // Force close to free memory end: close_thread_tables(new_thd); diff --git a/sql/table.h b/sql/table.h index 30cac15f8d1..1307770d8e1 100644 --- a/sql/table.h +++ b/sql/table.h @@ -590,7 +590,6 @@ struct TABLE_SHARE enum enum_ha_unused unused2; uint ref_count; /* How many TABLE objects uses this */ - uint open_count; /* Number of tables in open list */ uint blob_ptr_size; /* 4 or 8 */ uint key_block_size; /* create key_block_size, if used */ uint null_bytes, last_null_bit_pos; diff --git a/sql/tztime.cc b/sql/tztime.cc index b23456b5465..79f3b83553e 100644 --- a/sql/tztime.cc +++ b/sql/tztime.cc @@ -1692,7 +1692,11 @@ my_tz_init(THD *org_thd, const char *default_tzname, my_bool bootstrap) } for (TABLE_LIST *tl= tz_tables; tl; tl= tl->next_global) + { tl->table->use_all_columns(); + /* Force close at the end of the function to free memory. */ + tl->table->m_needs_reopen= TRUE; + } /* Now we are going to load leap seconds descriptions that are shared @@ -1781,7 +1785,6 @@ end_with_setting_default_tz: end_with_close: if (time_zone_tables_exist) { - thd->version--; /* Force close to free memory */ close_thread_tables(thd); thd->mdl_context.release_transactional_locks(); } -- cgit v1.2.1