68
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::Concurrent *entry);
75
void remove_table(table::Concurrent *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::Concurrent *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::Concurrent *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::Concurrent *tables; /* Used by mysql_test */
106
table::Concurrent *getTable() const
111
table::Concurrent *setTable(Table *arg)
113
return tables= static_cast<table::Concurrent *>(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::Concurrent *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::Concurrent *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::Concurrent *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;
196
unsigned char *table_cache_key(const unsigned char *record,
200
unsigned char *table_cache_key(const unsigned char *record,
204
Table *entry=(Table*) record;
205
*length= entry->getShare()->getCacheKey().size();
206
return (unsigned char*) &entry->getShare()->getCacheKey()[0];
70
209
bool table_cache_init(void)
113
252
This has to be done to ensure that the table share is removed from
114
253
the table defintion cache as soon as the last instance is removed
116
identifier::Table identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), message::Table::INTERNAL);
117
const identifier::Table::Key &key(identifier.getKey());
255
TableIdentifier identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), message::Table::INTERNAL);
256
const TableIdentifier::Key &key(identifier.getKey());
118
257
TableShare *share= new TableShare(identifier.getType(),
120
const_cast<char *>(key.vector()), static_cast<uint32_t>(table->getShare()->getCacheKeySize()));
259
const_cast<char *>(&key[0]), static_cast<uint32_t>(table->getShare()->getCacheKeySize()));
122
261
table->cursor->close();
123
262
table->db_stat= 0; // Mark cursor closed
124
table::instance::release(table->getMutableShare());
263
TableShare::release(table->getMutableShare());
125
264
table->setShare(share);
536
void Open_tables_state::doGetTableNames(CachedDirectory &,
537
const identifier::Schema &schema_identifier,
538
std::set<std::string> &set_of_names)
696
void Session::doGetTableNames(CachedDirectory &,
697
const SchemaIdentifier &schema_identifier,
698
std::set<std::string> &set_of_names)
540
700
doGetTableNames(schema_identifier, set_of_names);
543
void Open_tables_state::doGetTableIdentifiers(const identifier::Schema &schema_identifier,
544
identifier::Table::vector &set_of_identifiers)
703
void Session::doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
704
TableIdentifiers &set_of_identifiers)
546
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
706
for (Table *table= temporary_tables ; table ; table= table->getNext())
548
708
if (schema_identifier.compare(table->getShare()->getSchemaName()))
550
set_of_identifiers.push_back(identifier::Table(table->getShare()->getSchemaName(),
710
set_of_identifiers.push_back(TableIdentifier(table->getShare()->getSchemaName(),
551
711
table->getShare()->getTableName(),
552
712
table->getShare()->getPath()));
557
void Open_tables_state::doGetTableIdentifiers(CachedDirectory &,
558
const identifier::Schema &schema_identifier,
559
identifier::Table::vector &set_of_identifiers)
717
void Session::doGetTableIdentifiers(CachedDirectory &,
718
const SchemaIdentifier &schema_identifier,
719
TableIdentifiers &set_of_identifiers)
561
721
doGetTableIdentifiers(schema_identifier, set_of_identifiers);
564
bool Open_tables_state::doDoesTableExist(const identifier::Table &identifier)
724
bool Session::doDoesTableExist(const TableIdentifier &identifier)
566
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
726
for (Table *table= temporary_tables ; table ; table= table->getNext())
568
728
if (table->getShare()->getType() == message::Table::TEMPORARY)
661
821
@param session Thread context
662
822
@param find Table to remove
664
@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.
824
@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 TableSharePtr here.
667
827
void Session::unlink_open_table(Table *find)
669
const identifier::Table::Key find_key(find->getShare()->getCacheKey());
829
const TableIdentifier::Key find_key(find->getShare()->getCacheKey());
671
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
831
safe_mutex_assert_owner(LOCK_open.native_handle());
674
Note that we need to hold table::Cache::singleton().mutex() while changing the
834
Note that we need to hold LOCK_open while changing the
675
835
open_tables list. Another thread may work on it.
676
(See: table::Cache::singleton().removeTable(), wait_completed_table())
836
(See: remove_table_from_cache(), mysql_wait_completed_table())
677
837
Closing a MERGE child before the parent would be fatal if the
678
838
other thread tries to abort the MERGE lock in between.
798
table::Placeholder *Session::table_cache_insert_placeholder(const drizzled::identifier::Table &arg)
958
Table *Session::table_cache_insert_placeholder(const drizzled::TableIdentifier &arg)
800
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
960
safe_mutex_assert_owner(LOCK_open.native_handle());
803
963
Create a table entry with the right key and with an old refresh version
805
identifier::Table identifier(arg.getSchemaName(), arg.getTableName(), message::Table::INTERNAL);
965
TableIdentifier identifier(arg.getSchemaName(), arg.getTableName(), message::Table::INTERNAL);
806
966
table::Placeholder *table= new table::Placeholder(this, identifier);
808
if (not table::Cache::singleton().insert(table))
968
if (not add_table(table))
837
997
@retval true Error occured (OOM)
838
998
@retval false Success. 'table' parameter set according to above rules.
840
bool Session::lock_table_name_if_not_cached(const identifier::Table &identifier, Table **table)
1000
bool Session::lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table)
842
const identifier::Table::Key &key(identifier.getKey());
844
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) */
846
table::CacheMap::iterator iter;
848
iter= table::getCache().find(key);
850
if (iter != table::getCache().end())
1002
const TableIdentifier::Key &key(identifier.getKey());
1004
boost_unique_lock_t scope_lock(LOCK_open); /* Obtain a name lock even though table is not in cache (like for create table) */
1006
TableOpenCache::iterator iter;
1008
iter= get_open_cache().find(key);
1010
if (iter != get_open_cache().end())
1269
1431
combination when one needs tables to be reopened (for
1270
1432
example see openTablesLock()).
1272
@note One should have lock on table::Cache::singleton().mutex() when calling this.
1434
@note One should have lock on LOCK_open when calling this.
1274
1436
@return false in case of success, true - otherwise.
1277
bool Session::reopen_tables()
1439
bool Session::reopen_tables(bool get_locks, bool)
1279
1441
Table *table,*next,**prev;
1280
Table **tables= 0; // For locks
1281
Table **tables_ptr= 0; // For locks
1442
Table **tables,**tables_ptr; // For locks
1443
bool error=0, not_used;
1283
1444
const uint32_t flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
1284
1445
DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
1285
1446
DRIZZLE_LOCK_IGNORE_FLUSH;
1310
1475
next= table->getNext();
1312
1477
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
1313
table::remove_table(static_cast<table::Concurrent *>(table));
1478
remove_table(static_cast<table::Concurrent *>(table));
1318
1482
if (tables != tables_ptr) // Should we get back old locks
1320
1484
DrizzleLock *local_lock;
1322
1486
We should always get these locks. Anyway, we must not go into
1323
wait_for_tables() as it tries to acquire table::Cache::singleton().mutex(), which is
1487
wait_for_tables() as it tries to acquire LOCK_open, which is
1324
1488
already locked.
1326
1490
some_tables_deleted= false;
1328
if ((local_lock= lockTables(tables, (uint32_t) (tables_ptr - tables), flags)))
1492
if ((local_lock= mysql_lock_tables(this, tables, (uint32_t) (tables_ptr - tables),
1434
locking::broadcast_refresh();
1600
broadcast_refresh();
1605
Wait until all threads has closed the tables in the list
1606
We have also to wait if there is thread that has a lock on this table even
1607
if the table is closed
1610
bool table_is_used(Table *table, bool wait_for_name_lock)
1614
const TableIdentifier::Key &key(table->getShare()->getCacheKey());
1616
TableOpenCacheRange ppp;
1617
ppp= get_open_cache().equal_range(key);
1619
for (TableOpenCache::const_iterator iter= ppp.first;
1620
iter != ppp.second; ++iter)
1622
Table *search= (*iter).second;
1623
if (search->in_use == table->in_use)
1624
continue; // Name locked by this thread
1626
We can't use the table under any of the following conditions:
1627
- There is an name lock on it (Table is to be deleted or altered)
1628
- If we are in flush table and we didn't execute the flush
1629
- If the table engine is open and it's an old version
1630
(We must wait until all engines are shut down to use the table)
1632
if ( (search->locked_by_name && wait_for_name_lock) ||
1633
(search->is_name_opened() && search->needs_reopen_or_name_lock()))
1636
} while ((table=table->getNext()));
1641
/* Wait until all used tables are refreshed */
1643
bool wait_for_tables(Session *session)
1647
session->set_proc_info("Waiting for tables");
1649
boost_unique_lock_t lock(LOCK_open);
1650
while (!session->killed)
1652
session->some_tables_deleted= false;
1653
session->close_old_data_files(false, dropping_tables != 0);
1654
if (!table_is_used(session->open_tables, 1))
1656
COND_refresh.wait(lock);
1658
if (session->killed)
1659
result= true; // aborted
1662
/* Now we can open all tables without any interference */
1663
session->set_proc_info("Reopen tables");
1664
session->version= refresh_version;
1665
result= session->reopen_tables(false, false);
1668
session->set_proc_info(0);
1462
Table *drop_locked_tables(Session *session, const drizzled::identifier::Table &identifier)
1698
Table *drop_locked_tables(Session *session, const drizzled::TableIdentifier &identifier)
1464
1700
Table *table,*next,**prev, *found= 0;
1465
1701
prev= &session->open_tables;
1468
Note that we need to hold table::Cache::singleton().mutex() while changing the
1704
Note that we need to hold LOCK_open while changing the
1469
1705
open_tables list. Another thread may work on it.
1470
(See: table::Cache::singleton().removeTable(), wait_completed_table())
1706
(See: remove_table_from_cache(), mysql_wait_completed_table())
1471
1707
Closing a MERGE child before the parent would be fatal if the
1472
1708
other thread tries to abort the MERGE lock in between.
2387
2622
strcat(buff, table_name);
2388
2623
table_name=buff;
2390
my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, session->where());
2625
my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, session->where);
2394
2629
if (report_error == REPORT_ALL_ERRORS ||
2395
2630
report_error == REPORT_EXCEPT_NON_UNIQUE)
2396
my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), session->where());
2631
my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), session->where);
2398
2633
found= not_found_field;
3932
4159
unireg support functions
3933
4160
*****************************************************************************/
4163
Invalidate any cache entries that are for some DB
4166
remove_db_from_cache()
4167
db Database name. This will be in lower case if
4168
lower_case_table_name is set
4171
We can't use hash_delete when looping hash_elements. We mark them first
4172
and afterwards delete those marked unused.
4175
void remove_db_from_cache(const SchemaIdentifier &schema_identifier)
4177
safe_mutex_assert_owner(LOCK_open.native_handle());
4179
for (TableOpenCache::const_iterator iter= get_open_cache().begin();
4180
iter != get_open_cache().end();
4183
table::Concurrent *table= (*iter).second;
4185
if (not schema_identifier.getPath().compare(table->getShare()->getSchemaName()))
4187
table->getMutableShare()->resetVersion(); /* Free when thread is ready */
4188
if (not table->in_use)
4189
unused_tables.relink(table);
4193
unused_tables.cullByVersion();
4198
Mark all entries with the table as deleted to force an reopen of the table
4200
The table will be closed (not stored in cache) by the current thread when
4201
close_thread_tables() is called.
4207
0 This thread now have exclusive access to this table and no other thread
4208
can access the table until close_thread_tables() is called.
4209
1 Table is in use by another thread
4212
bool remove_table_from_cache(Session *session, TableIdentifier &identifier, uint32_t flags)
4214
const TableIdentifier::Key &key(identifier.getKey());
4216
bool signalled= false;
4220
result= signalled= false;
4222
TableOpenCacheRange ppp;
4223
ppp= get_open_cache().equal_range(key);
4225
for (TableOpenCache::const_iterator iter= ppp.first;
4226
iter != ppp.second; ++iter)
4228
table::Concurrent *table= (*iter).second;
4231
table->getMutableShare()->resetVersion(); /* Free when thread is ready */
4232
if (not (in_use= table->in_use))
4234
unused_tables.relink(table);
4236
else if (in_use != session)
4239
Mark that table is going to be deleted from cache. This will
4240
force threads that are in mysql_lock_tables() (but not yet
4241
in thr_multi_lock()) to abort it's locks, close all tables and retry
4243
in_use->some_tables_deleted= true;
4244
if (table->is_name_opened())
4249
Now we must abort all tables locks used by this thread
4250
as the thread may be waiting to get a lock for another table.
4251
Note that we need to hold LOCK_open while going through the
4252
list. So that the other thread cannot change it. The other
4253
thread must also hold LOCK_open whenever changing the
4254
open_tables list. Aborting the MERGE lock after a child was
4255
closed and before the parent is closed would be fatal.
4257
for (Table *session_table= in_use->open_tables;
4259
session_table= session_table->getNext())
4261
/* Do not handle locks of MERGE children. */
4262
if (session_table->db_stat) // If table is open
4263
signalled|= mysql_lock_abort_for_thread(session, session_table);
4268
result= result || (flags & RTFC_OWNED_BY_Session_FLAG);
4272
unused_tables.cullByVersion();
4274
/* Remove table from table definition cache if it's not in use */
4275
TableShare::release(identifier);
4277
if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG))
4280
Signal any thread waiting for tables to be freed to
4283
broadcast_refresh();
4284
if (!(flags & RTFC_CHECK_KILLED_FLAG) || !session->killed)
4287
if (likely(signalled))
4289
boost_unique_lock_t scoped(LOCK_open, boost::adopt_lock_t());
4290
COND_refresh.wait(scoped);
4296
It can happen that another thread has opened the
4297
table but has not yet locked any table at all. Since
4298
it can be locked waiting for a table that our thread
4299
has done LOCK Table x WRITE on previously, we need to
4300
ensure that the thread actually hears our signal
4301
before we go to sleep. Thus we wait for a short time
4302
and then we retry another loop in the
4303
remove_table_from_cache routine.
4306
xtime_get(&xt, boost::TIME_UTC);
4308
boost_unique_lock_t scoped(LOCK_open, boost::adopt_lock_t());
4309
COND_refresh.timed_wait(scoped, xt);