summaryrefslogtreecommitdiff
path: root/sql/table_cache.cc
diff options
context:
space:
mode:
authorSergey Vojtovich <svoj@mariadb.org>2019-12-17 16:25:15 +0400
committerSergey Vojtovich <svoj@mariadb.org>2020-04-03 13:01:21 +0400
commit7a947614fbf8b925ae3df70ec8df80c745eafd4c (patch)
treed462b7218559c2e7855a6019ffc382f003f1f889 /sql/table_cache.cc
parent06fae75859821fe36f68eb2d77f007f014143282 (diff)
downloadmariadb-git-7a947614fbf8b925ae3df70ec8df80c745eafd4c.tar.gz
Split tdc_remove_table()
TDC_RT_REMOVE_ALL -> tdc_remove_table(). Some occurrences replaced with TDC_element::flush() (whenver TABLE_SHARE is available). TDC_RT_REMOVE_NOT_OWN[_KEEP_SHARE] -> TDC_element::flush(). These modes assume that current thread owns TABLE_SHARE reference, which means we can avoid hash lookup and flush unused TABLE instances directly. TDC_RT_REMOVE_UNUSED -> TDC_element::flush_unused(). Only [ab]used by mysql_admin_table() currently. Should be removed eventually. Part of MDEV-17882 - Cleanup refresh version
Diffstat (limited to 'sql/table_cache.cc')
-rw-r--r--sql/table_cache.cc206
1 files changed, 104 insertions, 102 deletions
diff --git a/sql/table_cache.cc b/sql/table_cache.cc
index 4dda19c0c28..82c60359e88 100644
--- a/sql/table_cache.cc
+++ b/sql/table_cache.cc
@@ -997,36 +997,9 @@ void tdc_release_share(TABLE_SHARE *share)
/**
- Remove all or some (depending on parameter) instances of TABLE and
- TABLE_SHARE from the table definition cache.
+ Removes all TABLE instances and corresponding TABLE_SHARE
@param thd Thread context
- @param remove_type Type of removal:
- TDC_RT_REMOVE_ALL - remove all TABLE instances and
- TABLE_SHARE instance. There
- should be no used TABLE objects
- and caller should have exclusive
- metadata lock on the table.
- TDC_RT_REMOVE_NOT_OWN - remove all TABLE instances
- except those that belong to
- this thread. There should be
- no TABLE objects used by other
- threads and caller should have
- exclusive metadata lock on the
- table.
- TDC_RT_REMOVE_UNUSED - remove all unused TABLE
- instances (if there are no
- used instances will also
- remove TABLE_SHARE).
- TDC_RT_REMOVE_NOT_OWN_KEEP_SHARE -
- remove all TABLE instances
- except those that belong to
- this thread, but don't mark
- TABLE_SHARE as old. There
- should be no TABLE objects
- used by other threads and
- caller should have exclusive
- metadata lock on the table.
@param db Name of database
@param table_name Name of table
@@ -1034,27 +1007,21 @@ void tdc_release_share(TABLE_SHARE *share)
(other) thread (this should be achieved by using meta-data locks).
*/
-bool tdc_remove_table(THD *thd, enum_tdc_remove_table_type remove_type,
- const char *db, const char *table_name)
+void tdc_remove_table(THD *thd, const char *db, const char *table_name)
{
Share_free_tables::List purge_tables;
- TABLE *table;
TDC_element *element;
- uint my_refs= 1;
- bool res= false;
DBUG_ENTER("tdc_remove_table");
- DBUG_PRINT("enter",("name: %s remove_type: %d", table_name, remove_type));
+ DBUG_PRINT("enter", ("name: %s", table_name));
- DBUG_ASSERT(remove_type == TDC_RT_REMOVE_UNUSED ||
- thd->mdl_context.is_lock_owner(MDL_key::TABLE, db, table_name,
+ DBUG_ASSERT(thd->mdl_context.is_lock_owner(MDL_key::TABLE, db, table_name,
MDL_EXCLUSIVE));
mysql_mutex_lock(&LOCK_unused_shares);
if (!(element= tdc_lock_share(thd, db, table_name)))
{
mysql_mutex_unlock(&LOCK_unused_shares);
- DBUG_ASSERT(remove_type != TDC_RT_REMOVE_NOT_OWN_KEEP_SHARE);
- DBUG_RETURN(false);
+ DBUG_VOID_RETURN;
}
DBUG_ASSERT(element != MY_ERRPTR); // What can we do about it?
@@ -1070,80 +1037,24 @@ bool tdc_remove_table(THD *thd, enum_tdc_remove_table_type remove_type,
mysql_mutex_unlock(&LOCK_unused_shares);
tdc_delete_share_from_hash(element);
- DBUG_RETURN(false);
+ DBUG_VOID_RETURN;
}
mysql_mutex_unlock(&LOCK_unused_shares);
element->ref_count++;
- if (remove_type != TDC_RT_REMOVE_NOT_OWN_KEEP_SHARE)
- element->flushed= true;
tc_remove_all_unused_tables(element, &purge_tables);
-
- if (remove_type == TDC_RT_REMOVE_NOT_OWN ||
- remove_type == TDC_RT_REMOVE_NOT_OWN_KEEP_SHARE)
- {
- All_share_tables_list::Iterator it(element->all_tables);
- while ((table= it++))
- {
- if (table->in_use == thd)
- my_refs++;
- }
- }
mysql_mutex_unlock(&element->LOCK_table_share);
- while ((table= purge_tables.pop_front()))
+ while (auto table= purge_tables.pop_front())
intern_close_table(table);
- if (remove_type != TDC_RT_REMOVE_UNUSED)
- {
- /*
- Even though current thread holds exclusive metadata lock on this share
- (asserted above), concurrent FLUSH TABLES threads may be in process of
- closing unused table instances belonging to this share. E.g.:
- thr1 (FLUSH TABLES): table= share->tdc.free_tables.pop_front();
- thr1 (FLUSH TABLES): share->tdc.all_tables.remove(table);
- thr2 (ALTER TABLE): tdc_remove_table();
- thr1 (FLUSH TABLES): intern_close_table(table);
-
- Current remove type assumes that all table instances (except for those
- that are owned by current thread) must be closed before
- thd_remove_table() returns. Wait for such tables now.
-
- intern_close_table() decrements ref_count and signals COND_release. When
- ref_count drops down to number of references owned by current thread
- waiting is completed.
-
- Unfortunately TABLE_SHARE::wait_for_old_version() cannot be used here
- because it waits for all table instances, whereas we have to wait only
- for those that are not owned by current thread.
- */
- mysql_mutex_lock(&element->LOCK_table_share);
- while (element->ref_count > my_refs)
- mysql_cond_wait(&element->COND_release, &element->LOCK_table_share);
- DBUG_ASSERT(element->all_tables.is_empty() ||
- remove_type != TDC_RT_REMOVE_ALL);
-#ifndef DBUG_OFF
- if (remove_type == TDC_RT_REMOVE_NOT_OWN ||
- remove_type == TDC_RT_REMOVE_NOT_OWN_KEEP_SHARE)
- {
- All_share_tables_list::Iterator it(element->all_tables);
- while ((table= it++))
- DBUG_ASSERT(table->in_use == thd);
- }
-#endif
- mysql_mutex_unlock(&element->LOCK_table_share);
- }
- else
- {
- mysql_mutex_lock(&element->LOCK_table_share);
- res= element->ref_count > 1;
- mysql_mutex_unlock(&element->LOCK_table_share);
- }
-
- tdc_release_share(element->share);
-
- DBUG_RETURN(res);
+ mysql_mutex_lock(&element->LOCK_table_share);
+ element->wait_for_refs(1);
+ DBUG_ASSERT(element->all_tables.is_empty());
+ element->ref_count--;
+ tdc_delete_share_from_hash(element);
+ DBUG_VOID_RETURN;
}
@@ -1284,3 +1195,94 @@ int show_tc_active_instances(THD *thd, SHOW_VAR *var, char *buff,
tc_active_instances.load(std::memory_order_relaxed);
return 0;
}
+
+
+/**
+ Waits until ref_count goes down to given number
+
+ @param my_refs Number of references owned by the caller
+
+ Caller must own at least one TABLE_SHARE reference.
+
+ Even though current thread holds exclusive metadata lock on this share,
+ concurrent FLUSH TABLES threads may be in process of closing unused table
+ instances belonging to this share. E.g.:
+ thr1 (FLUSH TABLES): table= share->tdc.free_tables.pop_front();
+ thr1 (FLUSH TABLES): share->tdc.all_tables.remove(table);
+ thr2 (ALTER TABLE): tdc_remove_table();
+ thr1 (FLUSH TABLES): intern_close_table(table);
+
+ Current remove type assumes that all table instances (except for those
+ that are owned by current thread) must be closed before
+ thd_remove_table() returns. Wait for such tables now.
+
+ intern_close_table() decrements ref_count and signals COND_release. When
+ ref_count drops down to number of references owned by current thread
+ waiting is completed.
+
+ Unfortunately TABLE_SHARE::wait_for_old_version() cannot be used here
+ because it waits for all table instances, whereas we have to wait only
+ for those that are not owned by current thread.
+*/
+
+void TDC_element::wait_for_refs(uint my_refs)
+{
+ while (ref_count > my_refs)
+ mysql_cond_wait(&COND_release, &LOCK_table_share);
+}
+
+
+/**
+ Flushes unused TABLE instances
+
+ @param thd Thread context
+ @param mark_flushed Whether to destroy TABLE_SHARE when released
+
+ Caller is allowed to own used TABLE instances.
+ There must be no TABLE objects used by other threads and caller must own
+ exclusive metadata lock on the table.
+*/
+
+void TDC_element::flush(THD *thd, bool mark_flushed)
+{
+ DBUG_ASSERT(thd->mdl_context.is_lock_owner(MDL_key::TABLE, share->db.str,
+ share->table_name.str,
+ MDL_EXCLUSIVE));
+
+ flush_unused(mark_flushed);
+
+ mysql_mutex_lock(&LOCK_table_share);
+ All_share_tables_list::Iterator it(all_tables);
+ uint my_refs= 0;
+ while (auto table= it++)
+ {
+ if (table->in_use == thd)
+ my_refs++;
+ }
+ wait_for_refs(my_refs);
+#ifndef DBUG_OFF
+ it.rewind();
+ while (auto table= it++)
+ DBUG_ASSERT(table->in_use == thd);
+#endif
+ mysql_mutex_unlock(&LOCK_table_share);
+}
+
+
+/**
+ Flushes unused TABLE instances
+*/
+
+void TDC_element::flush_unused(bool mark_flushed)
+{
+ Share_free_tables::List purge_tables;
+
+ mysql_mutex_lock(&LOCK_table_share);
+ if (mark_flushed)
+ flushed= true;
+ tc_remove_all_unused_tables(this, &purge_tables);
+ mysql_mutex_unlock(&LOCK_table_share);
+
+ while (auto table= purge_tables.pop_front())
+ intern_close_table(table);
+}