65
64
extern bool volatile shutdown_in_progress;
66
TableOpenCache &get_open_cache()
68
static TableOpenCache open_cache; /* Used by mysql_test */
73
static void free_cache_entry(Table *entry);
75
void remove_table(Table *arg)
77
TableOpenCacheRange ppp;
78
ppp= get_open_cache().equal_range(arg->getShare()->getCacheKey());
80
for (TableOpenCache::const_iterator iter= ppp.first;
81
iter != ppp.second; ++iter)
83
Table *found_table= (*iter).second;
85
if (found_table == arg)
87
free_cache_entry(arg);
88
get_open_cache().erase(iter);
94
static bool add_table(Table *arg)
96
TableOpenCache &open_cache(get_open_cache());
98
TableOpenCache::iterator returnable= open_cache.insert(make_pair(arg->getShare()->getCacheKey(), arg));
100
return not (returnable == open_cache.end());
104
Table *tables; /* Used by mysql_test */
106
Table *getTable() const
111
Table *setTable(Table *arg)
120
/* Free cache if too big */
121
while (cached_open_tables() > table_cache_size && getTable())
122
remove_table(getTable());
127
while (getTable() && not getTable()->getShare()->getVersion())
128
remove_table(getTable());
131
void link(Table *table)
135
table->setNext(getTable()); /* Link in last */
136
table->setPrev(getTable()->getPrev());
137
getTable()->setPrev(table);
138
table->getPrev()->setNext(table);
142
table->setPrev(setTable(table));
143
table->setNext(table->getPrev());
144
assert(table->getNext() == table && table->getPrev() == table);
149
void unlink(Table *table)
153
/* Unlink the table from "unused_tables" list. */
154
if (table == getTable())
156
setTable(getTable()->getNext()); // Remove from link
157
if (table == getTable())
162
/* move table first in unused links */
164
void relink(Table *table)
166
if (table != getTable())
170
table->setNext(getTable()); /* Link in unused tables */
171
table->setPrev(getTable()->getPrev());
172
getTable()->getPrev()->setNext(table);
173
getTable()->setPrev(table);
182
remove_table(getTable());
194
static UnusedTables unused_tables;
195
static int open_unireg_entry(Session *session,
198
TableIdentifier &identifier);
200
unsigned char *table_cache_key(const unsigned char *record,
204
unsigned char *table_cache_key(const unsigned char *record,
208
Table *entry=(Table*) record;
209
*length= entry->getShare()->getCacheKey().size();
210
return (unsigned char*) &entry->getShare()->getCacheKey()[0];
67
213
bool table_cache_init(void)
532
void Open_tables_state::doGetTableNames(CachedDirectory &,
533
const SchemaIdentifier &schema_identifier,
534
std::set<std::string> &set_of_names)
701
void Session::doGetTableNames(CachedDirectory &,
702
const SchemaIdentifier &schema_identifier,
703
std::set<std::string> &set_of_names)
536
705
doGetTableNames(schema_identifier, set_of_names);
539
void Open_tables_state::doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
540
TableIdentifier::vector &set_of_identifiers)
708
void Session::doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
709
TableIdentifiers &set_of_identifiers)
542
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
711
for (Table *table= temporary_tables ; table ; table= table->getNext())
544
713
if (schema_identifier.compare(table->getShare()->getSchemaName()))
553
void Open_tables_state::doGetTableIdentifiers(CachedDirectory &,
554
const SchemaIdentifier &schema_identifier,
555
TableIdentifier::vector &set_of_identifiers)
722
void Session::doGetTableIdentifiers(CachedDirectory &,
723
const SchemaIdentifier &schema_identifier,
724
TableIdentifiers &set_of_identifiers)
557
726
doGetTableIdentifiers(schema_identifier, set_of_identifiers);
560
bool Open_tables_state::doDoesTableExist(const TableIdentifier &identifier)
729
bool Session::doDoesTableExist(const TableIdentifier &identifier)
562
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
731
for (Table *table= temporary_tables ; table ; table= table->getNext())
564
733
if (table->getShare()->getType() == message::Table::TEMPORARY)
595
Table *Open_tables_state::find_temporary_table(const TableIdentifier &identifier)
764
Table *Session::find_temporary_table(const char *new_db, const char *table_name)
766
char key[MAX_DBKEY_LENGTH];
769
key_length= TableIdentifier::createKey(key, new_db, table_name);
771
for (Table *table= temporary_tables ; table ; table= table->getNext())
773
const TableIdentifier::Key &share_key(table->getShare()->getCacheKey());
774
if (share_key.size() == key_length &&
775
not memcmp(&share_key[0], key, key_length))
780
return NULL; // Not a temporary table
783
Table *Session::find_temporary_table(TableList *table_list)
785
return find_temporary_table(table_list->db, table_list->table_name);
788
Table *Session::find_temporary_table(TableIdentifier &identifier)
597
790
for (Table *table= temporary_tables ; table ; table= table->getNext())
657
850
@param session Thread context
658
851
@param find Table to remove
660
@note because we risk the chance of deleting the share, we can't assume that it will exist past, this should be modified once we can use a TableShare::shared_ptr here.
663
854
void Session::unlink_open_table(Table *find)
665
const TableIdentifier::Key find_key(find->getShare()->getCacheKey());
667
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
856
char key[MAX_DBKEY_LENGTH];
857
uint32_t key_length= find->getShare()->getCacheKeySize();
859
safe_mutex_assert_owner(LOCK_open.native_handle());
861
memcpy(key, &find->getShare()->getCacheKey()[0], key_length);
670
Note that we need to hold table::Cache::singleton().mutex() while changing the
863
Note that we need to hold LOCK_open while changing the
671
864
open_tables list. Another thread may work on it.
672
(See: table::Cache::singleton().removeTable(), mysql_wait_completed_table())
865
(See: remove_table_from_cache(), mysql_wait_completed_table())
673
866
Closing a MERGE child before the parent would be fatal if the
674
867
other thread tries to abort the MERGE lock in between.
676
869
for (prev= &open_tables; *prev; )
680
if (list->getShare()->getCacheKey() == find_key)
873
if (list->getShare()->getCacheKeySize() == key_length &&
874
not memcmp(&list->getShare()->getCacheKey()[0], key, key_length))
682
876
/* Remove table from open_tables list. */
683
877
*prev= list->getNext();
685
879
/* Close table. */
686
table::remove_table(static_cast<table::Concurrent *>(list));
976
Open table which is already name-locked by this thread.
979
reopen_name_locked_table()
980
session Thread handle
981
table_list TableList object for table to be open, TableList::table
982
member should point to Table object which was used for
984
link_in true - if Table object for table to be opened should be
985
linked into Session::open_tables list.
986
false - placeholder used for name-locking is already in
987
this list so we only need to preserve Table::next
991
This function assumes that its caller already acquired LOCK_open mutex.
998
bool Session::reopen_name_locked_table(TableList* table_list, bool link_in)
1000
Table *table= table_list->table;
1001
char *table_name= table_list->table_name;
1004
safe_mutex_assert_owner(LOCK_open.native_handle());
1006
if (killed || !table)
1011
TableIdentifier identifier(table_list->db, table_list->table_name);
1012
if (open_unireg_entry(this, table, table_name, identifier))
1014
table->intern_close_table();
1016
If there was an error during opening of table (for example if it
1017
does not exist) '*table' object can be wiped out. To be able
1018
properly release name-lock in this case we should restore this
1019
object to its original state.
1026
We want to prevent other connections from opening this table until end
1027
of statement as it is likely that modifications of table's metadata are
1028
not yet finished (for example CREATE TRIGGER have to change .TRG cursor,
1029
or we might want to drop table if CREATE TABLE ... SELECT fails).
1030
This also allows us to assume that no other connection will sneak in
1031
before we will get table-level lock on this table.
1033
table->getMutableShare()->resetVersion();
1034
table->in_use = this;
1038
table->setNext(open_tables);
1044
Table object should be already in Session::open_tables list so we just
1045
need to set Table::next correctly.
1047
table->setNext(orig_table.getNext());
1050
table->tablenr= current_tablenr++;
1051
table->used_fields= 0;
1052
table->const_table= 0;
1053
table->null_row= false;
1054
table->maybe_null= false;
1055
table->force_index= false;
1056
table->status= STATUS_NO_RECORD;
782
1063
Create and insert into table cache placeholder for table
783
1064
which will prevent its opening (or creation) (a.k.a lock
791
1072
case of failure.
794
table::Placeholder *Session::table_cache_insert_placeholder(const drizzled::TableIdentifier &arg)
1075
Table *Session::table_cache_insert_placeholder(const char *db_name, const char *table_name)
796
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
1077
safe_mutex_assert_owner(LOCK_open.native_handle());
799
1080
Create a table entry with the right key and with an old refresh version
801
TableIdentifier identifier(arg.getSchemaName(), arg.getTableName(), message::Table::INTERNAL);
1082
TableIdentifier identifier(db_name, table_name, message::Table::INTERNAL);
802
1083
table::Placeholder *table= new table::Placeholder(this, identifier);
804
if (not table::Cache::singleton().insert(table))
1085
if (not add_table(table))
833
1114
@retval true Error occured (OOM)
834
1115
@retval false Success. 'table' parameter set according to above rules.
836
bool Session::lock_table_name_if_not_cached(const TableIdentifier &identifier, Table **table)
1117
bool Session::lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table)
838
1119
const TableIdentifier::Key &key(identifier.getKey());
840
boost_unique_lock_t scope_lock(table::Cache::singleton().mutex()); /* Obtain a name lock even though table is not in cache (like for create table) */
842
table::CacheMap::iterator iter;
844
iter= table::getCache().find(key);
846
if (iter != table::getCache().end())
1121
boost_unique_lock_t scope_lock(LOCK_open); /* Obtain a name lock even though table is not in cache (like for create table) */
1123
TableOpenCache::iterator iter;
1125
iter= get_open_cache().find(key);
1127
if (iter != get_open_cache().end())
852
if (not (*table= table_cache_insert_placeholder(identifier)))
1133
if (not (*table= table_cache_insert_placeholder(identifier.getSchemaName().c_str(), identifier.getTableName().c_str())))
1111
1392
/* Insert a new Table instance into the open cache */
1113
1394
/* Free cache if too big */
1114
table::getUnused().cull();
1395
unused_tables.cull();
1116
1397
if (table_list->isCreate())
1118
TableIdentifier lock_table_identifier(table_list->getSchemaName(), table_list->getTableName(), message::Table::STANDARD);
1399
TableIdentifier lock_table_identifier(table_list->db, table_list->table_name, message::Table::STANDARD);
1120
1401
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1123
1404
Table to be created, so we need to create placeholder in table-cache.
1125
if (!(table= table_cache_insert_placeholder(lock_table_identifier)))
1406
if (!(table= table_cache_insert_placeholder(table_list->db, table_list->table_name)))
1127
table::Cache::singleton().mutex().unlock();
1436
locking::broadcast_refresh();
1716
broadcast_refresh();
1721
Wait until all threads has closed the tables in the list
1722
We have also to wait if there is thread that has a lock on this table even
1723
if the table is closed
1726
bool table_is_used(Table *table, bool wait_for_name_lock)
1730
const TableIdentifier::Key &key(table->getShare()->getCacheKey());
1732
TableOpenCacheRange ppp;
1733
ppp= get_open_cache().equal_range(key);
1735
for (TableOpenCache::const_iterator iter= ppp.first;
1736
iter != ppp.second; ++iter)
1738
Table *search= (*iter).second;
1739
if (search->in_use == table->in_use)
1740
continue; // Name locked by this thread
1742
We can't use the table under any of the following conditions:
1743
- There is an name lock on it (Table is to be deleted or altered)
1744
- If we are in flush table and we didn't execute the flush
1745
- If the table engine is open and it's an old version
1746
(We must wait until all engines are shut down to use the table)
1748
if ( (search->locked_by_name && wait_for_name_lock) ||
1749
(search->is_name_opened() && search->needs_reopen_or_name_lock()))
1752
} while ((table=table->getNext()));
1559
1874
if (table->getShare()->getCacheKey() == identifier.getKey())
1561
1876
/* If MERGE child, forward lock handling to parent. */
1562
session->abortLock(table);
1877
mysql_lock_abort(session, table);
1884
Load a table definition from cursor and open unireg table
1888
session Thread handle
1889
entry Store open table definition here
1890
table_list TableList with db, table_name
1892
cache_key Key for share_cache
1893
cache_key_length length of cache_key
1896
Extra argument for open is taken from session->open_options
1897
One must have a lock on LOCK_open when calling this function
1904
static int open_unireg_entry(Session *session,
1907
TableIdentifier &identifier)
1910
TableSharePtr share;
1911
uint32_t discover_retry_count= 0;
1913
safe_mutex_assert_owner(LOCK_open.native_handle());
1915
if (not (share= TableShare::getShareCreate(session,
1920
while ((error= share->open_table_from_share(session,
1923
(uint32_t) (HA_OPEN_KEYFILE |
1927
session->open_options, *entry)))
1929
if (error == 7) // Table def changed
1931
share->resetVersion(); // Mark share as old
1932
if (discover_retry_count++) // Retry once
1937
Here we should wait until all threads has released the table.
1938
For now we do one retry. This may cause a deadlock if there
1939
is other threads waiting for other tables used by this thread.
1941
Proper fix would be to if the second retry failed:
1942
- Mark that table def changed
1943
- Return from open table
1944
- Close all tables used by this thread
1945
- Start waiting that the share is released
1946
- Retry by opening all tables again
1951
To avoid deadlock, only wait for release if no one else is
1954
if (share->getTableCount() != 1)
1956
/* Free share and wait until it's released by all threads */
1957
TableShare::release(share);
1959
if (!session->killed)
1961
drizzle_reset_errors(session, 1); // Clear warnings
1962
session->clear_error(); // Clear error message
1974
TableShare::release(share);
1570
1981
Open all tables in list
1837
Table *Open_tables_state::open_temporary_table(const TableIdentifier &identifier,
2249
Table *Session::open_temporary_table(TableIdentifier &identifier,
1840
2254
assert(identifier.isTmp());
1843
table::Temporary *new_tmp_table= new table::Temporary(identifier.getType(),
1845
const_cast<char *>(const_cast<TableIdentifier&>(identifier).getPath().c_str()),
1846
static_cast<uint32_t>(identifier.getPath().length()));
2255
share= new TableShare(identifier.getType(),
2257
const_cast<char *>(identifier.getPath().c_str()), static_cast<uint32_t>(identifier.getPath().length()));
2260
table::Temporary *new_tmp_table= new table::Temporary;
1847
2261
if (not new_tmp_table)
1851
2265
First open the share, and then open the table from the share we just opened.
1853
if (new_tmp_table->getMutableShare()->open_table_def(*static_cast<Session *>(this), identifier) ||
1854
new_tmp_table->getMutableShare()->open_table_from_share(static_cast<Session *>(this), identifier, identifier.getTableName().c_str(),
1855
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2267
if (share->open_table_def(*this, identifier) ||
2268
share->open_table_from_share(this, identifier, identifier.getTableName().c_str(),
2269
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
1860
2274
/* No need to lock share->mutex as this is not needed for tmp tables */
1861
delete new_tmp_table->getMutableShare();
1862
2276
delete new_tmp_table;
3958
4378
unireg support functions
3959
4379
*****************************************************************************/
4382
Invalidate any cache entries that are for some DB
4385
remove_db_from_cache()
4386
db Database name. This will be in lower case if
4387
lower_case_table_name is set
4390
We can't use hash_delete when looping hash_elements. We mark them first
4391
and afterwards delete those marked unused.
4394
void remove_db_from_cache(const SchemaIdentifier &schema_identifier)
4396
safe_mutex_assert_owner(LOCK_open.native_handle());
4398
for (TableOpenCache::const_iterator iter= get_open_cache().begin();
4399
iter != get_open_cache().end();
4402
Table *table= (*iter).second;
4404
if (not schema_identifier.getPath().compare(table->getShare()->getSchemaName()))
4406
table->getMutableShare()->resetVersion(); /* Free when thread is ready */
4407
if (not table->in_use)
4408
unused_tables.relink(table);
4412
unused_tables.cullByVersion();
4417
Mark all entries with the table as deleted to force an reopen of the table
4419
The table will be closed (not stored in cache) by the current thread when
4420
close_thread_tables() is called.
4426
0 This thread now have exclusive access to this table and no other thread
4427
can access the table until close_thread_tables() is called.
4428
1 Table is in use by another thread
4431
bool remove_table_from_cache(Session *session, TableIdentifier &identifier, uint32_t flags)
4433
const TableIdentifier::Key &key(identifier.getKey());
4435
bool signalled= false;
4439
result= signalled= false;
4441
TableOpenCacheRange ppp;
4442
ppp= get_open_cache().equal_range(key);
4444
for (TableOpenCache::const_iterator iter= ppp.first;
4445
iter != ppp.second; ++iter)
4447
Table *table= (*iter).second;
4450
table->getMutableShare()->resetVersion(); /* Free when thread is ready */
4451
if (!(in_use=table->in_use))
4453
unused_tables.relink(table);
4455
else if (in_use != session)
4458
Mark that table is going to be deleted from cache. This will
4459
force threads that are in mysql_lock_tables() (but not yet
4460
in thr_multi_lock()) to abort it's locks, close all tables and retry
4462
in_use->some_tables_deleted= true;
4463
if (table->is_name_opened())
4468
Now we must abort all tables locks used by this thread
4469
as the thread may be waiting to get a lock for another table.
4470
Note that we need to hold LOCK_open while going through the
4471
list. So that the other thread cannot change it. The other
4472
thread must also hold LOCK_open whenever changing the
4473
open_tables list. Aborting the MERGE lock after a child was
4474
closed and before the parent is closed would be fatal.
4476
for (Table *session_table= in_use->open_tables;
4478
session_table= session_table->getNext())
4480
/* Do not handle locks of MERGE children. */
4481
if (session_table->db_stat) // If table is open
4482
signalled|= mysql_lock_abort_for_thread(session, session_table);
4486
result= result || (flags & RTFC_OWNED_BY_Session_FLAG);
4489
unused_tables.cullByVersion();
4491
/* Remove table from table definition cache if it's not in use */
4492
TableShare::release(identifier);
4494
if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG))
4497
Signal any thread waiting for tables to be freed to
4500
broadcast_refresh();
4501
if (!(flags & RTFC_CHECK_KILLED_FLAG) || !session->killed)
4504
if (likely(signalled))
4506
boost_unique_lock_t scoped(LOCK_open, boost::adopt_lock_t());
4507
COND_refresh.wait(scoped);
4513
It can happen that another thread has opened the
4514
table but has not yet locked any table at all. Since
4515
it can be locked waiting for a table that our thread
4516
has done LOCK Table x WRITE on previously, we need to
4517
ensure that the thread actually hears our signal
4518
before we go to sleep. Thus we wait for a short time
4519
and then we retry another loop in the
4520
remove_table_from_cache routine.
4523
xtime_get(&xt, boost::TIME_UTC);
4525
boost_unique_lock_t scoped(LOCK_open, boost::adopt_lock_t());
4526
COND_refresh.timed_wait(scoped, xt);