MySQL 8.0.40
Source Code Documentation
|
Implementation of a shared set of maps for a given object type. More...
#include <shared_multi_map.h>
Classes | |
class | Autolocker |
Public Member Functions | |
Shared_multi_map () | |
Initialize the mutex and condition variable. More... | |
~Shared_multi_map () | |
Destroy the mutex and condition variable. More... | |
void | shutdown () |
Shutdown the shared map. More... | |
bool | reset (THD *thd) |
Reset the shared map. More... | |
void | set_capacity (size_t capacity) |
Set capacity of the shared map. More... | |
template<typename K > | |
bool | available (const K &key) |
Check if an element with the given key is available. More... | |
template<typename K > | |
bool | get (const K &key, Cache_element< T > **element) |
Get a wrapper element from the map handling the given key type. More... | |
template<typename K > | |
void | put (const K *key, const T *object, Cache_element< T > **element) |
Put a new object and element wrapper into the map. More... | |
void | release (Cache_element< T > *element) |
Release one element. More... | |
void | drop (Cache_element< T > *element) |
Delete an element from the map. More... | |
template<typename K > | |
void | drop_if_present (const K &key) |
Delete an object corresponding to the key from the map if exists. More... | |
void | replace (Cache_element< T > *element, const T *object) |
Replace the object and re-generate the keys for an element. More... | |
void | dump () const |
Debug dump of the shared multi map to stderr. More... | |
Private Member Functions | |
template<typename K > | |
Element_map< K, Cache_element< T > > * | m_map () |
Template helper function getting the element map. More... | |
template<typename K > | |
const Element_map< K, Cache_element< T > > * | m_map () const |
template<typename K > | |
Cache_element< T > * | use_if_present (const K &key) |
Template function to find an element and mark it as used. More... | |
void | remove (Cache_element< T > *element, Autolocker *lock) |
Remove an element from the map. More... | |
bool | map_capacity_exceeded () const |
Check if the current map capacity is exceeded. More... | |
bool | pool_capacity_exceeded () const |
Check if the pool capacity is exceeded. More... | |
void | rectify_free_list (Autolocker *lock) |
Helper function to evict unused elements from the free list until the cache capacity is not exceeded. More... | |
void | evict_all_unused (Autolocker *lock) |
Helper function to evict all unused elements from the free list and the cache. More... | |
Private Attributes | |
mysql_mutex_t | m_lock |
We need a mutex to lock this instance for thread safety, as well as a condition variable for synchronizing handling of cache misses. More... | |
mysql_cond_t | m_miss_handled |
Free_list< Cache_element< T > > | m_free_list |
std::vector< Cache_element< T > * > | m_element_pool |
size_t | m_capacity |
Static Private Attributes | |
static const size_t | initial_capacity = 256 |
Additional Inherited Members | |
Public Types inherited from dd::cache::Multi_map_base< T > | |
typedef Element_map< constT *, Cache_element< T > >::Const_iterator | Const_iterator |
typedef Element_map< constT *, Cache_element< T > >::Iterator | Iterator |
Protected Member Functions inherited from dd::cache::Multi_map_base< T > | |
template<typename K > | |
Element_map< K, Cache_element< T > > * | m_map () |
Template function to get an element map. More... | |
template<typename K > | |
const Element_map< K, Cache_element< T > > * | m_map () const |
void | remove_single_element (Cache_element< T > *element) |
Helper function to remove the mapping of a single element, without deleting the element itself. More... | |
void | add_single_element (Cache_element< T > *element) |
Helper function to add a single element. More... | |
void | dump () const |
Debug dump of the multi map base to stderr. More... | |
Implementation of a shared set of maps for a given object type.
The implementation is an extension of the Multi_map_base, and adds support for:
The pool of vacant cache elements is used to avoid deleting and allocating memory. We keep a pool of up to 'max_connections' elements. When a new element is needed, we first try to get one from the pool. If that does not succeed, a new element is allocated dynamically. In this case, the allocation will happen while the mutex is locked.
When an element and object are to be removed from the shared map and deleted, the size of the pool decides whether the element should be added to the pool or not. If it is not added to the pool, it is signed up for being deleted by the Autolocker. The element's object is always signed up for being deleted, it is never reused even if the corresponding element is added to the pool. The Autolocker is used for two purposes:
When the Autolocker is deleted, it will first unlock the mutex, then it will iterate over the elements and objects and delete them.
T | Dictionary object type. |
|
inline |
Initialize the mutex and condition variable.
Set initial map capacity.
|
inline |
Destroy the mutex and condition variable.
|
inline |
Check if an element with the given key is available.
void dd::cache::Shared_multi_map< T >::drop | ( | Cache_element< T > * | element | ) |
Delete an element from the map.
This function will remove the element from all maps, using remove(), and delete the object pointed to. This means that all keys associated with the element will be removed from the maps, and the cache element wrapper will be deleted. The object may not be accessed after calling this function.
element | Element pointer. |
template void dd::cache::Shared_multi_map< T >::drop_if_present< Resource_group::Id_key > | ( | const K & | key | ) |
Delete an object corresponding to the key from the map if exists.
This function will find the element corresponding to the key if it exists. After that it will remove the element from all maps, using remove(), and delete the object pointed to. This means that all keys associated with the element will be removed from the maps, and the cache element wrapper will be deleted.
K | Key type. |
key | Key to be checked. |
|
inline |
Debug dump of the shared multi map to stderr.
|
private |
Helper function to evict all unused elements from the free list and the cache.
Used during e.g. shutdown.
lock | Autolocker to use for signing up for auto delete. |
template bool dd::cache::Shared_multi_map< T >::get< Resource_group::Aux_key > | ( | const K & | key, |
Cache_element< T > ** | element | ||
) |
Get a wrapper element from the map handling the given key type.
If the wrapped object is present, mark it as used and return it. If the object is in the process of being loaded, wait for loading to be completed. If the object is not present, and not being loaded, mark it as missed and return.
K | Key type. |
key | Key to use for looking up the object. | |
[out] | element | Wrapper element pointer, if present, either immediately or after a cache miss handled by another thread. NULL if awakened after a miss handled by another thread, which was unable to load the object from elsewhere. NULL if the object is not present, and no other thread is missing it. |
true | If the object is missed, and this thread must handle the miss. |
false | Otherwise. |
|
inlineprivate |
Template helper function getting the element map.
Const and non-const variants.
K | Key type. |
|
inlineprivate |
|
inlineprivate |
Check if the current map capacity is exceeded.
|
inlineprivate |
Check if the pool capacity is exceeded.
We keep enough elements to let all connections have a cache miss at the same time without needing to allocate a new element, i.e., we use 'max_connections' as the pool capacity.
template void dd::cache::Shared_multi_map< T >::put< Resource_group::Aux_key > | ( | const K * | key, |
const T * | object, | ||
Cache_element< T > ** | element | ||
) |
Put a new object and element wrapper into the map.
The new object may be NULL if it does not exist in the DD tables. In that case, we must still update the missing set and broadcast, if necessary, because other threads may be waiting. Then, the element returned is also NULL.
Otherwise, we check that the actual object instance is not present in the map, then all the keys are generated based on the new object. The keys that are missed are removed from the missed set, and then we verify that all keys are either present or absent.
If no keys are present, it means we must add the new object. We remove the least recently used objects, if cache capacity is exceeded, and add the new object to the reverse map. We also mark the object as being used. The element wrapper of the new object is returned.
If all keys are present, we return the wrapper element already present and delete the newly read object.
Finally, if at least one key was missed, we broadcast that the miss has been handled.
K | Key type. |
key | Key used when loading the object. | |
object | New object to be added. | |
[out] | element | Element wrapper: NULL if the new object is NULL, otherwise the wrapper of the newly read object or the existing object with the same keys. |
|
private |
Helper function to evict unused elements from the free list until the cache capacity is not exceeded.
lock | Autolocker to use for signing up for auto delete. |
void dd::cache::Shared_multi_map< T >::release | ( | Cache_element< T > * | element | ) |
Release one element.
The element must be present and in use. If the element becomes unused, it is added to the free list, which is then rectified to enforce its capacity constraints.
element | Element pointer. |
|
private |
Remove an element from the map.
This function will remove the element from the multi map. This means that all keys associated with the element will be removed from the maps, and the cache element wrapper will be deleted. The object itself is not deleted. It is up to the outer layer to decide what to do with the object.
element | Element to be removed. |
lock | Autolocker to use for signing up for auto delete. |
void dd::cache::Shared_multi_map< T >::replace | ( | Cache_element< T > * | element, |
const T * | object | ||
) |
Replace the object and re-generate the keys for an element.
The element must be present and in use. The keys by which it is hashed are removed from the internal maps. The new keys are generated, and the element, with its new keys, is added to the maps again.
element | Element for which new keys should be generated. |
object | New object to replace the old one. |
bool dd::cache::Shared_multi_map< T >::reset | ( | THD * | thd | ) |
Reset the shared map.
Locks and deletes all objects present, but keeps the element pool and the capacity setting.
thd | Thread context. |
true | Failure, e.g. timeout from metadata lock acquisition. |
false | Otherwise. |
|
inline |
Set capacity of the shared map.
void dd::cache::Shared_multi_map< T >::shutdown |
Shutdown the shared map.
Delete all objects present.
|
private |
Template function to find an element and mark it as used.
The function looks up the appropriate element map. If the key is present in the map, the element is marked as being used and returned. If the element was in the free list, it is removed from the list.
K | Key type. |
key | Key to use for looking up the element. |
|
staticprivate |
|
private |
|
private |
|
private |
|
private |
We need a mutex to lock this instance for thread safety, as well as a condition variable for synchronizing handling of cache misses.
We provide a set of operations for using these variables.
|
private |