241
170
bool result= false;
242
171
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);
174
boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* 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)
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.
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
identifier::Table identifier(table->getSchemaName(), table->getTableName());
237
if (table::Cache::singleton().removeTable(session, identifier,
238
RTFC_OWNED_BY_Session_FLAG))
244
wait_for_refresh= false; // Nothing to wait for
252
247
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.
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
291
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
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())
293
Table *table=(Table*) hash_element(&open_cache,idx);
295
table->in_use->some_tables_deleted= false;
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
COND_refresh.wait(scopedLock);
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++)
296
No other thread has the locked tables open; reopen them and get the
297
old locks. This should always succeed (unless some external process
298
has removed the tables)
300
result= session->reopen_tables();
302
/* Set version for table */
303
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
306
Preserve the version (0) of write locked tables so that a impending
307
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);
309
if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
310
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
315
if (wait_for_refresh)
381
pthread_mutex_lock(&session->mysys_var->mutex);
317
boost_unique_lock_t scopedLock(session->mysys_var->mutex);
382
318
session->mysys_var->current_mutex= 0;
383
319
session->mysys_var->current_cond= 0;
384
320
session->set_proc_info(0);
385
pthread_mutex_unlock(&session->mysys_var->mutex);
589
void Session::doGetTableNames(SchemaIdentifier &schema_identifier,
590
std::set<std::string>& set_of_names)
521
void Open_tables_state::doGetTableNames(const identifier::Schema &schema_identifier,
522
std::set<std::string>& set_of_names)
592
for (Table *table= temporary_tables ; table ; table= table->next)
524
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
594
if (schema_identifier.compare(table->s->getSchemaName()))
526
if (schema_identifier.compare(table->getShare()->getSchemaName()))
596
set_of_names.insert(table->s->table_name.str);
528
set_of_names.insert(table->getShare()->getTableName());
601
void Session::doGetTableNames(CachedDirectory &,
602
SchemaIdentifier &schema_identifier,
603
std::set<std::string> &set_of_names)
533
void Open_tables_state::doGetTableNames(CachedDirectory &,
534
const identifier::Schema &schema_identifier,
535
std::set<std::string> &set_of_names)
605
537
doGetTableNames(schema_identifier, set_of_names);
608
void Session::doGetTableIdentifiers(SchemaIdentifier &schema_identifier,
609
TableIdentifiers &set_of_identifiers)
540
void Open_tables_state::doGetTableIdentifiers(const identifier::Schema &schema_identifier,
541
identifier::Table::vector &set_of_identifiers)
611
for (Table *table= temporary_tables ; table ; table= table->next)
543
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
613
if (schema_identifier.compare(table->s->getSchemaName()))
545
if (schema_identifier.compare(table->getShare()->getSchemaName()))
615
set_of_identifiers.push_back(TableIdentifier(table->getShare()->getSchemaName(),
547
set_of_identifiers.push_back(identifier::Table(table->getShare()->getSchemaName(),
616
548
table->getShare()->getTableName(),
617
549
table->getShare()->getPath()));
622
void Session::doGetTableIdentifiers(CachedDirectory &,
623
SchemaIdentifier &schema_identifier,
624
TableIdentifiers &set_of_identifiers)
554
void Open_tables_state::doGetTableIdentifiers(CachedDirectory &,
555
const identifier::Schema &schema_identifier,
556
identifier::Table::vector &set_of_identifiers)
626
558
doGetTableIdentifiers(schema_identifier, set_of_identifiers);
629
bool Session::doDoesTableExist(TableIdentifier &identifier)
561
bool Open_tables_state::doDoesTableExist(const identifier::Table &identifier)
631
for (Table *table= temporary_tables ; table ; table= table->next)
563
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
633
if (table->s->tmp_table == message::Table::TEMPORARY)
565
if (table->getShare()->getType() == message::Table::TEMPORARY)
635
if (identifier.compare(table->s->getSchemaName(), table->s->table_name.str))
567
if (identifier.getKey() == table->getShare()->getCacheKey())
864
747
cond Condition to wait for
867
void Session::wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond)
750
void Session::wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond)
869
752
/* Wait until the current table is up to date */
870
753
const char *saved_proc_info;
871
mysys_var->current_mutex= mutex;
872
mysys_var->current_cond= cond;
754
mysys_var->current_mutex= &mutex;
755
mysys_var->current_cond= &cond;
873
756
saved_proc_info= get_proc_info();
874
757
set_proc_info("Waiting for table");
876
(void) pthread_cond_wait(cond, mutex);
879
We must unlock mutex first to avoid deadlock becasue conditions are
880
sent to this thread by doing locks in the following order:
881
lock(mysys_var->mutex)
882
lock(mysys_var->current_mutex)
884
One by effect of this that one can only use wait_for_condition with
885
condition variables that are guranteed to not disapper (freed) even if this
889
pthread_mutex_unlock(mutex);
890
pthread_mutex_lock(&mysys_var->mutex);
760
We must unlock mutex first to avoid deadlock becasue conditions are
761
sent to this thread by doing locks in the following order:
762
lock(mysys_var->mutex)
763
lock(mysys_var->current_mutex)
765
One by effect of this that one can only use wait_for_condition with
766
condition variables that are guranteed to not disapper (freed) even if this
769
boost_unique_lock_t scopedLock(mutex, boost::adopt_lock_t());
772
cond.wait(scopedLock);
775
boost_unique_lock_t mysys_scopedLock(mysys_var->mutex);
891
776
mysys_var->current_mutex= 0;
892
777
mysys_var->current_cond= 0;
893
778
set_proc_info(saved_proc_info);
894
pthread_mutex_unlock(&mysys_var->mutex);
899
Open table which is already name-locked by this thread.
902
reopen_name_locked_table()
903
session Thread handle
904
table_list TableList object for table to be open, TableList::table
905
member should point to Table object which was used for
907
link_in true - if Table object for table to be opened should be
908
linked into Session::open_tables list.
909
false - placeholder used for name-locking is already in
910
this list so we only need to preserve Table::next
914
This function assumes that its caller already acquired LOCK_open mutex.
921
bool Session::reopen_name_locked_table(TableList* table_list, bool link_in)
923
Table *table= table_list->table;
925
char *table_name= table_list->table_name;
928
safe_mutex_assert_owner(&LOCK_open);
930
if (killed || !table)
935
if (open_unireg_entry(this, table, table_list, table_name,
936
table->s->table_cache_key.str,
937
table->s->table_cache_key.length))
939
table->intern_close_table();
941
If there was an error during opening of table (for example if it
942
does not exist) '*table' object can be wiped out. To be able
943
properly release name-lock in this case we should restore this
944
object to its original state.
952
We want to prevent other connections from opening this table until end
953
of statement as it is likely that modifications of table's metadata are
954
not yet finished (for example CREATE TRIGGER have to change .TRG cursor,
955
or we might want to drop table if CREATE TABLE ... SELECT fails).
956
This also allows us to assume that no other connection will sneak in
957
before we will get table-level lock on this table.
960
table->in_use = this;
964
table->next= open_tables;
970
Table object should be already in Session::open_tables list so we just
971
need to set Table::next correctly.
973
table->next= orig_table.next;
976
table->tablenr= current_tablenr++;
977
table->used_fields= 0;
978
table->const_table= 0;
979
table->null_row= false;
980
table->maybe_null= false;
981
table->force_index= false;
982
table->status= STATUS_NO_RECORD;
1169
938
if (table->query_id)
1171
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
940
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
1174
943
table->query_id= getQueryId();
1179
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
1181
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
1186
If it's the first table from a list of tables used in a query,
1187
remember refresh_version (the version of open_cache state).
1188
If the version changes while we're opening the remaining tables,
1189
we will have to back off, close all the tables opened-so-far,
1190
and try to reopen them.
1192
Note-> refresh_version is currently changed only during FLUSH TABLES.
1195
version= refresh_version;
1196
else if ((version != refresh_version) &&
1197
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
1199
/* Someone did a refresh while thread was opening tables */
1207
Before we test the global cache, we test our local session cache.
1211
assert(false); /* Not implemented yet */
1215
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
1216
this is the normal use case.
1218
- try to find the table in the table cache.
1219
- if one of the discovered Table instances is name-locked
1220
(table->s->version == 0) back off -- we have to wait
1221
until no one holds a name lock on the table.
1222
- if there is no such Table in the name cache, read the table definition
1223
and insert it into the cache.
1224
We perform all of the above under LOCK_open which currently protects
1225
the open cache (also known as table cache) and table definitions stored
1229
pthread_mutex_lock(&LOCK_open); /* Lock for FLUSH TABLES for open table */
1232
Actually try to find the table in the open_cache.
1233
The cache may contain several "Table" instances for the same
1234
physical table. The instances that are currently "in use" by
1235
some thread have their "in_use" member != NULL.
1236
There is no good reason for having more than one entry in the
1237
hash for the same physical table, except that we use this as
1238
an implicit "pending locks queue" - see
1239
wait_for_locked_table_names for details.
1241
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
1243
table && table->in_use ;
1244
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
951
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
953
my_error(ER_TABLE_UNKNOWN, identifier);
1248
Here we flush tables marked for flush.
1249
Normally, table->s->version contains the value of
1250
refresh_version from the moment when this table was
1251
(re-)opened and added to the cache.
1252
If since then we did (or just started) FLUSH TABLES
1253
statement, refresh_version has been increased.
1254
For "name-locked" Table instances, table->s->version is set
1255
to 0 (see lock_table_name for details).
1256
In case there is a pending FLUSH TABLES or a name lock, we
1257
need to back off and re-start opening tables.
1258
If we do not back off now, we may dead lock in case of lock
1259
order mismatch with some other thread:
1260
c1: name lock t1; -- sort of exclusive lock
1261
c2: open t2; -- sort of shared lock
1262
c1: name lock t2; -- blocks
1263
c2: open t1; -- blocks
958
If it's the first table from a list of tables used in a query,
959
remember refresh_version (the version of open_cache state).
960
If the version changes while we're opening the remaining tables,
961
we will have to back off, close all the tables opened-so-far,
962
and try to reopen them.
964
Note-> refresh_version is currently changed only during FLUSH TABLES.
1265
if (table->needs_reopen_or_name_lock())
1267
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1269
/* Force close at once after usage */
1270
version= table->s->version;
1274
/* Avoid self-deadlocks by detecting self-dependencies. */
1275
if (table->open_placeholder && table->in_use == this)
1277
pthread_mutex_unlock(&LOCK_open);
1278
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
1283
Back off, part 1: mark the table as "unused" for the
1284
purpose of name-locking by setting table->db_stat to 0. Do
1285
that only for the tables in this thread that have an old
1286
table->s->version (this is an optimization (?)).
1287
table->db_stat == 0 signals wait_for_locked_table_names
1288
that the tables in question are not used any more. See
1289
table_is_used call for details.
1291
close_old_data_files(false, false);
1294
Back-off part 2: try to avoid "busy waiting" on the table:
1295
if the table is in use by some other thread, we suspend
1296
and wait till the operation is complete: when any
1297
operation that juggles with table->s->version completes,
1298
it broadcasts COND_refresh condition variable.
1299
If 'old' table we met is in use by current thread we return
1300
without waiting since in this situation it's this thread
1301
which is responsible for broadcasting on COND_refresh
1302
(and this was done already in Session::close_old_data_files()).
1303
Good example of such situation is when we have statement
1304
that needs two instances of table and FLUSH TABLES comes
1305
after we open first instance but before we open second
1308
if (table->in_use != this)
1310
/* wait_for_conditionwill unlock LOCK_open for us */
1311
wait_for_condition(&LOCK_open, &COND_refresh);
1315
pthread_mutex_unlock(&LOCK_open);
1318
There is a refresh in progress for this table.
1319
Signal the caller that it has to try again.
968
version= refresh_version;
970
else if ((version != refresh_version) &&
971
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
973
/* Someone did a refresh while thread was opening tables */
1328
/* Unlink the table from "unused_tables" list. */
1329
if (table == unused_tables)
1331
unused_tables=unused_tables->next; // Remove from link
1332
if (table == unused_tables)
1333
unused_tables= NULL;
981
Before we test the global cache, we test our local session cache.
985
assert(false); /* Not implemented yet */
1335
table->prev->next=table->next; /* Remove from unused list */
1336
table->next->prev=table->prev;
1337
table->in_use= this;
1341
/* Insert a new Table instance into the open cache */
1343
/* Free cache if too big */
1344
while (open_cache.records > table_cache_size && unused_tables)
1345
hash_delete(&open_cache,(unsigned char*) unused_tables);
1347
if (table_list->create)
989
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
990
this is the normal use case.
992
- try to find the table in the table cache.
993
- if one of the discovered Table instances is name-locked
994
(table->getShare()->version == 0) back off -- we have to wait
995
until no one holds a name lock on the table.
996
- if there is no such Table in the name cache, read the table definition
997
and insert it into the cache.
998
We perform all of the above under table::Cache::singleton().mutex() which currently protects
999
the open cache (also known as table cache) and table definitions stored
1349
TableIdentifier lock_table_identifier(table_list->db, table_list->table_name, message::Table::STANDARD);
1351
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1004
boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
1007
Actually try to find the table in the open_cache.
1008
The cache may contain several "Table" instances for the same
1009
physical table. The instances that are currently "in use" by
1010
some thread have their "in_use" member != NULL.
1011
There is no good reason for having more than one entry in the
1012
hash for the same physical table, except that we use this as
1013
an implicit "pending locks queue" - see
1014
wait_for_locked_table_names for details.
1016
ppp= table::getCache().equal_range(key);
1019
for (table::CacheMap::const_iterator iter= ppp.first;
1020
iter != ppp.second; ++iter, table= NULL)
1022
table= (*iter).second;
1024
if (not table->in_use)
1354
Table to be created, so we need to create placeholder in table-cache.
1027
Here we flush tables marked for flush.
1028
Normally, table->getShare()->version contains the value of
1029
refresh_version from the moment when this table was
1030
(re-)opened and added to the cache.
1031
If since then we did (or just started) FLUSH TABLES
1032
statement, refresh_version has been increased.
1033
For "name-locked" Table instances, table->getShare()->version is set
1034
to 0 (see lock_table_name for details).
1035
In case there is a pending FLUSH TABLES or a name lock, we
1036
need to back off and re-start opening tables.
1037
If we do not back off now, we may dead lock in case of lock
1038
order mismatch with some other thread:
1039
c1-> name lock t1; -- sort of exclusive lock
1040
c2-> open t2; -- sort of shared lock
1041
c1-> name lock t2; -- blocks
1042
c2-> open t1; -- blocks
1356
if (!(table= table_cache_insert_placeholder(key, key_length)))
1044
if (table->needs_reopen_or_name_lock())
1358
pthread_mutex_unlock(&LOCK_open);
1046
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1048
/* Force close at once after usage */
1049
version= table->getShare()->getVersion();
1053
/* Avoid self-deadlocks by detecting self-dependencies. */
1054
if (table->open_placeholder && table->in_use == this)
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);
1090
scopedLock.release();
1094
scopedLock.unlock();
1098
There is a refresh in progress for this table.
1099
Signal the caller that it has to try again.
1362
Link placeholder to the open tables list so it will be automatically
1363
removed once tables are closed. Also mark it so it won't be ignored
1364
by other trying to take name-lock.
1366
table->open_placeholder= true;
1367
table->next= open_tables;
1369
pthread_mutex_unlock(&LOCK_open);
1373
/* Table exists. Let us try to open it. */
1376
/* make a new table */
1377
table= (Table *)malloc(sizeof(Table));
1380
pthread_mutex_unlock(&LOCK_open);
1384
error= open_unireg_entry(this, table, table_list, alias, key, key_length);
1388
pthread_mutex_unlock(&LOCK_open);
1391
my_hash_insert(&open_cache, (unsigned char*) table);
1394
pthread_mutex_unlock(&LOCK_open);
1397
table->next= open_tables; /* Link into simple list */
1400
table->reginfo.lock_type= TL_READ; /* Assume read */
1403
assert(table->s->ref_count > 0 || table->s->tmp_table != message::Table::STANDARD);
1405
if (lex->need_correct_ident())
1406
table->alias_name_used= my_strcasecmp(table_alias_charset,
1407
table->s->table_name.str, alias);
1110
table::getUnused().unlink(static_cast<table::Concurrent *>(table));
1111
table->in_use= this;
1115
/* Insert a new Table instance into the open cache */
1117
/* Free cache if too big */
1118
table::getUnused().cull();
1120
if (table_list->isCreate())
1122
identifier::Table lock_table_identifier(table_list->getSchemaName(), table_list->getTableName(), message::Table::STANDARD);
1124
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1127
Table to be created, so we need to create placeholder in table-cache.
1129
if (!(table= table_cache_insert_placeholder(lock_table_identifier)))
1134
Link placeholder to the open tables list so it will be automatically
1135
removed once tables are closed. Also mark it so it won't be ignored
1136
by other trying to take name-lock.
1138
table->open_placeholder= true;
1139
table->setNext(open_tables);
1144
/* Table exists. Let us try to open it. */
1147
/* make a new table */
1149
table::Concurrent *new_table= new table::Concurrent;
1151
if (new_table == NULL)
1156
error= new_table->open_unireg_entry(this, alias, identifier);
1162
(void)table::Cache::singleton().insert(new_table);
1169
table->setNext(open_tables); /* Link into simple list */
1172
table->reginfo.lock_type= TL_READ; /* Assume read */
1175
assert(table->getShare()->getTableCount() > 0 || table->getShare()->getType() != message::Table::STANDARD);
1408
1177
/* Fix alias if table name changes */
1409
if (strcmp(table->alias, alias))
1178
if (strcmp(table->getAlias(), alias))
1411
uint32_t length=(uint32_t) strlen(alias)+1;
1412
table->alias= (char*) realloc((char*) table->alias, length);
1413
memcpy((void*) table->alias, alias, length);
1180
table->setAlias(alias);
1416
1183
/* These variables are also set in reopen_table() */
1926
1512
other threads trying to get the lock.
1929
void abort_locked_tables(Session *session,const char *db, const char *table_name)
1515
void abort_locked_tables(Session *session, const drizzled::identifier::Table &identifier)
1932
for (table= session->open_tables; table ; table= table->next)
1518
for (table= session->open_tables; table ; table= table->getNext())
1934
if (!strcmp(table->s->table_name.str, table_name) &&
1935
!strcmp(table->s->getSchemaName(), db))
1520
if (table->getShare()->getCacheKey() == identifier.getKey())
1937
1522
/* If MERGE child, forward lock handling to parent. */
1938
mysql_lock_abort(session, table);
1523
session->abortLock(table);
1945
Load a table definition from cursor and open unireg table
1949
session Thread handle
1950
entry Store open table definition here
1951
table_list TableList with db, table_name
1953
cache_key Key for share_cache
1954
cache_key_length length of cache_key
1957
Extra argument for open is taken from session->open_options
1958
One must have a lock on LOCK_open when calling this function
1965
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
1967
char *cache_key, uint32_t cache_key_length)
1971
uint32_t discover_retry_count= 0;
1973
safe_mutex_assert_owner(&LOCK_open);
1975
if (not (share= TableShare::getShare(session, table_list, cache_key,
1977
table_list->i_s_requested_object,
1981
while ((error= open_table_from_share(session, share, alias,
1982
(uint32_t) (HA_OPEN_KEYFILE |
1986
session->open_options, entry)))
1988
if (error == 7) // Table def changed
1990
share->version= 0; // Mark share as old
1991
if (discover_retry_count++) // Retry once
1996
Here we should wait until all threads has released the table.
1997
For now we do one retry. This may cause a deadlock if there
1998
is other threads waiting for other tables used by this thread.
2000
Proper fix would be to if the second retry failed:
2001
- Mark that table def changed
2002
- Return from open table
2003
- Close all tables used by this thread
2004
- Start waiting that the share is released
2005
- Retry by opening all tables again
2010
To avoid deadlock, only wait for release if no one else is
2013
if (share->ref_count != 1)
2015
/* Free share and wait until it's released by all threads */
2016
TableShare::release(share);
2018
if (!session->killed)
2020
drizzle_reset_errors(session, 1); // Clear warnings
2021
session->clear_error(); // Clear error message
2033
TableShare::release(share);
2040
1532
Open all tables in list
2308
Table *Session::open_temporary_table(TableIdentifier &identifier,
1799
Table *Open_tables_state::open_temporary_table(const identifier::Table &identifier,
2311
Table *new_tmp_table;
2313
char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
2314
uint32_t key_length, path_length;
2315
TableList table_list;
2317
table_list.db= const_cast<char*>(identifier.getSchemaName().c_str());
2318
table_list.table_name= const_cast<char*>(identifier.getTableName().c_str());
2319
/* Create the cache_key for temporary tables */
2320
key_length= table_list.create_table_def_key(cache_key);
2321
path_length= identifier.getPath().length();
2323
if (!(new_tmp_table= (Table*) malloc(sizeof(*new_tmp_table) + sizeof(*share) +
2324
path_length + 1 + key_length)))
1802
assert(identifier.isTmp());
1805
table::Temporary *new_tmp_table= new table::Temporary(identifier.getType(),
1807
const_cast<char *>(const_cast<identifier::Table&>(identifier).getPath().c_str()),
1808
static_cast<uint32_t>(identifier.getPath().length()));
1809
if (not new_tmp_table)
2327
share= (TableShare*) (new_tmp_table+1);
2328
tmp_path= (char*) (share+1);
2329
saved_cache_key= strcpy(tmp_path, identifier.getPath().c_str())+path_length+1;
2330
memcpy(saved_cache_key, cache_key, key_length);
2332
share->init(saved_cache_key, key_length, strchr(saved_cache_key, '\0')+1, tmp_path);
2335
1813
First open the share, and then open the table from the share we just opened.
2337
if (open_table_def(*this, identifier, share) ||
2338
open_table_from_share(this, share, identifier.getTableName().c_str(),
2339
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
1815
if (new_tmp_table->getMutableShare()->open_table_def(*static_cast<Session *>(this), identifier) ||
1816
new_tmp_table->getMutableShare()->open_table_from_share(static_cast<Session *>(this), identifier, identifier.getTableName().c_str(),
1817
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2344
1822
/* No need to lock share->mutex as this is not needed for tmp tables */
2345
share->free_table_share();
2346
free((char*) new_tmp_table);
1823
delete new_tmp_table->getMutableShare();
1824
delete new_tmp_table;
2350
1829
new_tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
2351
share->tmp_table= message::Table::TEMPORARY;
2353
1831
if (link_in_list)
2355
1833
/* growing temp list at the head */
2356
new_tmp_table->next= this->temporary_tables;
2357
if (new_tmp_table->next)
2358
new_tmp_table->next->prev= new_tmp_table;
1834
new_tmp_table->setNext(this->temporary_tables);
1835
if (new_tmp_table->getNext())
1837
new_tmp_table->getNext()->setPrev(new_tmp_table);
2359
1839
this->temporary_tables= new_tmp_table;
2360
this->temporary_tables->prev= 0;
1840
this->temporary_tables->setPrev(0);
2362
1842
new_tmp_table->pos_in_table_list= 0;
4445
3929
unireg support functions
4446
3930
*****************************************************************************/
4449
Invalidate any cache entries that are for some DB
4452
remove_db_from_cache()
4453
db Database name. This will be in lower case if
4454
lower_case_table_name is set
4457
We can't use hash_delete when looping hash_elements. We mark them first
4458
and afterwards delete those marked unused.
4461
void remove_db_from_cache(SchemaIdentifier &schema_identifier)
4463
safe_mutex_assert_owner(&LOCK_open);
4465
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
4467
Table *table=(Table*) hash_element(&open_cache,idx);
4468
if (not schema_identifier.getPath().compare(table->s->getSchemaName()))
4470
table->s->version= 0L; /* Free when thread is ready */
4471
if (not table->in_use)
4472
relink_unused(table);
4475
while (unused_tables && !unused_tables->s->version)
4476
hash_delete(&open_cache,(unsigned char*) unused_tables);
4481
Mark all entries with the table as deleted to force an reopen of the table
4483
The table will be closed (not stored in cache) by the current thread when
4484
close_thread_tables() is called.
4490
0 This thread now have exclusive access to this table and no other thread
4491
can access the table until close_thread_tables() is called.
4492
1 Table is in use by another thread
4495
bool remove_table_from_cache(Session *session, const char *db, const char *table_name,
4498
char key[MAX_DBKEY_LENGTH];
4500
uint32_t key_length;
4503
bool signalled= false;
4505
key_pos= strcpy(key_pos, db) + strlen(db);
4506
key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
4507
key_length= (uint32_t) (key_pos-key)+1;
4511
HASH_SEARCH_STATE state;
4512
result= signalled= false;
4514
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
4517
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
4522
table->s->version=0L; /* Free when thread is ready */
4523
if (!(in_use=table->in_use))
4525
relink_unused(table);
4527
else if (in_use != session)
4530
Mark that table is going to be deleted from cache. This will
4531
force threads that are in mysql_lock_tables() (but not yet
4532
in thr_multi_lock()) to abort it's locks, close all tables and retry
4534
in_use->some_tables_deleted= true;
4535
if (table->is_name_opened())
4540
Now we must abort all tables locks used by this thread
4541
as the thread may be waiting to get a lock for another table.
4542
Note that we need to hold LOCK_open while going through the
4543
list. So that the other thread cannot change it. The other
4544
thread must also hold LOCK_open whenever changing the
4545
open_tables list. Aborting the MERGE lock after a child was
4546
closed and before the parent is closed would be fatal.
4548
for (Table *session_table= in_use->open_tables;
4550
session_table= session_table->next)
4552
/* Do not handle locks of MERGE children. */
4553
if (session_table->db_stat) // If table is open
4554
signalled|= mysql_lock_abort_for_thread(session, session_table);
4558
result= result || (flags & RTFC_OWNED_BY_Session_FLAG);
4560
while (unused_tables && !unused_tables->s->version)
4561
hash_delete(&open_cache,(unsigned char*) unused_tables);
4563
/* Remove table from table definition cache if it's not in use */
4564
TableShare::release(key, key_length);
4566
if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG))
4569
Signal any thread waiting for tables to be freed to
4572
broadcast_refresh();
4573
if (!(flags & RTFC_CHECK_KILLED_FLAG) || !session->killed)
4576
if (likely(signalled))
4577
(void) pthread_cond_wait(&COND_refresh, &LOCK_open);
4580
struct timespec abstime;
4582
It can happen that another thread has opened the
4583
table but has not yet locked any table at all. Since
4584
it can be locked waiting for a table that our thread
4585
has done LOCK Table x WRITE on previously, we need to
4586
ensure that the thread actually hears our signal
4587
before we go to sleep. Thus we wait for a short time
4588
and then we retry another loop in the
4589
remove_table_from_cache routine.
4591
set_timespec(abstime, 10);
4592
pthread_cond_timedwait(&COND_refresh, &LOCK_open, &abstime);