1 /* Copyright (c) 2012, 2015, 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 "my_global.h"
27 #include "sql_class.h"
28 #include "sql_base.h"
29 
30 /**
31   Cache for open TABLE objects.
32 
33   The idea behind this cache is that most statements don't need to
34   go to a central table definition cache to get a TABLE object and
35   therefore don't need to lock LOCK_open mutex.
36   Instead they only need to go to one Table_cache instance (the
37   specific instance is determined by thread id) and only lock the
38   mutex protecting this cache.
39   DDL statements that need to remove all TABLE objects from all caches
40   need to lock mutexes for all Table_cache instances, but they are rare.
41 
42   This significantly increases scalability in some scenarios.
43 */
44 
45 class Table_cache
46 {
47 private:
48   /**
49     The table cache lock protects the following data:
50 
51     1) m_unused_tables list.
52     2) m_cache hash.
53     3) used_tables, free_tables lists in Table_cache_element objects in
54        this cache.
55     4) m_table_count - total number of TABLE objects in this cache.
56     5) the element in TABLE_SHARE::cache_element[] array that corresponds
57        to this cache,
58     6) in_use member in TABLE object.
59     7) Also ownership of mutexes for all caches are required to update
60        the refresh_version and table_def_shutdown_in_progress variables
61        and TABLE_SHARE::version member.
62 
63     The intention is that any query that finds a cached table object in
64     its designated table cache should only need to lock this mutex
65     instance and there should be no need to lock LOCK_open. LOCK_open is
66     still required however to create and release TABLE objects. However
67     most usage of the MySQL Server should be able to set the cache size
68     big enough so that the majority of the queries only need to lock this
69     mutex instance and not LOCK_open.
70   */
71   mysql_mutex_t m_lock;
72 
73   /**
74     The hash of Table_cache_element objects, each table/table share that
75     has any TABLE object in the Table_cache has a Table_cache_element from
76     which the list of free TABLE objects in this table cache AND the list
77     of used TABLE objects in this table cache is stored.
78     We use Table_cache_element::share::table_cache_key as key for this hash.
79   */
80   HASH m_cache;
81 
82   /**
83     List that contains all TABLE instances for tables in this particular
84     table cache that are in not use by any thread. Recently used TABLE
85     instances are appended to the end of the list. Thus the beginning of
86     the list contains which have been least recently used.
87   */
88   TABLE *m_unused_tables;
89 
90   /**
91     Total number of TABLE instances for tables in this particular table
92     cache (both in use by threads and not in use).
93     This value summed over all table caches is accessible to users as
94     Open_tables status variable.
95   */
96   uint m_table_count;
97 
98 #ifdef HAVE_PSI_INTERFACE
99   static PSI_mutex_key m_lock_key;
100   static PSI_mutex_info m_mutex_keys[];
101 #endif
102 
103 private:
104 
105 #ifdef EXTRA_DEBUG
106   void check_unused();
107 #else
108   void check_unused() {}
109 #endif
110   inline void link_unused_table(TABLE *table);
111   inline void unlink_unused_table(TABLE *table);
112 
113   inline void free_unused_tables_if_necessary(THD *thd);
114 
115 public:
116 
117   bool init();
118   void destroy();
119   static void init_psi_keys();
120 
121   /** Acquire lock on table cache instance. */
lock()122   void lock() { mysql_mutex_lock(&m_lock); }
123   /** Release lock on table cache instance. */
unlock()124   void unlock() { mysql_mutex_unlock(&m_lock); }
125   /** Assert that caller owns lock on the table cache. */
assert_owner()126   void assert_owner() { mysql_mutex_assert_owner(&m_lock); }
127 
128   inline TABLE* get_table(THD *thd, my_hash_value_type hash_value,
129                           const char *key, size_t key_length,
130                           TABLE_SHARE **share);
131 
132   inline void release_table(THD *thd, TABLE *table);
133 
134   inline bool add_used_table(THD *thd, TABLE *table);
135   inline void remove_table(TABLE *table);
136 
137   /** Get number of TABLE instances in the cache. */
cached_tables()138   uint cached_tables() const { return m_table_count; }
139 
140   void free_all_unused_tables();
141 
142 #ifndef DBUG_OFF
143   void print_tables();
144 #endif
145 };
146 
147 
148 /**
149   Container class for all table cache instances in the system.
150 */
151 
152 class Table_cache_manager
153 {
154 public:
155 
156   /** Maximum supported number of table cache instances. */
157   static const int MAX_TABLE_CACHES= 64;
158 
159   /** Default number of table cache instances */
160   static const int DEFAULT_MAX_TABLE_CACHES= 16;
161 
162   bool init();
163   void destroy();
164 
165   /** Get instance of table cache to be used by particular connection. */
get_cache(THD * thd)166   Table_cache* get_cache(THD *thd)
167   {
168     return &m_table_cache[thd->thread_id() % table_cache_instances];
169   }
170 
171   /** Get index for the table cache in container. */
cache_index(Table_cache * cache)172   uint cache_index(Table_cache *cache) const
173   {
174     return static_cast<uint>(cache - &m_table_cache[0]);
175   }
176 
177   uint cached_tables();
178 
179   void lock_all_and_tdc();
180   void unlock_all_and_tdc();
181   void assert_owner_all();
182   void assert_owner_all_and_tdc();
183 
184   void free_table(THD *thd,
185                   enum_tdc_remove_table_type remove_type,
186                   TABLE_SHARE *share);
187 
188   void free_all_unused_tables();
189 
190 #ifndef DBUG_OFF
191   void print_tables();
192 #endif
193 
194   friend class Table_cache_iterator;
195 
196 private:
197 
198   /**
199     An array of Table_cache instances.
200     Only the first table_cache_instances elements in it are used.
201   */
202   Table_cache m_table_cache[MAX_TABLE_CACHES];
203 };
204 
205 
206 extern Table_cache_manager table_cache_manager;
207 
208 
209 /**
210   Element that represents the table in the specific table cache.
211   Plays for table cache instance role similar to role of TABLE_SHARE
212   for table definition cache.
213 
214   It is an implementation detail of Table_cache and is present
215   in the header file only to allow inlining of some methods.
216 */
217 
218 class Table_cache_element
219 {
220 private:
221   /*
222     Doubly-linked (back-linked) lists of used and unused TABLE objects
223     for this table in this table cache (one such list per table cache).
224   */
225   typedef I_P_List <TABLE,
226                     I_P_List_adapter<TABLE,
227                                      &TABLE::cache_next,
228                                      &TABLE::cache_prev> > TABLE_list;
229 
230   TABLE_list used_tables;
231   TABLE_list free_tables;
232   TABLE_SHARE *share;
233 
234 public:
235 
Table_cache_element(TABLE_SHARE * share_arg)236   Table_cache_element(TABLE_SHARE *share_arg)
237     : share(share_arg)
238   {
239   }
240 
get_share()241   TABLE_SHARE * get_share() const { return share; };
242 
243   friend class Table_cache;
244   friend class Table_cache_manager;
245   friend class Table_cache_iterator;
246 };
247 
248 
249 /**
250   Iterator which allows to go through all used TABLE instances
251   for the table in all table caches.
252 */
253 
254 class Table_cache_iterator
255 {
256   const TABLE_SHARE *share;
257   uint current_cache_index;
258   TABLE *current_table;
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 /**
275   Add table to the tail of unused tables list for table cache
276   (i.e. as the most recently used table in this list).
277 */
278 
link_unused_table(TABLE * table)279 void Table_cache::link_unused_table(TABLE *table)
280 {
281   if (m_unused_tables)
282   {
283     table->next= m_unused_tables;
284     table->prev= m_unused_tables->prev;
285     m_unused_tables->prev= table;
286     table->prev->next= table;
287   }
288   else
289     m_unused_tables= table->next= table->prev= table;
290   check_unused();
291 }
292 
293 
294 /** Remove table from the unused tables list for table cache. */
295 
unlink_unused_table(TABLE * table)296 void Table_cache::unlink_unused_table(TABLE *table)
297 {
298   table->next->prev= table->prev;
299   table->prev->next= table->next;
300   if (table == m_unused_tables)
301   {
302     m_unused_tables= m_unused_tables->next;
303     if (table == m_unused_tables)
304       m_unused_tables= NULL;
305   }
306   check_unused();
307 }
308 
309 
310 /**
311   Free unused TABLE instances if total number of TABLE objects
312   in table cache has exceeded table_cache_size_per_instance
313   limit.
314 
315   @note That we might need to free more than one instance during
316         this call if table_cache_size was changed dynamically.
317 */
318 
free_unused_tables_if_necessary(THD * thd)319 void Table_cache::free_unused_tables_if_necessary(THD *thd)
320 {
321   /*
322     We have too many TABLE instances around let us try to get rid of them.
323 
324     Note that we might need to free more than one TABLE object, and thus
325     need the below loop, in case when table_cache_size is changed dynamically,
326     at server run time.
327   */
328   if (m_table_count > table_cache_size_per_instance && m_unused_tables)
329   {
330     mysql_mutex_lock(&LOCK_open);
331     while (m_table_count > table_cache_size_per_instance &&
332            m_unused_tables)
333     {
334       TABLE *table_to_free= m_unused_tables;
335       remove_table(table_to_free);
336       intern_close_table(table_to_free);
337       thd->status_var.table_open_cache_overflows++;
338     }
339     mysql_mutex_unlock(&LOCK_open);
340   }
341 }
342 
343 
344 /**
345    Add newly created TABLE object which is going to be used right away
346    to the table cache.
347 
348    @note Caller should own lock on the table cache.
349 
350    @note Sets TABLE::in_use member as side effect.
351 
352    @retval false - success.
353    @retval true  - failure.
354 */
355 
add_used_table(THD * thd,TABLE * table)356 bool Table_cache::add_used_table(THD *thd, TABLE *table)
357 {
358   Table_cache_element *el;
359 
360   assert_owner();
361 
362   DBUG_ASSERT(table->in_use == thd);
363 
364   /*
365     Try to get Table_cache_element representing this table in the cache
366     from array in the TABLE_SHARE.
367   */
368   el= table->s->cache_element[table_cache_manager.cache_index(this)];
369 
370   if (!el)
371   {
372     /*
373       If TABLE_SHARE doesn't have pointer to the element representing table
374       in this cache, the element for the table must be absent from table the
375       cache.
376 
377       Allocate new Table_cache_element object and add it to the cache
378       and array in TABLE_SHARE.
379     */
380     DBUG_ASSERT(! my_hash_search(&m_cache,
381                                  (uchar*)table->s->table_cache_key.str,
382                                  table->s->table_cache_key.length));
383 
384     if (!(el= new Table_cache_element(table->s)))
385       return true;
386 
387     if (my_hash_insert(&m_cache, (uchar*)el))
388     {
389       delete el;
390       return true;
391     }
392 
393     table->s->cache_element[table_cache_manager.cache_index(this)]= el;
394   }
395 
396   /* Add table to the used tables list */
397   el->used_tables.push_front(table);
398 
399   m_table_count++;
400 
401   free_unused_tables_if_necessary(thd);
402 
403   return false;
404 }
405 
406 
407 /**
408    Prepare used or unused TABLE instance for destruction by removing
409    it from the table cache.
410 
411    @note Caller should own lock on the table cache.
412 */
413 
remove_table(TABLE * table)414 void Table_cache::remove_table(TABLE *table)
415 {
416   Table_cache_element *el=
417     table->s->cache_element[table_cache_manager.cache_index(this)];
418 
419   assert_owner();
420 
421   if (table->in_use)
422   {
423     /* Remove from per-table chain of used TABLE objects. */
424     el->used_tables.remove(table);
425   }
426   else
427   {
428     /* Remove from per-table chain of unused TABLE objects. */
429     el->free_tables.remove(table);
430 
431     /* And per-cache unused chain. */
432     unlink_unused_table(table);
433   }
434 
435   m_table_count--;
436 
437   if (el->used_tables.is_empty() && el->free_tables.is_empty())
438   {
439     (void) my_hash_delete(&m_cache, (uchar*) el);
440     /*
441       Remove reference to deleted cache element from array
442       in the TABLE_SHARE.
443     */
444     table->s->cache_element[table_cache_manager.cache_index(this)]= NULL;
445   }
446 }
447 
448 
449 /**
450   Get an unused TABLE instance from the table cache.
451 
452   @param      thd         Thread context.
453   @param      hash_value  Hash value for the key identifying table.
454   @param      key         Key identifying table.
455   @param      key_length  Length of key for the table.
456   @param[out] share       NULL - if table cache doesn't contain any
457                           information about the table (i.e. doesn't have
458                           neither used nor unused TABLE objects for it).
459                           Pointer to TABLE_SHARE for the table otherwise.
460 
461   @note Caller should own lock on the table cache.
462   @note Sets TABLE::in_use member as side effect.
463 
464   @retval non-NULL - pointer to unused TABLE object, "share" out-parameter
465                      contains pointer to TABLE_SHARE for this table.
466   @retval NULL     - no unused TABLE object was found, "share" parameter
467                      contains pointer to TABLE_SHARE for this table if there
468                      are used TABLE objects in cache and NULL otherwise.
469 */
470 
get_table(THD * thd,my_hash_value_type hash_value,const char * key,size_t key_length,TABLE_SHARE ** share)471 TABLE* Table_cache::get_table(THD *thd, my_hash_value_type hash_value,
472                               const char *key, size_t key_length,
473                               TABLE_SHARE **share)
474 {
475   Table_cache_element *el;
476   TABLE *table;
477 
478   assert_owner();
479 
480   *share= NULL;
481 
482   if (!(el= (Table_cache_element*) my_hash_search_using_hash_value(&m_cache,
483                                      hash_value, (uchar*) key, key_length)))
484     return NULL;
485 
486   *share= el->share;
487 
488   if ((table= el->free_tables.front()))
489   {
490     DBUG_ASSERT(!table->in_use);
491 
492     /*
493       Unlink table from list of unused TABLE objects for this
494       table in this cache.
495     */
496     el->free_tables.remove(table);
497 
498     /* Unlink table from unused tables list for this cache. */
499     unlink_unused_table(table);
500 
501     /*
502       Add table to list of used TABLE objects for this table
503       in the table cache.
504     */
505     el->used_tables.push_front(table);
506 
507     table->in_use= thd;
508     /* The ex-unused table must be fully functional. */
509     DBUG_ASSERT(table->db_stat && table->file);
510     /* The children must be detached from the table. */
511     DBUG_ASSERT(! table->file->extra(HA_EXTRA_IS_ATTACHED_CHILDREN));
512   }
513 
514   return table;
515 }
516 
517 
518 /**
519   Put used TABLE instance back to the table cache and mark
520   it as unused.
521 
522   @note Caller should own lock on the table cache.
523   @note Sets TABLE::in_use member as side effect.
524 */
525 
release_table(THD * thd,TABLE * table)526 void Table_cache::release_table(THD *thd, TABLE *table)
527 {
528   Table_cache_element *el=
529     table->s->cache_element[table_cache_manager.cache_index(this)];
530 
531   assert_owner();
532 
533   DBUG_ASSERT(table->in_use);
534   DBUG_ASSERT(table->file);
535 
536   /* We shouldn't put the table to 'unused' list if the share is old. */
537   DBUG_ASSERT(! table->s->has_old_version());
538 
539   table->in_use= NULL;
540 
541   /* Remove TABLE from the list of used objects for the table in this cache. */
542   el->used_tables.remove(table);
543   /* Add TABLE to the list of unused objects for the table in this cache. */
544   el->free_tables.push_front(table);
545   /* Also link it last in the list of unused TABLE objects for the cache. */
546   link_unused_table(table);
547 
548   /*
549     We free the least used tables, not the subject table, to keep the LRU order.
550     Note that in most common case the below call won't free anything.
551   */
552   free_unused_tables_if_necessary(thd);
553 }
554 
555 
556 /**
557   Construct iterator over all used TABLE objects for the table share.
558 
559   @note Assumes that caller owns locks on all table caches.
560 */
Table_cache_iterator(const TABLE_SHARE * share_arg)561 Table_cache_iterator::Table_cache_iterator(const TABLE_SHARE *share_arg)
562   : share(share_arg), current_cache_index(0), current_table(NULL)
563 {
564   table_cache_manager.assert_owner_all();
565   move_to_next_table();
566 }
567 
568 
569 /** Helper that moves iterator to the next used TABLE for the table share. */
570 
move_to_next_table()571 void Table_cache_iterator::move_to_next_table()
572 {
573   for (; current_cache_index < table_cache_instances; ++current_cache_index)
574   {
575     Table_cache_element *el;
576 
577     if ((el= share->cache_element[current_cache_index]))
578     {
579       if ((current_table= el->used_tables.front()))
580         break;
581     }
582   }
583 }
584 
585 
586 /**
587   Get current used TABLE instance and move iterator to the next one.
588 
589   @note Assumes that caller owns locks on all table caches.
590 */
591 
592 TABLE* Table_cache_iterator::operator ++(int)
593 {
594   table_cache_manager.assert_owner_all();
595 
596   TABLE *result= current_table;
597 
598   if (current_table)
599   {
600     Table_cache_element::TABLE_list::Iterator
601       it(share->cache_element[current_cache_index]->used_tables, current_table);
602 
603     current_table= ++it;
604 
605     if (!current_table)
606     {
607       ++current_cache_index;
608       move_to_next_table();
609     }
610   }
611 
612   return result;
613 }
614 
615 
rewind()616 void Table_cache_iterator::rewind()
617 {
618   current_cache_index= 0;
619   current_table= NULL;
620   move_to_next_table();
621 }
622 
623 #endif /* TABLE_CACHE_INCLUDED */
624