summaryrefslogtreecommitdiff
path: root/sql/table_cache.h
blob: 2d70e4c90a504f5d35bd285be5e212a6d009ee3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* Copyright (c) 2000, 2012, Oracle and/or its affiliates.
   Copyright (c) 2010, 2011 Monty Program Ab
   Copyright (C) 2013 Sergey Vojtovich and MariaDB Foundation

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */


extern PSI_mutex_key key_TABLE_SHARE_LOCK_table_share;
extern PSI_cond_key key_TABLE_SHARE_COND_release;

class TDC_element
{
public:
  uchar m_key[NAME_LEN + 1 + NAME_LEN + 1];
  uint m_key_length;
  ulong version;
  bool flushed;
  TABLE_SHARE *share;

  typedef I_P_List <TABLE, TABLE_share> TABLE_list;
  typedef I_P_List <TABLE, All_share_tables> All_share_tables_list;
  /**
    Protects ref_count, m_flush_tickets, all_tables, free_tables, flushed,
    all_tables_refs.
  */
  mysql_mutex_t LOCK_table_share;
  mysql_cond_t COND_release;
  TDC_element *next, **prev;            /* Link to unused shares */
  uint ref_count;                       /* How many TABLE objects uses this */
  uint all_tables_refs;                 /* Number of refs to all_tables */
  /**
    List of tickets representing threads waiting for the share to be flushed.
  */
  Wait_for_flush_list m_flush_tickets;
  /*
    Doubly-linked (back-linked) lists of used and unused TABLE objects
    for this share.
  */
  All_share_tables_list all_tables;
  TABLE_list free_tables;

  TDC_element() {}

  TDC_element(const char *key, uint key_length) : m_key_length(key_length)
  {
    memcpy(m_key, key, key_length);
  }


  void assert_clean_share()
  {
    DBUG_ASSERT(share == 0);
    DBUG_ASSERT(ref_count == 0);
    DBUG_ASSERT(m_flush_tickets.is_empty());
    DBUG_ASSERT(all_tables.is_empty());
    DBUG_ASSERT(free_tables.is_empty());
    DBUG_ASSERT(all_tables_refs == 0);
    DBUG_ASSERT(next == 0);
    DBUG_ASSERT(prev == 0);
  }


  /**
    Acquire TABLE object from table cache.

    @pre share must be protected against removal.

    Acquired object cannot be evicted or acquired again.

    @return TABLE object, or NULL if no unused objects.
  */

  TABLE *acquire_table(THD *thd)
  {
    TABLE *table;

    mysql_mutex_lock(&LOCK_table_share);
    table= free_tables.pop_front();
    if (table)
    {
      DBUG_ASSERT(!table->in_use);
      table->in_use= thd;
      /* The ex-unused table must be fully functional. */
      DBUG_ASSERT(table->db_stat && table->file);
      /* The children must be detached from the table. */
      DBUG_ASSERT(!table->file->extra(HA_EXTRA_IS_ATTACHED_CHILDREN));
    }
    mysql_mutex_unlock(&LOCK_table_share);
    return table;
  }


  /**
    Get last element of free_tables.
  */

  TABLE *free_tables_back()
  {
    TABLE_list::Iterator it(share->tdc->free_tables);
    TABLE *entry, *last= 0;
     while ((entry= it++))
       last= entry;
    return last;
  }


  /**
    Wait for MDL deadlock detector to complete traversing tdc.all_tables.

    Must be called before updating TABLE_SHARE::tdc.all_tables.
  */

  void wait_for_mdl_deadlock_detector()
  {
    while (all_tables_refs)
      mysql_cond_wait(&COND_release, &LOCK_table_share);
  }


  /**
    Prepeare table share for use with table definition cache.
  */

  static void lf_alloc_constructor(uchar *arg)
  {
    TDC_element *element= (TDC_element*) (arg + LF_HASH_OVERHEAD);
    DBUG_ENTER("lf_alloc_constructor");
    mysql_mutex_init(key_TABLE_SHARE_LOCK_table_share,
                     &element->LOCK_table_share, MY_MUTEX_INIT_FAST);
    mysql_cond_init(key_TABLE_SHARE_COND_release, &element->COND_release, 0);
    element->m_flush_tickets.empty();
    element->all_tables.empty();
    element->free_tables.empty();
    element->all_tables_refs= 0;
    element->share= 0;
    element->ref_count= 0;
    element->next= 0;
    element->prev= 0;
    DBUG_VOID_RETURN;
  }


  /**
    Release table definition cache specific resources of table share.
  */

