65
63
extern bool volatile shutdown_in_progress;
66
@defgroup Data_Dictionary Data Dictionary
71
HASH &get_open_cache()
73
static HASH open_cache; /* Used by mysql_test */
79
Table *tables; /* Used by mysql_test */
81
Table *getTable() const
86
Table *setTable(Table *arg)
95
/* Free cache if too big */
96
while (get_open_cache().records > table_cache_size && getTable())
97
hash_delete(&get_open_cache(), (unsigned char*) getTable());
102
while (getTable() && not getTable()->getShare()->getVersion())
103
hash_delete(&get_open_cache(), (unsigned char*) getTable());
106
void link(Table *table)
110
table->setNext(getTable()); /* Link in last */
111
table->setPrev(getTable()->getPrev());
112
getTable()->setPrev(table);
113
table->getPrev()->setNext(table);
117
table->setPrev(setTable(table));
118
table->setNext(table->getPrev());
119
assert(table->getNext() == table && table->getPrev() == table);
124
void unlink(Table *table)
128
/* Unlink the table from "unused_tables" list. */
129
if (table == getTable())
131
setTable(getTable()->getNext()); // Remove from link
132
if (table == getTable())
137
/* move table first in unused links */
139
void relink(Table *table)
141
if (table != getTable())
145
table->setNext(getTable()); /* Link in unused tables */
146
table->setPrev(getTable()->getPrev());
147
getTable()->getPrev()->setNext(table);
148
getTable()->setPrev(table);
157
hash_delete(&get_open_cache(), (unsigned char*) getTable());
169
static UnusedTables unused_tables;
170
static int open_unireg_entry(Session *session,
173
TableIdentifier &identifier);
174
void free_cache_entry(void *entry);
175
unsigned char *table_cache_key(const unsigned char *record,
180
static bool reopen_table(Table *table);
183
unsigned char *table_cache_key(const unsigned char *record,
187
Table *entry=(Table*) record;
188
*length= entry->getShare()->getCacheKey().size();
189
return (unsigned char*) &entry->getShare()->getCacheKey()[0];
67
192
bool table_cache_init(void)
72
uint32_t cached_open_tables(void)
74
return table::getCache().size();
194
return hash_init(&get_open_cache(), &my_charset_bin,
195
(size_t) table_cache_size+16,
196
0, 0, table_cache_key,
197
free_cache_entry, 0);
77
200
void table_cache_free(void)
79
202
refresh_version++; // Force close of open tables
81
table::getUnused().clear();
82
table::getCache().clear();
204
unused_tables.clear();
206
if (not get_open_cache().records) // Safety first
207
hash_free(&get_open_cache());
210
uint32_t cached_open_tables(void)
212
return get_open_cache().records;
86
217
Close cursor handle, but leave the table in the table cache
170
334
bool result= false;
171
335
Session *session= this;
174
table::Cache::singleton().mutex().lock(); /* Optionally lock for remove tables from open_cahe if not in use */
178
refresh_version++; // Force close of open tables
180
table::getUnused().clear();
182
if (wait_for_refresh)
337
pthread_mutex_lock(&LOCK_open); /* Optionally lock for remove tables from open_cahe if not in use */
341
refresh_version++; // Force close of open tables
343
unused_tables.clear();
345
if (wait_for_refresh)
348
Other threads could wait in a loop in open_and_lock_tables(),
349
trying to lock one or more of our tables.
351
If they wait for the locks in thr_multi_lock(), their lock
352
request is aborted. They loop in open_and_lock_tables() and
353
enter open_table(). Here they notice the table is refreshed and
354
wait for COND_refresh. Then they loop again in
355
openTablesLock() and this time open_table() succeeds. At
356
this moment, if we (the FLUSH TABLES thread) are scheduled and
357
on another FLUSH TABLES enter close_cached_tables(), they could
358
awake while we sleep below, waiting for others threads (us) to
359
close their open tables. If this happens, the other threads
360
would find the tables unlocked. They would get the locks, one
361
after the other, and could do their destructive work. This is an
362
issue if we have LOCK TABLES in effect.
364
The problem is that the other threads passed all checks in
365
open_table() before we refresh the table.
367
The fix for this problem is to set some_tables_deleted for all
368
threads with open tables. These threads can still get their
369
locks, but will immediately release them again after checking
370
this variable. They will then loop in openTablesLock()
371
again. There they will wait until we update all tables version
374
Setting some_tables_deleted is done by remove_table_from_cache()
377
In other words (reviewer suggestion): You need this setting of
378
some_tables_deleted for the case when table was opened and all
379
related checks were passed before incrementing refresh_version
380
(which you already have) but attempt to lock the table happened
381
after the call to Session::close_old_data_files() i.e. after removal of
382
current thread locks.
384
for (uint32_t idx=0 ; idx < get_open_cache().records ; idx++)
386
Table *table=(Table*) hash_element(&get_open_cache(),idx);
388
table->in_use->some_tables_deleted= false;
395
for (TableList *table= tables; table; table= table->next_local)
397
TableIdentifier identifier(table->db, table->table_name);
398
if (remove_table_from_cache(session, identifier,
399
RTFC_OWNED_BY_Session_FLAG))
405
wait_for_refresh= false; // Nothing to wait for
408
if (wait_for_refresh)
411
If there is any table that has a lower refresh_version, wait until
412
this is closed (or this thread is killed) before returning
414
session->mysys_var->current_mutex= &LOCK_open;
415
session->mysys_var->current_cond= &COND_refresh;
416
session->set_proc_info("Flushing tables");
418
session->close_old_data_files();
421
/* Wait until all threads has closed all the tables we had locked */
422
while (found && ! session->killed)
425
for (uint32_t idx=0 ; idx < get_open_cache().records ; idx++)
427
Table *table=(Table*) hash_element(&get_open_cache(), idx);
428
/* Avoid a self-deadlock. */
429
if (table->in_use == session)
185
Other threads could wait in a loop in open_and_lock_tables(),
186
trying to lock one or more of our tables.
188
If they wait for the locks in thr_multi_lock(), their lock
189
request is aborted. They loop in open_and_lock_tables() and
190
enter open_table(). Here they notice the table is refreshed and
191
wait for COND_refresh. Then they loop again in
192
openTablesLock() and this time open_table() succeeds. At
193
this moment, if we (the FLUSH TABLES thread) are scheduled and
194
on another FLUSH TABLES enter close_cached_tables(), they could
195
awake while we sleep below, waiting for others threads (us) to
196
close their open tables. If this happens, the other threads
197
would find the tables unlocked. They would get the locks, one
198
after the other, and could do their destructive work. This is an
199
issue if we have LOCK TABLES in effect.
201
The problem is that the other threads passed all checks in
202
open_table() before we refresh the table.
204
The fix for this problem is to set some_tables_deleted for all
205
threads with open tables. These threads can still get their
206
locks, but will immediately release them again after checking
207
this variable. They will then loop in openTablesLock()
208
again. There they will wait until we update all tables version
211
Setting some_tables_deleted is done by table::Cache::singleton().removeTable()
214
In other words (reviewer suggestion): You need this setting of
215
some_tables_deleted for the case when table was opened and all
216
related checks were passed before incrementing refresh_version
217
(which you already have) but attempt to lock the table happened
218
after the call to Session::close_old_data_files() i.e. after removal of
219
current thread locks.
432
Note that we wait here only for tables which are actually open, and
433
not for placeholders with Table::open_placeholder set. Waiting for
434
latter will cause deadlock in the following scenario, for example:
436
conn1: lock table t1 write;
437
conn2: lock table t2 write;
441
It also does not make sense to wait for those of placeholders that
442
are employed by CREATE TABLE as in this case table simply does not
221
for (table::CacheMap::const_iterator iter= table::getCache().begin();
222
iter != table::getCache().end();
225
Table *table= (*iter).second;
227
table->in_use->some_tables_deleted= false;
234
for (TableList *table= tables; table; table= table->next_local)
236
TableIdentifier identifier(table->getSchemaName(), table->getTableName());
237
if (table::Cache::singleton().removeTable(session, identifier,
238
RTFC_OWNED_BY_Session_FLAG))
445
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
446
(table->open_placeholder && wait_for_placeholders)))
449
pthread_cond_wait(&COND_refresh,&LOCK_open);
244
wait_for_refresh= false; // Nothing to wait for
455
No other thread has the locked tables open; reopen them and get the
456
old locks. This should always succeed (unless some external process
457
has removed the tables)
459
result= session->reopen_tables(true, true);
247
if (wait_for_refresh)
461
/* Set version for table */
462
for (Table *table= session->open_tables; table ; table= table->getNext())
250
If there is any table that has a lower refresh_version, wait until
251
this is closed (or this thread is killed) before returning
253
session->mysys_var->current_mutex= &table::Cache::singleton().mutex();
254
session->mysys_var->current_cond= &COND_refresh;
255
session->set_proc_info("Flushing tables");
257
session->close_old_data_files();
260
/* Wait until all threads has closed all the tables we had locked */
261
while (found && ! session->getKilled())
264
for (table::CacheMap::const_iterator iter= table::getCache().begin();
265
iter != table::getCache().end();
268
Table *table= (*iter).second;
269
/* Avoid a self-deadlock. */
270
if (table->in_use == session)
273
Note that we wait here only for tables which are actually open, and
274
not for placeholders with Table::open_placeholder set. Waiting for
275
latter will cause deadlock in the following scenario, for example:
277
conn1-> lock table t1 write;
278
conn2-> lock table t2 write;
279
conn1-> flush tables;
280
conn2-> flush tables;
282
It also does not make sense to wait for those of placeholders that
283
are employed by CREATE TABLE as in this case table simply does not
286
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
287
(table->open_placeholder && wait_for_placeholders)))
290
boost_unique_lock_t scoped(table::Cache::singleton().mutex(), boost::adopt_lock_t());
291
COND_refresh.wait(scoped);
298
No other thread has the locked tables open; reopen them and get the
299
old locks. This should always succeed (unless some external process
300
has removed the tables)
302
result= session->reopen_tables(true, true);
304
/* Set version for table */
305
for (Table *table= session->open_tables; table ; table= table->getNext())
308
Preserve the version (0) of write locked tables so that a impending
309
global read lock won't sneak in.
311
if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
312
table->getMutableShare()->refreshVersion();
465
Preserve the version (0) of write locked tables so that a impending
466
global read lock won't sneak in.
468
if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
469
table->getMutableShare()->refreshVersion();
316
table::Cache::singleton().mutex().unlock();
473
pthread_mutex_unlock(&LOCK_open);
319
475
if (wait_for_refresh)
321
boost_unique_lock_t scopedLock(session->mysys_var->mutex);
477
pthread_mutex_lock(&session->mysys_var->mutex);
322
478
session->mysys_var->current_mutex= 0;
323
479
session->mysys_var->current_cond= 0;
324
480
session->set_proc_info(0);
481
pthread_mutex_unlock(&session->mysys_var->mutex);
746
931
cond Condition to wait for
749
void Session::wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond)
934
void Session::wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond)
751
936
/* Wait until the current table is up to date */
752
937
const char *saved_proc_info;
753
mysys_var->current_mutex= &mutex;
754
mysys_var->current_cond= &cond;
938
mysys_var->current_mutex= mutex;
939
mysys_var->current_cond= cond;
755
940
saved_proc_info= get_proc_info();
756
941
set_proc_info("Waiting for table");
759
We must unlock mutex first to avoid deadlock becasue conditions are
760
sent to this thread by doing locks in the following order:
761
lock(mysys_var->mutex)
762
lock(mysys_var->current_mutex)
764
One by effect of this that one can only use wait_for_condition with
765
condition variables that are guranteed to not disapper (freed) even if this
768
boost_unique_lock_t scopedLock(mutex, boost::adopt_lock_t());
771
cond.wait(scopedLock);
774
boost_unique_lock_t mysys_scopedLock(mysys_var->mutex);
943
(void) pthread_cond_wait(cond, mutex);
946
We must unlock mutex first to avoid deadlock becasue conditions are
947
sent to this thread by doing locks in the following order:
948
lock(mysys_var->mutex)
949
lock(mysys_var->current_mutex)
951
One by effect of this that one can only use wait_for_condition with
952
condition variables that are guranteed to not disapper (freed) even if this
956
pthread_mutex_unlock(mutex);
957
pthread_mutex_lock(&mysys_var->mutex);
775
958
mysys_var->current_mutex= 0;
776
959
mysys_var->current_cond= 0;
777
960
set_proc_info(saved_proc_info);
961
pthread_mutex_unlock(&mysys_var->mutex);
966
Open table which is already name-locked by this thread.
969
reopen_name_locked_table()
970
session Thread handle
971
table_list TableList object for table to be open, TableList::table
972
member should point to Table object which was used for
974
link_in true - if Table object for table to be opened should be
975
linked into Session::open_tables list.
976
false - placeholder used for name-locking is already in
977
this list so we only need to preserve Table::next
981
This function assumes that its caller already acquired LOCK_open mutex.
988
bool Session::reopen_name_locked_table(TableList* table_list, bool link_in)
990
Table *table= table_list->table;
992
char *table_name= table_list->table_name;
995
safe_mutex_assert_owner(&LOCK_open);
997
if (killed || !table)
1002
TableIdentifier identifier(table_list->db, table_list->table_name);
1003
if (open_unireg_entry(this, table, table_name, identifier))
1005
table->intern_close_table();
1007
If there was an error during opening of table (for example if it
1008
does not exist) '*table' object can be wiped out. To be able
1009
properly release name-lock in this case we should restore this
1010
object to its original state.
1016
share= table->getMutableShare();
1018
We want to prevent other connections from opening this table until end
1019
of statement as it is likely that modifications of table's metadata are
1020
not yet finished (for example CREATE TRIGGER have to change .TRG cursor,
1021
or we might want to drop table if CREATE TABLE ... SELECT fails).
1022
This also allows us to assume that no other connection will sneak in
1023
before we will get table-level lock on this table.
1025
share->resetVersion();
1026
table->in_use = this;
1030
table->setNext(open_tables);
1036
Table object should be already in Session::open_tables list so we just
1037
need to set Table::next correctly.
1039
table->setNext(orig_table.getNext());
1042
table->tablenr= current_tablenr++;
1043
table->used_fields= 0;
1044
table->const_table= 0;
1045
table->null_row= false;
1046
table->maybe_null= false;
1047
table->force_index= false;
1048
table->status= STATUS_NO_RECORD;
937
1221
if (table->query_id)
939
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
1223
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
942
1226
table->query_id= getQueryId();
950
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
952
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->getSchemaName(), table_list->getTableName());
1231
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
1233
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
1238
If it's the first table from a list of tables used in a query,
1239
remember refresh_version (the version of open_cache state).
1240
If the version changes while we're opening the remaining tables,
1241
we will have to back off, close all the tables opened-so-far,
1242
and try to reopen them.
1244
Note-> refresh_version is currently changed only during FLUSH TABLES.
1248
version= refresh_version;
1250
else if ((version != refresh_version) &&
1251
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
1253
/* Someone did a refresh while thread was opening tables */
1261
Before we test the global cache, we test our local session cache.
1265
assert(false); /* Not implemented yet */
1269
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
1270
this is the normal use case.
1272
- try to find the table in the table cache.
1273
- if one of the discovered Table instances is name-locked
1274
(table->getShare()->version == 0) back off -- we have to wait
1275
until no one holds a name lock on the table.
1276
- if there is no such Table in the name cache, read the table definition
1277
and insert it into the cache.
1278
We perform all of the above under LOCK_open which currently protects
1279
the open cache (also known as table cache) and table definitions stored
1283
pthread_mutex_lock(&LOCK_open); /* Lock for FLUSH TABLES for open table */
1286
Actually try to find the table in the open_cache.
1287
The cache may contain several "Table" instances for the same
1288
physical table. The instances that are currently "in use" by
1289
some thread have their "in_use" member != NULL.
1290
There is no good reason for having more than one entry in the
1291
hash for the same physical table, except that we use this as
1292
an implicit "pending locks queue" - see
1293
wait_for_locked_table_names for details.
1295
for (table= (Table*) hash_first(&get_open_cache(), (unsigned char*) &key[0], key_length,
1297
table && table->in_use ;
1298
table= (Table*) hash_next(&get_open_cache(), (unsigned char*) &key[0], key_length,
957
If it's the first table from a list of tables used in a query,
958
remember refresh_version (the version of open_cache state).
959
If the version changes while we're opening the remaining tables,
960
we will have to back off, close all the tables opened-so-far,
961
and try to reopen them.
963
Note-> refresh_version is currently changed only during FLUSH TABLES.
1302
Here we flush tables marked for flush.
1303
Normally, table->getShare()->version contains the value of
1304
refresh_version from the moment when this table was
1305
(re-)opened and added to the cache.
1306
If since then we did (or just started) FLUSH TABLES
1307
statement, refresh_version has been increased.
1308
For "name-locked" Table instances, table->getShare()->version is set
1309
to 0 (see lock_table_name for details).
1310
In case there is a pending FLUSH TABLES or a name lock, we
1311
need to back off and re-start opening tables.
1312
If we do not back off now, we may dead lock in case of lock
1313
order mismatch with some other thread:
1314
c1: name lock t1; -- sort of exclusive lock
1315
c2: open t2; -- sort of shared lock
1316
c1: name lock t2; -- blocks
1317
c2: open t1; -- blocks
967
version= refresh_version;
969
else if ((version != refresh_version) &&
970
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
972
/* Someone did a refresh while thread was opening tables */
1319
if (table->needs_reopen_or_name_lock())
1321
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1323
/* Force close at once after usage */
1324
version= table->getShare()->getVersion();
1328
/* Avoid self-deadlocks by detecting self-dependencies. */
1329
if (table->open_placeholder && table->in_use == this)
1331
pthread_mutex_unlock(&LOCK_open);
1332
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getMutableShare()->getTableName());
1337
Back off, part 1: mark the table as "unused" for the
1338
purpose of name-locking by setting table->db_stat to 0. Do
1339
that only for the tables in this thread that have an old
1340
table->getShare()->version (this is an optimization (?)).
1341
table->db_stat == 0 signals wait_for_locked_table_names
1342
that the tables in question are not used any more. See
1343
table_is_used call for details.
1345
close_old_data_files(false, false);
1348
Back-off part 2: try to avoid "busy waiting" on the table:
1349
if the table is in use by some other thread, we suspend
1350
and wait till the operation is complete: when any
1351
operation that juggles with table->getShare()->version completes,
1352
it broadcasts COND_refresh condition variable.
1353
If 'old' table we met is in use by current thread we return
1354
without waiting since in this situation it's this thread
1355
which is responsible for broadcasting on COND_refresh
1356
(and this was done already in Session::close_old_data_files()).
1357
Good example of such situation is when we have statement
1358
that needs two instances of table and FLUSH TABLES comes
1359
after we open first instance but before we open second
1362
if (table->in_use != this)
1364
/* wait_for_conditionwill unlock LOCK_open for us */
1365
wait_for_condition(&LOCK_open, &COND_refresh);
1369
pthread_mutex_unlock(&LOCK_open);
1372
There is a refresh in progress for this table.
1373
Signal the caller that it has to try again.
980
Before we test the global cache, we test our local session cache.
984
assert(false); /* Not implemented yet */
988
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
989
this is the normal use case.
991
- try to find the table in the table cache.
992
- if one of the discovered Table instances is name-locked
993
(table->getShare()->version == 0) back off -- we have to wait
994
until no one holds a name lock on the table.
995
- if there is no such Table in the name cache, read the table definition
996
and insert it into the cache.
997
We perform all of the above under table::Cache::singleton().mutex() which currently protects
998
the open cache (also known as table cache) and table definitions stored
1003
table::Cache::singleton().mutex().lock(); /* Lock for FLUSH TABLES for open table */
1006
Actually try to find the table in the open_cache.
1007
The cache may contain several "Table" instances for the same
1008
physical table. The instances that are currently "in use" by
1009
some thread have their "in_use" member != NULL.
1010
There is no good reason for having more than one entry in the
1011
hash for the same physical table, except that we use this as
1012
an implicit "pending locks queue" - see
1013
wait_for_locked_table_names for details.
1015
ppp= table::getCache().equal_range(key);
1018
for (table::CacheMap::const_iterator iter= ppp.first;
1019
iter != ppp.second; ++iter, table= NULL)
1382
unused_tables.unlink(table);
1383
table->in_use= this;
1387
/* Insert a new Table instance into the open cache */
1389
/* Free cache if too big */
1390
unused_tables.cull();
1392
if (table_list->isCreate())
1394
TableIdentifier lock_table_identifier(table_list->db, table_list->table_name, message::Table::STANDARD);
1396
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1021
table= (*iter).second;
1023
if (not table->in_use)
1026
Here we flush tables marked for flush.
1027
Normally, table->getShare()->version contains the value of
1028
refresh_version from the moment when this table was
1029
(re-)opened and added to the cache.
1030
If since then we did (or just started) FLUSH TABLES
1031
statement, refresh_version has been increased.
1032
For "name-locked" Table instances, table->getShare()->version is set
1033
to 0 (see lock_table_name for details).
1034
In case there is a pending FLUSH TABLES or a name lock, we
1035
need to back off and re-start opening tables.
1036
If we do not back off now, we may dead lock in case of lock
1037
order mismatch with some other thread:
1038
c1-> name lock t1; -- sort of exclusive lock
1039
c2-> open t2; -- sort of shared lock
1040
c1-> name lock t2; -- blocks
1041
c2-> open t1; -- blocks
1399
Table to be created, so we need to create placeholder in table-cache.
1043
if (table->needs_reopen_or_name_lock())
1401
if (!(table= table_cache_insert_placeholder(table_list->db, table_list->table_name, &key[0], key_length)))
1045
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1047
/* Force close at once after usage */
1048
version= table->getShare()->getVersion();
1052
/* Avoid self-deadlocks by detecting self-dependencies. */
1053
if (table->open_placeholder && table->in_use == this)
1055
table::Cache::singleton().mutex().unlock();
1056
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getShare()->getTableName());
1061
Back off, part 1: mark the table as "unused" for the
1062
purpose of name-locking by setting table->db_stat to 0. Do
1063
that only for the tables in this thread that have an old
1064
table->getShare()->version (this is an optimization (?)).
1065
table->db_stat == 0 signals wait_for_locked_table_names
1066
that the tables in question are not used any more. See
1067
table_is_used call for details.
1069
close_old_data_files(false, false);
1072
Back-off part 2: try to avoid "busy waiting" on the table:
1073
if the table is in use by some other thread, we suspend
1074
and wait till the operation is complete: when any
1075
operation that juggles with table->getShare()->version completes,
1076
it broadcasts COND_refresh condition variable.
1077
If 'old' table we met is in use by current thread we return
1078
without waiting since in this situation it's this thread
1079
which is responsible for broadcasting on COND_refresh
1080
(and this was done already in Session::close_old_data_files()).
1081
Good example of such situation is when we have statement
1082
that needs two instances of table and FLUSH TABLES comes
1083
after we open first instance but before we open second
1086
if (table->in_use != this)
1088
/* wait_for_conditionwill unlock table::Cache::singleton().mutex() for us */
1089
wait_for_condition(table::Cache::singleton().mutex(), COND_refresh);
1093
table::Cache::singleton().mutex().unlock();
1096
There is a refresh in progress for this table.
1097
Signal the caller that it has to try again.
1403
pthread_mutex_unlock(&LOCK_open);
1106
table::getUnused().unlink(static_cast<table::Concurrent *>(table));
1107
table->in_use= this;
1111
/* Insert a new Table instance into the open cache */
1113
/* Free cache if too big */
1114
table::getUnused().cull();
1116
if (table_list->isCreate())
1118
TableIdentifier lock_table_identifier(table_list->getSchemaName(), table_list->getTableName(), message::Table::STANDARD);
1120
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1123
Table to be created, so we need to create placeholder in table-cache.
1125
if (!(table= table_cache_insert_placeholder(lock_table_identifier)))
1127
table::Cache::singleton().mutex().unlock();
1131
Link placeholder to the open tables list so it will be automatically
1132
removed once tables are closed. Also mark it so it won't be ignored
1133
by other trying to take name-lock.
1135
table->open_placeholder= true;
1136
table->setNext(open_tables);
1138
table::Cache::singleton().mutex().unlock();
1142
/* Table exists. Let us try to open it. */
1145
/* make a new table */
1147
table::Concurrent *new_table= new table::Concurrent;
1149
if (new_table == NULL)
1151
table::Cache::singleton().mutex().unlock();
1155
error= new_table->open_unireg_entry(this, alias, identifier);
1159
table::Cache::singleton().mutex().unlock();
1162
(void)table::Cache::singleton().insert(new_table);
1166
table::Cache::singleton().mutex().unlock();
1170
table->setNext(open_tables); /* Link into simple list */
1173
table->reginfo.lock_type= TL_READ; /* Assume read */
1407
Link placeholder to the open tables list so it will be automatically
1408
removed once tables are closed. Also mark it so it won't be ignored
1409
by other trying to take name-lock.
1411
table->open_placeholder= true;
1412
table->setNext(open_tables);
1414
pthread_mutex_unlock(&LOCK_open);
1418
/* Table exists. Let us try to open it. */
1421
/* make a new table */
1422
table= (Table *)malloc(sizeof(Table));
1423
memset(table, 0, sizeof(Table));
1426
pthread_mutex_unlock(&LOCK_open);
1430
error= open_unireg_entry(this, table, alias, identifier);
1434
pthread_mutex_unlock(&LOCK_open);
1437
my_hash_insert(&get_open_cache(), (unsigned char*) table);
1440
pthread_mutex_unlock(&LOCK_open);
1443
table->setNext(open_tables); /* Link into simple list */
1446
table->reginfo.lock_type= TL_READ; /* Assume read */
1176
1449
assert(table->getShare()->getTableCount() > 0 || table->getShare()->getType() != message::Table::STANDARD);
1451
if (lex->need_correct_ident())
1452
table->alias_name_used= my_strcasecmp(table_alias_charset,
1453
table->getMutableShare()->getTableName(), alias);
1178
1454
/* Fix alias if table name changes */
1179
if (strcmp(table->getAlias(), alias))
1455
if (strcmp(table->alias, alias))
1181
table->setAlias(alias);
1457
uint32_t length=(uint32_t) strlen(alias)+1;
1458
table->alias= (char*) realloc((char*) table->alias, length);
1459
memcpy((void*) table->alias, alias, length);
1184
1462
/* These variables are also set in reopen_table() */
3958
4475
unireg support functions
3959
4476
*****************************************************************************/
4479
Invalidate any cache entries that are for some DB
4482
remove_db_from_cache()
4483
db Database name. This will be in lower case if
4484
lower_case_table_name is set
4487
We can't use hash_delete when looping hash_elements. We mark them first
4488
and afterwards delete those marked unused.
4491
void remove_db_from_cache(const SchemaIdentifier &schema_identifier)
4493
safe_mutex_assert_owner(&LOCK_open);
4495
for (uint32_t idx=0 ; idx < get_open_cache().records ; idx++)
4497
Table *table=(Table*) hash_element(&get_open_cache(),idx);
4498
if (not schema_identifier.getPath().compare(table->getMutableShare()->getSchemaName()))
4500
table->getMutableShare()->resetVersion(); /* Free when thread is ready */
4501
if (not table->in_use)
4502
unused_tables.relink(table);
4506
unused_tables.cullByVersion();
4511
Mark all entries with the table as deleted to force an reopen of the table
4513
The table will be closed (not stored in cache) by the current thread when
4514
close_thread_tables() is called.
4520
0 This thread now have exclusive access to this table and no other thread
4521
can access the table until close_thread_tables() is called.
4522
1 Table is in use by another thread
4525
bool remove_table_from_cache(Session *session, TableIdentifier &identifier, uint32_t flags)
4527
const TableIdentifier::Key &key(identifier.getKey());
4529
bool signalled= false;
4531
uint32_t key_length= key.size();
4535
HASH_SEARCH_STATE state;
4537
result= signalled= false;
4539
for (table= (Table*) hash_first(&get_open_cache(), (unsigned char*) &key[0], key_length,
4542
table= (Table*) hash_next(&get_open_cache(), (unsigned char*) &key[0], key_length,
4547
table->getMutableShare()->resetVersion(); /* Free when thread is ready */
4548
if (!(in_use=table->in_use))
4550
unused_tables.relink(table);
4552
else if (in_use != session)
4555
Mark that table is going to be deleted from cache. This will
4556
force threads that are in mysql_lock_tables() (but not yet
4557
in thr_multi_lock()) to abort it's locks, close all tables and retry
4559
in_use->some_tables_deleted= true;
4560
if (table->is_name_opened())
4565
Now we must abort all tables locks used by this thread
4566
as the thread may be waiting to get a lock for another table.
4567
Note that we need to hold LOCK_open while going through the
4568
list. So that the other thread cannot change it. The other
4569
thread must also hold LOCK_open whenever changing the
4570
open_tables list. Aborting the MERGE lock after a child was
4571
closed and before the parent is closed would be fatal.
4573
for (Table *session_table= in_use->open_tables;
4575
session_table= session_table->getNext())
4577
/* Do not handle locks of MERGE children. */
4578
if (session_table->db_stat) // If table is open
4579
signalled|= mysql_lock_abort_for_thread(session, session_table);
4583
result= result || (flags & RTFC_OWNED_BY_Session_FLAG);
4586
unused_tables.cullByVersion();
4588
/* Remove table from table definition cache if it's not in use */
4589
TableShare::release(identifier);
4591
if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG))
4594
Signal any thread waiting for tables to be freed to
4597
broadcast_refresh();
4598
if (!(flags & RTFC_CHECK_KILLED_FLAG) || !session->killed)
4601
if (likely(signalled))
4602
(void) pthread_cond_wait(&COND_refresh, &LOCK_open);
4605
struct timespec abstime;
4607
It can happen that another thread has opened the
4608
table but has not yet locked any table at all. Since
4609
it can be locked waiting for a table that our thread
4610
has done LOCK Table x WRITE on previously, we need to
4611
ensure that the thread actually hears our signal
4612
before we go to sleep. Thus we wait for a short time
4613
and then we retry another loop in the
4614
remove_table_from_cache routine.
4616
set_timespec(abstime, 10);
4617
pthread_cond_timedwait(&COND_refresh, &LOCK_open, &abstime);