MySQL  8.0.16
Source Code Documentation
table_cache.h
Go to the documentation of this file.
1 /* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef TABLE_CACHE_INCLUDED
24 #define TABLE_CACHE_INCLUDED
25 
26 #include <stddef.h>
27 #include <sys/types.h>
28 #include <memory>
29 #include <string>
30 #include <unordered_map>
31 #include <utility>
32 
33 #include "lex_string.h"
34 #include "my_base.h"
35 #include "my_dbug.h"
36 #include "my_psi_config.h"
39 #include "mysql/psi/mysql_mutex.h"
40 #include "sql/handler.h"
41 #include "sql/sql_base.h"
42 #include "sql/sql_class.h"
43 #include "sql/sql_plist.h"
44 #include "sql/system_variables.h"
45 #include "sql/table.h"
46 
48 
50 
51 /**
52  Cache for open TABLE objects.
53 
54  The idea behind this cache is that most statements don't need to
55  go to a central table definition cache to get a TABLE object and
56  therefore don't need to lock LOCK_open mutex.
57  Instead they only need to go to one Table_cache instance (the
58  specific instance is determined by thread id) and only lock the
59  mutex protecting this cache.
60  DDL statements that need to remove all TABLE objects from all caches
61  need to lock mutexes for all Table_cache instances, but they are rare.
62 
63  This significantly increases scalability in some scenarios.
64 */
65 
66 class Table_cache {
67  private:
68  /**
69  The table cache lock protects the following data:
70 
71  1) m_unused_tables list.
72  2) m_cache hash.
73  3) used_tables, free_tables lists in Table_cache_element objects in
74  this cache.
75  4) m_table_count - total number of TABLE objects in this cache.
76  5) the element in TABLE_SHARE::cache_element[] array that corresponds
77  to this cache,
78  6) in_use member in TABLE object.
79  7) Also ownership of mutexes for all caches are required to update
80  the refresh_version and table_def_shutdown_in_progress variables
81  and TABLE_SHARE::version member.
82 
83  The intention is that any query that finds a cached table object in
84  its designated table cache should only need to lock this mutex
85  instance and there should be no need to lock LOCK_open. LOCK_open is
86  still required however to create and release TABLE objects. However
87  most usage of the MySQL Server should be able to set the cache size
88  big enough so that the majority of the queries only need to lock this
89  mutex instance and not LOCK_open.
90  */
92 
93  /**
94  The hash of Table_cache_element objects, each table/table share that
95  has any TABLE object in the Table_cache has a Table_cache_element from
96  which the list of free TABLE objects in this table cache AND the list
97  of used TABLE objects in this table cache is stored.
98  We use Table_cache_element::share::table_cache_key as key for this hash.
99  */
100  std::unordered_map<std::string, std::unique_ptr<Table_cache_element>> m_cache;
101 
102  /**
103  List that contains all TABLE instances for tables in this particular
104  table cache that are in not use by any thread. Recently used TABLE
105  instances are appended to the end of the list. Thus the beginning of
106  the list contains which have been least recently used.
107  */
109 
110  /**
111  Total number of TABLE instances for tables in this particular table
112  cache (both in use by threads and not in use).
113  This value summed over all table caches is accessible to users as
114  Open_tables status variable.
115  */
117 
118 #ifdef HAVE_PSI_INTERFACE
121 #endif
122 
123  private:
124 #ifdef EXTRA_DEBUG
125  void check_unused();
126 #else
127  void check_unused() {}
128 #endif
129  inline void link_unused_table(TABLE *table);
130  inline void unlink_unused_table(TABLE *table);
131 
132  inline void free_unused_tables_if_necessary(THD *thd);
133 
134  public:
135  bool init();
136  void destroy();
137  static void init_psi_keys();
138 
139  /** Acquire lock on table cache instance. */
141  /** Release lock on table cache instance. */
143  /** Assert that caller owns lock on the table cache. */
145 
146  inline TABLE *get_table(THD *thd, const char *key, size_t key_length,
147  TABLE_SHARE **share);
148 
149  inline void release_table(THD *thd, TABLE *table);
150 
151  inline bool add_used_table(THD *thd, TABLE *table);
152  inline void remove_table(TABLE *table);
153 
154  /** Get number of TABLE instances in the cache. */
155  uint cached_tables() const { return m_table_count; }
156 
157  void free_all_unused_tables();
158 
159 #ifndef DBUG_OFF
160  void print_tables();
161 #endif
162 };
163 
164 /**
165  Container class for all table cache instances in the system.
166 */
167 
169  public:
170  /** Maximum supported number of table cache instances. */
171  static const int MAX_TABLE_CACHES = 64;
172 
173  /** Default number of table cache instances */
174  static const int DEFAULT_MAX_TABLE_CACHES = 16;
175 
176  bool init();
177  void destroy();
178 
179  /** Get instance of table cache to be used by particular connection. */
182  }
183 
184  /** Get index for the table cache in container. */
185  uint cache_index(Table_cache *cache) const {
186  return static_cast<uint>(cache - &m_table_cache[0]);
187  }
188 
190 
191  void lock_all_and_tdc();
192  void unlock_all_and_tdc();
193  void assert_owner_all();
195 
196  void free_table(THD *thd, enum_tdc_remove_table_type remove_type,
197  TABLE_SHARE *share);
198 
199  void free_all_unused_tables();
200 
201 #ifndef DBUG_OFF
202  void print_tables();
203 #endif
204 
205  friend class Table_cache_iterator;
206 
207  private:
208  /**
209  An array of Table_cache instances.
210  Only the first table_cache_instances elements in it are used.
211  */
213 };
214 
216 
217 /**
218  Element that represents the table in the specific table cache.
219  Plays for table cache instance role similar to role of TABLE_SHARE
220  for table definition cache.
221 
222  It is an implementation detail of Table_cache and is present
223  in the header file only to allow inlining of some methods.
224 */
225 
227  private:
228  /*
229  Doubly-linked (back-linked) lists of used and unused TABLE objects
230  for this table in this table cache (one such list per table cache).
231  */
232  typedef I_P_List<
235 
239 
240  public:
241  Table_cache_element(TABLE_SHARE *share_arg) : share(share_arg) {}
242 
243  TABLE_SHARE *get_share() const { return share; }
244 
245  friend class Table_cache;
246  friend class Table_cache_manager;
247  friend class Table_cache_iterator;
248 };
249 
250 /**
251  Iterator which allows to go through all used TABLE instances
252  for the table in all table caches.
253 */
254 
259 
260  inline void move_to_next_table();
261 
262  public:
263  /**
264  Construct iterator over all used TABLE objects for the table share.
265 
266  @note Assumes that caller owns locks on all table caches.
267  */
268  inline Table_cache_iterator(const TABLE_SHARE *share_arg);
269  inline TABLE *operator++(int);
270  inline void rewind();
271 };
272 
273 /**
274  Add table to the tail of unused tables list for table cache
275  (i.e. as the most recently used table in this list).
276 */
277 
279  if (m_unused_tables) {
280  table->next = m_unused_tables;
281  table->prev = m_unused_tables->prev;
282  m_unused_tables->prev = table;
283  table->prev->next = table;
284  } else
285  m_unused_tables = table->next = table->prev = table;
286  check_unused();
287 }
288 
289 /** Remove table from the unused tables list for table cache. */
290 
292  table->next->prev = table->prev;
293  table->prev->next = table->next;
294  if (table == m_unused_tables) {
296  if (table == m_unused_tables) m_unused_tables = NULL;
297  }
298  check_unused();
299 }
300 
301 /**
302  Free unused TABLE instances if total number of TABLE objects
303  in table cache has exceeded table_cache_size_per_instance
304  limit.
305 
306  @note That we might need to free more than one instance during
307  this call if table_cache_size was changed dynamically.
308 */
309 
311  /*
312  We have too many TABLE instances around let us try to get rid of them.
313 
314  Note that we might need to free more than one TABLE object, and thus
315  need the below loop, in case when table_cache_size is changed dynamically,
316  at server run time.
317  */
321  TABLE *table_to_free = m_unused_tables;
322  remove_table(table_to_free);
323  intern_close_table(table_to_free);
325  }
327  }
328 }
329 
330 /**
331  Add newly created TABLE object which is going to be used right away
332  to the table cache.
333 
334  @note Caller should own lock on the table cache.
335 
336  @note Sets TABLE::in_use member as side effect.
337 
338  @retval false - success.
339  @retval true - failure.
340 */
341 
344 
345  assert_owner();
346 
347  DBUG_ASSERT(table->in_use == thd);
348 
349  /*
350  Try to get Table_cache_element representing this table in the cache
351  from array in the TABLE_SHARE.
352  */
353  el = table->s->cache_element[table_cache_manager.cache_index(this)];
354 
355  if (!el) {
356  /*
357  If TABLE_SHARE doesn't have pointer to the element representing table
358  in this cache, the element for the table must be absent from table the
359  cache.
360 
361  Allocate new Table_cache_element object and add it to the cache
362  and array in TABLE_SHARE.
363  */
364  std::string key(table->s->table_cache_key.str,
365  table->s->table_cache_key.length);
366  DBUG_ASSERT(m_cache.count(key) == 0);
367 
368  el = new Table_cache_element(table->s);
369  m_cache.emplace(key, std::unique_ptr<Table_cache_element>(el));
370  table->s->cache_element[table_cache_manager.cache_index(this)] = el;
371  }
372 
373  /* Add table to the used tables list */
374  el->used_tables.push_front(table);
375 
376  m_table_count++;
377 
379 
380  return false;
381 }
382 
383 /**
384  Prepare used or unused TABLE instance for destruction by removing
385  it from the table cache.
386 
387  @note Caller should own lock on the table cache.
388 */
389 
391  Table_cache_element *el =
393 
394  assert_owner();
395 
396  if (table->in_use) {
397  /* Remove from per-table chain of used TABLE objects. */
398  el->used_tables.remove(table);
399  } else {
400  /* Remove from per-table chain of unused TABLE objects. */
401  el->free_tables.remove(table);
402 
403  /* And per-cache unused chain. */
404  unlink_unused_table(table);
405  }
406 
407  m_table_count--;
408 
409  if (el->used_tables.is_empty() && el->free_tables.is_empty()) {
410  std::string key(table->s->table_cache_key.str,
411  table->s->table_cache_key.length);
412  m_cache.erase(key);
413  /*
414  Remove reference to deleted cache element from array
415  in the TABLE_SHARE.
416  */
418  }
419 }
420 
421 /**
422  Get an unused TABLE instance from the table cache.
423 
424  @param thd Thread context.
425  @param key Key identifying table.
426  @param key_length Length of key for the table.
427  @param[out] share NULL - if table cache doesn't contain any
428  information about the table (i.e. doesn't have
429  neither used nor unused TABLE objects for it).
430  Pointer to TABLE_SHARE for the table otherwise.
431 
432  @note Caller should own lock on the table cache.
433  @note Sets TABLE::in_use member as side effect.
434 
435  @retval non-NULL - pointer to unused TABLE object, "share" out-parameter
436  contains pointer to TABLE_SHARE for this table.
437  @retval NULL - no unused TABLE object was found, "share" parameter
438  contains pointer to TABLE_SHARE for this table if there
439  are used TABLE objects in cache and NULL otherwise.
440 */
441 
442 TABLE *Table_cache::get_table(THD *thd, const char *key, size_t key_length,
443  TABLE_SHARE **share) {
444  TABLE *table;
445 
446  assert_owner();
447 
448  *share = NULL;
449 
450  std::string key_str(key, key_length);
451  const auto el_it = m_cache.find(key_str);
452  if (el_it == m_cache.end()) return NULL;
453  Table_cache_element *el = el_it->second.get();
454 
455  *share = el->share;
456 
457  if ((table = el->free_tables.front())) {
458  DBUG_ASSERT(!table->in_use);
459 
460  /*
461  Unlink table from list of unused TABLE objects for this
462  table in this cache.
463  */
464  el->free_tables.remove(table);
465 
466  /* Unlink table from unused tables list for this cache. */
467  unlink_unused_table(table);
468 
469  /*
470  Add table to list of used TABLE objects for this table
471  in the table cache.
472  */
473  el->used_tables.push_front(table);
474 
475  table->in_use = thd;
476  /* The ex-unused table must be fully functional. */
477  DBUG_ASSERT(table->db_stat && table->file);
478  /* The children must be detached from the table. */
480  }
481 
482  return table;
483 }
484 
485 /**
486  Put used TABLE instance back to the table cache and mark
487  it as unused.
488 
489  @note Caller should own lock on the table cache.
490  @note Sets TABLE::in_use member as side effect.
491 */
492 
494  Table_cache_element *el =
496 
497  assert_owner();
498 
499  DBUG_ASSERT(table->in_use);
500  DBUG_ASSERT(table->file);
501 
502  /* We shouldn't put the table to 'unused' list if the share is old. */
503  DBUG_ASSERT(!table->s->has_old_version());
504 
505  table->in_use = NULL;
506 
507  /* Remove TABLE from the list of used objects for the table in this cache. */
508  el->used_tables.remove(table);
509  /* Add TABLE to the list of unused objects for the table in this cache. */
510  el->free_tables.push_front(table);
511  /* Also link it last in the list of unused TABLE objects for the cache. */
512  link_unused_table(table);
513 
514  /*
515  We free the least used tables, not the subject table, to keep the LRU order.
516  Note that in most common case the below call won't free anything.
517  */
519 }
520 
521 /**
522  Construct iterator over all used TABLE objects for the table share.
523 
524  @note Assumes that caller owns locks on all table caches.
525 */
527  : share(share_arg), current_cache_index(0), current_table(NULL) {
530 }
531 
532 /** Helper that moves iterator to the next used TABLE for the table share. */
533 
537 
538  if ((el = share->cache_element[current_cache_index])) {
539  if ((current_table = el->used_tables.front())) break;
540  }
541  }
542 }
543 
544 /**
545  Get current used TABLE instance and move iterator to the next one.
546 
547  @note Assumes that caller owns locks on all table caches.
548 */
549 
552 
553  TABLE *result = current_table;
554 
555  if (current_table) {
558 
559  current_table = ++it;
560 
561  if (!current_table) {
564  }
565  }
566 
567  return result;
568 }
569 
574 }
575 
576 #endif /* TABLE_CACHE_INCLUDED */
TABLE * prev
Definition: table.h:1297
Instrumentation helpers for mutexes.
Iterator which allows to go through all used TABLE instances for the table in all table caches...
Definition: table_cache.h:255
void assert_owner_all_and_tdc()
Assert that caller owns locks on all instances of table cache and table definition cache...
Definition: table_cache.cc:258
char * str
Definition: mysql_lex_string.h:35
void destroy()
Destroy instance of table cache.
Definition: table_cache.cc:63
ulong table_cache_size_per_instance
Definition: mysqld.cc:1090
Element that represents the table in the specific table cache.
Definition: table_cache.h:226
bool is_empty() const
Definition: sql_plist.h:89
This structure is shared between different table objects.
Definition: table.h:646
Table_cache_element(TABLE_SHARE *share_arg)
Definition: table_cache.h:241
Iterator for I_P_List.
Definition: sql_plist.h:30
static PSI_mutex_info m_mutex_keys[]
Definition: table_cache.h:120
Table_cache_element ** cache_element
Array of table_cache_instances pointers to elements of table caches respresenting this table in each ...
Definition: table.h:693
I_P_List< TABLE, I_P_List_adapter< TABLE, &TABLE::cache_next, &TABLE::cache_prev > > TABLE_list
Definition: table_cache.h:234
Sergei Dialog Client Authentication NULL
Definition: dialog.cc:352
bool init()
Initialize all instances of table cache to be used by server.
Definition: table_cache.cc:188
Table_cache m_table_cache[MAX_TABLE_CACHES]
An array of Table_cache instances.
Definition: table_cache.h:212
void free_all_unused_tables()
Free all unused TABLE objects in the table cache.
Definition: table_cache.cc:122
void link_unused_table(TABLE *table)
Add table to the tail of unused tables list for table cache (i.e.
Definition: table_cache.h:278
Definition: my_base.h:398
uint db_stat
Definition: table.h:1474
TABLE_list used_tables
Definition: table_cache.h:236
THD * in_use
Definition: table.h:1322
void check_unused()
Definition: table_cache.h:127
TABLE_list free_tables
Definition: table_cache.h:237
Definition: table.h:1294
TABLE * next
Definition: table.h:1297
unsigned int PSI_mutex_key
Instrumented mutex key.
Definition: psi_mutex_bits.h:49
static PSI_mutex_key m_lock_key
Definition: table_cache.h:119
void free_all_unused_tables()
Free all unused TABLE objects in all table cache instances.
Definition: table_cache.cc:320
void print_tables()
Print debug information for the contents of all table cache instances.
Definition: table_cache.cc:332
LEX_STRING table_cache_key
Definition: table.h:723
const TABLE_SHARE * share
Definition: table_cache.h:256
This file includes constants used by all storage engines.
void intern_close_table(TABLE *table)
Definition: sql_base.cc:1091
#define DBUG_ASSERT(A)
Definition: my_dbug.h:128
ulonglong table_open_cache_overflows
Definition: system_variables.h:425
my_thread_id thread_id() const
Definition: sql_class.h:2101
Hook class which via its methods specifies which members of T should be used for participating in a i...
Definition: sql_plist.h:197
Table_cache_iterator(const TABLE_SHARE *share_arg)
Construct iterator over all used TABLE objects for the table share.
Definition: table_cache.h:526
void rewind()
Definition: table_cache.h:570
void unlock_all_and_tdc()
Release locks on all instances of table cache and table definition cache.
Definition: table_cache.cc:238
void move_to_next_table()
Helper that moves iterator to the next used TABLE for the table share.
Definition: table_cache.h:534
TABLE_SHARE * s
Definition: table.h:1295
void assert_owner_all()
Assert that caller owns locks on all instances of table cache.
Definition: table_cache.cc:248
Container class for all table cache instances in the system.
Definition: table_cache.h:168
bool init()
Initialize instance of table cache.
Definition: table_cache.cc:54
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
void release_table(THD *thd, TABLE *table)
Put used TABLE instance back to the table cache and mark it as unused.
Definition: table_cache.h:493
unsigned int uint
Definition: uca-dump.cc:29
mysql_mutex_t m_lock
The table cache lock protects the following data:
Definition: table_cache.h:91
void unlink_unused_table(TABLE *table)
Remove table from the unused tables list for table cache.
Definition: table_cache.h:291
uint cache_index(Table_cache *cache) const
Get index for the table cache in container.
Definition: table_cache.h:185
void free_unused_tables_if_necessary(THD *thd)
Free unused TABLE instances if total number of TABLE objects in table cache has exceeded table_cache_...
Definition: table_cache.h:310
void remove_table(TABLE *table)
Prepare used or unused TABLE instance for destruction by removing it from the table cache...
Definition: table_cache.h:390
uint cached_tables() const
Get number of TABLE instances in the cache.
Definition: table_cache.h:155
TABLE * operator++(int)
Get current used TABLE instance and move iterator to the next one.
Definition: table_cache.h:550
TABLE_SHARE * share
Definition: table_cache.h:238
Instrumentation helpers for mutexes.
Intrusive parameterized list.
Definition: sql_plist.h:74
size_t length
Definition: mysql_lex_string.h:36
Mutex information.
Definition: psi_mutex_bits.h:70
TABLE * m_unused_tables
List that contains all TABLE instances for tables in this particular table cache that are in not use ...
Definition: table_cache.h:108
Table_cache * get_cache(THD *thd)
Get instance of table cache to be used by particular connection.
Definition: table_cache.h:180
static const char * key
Definition: suite_stubs.c:14
bool has_old_version() const
Is this table share being expelled from the table definition cache?
Definition: table.h:1004
void lock()
Acquire lock on table cache instance.
Definition: table_cache.h:140
enum_tdc_remove_table_type
Definition: sql_base.h:95
TABLE_SHARE * get_share() const
Definition: table_cache.h:243
ulong table_cache_instances
Definition: mysqld.cc:1089
void push_front(T *a)
Definition: sql_plist.h:90
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
void lock_all_and_tdc()
Acquire locks on all instances of table cache and table definition cache (i.e.
Definition: table_cache.cc:227
Cache for open TABLE objects.
Definition: table_cache.h:66
handler * file
Definition: table.h:1296
ABI for instrumented mutexes.
#define mysql_mutex_assert_owner(M)
Wrapper, to use safe_mutex_assert_owner with instrumented mutexes.
Definition: mysql_mutex.h:109
static const int MAX_TABLE_CACHES
Maximum supported number of table cache instances.
Definition: table_cache.h:171
#define mysql_mutex_unlock(M)
Definition: mysql_mutex.h:53
void destroy()
Destroy all instances of table cache which were used by server.
Definition: table_cache.cc:202
void unlock()
Release lock on table cache instance.
Definition: table_cache.h:142
#define mysql_mutex_lock(M)
Definition: mysql_mutex.h:44
Table_cache_manager table_cache_manager
Container for all table cache instances in the system.
Definition: table_cache.cc:39
void print_tables()
Print debug information for the contents of the table cache.
Definition: table_cache.cc:137
uint m_table_count
Total number of TABLE instances for tables in this particular table cache (both in use by threads and...
Definition: table_cache.h:116
static void init_psi_keys()
Init P_S instrumentation key for mutex protecting Table_cache instance.
Definition: table_cache.cc:67
void remove(T *a)
Definition: sql_plist.h:123
T * front()
Definition: sql_plist.h:132
struct System_status_var status_var
Definition: sql_class.h:956
void assert_owner()
Assert that caller owns lock on the table cache.
Definition: table_cache.h:144
bool add_used_table(THD *thd, TABLE *table)
Add newly created TABLE object which is going to be used right away to the table cache.
Definition: table_cache.h:342
void free_table(THD *thd, enum_tdc_remove_table_type remove_type, TABLE_SHARE *share)
Remove and free all or some (depending on parameter) TABLE objects for the table from all table cache...
Definition: table_cache.cc:275
mysql_mutex_t LOCK_open
LOCK_open protects the following variables/objects:
Definition: sql_base.cc:243
uint current_cache_index
Definition: table_cache.h:257
static const int DEFAULT_MAX_TABLE_CACHES
Default number of table cache instances.
Definition: table_cache.h:174
unsigned long ulong
Definition: my_inttypes.h:46
TABLE * get_table(THD *thd, const char *key, size_t key_length, TABLE_SHARE **share)
Get an unused TABLE instance from the table cache.
Definition: table_cache.h:442
virtual int extra(enum ha_extra_function operation)
Definition: handler.h:4965
static uint key_length
Definition: mi_test1.cc:42
uint cached_tables()
Get total number of used and unused TABLE objects in all table caches.
Definition: table_cache.cc:213
std::unordered_map< std::string, std::unique_ptr< Table_cache_element > > m_cache
The hash of Table_cache_element objects, each table/table share that has any TABLE object in the Tabl...
Definition: table_cache.h:100
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:776
TABLE * current_table
Definition: table_cache.h:258