  static void lf_alloc_destructor(uchar *arg)
  {
    TDC_element *element= (TDC_element*) (arg + LF_HASH_OVERHEAD);
    DBUG_ENTER("lf_alloc_destructor");
    element->assert_clean_share();
    mysql_cond_destroy(&element->COND_release);
    mysql_mutex_destroy(&element->LOCK_table_share);
    DBUG_VOID_RETURN;
  }


  static void lf_hash_initializer(LF_HASH *hash __attribute__((unused)),
                                  TDC_element *element, LEX_STRING *key)
  {
    memcpy(element->m_key, key->str, key->length);
    element->m_key_length= key->length;
    element->assert_clean_share();
  }


  static uchar *key(const TDC_element *element, size_t *length,
                    my_bool not_used __attribute__((unused)))
  {
    *length= element->m_key_length;
    return (uchar*) element->m_key;
  }
};


enum enum_tdc_remove_table_type
{
  TDC_RT_REMOVE_ALL,
  TDC_RT_REMOVE_NOT_OWN,
  TDC_RT_REMOVE_UNUSED,
  TDC_RT_REMOVE_NOT_OWN_KEEP_SHARE
};

extern ulong tdc_size;
extern ulong tc_size;

extern void tdc_init(void);
extern void tdc_start_shutdown(void);
extern void tdc_deinit(void);
extern ulong tdc_records(void);
extern void tdc_purge(bool all);
extern TDC_element *tdc_lock_share(THD *thd, const char *db,
                                   const char *table_name);
extern void tdc_unlock_share(TDC_element *element);
extern TABLE_SHARE *tdc_acquire_share(THD *thd, const char *db,
                                      const char *table_name,
                                      const char *key, uint key_length,
                                      my_hash_value_type hash_value,
                                      uint flags, TABLE **out_table);
extern void tdc_release_share(TABLE_SHARE *share);
extern bool tdc_remove_table(THD *thd, enum_tdc_remove_table_type remove_type,
                             const char *db, const char *table_name,
                             bool kill_delayed_threads);
extern int tdc_wait_for_old_version(THD *thd, const char *db,
                                    const char *table_name,
                                    ulong wait_timeout, uint deadlock_weight,
                                    ulong refresh_version= ULONG_MAX);
extern ulong tdc_refresh_version(void);
extern ulong tdc_increment_refresh_version(void);
extern void tdc_assign_new_table_id(TABLE_SHARE *share);
extern int tdc_iterate(THD *thd, my_hash_walk_action action, void *argument,
                       bool no_dups= false);

extern uint tc_records(void);
extern void tc_purge(bool mark_flushed= false);
extern void tc_add_table(THD *thd, TABLE *table);
extern bool tc_release_table(TABLE *table);

/**
  Create a table cache key for non-temporary table.

  @param key         Buffer for key (must be at least NAME_LEN*2+2 bytes).
  @param db          Database name.
  @param table_name  Table name.

  @return Length of key.
*/

inline uint tdc_create_key(char *key, const char *db, const char *table_name)
{
  /*
    In theory caller should ensure that both db and table_name are
    not longer than NAME_LEN bytes. In practice we play safe to avoid
    buffer overruns.
  */
  return (uint) (strmake(strmake(key, db, NAME_LEN) + 1, table_name,
                         NAME_LEN) - key + 1);
}

/**
  Convenience helper: call tdc_acquire_share() without out_table.
*/

static inline TABLE_SHARE *tdc_acquire_share(THD *thd, const char *db,
                                             const char *table_name,
                                             const char *key,
                                             uint key_length, uint flags)
{
  return tdc_acquire_share(thd, db, table_name, key, key_length,
                           my_hash_sort(&my_charset_bin, (uchar*) key,
                                        key_length), flags, 0);
}


/**
  Convenience helper: call tdc_acquire_share() without precomputed cache key.
*/

static inline TABLE_SHARE *tdc_acquire_share(THD *thd, const char *db,
                                             const char *table_name, uint flags)
{
  char key[MAX_DBKEY_LENGTH];
  uint key_length;
  key_length= tdc_create_key(key, db, table_name);
  return tdc_acquire_share(thd, db, table_name, key, key_length, flags);
}


/**
  Convenience helper: call tdc_acquire_share() reusing the MDL cache key.

  @note lifetime of the returned TABLE_SHARE is limited by the
        lifetime of the TABLE_LIST object!!!
*/

uint get_table_def_key(const TABLE_LIST *table_list, const char **key);

static inline TABLE_SHARE *tdc_acquire_share_shortlived(THD *thd, TABLE_LIST *tl,
                                                        uint flags)
{
  const char *key;
  uint        key_length= get_table_def_key(tl, &key);
  return tdc_acquire_share(thd, tl->db, tl->table_name, key, key_length,
                           tl->mdl_request.key.tc_hash_value(), flags, 0);
}