68
63
extern bool volatile shutdown_in_progress;
65
TableOpenCache &get_open_cache()
67
static TableOpenCache open_cache; /* Used by mysql_test */
72
static void free_cache_entry(Table *entry);
74
void remove_table(Table *arg)
76
TableOpenCacheRange ppp;
77
ppp= get_open_cache().equal_range(arg->getShare()->getCacheKey());
79
for (TableOpenCache::const_iterator iter= ppp.first;
80
iter != ppp.second; ++iter)
82
Table *found_table= (*iter).second;
84
if (found_table == arg)
86
free_cache_entry(arg);
87
get_open_cache().erase(iter);
93
static bool add_table(Table *arg)
95
TableOpenCache &open_cache(get_open_cache());
97
TableOpenCache::iterator returnable= open_cache.insert(make_pair(arg->getShare()->getCacheKey(), arg));
99
return not (returnable == open_cache.end());
103
Table *tables; /* Used by mysql_test */
105
Table *getTable() const
110
Table *setTable(Table *arg)
119
/* Free cache if too big */
120
while (cached_open_tables() > table_cache_size && getTable())
121
remove_table(getTable());
126
while (getTable() && not getTable()->getShare()->getVersion())
127
remove_table(getTable());
130
void link(Table *table)
134
table->setNext(getTable()); /* Link in last */
135
table->setPrev(getTable()->getPrev());
136
getTable()->setPrev(table);
137
table->getPrev()->setNext(table);
141
table->setPrev(setTable(table));
142
table->setNext(table->getPrev());
143
assert(table->getNext() == table && table->getPrev() == table);
148
void unlink(Table *table)
152
/* Unlink the table from "unused_tables" list. */
153
if (table == getTable())
155
setTable(getTable()->getNext()); // Remove from link
156
if (table == getTable())
161
/* move table first in unused links */
163
void relink(Table *table)
165
if (table != getTable())
169
table->setNext(getTable()); /* Link in unused tables */
170
table->setPrev(getTable()->getPrev());
171
getTable()->getPrev()->setNext(table);
172
getTable()->setPrev(table);
181
remove_table(getTable());
193
static UnusedTables unused_tables;
194
static int open_unireg_entry(Session *session,
197
TableIdentifier &identifier);
199
unsigned char *table_cache_key(const unsigned char *record,
204
static bool reopen_table(Table *table);
207
unsigned char *table_cache_key(const unsigned char *record,
211
Table *entry=(Table*) record;
212
*length= entry->getShare()->getCacheKey().size();
213
return (unsigned char*) &entry->getShare()->getCacheKey()[0];
70
216
bool table_cache_init(void)
173
343
bool result= false;
174
344
Session *session= this;
177
boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* Optionally lock for remove tables from open_cahe if not in use */
181
refresh_version++; // Force close of open tables
183
table::getUnused().clear();
185
if (wait_for_refresh)
346
LOCK_open.lock(); /* Optionally lock for remove tables from open_cahe if not in use */
350
refresh_version++; // Force close of open tables
352
unused_tables.clear();
354
if (wait_for_refresh)
357
Other threads could wait in a loop in open_and_lock_tables(),
358
trying to lock one or more of our tables.
360
If they wait for the locks in thr_multi_lock(), their lock
361
request is aborted. They loop in open_and_lock_tables() and
362
enter open_table(). Here they notice the table is refreshed and
363
wait for COND_refresh. Then they loop again in
364
openTablesLock() and this time open_table() succeeds. At
365
this moment, if we (the FLUSH TABLES thread) are scheduled and
366
on another FLUSH TABLES enter close_cached_tables(), they could
367
awake while we sleep below, waiting for others threads (us) to
368
close their open tables. If this happens, the other threads
369
would find the tables unlocked. They would get the locks, one
370
after the other, and could do their destructive work. This is an
371
issue if we have LOCK TABLES in effect.
373
The problem is that the other threads passed all checks in
374
open_table() before we refresh the table.
376
The fix for this problem is to set some_tables_deleted for all
377
threads with open tables. These threads can still get their
378
locks, but will immediately release them again after checking
379
this variable. They will then loop in openTablesLock()
380
again. There they will wait until we update all tables version
383
Setting some_tables_deleted is done by remove_table_from_cache()
386
In other words (reviewer suggestion): You need this setting of
387
some_tables_deleted for the case when table was opened and all
388
related checks were passed before incrementing refresh_version
389
(which you already have) but attempt to lock the table happened
390
after the call to Session::close_old_data_files() i.e. after removal of
391
current thread locks.
393
for (TableOpenCache::const_iterator iter= get_open_cache().begin();
394
iter != get_open_cache().end();
397
Table *table= (*iter).second;
399
table->in_use->some_tables_deleted= false;
406
for (TableList *table= tables; table; table= table->next_local)
408
TableIdentifier identifier(table->db, table->table_name);
409
if (remove_table_from_cache(session, identifier,
410
RTFC_OWNED_BY_Session_FLAG))
416
wait_for_refresh= false; // Nothing to wait for
419
if (wait_for_refresh)
422
If there is any table that has a lower refresh_version, wait until
423
this is closed (or this thread is killed) before returning
425
session->mysys_var->current_mutex= LOCK_open.native_handle();
426
session->mysys_var->current_cond= COND_refresh.native_handle();
427
session->set_proc_info("Flushing tables");
429
session->close_old_data_files();
432
/* Wait until all threads has closed all the tables we had locked */
433
while (found && ! session->killed)
436
for (TableOpenCache::const_iterator iter= get_open_cache().begin();
437
iter != get_open_cache().end();
440
Table *table= (*iter).second;
441
/* Avoid a self-deadlock. */
442
if (table->in_use == session)
188
Other threads could wait in a loop in open_and_lock_tables(),
189
trying to lock one or more of our tables.
191
If they wait for the locks in thr_multi_lock(), their lock
192
request is aborted. They loop in open_and_lock_tables() and
193
enter open_table(). Here they notice the table is refreshed and
194
wait for COND_refresh. Then they loop again in
195
openTablesLock() and this time open_table() succeeds. At
196
this moment, if we (the FLUSH TABLES thread) are scheduled and
197
on another FLUSH TABLES enter close_cached_tables(), they could
198
awake while we sleep below, waiting for others threads (us) to
199
close their open tables. If this happens, the other threads
200
would find the tables unlocked. They would get the locks, one
201
after the other, and could do their destructive work. This is an
202
issue if we have LOCK TABLES in effect.
204
The problem is that the other threads passed all checks in
205
open_table() before we refresh the table.
207
The fix for this problem is to set some_tables_deleted for all
208
threads with open tables. These threads can still get their
209
locks, but will immediately release them again after checking
210
this variable. They will then loop in openTablesLock()
211
again. There they will wait until we update all tables version
214
Setting some_tables_deleted is done by table::Cache::singleton().removeTable()
217
In other words (reviewer suggestion): You need this setting of
218
some_tables_deleted for the case when table was opened and all
219
related checks were passed before incrementing refresh_version
220
(which you already have) but attempt to lock the table happened
221
after the call to Session::close_old_data_files() i.e. after removal of
222
current thread locks.
445
Note that we wait here only for tables which are actually open, and
446
not for placeholders with Table::open_placeholder set. Waiting for
447
latter will cause deadlock in the following scenario, for example:
449
conn1-> lock table t1 write;
450
conn2-> lock table t2 write;
451
conn1-> flush tables;
452
conn2-> flush tables;
454
It also does not make sense to wait for those of placeholders that
455
are employed by CREATE TABLE as in this case table simply does not
224
for (table::CacheMap::const_iterator iter= table::getCache().begin();
225
iter != table::getCache().end();
228
Table *table= (*iter).second;
230
table->in_use->some_tables_deleted= false;
237
for (TableList *table= tables; table; table= table->next_local)
239
identifier::Table identifier(table->getSchemaName(), table->getTableName());
240
if (table::Cache::singleton().removeTable(session, identifier,
241
RTFC_OWNED_BY_Session_FLAG))
458
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
459
(table->open_placeholder && wait_for_placeholders)))
462
pthread_cond_wait(COND_refresh.native_handle(),LOCK_open.native_handle());
247
wait_for_refresh= false; // Nothing to wait for
468
No other thread has the locked tables open; reopen them and get the
469
old locks. This should always succeed (unless some external process
470
has removed the tables)
472
result= session->reopen_tables(true, true);
250
if (wait_for_refresh)
474
/* Set version for table */
475
for (Table *table= session->open_tables; table ; table= table->getNext())
253
If there is any table that has a lower refresh_version, wait until
254
this is closed (or this thread is killed) before returning
256
session->mysys_var->current_mutex= &table::Cache::singleton().mutex();
257
session->mysys_var->current_cond= &COND_refresh;
258
session->set_proc_info("Flushing tables");
260
session->close_old_data_files();
263
/* Wait until all threads has closed all the tables we had locked */
264
while (found && ! session->getKilled())
267
for (table::CacheMap::const_iterator iter= table::getCache().begin();
268
iter != table::getCache().end();
271
Table *table= (*iter).second;
272
/* Avoid a self-deadlock. */
273
if (table->in_use == session)
276
Note that we wait here only for tables which are actually open, and
277
not for placeholders with Table::open_placeholder set. Waiting for
278
latter will cause deadlock in the following scenario, for example:
280
conn1-> lock table t1 write;
281
conn2-> lock table t2 write;
282
conn1-> flush tables;
283
conn2-> flush tables;
285
It also does not make sense to wait for those of placeholders that
286
are employed by CREATE TABLE as in this case table simply does not
289
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
290
(table->open_placeholder && wait_for_placeholders)))
293
COND_refresh.wait(scopedLock);
299
No other thread has the locked tables open; reopen them and get the
300
old locks. This should always succeed (unless some external process
301
has removed the tables)
303
result= session->reopen_tables();
305
/* Set version for table */
306
for (Table *table= session->open_tables; table ; table= table->getNext())
309
Preserve the version (0) of write locked tables so that a impending
310
global read lock won't sneak in.
312
if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
313
table->getMutableShare()->refreshVersion();
478
Preserve the version (0) of write locked tables so that a impending
479
global read lock won't sneak in.
481
if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
482
table->getMutableShare()->refreshVersion();
318
488
if (wait_for_refresh)
320
boost_unique_lock_t scopedLock(session->mysys_var->mutex);
490
pthread_mutex_lock(&session->mysys_var->mutex);
321
491
session->mysys_var->current_mutex= 0;
322
492
session->mysys_var->current_cond= 0;
323
493
session->set_proc_info(0);
494
pthread_mutex_unlock(&session->mysys_var->mutex);
750
941
cond Condition to wait for
753
void Session::wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond)
944
void Session::wait_for_condition(boost::mutex &mutex, boost::condition_variable &cond)
755
946
/* Wait until the current table is up to date */
756
947
const char *saved_proc_info;
757
mysys_var->current_mutex= &mutex;
758
mysys_var->current_cond= &cond;
948
mysys_var->current_mutex= mutex.native_handle();
949
mysys_var->current_cond= cond.native_handle();
759
950
saved_proc_info= get_proc_info();
760
951
set_proc_info("Waiting for table");
763
We must unlock mutex first to avoid deadlock becasue conditions are
764
sent to this thread by doing locks in the following order:
765
lock(mysys_var->mutex)
766
lock(mysys_var->current_mutex)
768
One by effect of this that one can only use wait_for_condition with
769
condition variables that are guranteed to not disapper (freed) even if this
772
boost_unique_lock_t scopedLock(mutex, boost::adopt_lock_t());
775
cond.wait(scopedLock);
778
boost_unique_lock_t mysys_scopedLock(mysys_var->mutex);
953
(void) pthread_cond_wait(cond.native_handle(), mutex.native_handle());
956
We must unlock mutex first to avoid deadlock becasue conditions are
957
sent to this thread by doing locks in the following order:
958
lock(mysys_var->mutex)
959
lock(mysys_var->current_mutex)
961
One by effect of this that one can only use wait_for_condition with
962
condition variables that are guranteed to not disapper (freed) even if this
966
pthread_mutex_unlock(mutex.native_handle());
967
pthread_mutex_lock(&mysys_var->mutex);
779
968
mysys_var->current_mutex= 0;
780
969
mysys_var->current_cond= 0;
781
970
set_proc_info(saved_proc_info);
971
pthread_mutex_unlock(&mysys_var->mutex);
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;
1002
char *table_name= table_list->table_name;
1005
safe_mutex_assert_owner(LOCK_open.native_handle());
1007
if (killed || !table)
1012
TableIdentifier identifier(table_list->db, table_list->table_name);
1013
if (open_unireg_entry(this, table, table_name, identifier))
1015
table->intern_close_table();
1017
If there was an error during opening of table (for example if it
1018
does not exist) '*table' object can be wiped out. To be able
1019
properly release name-lock in this case we should restore this
1020
object to its original state.
1026
share= table->getMutableShare();
1028
We want to prevent other connections from opening this table until end
1029
of statement as it is likely that modifications of table's metadata are
1030
not yet finished (for example CREATE TRIGGER have to change .TRG cursor,
1031
or we might want to drop table if CREATE TABLE ... SELECT fails).
1032
This also allows us to assume that no other connection will sneak in
1033
before we will get table-level lock on this table.
1035
share->resetVersion();
1036
table->in_use = this;
1040
table->setNext(open_tables);
1046
Table object should be already in Session::open_tables list so we just
1047
need to set Table::next correctly.
1049
table->setNext(orig_table.getNext());
1052
table->tablenr= current_tablenr++;
1053
table->used_fields= 0;
1054
table->const_table= 0;
1055
table->null_row= false;
1056
table->maybe_null= false;
1057
table->force_index= false;
1058
table->status= STATUS_NO_RECORD;
946
1224
table->query_id= getQueryId();
1229
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
1231
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
1236
If it's the first table from a list of tables used in a query,
1237
remember refresh_version (the version of open_cache state).
1238
If the version changes while we're opening the remaining tables,
1239
we will have to back off, close all the tables opened-so-far,
1240
and try to reopen them.
1242
Note-> refresh_version is currently changed only during FLUSH TABLES.
1246
version= refresh_version;
1248
else if ((version != refresh_version) &&
1249
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
1251
/* Someone did a refresh while thread was opening tables */
1259
Before we test the global cache, we test our local session cache.
1263
assert(false); /* Not implemented yet */
1267
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
1268
this is the normal use case.
1270
- try to find the table in the table cache.
1271
- if one of the discovered Table instances is name-locked
1272
(table->getShare()->version == 0) back off -- we have to wait
1273
until no one holds a name lock on the table.
1274
- if there is no such Table in the name cache, read the table definition
1275
and insert it into the cache.
1276
We perform all of the above under LOCK_open which currently protects
1277
the open cache (also known as table cache) and table definitions stored
1281
LOCK_open.lock(); /* Lock for FLUSH TABLES for open table */
1284
Actually try to find the table in the open_cache.
1285
The cache may contain several "Table" instances for the same
1286
physical table. The instances that are currently "in use" by
1287
some thread have their "in_use" member != NULL.
1288
There is no good reason for having more than one entry in the
1289
hash for the same physical table, except that we use this as
1290
an implicit "pending locks queue" - see
1291
wait_for_locked_table_names for details.
1293
ppp= get_open_cache().equal_range(key);
1296
for (TableOpenCache::const_iterator iter= ppp.first;
1297
iter != ppp.second; ++iter, table= NULL)
1299
table= (*iter).second;
1301
if (not table->in_use)
954
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
956
my_error(ER_TABLE_UNKNOWN, identifier);
961
If it's the first table from a list of tables used in a query,
962
remember refresh_version (the version of open_cache state).
963
If the version changes while we're opening the remaining tables,
964
we will have to back off, close all the tables opened-so-far,
965
and try to reopen them.
967
Note-> refresh_version is currently changed only during FLUSH TABLES.
1304
Here we flush tables marked for flush.
1305
Normally, table->getShare()->version contains the value of
1306
refresh_version from the moment when this table was
1307
(re-)opened and added to the cache.
1308
If since then we did (or just started) FLUSH TABLES
1309
statement, refresh_version has been increased.
1310
For "name-locked" Table instances, table->getShare()->version is set
1311
to 0 (see lock_table_name for details).
1312
In case there is a pending FLUSH TABLES or a name lock, we
1313
need to back off and re-start opening tables.
1314
If we do not back off now, we may dead lock in case of lock
1315
order mismatch with some other thread:
1316
c1-> name lock t1; -- sort of exclusive lock
1317
c2-> open t2; -- sort of shared lock
1318
c1-> name lock t2; -- blocks
1319
c2-> open t1; -- blocks
971
version= refresh_version;
973
else if ((version != refresh_version) &&
974
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
976
/* Someone did a refresh while thread was opening tables */
1321
if (table->needs_reopen_or_name_lock())
1323
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1325
/* Force close at once after usage */
1326
version= table->getShare()->getVersion();
1330
/* Avoid self-deadlocks by detecting self-dependencies. */
1331
if (table->open_placeholder && table->in_use == this)
1334
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getMutableShare()->getTableName());
1339
Back off, part 1: mark the table as "unused" for the
1340
purpose of name-locking by setting table->db_stat to 0. Do
1341
that only for the tables in this thread that have an old
1342
table->getShare()->version (this is an optimization (?)).
1343
table->db_stat == 0 signals wait_for_locked_table_names
1344
that the tables in question are not used any more. See
1345
table_is_used call for details.
1347
close_old_data_files(false, false);
1350
Back-off part 2: try to avoid "busy waiting" on the table:
1351
if the table is in use by some other thread, we suspend
1352
and wait till the operation is complete: when any
1353
operation that juggles with table->getShare()->version completes,
1354
it broadcasts COND_refresh condition variable.
1355
If 'old' table we met is in use by current thread we return
1356
without waiting since in this situation it's this thread
1357
which is responsible for broadcasting on COND_refresh
1358
(and this was done already in Session::close_old_data_files()).
1359
Good example of such situation is when we have statement
1360
that needs two instances of table and FLUSH TABLES comes
1361
after we open first instance but before we open second
1364
if (table->in_use != this)
1366
/* wait_for_conditionwill unlock LOCK_open for us */
1367
wait_for_condition(LOCK_open, COND_refresh);
1374
There is a refresh in progress for this table.
1375
Signal the caller that it has to try again.
984
Before we test the global cache, we test our local session cache.
988
assert(false); /* Not implemented yet */
992
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
993
this is the normal use case.
995
- try to find the table in the table cache.
996
- if one of the discovered Table instances is name-locked
997
(table->getShare()->version == 0) back off -- we have to wait
998
until no one holds a name lock on the table.
999
- if there is no such Table in the name cache, read the table definition
1000
and insert it into the cache.
1001
We perform all of the above under table::Cache::singleton().mutex() which currently protects
1002
the open cache (also known as table cache) and table definitions stored
1007
boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
1010
Actually try to find the table in the open_cache.
1011
The cache may contain several "Table" instances for the same
1012
physical table. The instances that are currently "in use" by
1013
some thread have their "in_use" member != NULL.
1014
There is no good reason for having more than one entry in the
1015
hash for the same physical table, except that we use this as
1016
an implicit "pending locks queue" - see
1017
wait_for_locked_table_names for details.
1019
ppp= table::getCache().equal_range(key);
1022
for (table::CacheMap::const_iterator iter= ppp.first;
1023
iter != ppp.second; ++iter, table= NULL)
1384
unused_tables.unlink(table);
1385
table->in_use= this;
1389
/* Insert a new Table instance into the open cache */
1391
/* Free cache if too big */
1392
unused_tables.cull();
1394
if (table_list->isCreate())
1396
TableIdentifier lock_table_identifier(table_list->db, table_list->table_name, message::Table::STANDARD);
1398
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1025
table= (*iter).second;
1027
if (not table->in_use)
1030
Here we flush tables marked for flush.
1031
Normally, table->getShare()->version contains the value of
1032
refresh_version from the moment when this table was
1033
(re-)opened and added to the cache.
1034
If since then we did (or just started) FLUSH TABLES
1035
statement, refresh_version has been increased.
1036
For "name-locked" Table instances, table->getShare()->version is set
1037
to 0 (see lock_table_name for details).
1038
In case there is a pending FLUSH TABLES or a name lock, we
1039
need to back off and re-start opening tables.
1040
If we do not back off now, we may dead lock in case of lock
1041
order mismatch with some other thread:
1042
c1-> name lock t1; -- sort of exclusive lock
1043
c2-> open t2; -- sort of shared lock
1044
c1-> name lock t2; -- blocks
1045
c2-> open t1; -- blocks
1401
Table to be created, so we need to create placeholder in table-cache.
1047
if (table->needs_reopen_or_name_lock())
1403
if (!(table= table_cache_insert_placeholder(table_list->db, table_list->table_name)))
1049
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1051
/* Force close at once after usage */
1052
version= table->getShare()->getVersion();
1056
/* Avoid self-deadlocks by detecting self-dependencies. */
1057
if (table->open_placeholder && table->in_use == this)
1059
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getShare()->getTableName());
1064
Back off, part 1: mark the table as "unused" for the
1065
purpose of name-locking by setting table->db_stat to 0. Do
1066
that only for the tables in this thread that have an old
1067
table->getShare()->version (this is an optimization (?)).
1068
table->db_stat == 0 signals wait_for_locked_table_names
1069
that the tables in question are not used any more. See
1070
table_is_used call for details.
1072
close_old_data_files(false, false);
1075
Back-off part 2: try to avoid "busy waiting" on the table:
1076
if the table is in use by some other thread, we suspend
1077
and wait till the operation is complete: when any
1078
operation that juggles with table->getShare()->version completes,
1079
it broadcasts COND_refresh condition variable.
1080
If 'old' table we met is in use by current thread we return
1081
without waiting since in this situation it's this thread
1082
which is responsible for broadcasting on COND_refresh
1083
(and this was done already in Session::close_old_data_files()).
1084
Good example of such situation is when we have statement
1085
that needs two instances of table and FLUSH TABLES comes
1086
after we open first instance but before we open second
1089
if (table->in_use != this)
1091
/* wait_for_conditionwill unlock table::Cache::singleton().mutex() for us */
1092
wait_for_condition(table::Cache::singleton().mutex(), COND_refresh);
1093
scopedLock.release();
1097
scopedLock.unlock();
1101
There is a refresh in progress for this table.
1102
Signal the caller that it has to try again.
1113
table::getUnused().unlink(static_cast<table::Concurrent *>(table));
1114
table->in_use= this;
1118
/* Insert a new Table instance into the open cache */
1120
/* Free cache if too big */
1121
table::getUnused().cull();
1123
if (table_list->isCreate())
1125
identifier::Table lock_table_identifier(table_list->getSchemaName(), table_list->getTableName(), message::Table::STANDARD);
1127
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1130
Table to be created, so we need to create placeholder in table-cache.
1132
if (!(table= table_cache_insert_placeholder(lock_table_identifier)))
1137
Link placeholder to the open tables list so it will be automatically
1138
removed once tables are closed. Also mark it so it won't be ignored
1139
by other trying to take name-lock.
1141
table->open_placeholder= true;
1142
table->setNext(open_tables);
1147
/* Table exists. Let us try to open it. */
1150
/* make a new table */
1152
table::Concurrent *new_table= new table::Concurrent;
1154
if (new_table == NULL)
1159
error= new_table->open_unireg_entry(this, alias, identifier);
1165
(void)table::Cache::singleton().insert(new_table);
1172
table->setNext(open_tables); /* Link into simple list */
1175
table->reginfo.lock_type= TL_READ; /* Assume read */
1409
Link placeholder to the open tables list so it will be automatically
1410
removed once tables are closed. Also mark it so it won't be ignored
1411
by other trying to take name-lock.
1413
table->open_placeholder= true;
1414
table->setNext(open_tables);
1420
/* Table exists. Let us try to open it. */
1423
/* make a new table */
1431
error= open_unireg_entry(this, table, alias, identifier);
1438
(void)add_table(table);
1444
table->setNext(open_tables); /* Link into simple list */
1447
table->reginfo.lock_type= TL_READ; /* Assume read */
1178
1450
assert(table->getShare()->getTableCount() > 0 || table->getShare()->getType() != message::Table::STANDARD);
1452
if (lex->need_correct_ident())
1453
table->alias_name_used= my_strcasecmp(table_alias_charset,
1454
table->getMutableShare()->getTableName(), alias);
1180
1455
/* Fix alias if table name changes */
1181
1456
if (strcmp(table->getAlias(), alias))
1183
table->setAlias(alias);
1458
uint32_t length=(uint32_t) strlen(alias)+1;
1459
table->alias= (char*) realloc((char*) table->alias, length);
1460
memcpy((void*) table->alias, alias, length);
1186
1463
/* These variables are also set in reopen_table() */
3932
4469
unireg support functions
3933
4470
*****************************************************************************/
4473
Invalidate any cache entries that are for some DB
4476
remove_db_from_cache()
4477
db Database name. This will be in lower case if
4478
lower_case_table_name is set
4481
We can't use hash_delete when looping hash_elements. We mark them first
4482
and afterwards delete those marked unused.
4485
void remove_db_from_cache(const SchemaIdentifier &schema_identifier)
4487
safe_mutex_assert_owner(LOCK_open.native_handle());
4489
for (TableOpenCache::const_iterator iter= get_open_cache().begin();
4490
iter != get_open_cache().end();
4493
Table *table= (*iter).second;
4495
if (not schema_identifier.getPath().compare(table->getMutableShare()->getSchemaName()))
4497
table->getMutableShare()->resetVersion(); /* Free when thread is ready */
4498
if (not table->in_use)
4499
unused_tables.relink(table);
4503
unused_tables.cullByVersion();
4508
Mark all entries with the table as deleted to force an reopen of the table
4510
The table will be closed (not stored in cache) by the current thread when
4511
close_thread_tables() is called.
4517
0 This thread now have exclusive access to this table and no other thread
4518
can access the table until close_thread_tables() is called.
4519
1 Table is in use by another thread
4522
bool remove_table_from_cache(Session *session, TableIdentifier &identifier, uint32_t flags)
4524
const TableIdentifier::Key &key(identifier.getKey());
4526
bool signalled= false;
4530
result= signalled= false;
4532
TableOpenCacheRange ppp;
4533
ppp= get_open_cache().equal_range(key);
4535
for (TableOpenCache::const_iterator iter= ppp.first;
4536
iter != ppp.second; ++iter)
4538
Table *table= (*iter).second;
4541
table->getMutableShare()->resetVersion(); /* Free when thread is ready */
4542
if (!(in_use=table->in_use))
4544
unused_tables.relink(table);
4546
else if (in_use != session)
4549
Mark that table is going to be deleted from cache. This will
4550
force threads that are in mysql_lock_tables() (but not yet
4551
in thr_multi_lock()) to abort it's locks, close all tables and retry
4553
in_use->some_tables_deleted= true;
4554
if (table->is_name_opened())
4559
Now we must abort all tables locks used by this thread
4560
as the thread may be waiting to get a lock for another table.
4561
Note that we need to hold LOCK_open while going through the
4562
list. So that the other thread cannot change it. The other
4563
thread must also hold LOCK_open whenever changing the
4564
open_tables list. Aborting the MERGE lock after a child was
4565
closed and before the parent is closed would be fatal.
4567
for (Table *session_table= in_use->open_tables;
4569
session_table= session_table->getNext())
4571
/* Do not handle locks of MERGE children. */
4572
if (session_table->db_stat) // If table is open
4573
signalled|= mysql_lock_abort_for_thread(session, session_table);
4577
result= result || (flags & RTFC_OWNED_BY_Session_FLAG);
4580
unused_tables.cullByVersion();
4582
/* Remove table from table definition cache if it's not in use */
4583
TableShare::release(identifier);
4585
if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG))
4588
Signal any thread waiting for tables to be freed to
4591
broadcast_refresh();
4592
if (!(flags & RTFC_CHECK_KILLED_FLAG) || !session->killed)
4595
if (likely(signalled))
4596
(void) pthread_cond_wait(COND_refresh.native_handle(), LOCK_open.native_handle());
4599
struct timespec abstime;
4601
It can happen that another thread has opened the
4602
table but has not yet locked any table at all. Since
4603
it can be locked waiting for a table that our thread
4604
has done LOCK Table x WRITE on previously, we need to
4605
ensure that the thread actually hears our signal
4606
before we go to sleep. Thus we wait for a short time
4607
and then we retry another loop in the
4608
remove_table_from_cache routine.
4610
set_timespec(abstime, 10);
4611
pthread_cond_timedwait(COND_refresh.native_handle(), LOCK_open.native_handle(), &abstime);