170
241
bool result= false;
171
242
Session *session= this;
174
table::Cache::singleton().mutex().lock(); /* Optionally lock for remove tables from open_cahe if not in use */
178
refresh_version++; // Force close of open tables
180
table::getUnused().clear();
182
if (wait_for_refresh)
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);
252
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.
291
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
293
Table *table=(Table*) hash_element(&open_cache,idx);
295
table->in_use->some_tables_deleted= false;
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++)
331
Table *table=(Table*) hash_element(&open_cache,idx);
332
/* Avoid a self-deadlock. */
333
if (table->in_use == session)
185
Other threads could wait in a loop in open_and_lock_tables(),
186
trying to lock one or more of our tables.
188
If they wait for the locks in thr_multi_lock(), their lock
189
request is aborted. They loop in open_and_lock_tables() and
190
enter open_table(). Here they notice the table is refreshed and
191
wait for COND_refresh. Then they loop again in
192
openTablesLock() and this time open_table() succeeds. At
193
this moment, if we (the FLUSH TABLES thread) are scheduled and
194
on another FLUSH TABLES enter close_cached_tables(), they could
195
awake while we sleep below, waiting for others threads (us) to
196
close their open tables. If this happens, the other threads
197
would find the tables unlocked. They would get the locks, one
198
after the other, and could do their destructive work. This is an
199
issue if we have LOCK TABLES in effect.
201
The problem is that the other threads passed all checks in
202
open_table() before we refresh the table.
204
The fix for this problem is to set some_tables_deleted for all
205
threads with open tables. These threads can still get their
206
locks, but will immediately release them again after checking
207
this variable. They will then loop in openTablesLock()
208
again. There they will wait until we update all tables version
211
Setting some_tables_deleted is done by table::Cache::singleton().removeTable()
214
In other words (reviewer suggestion): You need this setting of
215
some_tables_deleted for the case when table was opened and all
216
related checks were passed before incrementing refresh_version
217
(which you already have) but attempt to lock the table happened
218
after the call to Session::close_old_data_files() i.e. after removal of
219
current thread locks.
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
221
for (table::CacheMap::const_iterator iter= table::getCache().begin();
222
iter != table::getCache().end();
225
Table *table= (*iter).second;
227
table->in_use->some_tables_deleted= false;
234
for (TableList *table= tables; table; table= table->next_local)
236
TableIdentifier identifier(table->getSchemaName(), table->getTableName());
237
if (table::Cache::singleton().removeTable(session, identifier,
238
RTFC_OWNED_BY_Session_FLAG))
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);
244
wait_for_refresh= false; // Nothing to wait for
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);
247
if (wait_for_refresh)
365
/* Set version for table */
366
for (Table *table= session->open_tables; table ; table= table->next)
250
If there is any table that has a lower refresh_version, wait until
251
this is closed (or this thread is killed) before returning
253
session->mysys_var->current_mutex= &table::Cache::singleton().mutex();
254
session->mysys_var->current_cond= &COND_refresh;
255
session->set_proc_info("Flushing tables");
257
session->close_old_data_files();
260
/* Wait until all threads has closed all the tables we had locked */
261
while (found && ! session->getKilled())
264
for (table::CacheMap::const_iterator iter= table::getCache().begin();
265
iter != table::getCache().end();
268
Table *table= (*iter).second;
269
/* Avoid a self-deadlock. */
270
if (table->in_use == session)
273
Note that we wait here only for tables which are actually open, and
274
not for placeholders with Table::open_placeholder set. Waiting for
275
latter will cause deadlock in the following scenario, for example:
277
conn1-> lock table t1 write;
278
conn2-> lock table t2 write;
279
conn1-> flush tables;
280
conn2-> flush tables;
282
It also does not make sense to wait for those of placeholders that
283
are employed by CREATE TABLE as in this case table simply does not
286
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
287
(table->open_placeholder && wait_for_placeholders)))
290
boost_unique_lock_t scoped(table::Cache::singleton().mutex(), boost::adopt_lock_t());
291
COND_refresh.wait(scoped);
298
No other thread has the locked tables open; reopen them and get the
299
old locks. This should always succeed (unless some external process
300
has removed the tables)
302
result= session->reopen_tables(true, true);
304
/* Set version for table */
305
for (Table *table= session->open_tables; table ; table= table->getNext())
308
Preserve the version (0) of write locked tables so that a impending
309
global read lock won't sneak in.
311
if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
312
table->getMutableShare()->refreshVersion();
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;
316
table::Cache::singleton().mutex().unlock();
377
pthread_mutex_unlock(&LOCK_open);
319
379
if (wait_for_refresh)
321
boost_unique_lock_t scopedLock(session->mysys_var->mutex);
381
pthread_mutex_lock(&session->mysys_var->mutex);
322
382
session->mysys_var->current_mutex= 0;
323
383
session->mysys_var->current_cond= 0;
324
384
session->set_proc_info(0);
385
pthread_mutex_unlock(&session->mysys_var->mutex);
520
void Open_tables_state::doGetTableNames(const SchemaIdentifier &schema_identifier,
521
std::set<std::string>& set_of_names)
523
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
525
if (schema_identifier.compare(table->getShare()->getSchemaName()))
527
set_of_names.insert(table->getShare()->getTableName());
532
void Open_tables_state::doGetTableNames(CachedDirectory &,
533
const SchemaIdentifier &schema_identifier,
534
std::set<std::string> &set_of_names)
536
doGetTableNames(schema_identifier, set_of_names);
539
void Open_tables_state::doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
540
TableIdentifier::vector &set_of_identifiers)
542
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
544
if (schema_identifier.compare(table->getShare()->getSchemaName()))
546
set_of_identifiers.push_back(TableIdentifier(table->getShare()->getSchemaName(),
547
table->getShare()->getTableName(),
548
table->getShare()->getPath()));
553
void Open_tables_state::doGetTableIdentifiers(CachedDirectory &,
554
const SchemaIdentifier &schema_identifier,
555
TableIdentifier::vector &set_of_identifiers)
557
doGetTableIdentifiers(schema_identifier, set_of_identifiers);
560
bool Open_tables_state::doDoesTableExist(const TableIdentifier &identifier)
562
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
564
if (table->getShare()->getType() == message::Table::TEMPORARY)
566
if (identifier.getKey() == table->getShare()->getCacheKey())
576
int Open_tables_state::doGetTableDefinition(const TableIdentifier &identifier,
577
message::Table &table_proto)
579
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
581
if (table->getShare()->getType() == message::Table::TEMPORARY)
583
if (identifier.getKey() == table->getShare()->getCacheKey())
585
table_proto.CopyFrom(*(table->getShare()->getTableProto()));
589
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()));
746
811
cond Condition to wait for
749
void Session::wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond)
814
void Session::wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond)
751
816
/* Wait until the current table is up to date */
752
817
const char *saved_proc_info;
753
mysys_var->current_mutex= &mutex;
754
mysys_var->current_cond= &cond;
818
mysys_var->current_mutex= mutex;
819
mysys_var->current_cond= cond;
755
820
saved_proc_info= get_proc_info();
756
821
set_proc_info("Waiting for table");
759
We must unlock mutex first to avoid deadlock becasue conditions are
760
sent to this thread by doing locks in the following order:
761
lock(mysys_var->mutex)
762
lock(mysys_var->current_mutex)
764
One by effect of this that one can only use wait_for_condition with
765
condition variables that are guranteed to not disapper (freed) even if this
768
boost_unique_lock_t scopedLock(mutex, boost::adopt_lock_t());
771
cond.wait(scopedLock);
774
boost_unique_lock_t mysys_scopedLock(mysys_var->mutex);
823
(void) pthread_cond_wait(cond, mutex);
826
We must unlock mutex first to avoid deadlock becasue conditions are
827
sent to this thread by doing locks in the following order:
828
lock(mysys_var->mutex)
829
lock(mysys_var->current_mutex)
831
One by effect of this that one can only use wait_for_condition with
832
condition variables that are guranteed to not disapper (freed) even if this
836
pthread_mutex_unlock(mutex);
837
pthread_mutex_lock(&mysys_var->mutex);
775
838
mysys_var->current_mutex= 0;
776
839
mysys_var->current_cond= 0;
777
840
set_proc_info(saved_proc_info);
841
pthread_mutex_unlock(&mysys_var->mutex);
846
Open table which is already name-locked by this thread.
849
reopen_name_locked_table()
850
session Thread handle
851
table_list TableList object for table to be open, TableList::table
852
member should point to Table object which was used for
854
link_in true - if Table object for table to be opened should be
855
linked into Session::open_tables list.
856
false - placeholder used for name-locking is already in
857
this list so we only need to preserve Table::next
861
This function assumes that its caller already acquired LOCK_open mutex.
868
bool Session::reopen_name_locked_table(TableList* table_list, bool link_in)
870
Table *table= table_list->table;
872
char *table_name= table_list->table_name;
875
safe_mutex_assert_owner(&LOCK_open);
877
if (killed || !table)
882
if (open_unireg_entry(this, table, table_list, table_name,
883
table->s->table_cache_key.str,
884
table->s->table_cache_key.length))
886
table->intern_close_table();
888
If there was an error during opening of table (for example if it
889
does not exist) '*table' object can be wiped out. To be able
890
properly release name-lock in this case we should restore this
891
object to its original state.
899
We want to prevent other connections from opening this table until end
900
of statement as it is likely that modifications of table's metadata are
901
not yet finished (for example CREATE TRIGGER have to change .TRG cursor,
902
or we might want to drop table if CREATE TABLE ... SELECT fails).
903
This also allows us to assume that no other connection will sneak in
904
before we will get table-level lock on this table.
907
table->in_use = this;
911
table->next= open_tables;
917
Table object should be already in Session::open_tables list so we just
918
need to set Table::next correctly.
920
table->next= orig_table.next;
923
table->tablenr= current_tablenr++;
924
table->used_fields= 0;
925
table->const_table= 0;
926
table->null_row= false;
927
table->maybe_null= false;
928
table->force_index= false;
929
table->status= STATUS_NO_RECORD;
937
1112
if (table->query_id)
939
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
1114
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
942
1117
table->query_id= getQueryId();
950
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
952
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->getSchemaName(), table_list->getTableName());
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,
957
If it's the first table from a list of tables used in a query,
958
remember refresh_version (the version of open_cache state).
959
If the version changes while we're opening the remaining tables,
960
we will have to back off, close all the tables opened-so-far,
961
and try to reopen them.
963
Note-> refresh_version is currently changed only during FLUSH TABLES.
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
967
version= refresh_version;
969
else if ((version != refresh_version) &&
970
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
972
/* Someone did a refresh while thread was opening tables */
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.
980
Before we test the global cache, we test our local session cache.
984
assert(false); /* Not implemented yet */
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;
988
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
989
this is the normal use case.
991
- try to find the table in the table cache.
992
- if one of the discovered Table instances is name-locked
993
(table->getShare()->version == 0) back off -- we have to wait
994
until no one holds a name lock on the table.
995
- if there is no such Table in the name cache, read the table definition
996
and insert it into the cache.
997
We perform all of the above under table::Cache::singleton().mutex() which currently protects
998
the open cache (also known as table cache) and table definitions stored
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)
1003
table::Cache::singleton().mutex().lock(); /* Lock for FLUSH TABLES for open table */
1006
Actually try to find the table in the open_cache.
1007
The cache may contain several "Table" instances for the same
1008
physical table. The instances that are currently "in use" by
1009
some thread have their "in_use" member != NULL.
1010
There is no good reason for having more than one entry in the
1011
hash for the same physical table, except that we use this as
1012
an implicit "pending locks queue" - see
1013
wait_for_locked_table_names for details.
1015
ppp= table::getCache().equal_range(key);
1018
for (table::CacheMap::const_iterator iter= ppp.first;
1019
iter != ppp.second; ++iter, table= NULL)
1292
TableIdentifier lock_table_identifier(table_list->db, table_list->table_name, STANDARD_TABLE);
1294
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1021
table= (*iter).second;
1023
if (not table->in_use)
1026
Here we flush tables marked for flush.
1027
Normally, table->getShare()->version contains the value of
1028
refresh_version from the moment when this table was
1029
(re-)opened and added to the cache.
1030
If since then we did (or just started) FLUSH TABLES
1031
statement, refresh_version has been increased.
1032
For "name-locked" Table instances, table->getShare()->version is set
1033
to 0 (see lock_table_name for details).
1034
In case there is a pending FLUSH TABLES or a name lock, we
1035
need to back off and re-start opening tables.
1036
If we do not back off now, we may dead lock in case of lock
1037
order mismatch with some other thread:
1038
c1-> name lock t1; -- sort of exclusive lock
1039
c2-> open t2; -- sort of shared lock
1040
c1-> name lock t2; -- blocks
1041
c2-> open t1; -- blocks
1297
Table to be created, so we need to create placeholder in table-cache.
1043
if (table->needs_reopen_or_name_lock())
1299
if (!(table= table_cache_insert_placeholder(key, key_length)))
1045
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1047
/* Force close at once after usage */
1048
version= table->getShare()->getVersion();
1052
/* Avoid self-deadlocks by detecting self-dependencies. */
1053
if (table->open_placeholder && table->in_use == this)
1055
table::Cache::singleton().mutex().unlock();
1056
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getShare()->getTableName());
1061
Back off, part 1: mark the table as "unused" for the
1062
purpose of name-locking by setting table->db_stat to 0. Do
1063
that only for the tables in this thread that have an old
1064
table->getShare()->version (this is an optimization (?)).
1065
table->db_stat == 0 signals wait_for_locked_table_names
1066
that the tables in question are not used any more. See
1067
table_is_used call for details.
1069
close_old_data_files(false, false);
1072
Back-off part 2: try to avoid "busy waiting" on the table:
1073
if the table is in use by some other thread, we suspend
1074
and wait till the operation is complete: when any
1075
operation that juggles with table->getShare()->version completes,
1076
it broadcasts COND_refresh condition variable.
1077
If 'old' table we met is in use by current thread we return
1078
without waiting since in this situation it's this thread
1079
which is responsible for broadcasting on COND_refresh
1080
(and this was done already in Session::close_old_data_files()).
1081
Good example of such situation is when we have statement
1082
that needs two instances of table and FLUSH TABLES comes
1083
after we open first instance but before we open second
1086
if (table->in_use != this)
1088
/* wait_for_conditionwill unlock table::Cache::singleton().mutex() for us */
1089
wait_for_condition(table::Cache::singleton().mutex(), COND_refresh);
1093
table::Cache::singleton().mutex().unlock();
1096
There is a refresh in progress for this table.
1097
Signal the caller that it has to try again.
1301
pthread_mutex_unlock(&LOCK_open);
1106
table::getUnused().unlink(static_cast<table::Concurrent *>(table));
1107
table->in_use= this;
1111
/* Insert a new Table instance into the open cache */
1113
/* Free cache if too big */
1114
table::getUnused().cull();
1116
if (table_list->isCreate())
1118
TableIdentifier lock_table_identifier(table_list->getSchemaName(), table_list->getTableName(), message::Table::STANDARD);
1120
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1123
Table to be created, so we need to create placeholder in table-cache.
1125
if (!(table= table_cache_insert_placeholder(lock_table_identifier)))
1127
table::Cache::singleton().mutex().unlock();
1131
Link placeholder to the open tables list so it will be automatically
1132
removed once tables are closed. Also mark it so it won't be ignored
1133
by other trying to take name-lock.
1135
table->open_placeholder= true;
1136
table->setNext(open_tables);
1138
table::Cache::singleton().mutex().unlock();
1142
/* Table exists. Let us try to open it. */
1145
/* make a new table */
1147
table::Concurrent *new_table= new table::Concurrent;
1149
if (new_table == NULL)
1151
table::Cache::singleton().mutex().unlock();
1155
error= new_table->open_unireg_entry(this, alias, identifier);
1159
table::Cache::singleton().mutex().unlock();
1162
(void)table::Cache::singleton().insert(new_table);
1166
table::Cache::singleton().mutex().unlock();
1170
table->setNext(open_tables); /* Link into simple list */
1173
table->reginfo.lock_type= TL_READ; /* Assume read */
1176
assert(table->getShare()->getTableCount() > 0 || table->getShare()->getType() != message::Table::STANDARD);
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);
1348
if (lex->need_correct_ident())
1349
table->alias_name_used= my_strcasecmp(table_alias_charset,
1350
table->s->table_name.str, alias);
1178
1351
/* Fix alias if table name changes */
1179
if (strcmp(table->getAlias(), alias))
1352
if (strcmp(table->alias, alias))
1181
table->setAlias(alias);
1354
uint32_t length=(uint32_t) strlen(alias)+1;
1355
table->alias= (char*) realloc((char*) table->alias, length);
1356
memcpy((void*) table->alias, alias, length);
1184
1359
/* These variables are also set in reopen_table() */
1551
1864
other threads trying to get the lock.
1554
void abort_locked_tables(Session *session, const drizzled::TableIdentifier &identifier)
1867
void abort_locked_tables(Session *session,const char *db, const char *table_name)
1557
for (table= session->open_tables; table ; table= table->getNext())
1870
for (table= session->open_tables; table ; table= table->next)
1559
if (table->getShare()->getCacheKey() == identifier.getKey())
1872
if (!strcmp(table->s->table_name.str, table_name) &&
1873
!strcmp(table->s->getSchemaName(), db))
1561
1875
/* If MERGE child, forward lock handling to parent. */
1562
session->abortLock(table);
1876
mysql_lock_abort(session, table);
1883
Load a table definition from cursor and open unireg table
1887
session Thread handle
1888
entry Store open table definition here
1889
table_list TableList with db, table_name
1891
cache_key Key for share_cache
1892
cache_key_length length of cache_key
1895
Extra argument for open is taken from session->open_options
1896
One must have a lock on LOCK_open when calling this function
1903
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
1905
char *cache_key, uint32_t cache_key_length)
1909
uint32_t discover_retry_count= 0;
1911
safe_mutex_assert_owner(&LOCK_open);
1913
if (!(share= TableShare::getShare(session, table_list, cache_key,
1915
table_list->i_s_requested_object,
1919
while ((error= open_table_from_share(session, share, alias,
1920
(uint32_t) (HA_OPEN_KEYFILE |
1924
session->open_options, entry)))
1926
if (error == 7) // Table def changed
1928
share->version= 0; // Mark share as old
1929
if (discover_retry_count++) // Retry once
1934
Here we should wait until all threads has released the table.
1935
For now we do one retry. This may cause a deadlock if there
1936
is other threads waiting for other tables used by this thread.
1938
Proper fix would be to if the second retry failed:
1939
- Mark that table def changed
1940
- Return from open table
1941
- Close all tables used by this thread
1942
- Start waiting that the share is released
1943
- Retry by opening all tables again
1948
To avoid deadlock, only wait for release if no one else is
1951
if (share->ref_count != 1)
1953
/* Free share and wait until it's released by all threads */
1954
TableShare::release(share);
1956
if (!session->killed)
1958
drizzle_reset_errors(session, 1); // Clear warnings
1959
session->clear_error(); // Clear error message
1971
TableShare::release(share);
1570
1978
Open all tables in list
1837
Table *Open_tables_state::open_temporary_table(const TableIdentifier &identifier,
2246
Table *Session::open_temporary_table(TableIdentifier &identifier,
1840
assert(identifier.isTmp());
1843
table::Temporary *new_tmp_table= new table::Temporary(identifier.getType(),
1845
const_cast<char *>(const_cast<TableIdentifier&>(identifier).getPath().c_str()),
1846
static_cast<uint32_t>(identifier.getPath().length()));
1847
if (not new_tmp_table)
2249
Table *new_tmp_table;
2251
char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
2252
uint32_t key_length, path_length;
2253
TableList table_list;
2255
table_list.db= (char*) identifier.getDBName();
2256
table_list.table_name= (char*) identifier.getTableName();
2257
/* Create the cache_key for temporary tables */
2258
key_length= table_list.create_table_def_key(cache_key);
2259
path_length= strlen(identifier.getPath());
2261
if (!(new_tmp_table= (Table*) malloc(sizeof(*new_tmp_table) + sizeof(*share) +
2262
path_length + 1 + key_length)))
2265
share= (TableShare*) (new_tmp_table+1);
2266
tmp_path= (char*) (share+1);
2267
saved_cache_key= strcpy(tmp_path, identifier.getPath())+path_length+1;
2268
memcpy(saved_cache_key, cache_key, key_length);
2270
share->init(saved_cache_key, key_length, strchr(saved_cache_key, '\0')+1, tmp_path);
1851
2273
First open the share, and then open the table from the share we just opened.
1853
if (new_tmp_table->getMutableShare()->open_table_def(*static_cast<Session *>(this), identifier) ||
1854
new_tmp_table->getMutableShare()->open_table_from_share(static_cast<Session *>(this), identifier, identifier.getTableName().c_str(),
1855
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2275
if (open_table_def(*this, share) ||
2276
open_table_from_share(this, share, identifier.getTableName(),
2277
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
1860
2282
/* No need to lock share->mutex as this is not needed for tmp tables */
1861
delete new_tmp_table->getMutableShare();
1862
delete new_tmp_table;
2283
share->free_table_share();
2284
free((char*) new_tmp_table);
1867
2288
new_tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
2289
share->tmp_table= TEMP_TABLE;
1869
2291
if (link_in_list)
1871
2293
/* growing temp list at the head */
1872
new_tmp_table->setNext(this->temporary_tables);
1873
if (new_tmp_table->getNext())
1875
new_tmp_table->getNext()->setPrev(new_tmp_table);
2294
new_tmp_table->next= this->temporary_tables;
2295
if (new_tmp_table->next)
2296
new_tmp_table->next->prev= new_tmp_table;
1877
2297
this->temporary_tables= new_tmp_table;
1878
this->temporary_tables->setPrev(0);
2298
this->temporary_tables->prev= 0;
1880
2300
new_tmp_table->pos_in_table_list= 0;
3958
4383
unireg support functions
3959
4384
*****************************************************************************/
4387
Invalidate any cache entries that are for some DB
4390
remove_db_from_cache()
4391
db Database name. This will be in lower case if
4392
lower_case_table_name is set
4395
We can't use hash_delete when looping hash_elements. We mark them first
4396
and afterwards delete those marked unused.
4399
void remove_db_from_cache(const std::string schema_name)
4401
safe_mutex_assert_owner(&LOCK_open);
4403
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
4405
Table *table=(Table*) hash_element(&open_cache,idx);
4406
if (not strcmp(table->s->getSchemaName(), schema_name.c_str()))
4408
table->s->version= 0L; /* Free when thread is ready */
4409
if (not table->in_use)
4410
relink_unused(table);
4413
while (unused_tables && !unused_tables->s->version)
4414
hash_delete(&open_cache,(unsigned char*) unused_tables);
4419
Mark all entries with the table as deleted to force an reopen of the table
4421
The table will be closed (not stored in cache) by the current thread when
4422
close_thread_tables() is called.
4428
0 This thread now have exclusive access to this table and no other thread
4429
can access the table until close_thread_tables() is called.
4430
1 Table is in use by another thread
4433
bool remove_table_from_cache(Session *session, const char *db, const char *table_name,
4436
char key[MAX_DBKEY_LENGTH];
4438
uint32_t key_length;
4441
bool signalled= false;
4443
key_pos= strcpy(key_pos, db) + strlen(db);
4444
key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
4445
key_length= (uint32_t) (key_pos-key)+1;
4449
HASH_SEARCH_STATE state;
4450
result= signalled= false;
4452
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
4455
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
4460
table->s->version=0L; /* Free when thread is ready */
4461
if (!(in_use=table->in_use))
4463
relink_unused(table);
4465
else if (in_use != session)
4468
Mark that table is going to be deleted from cache. This will
4469
force threads that are in mysql_lock_tables() (but not yet
4470
in thr_multi_lock()) to abort it's locks, close all tables and retry
4472
in_use->some_tables_deleted= true;
4473
if (table->is_name_opened())
4478
Now we must abort all tables locks used by this thread
4479
as the thread may be waiting to get a lock for another table.
4480
Note that we need to hold LOCK_open while going through the
4481
list. So that the other thread cannot change it. The other
4482
thread must also hold LOCK_open whenever changing the
4483
open_tables list. Aborting the MERGE lock after a child was
4484
closed and before the parent is closed would be fatal.
4486
for (Table *session_table= in_use->open_tables;
4488
session_table= session_table->next)
4490
/* Do not handle locks of MERGE children. */
4491
if (session_table->db_stat) // If table is open
4492
signalled|= mysql_lock_abort_for_thread(session, session_table);
4496
result= result || (flags & RTFC_OWNED_BY_Session_FLAG);
4498
while (unused_tables && !unused_tables->s->version)
4499
hash_delete(&open_cache,(unsigned char*) unused_tables);
4501
/* Remove table from table definition cache if it's not in use */
4502
TableShare::release(key, key_length);
4504
if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG))
4507
Signal any thread waiting for tables to be freed to
4510
broadcast_refresh();
4511
if (!(flags & RTFC_CHECK_KILLED_FLAG) || !session->killed)
4514
if (likely(signalled))
4515
(void) pthread_cond_wait(&COND_refresh, &LOCK_open);
4518
struct timespec abstime;
4520
It can happen that another thread has opened the
4521
table but has not yet locked any table at all. Since
4522
it can be locked waiting for a table that our thread
4523
has done LOCK Table x WRITE on previously, we need to
4524
ensure that the thread actually hears our signal
4525
before we go to sleep. Thus we wait for a short time
4526
and then we retry another loop in the
4527
remove_table_from_cache routine.
4529
set_timespec(abstime, 10);
4530
pthread_cond_timedwait(&COND_refresh, &LOCK_open, &abstime);