MySQL 8.3.0
Source Code Documentation
shared_dictionary_cache.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2023, Oracle and/or its affiliates.
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 DD_CACHE__SHARED_DICTIONARY_CACHE_INCLUDED
24#define DD_CACHE__SHARED_DICTIONARY_CACHE_INCLUDED
25
26#include <stdio.h>
27
28#include "sql/dd/impl/cache/shared_multi_map.h" // Shared_multi_map
30#include "sql/dd/types/charset.h" // Charset
31#include "sql/dd/types/collation.h" // Collation
32#include "sql/dd/types/column_statistics.h" // Column_statistics
33#include "sql/dd/types/event.h" // Event
34#include "sql/dd/types/routine.h" // Routine
35#include "sql/dd/types/schema.h" // Schema
36#include "sql/dd/types/spatial_reference_system.h" // Spatial_reference_system
37#include "sql/dd/types/table.h" // IWYU pragma: keep
38#include "sql/dd/types/tablespace.h" // Tablespace
39#include "sql/handler.h" // enum_tx_isolation
40
41class THD;
42
43namespace dd {
44namespace cache {
45
46/**
47 Shared dictionary cache containing several maps.
48
49 The dictionary cache is mainly a collection of shared maps for the
50 object types supported. The functions dispatch to the appropriate
51 map based on the key and object type parameter. Cache misses are handled
52 by retrieving the object from the storage adapter singleton.
53
54 The shared dictionary cache itself does not handle concurrency at this
55 outer layer. Concurrency is handled by the various instances of the
56 shared multi map.
57*/
58
59template <typename T>
60class Cache_element;
61
63 private:
64 // Collation and character set cache sizes are chosen so that they can hold
65 // all collations and character sets built into the server. The spatial
66 // reference system cache size is chosen to hold a reasonable number of SRSs
67 // for normal server use.
68 static const size_t collation_capacity = 256;
69 static const size_t column_statistics_capacity = 32;
70 static const size_t charset_capacity = 64;
71 static const size_t event_capacity = 256;
72 static const size_t spatial_reference_system_capacity = 256;
73 /**
74 Maximum number of DD resource group objects to be kept in
75 cache. We use value of 32 which is a fairly reasonable upper limit
76 of resource group configurations that may be in use.
77 */
78 static const size_t resource_group_capacity = 32;
79
90
91 template <typename T>
92 struct Type_selector {}; // Dummy type to use for
93 // selecting map instance.
94
95 /**
96 Overloaded functions to use for selecting map instance based
97 on a key type. Const and non-const variants.
98 */
99
101 return &m_abstract_table_map;
102 }
104 return &m_charset_map;
105 }
107 return &m_collation_map;
108 }
110 return &m_column_stat_map;
111 }
114 return &m_resource_group_map;
115 }
117 return &m_routine_map;
118 }
120 return &m_schema_map;
121 }
125 }
127 return &m_tablespace_map;
128 }
129
132 return &m_abstract_table_map;
133 }
135 return &m_charset_map;
136 }
138 return &m_collation_map;
139 }
142 return &m_column_stat_map;
143 }
145 return &m_schema_map;
146 }
150 }
152 return &m_tablespace_map;
153 }
156 return &m_resource_group_map;
157 }
158
159 /**
160 Template function to get a map instance.
161
162 To support generic code, the map instances are available through
163 template function instances. This allows looking up the
164 appropriate instance based on the key type. We must use
165 overloading to accomplish this (see above). Const and non-const
166 variants.
167
168 @tparam T Dictionary object type.
169
170 @return The shared map handling objects of type T.
171 */
172
173 template <typename T>
175 return m_map(Type_selector<T>());
176 }
177
178 template <typename T>
179 const Shared_multi_map<T> *m_map() const {
180 return m_map(Type_selector<T>());
181 }
182
184
185 public:
187
188 // Set capacity of the shared maps.
189 static void init();
190
191 // Shutdown the shared maps.
192 static void shutdown();
193
194 // Reset the shared cache. Optionally keep the core DD table meta data.
195 static void reset(bool keep_dd_entities);
196
197 // Reset the table and tablespace partitions.
198 static bool reset_tables_and_tablespaces(THD *thd);
199
200 /**
201 Check if an element with the given key is available.
202
203 @param key Key to check for presence.
204
205 @retval true The key exist.
206 @retval false The key does not exist.
207 */
208
209 template <typename K, typename T>
210 bool available(const K &key) {
211 return m_map<T>()->available(key);
212 }
213
214 /**
215 Get an element from the cache, given the key.
216
217 The operation retrieves an element by one of its keys from the cache
218 (possibly involving a cache miss, which will need the thd to handle the
219 miss) and returns it through the parameter. If there is no element for
220 the given key, NULL is returned. The cache owns the returned element,
221 i.e., the caller must not delete it. After using the element, release()
222 must be called for every element received via get(). The reference
223 counter for the element is incremented if the element is retrieved from
224 the shared cache.
225
226 @tparam K Key type.
227 @tparam T Dictionary object type.
228 @param thd Thread context.
229 @param key Key to use for looking up the object.
230 @param [out] element Element pointer, if present. NULL if not present.
231
232 @retval false No error.
233 @retval true Error (from handling a cache miss).
234 */
235
236 template <typename K, typename T>
237 bool get(THD *thd, const K &key, Cache_element<T> **element);
238
241 /**
242 Read an object directly from disk, given the key.
243
244 The operation retrieves an object by one of its keys from the persistent
245 dd tables. The object is returned without being added to the shared
246 cache. The object returned is owned by the caller, who thus becomes
247 responsible of deleting it.
248
249 @tparam K Key type.
250 @tparam T Dictionary object type.
251 @param thd Thread context.
252 @param key Key to use for looking up the object.
253 @param isolation Isolation level to use.
254 @param [out] object Object pointer, if present. NULL if not present.
255
256 @retval false No error.
257 @retval true Error (from reading from the DD tables).
258 */
260
261 template <typename K, typename T>
262 bool get_uncached(THD *thd, const K &key, enum_tx_isolation isolation,
263 const T **object) const;
264
265 /**
266 Add an object to the shared cache.
267
268 The object may not be present already. The object is added to the cache,
269 the use counter of its element wrapper in incremented, and the element
270 pointer is returned. The user must release the object afterwards. The
271 cache is the owner of the returned element and object.
272
273 @tparam T Dictionary object type.
274 @param object Object pointer to be added. May not be NULL.
275 @param element Element pointer, if present. NULL if not present.
276 */
277
278 template <typename T>
279 void put(const T *object, Cache_element<T> **element);
280
281 /**
282 Release an element used by a client.
283
284 The element must be present and in use. If the element becomes unused,
285 it is added to the free list, which is then rectified to enforce
286 its capacity constraints.
287
288 @tparam T Dictionary object type.
289 @param e Element pointer.
290 */
291
292 template <typename T>
293 void release(Cache_element<T> *e) {
294 m_map<T>()->release(e);
295 }
296
297 /**
298 Delete an element from the cache.
299
300 This function will remove the element from the cache and delete the
301 object pointed to. This means that all keys associated with the element
302 will be removed from the maps, and the cache element wrapper will be
303 deleted. The object may not be accessed after calling this function.
304
305 @tparam T Dictionary object type.
306 @param element Element pointer.
307 */
308
309 template <typename T>
310 void drop(Cache_element<T> *element) {
311 m_map<T>()->drop(element);
312 }
313
314 /**
315 Delete an element corresponding to the key from the cache if exists.
316
317 This function will find the element corresponding to the key if
318 it exists. After that it will remove the element from the cache
319 i.e. all maps, and delete the object pointed to. This means that
320 all keys associated with the element will be removed from the maps,
321 and the cache element wrapper will be deleted.
322
323 @tparam K Key type.
324 @tparam T Dictionary object type.
325 @param key Key to be checked.
326 */
327
328 template <typename K, typename T>
329 void drop_if_present(const K &key) {
330 m_map<T>()->drop_if_present(key);
331 }
332
333 /**
334 Replace the object and re-create the keys for an element.
335
336 The operation removes the current keys from the internal maps in the
337 cache, assigns the new object to the element, generates new keys based
338 on the new object, and inserts the new keys into the internal maps in the
339 cache. The old object is deleted.
340
341 @tparam T Dictionary object type.
342 @param element Element pointer.
343 @param object New object to replace the old one.
344 */
345
346 template <typename T>
347 void replace(Cache_element<T> *element, const T *object) {
348 m_map<T>()->replace(element, object);
349 }
350
351 /**
352 Debug dump of a shared cache partition to stderr.
353
354 @tparam T Dictionary object type.
355 */
356
357 template <typename T>
358 void dump() const {
359#ifndef NDEBUG
360 fprintf(stderr, "================================\n");
361 fprintf(stderr, "Shared dictionary cache\n");
362 m_map<T>()->dump();
363 fprintf(stderr, "================================\n");
364#endif
365 }
366};
367
368} // namespace cache
369} // namespace dd
370
371#endif // DD_CACHE__SHARED_DICTIONARY_CACHE_INCLUDED
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:35
Implementation of a dictionary client.
Definition: cache_element.h:68
Definition: shared_dictionary_cache.h:62
Shared_multi_map< Resource_group > m_resource_group_map
Definition: shared_dictionary_cache.h:85
bool get(THD *thd, const K &key, Cache_element< T > **element)
Get an element from the cache, given the key.
Definition: shared_dictionary_cache.cc:91
Shared_multi_map< Spatial_reference_system > * m_map(Type_selector< Spatial_reference_system >)
Definition: shared_dictionary_cache.h:122
Shared_multi_map< Schema > m_schema_map
Definition: shared_dictionary_cache.h:87
Shared_multi_map< Routine > m_routine_map
Definition: shared_dictionary_cache.h:86
Shared_multi_map< Charset > m_charset_map
Definition: shared_dictionary_cache.h:81
static void shutdown()
Definition: shared_dictionary_cache.cc:63
static Shared_dictionary_cache * instance()
Definition: shared_dictionary_cache.cc:39
Shared_multi_map< Routine > * m_map(Type_selector< Routine >)
Definition: shared_dictionary_cache.h:116
const Shared_multi_map< Column_statistics > * m_map(Type_selector< Column_statistics >) const
Definition: shared_dictionary_cache.h:140
const Shared_multi_map< Charset > * m_map(Type_selector< Charset >) const
Definition: shared_dictionary_cache.h:134
static const size_t resource_group_capacity
Maximum number of DD resource group objects to be kept in cache.
Definition: shared_dictionary_cache.h:78
void drop(Cache_element< T > *element)
Delete an element from the cache.
Definition: shared_dictionary_cache.h:310
const Shared_multi_map< Tablespace > * m_map(Type_selector< Tablespace >) const
Definition: shared_dictionary_cache.h:151
Shared_multi_map< Collation > * m_map(Type_selector< Collation >)
Definition: shared_dictionary_cache.h:106
static const size_t event_capacity
Definition: shared_dictionary_cache.h:71
const Shared_multi_map< T > * m_map() const
Definition: shared_dictionary_cache.h:179
Shared_multi_map< Abstract_table > * m_map(Type_selector< Abstract_table >)
Overloaded functions to use for selecting map instance based on a key type.
Definition: shared_dictionary_cache.h:100
Shared_multi_map< Tablespace > * m_map(Type_selector< Tablespace >)
Definition: shared_dictionary_cache.h:126
Shared_multi_map< Schema > * m_map(Type_selector< Schema >)
Definition: shared_dictionary_cache.h:119
const Shared_multi_map< Collation > * m_map(Type_selector< Collation >) const
Definition: shared_dictionary_cache.h:137
Shared_multi_map< Column_statistics > * m_map(Type_selector< Column_statistics >)
Definition: shared_dictionary_cache.h:109
bool get_uncached(THD *thd, const K &key, enum_tx_isolation isolation, const T **object) const
Read an object directly from disk, given the key.
Definition: shared_dictionary_cache.cc:109
Shared_multi_map< Column_statistics > m_column_stat_map
Definition: shared_dictionary_cache.h:83
static const size_t charset_capacity
Definition: shared_dictionary_cache.h:70
Shared_multi_map< Spatial_reference_system > m_spatial_reference_system_map
Definition: shared_dictionary_cache.h:88
static bool reset_tables_and_tablespaces(THD *thd)
Definition: shared_dictionary_cache.cc:84
Shared_multi_map< T > * m_map()
Template function to get a map instance.
Definition: shared_dictionary_cache.h:174
Shared_multi_map< Abstract_table > m_abstract_table_map
Definition: shared_dictionary_cache.h:80
Shared_multi_map< Charset > * m_map(Type_selector< Charset >)
Definition: shared_dictionary_cache.h:103
const Shared_multi_map< Spatial_reference_system > * m_map(Type_selector< Spatial_reference_system >) const
Definition: shared_dictionary_cache.h:147
void drop_if_present(const K &key)
Delete an element corresponding to the key from the cache if exists.
Definition: shared_dictionary_cache.h:329
Shared_multi_map< Resource_group > * m_map(Type_selector< Resource_group >)
Definition: shared_dictionary_cache.h:113
static void init()
Definition: shared_dictionary_cache.cc:44
const Shared_multi_map< Abstract_table > * m_map(Type_selector< Abstract_table >) const
Definition: shared_dictionary_cache.h:130
static const size_t spatial_reference_system_capacity
Definition: shared_dictionary_cache.h:72
void release(Cache_element< T > *e)
Release an element used by a client.
Definition: shared_dictionary_cache.h:293
bool available(const K &key)
Check if an element with the given key is available.
Definition: shared_dictionary_cache.h:210
static const size_t column_statistics_capacity
Definition: shared_dictionary_cache.h:69
const Shared_multi_map< Resource_group > * m_map(Type_selector< Resource_group >) const
Definition: shared_dictionary_cache.h:154
void replace(Cache_element< T > *element, const T *object)
Replace the object and re-create the keys for an element.
Definition: shared_dictionary_cache.h:347
Shared_multi_map< Tablespace > m_tablespace_map
Definition: shared_dictionary_cache.h:89
Shared_multi_map< Collation > m_collation_map
Definition: shared_dictionary_cache.h:82
void dump() const
Debug dump of a shared cache partition to stderr.
Definition: shared_dictionary_cache.h:358
Shared_multi_map< Event > * m_map(Type_selector< Event >)
Definition: shared_dictionary_cache.h:112
void put(const T *object, Cache_element< T > **element)
Add an object to the shared cache.
Definition: shared_dictionary_cache.cc:121
const Shared_multi_map< Schema > * m_map(Type_selector< Schema >) const
Definition: shared_dictionary_cache.h:144
static const size_t collation_capacity
Definition: shared_dictionary_cache.h:68
static void reset(bool keep_dd_entities)
Definition: shared_dictionary_cache.cc:77
Shared_multi_map< Event > m_event_map
Definition: shared_dictionary_cache.h:84
Implementation of a shared set of maps for a given object type.
Definition: shared_multi_map.h:117
#define MY_COMPILER_DIAGNOSTIC_PUSH()
save the compiler's diagnostic (enabled warnings, errors, ...) state
Definition: my_compiler.h:284
#define MY_COMPILER_DIAGNOSTIC_POP()
restore the compiler's diagnostic (enabled warnings, errors, ...) state
Definition: my_compiler.h:285
#define MY_COMPILER_CLANG_WORKAROUND_TPARAM_DOCBUG()
ignore -Wdocumentation compiler warnings for @tparam.
Definition: my_compiler.h:307
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:42
required string key
Definition: replication_asynchronous_connection_failover.proto:59
enum_tx_isolation
Definition: handler.h:3161
Definition: shared_dictionary_cache.h:92