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