44
43
#include <drizzled/check_stack_overrun.h>
45
44
#include <drizzled/lock.h>
46
45
#include <drizzled/plugin/listen.h>
47
#include "drizzled/cached_directory.h"
48
#include <drizzled/field/epoch.h>
46
#include <mysys/cached_directory.h>
47
#include <drizzled/field/timestamp.h>
49
48
#include <drizzled/field/null.h>
50
#include "drizzled/sql_table.h"
51
#include "drizzled/global_charset_info.h"
52
#include "drizzled/pthread_globals.h"
53
#include "drizzled/internal/iocache.h"
54
#include "drizzled/drizzled.h"
55
#include "drizzled/plugin/authorization.h"
56
#include "drizzled/table/temporary.h"
57
#include "drizzled/table/placeholder.h"
58
#include "drizzled/table/unused.h"
59
#include "drizzled/plugin/storage_engine.h"
61
#include <drizzled/refresh_version.h>
49
#include "drizzled/memory/multi_malloc.h"
63
51
using namespace std;
68
extern bool volatile shutdown_in_progress;
52
using namespace drizzled;
54
bool drizzle_rm_tmp_tables();
57
@defgroup Data_Dictionary Data Dictionary
60
Table *unused_tables; /* Used by mysql_test */
61
HASH open_cache; /* Used by mysql_test */
62
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
64
char *cache_key, uint32_t cache_key_length);
67
void free_cache_entry(void *entry);
68
unsigned char *table_cache_key(const unsigned char *record,
75
unsigned char *table_cache_key(const unsigned char *record,
79
Table *entry=(Table*) record;
80
*length= entry->s->table_cache_key.length;
81
return (unsigned char*) entry->s->table_cache_key.str;
70
85
bool table_cache_init(void)
75
uint32_t cached_open_tables(void)
77
return table::getCache().size();
87
return hash_init(&open_cache, &my_charset_bin,
88
(size_t) table_cache_size+16,
89
0, 0, table_cache_key,
80
93
void table_cache_free(void)
82
95
refresh_version++; // Force close of open tables
84
table::getUnused().clear();
85
table::getCache().clear();
98
hash_delete(&open_cache,(unsigned char*) unused_tables);
100
if (!open_cache.records) // Safety first
101
hash_free(&open_cache);
104
uint32_t cached_open_tables(void)
106
return open_cache.records;
89
111
Close cursor handle, but leave the table in the table cache
106
128
void close_handle_and_leave_table_as_lock(Table *table)
130
TableShare *share, *old_share= table->s;
132
MEM_ROOT *mem_root= &table->mem_root;
108
134
assert(table->db_stat);
109
assert(table->getShare()->getType() == message::Table::STANDARD);
112
137
Make a local copy of the table share and free the current one.
113
138
This has to be done to ensure that the table share is removed from
114
139
the table defintion cache as soon as the last instance is removed
116
identifier::Table identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), message::Table::INTERNAL);
117
const identifier::Table::Key &key(identifier.getKey());
118
TableShare *share= new TableShare(identifier.getType(),
120
const_cast<char *>(key.vector()), static_cast<uint32_t>(table->getShare()->getCacheKeySize()));
141
if (multi_alloc_root(mem_root,
142
&share, sizeof(*share),
143
&key_buff, old_share->table_cache_key.length,
146
memset(share, 0, sizeof(*share));
147
share->set_table_cache_key(key_buff, old_share->table_cache_key.str,
148
old_share->table_cache_key.length);
149
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table()
122
152
table->cursor->close();
123
153
table->db_stat= 0; // Mark cursor closed
124
table::instance::release(table->getMutableShare());
125
table->setShare(share);
154
TableShare::release(table->s);
156
table->cursor->change_table_ptr(table, table->s);
162
Create a list for all open tables matching SQL expression
166
wild SQL like expression
169
One gets only a list of tables for which one has any kind of privilege.
170
db and table names are allocated in result struct, so one doesn't need
171
a lock on LOCK_open when traversing the return list.
177
bool list_open_tables(const char *db, const char *wild, bool(*func)(Table *table, open_table_list_st& open_list), Table *display)
179
vector<open_table_list_st> open_list;
180
vector<open_table_list_st>::iterator it;
181
open_table_list_st table;
183
/* What we really need is an optimization for knowing unique tables */
185
open_list.reserve(sizeof(open_table_list_st) * (open_cache.records % 2));
187
open_list.reserve(sizeof(open_table_list_st) * open_cache.records);
189
pthread_mutex_lock(&LOCK_open); /* List all open tables */
191
for (uint32_t idx= 0; idx < open_cache.records; idx++)
194
Table *entry=(Table*) hash_element(&open_cache,idx);
196
if (db && my_strcasecmp(system_charset_info, db, entry->s->db.str))
198
if (wild && wild_compare(entry->s->table_name.str, wild, 0))
201
for (it= open_list.begin(); it < open_list.end(); it++)
203
if (!(*it).table.compare(entry->s->table_name.str) &&
204
!(*it).db.compare(entry->s->db.str))
208
if (entry->locked_by_name)
220
table.db= entry->s->db.str;
221
table.table= entry->s->table_name.str;
222
open_list.push_back(table);
224
pthread_mutex_unlock(&LOCK_open);
226
for (it= open_list.begin(); it < open_list.end(); it++)
228
if (func(display, *it))
129
235
/*****************************************************************************
130
236
* Functions to free open table cache
173
306
bool result= false;
174
307
Session *session= this;
177
boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* Optionally lock for remove tables from open_cahe if not in use */
181
refresh_version++; // Force close of open tables
183
table::getUnused().clear();
185
if (wait_for_refresh)
309
pthread_mutex_lock(&LOCK_open); /* Optionally lock for remove tables from open_cahe if not in use */
313
refresh_version++; // Force close of open tables
314
while (unused_tables)
315
hash_delete(&open_cache,(unsigned char*) unused_tables);
317
if (wait_for_refresh)
320
Other threads could wait in a loop in open_and_lock_tables(),
321
trying to lock one or more of our tables.
323
If they wait for the locks in thr_multi_lock(), their lock
324
request is aborted. They loop in open_and_lock_tables() and
325
enter open_table(). Here they notice the table is refreshed and
326
wait for COND_refresh. Then they loop again in
327
openTablesLock() and this time open_table() succeeds. At
328
this moment, if we (the FLUSH TABLES thread) are scheduled and
329
on another FLUSH TABLES enter close_cached_tables(), they could
330
awake while we sleep below, waiting for others threads (us) to
331
close their open tables. If this happens, the other threads
332
would find the tables unlocked. They would get the locks, one
333
after the other, and could do their destructive work. This is an
334
issue if we have LOCK TABLES in effect.
336
The problem is that the other threads passed all checks in
337
open_table() before we refresh the table.
339
The fix for this problem is to set some_tables_deleted for all
340
threads with open tables. These threads can still get their
341
locks, but will immediately release them again after checking
342
this variable. They will then loop in openTablesLock()
343
again. There they will wait until we update all tables version
346
Setting some_tables_deleted is done by remove_table_from_cache()
349
In other words (reviewer suggestion): You need this setting of
350
some_tables_deleted for the case when table was opened and all
351
related checks were passed before incrementing refresh_version
352
(which you already have) but attempt to lock the table happened
353
after the call to Session::close_old_data_files() i.e. after removal of
354
current thread locks.
356
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
358
Table *table=(Table*) hash_element(&open_cache,idx);
360
table->in_use->some_tables_deleted= false;
367
for (TableList *table= tables; table; table= table->next_local)
369
if (remove_table_from_cache(session, table->db, table->table_name,
370
RTFC_OWNED_BY_Session_FLAG))
374
wait_for_refresh= false; // Nothing to wait for
377
if (wait_for_refresh)
380
If there is any table that has a lower refresh_version, wait until
381
this is closed (or this thread is killed) before returning
383
session->mysys_var->current_mutex= &LOCK_open;
384
session->mysys_var->current_cond= &COND_refresh;
385
session->set_proc_info("Flushing tables");
387
session->close_old_data_files();
390
/* Wait until all threads has closed all the tables we had locked */
391
while (found && ! session->killed)
394
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
396
Table *table=(Table*) hash_element(&open_cache,idx);
397
/* Avoid a self-deadlock. */
398
if (table->in_use == session)
188
Other threads could wait in a loop in open_and_lock_tables(),
189
trying to lock one or more of our tables.
191
If they wait for the locks in thr_multi_lock(), their lock
192
request is aborted. They loop in open_and_lock_tables() and
193
enter open_table(). Here they notice the table is refreshed and
194
wait for COND_refresh. Then they loop again in
195
openTablesLock() and this time open_table() succeeds. At
196
this moment, if we (the FLUSH TABLES thread) are scheduled and
197
on another FLUSH TABLES enter close_cached_tables(), they could
198
awake while we sleep below, waiting for others threads (us) to
199
close their open tables. If this happens, the other threads
200
would find the tables unlocked. They would get the locks, one
201
after the other, and could do their destructive work. This is an
202
issue if we have LOCK TABLES in effect.
204
The problem is that the other threads passed all checks in
205
open_table() before we refresh the table.
207
The fix for this problem is to set some_tables_deleted for all
208
threads with open tables. These threads can still get their
209
locks, but will immediately release them again after checking
210
this variable. They will then loop in openTablesLock()
211
again. There they will wait until we update all tables version
214
Setting some_tables_deleted is done by table::Cache::singleton().removeTable()
217
In other words (reviewer suggestion): You need this setting of
218
some_tables_deleted for the case when table was opened and all
219
related checks were passed before incrementing refresh_version
220
(which you already have) but attempt to lock the table happened
221
after the call to Session::close_old_data_files() i.e. after removal of
222
current thread locks.
401
Note that we wait here only for tables which are actually open, and
402
not for placeholders with Table::open_placeholder set. Waiting for
403
latter will cause deadlock in the following scenario, for example:
405
conn1: lock table t1 write;
406
conn2: lock table t2 write;
410
It also does not make sense to wait for those of placeholders that
411
are employed by CREATE TABLE as in this case table simply does not
224
for (table::CacheMap::const_iterator iter= table::getCache().begin();
225
iter != table::getCache().end();
228
Table *table= (*iter).second;
230
table->in_use->some_tables_deleted= false;
237
for (TableList *table= tables; table; table= table->next_local)
239
identifier::Table identifier(table->getSchemaName(), table->getTableName());
240
if (table::Cache::singleton().removeTable(session, identifier,
241
RTFC_OWNED_BY_Session_FLAG))
414
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
415
(table->open_placeholder && wait_for_placeholders)))
418
pthread_cond_wait(&COND_refresh,&LOCK_open);
247
wait_for_refresh= false; // Nothing to wait for
424
No other thread has the locked tables open; reopen them and get the
425
old locks. This should always succeed (unless some external process
426
has removed the tables)
428
result= session->reopen_tables(true, true);
250
if (wait_for_refresh)
430
/* Set version for table */
431
for (Table *table= session->open_tables; table ; table= table->next)
253
If there is any table that has a lower refresh_version, wait until
254
this is closed (or this thread is killed) before returning
256
session->mysys_var->current_mutex= &table::Cache::singleton().mutex();
257
session->mysys_var->current_cond= &COND_refresh;
258
session->set_proc_info("Flushing tables");
260
session->close_old_data_files();
263
/* Wait until all threads has closed all the tables we had locked */
264
while (found && ! session->getKilled())
267
for (table::CacheMap::const_iterator iter= table::getCache().begin();
268
iter != table::getCache().end();
271
Table *table= (*iter).second;
272
/* Avoid a self-deadlock. */
273
if (table->in_use == session)
276
Note that we wait here only for tables which are actually open, and
277
not for placeholders with Table::open_placeholder set. Waiting for
278
latter will cause deadlock in the following scenario, for example:
280
conn1-> lock table t1 write;
281
conn2-> lock table t2 write;
282
conn1-> flush tables;
283
conn2-> flush tables;
285
It also does not make sense to wait for those of placeholders that
286
are employed by CREATE TABLE as in this case table simply does not
289
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
290
(table->open_placeholder && wait_for_placeholders)))
293
COND_refresh.wait(scopedLock);
299
No other thread has the locked tables open; reopen them and get the
300
old locks. This should always succeed (unless some external process
301
has removed the tables)
303
result= session->reopen_tables();
305
/* Set version for table */
306
for (Table *table= session->open_tables; table ; table= table->getNext())
309
Preserve the version (0) of write locked tables so that a impending
310
global read lock won't sneak in.
312
if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
313
table->getMutableShare()->refreshVersion();
434
Preserve the version (0) of write locked tables so that a impending
435
global read lock won't sneak in.
437
if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
438
table->s->version= refresh_version;
442
pthread_mutex_unlock(&LOCK_open);
318
444
if (wait_for_refresh)
320
boost_unique_lock_t scopedLock(session->mysys_var->mutex);
446
pthread_mutex_lock(&session->mysys_var->mutex);
321
447
session->mysys_var->current_mutex= 0;
322
448
session->mysys_var->current_cond= 0;
323
449
session->set_proc_info(0);
450
pthread_mutex_unlock(&session->mysys_var->mutex);
524
void Open_tables_state::doGetTableNames(const identifier::Schema &schema_identifier,
525
std::set<std::string>& set_of_names)
527
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
529
if (schema_identifier.compare(table->getShare()->getSchemaName()))
531
set_of_names.insert(table->getShare()->getTableName());
536
void Open_tables_state::doGetTableNames(CachedDirectory &,
537
const identifier::Schema &schema_identifier,
538
std::set<std::string> &set_of_names)
540
doGetTableNames(schema_identifier, set_of_names);
543
void Open_tables_state::doGetTableIdentifiers(const identifier::Schema &schema_identifier,
544
identifier::Table::vector &set_of_identifiers)
546
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
548
if (schema_identifier.compare(table->getShare()->getSchemaName()))
550
set_of_identifiers.push_back(identifier::Table(table->getShare()->getSchemaName(),
551
table->getShare()->getTableName(),
552
table->getShare()->getPath()));
557
void Open_tables_state::doGetTableIdentifiers(CachedDirectory &,
558
const identifier::Schema &schema_identifier,
559
identifier::Table::vector &set_of_identifiers)
561
doGetTableIdentifiers(schema_identifier, set_of_identifiers);
564
bool Open_tables_state::doDoesTableExist(const identifier::Table &identifier)
566
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
568
if (table->getShare()->getType() == message::Table::TEMPORARY)
570
if (identifier.getKey() == table->getShare()->getCacheKey())
580
int Open_tables_state::doGetTableDefinition(const identifier::Table &identifier,
581
message::Table &table_proto)
583
for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
585
if (table->getShare()->getType() == message::Table::TEMPORARY)
587
if (identifier.getKey() == table->getShare()->getCacheKey())
589
table_proto.CopyFrom(*(table->getShare()->getTableMessage()));
599
Table *Open_tables_state::find_temporary_table(const identifier::Table &identifier)
601
for (Table *table= temporary_tables ; table ; table= table->getNext())
603
if (identifier.getKey() == table->getShare()->getCacheKey())
654
Table *Session::find_temporary_table(const char *new_db, const char *table_name)
656
char key[MAX_DBKEY_LENGTH];
660
key_length= TableShare::createKey(key, new_db, table_name);
662
for (table= temporary_tables ; table ; table= table->next)
664
if (table->s->table_cache_key.length == key_length &&
665
!memcmp(table->s->table_cache_key.str, key, key_length))
607
668
return NULL; // Not a temporary table
671
Table *Session::find_temporary_table(TableList *table_list)
673
return find_temporary_table(table_list->db, table_list->table_name);
612
678
Drop a temporary table.
750
834
cond Condition to wait for
753
void Session::wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond)
837
void Session::wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond)
755
839
/* Wait until the current table is up to date */
756
840
const char *saved_proc_info;
757
mysys_var->current_mutex= &mutex;
758
mysys_var->current_cond= &cond;
841
mysys_var->current_mutex= mutex;
842
mysys_var->current_cond= cond;
759
843
saved_proc_info= get_proc_info();
760
844
set_proc_info("Waiting for table");
763
We must unlock mutex first to avoid deadlock becasue conditions are
764
sent to this thread by doing locks in the following order:
765
lock(mysys_var->mutex)
766
lock(mysys_var->current_mutex)
768
One by effect of this that one can only use wait_for_condition with
769
condition variables that are guranteed to not disapper (freed) even if this
772
boost_unique_lock_t scopedLock(mutex, boost::adopt_lock_t());
775
cond.wait(scopedLock);
778
boost_unique_lock_t mysys_scopedLock(mysys_var->mutex);
846
(void) pthread_cond_wait(cond, mutex);
849
We must unlock mutex first to avoid deadlock becasue conditions are
850
sent to this thread by doing locks in the following order:
851
lock(mysys_var->mutex)
852
lock(mysys_var->current_mutex)
854
One by effect of this that one can only use wait_for_condition with
855
condition variables that are guranteed to not disapper (freed) even if this
859
pthread_mutex_unlock(mutex);
860
pthread_mutex_lock(&mysys_var->mutex);
779
861
mysys_var->current_mutex= 0;
780
862
mysys_var->current_cond= 0;
781
863
set_proc_info(saved_proc_info);
864
pthread_mutex_unlock(&mysys_var->mutex);
869
Open table which is already name-locked by this thread.
872
reopen_name_locked_table()
873
session Thread handle
874
table_list TableList object for table to be open, TableList::table
875
member should point to Table object which was used for
877
link_in true - if Table object for table to be opened should be
878
linked into Session::open_tables list.
879
false - placeholder used for name-locking is already in
880
this list so we only need to preserve Table::next
884
This function assumes that its caller already acquired LOCK_open mutex.
891
bool Session::reopen_name_locked_table(TableList* table_list, bool link_in)
893
Table *table= table_list->table;
895
char *table_name= table_list->table_name;
898
safe_mutex_assert_owner(&LOCK_open);
900
if (killed || !table)
905
if (open_unireg_entry(this, table, table_list, table_name,
906
table->s->table_cache_key.str,
907
table->s->table_cache_key.length))
909
table->intern_close_table();
911
If there was an error during opening of table (for example if it
912
does not exist) '*table' object can be wiped out. To be able
913
properly release name-lock in this case we should restore this
914
object to its original state.
922
We want to prevent other connections from opening this table until end
923
of statement as it is likely that modifications of table's metadata are
924
not yet finished (for example CREATE TRIGGER have to change .TRG cursor,
925
or we might want to drop table if CREATE TABLE ... SELECT fails).
926
This also allows us to assume that no other connection will sneak in
927
before we will get table-level lock on this table.
930
table->in_use = this;
934
table->next= open_tables;
940
Table object should be already in Session::open_tables list so we just
941
need to set Table::next correctly.
943
table->next= orig_table.next;
946
table->tablenr= current_tablenr++;
947
table->used_fields= 0;
948
table->const_table= 0;
949
table->null_row= false;
950
table->maybe_null= false;
951
table->force_index= false;
952
table->status= STATUS_NO_RECORD;
941
1135
if (table->query_id)
943
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
1137
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
946
table->query_id= getQueryId();
954
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
956
my_error(ER_TABLE_UNKNOWN, identifier);
1140
table->query_id= query_id;
1145
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
1147
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
1152
If it's the first table from a list of tables used in a query,
1153
remember refresh_version (the version of open_cache state).
1154
If the version changes while we're opening the remaining tables,
1155
we will have to back off, close all the tables opened-so-far,
1156
and try to reopen them.
1158
Note-> refresh_version is currently changed only during FLUSH TABLES.
1161
version= refresh_version;
1162
else if ((version != refresh_version) &&
1163
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
1165
/* Someone did a refresh while thread was opening tables */
1173
Before we test the global cache, we test our local session cache.
1177
assert(false); /* Not implemented yet */
1181
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
1182
this is the normal use case.
1184
- try to find the table in the table cache.
1185
- if one of the discovered Table instances is name-locked
1186
(table->s->version == 0) back off -- we have to wait
1187
until no one holds a name lock on the table.
1188
- if there is no such Table in the name cache, read the table definition
1189
and insert it into the cache.
1190
We perform all of the above under LOCK_open which currently protects
1191
the open cache (also known as table cache) and table definitions stored
1195
pthread_mutex_lock(&LOCK_open); /* Lock for FLUSH TABLES for open table */
1198
Actually try to find the table in the open_cache.
1199
The cache may contain several "Table" instances for the same
1200
physical table. The instances that are currently "in use" by
1201
some thread have their "in_use" member != NULL.
1202
There is no good reason for having more than one entry in the
1203
hash for the same physical table, except that we use this as
1204
an implicit "pending locks queue" - see
1205
wait_for_locked_table_names for details.
1207
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
1209
table && table->in_use ;
1210
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
961
If it's the first table from a list of tables used in a query,
962
remember refresh_version (the version of open_cache state).
963
If the version changes while we're opening the remaining tables,
964
we will have to back off, close all the tables opened-so-far,
965
and try to reopen them.
967
Note-> refresh_version is currently changed only during FLUSH TABLES.
1214
Here we flush tables marked for flush.
1215
Normally, table->s->version contains the value of
1216
refresh_version from the moment when this table was
1217
(re-)opened and added to the cache.
1218
If since then we did (or just started) FLUSH TABLES
1219
statement, refresh_version has been increased.
1220
For "name-locked" Table instances, table->s->version is set
1221
to 0 (see lock_table_name for details).
1222
In case there is a pending FLUSH TABLES or a name lock, we
1223
need to back off and re-start opening tables.
1224
If we do not back off now, we may dead lock in case of lock
1225
order mismatch with some other thread:
1226
c1: name lock t1; -- sort of exclusive lock
1227
c2: open t2; -- sort of shared lock
1228
c1: name lock t2; -- blocks
1229
c2: open t1; -- blocks
971
version= refresh_version;
973
else if ((version != refresh_version) &&
974
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
976
/* Someone did a refresh while thread was opening tables */
1231
if (table->needs_reopen_or_name_lock())
1233
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1235
/* Force close at once after usage */
1236
version= table->s->version;
1240
/* Avoid self-deadlocks by detecting self-dependencies. */
1241
if (table->open_placeholder && table->in_use == this)
1243
pthread_mutex_unlock(&LOCK_open);
1244
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
1249
Back off, part 1: mark the table as "unused" for the
1250
purpose of name-locking by setting table->db_stat to 0. Do
1251
that only for the tables in this thread that have an old
1252
table->s->version (this is an optimization (?)).
1253
table->db_stat == 0 signals wait_for_locked_table_names
1254
that the tables in question are not used any more. See
1255
table_is_used call for details.
1257
close_old_data_files(false, false);
1260
Back-off part 2: try to avoid "busy waiting" on the table:
1261
if the table is in use by some other thread, we suspend
1262
and wait till the operation is complete: when any
1263
operation that juggles with table->s->version completes,
1264
it broadcasts COND_refresh condition variable.
1265
If 'old' table we met is in use by current thread we return
1266
without waiting since in this situation it's this thread
1267
which is responsible for broadcasting on COND_refresh
1268
(and this was done already in Session::close_old_data_files()).
1269
Good example of such situation is when we have statement
1270
that needs two instances of table and FLUSH TABLES comes
1271
after we open first instance but before we open second
1274
if (table->in_use != this)
1276
/* wait_for_conditionwill unlock LOCK_open for us */
1277
wait_for_condition(&LOCK_open, &COND_refresh);
1281
pthread_mutex_unlock(&LOCK_open);
1284
There is a refresh in progress for this table.
1285
Signal the caller that it has to try again.
984
Before we test the global cache, we test our local session cache.
988
assert(false); /* Not implemented yet */
1294
/* Unlink the table from "unused_tables" list. */
1295
if (table == unused_tables)
1297
unused_tables=unused_tables->next; // Remove from link
1298
if (table == unused_tables)
1299
unused_tables= NULL;
992
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
993
this is the normal use case.
995
- try to find the table in the table cache.
996
- if one of the discovered Table instances is name-locked
997
(table->getShare()->version == 0) back off -- we have to wait
998
until no one holds a name lock on the table.
999
- if there is no such Table in the name cache, read the table definition
1000
and insert it into the cache.
1001
We perform all of the above under table::Cache::singleton().mutex() which currently protects
1002
the open cache (also known as table cache) and table definitions stored
1301
table->prev->next=table->next; /* Remove from unused list */
1302
table->next->prev=table->prev;
1303
table->in_use= this;
1307
/* Insert a new Table instance into the open cache */
1309
/* Free cache if too big */
1310
while (open_cache.records > table_cache_size && unused_tables)
1311
hash_delete(&open_cache,(unsigned char*) unused_tables);
1313
if (table_list->create)
1007
boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
1010
Actually try to find the table in the open_cache.
1011
The cache may contain several "Table" instances for the same
1012
physical table. The instances that are currently "in use" by
1013
some thread have their "in_use" member != NULL.
1014
There is no good reason for having more than one entry in the
1015
hash for the same physical table, except that we use this as
1016
an implicit "pending locks queue" - see
1017
wait_for_locked_table_names for details.
1019
ppp= table::getCache().equal_range(key);
1022
for (table::CacheMap::const_iterator iter= ppp.first;
1023
iter != ppp.second; ++iter, table= NULL)
1315
char path[FN_REFLEN];
1318
length= build_table_filename(path, sizeof(path),
1319
table_list->db, table_list->table_name,
1322
if (plugin::StorageEngine::getTableDefinition(*this, path, table_list->db, table_list->table_name, false) != EEXIST)
1025
table= (*iter).second;
1027
if (not table->in_use)
1030
Here we flush tables marked for flush.
1031
Normally, table->getShare()->version contains the value of
1032
refresh_version from the moment when this table was
1033
(re-)opened and added to the cache.
1034
If since then we did (or just started) FLUSH TABLES
1035
statement, refresh_version has been increased.
1036
For "name-locked" Table instances, table->getShare()->version is set
1037
to 0 (see lock_table_name for details).
1038
In case there is a pending FLUSH TABLES or a name lock, we
1039
need to back off and re-start opening tables.
1040
If we do not back off now, we may dead lock in case of lock
1041
order mismatch with some other thread:
1042
c1-> name lock t1; -- sort of exclusive lock
1043
c2-> open t2; -- sort of shared lock
1044
c1-> name lock t2; -- blocks
1045
c2-> open t1; -- blocks
1325
Table to be created, so we need to create placeholder in table-cache.
1047
if (table->needs_reopen_or_name_lock())
1327
if (!(table= table_cache_insert_placeholder(key, key_length)))
1049
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
1051
/* Force close at once after usage */
1052
version= table->getShare()->getVersion();
1056
/* Avoid self-deadlocks by detecting self-dependencies. */
1057
if (table->open_placeholder && table->in_use == this)
1059
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getShare()->getTableName());
1064
Back off, part 1: mark the table as "unused" for the
1065
purpose of name-locking by setting table->db_stat to 0. Do
1066
that only for the tables in this thread that have an old
1067
table->getShare()->version (this is an optimization (?)).
1068
table->db_stat == 0 signals wait_for_locked_table_names
1069
that the tables in question are not used any more. See
1070
table_is_used call for details.
1072
close_old_data_files(false, false);
1075
Back-off part 2: try to avoid "busy waiting" on the table:
1076
if the table is in use by some other thread, we suspend
1077
and wait till the operation is complete: when any
1078
operation that juggles with table->getShare()->version completes,
1079
it broadcasts COND_refresh condition variable.
1080
If 'old' table we met is in use by current thread we return
1081
without waiting since in this situation it's this thread
1082
which is responsible for broadcasting on COND_refresh
1083
(and this was done already in Session::close_old_data_files()).
1084
Good example of such situation is when we have statement
1085
that needs two instances of table and FLUSH TABLES comes
1086
after we open first instance but before we open second
1089
if (table->in_use != this)
1091
/* wait_for_conditionwill unlock table::Cache::singleton().mutex() for us */
1092
wait_for_condition(table::Cache::singleton().mutex(), COND_refresh);
1093
scopedLock.release();
1097
scopedLock.unlock();
1101
There is a refresh in progress for this table.
1102
Signal the caller that it has to try again.
1329
pthread_mutex_unlock(&LOCK_open);
1113
table::getUnused().unlink(static_cast<table::Concurrent *>(table));
1114
table->in_use= this;
1118
/* Insert a new Table instance into the open cache */
1120
/* Free cache if too big */
1121
table::getUnused().cull();
1123
if (table_list->isCreate())
1125
identifier::Table lock_table_identifier(table_list->getSchemaName(), table_list->getTableName(), message::Table::STANDARD);
1127
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1130
Table to be created, so we need to create placeholder in table-cache.
1132
if (!(table= table_cache_insert_placeholder(lock_table_identifier)))
1137
Link placeholder to the open tables list so it will be automatically
1138
removed once tables are closed. Also mark it so it won't be ignored
1139
by other trying to take name-lock.
1141
table->open_placeholder= true;
1142
table->setNext(open_tables);
1147
/* Table exists. Let us try to open it. */
1150
/* make a new table */
1152
table::Concurrent *new_table= new table::Concurrent;
1154
if (new_table == NULL)
1159
error= new_table->open_unireg_entry(this, alias, identifier);
1165
(void)table::Cache::singleton().insert(new_table);
1172
table->setNext(open_tables); /* Link into simple list */
1175
table->reginfo.lock_type= TL_READ; /* Assume read */
1178
assert(table->getShare()->getTableCount() > 0 || table->getShare()->getType() != message::Table::STANDARD);
1333
Link placeholder to the open tables list so it will be automatically
1334
removed once tables are closed. Also mark it so it won't be ignored
1335
by other trying to take name-lock.
1337
table->open_placeholder= true;
1338
table->next= open_tables;
1340
pthread_mutex_unlock(&LOCK_open);
1344
/* Table exists. Let us try to open it. */
1347
/* make a new table */
1348
table= (Table *)malloc(sizeof(Table));
1351
pthread_mutex_unlock(&LOCK_open);
1355
error= open_unireg_entry(this, table, table_list, alias, key, key_length);
1359
pthread_mutex_unlock(&LOCK_open);
1362
my_hash_insert(&open_cache, (unsigned char*) table);
1365
pthread_mutex_unlock(&LOCK_open);
1368
table->next= open_tables; /* Link into simple list */
1371
table->reginfo.lock_type= TL_READ; /* Assume read */
1374
assert(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
1376
if (lex->need_correct_ident())
1377
table->alias_name_used= my_strcasecmp(table_alias_charset,
1378
table->s->table_name.str, alias);
1180
1379
/* Fix alias if table name changes */
1181
if (strcmp(table->getAlias(), alias))
1380
if (strcmp(table->alias, alias))
1183
table->setAlias(alias);
1382
uint32_t length=(uint32_t) strlen(alias)+1;
1383
table->alias= (char*) realloc((char*) table->alias, length);
1384
memcpy((void*) table->alias, alias, length);
1186
1387
/* These variables are also set in reopen_table() */
1515
1892
other threads trying to get the lock.
1518
void abort_locked_tables(Session *session, const drizzled::identifier::Table &identifier)
1895
void abort_locked_tables(Session *session,const char *db, const char *table_name)
1521
for (table= session->open_tables; table ; table= table->getNext())
1898
for (table= session->open_tables; table ; table= table->next)
1523
if (table->getShare()->getCacheKey() == identifier.getKey())
1900
if (!strcmp(table->s->table_name.str, table_name) &&
1901
!strcmp(table->s->db.str, db))
1525
1903
/* If MERGE child, forward lock handling to parent. */
1526
session->abortLock(table);
1904
mysql_lock_abort(session, table);
1911
Load a table definition from cursor and open unireg table
1915
session Thread handle
1916
entry Store open table definition here
1917
table_list TableList with db, table_name
1919
cache_key Key for share_cache
1920
cache_key_length length of cache_key
1923
Extra argument for open is taken from session->open_options
1924
One must have a lock on LOCK_open when calling this function
1931
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
1933
char *cache_key, uint32_t cache_key_length)
1937
uint32_t discover_retry_count= 0;
1939
safe_mutex_assert_owner(&LOCK_open);
1941
if (!(share= TableShare::getShare(session, table_list, cache_key,
1943
table_list->i_s_requested_object,
1947
while ((error= open_table_from_share(session, share, alias,
1948
(uint32_t) (HA_OPEN_KEYFILE |
1953
session->open_options, entry)))
1955
if (error == 7) // Table def changed
1957
share->version= 0; // Mark share as old
1958
if (discover_retry_count++) // Retry once
1963
Here we should wait until all threads has released the table.
1964
For now we do one retry. This may cause a deadlock if there
1965
is other threads waiting for other tables used by this thread.
1967
Proper fix would be to if the second retry failed:
1968
- Mark that table def changed
1969
- Return from open table
1970
- Close all tables used by this thread
1971
- Start waiting that the share is released
1972
- Retry by opening all tables again
1977
To avoid deadlock, only wait for release if no one else is
1980
if (share->ref_count != 1)
1982
/* Free share and wait until it's released by all threads */
1983
TableShare::release(share);
1985
if (!session->killed)
1987
drizzle_reset_errors(session, 1); // Clear warnings
1988
session->clear_error(); // Clear error message
1993
if (!entry->s || !entry->s->crashed)
1995
// Code below is for repairing a crashed cursor
1996
if ((error= lock_table_name(session, table_list, true)))
2000
if (wait_for_locked_table_names(session, table_list))
2002
unlock_table_name(table_list);
2006
pthread_mutex_unlock(&LOCK_open);
2007
session->clear_error(); // Clear error message
2009
if (open_table_from_share(session, share, alias,
2010
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2014
ha_open_options | HA_OPEN_FOR_REPAIR,
2015
entry) || ! entry->cursor)
2017
/* Give right error message */
2018
session->clear_error();
2019
my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
2020
errmsg_printf(ERRMSG_LVL_ERROR, _("Couldn't repair table: %s.%s"), share->db.str,
2021
share->table_name.str);
2023
entry->closefrm(false);
2027
session->clear_error(); // Clear error message
2028
pthread_mutex_lock(&LOCK_open);
2029
unlock_table_name(table_list);
2037
If we are here, there was no fatal error (but error may be still
2040
if (unlikely(entry->cursor->implicit_emptied))
2042
ReplicationServices &replication_services= ReplicationServices::singleton();
2043
entry->cursor->implicit_emptied= 0;
2046
uint32_t query_buf_size= 20 + share->db.length + share->table_name.length +1;
2047
if ((query= (char*) malloc(query_buf_size)))
2050
"this DELETE FROM is needed even with row-based binlogging"
2052
We inherited this from MySQL. TODO: fix it to issue a propper truncate
2053
of the table (though that may not be completely right sematics).
2056
end+= sprintf(query, "DELETE FROM `%s`.`%s`", share->db.str,
2057
share->table_name.str);
2058
replication_services.rawStatement(session, query, (size_t)(end - query));
2063
errmsg_printf(ERRMSG_LVL_ERROR, _("When opening HEAP table, could not allocate memory "
2064
"to write 'DELETE FROM `%s`.`%s`' to replication"),
2065
table_list->db, table_list->table_name);
2066
my_error(ER_OUTOFMEMORY, MYF(0), query_buf_size);
2067
entry->closefrm(false);
2075
TableShare::release(share);
1535
2082
Open all tables in list
1802
Table *Open_tables_state::open_temporary_table(const identifier::Table &identifier,
2348
Table *Session::open_temporary_table(const char *path, const char *db_arg,
2349
const char *table_name_arg, bool link_in_list)
1805
assert(identifier.isTmp());
1808
table::Temporary *new_tmp_table= new table::Temporary(identifier.getType(),
1810
const_cast<char *>(const_cast<identifier::Table&>(identifier).getPath().c_str()),
1811
static_cast<uint32_t>(identifier.getPath().length()));
1812
if (not new_tmp_table)
2351
Table *new_tmp_table;
2353
char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
2354
uint32_t key_length, path_length;
2355
TableList table_list;
2357
table_list.db= (char*) db_arg;
2358
table_list.table_name= (char*) table_name_arg;
2359
/* Create the cache_key for temporary tables */
2360
key_length= table_list.create_table_def_key(cache_key);
2361
path_length= strlen(path);
2363
if (!(new_tmp_table= (Table*) malloc(sizeof(*new_tmp_table) + sizeof(*share) +
2364
path_length + 1 + key_length)))
2367
share= (TableShare*) (new_tmp_table+1);
2368
tmp_path= (char*) (share+1);
2369
saved_cache_key= strcpy(tmp_path, path)+path_length+1;
2370
memcpy(saved_cache_key, cache_key, key_length);
2372
share->init(saved_cache_key, key_length, strchr(saved_cache_key, '\0')+1, tmp_path);
1816
2375
First open the share, and then open the table from the share we just opened.
1818
if (new_tmp_table->getMutableShare()->open_table_def(*static_cast<Session *>(this), identifier) ||
1819
new_tmp_table->getMutableShare()->open_table_from_share(static_cast<Session *>(this), identifier, identifier.getTableName().c_str(),
1820
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2377
if (open_table_def(*this, share) ||
2378
open_table_from_share(this, share, table_name_arg,
2379
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
1825
2385
/* No need to lock share->mutex as this is not needed for tmp tables */
1826
delete new_tmp_table->getMutableShare();
1827
delete new_tmp_table;
2386
share->free_table_share();
2387
free((char*) new_tmp_table);
1832
2391
new_tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
2392
share->tmp_table= (new_tmp_table->cursor->has_transactions() ?
2393
TRANSACTIONAL_TMP_TABLE : NON_TRANSACTIONAL_TMP_TABLE);
1834
2395
if (link_in_list)
1836
2397
/* growing temp list at the head */
1837
new_tmp_table->setNext(this->temporary_tables);
1838
if (new_tmp_table->getNext())
1840
new_tmp_table->getNext()->setPrev(new_tmp_table);
2398
new_tmp_table->next= this->temporary_tables;
2399
if (new_tmp_table->next)
2400
new_tmp_table->next->prev= new_tmp_table;
1842
2401
this->temporary_tables= new_tmp_table;
1843
this->temporary_tables->setPrev(0);
2402
this->temporary_tables->prev= 0;
1845
2404
new_tmp_table->pos_in_table_list= 0;
3932
4539
unireg support functions
3933
4540
*****************************************************************************/
4543
Invalidate any cache entries that are for some DB
4546
remove_db_from_cache()
4547
db Database name. This will be in lower case if
4548
lower_case_table_name is set
4551
We can't use hash_delete when looping hash_elements. We mark them first
4552
and afterwards delete those marked unused.
4555
void remove_db_from_cache(const char *db)
4557
safe_mutex_assert_owner(&LOCK_open);
4559
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
4561
Table *table=(Table*) hash_element(&open_cache,idx);
4562
if (!strcmp(table->s->db.str, db))
4564
table->s->version= 0L; /* Free when thread is ready */
4566
relink_unused(table);
4569
while (unused_tables && !unused_tables->s->version)
4570
hash_delete(&open_cache,(unsigned char*) unused_tables);
4575
Mark all entries with the table as deleted to force an reopen of the table
4577
The table will be closed (not stored in cache) by the current thread when
4578
close_thread_tables() is called.
4584
0 This thread now have exclusive access to this table and no other thread
4585
can access the table until close_thread_tables() is called.
4586
1 Table is in use by another thread
4589
bool remove_table_from_cache(Session *session, const char *db, const char *table_name,
4592
char key[MAX_DBKEY_LENGTH];
4594
uint32_t key_length;
4597
bool signalled= false;
4599
key_pos= strcpy(key_pos, db) + strlen(db);
4600
key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
4601
key_length= (uint32_t) (key_pos-key)+1;
4605
HASH_SEARCH_STATE state;
4606
result= signalled= false;
4608
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
4611
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
4616
table->s->version=0L; /* Free when thread is ready */
4617
if (!(in_use=table->in_use))
4619
relink_unused(table);
4621
else if (in_use != session)
4624
Mark that table is going to be deleted from cache. This will
4625
force threads that are in mysql_lock_tables() (but not yet
4626
in thr_multi_lock()) to abort it's locks, close all tables and retry
4628
in_use->some_tables_deleted= true;
4629
if (table->is_name_opened())
4634
Now we must abort all tables locks used by this thread
4635
as the thread may be waiting to get a lock for another table.
4636
Note that we need to hold LOCK_open while going through the
4637
list. So that the other thread cannot change it. The other
4638
thread must also hold LOCK_open whenever changing the
4639
open_tables list. Aborting the MERGE lock after a child was
4640
closed and before the parent is closed would be fatal.
4642
for (Table *session_table= in_use->open_tables;
4644
session_table= session_table->next)
4646
/* Do not handle locks of MERGE children. */
4647
if (session_table->db_stat) // If table is open
4648
signalled|= mysql_lock_abort_for_thread(session, session_table);
4652
result= result || (flags & RTFC_OWNED_BY_Session_FLAG);
4654
while (unused_tables && !unused_tables->s->version)
4655
hash_delete(&open_cache,(unsigned char*) unused_tables);
4657
/* Remove table from table definition cache if it's not in use */
4658
TableShare::release(key, key_length);
4660
if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG))
4663
Signal any thread waiting for tables to be freed to
4666
broadcast_refresh();
4667
if (!(flags & RTFC_CHECK_KILLED_FLAG) || !session->killed)
4670
if (likely(signalled))
4671
(void) pthread_cond_wait(&COND_refresh, &LOCK_open);
4674
struct timespec abstime;
4676
It can happen that another thread has opened the
4677
table but has not yet locked any table at all. Since
4678
it can be locked waiting for a table that our thread
4679
has done LOCK Table x WRITE on previously, we need to
4680
ensure that the thread actually hears our signal
4681
before we go to sleep. Thus we wait for a short time
4682
and then we retry another loop in the
4683
remove_table_from_cache routine.
4685
set_timespec(abstime, 10);
4686
pthread_cond_timedwait(&COND_refresh, &LOCK_open, &abstime);