61
63
extern bool volatile shutdown_in_progress;
63
bool drizzle_rm_tmp_tables();
66
@defgroup Data_Dictionary Data Dictionary
69
Table *unused_tables; /* Used by mysql_test */
70
HASH open_cache; /* Used by mysql_test */
71
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
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,
72
196
const char *alias,
73
char *cache_key, uint32_t cache_key_length);
74
void free_cache_entry(void *entry);
197
TableIdentifier &identifier);
75
199
unsigned char *table_cache_key(const unsigned char *record,
80
203
unsigned char *table_cache_key(const unsigned char *record,
84
207
Table *entry=(Table*) record;
85
*length= entry->s->table_cache_key.length;
86
return (unsigned char*) entry->s->table_cache_key.str;
89
HASH *get_open_cache()
208
*length= entry->getShare()->getCacheKey().size();
209
return (unsigned char*) &entry->getShare()->getCacheKey()[0];
95
212
bool table_cache_init(void)
97
return hash_init(&open_cache, &my_charset_bin,
98
(size_t) table_cache_size+16,
99
0, 0, table_cache_key,
100
free_cache_entry, 0);
217
uint32_t cached_open_tables(void)
219
return get_open_cache().size();
103
222
void table_cache_free(void)
105
224
refresh_version++; // Force close of open tables
107
while (unused_tables)
108
hash_delete(&open_cache, (unsigned char*) unused_tables);
110
if (not open_cache.records) // Safety first
111
hash_free(&open_cache);
114
uint32_t cached_open_tables(void)
116
return open_cache.records;
226
unused_tables.clear();
227
get_open_cache().clear();
121
231
Close cursor handle, but leave the table in the table cache
241
339
bool result= false;
242
340
Session *session= this;
244
pthread_mutex_lock(&LOCK_open); /* Optionally lock for remove tables from open_cahe if not in use */
248
refresh_version++; // Force close of open tables
249
while (unused_tables)
250
hash_delete(&open_cache,(unsigned char*) unused_tables);
343
LOCK_open.lock(); /* Optionally lock for remove tables from open_cahe if not in use */
347
refresh_version++; // Force close of open tables
349
unused_tables.clear();
351
if (wait_for_refresh)
354
Other threads could wait in a loop in open_and_lock_tables(),
355
trying to lock one or more of our tables.
357
If they wait for the locks in thr_multi_lock(), their lock
358
request is aborted. They loop in open_and_lock_tables() and
359
enter open_table(). Here they notice the table is refreshed and
360
wait for COND_refresh. Then they loop again in
361
openTablesLock() and this time open_table() succeeds. At
362
this moment, if we (the FLUSH TABLES thread) are scheduled and
363
on another FLUSH TABLES enter close_cached_tables(), they could
364
awake while we sleep below, waiting for others threads (us) to
365
close their open tables. If this happens, the other threads
366
would find the tables unlocked. They would get the locks, one
367
after the other, and could do their destructive work. This is an
368
issue if we have LOCK TABLES in effect.
370
The problem is that the other threads passed all checks in
371
open_table() before we refresh the table.
373
The fix for this problem is to set some_tables_deleted for all
374
threads with open tables. These threads can still get their
375
locks, but will immediately release them again after checking
376
this variable. They will then loop in openTablesLock()
377
again. There they will wait until we update all tables version
380
Setting some_tables_deleted is done by remove_table_from_cache()
383
In other words (reviewer suggestion): You need this setting of
384
some_tables_deleted for the case when table was opened and all
385
related checks were passed before incrementing refresh_version
386
(which you already have) but attempt to lock the table happened
387
after the call to Session::close_old_data_files() i.e. after removal of
388
current thread locks.
390
for (TableOpenCache::const_iterator iter= get_open_cache().begin();
391
iter != get_open_cache().end();
394
Table *table= (*iter).second;
396
table->in_use->some_tables_deleted= false;
403
for (TableList *table= tables; table; table= table->next_local)
405
TableIdentifier identifier(table->db, table->table_name);
406
if (remove_table_from_cache(session, identifier,
407
RTFC_OWNED_BY_Session_FLAG))
413
wait_for_refresh= false; // Nothing to wait for
252
416
if (wait_for_refresh)
255
Other threads could wait in a loop in open_and_lock_tables(),
256
trying to lock one or more of our tables.
258
If they wait for the locks in thr_multi_lock(), their lock
259
request is aborted. They loop in open_and_lock_tables() and
260
enter open_table(). Here they notice the table is refreshed and
261
wait for COND_refresh. Then they loop again in
262
openTablesLock() and this time open_table() succeeds. At
263
this moment, if we (the FLUSH TABLES thread) are scheduled and
264
on another FLUSH TABLES enter close_cached_tables(), they could
265
awake while we sleep below, waiting for others threads (us) to
266
close their open tables. If this happens, the other threads
267
would find the tables unlocked. They would get the locks, one
268
after the other, and could do their destructive work. This is an
269
issue if we have LOCK TABLES in effect.
271
The problem is that the other threads passed all checks in
272
open_table() before we refresh the table.
274
The fix for this problem is to set some_tables_deleted for all
275
threads with open tables. These threads can still get their
276
locks, but will immediately release them again after checking
277
this variable. They will then loop in openTablesLock()
278
again. There they will wait until we update all tables version
281
Setting some_tables_deleted is done by remove_table_from_cache()
284
In other words (reviewer suggestion): You need this setting of
285
some_tables_deleted for the case when table was opened and all
286
related checks were passed before incrementing refresh_version
287
(which you already have) but attempt to lock the table happened
288
after the call to Session::close_old_data_files() i.e. after removal of
289
current thread locks.
419
If there is any table that has a lower refresh_version, wait until
420
this is closed (or this thread is killed) before returning
291
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
422
session->mysys_var->current_mutex= &LOCK_open;
423
session->mysys_var->current_cond= &COND_refresh;
424
session->set_proc_info("Flushing tables");
426
session->close_old_data_files();
429
/* Wait until all threads has closed all the tables we had locked */
430
while (found && ! session->killed)
293
Table *table=(Table*) hash_element(&open_cache,idx);
295
table->in_use->some_tables_deleted= false;
433
for (TableOpenCache::const_iterator iter= get_open_cache().begin();
434
iter != get_open_cache().end();
437
Table *table= (*iter).second;
438
/* Avoid a self-deadlock. */
439
if (table->in_use == session)
442
Note that we wait here only for tables which are actually open, and
443
not for placeholders with Table::open_placeholder set. Waiting for
444
latter will cause deadlock in the following scenario, for example:
446
conn1-> lock table t1 write;
447
conn2-> lock table t2 write;
448
conn1-> flush tables;
449
conn2-> flush tables;
451
It also does not make sense to wait for those of placeholders that
452
are employed by CREATE TABLE as in this case table simply does not
455
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
456
(table->open_placeholder && wait_for_placeholders)))
459
boost::mutex::scoped_lock scoped(LOCK_open, boost::adopt_lock_t());
460
COND_refresh.wait(scoped);
302
for (TableList *table= tables; table; table= table->next_local)
304
if (remove_table_from_cache(session, table->db, table->table_name,
305
RTFC_OWNED_BY_Session_FLAG))
309
wait_for_refresh= false; // Nothing to wait for
312
if (wait_for_refresh)
315
If there is any table that has a lower refresh_version, wait until
316
this is closed (or this thread is killed) before returning
318
session->mysys_var->current_mutex= &LOCK_open;
319
session->mysys_var->current_cond= &COND_refresh;
320
session->set_proc_info("Flushing tables");
322
session->close_old_data_files();
325
/* Wait until all threads has closed all the tables we had locked */
326
while (found && ! session->killed)
329
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
467
No other thread has the locked tables open; reopen them and get the
468
old locks. This should always succeed (unless some external process
469
has removed the tables)
471
result= session->reopen_tables(true, true);
473
/* Set version for table */
474
for (Table *table= session->open_tables; table ; table= table->getNext())
331
Table *table=(Table*) hash_element(&open_cache,idx);
332
/* Avoid a self-deadlock. */
333
if (table->in_use == session)
336
Note that we wait here only for tables which are actually open, and
337
not for placeholders with Table::open_placeholder set. Waiting for
338
latter will cause deadlock in the following scenario, for example:
340
conn1: lock table t1 write;
341
conn2: lock table t2 write;
345
It also does not make sense to wait for those of placeholders that
346
are employed by CREATE TABLE as in this case table simply does not
477
Preserve the version (0) of write locked tables so that a impending
478
global read lock won't sneak in.
349
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
350
(table->open_placeholder && wait_for_placeholders)))
353
pthread_cond_wait(&COND_refresh,&LOCK_open);
480
if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
481
table->getMutableShare()->refreshVersion();
359
No other thread has the locked tables open; reopen them and get the
360
old locks. This should always succeed (unless some external process
361
has removed the tables)
363
result= session->reopen_tables(true, true);
365
/* Set version for table */
366
for (Table *table= session->open_tables; table ; table= table->next)
369
Preserve the version (0) of write locked tables so that a impending
370
global read lock won't sneak in.
372
if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
373
table->s->version= refresh_version;
377
pthread_mutex_unlock(&LOCK_open);
379
488
if (wait_for_refresh)
381
pthread_mutex_lock(&session->mysys_var->mutex);
490
boost::mutex::scoped_lock scopedLock(session->mysys_var->mutex);
382
491
session->mysys_var->current_mutex= 0;
383
492
session->mysys_var->current_cond= 0;
384
493
session->set_proc_info(0);
385
pthread_mutex_unlock(&session->mysys_var->mutex);
689
void Session::doGetTableNames(const SchemaIdentifier &schema_identifier,
690
std::set<std::string>& set_of_names)
692
for (Table *table= temporary_tables ; table ; table= table->getNext())
694
if (schema_identifier.compare(table->getShare()->getSchemaName()))
696
set_of_names.insert(table->getShare()->getTableName());
589
701
void Session::doGetTableNames(CachedDirectory &,
590
const std::string& db_name,
591
std::set<std::string>& set_of_names)
593
for (Table *table= temporary_tables ; table ; table= table->next)
595
if (not db_name.compare(table->s->getSchemaName()))
597
set_of_names.insert(table->s->table_name.str);
602
int Session::doGetTableDefinition(const char *,
604
const char *table_name_arg,
606
message::Table *table_proto)
608
for (Table *table= temporary_tables ; table ; table= table->next)
610
if (table->s->tmp_table == TEMP_TABLE)
612
if (not strcmp(db_arg, table->s->getSchemaName()))
614
if (not strcmp(table_name_arg, table->s->table_name.str))
617
table_proto->CopyFrom(*(table->s->getTableProto()));
702
const SchemaIdentifier &schema_identifier,
703
std::set<std::string> &set_of_names)
705
doGetTableNames(schema_identifier, set_of_names);
708
void Session::doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
709
TableIdentifiers &set_of_identifiers)
711
for (Table *table= temporary_tables ; table ; table= table->getNext())
713
if (schema_identifier.compare(table->getShare()->getSchemaName()))
715
set_of_identifiers.push_back(TableIdentifier(table->getShare()->getSchemaName(),
716
table->getShare()->getTableName(),
717
table->getShare()->getPath()));
722
void Session::doGetTableIdentifiers(CachedDirectory &,
723
const SchemaIdentifier &schema_identifier,
724
TableIdentifiers &set_of_identifiers)
726
doGetTableIdentifiers(schema_identifier, set_of_identifiers);
729
bool Session::doDoesTableExist(const TableIdentifier &identifier)
731
for (Table *table= temporary_tables ; table ; table= table->getNext())
733
if (table->getShare()->getType() == message::Table::TEMPORARY)
735
if (identifier.getKey() == table->getShare()->getCacheKey())
745
int Session::doGetTableDefinition(const TableIdentifier &identifier,
746
message::Table &table_proto)
748
for (Table *table= temporary_tables ; table ; table= table->getNext())
750
if (table->getShare()->getType() == message::Table::TEMPORARY)
752
if (identifier.getKey() == table->getShare()->getCacheKey())
754
table_proto.CopyFrom(*(table->getShare()->getTableProto()));
1112
1220
if (table->query_id)
1114
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1222
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
1117
1225
table->query_id= getQueryId();
1122
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
1124
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
1129
If it's the first table from a list of tables used in a query,
1130
remember refresh_version (the version of open_cache state).
1131
If the version changes while we're opening the remaining tables,
1132
we will have to back off, close all the tables opened-so-far,
1133
and try to reopen them.
1135
Note-> refresh_version is currently changed only during FLUSH TABLES.
1138
version= refresh_version;
1139
else if ((version != refresh_version) &&
1140
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
1142
/* Someone did a refresh while thread was opening tables */
1150
Before we test the global cache, we test our local session cache.
1154
assert(false); /* Not implemented yet */
1158
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
1159
this is the normal use case.
1161
- try to find the table in the table cache.
1162
- if one of the discovered Table instances is name-locked
1163
(table->s->version == 0) back off -- we have to wait
1164
until no one holds a name lock on the table.
1165
- if there is no such Table in the name cache, read the table definition
1166
and insert it into the cache.
1167
We perform all of the above under LOCK_open which currently protects
1168
the open cache (also known as table cache) and table definitions stored
1172
pthread_mutex_lock(&LOCK_open); /* Lock for FLUSH TABLES for open table */
1175
Actually try to find the table in the open_cache.
1176
The cache may contain several "Table" instances for the same
1177
physical table. The instances that are currently "in use" by
1178
some thread have their "in_use" member != NULL.
1179
There is no good reason for having more than one entry in the
1180
hash for the same physical table, except that we use this as
1181
an implicit "pending locks queue" - see
1182
wait_for_locked_table_names for details.
1184
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
1186
table && table->in_use ;
1187
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
1233
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
1235
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
1191
Here we flush tables marked for flush.
1192
Normally, table->s->version contains the value of
1193
refresh_version from the moment when this table was
1194
(re-)opened and added to the cache.
1195
If since then we did (or just started) FLUSH TABLES
1196
statement, refresh_version has been increased.
1197
For "name-locked" Table instances, table->s->version is set
1198
to 0 (see lock_table_name for details).
1199
In case there is a pending FLUSH TABLES or a name lock, we
1200
need to back off and re-start opening tables.
1201
If we do not back off now, we may dead lock in case of lock
1202
order mismatch with some other thread:
1203
c1: name lock t1; -- sort of exclusive lock
1204
c2: open t2; -- sort of shared lock
1205
c1: name lock t2; -- blocks
1206
c2: open t1; -- blocks
1240
If it's the first table from a list of tables used in a query,
1241
remember refresh_version (the version of open_cache state).
1242
If the version changes while we're opening the remaining tables,
1243
we will have to back off, close all the tables opened-so-far,
1244
and try to reopen them.
1246
Note-> refresh_version is currently changed only during FLUSH TABLES.
1208
if (table->needs_reopen_or_name_lock())
1210
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1212
/* Force close at once after usage */
1213
version= table->s->version;
1217
/* Avoid self-deadlocks by detecting self-dependencies. */
1218
if (table->open_placeholder && table->in_use == this)
1220
pthread_mutex_unlock(&LOCK_open);
1221
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
1226
Back off, part 1: mark the table as "unused" for the
1227
purpose of name-locking by setting table->db_stat to 0. Do
1228
that only for the tables in this thread that have an old
1229
table->s->version (this is an optimization (?)).
1230
table->db_stat == 0 signals wait_for_locked_table_names
1231
that the tables in question are not used any more. See
1232
table_is_used call for details.
1234
close_old_data_files(false, false);
1237
Back-off part 2: try to avoid "busy waiting" on the table:
1238
if the table is in use by some other thread, we suspend
1239
and wait till the operation is complete: when any
1240
operation that juggles with table->s->version completes,
1241
it broadcasts COND_refresh condition variable.
1242
If 'old' table we met is in use by current thread we return
1243
without waiting since in this situation it's this thread
1244
which is responsible for broadcasting on COND_refresh
1245
(and this was done already in Session::close_old_data_files()).
1246
Good example of such situation is when we have statement
1247
that needs two instances of table and FLUSH TABLES comes
1248
after we open first instance but before we open second
1251
if (table->in_use != this)
1253
/* wait_for_conditionwill unlock LOCK_open for us */
1254
wait_for_condition(&LOCK_open, &COND_refresh);
1258
pthread_mutex_unlock(&LOCK_open);
1261
There is a refresh in progress for this table.
1262
Signal the caller that it has to try again.
1250
version= refresh_version;
1252
else if ((version != refresh_version) &&
1253
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
1255
/* Someone did a refresh while thread was opening tables */
1265
1257
*refresh= true;
1271
/* Unlink the table from "unused_tables" list. */
1272
if (table == unused_tables)
1274
unused_tables=unused_tables->next; // Remove from link
1275
if (table == unused_tables)
1276
unused_tables= NULL;
1278
table->prev->next=table->next; /* Remove from unused list */
1279
table->next->prev=table->prev;
1280
table->in_use= this;
1284
/* Insert a new Table instance into the open cache */
1286
/* Free cache if too big */
1287
while (open_cache.records > table_cache_size && unused_tables)
1288
hash_delete(&open_cache,(unsigned char*) unused_tables);
1290
if (table_list->create)
1292
TableIdentifier lock_table_identifier(table_list->db, table_list->table_name, STANDARD_TABLE);
1294
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1297
Table to be created, so we need to create placeholder in table-cache.
1299
if (!(table= table_cache_insert_placeholder(key, key_length)))
1301
pthread_mutex_unlock(&LOCK_open);
1305
Link placeholder to the open tables list so it will be automatically
1306
removed once tables are closed. Also mark it so it won't be ignored
1307
by other trying to take name-lock.
1309
table->open_placeholder= true;
1310
table->next= open_tables;
1312
pthread_mutex_unlock(&LOCK_open);
1316
/* Table exists. Let us try to open it. */
1319
/* make a new table */
1320
table= (Table *)malloc(sizeof(Table));
1323
pthread_mutex_unlock(&LOCK_open);
1327
error= open_unireg_entry(this, table, table_list, alias, key, key_length);
1331
pthread_mutex_unlock(&LOCK_open);
1334
my_hash_insert(&open_cache, (unsigned char*) table);
1337
pthread_mutex_unlock(&LOCK_open);
1340
table->next= open_tables; /* Link into simple list */
1343
table->reginfo.lock_type= TL_READ; /* Assume read */
1346
assert(table->s->ref_count > 0 || table->s->tmp_table != STANDARD_TABLE);
1263
Before we test the global cache, we test our local session cache.
1267
assert(false); /* Not implemented yet */
1271
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
1272
this is the normal use case.
1274
- try to find the table in the table cache.
1275
- if one of the discovered Table instances is name-locked
1276
(table->getShare()->version == 0) back off -- we have to wait
1277
until no one holds a name lock on the table.
1278
- if there is no such Table in the name cache, read the table definition
1279
and insert it into the cache.
1280
We perform all of the above under LOCK_open which currently protects
1281
the open cache (also known as table cache) and table definitions stored
1286
LOCK_open.lock(); /* Lock for FLUSH TABLES for open table */
1289
Actually try to find the table in the open_cache.
1290
The cache may contain several "Table" instances for the same
1291
physical table. The instances that are currently "in use" by
1292
some thread have their "in_use" member != NULL.
1293
There is no good reason for having more than one entry in the
1294
hash for the same physical table, except that we use this as
1295
an implicit "pending locks queue" - see
1296
wait_for_locked_table_names for details.
1298
ppp= get_open_cache().equal_range(key);
1301
for (TableOpenCache::const_iterator iter= ppp.first;
1302
iter != ppp.second; ++iter, table= NULL)
1304
table= (*iter).second;
1306
if (not table->in_use)
1309
Here we flush tables marked for flush.
1310
Normally, table->getShare()->version contains the value of
1311
refresh_version from the moment when this table was
1312
(re-)opened and added to the cache.
1313
If since then we did (or just started) FLUSH TABLES
1314
statement, refresh_version has been increased.
1315
For "name-locked" Table instances, table->getShare()->version is set
1316
to 0 (see lock_table_name for details).
1317
In case there is a pending FLUSH TABLES or a name lock, we
1318
need to back off and re-start opening tables.
1319
If we do not back off now, we may dead lock in case of lock
1320
order mismatch with some other thread:
1321
c1-> name lock t1; -- sort of exclusive lock
1322
c2-> open t2; -- sort of shared lock
1323
c1-> name lock t2; -- blocks
1324
c2-> open t1; -- blocks
1326
if (table->needs_reopen_or_name_lock())
1328
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1330
/* Force close at once after usage */
1331
version= table->getShare()->getVersion();
1335
/* Avoid self-deadlocks by detecting self-dependencies. */
1336
if (table->open_placeholder && table->in_use == this)
1339
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getMutableShare()->getTableName());
1344
Back off, part 1: mark the table as "unused" for the
1345
purpose of name-locking by setting table->db_stat to 0. Do
1346
that only for the tables in this thread that have an old
1347
table->getShare()->version (this is an optimization (?)).
1348
table->db_stat == 0 signals wait_for_locked_table_names
1349
that the tables in question are not used any more. See
1350
table_is_used call for details.
1352
close_old_data_files(false, false);
1355
Back-off part 2: try to avoid "busy waiting" on the table:
1356
if the table is in use by some other thread, we suspend
1357
and wait till the operation is complete: when any
1358
operation that juggles with table->getShare()->version completes,
1359
it broadcasts COND_refresh condition variable.
1360
If 'old' table we met is in use by current thread we return
1361
without waiting since in this situation it's this thread
1362
which is responsible for broadcasting on COND_refresh
1363
(and this was done already in Session::close_old_data_files()).
1364
Good example of such situation is when we have statement
1365
that needs two instances of table and FLUSH TABLES comes
1366
after we open first instance but before we open second
1369
if (table->in_use != this)
1371
/* wait_for_conditionwill unlock LOCK_open for us */
1372
wait_for_condition(LOCK_open, COND_refresh);
1379
There is a refresh in progress for this table.
1380
Signal the caller that it has to try again.
1389
unused_tables.unlink(table);
1390
table->in_use= this;
1394
/* Insert a new Table instance into the open cache */
1396
/* Free cache if too big */
1397
unused_tables.cull();
1399
if (table_list->isCreate())
1401
TableIdentifier lock_table_identifier(table_list->db, table_list->table_name, message::Table::STANDARD);
1403
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1406
Table to be created, so we need to create placeholder in table-cache.
1408
if (!(table= table_cache_insert_placeholder(table_list->db, table_list->table_name)))
1414
Link placeholder to the open tables list so it will be automatically
1415
removed once tables are closed. Also mark it so it won't be ignored
1416
by other trying to take name-lock.
1418
table->open_placeholder= true;
1419
table->setNext(open_tables);
1425
/* Table exists. Let us try to open it. */
1428
/* make a new table */
1436
error= open_unireg_entry(this, table, alias, identifier);
1443
(void)add_table(table);
1450
table->setNext(open_tables); /* Link into simple list */
1453
table->reginfo.lock_type= TL_READ; /* Assume read */
1456
assert(table->getShare()->getTableCount() > 0 || table->getShare()->getType() != message::Table::STANDARD);
1348
1458
if (lex->need_correct_ident())
1349
1459
table->alias_name_used= my_strcasecmp(table_alias_charset,
1350
table->s->table_name.str, alias);
1460
table->getMutableShare()->getTableName(), alias);
1351
1461
/* Fix alias if table name changes */
1352
if (strcmp(table->alias, alias))
1462
if (strcmp(table->getAlias(), alias))
1354
1464
uint32_t length=(uint32_t) strlen(alias)+1;
1355
1465
table->alias= (char*) realloc((char*) table->alias, length);